org.el 888 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497
  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 a 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. (unless (boundp 'calendar-view-holidays-initially-flag)
  94. (org-defvaralias 'calendar-view-holidays-initially-flag
  95. 'view-calendar-holidays-initially))
  96. (unless (boundp 'calendar-view-diary-initially-flag)
  97. (org-defvaralias 'calendar-view-diary-initially-flag
  98. 'view-diary-entries-initially))
  99. (unless (boundp 'diary-fancy-buffer)
  100. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  101. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  102. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  103. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  104. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  105. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  106. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  107. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  108. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  109. (declare-function orgtbl-mode "org-table" (&optional arg))
  110. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  111. (declare-function org-beamer-mode "ox-beamer" ())
  112. (declare-function org-table-edit-field "org-table" (arg))
  113. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  114. (declare-function org-table-set-constants "org-table" ())
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-agenda-redo "org-agenda" (&optional all))
  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. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-restriction "org-element" (element))
  145. (declare-function org-element-type "org-element" (element))
  146. ;; load languages based on value of `org-babel-load-languages'
  147. (defvar org-babel-load-languages)
  148. ;;;###autoload
  149. (defun org-babel-do-load-languages (sym value)
  150. "Load the languages defined in `org-babel-load-languages'."
  151. (set-default sym value)
  152. (mapc (lambda (pair)
  153. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  154. (if active
  155. (progn
  156. (require (intern (concat "ob-" lang))))
  157. (progn
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-execute:" lang)))
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-expand-body:" lang)))))))
  162. org-babel-load-languages))
  163. (defcustom org-babel-load-languages '((emacs-lisp . t))
  164. "Languages which can be evaluated in Org-mode buffers.
  165. This list can be used to load support for any of the languages
  166. below, note that each language will depend on a different set of
  167. system executables and/or Emacs modes. When a language is
  168. \"loaded\", then code blocks in that language can be evaluated
  169. with `org-babel-execute-src-block' bound by default to C-c
  170. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  171. be set to remove code block evaluation from the C-c C-c
  172. keybinding. By default only Emacs Lisp (which has no
  173. requirements) is loaded."
  174. :group 'org-babel
  175. :set 'org-babel-do-load-languages
  176. :version "24.1"
  177. :type '(alist :tag "Babel Languages"
  178. :key-type
  179. (choice
  180. (const :tag "Awk" awk)
  181. (const :tag "C" C)
  182. (const :tag "R" R)
  183. (const :tag "Asymptote" asymptote)
  184. (const :tag "Calc" calc)
  185. (const :tag "Clojure" clojure)
  186. (const :tag "CSS" css)
  187. (const :tag "Ditaa" ditaa)
  188. (const :tag "Dot" dot)
  189. (const :tag "Emacs Lisp" emacs-lisp)
  190. (const :tag "Fortran" fortran)
  191. (const :tag "Gnuplot" gnuplot)
  192. (const :tag "Haskell" haskell)
  193. (const :tag "IO" io)
  194. (const :tag "Java" java)
  195. (const :tag "Javascript" js)
  196. (const :tag "LaTeX" latex)
  197. (const :tag "Ledger" ledger)
  198. (const :tag "Lilypond" lilypond)
  199. (const :tag "Lisp" lisp)
  200. (const :tag "Makefile" makefile)
  201. (const :tag "Maxima" maxima)
  202. (const :tag "Matlab" matlab)
  203. (const :tag "Mscgen" mscgen)
  204. (const :tag "Ocaml" ocaml)
  205. (const :tag "Octave" octave)
  206. (const :tag "Org" org)
  207. (const :tag "Perl" perl)
  208. (const :tag "Pico Lisp" picolisp)
  209. (const :tag "PlantUML" plantuml)
  210. (const :tag "Python" python)
  211. (const :tag "Ruby" ruby)
  212. (const :tag "Sass" sass)
  213. (const :tag "Scala" scala)
  214. (const :tag "Scheme" scheme)
  215. (const :tag "Screen" screen)
  216. (const :tag "Shell Script" sh)
  217. (const :tag "Shen" shen)
  218. (const :tag "Sql" sql)
  219. (const :tag "Sqlite" sqlite))
  220. :value-type (boolean :tag "Activate" :value t)))
  221. ;;;; Customization variables
  222. (defcustom org-clone-delete-id nil
  223. "Remove ID property of clones of a subtree.
  224. When non-nil, clones of a subtree don't inherit the ID property.
  225. Otherwise they inherit the ID property with a new unique
  226. identifier."
  227. :type 'boolean
  228. :version "24.1"
  229. :group 'org-id)
  230. ;;; Version
  231. (org-check-version)
  232. ;;;###autoload
  233. (defun org-version (&optional here full message)
  234. "Show the org-mode version in the echo area.
  235. With prefix argument HERE, insert it at point.
  236. When FULL is non-nil, use a verbose version string.
  237. When MESSAGE is non-nil, display a message with the version."
  238. (interactive "P")
  239. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  240. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  241. (load-suffixes (list ".el"))
  242. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  243. (org-trash (or
  244. (and (fboundp 'org-release) (fboundp 'org-git-version))
  245. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  246. (load-suffixes save-load-suffixes)
  247. (org-version (org-release))
  248. (git-version (org-git-version))
  249. (version (format "Org-mode version %s (%s @ %s)"
  250. org-version
  251. git-version
  252. (if org-install-dir
  253. (if (string= org-dir org-install-dir)
  254. org-install-dir
  255. (concat "mixed installation! " org-install-dir " and " org-dir))
  256. "org-loaddefs.el can not be found!")))
  257. (_version (if full version org-version)))
  258. (if (org-called-interactively-p 'interactive)
  259. (if here
  260. (insert version)
  261. (message version))
  262. (if message (message _version))
  263. _version)))
  264. (defconst org-version (org-version))
  265. ;;; Compatibility constants
  266. ;;; The custom variables
  267. (defgroup org nil
  268. "Outline-based notes management and organizer."
  269. :tag "Org"
  270. :group 'outlines
  271. :group 'calendar)
  272. (defcustom org-mode-hook nil
  273. "Mode hook for Org-mode, run after the mode was turned on."
  274. :group 'org
  275. :type 'hook)
  276. (defcustom org-load-hook nil
  277. "Hook that is run after org.el has been loaded."
  278. :group 'org
  279. :type 'hook)
  280. (defcustom org-log-buffer-setup-hook nil
  281. "Hook that is run after an Org log buffer is created."
  282. :group 'org
  283. :version "24.1"
  284. :type 'hook)
  285. (defvar org-modules) ; defined below
  286. (defvar org-modules-loaded nil
  287. "Have the modules been loaded already?")
  288. (defun org-load-modules-maybe (&optional force)
  289. "Load all extensions listed in `org-modules'."
  290. (when (or force (not org-modules-loaded))
  291. (mapc (lambda (ext)
  292. (condition-case nil (require ext)
  293. (error (message "Problems while trying to load feature `%s'" ext))))
  294. org-modules)
  295. (setq org-modules-loaded t)))
  296. (defun org-set-modules (var value)
  297. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  298. (set var value)
  299. (when (featurep 'org)
  300. (org-load-modules-maybe 'force)))
  301. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  302. "Modules that should always be loaded together with org.el.
  303. If a description starts with <C>, the file is not part of Emacs
  304. and loading it will require that you have downloaded and properly
  305. installed the Org mode distribution.
  306. You can also use this system to load external packages (i.e. neither Org
  307. core modules, nor modules from the CONTRIB directory). Just add symbols
  308. to the end of the list. If the package is called org-xyz.el, then you need
  309. to add the symbol `xyz', and the package must have a call to:
  310. \(provide 'org-xyz)
  311. For export specific modules, see also `org-export-backends'."
  312. :group 'org
  313. :set 'org-set-modules
  314. :version "24.4"
  315. :package-version '(Org . "8.0")
  316. :type
  317. '(set :greedy t
  318. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  319. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  320. (const :tag " crypt: Encryption of subtrees" org-crypt)
  321. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  322. (const :tag " docview: Links to doc-view buffers" org-docview)
  323. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  324. (const :tag " id: Global IDs for identifying entries" org-id)
  325. (const :tag " info: Links to Info nodes" org-info)
  326. (const :tag " habit: Track your consistency with habits" org-habit)
  327. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  328. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  329. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  330. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  331. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  332. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  333. (const :tag " mouse: Additional mouse support" org-mouse)
  334. (const :tag "C vm: Links to VM folders/messages" org-vm)
  335. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  336. (const :tag "C w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  337. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  338. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  339. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  340. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  341. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  342. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  343. (const :tag "C collector: Collect properties into tables" org-collector)
  344. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  345. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  346. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  347. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  348. (const :tag "C eval: Include command output as text" org-eval)
  349. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  350. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  351. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  352. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  353. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  354. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  355. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  356. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  357. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  358. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  359. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  360. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  361. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  362. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  363. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  364. (const :tag "C registry: A registry for Org-mode links" org-registry)
  365. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  366. (const :tag "C secretary: Team management with org-mode" org-secretary)
  367. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  368. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  369. (const :tag "C track: Keep up with Org-mode development" org-track)
  370. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  371. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  372. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  373. (defvar org-export-registered-backends) ; From ox.el
  374. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  375. (defcustom org-export-backends '(ascii html icalendar latex)
  376. "List of export back-ends that should be always available.
  377. If a description starts with <C>, the file is not part of Emacs
  378. and loading it will require that you have downloaded and properly
  379. installed the Org mode distribution.
  380. Unlike to `org-modules', libraries in this list will not be
  381. loaded along with Org, but only once the export framework is
  382. needed.
  383. This variable needs to be set before org.el is loaded. If you
  384. need to make a change while Emacs is running, use the customize
  385. interface or run the following code, where VALUE stands for the
  386. new value of the variable, after updating it:
  387. \(progn
  388. \(setq org-export-registered-backends
  389. \(org-remove-if-not
  390. \(lambda (backend)
  391. \(or (memq backend val)
  392. \(catch 'parentp
  393. \(mapc
  394. \(lambda (b)
  395. \(and (org-export-derived-backend-p b (car backend))
  396. \(throw 'parentp t)))
  397. val)
  398. nil)))
  399. org-export-registered-backends))
  400. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  401. \(dolist (backend val)
  402. \(cond
  403. \((not (load (format \"ox-%s\" backend) t t))
  404. \(message \"Problems while trying to load export back-end `%s'\"
  405. backend))
  406. \((not (memq backend new-list)) (push backend new-list))))
  407. \(set-default var new-list)))
  408. Adding a back-end to this list will also pull the back-end it
  409. depends on, if any."
  410. :group 'org
  411. :group 'org-export
  412. :version "24.4"
  413. :package-version '(Org . "8.0")
  414. :initialize 'custom-initialize-set
  415. :set (lambda (var val)
  416. (if (not (featurep 'ox)) (set-default var val)
  417. ;; Any back-end not required anymore (not present in VAL and not
  418. ;; a parent of any back-end in the new value) is removed from the
  419. ;; list of registered back-ends.
  420. (setq org-export-registered-backends
  421. (org-remove-if-not
  422. (lambda (backend)
  423. (or (memq backend val)
  424. (catch 'parentp
  425. (mapc
  426. (lambda (b)
  427. (and (org-export-derived-backend-p b (car backend))
  428. (throw 'parentp t)))
  429. val)
  430. nil)))
  431. org-export-registered-backends))
  432. ;; Now build NEW-LIST of both new back-ends and required
  433. ;; parents.
  434. (let ((new-list (mapcar 'car org-export-registered-backends)))
  435. (dolist (backend val)
  436. (cond
  437. ((not (load (format "ox-%s" backend) t t))
  438. (message "Problems while trying to load export back-end `%s'"
  439. backend))
  440. ((not (memq backend new-list)) (push backend new-list))))
  441. ;; Set VAR to that list with fixed dependencies.
  442. (set-default var new-list))))
  443. :type '(set :greedy t
  444. (const :tag " ascii Export buffer to ASCII format" ascii)
  445. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  446. (const :tag " html Export buffer to HTML format" html)
  447. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  448. (const :tag " latex Export buffer to LaTeX format" latex)
  449. (const :tag " man Export buffer to MAN format" man)
  450. (const :tag " md Export buffer to Markdown format" md)
  451. (const :tag " odt Export buffer to ODT format" odt)
  452. (const :tag " org Export buffer to Org format" org)
  453. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  454. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  455. (const :tag "C deck Export buffer to deck.js presentations" deck)
  456. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  457. (const :tag "C groff Export buffer to Groff format" groff)
  458. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  459. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  460. (const :tag "C s5 Export buffer to s5 presentations" s5)
  461. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  462. (eval-after-load 'ox
  463. '(mapc
  464. (lambda (backend)
  465. (condition-case nil (require (intern (format "ox-%s" backend)))
  466. (error (message "Problems while trying to load export back-end `%s'"
  467. backend))))
  468. org-export-backends))
  469. (defcustom org-support-shift-select nil
  470. "Non-nil means make shift-cursor commands select text when possible.
  471. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  472. start selecting a region, or enlarge regions started in this way.
  473. In Org-mode, in special contexts, these same keys are used for
  474. other purposes, important enough to compete with shift selection.
  475. Org tries to balance these needs by supporting `shift-select-mode'
  476. outside these special contexts, under control of this variable.
  477. The default of this variable is nil, to avoid confusing behavior. Shifted
  478. cursor keys will then execute Org commands in the following contexts:
  479. - on a headline, changing TODO state (left/right) and priority (up/down)
  480. - on a time stamp, changing the time
  481. - in a plain list item, changing the bullet type
  482. - in a property definition line, switching between allowed values
  483. - in the BEGIN line of a clock table (changing the time block).
  484. Outside these contexts, the commands will throw an error.
  485. When this variable is t and the cursor is not in a special
  486. context, Org-mode will support shift-selection for making and
  487. enlarging regions. To make this more effective, the bullet
  488. cycling will no longer happen anywhere in an item line, but only
  489. if the cursor is exactly on the bullet.
  490. If you set this variable to the symbol `always', then the keys
  491. will not be special in headlines, property lines, and item lines,
  492. to make shift selection work there as well. If this is what you
  493. want, you can use the following alternative commands: `C-c C-t'
  494. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  495. can be used to switch TODO sets, `C-c -' to cycle item bullet
  496. types, and properties can be edited by hand or in column view.
  497. However, when the cursor is on a timestamp, shift-cursor commands
  498. will still edit the time stamp - this is just too good to give up.
  499. XEmacs user should have this variable set to nil, because
  500. `shift-select-mode' is in Emacs 23 or later only."
  501. :group 'org
  502. :type '(choice
  503. (const :tag "Never" nil)
  504. (const :tag "When outside special context" t)
  505. (const :tag "Everywhere except timestamps" always)))
  506. (defcustom org-loop-over-headlines-in-active-region nil
  507. "Shall some commands act upon headlines in the active region?
  508. When set to `t', some commands will be performed in all headlines
  509. within the active region.
  510. When set to `start-level', some commands will be performed in all
  511. headlines within the active region, provided that these headlines
  512. are of the same level than the first one.
  513. When set to a string, those commands will be performed on the
  514. matching headlines within the active region. Such string must be
  515. a tags/property/todo match as it is used in the agenda tags view.
  516. The list of commands is: `org-schedule', `org-deadline',
  517. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  518. `org-archive-to-archive-sibling'. The archiving commands skip
  519. already archived entries."
  520. :type '(choice (const :tag "Don't loop" nil)
  521. (const :tag "All headlines in active region" t)
  522. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  523. (string :tag "Tags/Property/Todo matcher"))
  524. :version "24.1"
  525. :group 'org-todo
  526. :group 'org-archive)
  527. (defgroup org-startup nil
  528. "Options concerning startup of Org-mode."
  529. :tag "Org Startup"
  530. :group 'org)
  531. (defcustom org-startup-folded t
  532. "Non-nil means entering Org-mode will switch to OVERVIEW.
  533. This can also be configured on a per-file basis by adding one of
  534. the following lines anywhere in the buffer:
  535. #+STARTUP: fold (or `overview', this is equivalent)
  536. #+STARTUP: nofold (or `showall', this is equivalent)
  537. #+STARTUP: content
  538. #+STARTUP: showeverything
  539. By default, this option is ignored when Org opens agenda files
  540. for the first time. If you want the agenda to honor the startup
  541. option, set `org-agenda-inhibit-startup' to nil."
  542. :group 'org-startup
  543. :type '(choice
  544. (const :tag "nofold: show all" nil)
  545. (const :tag "fold: overview" t)
  546. (const :tag "content: all headlines" content)
  547. (const :tag "show everything, even drawers" showeverything)))
  548. (defcustom org-startup-truncated t
  549. "Non-nil means entering Org-mode will set `truncate-lines'.
  550. This is useful since some lines containing links can be very long and
  551. uninteresting. Also tables look terrible when wrapped."
  552. :group 'org-startup
  553. :type 'boolean)
  554. (defcustom org-startup-indented nil
  555. "Non-nil means turn on `org-indent-mode' on startup.
  556. This can also be configured on a per-file basis by adding one of
  557. the following lines anywhere in the buffer:
  558. #+STARTUP: indent
  559. #+STARTUP: noindent"
  560. :group 'org-structure
  561. :type '(choice
  562. (const :tag "Not" nil)
  563. (const :tag "Globally (slow on startup in large files)" t)))
  564. (defcustom org-use-sub-superscripts t
  565. "Non-nil means interpret \"_\" and \"^\" for display.
  566. When this option is turned on, you can use TeX-like syntax for sub- and
  567. superscripts. Several characters after \"_\" or \"^\" will be
  568. considered as a single item - so grouping with {} is normally not
  569. needed. For example, the following things will be parsed as single
  570. sub- or superscripts.
  571. 10^24 or 10^tau several digits will be considered 1 item.
  572. 10^-12 or 10^-tau a leading sign with digits or a word
  573. x^2-y^3 will be read as x^2 - y^3, because items are
  574. terminated by almost any nonword/nondigit char.
  575. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  576. Still, ambiguity is possible - so when in doubt use {} to enclose
  577. the sub/superscript. If you set this variable to the symbol
  578. `{}', the braces are *required* in order to trigger
  579. interpretations as sub/superscript. This can be helpful in
  580. documents that need \"_\" frequently in plain text."
  581. :group 'org-startup
  582. :version "24.1"
  583. :type '(choice
  584. (const :tag "Always interpret" t)
  585. (const :tag "Only with braces" {})
  586. (const :tag "Never interpret" nil)))
  587. (defcustom org-startup-with-beamer-mode nil
  588. "Non-nil means turn on `org-beamer-mode' on startup.
  589. This can also be configured on a per-file basis by adding one of
  590. the following lines anywhere in the buffer:
  591. #+STARTUP: beamer"
  592. :group 'org-startup
  593. :version "24.1"
  594. :type 'boolean)
  595. (defcustom org-startup-align-all-tables nil
  596. "Non-nil means align all tables when visiting a file.
  597. This is useful when the column width in tables is forced with <N> cookies
  598. in table fields. Such tables will look correct only after the first re-align.
  599. This can also be configured on a per-file basis by adding one of
  600. the following lines anywhere in the buffer:
  601. #+STARTUP: align
  602. #+STARTUP: noalign"
  603. :group 'org-startup
  604. :type 'boolean)
  605. (defcustom org-startup-with-inline-images nil
  606. "Non-nil means show inline images when loading a new Org file.
  607. This can also be configured on a per-file basis by adding one of
  608. the following lines anywhere in the buffer:
  609. #+STARTUP: inlineimages
  610. #+STARTUP: noinlineimages"
  611. :group 'org-startup
  612. :version "24.1"
  613. :type 'boolean)
  614. (defcustom org-startup-with-latex-preview nil
  615. "Non-nil means preview LaTeX fragments when loading a new Org file.
  616. This can also be configured on a per-file basis by adding one of
  617. the followinglines anywhere in the buffer:
  618. #+STARTUP: latexpreview
  619. #+STARTUP: nolatexpreview"
  620. :group 'org-startup
  621. :version "24.4"
  622. :package-version '(Org . "8.0")
  623. :type 'boolean)
  624. (defcustom org-insert-mode-line-in-empty-file nil
  625. "Non-nil means insert the first line setting Org-mode in empty files.
  626. When the function `org-mode' is called interactively in an empty file, this
  627. normally means that the file name does not automatically trigger Org-mode.
  628. To ensure that the file will always be in Org-mode in the future, a
  629. line enforcing Org-mode will be inserted into the buffer, if this option
  630. has been set."
  631. :group 'org-startup
  632. :type 'boolean)
  633. (defcustom org-replace-disputed-keys nil
  634. "Non-nil means use alternative key bindings for some keys.
  635. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  636. These keys are also used by other packages like shift-selection-mode'
  637. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  638. If you want to use Org-mode together with one of these other modes,
  639. or more generally if you would like to move some Org-mode commands to
  640. other keys, set this variable and configure the keys with the variable
  641. `org-disputed-keys'.
  642. This option is only relevant at load-time of Org-mode, and must be set
  643. *before* org.el is loaded. Changing it requires a restart of Emacs to
  644. become effective."
  645. :group 'org-startup
  646. :type 'boolean)
  647. (defcustom org-use-extra-keys nil
  648. "Non-nil means use extra key sequence definitions for certain commands.
  649. This happens automatically if you run XEmacs or if `window-system'
  650. is nil. This variable lets you do the same manually. You must
  651. set it before loading org.
  652. Example: on Carbon Emacs 22 running graphically, with an external
  653. keyboard on a Powerbook, the default way of setting M-left might
  654. not work for either Alt or ESC. Setting this variable will make
  655. it work for ESC."
  656. :group 'org-startup
  657. :type 'boolean)
  658. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  659. (defcustom org-disputed-keys
  660. '(([(shift up)] . [(meta p)])
  661. ([(shift down)] . [(meta n)])
  662. ([(shift left)] . [(meta -)])
  663. ([(shift right)] . [(meta +)])
  664. ([(control shift right)] . [(meta shift +)])
  665. ([(control shift left)] . [(meta shift -)]))
  666. "Keys for which Org-mode and other modes compete.
  667. This is an alist, cars are the default keys, second element specifies
  668. the alternative to use when `org-replace-disputed-keys' is t.
  669. Keys can be specified in any syntax supported by `define-key'.
  670. The value of this option takes effect only at Org-mode's startup,
  671. therefore you'll have to restart Emacs to apply it after changing."
  672. :group 'org-startup
  673. :type 'alist)
  674. (defun org-key (key)
  675. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  676. Or return the original if not disputed.
  677. Also apply the translations defined in `org-xemacs-key-equivalents'."
  678. (when org-replace-disputed-keys
  679. (let* ((nkey (key-description key))
  680. (x (org-find-if (lambda (x)
  681. (equal (key-description (car x)) nkey))
  682. org-disputed-keys)))
  683. (setq key (if x (cdr x) key))))
  684. (when (featurep 'xemacs)
  685. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  686. key)
  687. (defun org-find-if (predicate seq)
  688. (catch 'exit
  689. (while seq
  690. (if (funcall predicate (car seq))
  691. (throw 'exit (car seq))
  692. (pop seq)))))
  693. (defun org-defkey (keymap key def)
  694. "Define a key, possibly translated, as returned by `org-key'."
  695. (define-key keymap (org-key key) def))
  696. (defcustom org-ellipsis nil
  697. "The ellipsis to use in the Org-mode outline.
  698. When nil, just use the standard three dots. When a string, use that instead,
  699. When a face, use the standard 3 dots, but with the specified face.
  700. The change affects only Org-mode (which will then use its own display table).
  701. Changing this requires executing `M-x org-mode' in a buffer to become
  702. effective."
  703. :group 'org-startup
  704. :type '(choice (const :tag "Default" nil)
  705. (face :tag "Face" :value org-warning)
  706. (string :tag "String" :value "...#")))
  707. (defvar org-display-table nil
  708. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  709. (defgroup org-keywords nil
  710. "Keywords in Org-mode."
  711. :tag "Org Keywords"
  712. :group 'org)
  713. (defcustom org-deadline-string "DEADLINE:"
  714. "String to mark deadline entries.
  715. A deadline is this string, followed by a time stamp. Should be a word,
  716. terminated by a colon. You can insert a schedule keyword and
  717. a timestamp with \\[org-deadline].
  718. Changes become only effective after restarting Emacs."
  719. :group 'org-keywords
  720. :type 'string)
  721. (defcustom org-scheduled-string "SCHEDULED:"
  722. "String to mark scheduled TODO entries.
  723. A schedule is this string, followed by a time stamp. Should be a word,
  724. terminated by a colon. You can insert a schedule keyword and
  725. a timestamp with \\[org-schedule].
  726. Changes become only effective after restarting Emacs."
  727. :group 'org-keywords
  728. :type 'string)
  729. (defcustom org-closed-string "CLOSED:"
  730. "String used as the prefix for timestamps logging closing a TODO entry."
  731. :group 'org-keywords
  732. :type 'string)
  733. (defcustom org-clock-string "CLOCK:"
  734. "String used as prefix for timestamps clocking work hours on an item."
  735. :group 'org-keywords
  736. :type 'string)
  737. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  738. org-scheduled-string "\\|"
  739. org-deadline-string "\\|"
  740. org-closed-string "\\|"
  741. org-clock-string "\\)")
  742. "Matches a line with planning or clock info.")
  743. (defcustom org-comment-string "COMMENT"
  744. "Entries starting with this keyword will never be exported.
  745. An entry can be toggled between COMMENT and normal with
  746. \\[org-toggle-comment].
  747. Changes become only effective after restarting Emacs."
  748. :group 'org-keywords
  749. :type 'string)
  750. (defcustom org-quote-string "QUOTE"
  751. "Entries starting with this keyword will be exported in fixed-width font.
  752. Quoting applies only to the text in the entry following the headline, and does
  753. not extend beyond the next headline, even if that is lower level.
  754. An entry can be toggled between QUOTE and normal with
  755. \\[org-toggle-fixed-width-section]."
  756. :group 'org-keywords
  757. :type 'string)
  758. (defconst org-repeat-re
  759. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  760. "Regular expression for specifying repeated events.
  761. After a match, group 1 contains the repeat expression.")
  762. (defgroup org-structure nil
  763. "Options concerning the general structure of Org-mode files."
  764. :tag "Org Structure"
  765. :group 'org)
  766. (defgroup org-reveal-location nil
  767. "Options about how to make context of a location visible."
  768. :tag "Org Reveal Location"
  769. :group 'org-structure)
  770. (defconst org-context-choice
  771. '(choice
  772. (const :tag "Always" t)
  773. (const :tag "Never" nil)
  774. (repeat :greedy t :tag "Individual contexts"
  775. (cons
  776. (choice :tag "Context"
  777. (const agenda)
  778. (const org-goto)
  779. (const occur-tree)
  780. (const tags-tree)
  781. (const link-search)
  782. (const mark-goto)
  783. (const bookmark-jump)
  784. (const isearch)
  785. (const default))
  786. (boolean))))
  787. "Contexts for the reveal options.")
  788. (defcustom org-show-hierarchy-above '((default . t))
  789. "Non-nil means show full hierarchy when revealing a location.
  790. Org-mode often shows locations in an org-mode file which might have
  791. been invisible before. When this is set, the hierarchy of headings
  792. above the exposed location is shown.
  793. Turning this off for example for sparse trees makes them very compact.
  794. Instead of t, this can also be an alist specifying this option for different
  795. contexts. Valid contexts are
  796. agenda when exposing an entry from the agenda
  797. org-goto when using the command `org-goto' on key C-c C-j
  798. occur-tree when using the command `org-occur' on key C-c /
  799. tags-tree when constructing a sparse tree based on tags matches
  800. link-search when exposing search matches associated with a link
  801. mark-goto when exposing the jump goal of a mark
  802. bookmark-jump when exposing a bookmark location
  803. isearch when exiting from an incremental search
  804. default default for all contexts not set explicitly"
  805. :group 'org-reveal-location
  806. :type org-context-choice)
  807. (defcustom org-show-following-heading '((default . nil))
  808. "Non-nil means show following heading when revealing a location.
  809. Org-mode often shows locations in an org-mode file which might have
  810. been invisible before. When this is set, the heading following the
  811. match is shown.
  812. Turning this off for example for sparse trees makes them very compact,
  813. but makes it harder to edit the location of the match. In such a case,
  814. use the command \\[org-reveal] to show more context.
  815. Instead of t, this can also be an alist specifying this option for different
  816. contexts. See `org-show-hierarchy-above' for valid contexts."
  817. :group 'org-reveal-location
  818. :type org-context-choice)
  819. (defcustom org-show-siblings '((default . nil) (isearch t))
  820. "Non-nil means show all sibling heading when revealing a location.
  821. Org-mode often shows locations in an org-mode file which might have
  822. been invisible before. When this is set, the sibling of the current entry
  823. heading are all made visible. If `org-show-hierarchy-above' is t,
  824. the same happens on each level of the hierarchy above the current entry.
  825. By default this is on for the isearch context, off for all other contexts.
  826. Turning this off for example for sparse trees makes them very compact,
  827. but makes it harder to edit the location of the match. In such a case,
  828. use the command \\[org-reveal] to show more context.
  829. Instead of t, this can also be an alist specifying this option for different
  830. contexts. See `org-show-hierarchy-above' for valid contexts."
  831. :group 'org-reveal-location
  832. :type org-context-choice)
  833. (defcustom org-show-entry-below '((default . nil))
  834. "Non-nil means show the entry below a headline when revealing a location.
  835. Org-mode often shows locations in an org-mode file which might have
  836. been invisible before. When this is set, the text below the headline that is
  837. exposed is also shown.
  838. By default this is off for all contexts.
  839. Instead of t, this can also be an alist specifying this option for different
  840. contexts. See `org-show-hierarchy-above' for valid contexts."
  841. :group 'org-reveal-location
  842. :type org-context-choice)
  843. (defcustom org-indirect-buffer-display 'other-window
  844. "How should indirect tree buffers be displayed?
  845. This applies to indirect buffers created with the commands
  846. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  847. Valid values are:
  848. current-window Display in the current window
  849. other-window Just display in another window.
  850. dedicated-frame Create one new frame, and re-use it each time.
  851. new-frame Make a new frame each time. Note that in this case
  852. previously-made indirect buffers are kept, and you need to
  853. kill these buffers yourself."
  854. :group 'org-structure
  855. :group 'org-agenda-windows
  856. :type '(choice
  857. (const :tag "In current window" current-window)
  858. (const :tag "In current frame, other window" other-window)
  859. (const :tag "Each time a new frame" new-frame)
  860. (const :tag "One dedicated frame" dedicated-frame)))
  861. (defcustom org-use-speed-commands nil
  862. "Non-nil means activate single letter commands at beginning of a headline.
  863. This may also be a function to test for appropriate locations where speed
  864. commands should be active."
  865. :group 'org-structure
  866. :type '(choice
  867. (const :tag "Never" nil)
  868. (const :tag "At beginning of headline stars" t)
  869. (function)))
  870. (defcustom org-speed-commands-user nil
  871. "Alist of additional speed commands.
  872. This list will be checked before `org-speed-commands-default'
  873. when the variable `org-use-speed-commands' is non-nil
  874. and when the cursor is at the beginning of a headline.
  875. The car if each entry is a string with a single letter, which must
  876. be assigned to `self-insert-command' in the global map.
  877. The cdr is either a command to be called interactively, a function
  878. to be called, or a form to be evaluated.
  879. An entry that is just a list with a single string will be interpreted
  880. as a descriptive headline that will be added when listing the speed
  881. commands in the Help buffer using the `?' speed command."
  882. :group 'org-structure
  883. :type '(repeat :value ("k" . ignore)
  884. (choice :value ("k" . ignore)
  885. (list :tag "Descriptive Headline" (string :tag "Headline"))
  886. (cons :tag "Letter and Command"
  887. (string :tag "Command letter")
  888. (choice
  889. (function)
  890. (sexp))))))
  891. (defgroup org-cycle nil
  892. "Options concerning visibility cycling in Org-mode."
  893. :tag "Org Cycle"
  894. :group 'org-structure)
  895. (defcustom org-cycle-skip-children-state-if-no-children t
  896. "Non-nil means skip CHILDREN state in entries that don't have any."
  897. :group 'org-cycle
  898. :type 'boolean)
  899. (defcustom org-cycle-max-level nil
  900. "Maximum level which should still be subject to visibility cycling.
  901. Levels higher than this will, for cycling, be treated as text, not a headline.
  902. When `org-odd-levels-only' is set, a value of N in this variable actually
  903. means 2N-1 stars as the limiting headline.
  904. When nil, cycle all levels.
  905. Note that the limiting level of cycling is also influenced by
  906. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  907. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  908. than its value."
  909. :group 'org-cycle
  910. :type '(choice
  911. (const :tag "No limit" nil)
  912. (integer :tag "Maximum level")))
  913. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  914. "Names of drawers. Drawers are not opened by cycling on the headline above.
  915. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  916. this:
  917. :DRAWERNAME:
  918. .....
  919. :END:
  920. The drawer \"PROPERTIES\" is special for capturing properties through
  921. the property API.
  922. Drawers can be defined on the per-file basis with a line like:
  923. #+DRAWERS: HIDDEN STATE PROPERTIES"
  924. :group 'org-structure
  925. :group 'org-cycle
  926. :type '(repeat (string :tag "Drawer Name")))
  927. (defcustom org-hide-block-startup nil
  928. "Non-nil means entering Org-mode will fold all blocks.
  929. This can also be set in on a per-file basis with
  930. #+STARTUP: hideblocks
  931. #+STARTUP: showblocks"
  932. :group 'org-startup
  933. :group 'org-cycle
  934. :type 'boolean)
  935. (defcustom org-cycle-global-at-bob nil
  936. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  937. This makes it possible to do global cycling without having to use S-TAB or
  938. \\[universal-argument] TAB. For this special case to work, the first line
  939. of the buffer must not be a headline -- it may be empty or some other text.
  940. When used in this way, `org-cycle-hook' is disabled temporarily to make
  941. sure the cursor stays at the beginning of the buffer. When this option is
  942. nil, don't do anything special at the beginning of the buffer."
  943. :group 'org-cycle
  944. :type 'boolean)
  945. (defcustom org-cycle-level-after-item/entry-creation t
  946. "Non-nil means cycle entry level or item indentation in new empty entries.
  947. When the cursor is at the end of an empty headline, i.e., with only stars
  948. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  949. and then all possible ancestor states, before returning to the original state.
  950. This makes data entry extremely fast: M-RET to create a new headline,
  951. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  952. When the cursor is at the end of an empty plain list item, one TAB will
  953. make it a subitem, two or more tabs will back up to make this an item
  954. higher up in the item hierarchy."
  955. :group 'org-cycle
  956. :type 'boolean)
  957. (defcustom org-cycle-emulate-tab t
  958. "Where should `org-cycle' emulate TAB.
  959. nil Never
  960. white Only in completely white lines
  961. whitestart Only at the beginning of lines, before the first non-white char
  962. t Everywhere except in headlines
  963. exc-hl-bol Everywhere except at the start of a headline
  964. If TAB is used in a place where it does not emulate TAB, the current subtree
  965. visibility is cycled."
  966. :group 'org-cycle
  967. :type '(choice (const :tag "Never" nil)
  968. (const :tag "Only in completely white lines" white)
  969. (const :tag "Before first char in a line" whitestart)
  970. (const :tag "Everywhere except in headlines" t)
  971. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  972. ))
  973. (defcustom org-cycle-separator-lines 2
  974. "Number of empty lines needed to keep an empty line between collapsed trees.
  975. If you leave an empty line between the end of a subtree and the following
  976. headline, this empty line is hidden when the subtree is folded.
  977. Org-mode will leave (exactly) one empty line visible if the number of
  978. empty lines is equal or larger to the number given in this variable.
  979. So the default 2 means at least 2 empty lines after the end of a subtree
  980. are needed to produce free space between a collapsed subtree and the
  981. following headline.
  982. If the number is negative, and the number of empty lines is at least -N,
  983. all empty lines are shown.
  984. Special case: when 0, never leave empty lines in collapsed view."
  985. :group 'org-cycle
  986. :type 'integer)
  987. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  988. (defcustom org-pre-cycle-hook nil
  989. "Hook that is run before visibility cycling is happening.
  990. The function(s) in this hook must accept a single argument which indicates
  991. the new state that will be set right after running this hook. The
  992. argument is a symbol. Before a global state change, it can have the values
  993. `overview', `content', or `all'. Before a local state change, it can have
  994. the values `folded', `children', or `subtree'."
  995. :group 'org-cycle
  996. :type 'hook)
  997. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  998. org-cycle-hide-drawers
  999. org-cycle-hide-inline-tasks
  1000. org-cycle-show-empty-lines
  1001. org-optimize-window-after-visibility-change)
  1002. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1003. The function(s) in this hook must accept a single argument which indicates
  1004. the new state that was set by the most recent `org-cycle' command. The
  1005. argument is a symbol. After a global state change, it can have the values
  1006. `overview', `contents', or `all'. After a local state change, it can have
  1007. the values `folded', `children', or `subtree'."
  1008. :group 'org-cycle
  1009. :type 'hook)
  1010. (defgroup org-edit-structure nil
  1011. "Options concerning structure editing in Org-mode."
  1012. :tag "Org Edit Structure"
  1013. :group 'org-structure)
  1014. (defcustom org-odd-levels-only nil
  1015. "Non-nil means skip even levels and only use odd levels for the outline.
  1016. This has the effect that two stars are being added/taken away in
  1017. promotion/demotion commands. It also influences how levels are
  1018. handled by the exporters.
  1019. Changing it requires restart of `font-lock-mode' to become effective
  1020. for fontification also in regions already fontified.
  1021. You may also set this on a per-file basis by adding one of the following
  1022. lines to the buffer:
  1023. #+STARTUP: odd
  1024. #+STARTUP: oddeven"
  1025. :group 'org-edit-structure
  1026. :group 'org-appearance
  1027. :type 'boolean)
  1028. (defcustom org-adapt-indentation t
  1029. "Non-nil means adapt indentation to outline node level.
  1030. When this variable is set, Org assumes that you write outlines by
  1031. indenting text in each node to align with the headline (after the stars).
  1032. The following issues are influenced by this variable:
  1033. - When this is set and the *entire* text in an entry is indented, the
  1034. indentation is increased by one space in a demotion command, and
  1035. decreased by one in a promotion command. If any line in the entry
  1036. body starts with text at column 0, indentation is not changed at all.
  1037. - Property drawers and planning information is inserted indented when
  1038. this variable s set. When nil, they will not be indented.
  1039. - TAB indents a line relative to context. The lines below a headline
  1040. will be indented when this variable is set.
  1041. Note that this is all about true indentation, by adding and removing
  1042. space characters. See also `org-indent.el' which does level-dependent
  1043. indentation in a virtual way, i.e. at display time in Emacs."
  1044. :group 'org-edit-structure
  1045. :type 'boolean)
  1046. (defcustom org-special-ctrl-a/e nil
  1047. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1048. When t, `C-a' will bring back the cursor to the beginning of the
  1049. headline text, i.e. after the stars and after a possible TODO
  1050. keyword. In an item, this will be the position after bullet and
  1051. check-box, if any. When the cursor is already at that position,
  1052. another `C-a' will bring it to the beginning of the line.
  1053. `C-e' will jump to the end of the headline, ignoring the presence
  1054. of tags in the headline. A second `C-e' will then jump to the
  1055. true end of the line, after any tags. This also means that, when
  1056. this variable is non-nil, `C-e' also will never jump beyond the
  1057. end of the heading of a folded section, i.e. not after the
  1058. ellipses.
  1059. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1060. normally, going to the true line boundary first. Only a directly
  1061. following, identical keypress will bring the cursor to the
  1062. special positions.
  1063. This may also be a cons cell where the behavior for `C-a' and
  1064. `C-e' is set separately."
  1065. :group 'org-edit-structure
  1066. :type '(choice
  1067. (const :tag "off" nil)
  1068. (const :tag "on: after stars/bullet and before tags first" t)
  1069. (const :tag "reversed: true line boundary first" reversed)
  1070. (cons :tag "Set C-a and C-e separately"
  1071. (choice :tag "Special C-a"
  1072. (const :tag "off" nil)
  1073. (const :tag "on: after stars/bullet first" t)
  1074. (const :tag "reversed: before stars/bullet first" reversed))
  1075. (choice :tag "Special C-e"
  1076. (const :tag "off" nil)
  1077. (const :tag "on: before tags first" t)
  1078. (const :tag "reversed: after tags first" reversed)))))
  1079. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1080. (defcustom org-special-ctrl-k nil
  1081. "Non-nil means `C-k' will behave specially in headlines.
  1082. When nil, `C-k' will call the default `kill-line' command.
  1083. When t, the following will happen while the cursor is in the headline:
  1084. - When the cursor is at the beginning of a headline, kill the entire
  1085. line and possible the folded subtree below the line.
  1086. - When in the middle of the headline text, kill the headline up to the tags.
  1087. - When after the headline text, kill the tags."
  1088. :group 'org-edit-structure
  1089. :type 'boolean)
  1090. (defcustom org-ctrl-k-protect-subtree nil
  1091. "Non-nil means, do not delete a hidden subtree with C-k.
  1092. When set to the symbol `error', simply throw an error when C-k is
  1093. used to kill (part-of) a headline that has hidden text behind it.
  1094. Any other non-nil value will result in a query to the user, if it is
  1095. OK to kill that hidden subtree. When nil, kill without remorse."
  1096. :group 'org-edit-structure
  1097. :version "24.1"
  1098. :type '(choice
  1099. (const :tag "Do not protect hidden subtrees" nil)
  1100. (const :tag "Protect hidden subtrees with a security query" t)
  1101. (const :tag "Never kill a hidden subtree with C-k" error)))
  1102. (defcustom org-catch-invisible-edits nil
  1103. "Check if in invisible region before inserting or deleting a character.
  1104. Valid values are:
  1105. nil Do not check, so just do invisible edits.
  1106. error Throw an error and do nothing.
  1107. show Make point visible, and do the requested edit.
  1108. show-and-error Make point visible, then throw an error and abort the edit.
  1109. smart Make point visible, and do insertion/deletion if it is
  1110. adjacent to visible text and the change feels predictable.
  1111. Never delete a previously invisible character or add in the
  1112. middle or right after an invisible region. Basically, this
  1113. allows insertion and backward-delete right before ellipses.
  1114. FIXME: maybe in this case we should not even show?"
  1115. :group 'org-edit-structure
  1116. :version "24.1"
  1117. :type '(choice
  1118. (const :tag "Do not check" nil)
  1119. (const :tag "Throw error when trying to edit" error)
  1120. (const :tag "Unhide, but do not do the edit" show-and-error)
  1121. (const :tag "Show invisible part and do the edit" show)
  1122. (const :tag "Be smart and do the right thing" smart)))
  1123. (defcustom org-yank-folded-subtrees t
  1124. "Non-nil means when yanking subtrees, fold them.
  1125. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1126. it starts with a heading and all other headings in it are either children
  1127. or siblings, then fold all the subtrees. However, do this only if no
  1128. text after the yank would be swallowed into a folded tree by this action."
  1129. :group 'org-edit-structure
  1130. :type 'boolean)
  1131. (defcustom org-yank-adjusted-subtrees nil
  1132. "Non-nil means when yanking subtrees, adjust the level.
  1133. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1134. this function for details."
  1135. :group 'org-edit-structure
  1136. :type 'boolean)
  1137. (defcustom org-M-RET-may-split-line '((default . t))
  1138. "Non-nil means M-RET will split the line at the cursor position.
  1139. When nil, it will go to the end of the line before making a
  1140. new line.
  1141. You may also set this option in a different way for different
  1142. contexts. Valid contexts are:
  1143. headline when creating a new headline
  1144. item when creating a new item
  1145. table in a table field
  1146. default the value to be used for all contexts not explicitly
  1147. customized"
  1148. :group 'org-structure
  1149. :group 'org-table
  1150. :type '(choice
  1151. (const :tag "Always" t)
  1152. (const :tag "Never" nil)
  1153. (repeat :greedy t :tag "Individual contexts"
  1154. (cons
  1155. (choice :tag "Context"
  1156. (const headline)
  1157. (const item)
  1158. (const table)
  1159. (const default))
  1160. (boolean)))))
  1161. (defcustom org-insert-heading-respect-content nil
  1162. "Non-nil means insert new headings after the current subtree.
  1163. When nil, the new heading is created directly after the current line.
  1164. The commands \\[org-insert-heading-respect-content] and
  1165. \\[org-insert-todo-heading-respect-content] turn this variable on
  1166. for the duration of the command."
  1167. :group 'org-structure
  1168. :type 'boolean)
  1169. (defcustom org-blank-before-new-entry '((heading . auto)
  1170. (plain-list-item . auto))
  1171. "Should `org-insert-heading' leave a blank line before new heading/item?
  1172. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1173. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1174. which case Org will look at the surrounding headings/items and try to
  1175. make an intelligent decision whether to insert a blank line or not.
  1176. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1177. the setting here is ignored and no empty line is inserted to avoid breaking
  1178. the list structure."
  1179. :group 'org-edit-structure
  1180. :type '(list
  1181. (cons (const heading)
  1182. (choice (const :tag "Never" nil)
  1183. (const :tag "Always" t)
  1184. (const :tag "Auto" auto)))
  1185. (cons (const plain-list-item)
  1186. (choice (const :tag "Never" nil)
  1187. (const :tag "Always" t)
  1188. (const :tag "Auto" auto)))))
  1189. (defcustom org-insert-heading-hook nil
  1190. "Hook being run after inserting a new heading."
  1191. :group 'org-edit-structure
  1192. :type 'hook)
  1193. (defcustom org-enable-fixed-width-editor t
  1194. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1195. This currently only means they are never auto-wrapped.
  1196. When nil, such lines will be treated like ordinary lines.
  1197. See also the QUOTE keyword."
  1198. :group 'org-edit-structure
  1199. :type 'boolean)
  1200. (defcustom org-goto-auto-isearch t
  1201. "Non-nil means typing characters in `org-goto' starts incremental search.
  1202. When nil, you can use these keybindings to navigate the buffer:
  1203. q Quit the org-goto interface
  1204. n Go to the next visible heading
  1205. p Go to the previous visible heading
  1206. f Go one heading forward on same level
  1207. b Go one heading backward on same level
  1208. u Go one heading up"
  1209. :group 'org-edit-structure
  1210. :type 'boolean)
  1211. (defgroup org-sparse-trees nil
  1212. "Options concerning sparse trees in Org-mode."
  1213. :tag "Org Sparse Trees"
  1214. :group 'org-structure)
  1215. (defcustom org-highlight-sparse-tree-matches t
  1216. "Non-nil means highlight all matches that define a sparse tree.
  1217. The highlights will automatically disappear the next time the buffer is
  1218. changed by an edit command."
  1219. :group 'org-sparse-trees
  1220. :type 'boolean)
  1221. (defcustom org-remove-highlights-with-change t
  1222. "Non-nil means any change to the buffer will remove temporary highlights.
  1223. Such highlights are created by `org-occur' and `org-clock-display'.
  1224. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1225. The highlights created by `org-preview-latex-fragment' always need
  1226. `C-c C-c' to be removed."
  1227. :group 'org-sparse-trees
  1228. :group 'org-time
  1229. :type 'boolean)
  1230. (defcustom org-occur-hook '(org-first-headline-recenter)
  1231. "Hook that is run after `org-occur' has constructed a sparse tree.
  1232. This can be used to recenter the window to show as much of the structure
  1233. as possible."
  1234. :group 'org-sparse-trees
  1235. :type 'hook)
  1236. (defgroup org-imenu-and-speedbar nil
  1237. "Options concerning imenu and speedbar in Org-mode."
  1238. :tag "Org Imenu and Speedbar"
  1239. :group 'org-structure)
  1240. (defcustom org-imenu-depth 2
  1241. "The maximum level for Imenu access to Org-mode headlines.
  1242. This also applied for speedbar access."
  1243. :group 'org-imenu-and-speedbar
  1244. :type 'integer)
  1245. (defgroup org-table nil
  1246. "Options concerning tables in Org-mode."
  1247. :tag "Org Table"
  1248. :group 'org)
  1249. (defcustom org-enable-table-editor 'optimized
  1250. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1251. When nil, such lines will be treated like ordinary lines.
  1252. When equal to the symbol `optimized', the table editor will be optimized to
  1253. do the following:
  1254. - Automatic overwrite mode in front of whitespace in table fields.
  1255. This makes the structure of the table stay in tact as long as the edited
  1256. field does not exceed the column width.
  1257. - Minimize the number of realigns. Normally, the table is aligned each time
  1258. TAB or RET are pressed to move to another field. With optimization this
  1259. happens only if changes to a field might have changed the column width.
  1260. Optimization requires replacing the functions `self-insert-command',
  1261. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1262. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1263. very good at guessing when a re-align will be necessary, but you can always
  1264. force one with \\[org-ctrl-c-ctrl-c].
  1265. If you would like to use the optimized version in Org-mode, but the
  1266. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1267. This variable can be used to turn on and off the table editor during a session,
  1268. but in order to toggle optimization, a restart is required.
  1269. See also the variable `org-table-auto-blank-field'."
  1270. :group 'org-table
  1271. :type '(choice
  1272. (const :tag "off" nil)
  1273. (const :tag "on" t)
  1274. (const :tag "on, optimized" optimized)))
  1275. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1276. (version<= emacs-version "24.1"))
  1277. "Non-nil means cluster self-insert commands for undo when possible.
  1278. If this is set, then, like in the Emacs command loop, 20 consecutive
  1279. characters will be undone together.
  1280. This is configurable, because there is some impact on typing performance."
  1281. :group 'org-table
  1282. :type 'boolean)
  1283. (defcustom org-table-tab-recognizes-table.el t
  1284. "Non-nil means TAB will automatically notice a table.el table.
  1285. When it sees such a table, it moves point into it and - if necessary -
  1286. calls `table-recognize-table'."
  1287. :group 'org-table-editing
  1288. :type 'boolean)
  1289. (defgroup org-link nil
  1290. "Options concerning links in Org-mode."
  1291. :tag "Org Link"
  1292. :group 'org)
  1293. (defvar org-link-abbrev-alist-local nil
  1294. "Buffer-local version of `org-link-abbrev-alist', which see.
  1295. The value of this is taken from the #+LINK lines.")
  1296. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1297. (defcustom org-link-abbrev-alist nil
  1298. "Alist of link abbreviations.
  1299. The car of each element is a string, to be replaced at the start of a link.
  1300. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1301. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1302. [[linkkey:tag][description]]
  1303. The 'linkkey' must be a word word, starting with a letter, followed
  1304. by letters, numbers, '-' or '_'.
  1305. If REPLACE is a string, the tag will simply be appended to create the link.
  1306. If the string contains \"%s\", the tag will be inserted there. If the string
  1307. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1308. at that point (see the function `url-hexify-string'). If the string contains
  1309. the specifier \"%(my-function)\", then the custom function `my-function' will
  1310. be invoked: this function takes the tag as its only argument and must return
  1311. a string.
  1312. REPLACE may also be a function that will be called with the tag as the
  1313. only argument to create the link, which should be returned as a string.
  1314. See the manual for examples."
  1315. :group 'org-link
  1316. :type '(repeat
  1317. (cons
  1318. (string :tag "Protocol")
  1319. (choice
  1320. (string :tag "Format")
  1321. (function)))))
  1322. (defcustom org-descriptive-links t
  1323. "Non-nil means Org will display descriptive links.
  1324. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1325. \"Org Website\", hiding the link itself and just displaying its
  1326. description. When set to `nil', Org will display the full links
  1327. literally.
  1328. You can interactively set the value of this variable by calling
  1329. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1330. :group 'org-link
  1331. :type 'boolean)
  1332. (defcustom org-link-file-path-type 'adaptive
  1333. "How the path name in file links should be stored.
  1334. Valid values are:
  1335. relative Relative to the current directory, i.e. the directory of the file
  1336. into which the link is being inserted.
  1337. absolute Absolute path, if possible with ~ for home directory.
  1338. noabbrev Absolute path, no abbreviation of home directory.
  1339. adaptive Use relative path for files in the current directory and sub-
  1340. directories of it. For other files, use an absolute path."
  1341. :group 'org-link
  1342. :type '(choice
  1343. (const relative)
  1344. (const absolute)
  1345. (const noabbrev)
  1346. (const adaptive)))
  1347. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1348. "Types of links that should be activated in Org-mode files.
  1349. This is a list of symbols, each leading to the activation of a certain link
  1350. type. In principle, it does not hurt to turn on most link types - there may
  1351. be a small gain when turning off unused link types. The types are:
  1352. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1353. angle Links in angular brackets that may contain whitespace like
  1354. <bbdb:Carsten Dominik>.
  1355. plain Plain links in normal text, no whitespace, like http://google.com.
  1356. radio Text that is matched by a radio target, see manual for details.
  1357. tag Tag settings in a headline (link to tag search).
  1358. date Time stamps (link to calendar).
  1359. footnote Footnote labels.
  1360. Changing this variable requires a restart of Emacs to become effective."
  1361. :group 'org-link
  1362. :type '(set :greedy t
  1363. (const :tag "Double bracket links" bracket)
  1364. (const :tag "Angular bracket links" angle)
  1365. (const :tag "Plain text links" plain)
  1366. (const :tag "Radio target matches" radio)
  1367. (const :tag "Tags" tag)
  1368. (const :tag "Timestamps" date)
  1369. (const :tag "Footnotes" footnote)))
  1370. (defcustom org-make-link-description-function nil
  1371. "Function to use for generating link descriptions from links.
  1372. When nil, the link location will be used. This function must take
  1373. two parameters: the first one is the link, the second one is the
  1374. description generated by `org-insert-link'. The function should
  1375. return the description to use."
  1376. :group 'org-link
  1377. :type 'function)
  1378. (defgroup org-link-store nil
  1379. "Options concerning storing links in Org-mode."
  1380. :tag "Org Store Link"
  1381. :group 'org-link)
  1382. (defcustom org-url-hexify-p t
  1383. "When non-nil, hexify URL when creating a link."
  1384. :type 'boolean
  1385. :version "24.3"
  1386. :group 'org-link-store)
  1387. (defcustom org-email-link-description-format "Email %c: %.30s"
  1388. "Format of the description part of a link to an email or usenet message.
  1389. The following %-escapes will be replaced by corresponding information:
  1390. %F full \"From\" field
  1391. %f name, taken from \"From\" field, address if no name
  1392. %T full \"To\" field
  1393. %t first name in \"To\" field, address if no name
  1394. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1395. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1396. %s subject
  1397. %d date
  1398. %m message-id.
  1399. You may use normal field width specification between the % and the letter.
  1400. This is for example useful to limit the length of the subject.
  1401. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1402. :group 'org-link-store
  1403. :type 'string)
  1404. (defcustom org-from-is-user-regexp
  1405. (let (r1 r2)
  1406. (when (and user-mail-address (not (string= user-mail-address "")))
  1407. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1408. (when (and user-full-name (not (string= user-full-name "")))
  1409. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1410. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1411. "Regexp matched against the \"From:\" header of an email or usenet message.
  1412. It should match if the message is from the user him/herself."
  1413. :group 'org-link-store
  1414. :type 'regexp)
  1415. (defcustom org-context-in-file-links t
  1416. "Non-nil means file links from `org-store-link' contain context.
  1417. A search string will be added to the file name with :: as separator and
  1418. used to find the context when the link is activated by the command
  1419. `org-open-at-point'. When this option is t, the entire active region
  1420. will be placed in the search string of the file link. If set to a
  1421. positive integer, only the first n lines of context will be stored.
  1422. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1423. negates this setting for the duration of the command."
  1424. :group 'org-link-store
  1425. :type '(choice boolean integer))
  1426. (defcustom org-keep-stored-link-after-insertion nil
  1427. "Non-nil means keep link in list for entire session.
  1428. The command `org-store-link' adds a link pointing to the current
  1429. location to an internal list. These links accumulate during a session.
  1430. The command `org-insert-link' can be used to insert links into any
  1431. Org-mode file (offering completion for all stored links). When this
  1432. option is nil, every link which has been inserted once using \\[org-insert-link]
  1433. will be removed from the list, to make completing the unused links
  1434. more efficient."
  1435. :group 'org-link-store
  1436. :type 'boolean)
  1437. (defgroup org-link-follow nil
  1438. "Options concerning following links in Org-mode."
  1439. :tag "Org Follow Link"
  1440. :group 'org-link)
  1441. (defcustom org-link-translation-function nil
  1442. "Function to translate links with different syntax to Org syntax.
  1443. This can be used to translate links created for example by the Planner
  1444. or emacs-wiki packages to Org syntax.
  1445. The function must accept two parameters, a TYPE containing the link
  1446. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1447. which is everything after the link protocol. It should return a cons
  1448. with possibly modified values of type and path.
  1449. Org contains a function for this, so if you set this variable to
  1450. `org-translate-link-from-planner', you should be able follow many
  1451. links created by planner."
  1452. :group 'org-link-follow
  1453. :type 'function)
  1454. (defcustom org-follow-link-hook nil
  1455. "Hook that is run after a link has been followed."
  1456. :group 'org-link-follow
  1457. :type 'hook)
  1458. (defcustom org-tab-follows-link nil
  1459. "Non-nil means on links TAB will follow the link.
  1460. Needs to be set before org.el is loaded.
  1461. This really should not be used, it does not make sense, and the
  1462. implementation is bad."
  1463. :group 'org-link-follow
  1464. :type 'boolean)
  1465. (defcustom org-return-follows-link nil
  1466. "Non-nil means on links RET will follow the link.
  1467. In tables, the special behavior of RET has precedence."
  1468. :group 'org-link-follow
  1469. :type 'boolean)
  1470. (defcustom org-mouse-1-follows-link
  1471. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1472. "Non-nil means mouse-1 on a link will follow the link.
  1473. A longer mouse click will still set point. Does not work on XEmacs.
  1474. Needs to be set before org.el is loaded."
  1475. :group 'org-link-follow
  1476. :type 'boolean)
  1477. (defcustom org-mark-ring-length 4
  1478. "Number of different positions to be recorded in the ring.
  1479. Changing this requires a restart of Emacs to work correctly."
  1480. :group 'org-link-follow
  1481. :type 'integer)
  1482. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1483. "Non-nil means internal links in Org files must exactly match a headline.
  1484. When nil, the link search tries to match a phrase with all words
  1485. in the search text."
  1486. :group 'org-link-follow
  1487. :version "24.1"
  1488. :type '(choice
  1489. (const :tag "Use fuzzy text search" nil)
  1490. (const :tag "Match only exact headline" t)
  1491. (const :tag "Match exact headline or query to create it"
  1492. query-to-create)))
  1493. (defcustom org-link-frame-setup
  1494. '((vm . vm-visit-folder-other-frame)
  1495. (vm-imap . vm-visit-imap-folder-other-frame)
  1496. (gnus . org-gnus-no-new-news)
  1497. (file . find-file-other-window)
  1498. (wl . wl-other-frame))
  1499. "Setup the frame configuration for following links.
  1500. When following a link with Emacs, it may often be useful to display
  1501. this link in another window or frame. This variable can be used to
  1502. set this up for the different types of links.
  1503. For VM, use any of
  1504. `vm-visit-folder'
  1505. `vm-visit-folder-other-window'
  1506. `vm-visit-folder-other-frame'
  1507. For Gnus, use any of
  1508. `gnus'
  1509. `gnus-other-frame'
  1510. `org-gnus-no-new-news'
  1511. For FILE, use any of
  1512. `find-file'
  1513. `find-file-other-window'
  1514. `find-file-other-frame'
  1515. For Wanderlust use any of
  1516. `wl'
  1517. `wl-other-frame'
  1518. For the calendar, use the variable `calendar-setup'.
  1519. For BBDB, it is currently only possible to display the matches in
  1520. another window."
  1521. :group 'org-link-follow
  1522. :type '(list
  1523. (cons (const vm)
  1524. (choice
  1525. (const vm-visit-folder)
  1526. (const vm-visit-folder-other-window)
  1527. (const vm-visit-folder-other-frame)))
  1528. (cons (const gnus)
  1529. (choice
  1530. (const gnus)
  1531. (const gnus-other-frame)
  1532. (const org-gnus-no-new-news)))
  1533. (cons (const file)
  1534. (choice
  1535. (const find-file)
  1536. (const find-file-other-window)
  1537. (const find-file-other-frame)))
  1538. (cons (const wl)
  1539. (choice
  1540. (const wl)
  1541. (const wl-other-frame)))))
  1542. (defcustom org-display-internal-link-with-indirect-buffer nil
  1543. "Non-nil means use indirect buffer to display infile links.
  1544. Activating internal links (from one location in a file to another location
  1545. in the same file) normally just jumps to the location. When the link is
  1546. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1547. is displayed in
  1548. another window. When this option is set, the other window actually displays
  1549. an indirect buffer clone of the current buffer, to avoid any visibility
  1550. changes to the current buffer."
  1551. :group 'org-link-follow
  1552. :type 'boolean)
  1553. (defcustom org-open-non-existing-files nil
  1554. "Non-nil means `org-open-file' will open non-existing files.
  1555. When nil, an error will be generated.
  1556. This variable applies only to external applications because they
  1557. might choke on non-existing files. If the link is to a file that
  1558. will be opened in Emacs, the variable is ignored."
  1559. :group 'org-link-follow
  1560. :type 'boolean)
  1561. (defcustom org-open-directory-means-index-dot-org nil
  1562. "Non-nil means a link to a directory really means to index.org.
  1563. When nil, following a directory link will run dired or open a finder/explorer
  1564. window on that directory."
  1565. :group 'org-link-follow
  1566. :type 'boolean)
  1567. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1568. "Function and arguments to call for following mailto links.
  1569. This is a list with the first element being a Lisp function, and the
  1570. remaining elements being arguments to the function. In string arguments,
  1571. %a will be replaced by the address, and %s will be replaced by the subject
  1572. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1573. :group 'org-link-follow
  1574. :type '(choice
  1575. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1576. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1577. (const :tag "message-mail" (message-mail "%a" "%s"))
  1578. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1579. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1580. "Non-nil means ask for confirmation before executing shell links.
  1581. Shell links can be dangerous: just think about a link
  1582. [[shell:rm -rf ~/*][Google Search]]
  1583. This link would show up in your Org-mode document as \"Google Search\",
  1584. but really it would remove your entire home directory.
  1585. Therefore we advise against setting this variable to nil.
  1586. Just change it to `y-or-n-p' if you want to confirm with a
  1587. single keystroke rather than having to type \"yes\"."
  1588. :group 'org-link-follow
  1589. :type '(choice
  1590. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1591. (const :tag "with y-or-n (faster)" y-or-n-p)
  1592. (const :tag "no confirmation (dangerous)" nil)))
  1593. (put 'org-confirm-shell-link-function
  1594. 'safe-local-variable
  1595. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1596. (defcustom org-confirm-shell-link-not-regexp ""
  1597. "A regexp to skip confirmation for shell links."
  1598. :group 'org-link-follow
  1599. :version "24.1"
  1600. :type 'regexp)
  1601. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1602. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1603. Elisp links can be dangerous: just think about a link
  1604. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1605. This link would show up in your Org-mode document as \"Google Search\",
  1606. but really it would remove your entire home directory.
  1607. Therefore we advise against setting this variable to nil.
  1608. Just change it to `y-or-n-p' if you want to confirm with a
  1609. single keystroke rather than having to type \"yes\"."
  1610. :group 'org-link-follow
  1611. :type '(choice
  1612. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1613. (const :tag "with y-or-n (faster)" y-or-n-p)
  1614. (const :tag "no confirmation (dangerous)" nil)))
  1615. (put 'org-confirm-shell-link-function
  1616. 'safe-local-variable
  1617. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1618. (defcustom org-confirm-elisp-link-not-regexp ""
  1619. "A regexp to skip confirmation for Elisp links."
  1620. :group 'org-link-follow
  1621. :version "24.1"
  1622. :type 'regexp)
  1623. (defconst org-file-apps-defaults-gnu
  1624. '((remote . emacs)
  1625. (system . mailcap)
  1626. (t . mailcap))
  1627. "Default file applications on a UNIX or GNU/Linux system.
  1628. See `org-file-apps'.")
  1629. (defconst org-file-apps-defaults-macosx
  1630. '((remote . emacs)
  1631. (t . "open %s")
  1632. (system . "open %s")
  1633. ("ps.gz" . "gv %s")
  1634. ("eps.gz" . "gv %s")
  1635. ("dvi" . "xdvi %s")
  1636. ("fig" . "xfig %s"))
  1637. "Default file applications on a MacOS X system.
  1638. The system \"open\" is known as a default, but we use X11 applications
  1639. for some files for which the OS does not have a good default.
  1640. See `org-file-apps'.")
  1641. (defconst org-file-apps-defaults-windowsnt
  1642. (list
  1643. '(remote . emacs)
  1644. (cons t
  1645. (list (if (featurep 'xemacs)
  1646. 'mswindows-shell-execute
  1647. 'w32-shell-execute)
  1648. "open" 'file))
  1649. (cons 'system
  1650. (list (if (featurep 'xemacs)
  1651. 'mswindows-shell-execute
  1652. 'w32-shell-execute)
  1653. "open" 'file)))
  1654. "Default file applications on a Windows NT system.
  1655. The system \"open\" is used for most files.
  1656. See `org-file-apps'.")
  1657. (defcustom org-file-apps
  1658. '(
  1659. (auto-mode . emacs)
  1660. ("\\.mm\\'" . default)
  1661. ("\\.x?html?\\'" . default)
  1662. ("\\.pdf\\'" . default)
  1663. )
  1664. "External applications for opening `file:path' items in a document.
  1665. Org-mode uses system defaults for different file types, but
  1666. you can use this variable to set the application for a given file
  1667. extension. The entries in this list are cons cells where the car identifies
  1668. files and the cdr the corresponding command. Possible values for the
  1669. file identifier are
  1670. \"string\" A string as a file identifier can be interpreted in different
  1671. ways, depending on its contents:
  1672. - Alphanumeric characters only:
  1673. Match links with this file extension.
  1674. Example: (\"pdf\" . \"evince %s\")
  1675. to open PDFs with evince.
  1676. - Regular expression: Match links where the
  1677. filename matches the regexp. If you want to
  1678. use groups here, use shy groups.
  1679. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1680. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1681. to open *.html and *.xhtml with firefox.
  1682. - Regular expression which contains (non-shy) groups:
  1683. Match links where the whole link, including \"::\", and
  1684. anything after that, matches the regexp.
  1685. In a custom command string, %1, %2, etc. are replaced with
  1686. the parts of the link that were matched by the groups.
  1687. For backwards compatibility, if a command string is given
  1688. that does not use any of the group matches, this case is
  1689. handled identically to the second one (i.e. match against
  1690. file name only).
  1691. In a custom lisp form, you can access the group matches with
  1692. (match-string n link).
  1693. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1694. to open [[file:document.pdf::5]] with evince at page 5.
  1695. `directory' Matches a directory
  1696. `remote' Matches a remote file, accessible through tramp or efs.
  1697. Remote files most likely should be visited through Emacs
  1698. because external applications cannot handle such paths.
  1699. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1700. so all files Emacs knows how to handle. Using this with
  1701. command `emacs' will open most files in Emacs. Beware that this
  1702. will also open html files inside Emacs, unless you add
  1703. (\"html\" . default) to the list as well.
  1704. t Default for files not matched by any of the other options.
  1705. `system' The system command to open files, like `open' on Windows
  1706. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1707. that will be selected if you call `C-c C-o' with a double
  1708. \\[universal-argument] \\[universal-argument] prefix.
  1709. Possible values for the command are:
  1710. `emacs' The file will be visited by the current Emacs process.
  1711. `default' Use the default application for this file type, which is the
  1712. association for t in the list, most likely in the system-specific
  1713. part.
  1714. This can be used to overrule an unwanted setting in the
  1715. system-specific variable.
  1716. `system' Use the system command for opening files, like \"open\".
  1717. This command is specified by the entry whose car is `system'.
  1718. Most likely, the system-specific version of this variable
  1719. does define this command, but you can overrule/replace it
  1720. here.
  1721. string A command to be executed by a shell; %s will be replaced
  1722. by the path to the file.
  1723. sexp A Lisp form which will be evaluated. The file path will
  1724. be available in the Lisp variable `file'.
  1725. For more examples, see the system specific constants
  1726. `org-file-apps-defaults-macosx'
  1727. `org-file-apps-defaults-windowsnt'
  1728. `org-file-apps-defaults-gnu'."
  1729. :group 'org-link-follow
  1730. :type '(repeat
  1731. (cons (choice :value ""
  1732. (string :tag "Extension")
  1733. (const :tag "System command to open files" system)
  1734. (const :tag "Default for unrecognized files" t)
  1735. (const :tag "Remote file" remote)
  1736. (const :tag "Links to a directory" directory)
  1737. (const :tag "Any files that have Emacs modes"
  1738. auto-mode))
  1739. (choice :value ""
  1740. (const :tag "Visit with Emacs" emacs)
  1741. (const :tag "Use default" default)
  1742. (const :tag "Use the system command" system)
  1743. (string :tag "Command")
  1744. (sexp :tag "Lisp form")))))
  1745. (defcustom org-doi-server-url "http://dx.doi.org/"
  1746. "The URL of the DOI server."
  1747. :type 'string
  1748. :version "24.3"
  1749. :group 'org-link-follow)
  1750. (defgroup org-refile nil
  1751. "Options concerning refiling entries in Org-mode."
  1752. :tag "Org Refile"
  1753. :group 'org)
  1754. (defcustom org-directory "~/org"
  1755. "Directory with org files.
  1756. This is just a default location to look for Org files. There is no need
  1757. at all to put your files into this directory. It is only used in the
  1758. following situations:
  1759. 1. When a capture template specifies a target file that is not an
  1760. absolute path. The path will then be interpreted relative to
  1761. `org-directory'
  1762. 2. When a capture note is filed away in an interactive way (when exiting the
  1763. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1764. with `org-directory' as the default path."
  1765. :group 'org-refile
  1766. :group 'org-remember
  1767. :group 'org-capture
  1768. :type 'directory)
  1769. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1770. "Default target for storing notes.
  1771. Used as a fall back file for org-remember.el and org-capture.el, for
  1772. templates that do not specify a target file."
  1773. :group 'org-refile
  1774. :group 'org-remember
  1775. :group 'org-capture
  1776. :type '(choice
  1777. (const :tag "Default from remember-data-file" nil)
  1778. file))
  1779. (defcustom org-goto-interface 'outline
  1780. "The default interface to be used for `org-goto'.
  1781. Allowed values are:
  1782. outline The interface shows an outline of the relevant file
  1783. and the correct heading is found by moving through
  1784. the outline or by searching with incremental search.
  1785. outline-path-completion Headlines in the current buffer are offered via
  1786. completion. This is the interface also used by
  1787. the refile command."
  1788. :group 'org-refile
  1789. :type '(choice
  1790. (const :tag "Outline" outline)
  1791. (const :tag "Outline-path-completion" outline-path-completion)))
  1792. (defcustom org-goto-max-level 5
  1793. "Maximum target level when running `org-goto' with refile interface."
  1794. :group 'org-refile
  1795. :type 'integer)
  1796. (defcustom org-reverse-note-order nil
  1797. "Non-nil means store new notes at the beginning of a file or entry.
  1798. When nil, new notes will be filed to the end of a file or entry.
  1799. This can also be a list with cons cells of regular expressions that
  1800. are matched against file names, and values."
  1801. :group 'org-remember
  1802. :group 'org-capture
  1803. :group 'org-refile
  1804. :type '(choice
  1805. (const :tag "Reverse always" t)
  1806. (const :tag "Reverse never" nil)
  1807. (repeat :tag "By file name regexp"
  1808. (cons regexp boolean))))
  1809. (defcustom org-log-refile nil
  1810. "Information to record when a task is refiled.
  1811. Possible values are:
  1812. nil Don't add anything
  1813. time Add a time stamp to the task
  1814. note Prompt for a note and add it with template `org-log-note-headings'
  1815. This option can also be set with on a per-file-basis with
  1816. #+STARTUP: nologrefile
  1817. #+STARTUP: logrefile
  1818. #+STARTUP: lognoterefile
  1819. You can have local logging settings for a subtree by setting the LOGGING
  1820. property to one or more of these keywords.
  1821. When bulk-refiling from the agenda, the value `note' is forbidden and
  1822. will temporarily be changed to `time'."
  1823. :group 'org-refile
  1824. :group 'org-progress
  1825. :version "24.1"
  1826. :type '(choice
  1827. (const :tag "No logging" nil)
  1828. (const :tag "Record timestamp" time)
  1829. (const :tag "Record timestamp with note." note)))
  1830. (defcustom org-refile-targets nil
  1831. "Targets for refiling entries with \\[org-refile].
  1832. This is a list of cons cells. Each cell contains:
  1833. - a specification of the files to be considered, either a list of files,
  1834. or a symbol whose function or variable value will be used to retrieve
  1835. a file name or a list of file names. If you use `org-agenda-files' for
  1836. that, all agenda files will be scanned for targets. Nil means consider
  1837. headings in the current buffer.
  1838. - A specification of how to find candidate refile targets. This may be
  1839. any of:
  1840. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1841. This tag has to be present in all target headlines, inheritance will
  1842. not be considered.
  1843. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1844. todo keyword.
  1845. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1846. headlines that are refiling targets.
  1847. - a cons cell (:level . N). Any headline of level N is considered a target.
  1848. Note that, when `org-odd-levels-only' is set, level corresponds to
  1849. order in hierarchy, not to the number of stars.
  1850. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1851. Note that, when `org-odd-levels-only' is set, level corresponds to
  1852. order in hierarchy, not to the number of stars.
  1853. Each element of this list generates a set of possible targets.
  1854. The union of these sets is presented (with completion) to
  1855. the user by `org-refile'.
  1856. You can set the variable `org-refile-target-verify-function' to a function
  1857. to verify each headline found by the simple criteria above.
  1858. When this variable is nil, all top-level headlines in the current buffer
  1859. are used, equivalent to the value `((nil . (:level . 1))'."
  1860. :group 'org-refile
  1861. :type '(repeat
  1862. (cons
  1863. (choice :value org-agenda-files
  1864. (const :tag "All agenda files" org-agenda-files)
  1865. (const :tag "Current buffer" nil)
  1866. (function) (variable) (file))
  1867. (choice :tag "Identify target headline by"
  1868. (cons :tag "Specific tag" (const :value :tag) (string))
  1869. (cons :tag "TODO keyword" (const :value :todo) (string))
  1870. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1871. (cons :tag "Level number" (const :value :level) (integer))
  1872. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1873. (defcustom org-refile-target-verify-function nil
  1874. "Function to verify if the headline at point should be a refile target.
  1875. The function will be called without arguments, with point at the
  1876. beginning of the headline. It should return t and leave point
  1877. where it is if the headline is a valid target for refiling.
  1878. If the target should not be selected, the function must return nil.
  1879. In addition to this, it may move point to a place from where the search
  1880. should be continued. For example, the function may decide that the entire
  1881. subtree of the current entry should be excluded and move point to the end
  1882. of the subtree."
  1883. :group 'org-refile
  1884. :type 'function)
  1885. (defcustom org-refile-use-cache nil
  1886. "Non-nil means cache refile targets to speed up the process.
  1887. The cache for a particular file will be updated automatically when
  1888. the buffer has been killed, or when any of the marker used for flagging
  1889. refile targets no longer points at a live buffer.
  1890. If you have added new entries to a buffer that might themselves be targets,
  1891. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1892. find that easier, `C-u C-u C-u C-c C-w'."
  1893. :group 'org-refile
  1894. :version "24.1"
  1895. :type 'boolean)
  1896. (defcustom org-refile-use-outline-path nil
  1897. "Non-nil means provide refile targets as paths.
  1898. So a level 3 headline will be available as level1/level2/level3.
  1899. When the value is `file', also include the file name (without directory)
  1900. into the path. In this case, you can also stop the completion after
  1901. the file name, to get entries inserted as top level in the file.
  1902. When `full-file-path', include the full file path."
  1903. :group 'org-refile
  1904. :type '(choice
  1905. (const :tag "Not" nil)
  1906. (const :tag "Yes" t)
  1907. (const :tag "Start with file name" file)
  1908. (const :tag "Start with full file path" full-file-path)))
  1909. (defcustom org-outline-path-complete-in-steps t
  1910. "Non-nil means complete the outline path in hierarchical steps.
  1911. When Org-mode uses the refile interface to select an outline path
  1912. \(see variable `org-refile-use-outline-path'), the completion of
  1913. the path can be done is a single go, or if can be done in steps down
  1914. the headline hierarchy. Going in steps is probably the best if you
  1915. do not use a special completion package like `ido' or `icicles'.
  1916. However, when using these packages, going in one step can be very
  1917. fast, while still showing the whole path to the entry."
  1918. :group 'org-refile
  1919. :type 'boolean)
  1920. (defcustom org-refile-allow-creating-parent-nodes nil
  1921. "Non-nil means allow to create new nodes as refile targets.
  1922. New nodes are then created by adding \"/new node name\" to the completion
  1923. of an existing node. When the value of this variable is `confirm',
  1924. new node creation must be confirmed by the user (recommended)
  1925. When nil, the completion must match an existing entry.
  1926. Note that, if the new heading is not seen by the criteria
  1927. listed in `org-refile-targets', multiple instances of the same
  1928. heading would be created by trying again to file under the new
  1929. heading."
  1930. :group 'org-refile
  1931. :type '(choice
  1932. (const :tag "Never" nil)
  1933. (const :tag "Always" t)
  1934. (const :tag "Prompt for confirmation" confirm)))
  1935. (defcustom org-refile-active-region-within-subtree nil
  1936. "Non-nil means also refile active region within a subtree.
  1937. By default `org-refile' doesn't allow refiling regions if they
  1938. don't contain a set of subtrees, but it might be convenient to
  1939. do so sometimes: in that case, the first line of the region is
  1940. converted to a headline before refiling."
  1941. :group 'org-refile
  1942. :version "24.1"
  1943. :type 'boolean)
  1944. (defgroup org-todo nil
  1945. "Options concerning TODO items in Org-mode."
  1946. :tag "Org TODO"
  1947. :group 'org)
  1948. (defgroup org-progress nil
  1949. "Options concerning Progress logging in Org-mode."
  1950. :tag "Org Progress"
  1951. :group 'org-time)
  1952. (defvar org-todo-interpretation-widgets
  1953. '((:tag "Sequence (cycling hits every state)" sequence)
  1954. (:tag "Type (cycling directly to DONE)" type))
  1955. "The available interpretation symbols for customizing `org-todo-keywords'.
  1956. Interested libraries should add to this list.")
  1957. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1958. "List of TODO entry keyword sequences and their interpretation.
  1959. \\<org-mode-map>This is a list of sequences.
  1960. Each sequence starts with a symbol, either `sequence' or `type',
  1961. indicating if the keywords should be interpreted as a sequence of
  1962. action steps, or as different types of TODO items. The first
  1963. keywords are states requiring action - these states will select a headline
  1964. for inclusion into the global TODO list Org-mode produces. If one of
  1965. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1966. signify that no further action is necessary. If \"|\" is not found,
  1967. the last keyword is treated as the only DONE state of the sequence.
  1968. The command \\[org-todo] cycles an entry through these states, and one
  1969. additional state where no keyword is present. For details about this
  1970. cycling, see the manual.
  1971. TODO keywords and interpretation can also be set on a per-file basis with
  1972. the special #+SEQ_TODO and #+TYP_TODO lines.
  1973. Each keyword can optionally specify a character for fast state selection
  1974. \(in combination with the variable `org-use-fast-todo-selection')
  1975. and specifiers for state change logging, using the same syntax that
  1976. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1977. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1978. indicates to record a time stamp each time this state is selected.
  1979. Each keyword may also specify if a timestamp or a note should be
  1980. recorded when entering or leaving the state, by adding additional
  1981. characters in the parenthesis after the keyword. This looks like this:
  1982. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1983. record only the time of the state change. With X and Y being either
  1984. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1985. Y when leaving the state if and only if the *target* state does not
  1986. define X. You may omit any of the fast-selection key or X or /Y,
  1987. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1988. For backward compatibility, this variable may also be just a list
  1989. of keywords. In this case the interpretation (sequence or type) will be
  1990. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1991. :group 'org-todo
  1992. :group 'org-keywords
  1993. :type '(choice
  1994. (repeat :tag "Old syntax, just keywords"
  1995. (string :tag "Keyword"))
  1996. (repeat :tag "New syntax"
  1997. (cons
  1998. (choice
  1999. :tag "Interpretation"
  2000. ;;Quick and dirty way to see
  2001. ;;`org-todo-interpretations'. This takes the
  2002. ;;place of item arguments
  2003. :convert-widget
  2004. (lambda (widget)
  2005. (widget-put widget
  2006. :args (mapcar
  2007. #'(lambda (x)
  2008. (widget-convert
  2009. (cons 'const x)))
  2010. org-todo-interpretation-widgets))
  2011. widget))
  2012. (repeat
  2013. (string :tag "Keyword"))))))
  2014. (defvar org-todo-keywords-1 nil
  2015. "All TODO and DONE keywords active in a buffer.")
  2016. (make-variable-buffer-local 'org-todo-keywords-1)
  2017. (defvar org-todo-keywords-for-agenda nil)
  2018. (defvar org-done-keywords-for-agenda nil)
  2019. (defvar org-drawers-for-agenda nil)
  2020. (defvar org-todo-keyword-alist-for-agenda nil)
  2021. (defvar org-tag-alist-for-agenda nil
  2022. "Alist of all tags from all agenda files.")
  2023. (defvar org-tag-groups-alist-for-agenda nil
  2024. "Alist of all groups tags from all current agenda files.")
  2025. (defvar org-tag-groups-alist nil)
  2026. (make-variable-buffer-local 'org-tag-groups-alist)
  2027. (defvar org-agenda-contributing-files nil)
  2028. (defvar org-not-done-keywords nil)
  2029. (make-variable-buffer-local 'org-not-done-keywords)
  2030. (defvar org-done-keywords nil)
  2031. (make-variable-buffer-local 'org-done-keywords)
  2032. (defvar org-todo-heads nil)
  2033. (make-variable-buffer-local 'org-todo-heads)
  2034. (defvar org-todo-sets nil)
  2035. (make-variable-buffer-local 'org-todo-sets)
  2036. (defvar org-todo-log-states nil)
  2037. (make-variable-buffer-local 'org-todo-log-states)
  2038. (defvar org-todo-kwd-alist nil)
  2039. (make-variable-buffer-local 'org-todo-kwd-alist)
  2040. (defvar org-todo-key-alist nil)
  2041. (make-variable-buffer-local 'org-todo-key-alist)
  2042. (defvar org-todo-key-trigger nil)
  2043. (make-variable-buffer-local 'org-todo-key-trigger)
  2044. (defcustom org-todo-interpretation 'sequence
  2045. "Controls how TODO keywords are interpreted.
  2046. This variable is in principle obsolete and is only used for
  2047. backward compatibility, if the interpretation of todo keywords is
  2048. not given already in `org-todo-keywords'. See that variable for
  2049. more information."
  2050. :group 'org-todo
  2051. :group 'org-keywords
  2052. :type '(choice (const sequence)
  2053. (const type)))
  2054. (defcustom org-use-fast-todo-selection t
  2055. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2056. This variable describes if and under what circumstances the cycling
  2057. mechanism for TODO keywords will be replaced by a single-key, direct
  2058. selection scheme.
  2059. When nil, fast selection is never used.
  2060. When the symbol `prefix', it will be used when `org-todo' is called
  2061. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2062. `C-u t' in an agenda buffer.
  2063. When t, fast selection is used by default. In this case, the prefix
  2064. argument forces cycling instead.
  2065. In all cases, the special interface is only used if access keys have
  2066. actually been assigned by the user, i.e. if keywords in the configuration
  2067. are followed by a letter in parenthesis, like TODO(t)."
  2068. :group 'org-todo
  2069. :type '(choice
  2070. (const :tag "Never" nil)
  2071. (const :tag "By default" t)
  2072. (const :tag "Only with C-u C-c C-t" prefix)))
  2073. (defcustom org-provide-todo-statistics t
  2074. "Non-nil means update todo statistics after insert and toggle.
  2075. ALL-HEADLINES means update todo statistics by including headlines
  2076. with no TODO keyword as well, counting them as not done.
  2077. A list of TODO keywords means the same, but skip keywords that are
  2078. not in this list.
  2079. When this is set, todo statistics is updated in the parent of the
  2080. current entry each time a todo state is changed."
  2081. :group 'org-todo
  2082. :type '(choice
  2083. (const :tag "Yes, only for TODO entries" t)
  2084. (const :tag "Yes, including all entries" 'all-headlines)
  2085. (repeat :tag "Yes, for TODOs in this list"
  2086. (string :tag "TODO keyword"))
  2087. (other :tag "No TODO statistics" nil)))
  2088. (defcustom org-hierarchical-todo-statistics t
  2089. "Non-nil means TODO statistics covers just direct children.
  2090. When nil, all entries in the subtree are considered.
  2091. This has only an effect if `org-provide-todo-statistics' is set.
  2092. To set this to nil for only a single subtree, use a COOKIE_DATA
  2093. property and include the word \"recursive\" into the value."
  2094. :group 'org-todo
  2095. :type 'boolean)
  2096. (defcustom org-after-todo-state-change-hook nil
  2097. "Hook which is run after the state of a TODO item was changed.
  2098. The new state (a string with a TODO keyword, or nil) is available in the
  2099. Lisp variable `org-state'."
  2100. :group 'org-todo
  2101. :type 'hook)
  2102. (defvar org-blocker-hook nil
  2103. "Hook for functions that are allowed to block a state change.
  2104. Functions in this hook should not modify the buffer.
  2105. Each function gets as its single argument a property list,
  2106. see `org-trigger-hook' for more information about this list.
  2107. If any of the functions in this hook returns nil, the state change
  2108. is blocked.")
  2109. (defvar org-trigger-hook nil
  2110. "Hook for functions that are triggered by a state change.
  2111. Each function gets as its single argument a property list with at
  2112. least the following elements:
  2113. (:type type-of-change :position pos-at-entry-start
  2114. :from old-state :to new-state)
  2115. Depending on the type, more properties may be present.
  2116. This mechanism is currently implemented for:
  2117. TODO state changes
  2118. ------------------
  2119. :type todo-state-change
  2120. :from previous state (keyword as a string), or nil, or a symbol
  2121. 'todo' or 'done', to indicate the general type of state.
  2122. :to new state, like in :from")
  2123. (defcustom org-enforce-todo-dependencies nil
  2124. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2125. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2126. be blocked if any prior sibling is not yet done.
  2127. Finally, if the parent is blocked because of ordered siblings of its own,
  2128. the child will also be blocked."
  2129. :set (lambda (var val)
  2130. (set var val)
  2131. (if val
  2132. (add-hook 'org-blocker-hook
  2133. 'org-block-todo-from-children-or-siblings-or-parent)
  2134. (remove-hook 'org-blocker-hook
  2135. 'org-block-todo-from-children-or-siblings-or-parent)))
  2136. :group 'org-todo
  2137. :type 'boolean)
  2138. (defcustom org-enforce-todo-checkbox-dependencies nil
  2139. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2140. When this is nil, checkboxes have no influence on switching TODO states.
  2141. When non-nil, you first need to check off all check boxes before the TODO
  2142. entry can be switched to DONE.
  2143. This variable needs to be set before org.el is loaded, and you need to
  2144. restart Emacs after a change to make the change effective. The only way
  2145. to change is while Emacs is running is through the customize interface."
  2146. :set (lambda (var val)
  2147. (set var val)
  2148. (if val
  2149. (add-hook 'org-blocker-hook
  2150. 'org-block-todo-from-checkboxes)
  2151. (remove-hook 'org-blocker-hook
  2152. 'org-block-todo-from-checkboxes)))
  2153. :group 'org-todo
  2154. :type 'boolean)
  2155. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2156. "Non-nil means inserting a TODO heading is treated as state change.
  2157. So when the command \\[org-insert-todo-heading] is used, state change
  2158. logging will apply if appropriate. When nil, the new TODO item will
  2159. be inserted directly, and no logging will take place."
  2160. :group 'org-todo
  2161. :type 'boolean)
  2162. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2163. "Non-nil means switching TODO states with S-cursor counts as state change.
  2164. This is the default behavior. However, setting this to nil allows a
  2165. convenient way to select a TODO state and bypass any logging associated
  2166. with that."
  2167. :group 'org-todo
  2168. :type 'boolean)
  2169. (defcustom org-todo-state-tags-triggers nil
  2170. "Tag changes that should be triggered by TODO state changes.
  2171. This is a list. Each entry is
  2172. (state-change (tag . flag) .......)
  2173. State-change can be a string with a state, and empty string to indicate the
  2174. state that has no TODO keyword, or it can be one of the symbols `todo'
  2175. or `done', meaning any not-done or done state, respectively."
  2176. :group 'org-todo
  2177. :group 'org-tags
  2178. :type '(repeat
  2179. (cons (choice :tag "When changing to"
  2180. (const :tag "Not-done state" todo)
  2181. (const :tag "Done state" done)
  2182. (string :tag "State"))
  2183. (repeat
  2184. (cons :tag "Tag action"
  2185. (string :tag "Tag")
  2186. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2187. (defcustom org-log-done nil
  2188. "Information to record when a task moves to the DONE state.
  2189. Possible values are:
  2190. nil Don't add anything, just change the keyword
  2191. time Add a time stamp to the task
  2192. note Prompt for a note and add it with template `org-log-note-headings'
  2193. This option can also be set with on a per-file-basis with
  2194. #+STARTUP: nologdone
  2195. #+STARTUP: logdone
  2196. #+STARTUP: lognotedone
  2197. You can have local logging settings for a subtree by setting the LOGGING
  2198. property to one or more of these keywords."
  2199. :group 'org-todo
  2200. :group 'org-progress
  2201. :type '(choice
  2202. (const :tag "No logging" nil)
  2203. (const :tag "Record CLOSED timestamp" time)
  2204. (const :tag "Record CLOSED timestamp with note." note)))
  2205. ;; Normalize old uses of org-log-done.
  2206. (cond
  2207. ((eq org-log-done t) (setq org-log-done 'time))
  2208. ((and (listp org-log-done) (memq 'done org-log-done))
  2209. (setq org-log-done 'note)))
  2210. (defcustom org-log-reschedule nil
  2211. "Information to record when the scheduling date of a tasks is modified.
  2212. Possible values are:
  2213. nil Don't add anything, just change the date
  2214. time Add a time stamp to the task
  2215. note Prompt for a note and add it with template `org-log-note-headings'
  2216. This option can also be set with on a per-file-basis with
  2217. #+STARTUP: nologreschedule
  2218. #+STARTUP: logreschedule
  2219. #+STARTUP: lognotereschedule"
  2220. :group 'org-todo
  2221. :group 'org-progress
  2222. :type '(choice
  2223. (const :tag "No logging" nil)
  2224. (const :tag "Record timestamp" time)
  2225. (const :tag "Record timestamp with note." note)))
  2226. (defcustom org-log-redeadline nil
  2227. "Information to record when the deadline date of a tasks is modified.
  2228. Possible values are:
  2229. nil Don't add anything, just change the date
  2230. time Add a time stamp to the task
  2231. note Prompt for a note and add it with template `org-log-note-headings'
  2232. This option can also be set with on a per-file-basis with
  2233. #+STARTUP: nologredeadline
  2234. #+STARTUP: logredeadline
  2235. #+STARTUP: lognoteredeadline
  2236. You can have local logging settings for a subtree by setting the LOGGING
  2237. property to one or more of these keywords."
  2238. :group 'org-todo
  2239. :group 'org-progress
  2240. :type '(choice
  2241. (const :tag "No logging" nil)
  2242. (const :tag "Record timestamp" time)
  2243. (const :tag "Record timestamp with note." note)))
  2244. (defcustom org-log-note-clock-out nil
  2245. "Non-nil means record a note when clocking out of an item.
  2246. This can also be configured on a per-file basis by adding one of
  2247. the following lines anywhere in the buffer:
  2248. #+STARTUP: lognoteclock-out
  2249. #+STARTUP: nolognoteclock-out"
  2250. :group 'org-todo
  2251. :group 'org-progress
  2252. :type 'boolean)
  2253. (defcustom org-log-done-with-time t
  2254. "Non-nil means the CLOSED time stamp will contain date and time.
  2255. When nil, only the date will be recorded."
  2256. :group 'org-progress
  2257. :type 'boolean)
  2258. (defcustom org-log-note-headings
  2259. '((done . "CLOSING NOTE %t")
  2260. (state . "State %-12s from %-12S %t")
  2261. (note . "Note taken on %t")
  2262. (reschedule . "Rescheduled from %S on %t")
  2263. (delschedule . "Not scheduled, was %S on %t")
  2264. (redeadline . "New deadline from %S on %t")
  2265. (deldeadline . "Removed deadline, was %S on %t")
  2266. (refile . "Refiled on %t")
  2267. (clock-out . ""))
  2268. "Headings for notes added to entries.
  2269. The value is an alist, with the car being a symbol indicating the note
  2270. context, and the cdr is the heading to be used. The heading may also be the
  2271. empty string.
  2272. %t in the heading will be replaced by a time stamp.
  2273. %T will be an active time stamp instead the default inactive one
  2274. %d will be replaced by a short-format time stamp.
  2275. %D will be replaced by an active short-format time stamp.
  2276. %s will be replaced by the new TODO state, in double quotes.
  2277. %S will be replaced by the old TODO state, in double quotes.
  2278. %u will be replaced by the user name.
  2279. %U will be replaced by the full user name.
  2280. In fact, it is not a good idea to change the `state' entry, because
  2281. agenda log mode depends on the format of these entries."
  2282. :group 'org-todo
  2283. :group 'org-progress
  2284. :type '(list :greedy t
  2285. (cons (const :tag "Heading when closing an item" done) string)
  2286. (cons (const :tag
  2287. "Heading when changing todo state (todo sequence only)"
  2288. state) string)
  2289. (cons (const :tag "Heading when just taking a note" note) string)
  2290. (cons (const :tag "Heading when clocking out" clock-out) string)
  2291. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2292. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2293. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2294. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2295. (cons (const :tag "Heading when refiling" refile) string)))
  2296. (unless (assq 'note org-log-note-headings)
  2297. (push '(note . "%t") org-log-note-headings))
  2298. (defcustom org-log-into-drawer nil
  2299. "Non-nil means insert state change notes and time stamps into a drawer.
  2300. When nil, state changes notes will be inserted after the headline and
  2301. any scheduling and clock lines, but not inside a drawer.
  2302. The value of this variable should be the name of the drawer to use.
  2303. LOGBOOK is proposed as the default drawer for this purpose, you can
  2304. also set this to a string to define the drawer of your choice.
  2305. A value of t is also allowed, representing \"LOGBOOK\".
  2306. A value of t or nil can also be set with on a per-file-basis with
  2307. #+STARTUP: logdrawer
  2308. #+STARTUP: nologdrawer
  2309. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2310. will be ignored.
  2311. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2312. a subtree."
  2313. :group 'org-todo
  2314. :group 'org-progress
  2315. :type '(choice
  2316. (const :tag "Not into a drawer" nil)
  2317. (const :tag "LOGBOOK" t)
  2318. (string :tag "Other")))
  2319. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2320. (defun org-log-into-drawer ()
  2321. "Return the value of `org-log-into-drawer', but let properties overrule.
  2322. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2323. used instead of the default value."
  2324. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2325. (cond
  2326. ((not p) org-log-into-drawer)
  2327. ((equal p "nil") nil)
  2328. ((equal p "t") "LOGBOOK")
  2329. (t p))))
  2330. (defcustom org-log-state-notes-insert-after-drawers nil
  2331. "Non-nil means insert state change notes after any drawers in entry.
  2332. Only the drawers that *immediately* follow the headline and the
  2333. deadline/scheduled line are skipped.
  2334. When nil, insert notes right after the heading and perhaps the line
  2335. with deadline/scheduling if present.
  2336. This variable will have no effect if `org-log-into-drawer' is
  2337. set."
  2338. :group 'org-todo
  2339. :group 'org-progress
  2340. :type 'boolean)
  2341. (defcustom org-log-states-order-reversed t
  2342. "Non-nil means the latest state note will be directly after heading.
  2343. When nil, the state change notes will be ordered according to time.
  2344. This option can also be set with on a per-file-basis with
  2345. #+STARTUP: logstatesreversed
  2346. #+STARTUP: nologstatesreversed"
  2347. :group 'org-todo
  2348. :group 'org-progress
  2349. :type 'boolean)
  2350. (defcustom org-todo-repeat-to-state nil
  2351. "The TODO state to which a repeater should return the repeating task.
  2352. By default this is the first task in a TODO sequence, or the previous state
  2353. in a TODO_TYP set. But you can specify another task here.
  2354. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2355. :group 'org-todo
  2356. :version "24.1"
  2357. :type '(choice (const :tag "Head of sequence" nil)
  2358. (string :tag "Specific state")))
  2359. (defcustom org-log-repeat 'time
  2360. "Non-nil means record moving through the DONE state when triggering repeat.
  2361. An auto-repeating task is immediately switched back to TODO when
  2362. marked DONE. If you are not logging state changes (by adding \"@\"
  2363. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2364. record a closing note, there will be no record of the task moving
  2365. through DONE. This variable forces taking a note anyway.
  2366. nil Don't force a record
  2367. time Record a time stamp
  2368. note Prompt for a note and add it with template `org-log-note-headings'
  2369. This option can also be set with on a per-file-basis with
  2370. #+STARTUP: nologrepeat
  2371. #+STARTUP: logrepeat
  2372. #+STARTUP: lognoterepeat
  2373. You can have local logging settings for a subtree by setting the LOGGING
  2374. property to one or more of these keywords."
  2375. :group 'org-todo
  2376. :group 'org-progress
  2377. :type '(choice
  2378. (const :tag "Don't force a record" nil)
  2379. (const :tag "Force recording the DONE state" time)
  2380. (const :tag "Force recording a note with the DONE state" note)))
  2381. (defgroup org-priorities nil
  2382. "Priorities in Org-mode."
  2383. :tag "Org Priorities"
  2384. :group 'org-todo)
  2385. (defcustom org-enable-priority-commands t
  2386. "Non-nil means priority commands are active.
  2387. When nil, these commands will be disabled, so that you never accidentally
  2388. set a priority."
  2389. :group 'org-priorities
  2390. :type 'boolean)
  2391. (defcustom org-highest-priority ?A
  2392. "The highest priority of TODO items. A character like ?A, ?B etc.
  2393. Must have a smaller ASCII number than `org-lowest-priority'."
  2394. :group 'org-priorities
  2395. :type 'character)
  2396. (defcustom org-lowest-priority ?C
  2397. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2398. Must have a larger ASCII number than `org-highest-priority'."
  2399. :group 'org-priorities
  2400. :type 'character)
  2401. (defcustom org-default-priority ?B
  2402. "The default priority of TODO items.
  2403. This is the priority an item gets if no explicit priority is given.
  2404. When starting to cycle on an empty priority the first step in the cycle
  2405. depends on `org-priority-start-cycle-with-default'. The resulting first
  2406. step priority must not exceed the range from `org-highest-priority' to
  2407. `org-lowest-priority' which means that `org-default-priority' has to be
  2408. in this range exclusive or inclusive the range boundaries. Else the
  2409. first step refuses to set the default and the second will fall back
  2410. to (depending on the command used) the highest or lowest priority."
  2411. :group 'org-priorities
  2412. :type 'character)
  2413. (defcustom org-priority-start-cycle-with-default t
  2414. "Non-nil means start with default priority when starting to cycle.
  2415. When this is nil, the first step in the cycle will be (depending on the
  2416. command used) one higher or lower than the default priority.
  2417. See also `org-default-priority'."
  2418. :group 'org-priorities
  2419. :type 'boolean)
  2420. (defcustom org-get-priority-function nil
  2421. "Function to extract the priority from a string.
  2422. The string is normally the headline. If this is nil Org computes the
  2423. priority from the priority cookie like [#A] in the headline. It returns
  2424. an integer, increasing by 1000 for each priority level.
  2425. The user can set a different function here, which should take a string
  2426. as an argument and return the numeric priority."
  2427. :group 'org-priorities
  2428. :version "24.1"
  2429. :type 'function)
  2430. (defgroup org-time nil
  2431. "Options concerning time stamps and deadlines in Org-mode."
  2432. :tag "Org Time"
  2433. :group 'org)
  2434. (defcustom org-insert-labeled-timestamps-at-point nil
  2435. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2436. When nil, these labeled time stamps are forces into the second line of an
  2437. entry, just after the headline. When scheduling from the global TODO list,
  2438. the time stamp will always be forced into the second line."
  2439. :group 'org-time
  2440. :type 'boolean)
  2441. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2442. "Formats for `format-time-string' which are used for time stamps.
  2443. It is not recommended to change this constant.")
  2444. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2445. "Number of minutes to round time stamps to.
  2446. These are two values, the first applies when first creating a time stamp.
  2447. The second applies when changing it with the commands `S-up' and `S-down'.
  2448. When changing the time stamp, this means that it will change in steps
  2449. of N minutes, as given by the second value.
  2450. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2451. numbers should be factors of 60, so for example 5, 10, 15.
  2452. When this is larger than 1, you can still force an exact time stamp by using
  2453. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2454. and by using a prefix arg to `S-up/down' to specify the exact number
  2455. of minutes to shift."
  2456. :group 'org-time
  2457. :get #'(lambda (var) ; Make sure both elements are there
  2458. (if (integerp (default-value var))
  2459. (list (default-value var) 5)
  2460. (default-value var)))
  2461. :type '(list
  2462. (integer :tag "when inserting times")
  2463. (integer :tag "when modifying times")))
  2464. ;; Normalize old customizations of this variable.
  2465. (when (integerp org-time-stamp-rounding-minutes)
  2466. (setq org-time-stamp-rounding-minutes
  2467. (list org-time-stamp-rounding-minutes
  2468. org-time-stamp-rounding-minutes)))
  2469. (defcustom org-display-custom-times nil
  2470. "Non-nil means overlay custom formats over all time stamps.
  2471. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2472. To turn this on on a per-file basis, insert anywhere in the file:
  2473. #+STARTUP: customtime"
  2474. :group 'org-time
  2475. :set 'set-default
  2476. :type 'sexp)
  2477. (make-variable-buffer-local 'org-display-custom-times)
  2478. (defcustom org-time-stamp-custom-formats
  2479. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2480. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2481. These are overlaid over the default ISO format if the variable
  2482. `org-display-custom-times' is set. Time like %H:%M should be at the
  2483. end of the second format. The custom formats are also honored by export
  2484. commands, if custom time display is turned on at the time of export."
  2485. :group 'org-time
  2486. :type 'sexp)
  2487. (defun org-time-stamp-format (&optional long inactive)
  2488. "Get the right format for a time string."
  2489. (let ((f (if long (cdr org-time-stamp-formats)
  2490. (car org-time-stamp-formats))))
  2491. (if inactive
  2492. (concat "[" (substring f 1 -1) "]")
  2493. f)))
  2494. (defcustom org-time-clocksum-format
  2495. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2496. "The format string used when creating CLOCKSUM lines.
  2497. This is also used when Org mode generates a time duration.
  2498. The value can be a single format string containing two
  2499. %-sequences, which will be filled with the number of hours and
  2500. minutes in that order.
  2501. Alternatively, the value can be a plist associating any of the
  2502. keys :years, :months, :weeks, :days, :hours or :minutes with
  2503. format strings. The time duration is formatted using only the
  2504. time components that are needed and concatenating the results.
  2505. If a time unit in absent, it falls back to the next smallest
  2506. unit.
  2507. The keys :require-years, :require-months, :require-days,
  2508. :require-weeks, :require-hours, :require-minutes are also
  2509. meaningful. A non-nil value for these keys indicates that the
  2510. corresponding time component should always be included, even if
  2511. its value is 0.
  2512. For example,
  2513. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2514. :require-minutes t)
  2515. means durations longer than a day will be expressed in days,
  2516. hours and minutes, and durations less than a day will always be
  2517. expressed in hours and minutes (even for durations less than an
  2518. hour).
  2519. The value
  2520. \(:days \"%dd\" :minutes \"%dm\")
  2521. means durations longer than a day will be expressed in days and
  2522. minutes, and durations less than a day will be expressed entirely
  2523. in minutes (even for durations longer than an hour)."
  2524. :group 'org-time
  2525. :group 'org-clock
  2526. :version "24.4"
  2527. :package-version '(Org . "8.0")
  2528. :type '(choice (string :tag "Format string")
  2529. (set :tag "Plist"
  2530. (group :inline t (const :tag "Years" :years)
  2531. (string :tag "Format string"))
  2532. (group :inline t
  2533. (const :tag "Always show years" :require-years)
  2534. (const t))
  2535. (group :inline t (const :tag "Months" :months)
  2536. (string :tag "Format string"))
  2537. (group :inline t
  2538. (const :tag "Always show months" :require-months)
  2539. (const t))
  2540. (group :inline t (const :tag "Weeks" :weeks)
  2541. (string :tag "Format string"))
  2542. (group :inline t
  2543. (const :tag "Always show weeks" :require-weeks)
  2544. (const t))
  2545. (group :inline t (const :tag "Days" :days)
  2546. (string :tag "Format string"))
  2547. (group :inline t
  2548. (const :tag "Always show days" :require-days)
  2549. (const t))
  2550. (group :inline t (const :tag "Hours" :hours)
  2551. (string :tag "Format string"))
  2552. (group :inline t
  2553. (const :tag "Always show hours" :require-hours)
  2554. (const t))
  2555. (group :inline t (const :tag "Minutes" :minutes)
  2556. (string :tag "Format string"))
  2557. (group :inline t
  2558. (const :tag "Always show minutes" :require-minutes)
  2559. (const t)))))
  2560. (defcustom org-time-clocksum-use-fractional nil
  2561. "When non-nil, \\[org-clock-display] uses fractional times.
  2562. See `org-time-clocksum-format' for more on time clock formats."
  2563. :group 'org-time
  2564. :group 'org-clock
  2565. :version "24.3"
  2566. :type 'boolean)
  2567. (defcustom org-time-clocksum-use-effort-durations nil
  2568. "When non-nil, \\[org-clock-display] uses effort durations.
  2569. E.g. by default, one day is considered to be a 8 hours effort,
  2570. so a task that has been clocked for 16 hours will be displayed
  2571. as during 2 days in the clock display or in the clocktable.
  2572. See `org-effort-durations' on how to set effort durations
  2573. and `org-time-clocksum-format' for more on time clock formats."
  2574. :group 'org-time
  2575. :group 'org-clock
  2576. :version "24.4"
  2577. :package-version '(Org . "8.0")
  2578. :type 'boolean)
  2579. (defcustom org-time-clocksum-fractional-format "%.2f"
  2580. "The format string used when creating CLOCKSUM lines,
  2581. or when Org mode generates a time duration, if
  2582. `org-time-clocksum-use-fractional' is enabled.
  2583. The value can be a single format string containing one
  2584. %-sequence, which will be filled with the number of hours as
  2585. a float.
  2586. Alternatively, the value can be a plist associating any of the
  2587. keys :years, :months, :weeks, :days, :hours or :minutes with
  2588. a format string. The time duration is formatted using the
  2589. largest time unit which gives a non-zero integer part. If all
  2590. specified formats have zero integer part, the smallest time unit
  2591. is used."
  2592. :group 'org-time
  2593. :type '(choice (string :tag "Format string")
  2594. (set (group :inline t (const :tag "Years" :years)
  2595. (string :tag "Format string"))
  2596. (group :inline t (const :tag "Months" :months)
  2597. (string :tag "Format string"))
  2598. (group :inline t (const :tag "Weeks" :weeks)
  2599. (string :tag "Format string"))
  2600. (group :inline t (const :tag "Days" :days)
  2601. (string :tag "Format string"))
  2602. (group :inline t (const :tag "Hours" :hours)
  2603. (string :tag "Format string"))
  2604. (group :inline t (const :tag "Minutes" :minutes)
  2605. (string :tag "Format string")))))
  2606. (defcustom org-deadline-warning-days 14
  2607. "Number of days before expiration during which a deadline becomes active.
  2608. This variable governs the display in sparse trees and in the agenda.
  2609. When 0 or negative, it means use this number (the absolute value of it)
  2610. even if a deadline has a different individual lead time specified.
  2611. Custom commands can set this variable in the options section."
  2612. :group 'org-time
  2613. :group 'org-agenda-daily/weekly
  2614. :type 'integer)
  2615. (defcustom org-scheduled-delay-days 0
  2616. "Number of days before a scheduled item becomes active.
  2617. This variable governs the display in sparse trees and in the agenda.
  2618. The default value (i.e. 0) means: don't delay scheduled item.
  2619. When negative, it means use this number (the absolute value of it)
  2620. even if a scheduled item has a different individual delay time
  2621. specified.
  2622. Custom commands can set this variable in the options section."
  2623. :group 'org-time
  2624. :group 'org-agenda-daily/weekly
  2625. :version "24.4"
  2626. :package-version '(Org . "8.0")
  2627. :type 'integer)
  2628. (defcustom org-read-date-prefer-future t
  2629. "Non-nil means assume future for incomplete date input from user.
  2630. This affects the following situations:
  2631. 1. The user gives a month but not a year.
  2632. For example, if it is April and you enter \"feb 2\", this will be read
  2633. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2634. 2. The user gives a day, but no month.
  2635. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2636. read this as the third of *next* month. However, if you enter \"17\",
  2637. it will be considered as *this* month.
  2638. If you set this variable to the symbol `time', then also the following
  2639. will work:
  2640. 3. If the user gives a time.
  2641. If the time is before now, it will be interpreted as tomorrow.
  2642. Currently none of this works for ISO week specifications.
  2643. When this option is nil, the current day, month and year will always be
  2644. used as defaults.
  2645. See also `org-agenda-jump-prefer-future'."
  2646. :group 'org-time
  2647. :type '(choice
  2648. (const :tag "Never" nil)
  2649. (const :tag "Check month and day" t)
  2650. (const :tag "Check month, day, and time" time)))
  2651. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2652. "Should the agenda jump command prefer the future for incomplete dates?
  2653. The default is to do the same as configured in `org-read-date-prefer-future'.
  2654. But you can also set a deviating value here.
  2655. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2656. :group 'org-agenda
  2657. :group 'org-time
  2658. :version "24.1"
  2659. :type '(choice
  2660. (const :tag "Use org-read-date-prefer-future"
  2661. org-read-date-prefer-future)
  2662. (const :tag "Never" nil)
  2663. (const :tag "Always" t)))
  2664. (defcustom org-read-date-force-compatible-dates t
  2665. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2666. Depending on the system Emacs is running on, certain dates cannot
  2667. be represented with the type used internally to represent time.
  2668. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2669. correctly. Some systems allow for earlier dates, some for later,
  2670. some for both. One way to find out it to insert any date into an
  2671. Org buffer, putting the cursor on the year and hitting S-up and
  2672. S-down to test the range.
  2673. When this variable is set to t, the date/time prompt will not let
  2674. you specify dates outside the 1970-2037 range, so it is certain that
  2675. these dates will work in whatever version of Emacs you are
  2676. running, and also that you can move a file from one Emacs implementation
  2677. to another. WHenever Org is forcing the year for you, it will display
  2678. a message and beep.
  2679. When this variable is nil, Org will check if the date is
  2680. representable in the specific Emacs implementation you are using.
  2681. If not, it will force a year, usually the current year, and beep
  2682. to remind you. Currently this setting is not recommended because
  2683. the likelihood that you will open your Org files in an Emacs that
  2684. has limited date range is not negligible.
  2685. A workaround for this problem is to use diary sexp dates for time
  2686. stamps outside of this range."
  2687. :group 'org-time
  2688. :version "24.1"
  2689. :type 'boolean)
  2690. (defcustom org-read-date-display-live t
  2691. "Non-nil means display current interpretation of date prompt live.
  2692. This display will be in an overlay, in the minibuffer."
  2693. :group 'org-time
  2694. :type 'boolean)
  2695. (defcustom org-read-date-popup-calendar t
  2696. "Non-nil means pop up a calendar when prompting for a date.
  2697. In the calendar, the date can be selected with mouse-1. However, the
  2698. minibuffer will also be active, and you can simply enter the date as well.
  2699. When nil, only the minibuffer will be available."
  2700. :group 'org-time
  2701. :type 'boolean)
  2702. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2703. 'org-read-date-popup-calendar)
  2704. (make-obsolete-variable
  2705. 'org-read-date-minibuffer-setup-hook
  2706. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2707. (defcustom org-read-date-minibuffer-setup-hook nil
  2708. "Hook to be used to set up keys for the date/time interface.
  2709. Add key definitions to `minibuffer-local-map', which will be a
  2710. temporary copy.
  2711. WARNING: This option is obsolete, you should use
  2712. `org-read-date-minibuffer-local-map' to set up keys."
  2713. :group 'org-time
  2714. :type 'hook)
  2715. (defcustom org-extend-today-until 0
  2716. "The hour when your day really ends. Must be an integer.
  2717. This has influence for the following applications:
  2718. - When switching the agenda to \"today\". It it is still earlier than
  2719. the time given here, the day recognized as TODAY is actually yesterday.
  2720. - When a date is read from the user and it is still before the time given
  2721. here, the current date and time will be assumed to be yesterday, 23:59.
  2722. Also, timestamps inserted in capture templates follow this rule.
  2723. IMPORTANT: This is a feature whose implementation is and likely will
  2724. remain incomplete. Really, it is only here because past midnight seems to
  2725. be the favorite working time of John Wiegley :-)"
  2726. :group 'org-time
  2727. :type 'integer)
  2728. (defcustom org-use-effective-time nil
  2729. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2730. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2731. \"effective time\" of any timestamps between midnight and 8am will be
  2732. 23:59 of the previous day."
  2733. :group 'org-time
  2734. :version "24.1"
  2735. :type 'boolean)
  2736. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2737. "When non-nil, use the last clock out time for `org-todo'.
  2738. Note that this option has precedence over the combined use of
  2739. `org-use-effective-time' and `org-extend-today-until'."
  2740. :group 'org-time
  2741. :version "24.4"
  2742. :package-version '(Org . "8.0")
  2743. :type 'boolean)
  2744. (defcustom org-edit-timestamp-down-means-later nil
  2745. "Non-nil means S-down will increase the time in a time stamp.
  2746. When nil, S-up will increase."
  2747. :group 'org-time
  2748. :type 'boolean)
  2749. (defcustom org-calendar-follow-timestamp-change t
  2750. "Non-nil means make the calendar window follow timestamp changes.
  2751. When a timestamp is modified and the calendar window is visible, it will be
  2752. moved to the new date."
  2753. :group 'org-time
  2754. :type 'boolean)
  2755. (defgroup org-tags nil
  2756. "Options concerning tags in Org-mode."
  2757. :tag "Org Tags"
  2758. :group 'org)
  2759. (defcustom org-tag-alist nil
  2760. "List of tags allowed in Org-mode files.
  2761. When this list is nil, Org-mode will base TAG input on what is already in the
  2762. buffer.
  2763. The value of this variable is an alist, the car of each entry must be a
  2764. keyword as a string, the cdr may be a character that is used to select
  2765. that tag through the fast-tag-selection interface.
  2766. See the manual for details."
  2767. :group 'org-tags
  2768. :type '(repeat
  2769. (choice
  2770. (cons (string :tag "Tag name")
  2771. (character :tag "Access char"))
  2772. (list :tag "Start radio group"
  2773. (const :startgroup)
  2774. (option (string :tag "Group description")))
  2775. (list :tag "Group tags delimiter"
  2776. (const :grouptags))
  2777. (list :tag "End radio group"
  2778. (const :endgroup)
  2779. (option (string :tag "Group description")))
  2780. (const :tag "New line" (:newline)))))
  2781. (defcustom org-tag-persistent-alist nil
  2782. "List of tags that will always appear in all Org-mode files.
  2783. This is in addition to any in buffer settings or customizations
  2784. of `org-tag-alist'.
  2785. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2786. The value of this variable is an alist, the car of each entry must be a
  2787. keyword as a string, the cdr may be a character that is used to select
  2788. that tag through the fast-tag-selection interface.
  2789. See the manual for details.
  2790. To disable these tags on a per-file basis, insert anywhere in the file:
  2791. #+STARTUP: noptag"
  2792. :group 'org-tags
  2793. :type '(repeat
  2794. (choice
  2795. (cons (string :tag "Tag name")
  2796. (character :tag "Access char"))
  2797. (const :tag "Start radio group" (:startgroup))
  2798. (const :tag "Group tags delimiter" (:grouptags))
  2799. (const :tag "End radio group" (:endgroup))
  2800. (const :tag "New line" (:newline)))))
  2801. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2802. "If non-nil, always offer completion for all tags of all agenda files.
  2803. Instead of customizing this variable directly, you might want to
  2804. set it locally for capture buffers, because there no list of
  2805. tags in that file can be created dynamically (there are none).
  2806. (add-hook 'org-capture-mode-hook
  2807. (lambda ()
  2808. (set (make-local-variable
  2809. 'org-complete-tags-always-offer-all-agenda-tags)
  2810. t)))"
  2811. :group 'org-tags
  2812. :version "24.1"
  2813. :type 'boolean)
  2814. (defvar org-file-tags nil
  2815. "List of tags that can be inherited by all entries in the file.
  2816. The tags will be inherited if the variable `org-use-tag-inheritance'
  2817. says they should be.
  2818. This variable is populated from #+FILETAGS lines.")
  2819. (defcustom org-use-fast-tag-selection 'auto
  2820. "Non-nil means use fast tag selection scheme.
  2821. This is a special interface to select and deselect tags with single keys.
  2822. When nil, fast selection is never used.
  2823. When the symbol `auto', fast selection is used if and only if selection
  2824. characters for tags have been configured, either through the variable
  2825. `org-tag-alist' or through a #+TAGS line in the buffer.
  2826. When t, fast selection is always used and selection keys are assigned
  2827. automatically if necessary."
  2828. :group 'org-tags
  2829. :type '(choice
  2830. (const :tag "Always" t)
  2831. (const :tag "Never" nil)
  2832. (const :tag "When selection characters are configured" 'auto)))
  2833. (defcustom org-fast-tag-selection-single-key nil
  2834. "Non-nil means fast tag selection exits after first change.
  2835. When nil, you have to press RET to exit it.
  2836. During fast tag selection, you can toggle this flag with `C-c'.
  2837. This variable can also have the value `expert'. In this case, the window
  2838. displaying the tags menu is not even shown, until you press C-c again."
  2839. :group 'org-tags
  2840. :type '(choice
  2841. (const :tag "No" nil)
  2842. (const :tag "Yes" t)
  2843. (const :tag "Expert" expert)))
  2844. (defvar org-fast-tag-selection-include-todo nil
  2845. "Non-nil means fast tags selection interface will also offer TODO states.
  2846. This is an undocumented feature, you should not rely on it.")
  2847. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2848. "The column to which tags should be indented in a headline.
  2849. If this number is positive, it specifies the column. If it is negative,
  2850. it means that the tags should be flushright to that column. For example,
  2851. -80 works well for a normal 80 character screen.
  2852. When 0, place tags directly after headline text, with only one space in
  2853. between."
  2854. :group 'org-tags
  2855. :type 'integer)
  2856. (defcustom org-auto-align-tags t
  2857. "Non-nil keeps tags aligned when modifying headlines.
  2858. Some operations (i.e. demoting) change the length of a headline and
  2859. therefore shift the tags around. With this option turned on, after
  2860. each such operation the tags are again aligned to `org-tags-column'."
  2861. :group 'org-tags
  2862. :type 'boolean)
  2863. (defcustom org-use-tag-inheritance t
  2864. "Non-nil means tags in levels apply also for sublevels.
  2865. When nil, only the tags directly given in a specific line apply there.
  2866. This may also be a list of tags that should be inherited, or a regexp that
  2867. matches tags that should be inherited. Additional control is possible
  2868. with the variable `org-tags-exclude-from-inheritance' which gives an
  2869. explicit list of tags to be excluded from inheritance, even if the value of
  2870. `org-use-tag-inheritance' would select it for inheritance.
  2871. If this option is t, a match early-on in a tree can lead to a large
  2872. number of matches in the subtree when constructing the agenda or creating
  2873. a sparse tree. If you only want to see the first match in a tree during
  2874. a search, check out the variable `org-tags-match-list-sublevels'."
  2875. :group 'org-tags
  2876. :type '(choice
  2877. (const :tag "Not" nil)
  2878. (const :tag "Always" t)
  2879. (repeat :tag "Specific tags" (string :tag "Tag"))
  2880. (regexp :tag "Tags matched by regexp")))
  2881. (defcustom org-tags-exclude-from-inheritance nil
  2882. "List of tags that should never be inherited.
  2883. This is a way to exclude a few tags from inheritance. For way to do
  2884. the opposite, to actively allow inheritance for selected tags,
  2885. see the variable `org-use-tag-inheritance'."
  2886. :group 'org-tags
  2887. :type '(repeat (string :tag "Tag")))
  2888. (defun org-tag-inherit-p (tag)
  2889. "Check if TAG is one that should be inherited."
  2890. (cond
  2891. ((member tag org-tags-exclude-from-inheritance) nil)
  2892. ((eq org-use-tag-inheritance t) t)
  2893. ((not org-use-tag-inheritance) nil)
  2894. ((stringp org-use-tag-inheritance)
  2895. (string-match org-use-tag-inheritance tag))
  2896. ((listp org-use-tag-inheritance)
  2897. (member tag org-use-tag-inheritance))
  2898. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2899. (defcustom org-tags-match-list-sublevels t
  2900. "Non-nil means list also sublevels of headlines matching a search.
  2901. This variable applies to tags/property searches, and also to stuck
  2902. projects because this search is based on a tags match as well.
  2903. When set to the symbol `indented', sublevels are indented with
  2904. leading dots.
  2905. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2906. the sublevels of a headline matching a tag search often also match
  2907. the same search. Listing all of them can create very long lists.
  2908. Setting this variable to nil causes subtrees of a match to be skipped.
  2909. This variable is semi-obsolete and probably should always be true. It
  2910. is better to limit inheritance to certain tags using the variables
  2911. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2912. :group 'org-tags
  2913. :type '(choice
  2914. (const :tag "No, don't list them" nil)
  2915. (const :tag "Yes, do list them" t)
  2916. (const :tag "List them, indented with leading dots" indented)))
  2917. (defcustom org-tags-sort-function nil
  2918. "When set, tags are sorted using this function as a comparator."
  2919. :group 'org-tags
  2920. :type '(choice
  2921. (const :tag "No sorting" nil)
  2922. (const :tag "Alphabetical" string<)
  2923. (const :tag "Reverse alphabetical" string>)
  2924. (function :tag "Custom function" nil)))
  2925. (defvar org-tags-history nil
  2926. "History of minibuffer reads for tags.")
  2927. (defvar org-last-tags-completion-table nil
  2928. "The last used completion table for tags.")
  2929. (defvar org-after-tags-change-hook nil
  2930. "Hook that is run after the tags in a line have changed.")
  2931. (defgroup org-properties nil
  2932. "Options concerning properties in Org-mode."
  2933. :tag "Org Properties"
  2934. :group 'org)
  2935. (defcustom org-property-format "%-10s %s"
  2936. "How property key/value pairs should be formatted by `indent-line'.
  2937. When `indent-line' hits a property definition, it will format the line
  2938. according to this format, mainly to make sure that the values are
  2939. lined-up with respect to each other."
  2940. :group 'org-properties
  2941. :type 'string)
  2942. (defcustom org-properties-postprocess-alist nil
  2943. "Alist of properties and functions to adjust inserted values.
  2944. Elements of this alist must be of the form
  2945. ([string] [function])
  2946. where [string] must be a property name and [function] must be a
  2947. lambda expression: this lambda expression must take one argument,
  2948. the value to adjust, and return the new value as a string.
  2949. For example, this element will allow the property \"Remaining\"
  2950. to be updated wrt the relation between the \"Effort\" property
  2951. and the clock summary:
  2952. ((\"Remaining\" (lambda(value)
  2953. (let ((clocksum (org-clock-sum-current-item))
  2954. (effort (org-duration-string-to-minutes
  2955. (org-entry-get (point) \"Effort\"))))
  2956. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2957. :group 'org-properties
  2958. :version "24.1"
  2959. :type '(alist :key-type (string :tag "Property")
  2960. :value-type (function :tag "Function")))
  2961. (defcustom org-use-property-inheritance nil
  2962. "Non-nil means properties apply also for sublevels.
  2963. This setting is chiefly used during property searches. Turning it on can
  2964. cause significant overhead when doing a search, which is why it is not
  2965. on by default.
  2966. When nil, only the properties directly given in the current entry count.
  2967. When t, every property is inherited. The value may also be a list of
  2968. properties that should have inheritance, or a regular expression matching
  2969. properties that should be inherited.
  2970. However, note that some special properties use inheritance under special
  2971. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2972. and the properties ending in \"_ALL\" when they are used as descriptor
  2973. for valid values of a property.
  2974. Note for programmers:
  2975. When querying an entry with `org-entry-get', you can control if inheritance
  2976. should be used. By default, `org-entry-get' looks only at the local
  2977. properties. You can request inheritance by setting the inherit argument
  2978. to t (to force inheritance) or to `selective' (to respect the setting
  2979. in this variable)."
  2980. :group 'org-properties
  2981. :type '(choice
  2982. (const :tag "Not" nil)
  2983. (const :tag "Always" t)
  2984. (repeat :tag "Specific properties" (string :tag "Property"))
  2985. (regexp :tag "Properties matched by regexp")))
  2986. (defun org-property-inherit-p (property)
  2987. "Check if PROPERTY is one that should be inherited."
  2988. (cond
  2989. ((eq org-use-property-inheritance t) t)
  2990. ((not org-use-property-inheritance) nil)
  2991. ((stringp org-use-property-inheritance)
  2992. (string-match org-use-property-inheritance property))
  2993. ((listp org-use-property-inheritance)
  2994. (member property org-use-property-inheritance))
  2995. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2996. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2997. "The default column format, if no other format has been defined.
  2998. This variable can be set on the per-file basis by inserting a line
  2999. #+COLUMNS: %25ITEM ....."
  3000. :group 'org-properties
  3001. :type 'string)
  3002. (defcustom org-columns-ellipses ".."
  3003. "The ellipses to be used when a field in column view is truncated.
  3004. When this is the empty string, as many characters as possible are shown,
  3005. but then there will be no visual indication that the field has been truncated.
  3006. When this is a string of length N, the last N characters of a truncated
  3007. field are replaced by this string. If the column is narrower than the
  3008. ellipses string, only part of the ellipses string will be shown."
  3009. :group 'org-properties
  3010. :type 'string)
  3011. (defcustom org-columns-modify-value-for-display-function nil
  3012. "Function that modifies values for display in column view.
  3013. For example, it can be used to cut out a certain part from a time stamp.
  3014. The function must take 2 arguments:
  3015. column-title The title of the column (*not* the property name)
  3016. value The value that should be modified.
  3017. The function should return the value that should be displayed,
  3018. or nil if the normal value should be used."
  3019. :group 'org-properties
  3020. :type 'function)
  3021. (defcustom org-effort-property "Effort"
  3022. "The property that is being used to keep track of effort estimates.
  3023. Effort estimates given in this property need to have the format H:MM."
  3024. :group 'org-properties
  3025. :group 'org-progress
  3026. :type '(string :tag "Property"))
  3027. (defconst org-global-properties-fixed
  3028. '(("VISIBILITY_ALL" . "folded children content all")
  3029. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3030. "List of property/value pairs that can be inherited by any entry.
  3031. These are fixed values, for the preset properties. The user variable
  3032. that can be used to add to this list is `org-global-properties'.
  3033. The entries in this list are cons cells where the car is a property
  3034. name and cdr is a string with the value. If the value represents
  3035. multiple items like an \"_ALL\" property, separate the items by
  3036. spaces.")
  3037. (defcustom org-global-properties nil
  3038. "List of property/value pairs that can be inherited by any entry.
  3039. This list will be combined with the constant `org-global-properties-fixed'.
  3040. The entries in this list are cons cells where the car is a property
  3041. name and cdr is a string with the value.
  3042. You can set buffer-local values for the same purpose in the variable
  3043. `org-file-properties' this by adding lines like
  3044. #+PROPERTY: NAME VALUE"
  3045. :group 'org-properties
  3046. :type '(repeat
  3047. (cons (string :tag "Property")
  3048. (string :tag "Value"))))
  3049. (defvar org-file-properties nil
  3050. "List of property/value pairs that can be inherited by any entry.
  3051. Valid for the current buffer.
  3052. This variable is populated from #+PROPERTY lines.")
  3053. (make-variable-buffer-local 'org-file-properties)
  3054. (defgroup org-agenda nil
  3055. "Options concerning agenda views in Org-mode."
  3056. :tag "Org Agenda"
  3057. :group 'org)
  3058. (defvar org-category nil
  3059. "Variable used by org files to set a category for agenda display.
  3060. Such files should use a file variable to set it, for example
  3061. # -*- mode: org; org-category: \"ELisp\"
  3062. or contain a special line
  3063. #+CATEGORY: ELisp
  3064. If the file does not specify a category, then file's base name
  3065. is used instead.")
  3066. (make-variable-buffer-local 'org-category)
  3067. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3068. (defcustom org-agenda-files nil
  3069. "The files to be used for agenda display.
  3070. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3071. \\[org-remove-file]. You can also use customize to edit the list.
  3072. If an entry is a directory, all files in that directory that are matched by
  3073. `org-agenda-file-regexp' will be part of the file list.
  3074. If the value of the variable is not a list but a single file name, then
  3075. the list of agenda files is actually stored and maintained in that file, one
  3076. agenda file per line. In this file paths can be given relative to
  3077. `org-directory'. Tilde expansion and environment variable substitution
  3078. are also made."
  3079. :group 'org-agenda
  3080. :type '(choice
  3081. (repeat :tag "List of files and directories" file)
  3082. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3083. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3084. "Regular expression to match files for `org-agenda-files'.
  3085. If any element in the list in that variable contains a directory instead
  3086. of a normal file, all files in that directory that are matched by this
  3087. regular expression will be included."
  3088. :group 'org-agenda
  3089. :type 'regexp)
  3090. (defcustom org-agenda-text-search-extra-files nil
  3091. "List of extra files to be searched by text search commands.
  3092. These files will be search in addition to the agenda files by the
  3093. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3094. Note that these files will only be searched for text search commands,
  3095. not for the other agenda views like todo lists, tag searches or the weekly
  3096. agenda. This variable is intended to list notes and possibly archive files
  3097. that should also be searched by these two commands.
  3098. In fact, if the first element in the list is the symbol `agenda-archives',
  3099. than all archive files of all agenda files will be added to the search
  3100. scope."
  3101. :group 'org-agenda
  3102. :type '(set :greedy t
  3103. (const :tag "Agenda Archives" agenda-archives)
  3104. (repeat :inline t (file))))
  3105. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3106. 'org-agenda-text-search-extra-files)
  3107. (defcustom org-agenda-skip-unavailable-files nil
  3108. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3109. A nil value means to remove them, after a query, from the list."
  3110. :group 'org-agenda
  3111. :type 'boolean)
  3112. (defcustom org-calendar-to-agenda-key [?c]
  3113. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3114. The command `org-calendar-goto-agenda' will be bound to this key. The
  3115. default is the character `c' because then `c' can be used to switch back and
  3116. forth between agenda and calendar."
  3117. :group 'org-agenda
  3118. :type 'sexp)
  3119. (defcustom org-calendar-insert-diary-entry-key [?i]
  3120. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3121. This option is irrelevant until `org-agenda-diary-file' has been configured
  3122. to point to an Org-mode file. When that is the case, the command
  3123. `org-agenda-diary-entry' will be bound to the key given here, by default
  3124. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3125. if you want to continue doing this, you need to change this to a different
  3126. key."
  3127. :group 'org-agenda
  3128. :type 'sexp)
  3129. (defcustom org-agenda-diary-file 'diary-file
  3130. "File to which to add new entries with the `i' key in agenda and calendar.
  3131. When this is the symbol `diary-file', the functionality in the Emacs
  3132. calendar will be used to add entries to the `diary-file'. But when this
  3133. points to a file, `org-agenda-diary-entry' will be used instead."
  3134. :group 'org-agenda
  3135. :type '(choice
  3136. (const :tag "The standard Emacs diary file" diary-file)
  3137. (file :tag "Special Org file diary entries")))
  3138. (eval-after-load "calendar"
  3139. '(progn
  3140. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3141. 'org-calendar-goto-agenda)
  3142. (add-hook 'calendar-mode-hook
  3143. (lambda ()
  3144. (unless (eq org-agenda-diary-file 'diary-file)
  3145. (define-key calendar-mode-map
  3146. org-calendar-insert-diary-entry-key
  3147. 'org-agenda-diary-entry))))))
  3148. (defgroup org-latex nil
  3149. "Options for embedding LaTeX code into Org-mode."
  3150. :tag "Org LaTeX"
  3151. :group 'org)
  3152. (defcustom org-format-latex-options
  3153. '(:foreground default :background default :scale 1.0
  3154. :html-foreground "Black" :html-background "Transparent"
  3155. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3156. "Options for creating images from LaTeX fragments.
  3157. This is a property list with the following properties:
  3158. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3159. `default' means use the foreground of the default face.
  3160. `auto' means use the foreground from the text face.
  3161. :background the background color, or \"Transparent\".
  3162. `default' means use the background of the default face.
  3163. `auto' means use the background from the text face.
  3164. :scale a scaling factor for the size of the images, to get more pixels
  3165. :html-foreground, :html-background, :html-scale
  3166. the same numbers for HTML export.
  3167. :matchers a list indicating which matchers should be used to
  3168. find LaTeX fragments. Valid members of this list are:
  3169. \"begin\" find environments
  3170. \"$1\" find single characters surrounded by $.$
  3171. \"$\" find math expressions surrounded by $...$
  3172. \"$$\" find math expressions surrounded by $$....$$
  3173. \"\\(\" find math expressions surrounded by \\(...\\)
  3174. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3175. :group 'org-latex
  3176. :type 'plist)
  3177. (defcustom org-format-latex-signal-error t
  3178. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3179. When nil, just push out a message."
  3180. :group 'org-latex
  3181. :version "24.1"
  3182. :type 'boolean)
  3183. (defcustom org-latex-to-mathml-jar-file nil
  3184. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3185. Use this to specify additional executable file say a jar file.
  3186. When using MathToWeb as the converter, specify the full-path to
  3187. your mathtoweb.jar file."
  3188. :group 'org-latex
  3189. :version "24.1"
  3190. :type '(choice
  3191. (const :tag "None" nil)
  3192. (file :tag "JAR file" :must-match t)))
  3193. (defcustom org-latex-to-mathml-convert-command nil
  3194. "Command to convert LaTeX fragments to MathML.
  3195. Replace format-specifiers in the command as noted below and use
  3196. `shell-command' to convert LaTeX to MathML.
  3197. %j: Executable file in fully expanded form as specified by
  3198. `org-latex-to-mathml-jar-file'.
  3199. %I: Input LaTeX file in fully expanded form
  3200. %o: Output MathML file
  3201. This command is used by `org-create-math-formula'.
  3202. When using MathToWeb as the converter, set this to
  3203. \"java -jar %j -unicode -force -df %o %I\"."
  3204. :group 'org-latex
  3205. :version "24.1"
  3206. :type '(choice
  3207. (const :tag "None" nil)
  3208. (string :tag "\nShell command")))
  3209. (defcustom org-latex-create-formula-image-program 'dvipng
  3210. "Program to convert LaTeX fragments with.
  3211. dvipng Process the LaTeX fragments to dvi file, then convert
  3212. dvi files to png files using dvipng.
  3213. This will also include processing of non-math environments.
  3214. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3215. to convert pdf files to png files"
  3216. :group 'org-latex
  3217. :version "24.1"
  3218. :type '(choice
  3219. (const :tag "dvipng" dvipng)
  3220. (const :tag "imagemagick" imagemagick)))
  3221. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3222. "Path to store latex preview images.
  3223. A relative path here creates many directories relative to the
  3224. processed org files paths. An absolute path puts all preview
  3225. images at the same place."
  3226. :group 'org-latex
  3227. :version "24.3"
  3228. :type 'string)
  3229. (defun org-format-latex-mathml-available-p ()
  3230. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3231. (save-match-data
  3232. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3233. org-latex-to-mathml-convert-command)
  3234. (let ((executable (car (split-string
  3235. org-latex-to-mathml-convert-command))))
  3236. (when (executable-find executable)
  3237. (if (string-match
  3238. "%j" org-latex-to-mathml-convert-command)
  3239. (file-readable-p org-latex-to-mathml-jar-file)
  3240. t))))))
  3241. (defcustom org-format-latex-header "\\documentclass{article}
  3242. \\usepackage[usenames]{color}
  3243. \\usepackage{amsmath}
  3244. \\usepackage[mathscr]{eucal}
  3245. \\pagestyle{empty} % do not remove
  3246. \[PACKAGES]
  3247. \[DEFAULT-PACKAGES]
  3248. % The settings below are copied from fullpage.sty
  3249. \\setlength{\\textwidth}{\\paperwidth}
  3250. \\addtolength{\\textwidth}{-3cm}
  3251. \\setlength{\\oddsidemargin}{1.5cm}
  3252. \\addtolength{\\oddsidemargin}{-2.54cm}
  3253. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3254. \\setlength{\\textheight}{\\paperheight}
  3255. \\addtolength{\\textheight}{-\\headheight}
  3256. \\addtolength{\\textheight}{-\\headsep}
  3257. \\addtolength{\\textheight}{-\\footskip}
  3258. \\addtolength{\\textheight}{-3cm}
  3259. \\setlength{\\topmargin}{1.5cm}
  3260. \\addtolength{\\topmargin}{-2.54cm}"
  3261. "The document header used for processing LaTeX fragments.
  3262. It is imperative that this header make sure that no page number
  3263. appears on the page. The package defined in the variables
  3264. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3265. will either replace the placeholder \"[PACKAGES]\" in this
  3266. header, or they will be appended."
  3267. :group 'org-latex
  3268. :type 'string)
  3269. (defun org-set-packages-alist (var val)
  3270. "Set the packages alist and make sure it has 3 elements per entry."
  3271. (set var (mapcar (lambda (x)
  3272. (if (and (consp x) (= (length x) 2))
  3273. (list (car x) (nth 1 x) t)
  3274. x))
  3275. val)))
  3276. (defun org-get-packages-alist (var)
  3277. "Get the packages alist and make sure it has 3 elements per entry."
  3278. (mapcar (lambda (x)
  3279. (if (and (consp x) (= (length x) 2))
  3280. (list (car x) (nth 1 x) t)
  3281. x))
  3282. (default-value var)))
  3283. (defcustom org-latex-default-packages-alist
  3284. '(("AUTO" "inputenc" t)
  3285. ("T1" "fontenc" t)
  3286. ("" "fixltx2e" nil)
  3287. ("" "graphicx" t)
  3288. ("" "longtable" nil)
  3289. ("" "float" nil)
  3290. ("" "wrapfig" nil)
  3291. ("" "soul" t)
  3292. ("" "textcomp" t)
  3293. ("" "marvosym" t)
  3294. ("" "wasysym" t)
  3295. ("" "latexsym" t)
  3296. ("" "amssymb" t)
  3297. ("" "hyperref" nil)
  3298. "\\tolerance=1000")
  3299. "Alist of default packages to be inserted in the header.
  3300. Change this only if one of the packages here causes an
  3301. incompatibility with another package you are using.
  3302. The packages in this list are needed by one part or another of
  3303. Org mode to function properly:
  3304. - inputenc, fontenc: for basic font and character selection
  3305. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3306. symbols used for interpreting the entities in `org-entities'.
  3307. You can skip some of these packages if you don't use any of the
  3308. symbols in it.
  3309. - graphicx: for including images
  3310. - float, wrapfig: for figure placement
  3311. - longtable: for long tables
  3312. - hyperref: for cross references
  3313. Therefore you should not modify this variable unless you know
  3314. what you are doing. The one reason to change it anyway is that
  3315. you might be loading some other package that conflicts with one
  3316. of the default packages. Each cell is of the format
  3317. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3318. the package also needs to be included when compiling LaTeX
  3319. snippets into images for inclusion into non-LaTeX output."
  3320. :group 'org-latex
  3321. :group 'org-export-latex
  3322. :set 'org-set-packages-alist
  3323. :get 'org-get-packages-alist
  3324. :version "24.1"
  3325. :type '(repeat
  3326. (choice
  3327. (list :tag "options/package pair"
  3328. (string :tag "options")
  3329. (string :tag "package")
  3330. (boolean :tag "Snippet"))
  3331. (string :tag "A line of LaTeX"))))
  3332. (defcustom org-latex-packages-alist nil
  3333. "Alist of packages to be inserted in every LaTeX header.
  3334. These will be inserted after `org-latex-default-packages-alist'.
  3335. Each cell is of the format:
  3336. \(\"options\" \"package\" snippet-flag)
  3337. SNIPPET-FLAG, when t, indicates that this package is also needed
  3338. when turning LaTeX snippets into images for inclusion into
  3339. non-LaTeX output.
  3340. Make sure that you only list packages here which:
  3341. - you want in every file
  3342. - do not conflict with the setup in `org-format-latex-header'.
  3343. - do not conflict with the default packages in
  3344. `org-latex-default-packages-alist'."
  3345. :group 'org-latex
  3346. :group 'org-export-latex
  3347. :set 'org-set-packages-alist
  3348. :get 'org-get-packages-alist
  3349. :type '(repeat
  3350. (choice
  3351. (list :tag "options/package pair"
  3352. (string :tag "options")
  3353. (string :tag "package")
  3354. (boolean :tag "Snippet"))
  3355. (string :tag "A line of LaTeX"))))
  3356. (defgroup org-appearance nil
  3357. "Settings for Org-mode appearance."
  3358. :tag "Org Appearance"
  3359. :group 'org)
  3360. (defcustom org-level-color-stars-only nil
  3361. "Non-nil means fontify only the stars in each headline.
  3362. When nil, the entire headline is fontified.
  3363. Changing it requires restart of `font-lock-mode' to become effective
  3364. also in regions already fontified."
  3365. :group 'org-appearance
  3366. :type 'boolean)
  3367. (defcustom org-hide-leading-stars nil
  3368. "Non-nil means hide the first N-1 stars in a headline.
  3369. This works by using the face `org-hide' for these stars. This
  3370. face is white for a light background, and black for a dark
  3371. background. You may have to customize the face `org-hide' to
  3372. make this work.
  3373. Changing it requires restart of `font-lock-mode' to become effective
  3374. also in regions already fontified.
  3375. You may also set this on a per-file basis by adding one of the following
  3376. lines to the buffer:
  3377. #+STARTUP: hidestars
  3378. #+STARTUP: showstars"
  3379. :group 'org-appearance
  3380. :type 'boolean)
  3381. (defcustom org-hidden-keywords nil
  3382. "List of symbols corresponding to keywords to be hidden the org buffer.
  3383. For example, a value '(title) for this list will make the document's title
  3384. appear in the buffer without the initial #+TITLE: keyword."
  3385. :group 'org-appearance
  3386. :version "24.1"
  3387. :type '(set (const :tag "#+AUTHOR" author)
  3388. (const :tag "#+DATE" date)
  3389. (const :tag "#+EMAIL" email)
  3390. (const :tag "#+TITLE" title)))
  3391. (defcustom org-custom-properties nil
  3392. "List of properties (as strings) with a special meaning.
  3393. The default use of these custom properties is to let the user
  3394. hide them with `org-toggle-custom-properties-visibility'."
  3395. :group 'org-properties
  3396. :group 'org-appearance
  3397. :version "24.3"
  3398. :type '(repeat (string :tag "Property Name")))
  3399. (defcustom org-fontify-done-headline nil
  3400. "Non-nil means change the face of a headline if it is marked DONE.
  3401. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3402. When this is non-nil, the headline after the keyword is set to the
  3403. `org-headline-done' as an additional indication."
  3404. :group 'org-appearance
  3405. :type 'boolean)
  3406. (defcustom org-fontify-emphasized-text t
  3407. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3408. Changing this variable requires a restart of Emacs to take effect."
  3409. :group 'org-appearance
  3410. :type 'boolean)
  3411. (defcustom org-fontify-whole-heading-line nil
  3412. "Non-nil means fontify the whole line for headings.
  3413. This is useful when setting a background color for the
  3414. org-level-* faces."
  3415. :group 'org-appearance
  3416. :type 'boolean)
  3417. (defcustom org-highlight-latex-and-related nil
  3418. "Non-nil means highlight LaTeX related syntax in the buffer.
  3419. When non nil, the value should be a list containing any of the
  3420. following symbols:
  3421. `latex' Highlight LaTeX snippets and environments.
  3422. `script' Highlight subscript and superscript.
  3423. `entities' Highlight entities."
  3424. :group 'org-appearance
  3425. :version "24.4"
  3426. :package-version '(Org . "8.0")
  3427. :type '(choice
  3428. (const :tag "No highlighting" nil)
  3429. (set :greedy t :tag "Highlight"
  3430. (const :tag "LaTeX snippets and environments" latex)
  3431. (const :tag "Subscript and superscript" script)
  3432. (const :tag "Entities" entities))))
  3433. (defcustom org-hide-emphasis-markers nil
  3434. "Non-nil mean font-lock should hide the emphasis marker characters."
  3435. :group 'org-appearance
  3436. :type 'boolean)
  3437. (defcustom org-pretty-entities nil
  3438. "Non-nil means show entities as UTF8 characters.
  3439. When nil, the \\name form remains in the buffer."
  3440. :group 'org-appearance
  3441. :version "24.1"
  3442. :type 'boolean)
  3443. (defcustom org-pretty-entities-include-sub-superscripts t
  3444. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3445. :group 'org-appearance
  3446. :version "24.1"
  3447. :type 'boolean)
  3448. (defvar org-emph-re nil
  3449. "Regular expression for matching emphasis.
  3450. After a match, the match groups contain these elements:
  3451. 0 The match of the full regular expression, including the characters
  3452. before and after the proper match
  3453. 1 The character before the proper match, or empty at beginning of line
  3454. 2 The proper match, including the leading and trailing markers
  3455. 3 The leading marker like * or /, indicating the type of highlighting
  3456. 4 The text between the emphasis markers, not including the markers
  3457. 5 The character after the match, empty at the end of a line")
  3458. (defvar org-verbatim-re nil
  3459. "Regular expression for matching verbatim text.")
  3460. (defvar org-emphasis-regexp-components) ; defined just below
  3461. (defvar org-emphasis-alist) ; defined just below
  3462. (defun org-set-emph-re (var val)
  3463. "Set variable and compute the emphasis regular expression."
  3464. (set var val)
  3465. (when (and (boundp 'org-emphasis-alist)
  3466. (boundp 'org-emphasis-regexp-components)
  3467. org-emphasis-alist org-emphasis-regexp-components)
  3468. (let* ((e org-emphasis-regexp-components)
  3469. (pre (car e))
  3470. (post (nth 1 e))
  3471. (border (nth 2 e))
  3472. (body (nth 3 e))
  3473. (nl (nth 4 e))
  3474. (body1 (concat body "*?"))
  3475. (markers (mapconcat 'car org-emphasis-alist ""))
  3476. (vmarkers (mapconcat
  3477. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3478. org-emphasis-alist "")))
  3479. ;; make sure special characters appear at the right position in the class
  3480. (if (string-match "\\^" markers)
  3481. (setq markers (concat (replace-match "" t t markers) "^")))
  3482. (if (string-match "-" markers)
  3483. (setq markers (concat (replace-match "" t t markers) "-")))
  3484. (if (string-match "\\^" vmarkers)
  3485. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3486. (if (string-match "-" vmarkers)
  3487. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3488. (if (> nl 0)
  3489. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3490. (int-to-string nl) "\\}")))
  3491. ;; Make the regexp
  3492. (setq org-emph-re
  3493. (concat "\\([" pre "]\\|^\\)"
  3494. "\\("
  3495. "\\([" markers "]\\)"
  3496. "\\("
  3497. "[^" border "]\\|"
  3498. "[^" border "]"
  3499. body1
  3500. "[^" border "]"
  3501. "\\)"
  3502. "\\3\\)"
  3503. "\\([" post "]\\|$\\)"))
  3504. (setq org-verbatim-re
  3505. (concat "\\([" pre "]\\|^\\)"
  3506. "\\("
  3507. "\\([" vmarkers "]\\)"
  3508. "\\("
  3509. "[^" border "]\\|"
  3510. "[^" border "]"
  3511. body1
  3512. "[^" border "]"
  3513. "\\)"
  3514. "\\3\\)"
  3515. "\\([" post "]\\|$\\)")))))
  3516. (defcustom org-emphasis-regexp-components
  3517. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3518. "Components used to build the regular expression for emphasis.
  3519. This is a list with five entries. Terminology: In an emphasis string
  3520. like \" *strong word* \", we call the initial space PREMATCH, the final
  3521. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3522. and \"trong wor\" is the body. The different components in this variable
  3523. specify what is allowed/forbidden in each part:
  3524. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3525. post Chars allowed as postmatch. End of line will be allowed too.
  3526. border The chars *forbidden* as border characters.
  3527. body-regexp A regexp like \".\" to match a body character. Don't use
  3528. non-shy groups here, and don't allow newline here.
  3529. newline The maximum number of newlines allowed in an emphasis exp.
  3530. Use customize to modify this, or restart Emacs after changing it."
  3531. :group 'org-appearance
  3532. :set 'org-set-emph-re
  3533. :type '(list
  3534. (sexp :tag "Allowed chars in pre ")
  3535. (sexp :tag "Allowed chars in post ")
  3536. (sexp :tag "Forbidden chars in border ")
  3537. (sexp :tag "Regexp for body ")
  3538. (integer :tag "number of newlines allowed")
  3539. (option (boolean :tag "Please ignore this button"))))
  3540. (defcustom org-emphasis-alist
  3541. `(("*" bold "<b>" "</b>")
  3542. ("/" italic "<i>" "</i>")
  3543. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3544. ("=" org-code "<code>" "</code>" verbatim)
  3545. ("~" org-verbatim "<code>" "</code>" verbatim)
  3546. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3547. "<del>" "</del>")
  3548. )
  3549. "Special syntax for emphasized text.
  3550. Text starting and ending with a special character will be emphasized, for
  3551. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3552. characters, the face to be used by font-lock for highlighting in Org-mode
  3553. Emacs buffers, and the HTML tags to be used for this.
  3554. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3555. Use customize to modify this, or restart Emacs after changing it."
  3556. :group 'org-appearance
  3557. :set 'org-set-emph-re
  3558. :type '(repeat
  3559. (list
  3560. (string :tag "Marker character")
  3561. (choice
  3562. (face :tag "Font-lock-face")
  3563. (plist :tag "Face property list"))
  3564. (string :tag "HTML start tag")
  3565. (string :tag "HTML end tag")
  3566. (option (const verbatim)))))
  3567. (defvar org-protecting-blocks
  3568. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3569. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3570. This is needed for font-lock setup.")
  3571. ;;; Miscellaneous options
  3572. (defgroup org-completion nil
  3573. "Completion in Org-mode."
  3574. :tag "Org Completion"
  3575. :group 'org)
  3576. (defcustom org-completion-use-ido nil
  3577. "Non-nil means use ido completion wherever possible.
  3578. Note that `ido-mode' must be active for this variable to be relevant.
  3579. If you decide to turn this variable on, you might well want to turn off
  3580. `org-outline-path-complete-in-steps'.
  3581. See also `org-completion-use-iswitchb'."
  3582. :group 'org-completion
  3583. :type 'boolean)
  3584. (defcustom org-completion-use-iswitchb nil
  3585. "Non-nil means use iswitchb completion wherever possible.
  3586. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3587. If you decide to turn this variable on, you might well want to turn off
  3588. `org-outline-path-complete-in-steps'.
  3589. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3590. :group 'org-completion
  3591. :type 'boolean)
  3592. (defcustom org-completion-fallback-command 'hippie-expand
  3593. "The expansion command called by \\[pcomplete] in normal context.
  3594. Normal means, no org-mode-specific context."
  3595. :group 'org-completion
  3596. :type 'function)
  3597. ;;; Functions and variables from their packages
  3598. ;; Declared here to avoid compiler warnings
  3599. ;; XEmacs only
  3600. (defvar outline-mode-menu-heading)
  3601. (defvar outline-mode-menu-show)
  3602. (defvar outline-mode-menu-hide)
  3603. (defvar zmacs-regions) ; XEmacs regions
  3604. ;; Emacs only
  3605. (defvar mark-active)
  3606. ;; Various packages
  3607. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3608. (declare-function calendar-forward-day "cal-move" (arg))
  3609. (declare-function calendar-goto-date "cal-move" (date))
  3610. (declare-function calendar-goto-today "cal-move" ())
  3611. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3612. (defvar calc-embedded-close-formula)
  3613. (defvar calc-embedded-open-formula)
  3614. (declare-function cdlatex-tab "ext:cdlatex" ())
  3615. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3616. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3617. (defvar font-lock-unfontify-region-function)
  3618. (declare-function iswitchb-read-buffer "iswitchb"
  3619. (prompt &optional default require-match start matches-set))
  3620. (defvar iswitchb-temp-buflist)
  3621. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3622. (defvar org-agenda-tags-todo-honor-ignore-options)
  3623. (declare-function org-agenda-skip "org-agenda" ())
  3624. (declare-function
  3625. org-agenda-format-item "org-agenda"
  3626. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3627. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3628. (declare-function org-agenda-change-all-lines "org-agenda"
  3629. (newhead hdmarker &optional fixface just-this))
  3630. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3631. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3632. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3633. (beg end))
  3634. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3635. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3636. "org-agenda" (&optional end))
  3637. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3638. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3639. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3640. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3641. (declare-function org-indent-mode "org-indent" (&optional arg))
  3642. (declare-function parse-time-string "parse-time" (string))
  3643. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3644. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3645. (defvar remember-data-file)
  3646. (defvar texmathp-why)
  3647. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3648. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3649. (defvar org-latex-regexps)
  3650. ;;; Autoload and prepare some org modules
  3651. ;; Some table stuff that needs to be defined here, because it is used
  3652. ;; by the functions setting up org-mode or checking for table context.
  3653. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3654. "Detect an org-type or table-type table.")
  3655. (defconst org-table-line-regexp "^[ \t]*|"
  3656. "Detect an org-type table line.")
  3657. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3658. "Detect an org-type table line.")
  3659. (defconst org-table-hline-regexp "^[ \t]*|-"
  3660. "Detect an org-type table hline.")
  3661. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3662. "Detect a table-type table hline.")
  3663. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3664. "Detect the first line outside a table when searching from within it.
  3665. This works for both table types.")
  3666. ;; Autoload the functions in org-table.el that are needed by functions here.
  3667. (eval-and-compile
  3668. (org-autoload "org-table"
  3669. '(org-table-begin org-table-blank-field org-table-end)))
  3670. ;;;###autoload
  3671. (defun turn-on-orgtbl ()
  3672. "Unconditionally turn on `orgtbl-mode'."
  3673. (require 'org-table)
  3674. (orgtbl-mode 1))
  3675. (defun org-at-table-p (&optional table-type)
  3676. "Return t if the cursor is inside an org-type table.
  3677. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3678. (if org-enable-table-editor
  3679. (save-excursion
  3680. (beginning-of-line 1)
  3681. (looking-at (if table-type org-table-any-line-regexp
  3682. org-table-line-regexp)))
  3683. nil))
  3684. (defsubst org-table-p () (org-at-table-p))
  3685. (defun org-at-table.el-p ()
  3686. "Return t if and only if we are at a table.el table."
  3687. (and (org-at-table-p 'any)
  3688. (save-excursion
  3689. (goto-char (org-table-begin 'any))
  3690. (looking-at org-table1-hline-regexp))))
  3691. (defun org-table-recognize-table.el ()
  3692. "If there is a table.el table nearby, recognize it and move into it."
  3693. (if org-table-tab-recognizes-table.el
  3694. (if (org-at-table.el-p)
  3695. (progn
  3696. (beginning-of-line 1)
  3697. (if (looking-at org-table-dataline-regexp)
  3698. nil
  3699. (if (looking-at org-table1-hline-regexp)
  3700. (progn
  3701. (beginning-of-line 2)
  3702. (if (looking-at org-table-any-border-regexp)
  3703. (beginning-of-line -1)))))
  3704. (if (re-search-forward "|" (org-table-end t) t)
  3705. (progn
  3706. (require 'table)
  3707. (if (table--at-cell-p (point))
  3708. t
  3709. (message "recognizing table.el table...")
  3710. (table-recognize-table)
  3711. (message "recognizing table.el table...done")))
  3712. (error "This should not happen"))
  3713. t)
  3714. nil)
  3715. nil))
  3716. (defun org-at-table-hline-p ()
  3717. "Return t if the cursor is inside a hline in a table."
  3718. (if org-enable-table-editor
  3719. (save-excursion
  3720. (beginning-of-line 1)
  3721. (looking-at org-table-hline-regexp))
  3722. nil))
  3723. (defvar org-table-clean-did-remove-column nil)
  3724. (defun org-table-map-tables (function &optional quietly)
  3725. "Apply FUNCTION to the start of all tables in the buffer."
  3726. (save-excursion
  3727. (save-restriction
  3728. (widen)
  3729. (goto-char (point-min))
  3730. (while (re-search-forward org-table-any-line-regexp nil t)
  3731. (unless quietly
  3732. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3733. (beginning-of-line 1)
  3734. (when (and (looking-at org-table-line-regexp)
  3735. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3736. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3737. (save-excursion (funcall function))
  3738. (or (looking-at org-table-line-regexp)
  3739. (forward-char 1)))
  3740. (re-search-forward org-table-any-border-regexp nil 1))))
  3741. (unless quietly (message "Mapping tables: done")))
  3742. ;; Declare and autoload functions from ox.el and al.
  3743. (declare-function org-export-get-environment "ox"
  3744. (&optional backend subtreep ext-plist))
  3745. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3746. ;; Declare and autoload functions from org-agenda.el
  3747. (eval-and-compile
  3748. (org-autoload "org-agenda"
  3749. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3750. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3751. (declare-function org-clock-update-mode-line "org-clock" ())
  3752. (declare-function org-resolve-clocks "org-clock"
  3753. (&optional also-non-dangling-p prompt last-valid))
  3754. (defvar org-clock-start-time)
  3755. (defvar org-clock-marker (make-marker)
  3756. "Marker recording the last clock-in.")
  3757. (defvar org-clock-hd-marker (make-marker)
  3758. "Marker recording the last clock-in, but the headline position.")
  3759. (defvar org-clock-heading ""
  3760. "The heading of the current clock entry.")
  3761. (defun org-clock-is-active ()
  3762. "Return non-nil if clock is currently running.
  3763. The return value is actually the clock marker."
  3764. (marker-buffer org-clock-marker))
  3765. (eval-and-compile
  3766. (org-autoload "org-clock" '(org-clock-remove-overlays
  3767. org-clock-update-time-maybe
  3768. org-clocktable-shift)))
  3769. (defun org-check-running-clock ()
  3770. "Check if the current buffer contains the running clock.
  3771. If yes, offer to stop it and to save the buffer with the changes."
  3772. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3773. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3774. (buffer-name))))
  3775. (org-clock-out)
  3776. (when (y-or-n-p "Save changed buffer?")
  3777. (save-buffer))))
  3778. (defun org-clocktable-try-shift (dir n)
  3779. "Check if this line starts a clock table, if yes, shift the time block."
  3780. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3781. (org-clocktable-shift dir n)))
  3782. ;;;###autoload
  3783. (defun org-clock-persistence-insinuate ()
  3784. "Set up hooks for clock persistence."
  3785. (require 'org-clock)
  3786. (add-hook 'org-mode-hook 'org-clock-load)
  3787. (add-hook 'kill-emacs-hook 'org-clock-save))
  3788. ;; Define the variable already here, to make sure we have it.
  3789. (defvar org-indent-mode nil
  3790. "Non-nil if Org-Indent mode is enabled.
  3791. Use the command `org-indent-mode' to change this variable.")
  3792. ;; Autoload archiving code
  3793. ;; The stuff that is needed for cycling and tags has to be defined here.
  3794. (defgroup org-archive nil
  3795. "Options concerning archiving in Org-mode."
  3796. :tag "Org Archive"
  3797. :group 'org-structure)
  3798. (defcustom org-archive-location "%s_archive::"
  3799. "The location where subtrees should be archived.
  3800. The value of this variable is a string, consisting of two parts,
  3801. separated by a double-colon. The first part is a filename and
  3802. the second part is a headline.
  3803. When the filename is omitted, archiving happens in the same file.
  3804. %s in the filename will be replaced by the current file
  3805. name (without the directory part). Archiving to a different file
  3806. is useful to keep archived entries from contributing to the
  3807. Org-mode Agenda.
  3808. The archived entries will be filed as subtrees of the specified
  3809. headline. When the headline is omitted, the subtrees are simply
  3810. filed away at the end of the file, as top-level entries. Also in
  3811. the heading you can use %s to represent the file name, this can be
  3812. useful when using the same archive for a number of different files.
  3813. Here are a few examples:
  3814. \"%s_archive::\"
  3815. If the current file is Projects.org, archive in file
  3816. Projects.org_archive, as top-level trees. This is the default.
  3817. \"::* Archived Tasks\"
  3818. Archive in the current file, under the top-level headline
  3819. \"* Archived Tasks\".
  3820. \"~/org/archive.org::\"
  3821. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3822. \"~/org/archive.org::* From %s\"
  3823. Archive in file ~/org/archive.org (absolute path), under headlines
  3824. \"From FILENAME\" where file name is the current file name.
  3825. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3826. The \"datetree/\" string is special, signifying to archive
  3827. items to the datetree. Items are placed in either the CLOSED
  3828. date of the item, or the current date if there is no CLOSED date.
  3829. The heading will be a subentry to the current date. There doesn't
  3830. need to be a heading, but there always needs to be a slash after
  3831. datetree. For example, to store archived items directly in the
  3832. datetree, use \"~/org/datetree.org::datetree/\".
  3833. \"basement::** Finished Tasks\"
  3834. Archive in file ./basement (relative path), as level 3 trees
  3835. below the level 2 heading \"** Finished Tasks\".
  3836. You may set this option on a per-file basis by adding to the buffer a
  3837. line like
  3838. #+ARCHIVE: basement::** Finished Tasks
  3839. You may also define it locally for a subtree by setting an ARCHIVE property
  3840. in the entry. If such a property is found in an entry, or anywhere up
  3841. the hierarchy, it will be used."
  3842. :group 'org-archive
  3843. :type 'string)
  3844. (defcustom org-archive-tag "ARCHIVE"
  3845. "The tag that marks a subtree as archived.
  3846. An archived subtree does not open during visibility cycling, and does
  3847. not contribute to the agenda listings.
  3848. After changing this, font-lock must be restarted in the relevant buffers to
  3849. get the proper fontification."
  3850. :group 'org-archive
  3851. :group 'org-keywords
  3852. :type 'string)
  3853. (defcustom org-agenda-skip-archived-trees t
  3854. "Non-nil means the agenda will skip any items located in archived trees.
  3855. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3856. variable is no longer recommended, you should leave it at the value t.
  3857. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3858. :group 'org-archive
  3859. :group 'org-agenda-skip
  3860. :type 'boolean)
  3861. (defcustom org-columns-skip-archived-trees t
  3862. "Non-nil means ignore archived trees when creating column view."
  3863. :group 'org-archive
  3864. :group 'org-properties
  3865. :type 'boolean)
  3866. (defcustom org-cycle-open-archived-trees nil
  3867. "Non-nil means `org-cycle' will open archived trees.
  3868. An archived tree is a tree marked with the tag ARCHIVE.
  3869. When nil, archived trees will stay folded. You can still open them with
  3870. normal outline commands like `show-all', but not with the cycling commands."
  3871. :group 'org-archive
  3872. :group 'org-cycle
  3873. :type 'boolean)
  3874. (defcustom org-sparse-tree-open-archived-trees nil
  3875. "Non-nil means sparse tree construction shows matches in archived trees.
  3876. When nil, matches in these trees are highlighted, but the trees are kept in
  3877. collapsed state."
  3878. :group 'org-archive
  3879. :group 'org-sparse-trees
  3880. :type 'boolean)
  3881. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3882. "The default date type when building a sparse tree.
  3883. When this is nil, a date is a scheduled or a deadline timestamp.
  3884. Otherwise, these types are allowed:
  3885. all: all timestamps
  3886. active: only active timestamps (<...>)
  3887. inactive: only inactive timestamps (<...)
  3888. scheduled: only scheduled timestamps
  3889. deadline: only deadline timestamps"
  3890. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3891. (const :tag "All timestamps" all)
  3892. (const :tag "Only active timestamps" active)
  3893. (const :tag "Only inactive timestamps" inactive)
  3894. (const :tag "Only scheduled timestamps" scheduled)
  3895. (const :tag "Only deadline timestamps" deadline)
  3896. (const :tag "Only closed timestamps" closed))
  3897. :version "24.3"
  3898. :group 'org-sparse-trees)
  3899. (defun org-cycle-hide-archived-subtrees (state)
  3900. "Re-hide all archived subtrees after a visibility state change."
  3901. (when (and (not org-cycle-open-archived-trees)
  3902. (not (memq state '(overview folded))))
  3903. (save-excursion
  3904. (let* ((globalp (memq state '(contents all)))
  3905. (beg (if globalp (point-min) (point)))
  3906. (end (if globalp (point-max) (org-end-of-subtree t))))
  3907. (org-hide-archived-subtrees beg end)
  3908. (goto-char beg)
  3909. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3910. (message "%s" (substitute-command-keys
  3911. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3912. (defun org-force-cycle-archived ()
  3913. "Cycle subtree even if it is archived."
  3914. (interactive)
  3915. (setq this-command 'org-cycle)
  3916. (let ((org-cycle-open-archived-trees t))
  3917. (call-interactively 'org-cycle)))
  3918. (defun org-hide-archived-subtrees (beg end)
  3919. "Re-hide all archived subtrees after a visibility state change."
  3920. (save-excursion
  3921. (let* ((re (concat ":" org-archive-tag ":")))
  3922. (goto-char beg)
  3923. (while (re-search-forward re end t)
  3924. (when (org-at-heading-p)
  3925. (org-flag-subtree t)
  3926. (org-end-of-subtree t))))))
  3927. (declare-function outline-end-of-heading "outline" ())
  3928. (declare-function outline-flag-region "outline" (from to flag))
  3929. (defun org-flag-subtree (flag)
  3930. (save-excursion
  3931. (org-back-to-heading t)
  3932. (outline-end-of-heading)
  3933. (outline-flag-region (point)
  3934. (progn (org-end-of-subtree t) (point))
  3935. flag)))
  3936. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3937. (eval-and-compile
  3938. (org-autoload "org-archive"
  3939. '(org-add-archive-files)))
  3940. ;; Autoload Column View Code
  3941. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3942. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3943. (declare-function org-columns-compute "org-colview" (property))
  3944. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3945. '(org-columns-number-to-string
  3946. org-columns-get-format-and-top-level
  3947. org-columns-compute
  3948. org-columns-remove-overlays))
  3949. ;; Autoload ID code
  3950. (declare-function org-id-store-link "org-id")
  3951. (declare-function org-id-locations-load "org-id")
  3952. (declare-function org-id-locations-save "org-id")
  3953. (defvar org-id-track-globally)
  3954. (org-autoload "org-id"
  3955. '(org-id-new
  3956. org-id-copy
  3957. org-id-get-with-outline-path-completion
  3958. org-id-get-with-outline-drilling))
  3959. ;;; Variables for pre-computed regular expressions, all buffer local
  3960. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3961. "Matches first line of a hidden block.")
  3962. (make-variable-buffer-local 'org-drawer-regexp)
  3963. (defvar org-todo-regexp nil
  3964. "Matches any of the TODO state keywords.")
  3965. (make-variable-buffer-local 'org-todo-regexp)
  3966. (defvar org-not-done-regexp nil
  3967. "Matches any of the TODO state keywords except the last one.")
  3968. (make-variable-buffer-local 'org-not-done-regexp)
  3969. (defvar org-not-done-heading-regexp nil
  3970. "Matches a TODO headline that is not done.")
  3971. (make-variable-buffer-local 'org-not-done-regexp)
  3972. (defvar org-todo-line-regexp nil
  3973. "Matches a headline and puts TODO state into group 2 if present.")
  3974. (make-variable-buffer-local 'org-todo-line-regexp)
  3975. (defvar org-complex-heading-regexp nil
  3976. "Matches a headline and puts everything into groups:
  3977. group 1: the stars
  3978. group 2: The todo keyword, maybe
  3979. group 3: Priority cookie
  3980. group 4: True headline
  3981. group 5: Tags")
  3982. (make-variable-buffer-local 'org-complex-heading-regexp)
  3983. (defvar org-complex-heading-regexp-format nil
  3984. "Printf format to make regexp to match an exact headline.
  3985. This regexp will match the headline of any node which has the
  3986. exact headline text that is put into the format, but may have any
  3987. TODO state, priority and tags.")
  3988. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3989. (defvar org-todo-line-tags-regexp nil
  3990. "Matches a headline and puts TODO state into group 2 if present.
  3991. Also put tags into group 4 if tags are present.")
  3992. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3993. (defvar org-ds-keyword-length 12
  3994. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3995. (make-variable-buffer-local 'org-ds-keyword-length)
  3996. (defvar org-deadline-regexp nil
  3997. "Matches the DEADLINE keyword.")
  3998. (make-variable-buffer-local 'org-deadline-regexp)
  3999. (defvar org-deadline-time-regexp nil
  4000. "Matches the DEADLINE keyword together with a time stamp.")
  4001. (make-variable-buffer-local 'org-deadline-time-regexp)
  4002. (defvar org-deadline-time-hour-regexp nil
  4003. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4004. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4005. (defvar org-deadline-line-regexp nil
  4006. "Matches the DEADLINE keyword and the rest of the line.")
  4007. (make-variable-buffer-local 'org-deadline-line-regexp)
  4008. (defvar org-scheduled-regexp nil
  4009. "Matches the SCHEDULED keyword.")
  4010. (make-variable-buffer-local 'org-scheduled-regexp)
  4011. (defvar org-scheduled-time-regexp nil
  4012. "Matches the SCHEDULED keyword together with a time stamp.")
  4013. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4014. (defvar org-scheduled-time-hour-regexp nil
  4015. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4016. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4017. (defvar org-closed-time-regexp nil
  4018. "Matches the CLOSED keyword together with a time stamp.")
  4019. (make-variable-buffer-local 'org-closed-time-regexp)
  4020. (defvar org-keyword-time-regexp nil
  4021. "Matches any of the 4 keywords, together with the time stamp.")
  4022. (make-variable-buffer-local 'org-keyword-time-regexp)
  4023. (defvar org-keyword-time-not-clock-regexp nil
  4024. "Matches any of the 3 keywords, together with the time stamp.")
  4025. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4026. (defvar org-maybe-keyword-time-regexp nil
  4027. "Matches a timestamp, possibly preceded by a keyword.")
  4028. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4029. (defvar org-all-time-keywords nil
  4030. "List of time keywords.")
  4031. (make-variable-buffer-local 'org-all-time-keywords)
  4032. (defconst org-plain-time-of-day-regexp
  4033. (concat
  4034. "\\(\\<[012]?[0-9]"
  4035. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4036. "\\(--?"
  4037. "\\(\\<[012]?[0-9]"
  4038. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4039. "\\)?")
  4040. "Regular expression to match a plain time or time range.
  4041. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4042. groups carry important information:
  4043. 0 the full match
  4044. 1 the first time, range or not
  4045. 8 the second time, if it is a range.")
  4046. (defconst org-plain-time-extension-regexp
  4047. (concat
  4048. "\\(\\<[012]?[0-9]"
  4049. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4050. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4051. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4052. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4053. groups carry important information:
  4054. 0 the full match
  4055. 7 hours of duration
  4056. 9 minutes of duration")
  4057. (defconst org-stamp-time-of-day-regexp
  4058. (concat
  4059. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4060. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4061. "\\(--?"
  4062. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4063. "Regular expression to match a timestamp time or time range.
  4064. After a match, the following groups carry important information:
  4065. 0 the full match
  4066. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4067. 2 the first time, range or not
  4068. 4 the second time, if it is a range.")
  4069. (defconst org-startup-options
  4070. '(("fold" org-startup-folded t)
  4071. ("overview" org-startup-folded t)
  4072. ("nofold" org-startup-folded nil)
  4073. ("showall" org-startup-folded nil)
  4074. ("showeverything" org-startup-folded showeverything)
  4075. ("content" org-startup-folded content)
  4076. ("indent" org-startup-indented t)
  4077. ("noindent" org-startup-indented nil)
  4078. ("hidestars" org-hide-leading-stars t)
  4079. ("showstars" org-hide-leading-stars nil)
  4080. ("odd" org-odd-levels-only t)
  4081. ("oddeven" org-odd-levels-only nil)
  4082. ("align" org-startup-align-all-tables t)
  4083. ("noalign" org-startup-align-all-tables nil)
  4084. ("inlineimages" org-startup-with-inline-images t)
  4085. ("noinlineimages" org-startup-with-inline-images nil)
  4086. ("latexpreview" org-startup-with-latex-preview t)
  4087. ("nolatexpreview" org-startup-with-latex-preview nil)
  4088. ("customtime" org-display-custom-times t)
  4089. ("logdone" org-log-done time)
  4090. ("lognotedone" org-log-done note)
  4091. ("nologdone" org-log-done nil)
  4092. ("lognoteclock-out" org-log-note-clock-out t)
  4093. ("nolognoteclock-out" org-log-note-clock-out nil)
  4094. ("logrepeat" org-log-repeat state)
  4095. ("lognoterepeat" org-log-repeat note)
  4096. ("logdrawer" org-log-into-drawer t)
  4097. ("nologdrawer" org-log-into-drawer nil)
  4098. ("logstatesreversed" org-log-states-order-reversed t)
  4099. ("nologstatesreversed" org-log-states-order-reversed nil)
  4100. ("nologrepeat" org-log-repeat nil)
  4101. ("logreschedule" org-log-reschedule time)
  4102. ("lognotereschedule" org-log-reschedule note)
  4103. ("nologreschedule" org-log-reschedule nil)
  4104. ("logredeadline" org-log-redeadline time)
  4105. ("lognoteredeadline" org-log-redeadline note)
  4106. ("nologredeadline" org-log-redeadline nil)
  4107. ("logrefile" org-log-refile time)
  4108. ("lognoterefile" org-log-refile note)
  4109. ("nologrefile" org-log-refile nil)
  4110. ("fninline" org-footnote-define-inline t)
  4111. ("nofninline" org-footnote-define-inline nil)
  4112. ("fnlocal" org-footnote-section nil)
  4113. ("fnauto" org-footnote-auto-label t)
  4114. ("fnprompt" org-footnote-auto-label nil)
  4115. ("fnconfirm" org-footnote-auto-label confirm)
  4116. ("fnplain" org-footnote-auto-label plain)
  4117. ("fnadjust" org-footnote-auto-adjust t)
  4118. ("nofnadjust" org-footnote-auto-adjust nil)
  4119. ("constcgs" constants-unit-system cgs)
  4120. ("constSI" constants-unit-system SI)
  4121. ("noptag" org-tag-persistent-alist nil)
  4122. ("hideblocks" org-hide-block-startup t)
  4123. ("nohideblocks" org-hide-block-startup nil)
  4124. ("beamer" org-startup-with-beamer-mode t)
  4125. ("entitiespretty" org-pretty-entities t)
  4126. ("entitiesplain" org-pretty-entities nil))
  4127. "Variable associated with STARTUP options for org-mode.
  4128. Each element is a list of three items: the startup options (as written
  4129. in the #+STARTUP line), the corresponding variable, and the value to set
  4130. this variable to if the option is found. An optional forth element PUSH
  4131. means to push this value onto the list in the variable.")
  4132. (defun org-update-property-plist (key val props)
  4133. "Update PROPS with KEY and VAL."
  4134. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4135. (key (if appending (substring key 0 (- (length key) 1)) key))
  4136. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4137. (previous (cdr (assoc key props))))
  4138. (if appending
  4139. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4140. (cons (cons key val) remainder))))
  4141. (defconst org-block-regexp
  4142. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4143. "Regular expression for hiding blocks.")
  4144. (defconst org-heading-keyword-regexp-format
  4145. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4146. "Printf format for a regexp matching a headline with some keyword.
  4147. This regexp will match the headline of any node which has the
  4148. exact keyword that is put into the format. The keyword isn't in
  4149. any group by default, but the stars and the body are.")
  4150. (defconst org-heading-keyword-maybe-regexp-format
  4151. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4152. "Printf format for a regexp matching a headline, possibly with some keyword.
  4153. This regexp can match any headline with the specified keyword, or
  4154. without a keyword. The keyword isn't in any group by default,
  4155. but the stars and the body are.")
  4156. (defcustom org-group-tags t
  4157. "When non-nil (the default), use group tags.
  4158. This can be turned on/off through `org-toggle-tags-groups'."
  4159. :group 'org-tags
  4160. :group 'org-startup
  4161. :type 'boolean)
  4162. (defun org-toggle-tags-groups ()
  4163. "Toggle support for group tags.
  4164. Support for group tags is controlled by the option
  4165. `org-group-tags', which is non-nil by default."
  4166. (interactive)
  4167. (setq org-group-tags (not org-group-tags))
  4168. (cond ((and (derived-mode-p 'org-agenda-mode)
  4169. org-group-tags)
  4170. (org-agenda-redo))
  4171. ((derived-mode-p 'org-mode)
  4172. (let ((org-inhibit-startup t)) (org-mode))))
  4173. (message "Groups tags support has been turned %s"
  4174. (if org-group-tags "on" "off")))
  4175. (defun org-set-regexps-and-options-for-tags ()
  4176. "Precompute regular expressions used for tags in the current buffer."
  4177. (when (derived-mode-p 'org-mode)
  4178. (org-set-local 'org-file-tags nil)
  4179. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4180. (splitre "[ \t]+")
  4181. tags ftags key value
  4182. (start 0))
  4183. (save-excursion
  4184. (save-restriction
  4185. (widen)
  4186. (goto-char (point-min))
  4187. (while (re-search-forward re nil t)
  4188. (setq key (upcase (org-match-string-no-properties 1))
  4189. value (org-match-string-no-properties 2))
  4190. (if (stringp value) (setq value (org-trim value)))
  4191. (cond
  4192. ((equal key "TAGS")
  4193. (setq tags (append tags (if tags '("\\n") nil)
  4194. (org-split-string value splitre))))
  4195. ((equal key "FILETAGS")
  4196. (when (string-match "\\S-" value)
  4197. (setq ftags
  4198. (append
  4199. ftags
  4200. (apply 'append
  4201. (mapcar (lambda (x) (org-split-string x ":"))
  4202. (org-split-string value)))))))))))
  4203. ;; Process the file tags.
  4204. (and ftags (org-set-local 'org-file-tags
  4205. (mapcar 'org-add-prop-inherited ftags)))
  4206. (org-set-local 'org-tag-groups-alist nil)
  4207. ;; Process the tags.
  4208. ;; FIXME
  4209. (when tags
  4210. (let (e tgs g)
  4211. (while (setq e (pop tags))
  4212. (cond
  4213. ((equal e "{")
  4214. (progn (push '(:startgroup) tgs)
  4215. (when (equal (nth 1 tags) ":")
  4216. (push (list (replace-regexp-in-string
  4217. "(.+)$" "" (nth 0 tags)))
  4218. org-tag-groups-alist)
  4219. (setq g 0))))
  4220. ((equal e ":") (push '(:grouptags) tgs))
  4221. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4222. ((equal e "\\n") (push '(:newline) tgs))
  4223. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4224. (push (cons (match-string 1 e)
  4225. (string-to-char (match-string 2 e))) tgs)
  4226. (if (and g (> g 0))
  4227. (setcar org-tag-groups-alist
  4228. (append (car org-tag-groups-alist)
  4229. (list (match-string 1 e)))))
  4230. (if g (setq g (1+ g))))
  4231. (t (push (list e) tgs)
  4232. (if (and g (> g 0))
  4233. (setcar org-tag-groups-alist
  4234. (append (car org-tag-groups-alist) (list e))))
  4235. (if g (setq g (1+ g))))))
  4236. (org-set-local 'org-tag-alist nil)
  4237. (while (setq e (pop tgs))
  4238. (or (and (stringp (car e))
  4239. (assoc (car e) org-tag-alist))
  4240. (push e org-tag-alist))))))))
  4241. (defun org-set-regexps-and-options ()
  4242. "Precompute regular expressions used in the current buffer."
  4243. (when (derived-mode-p 'org-mode)
  4244. (org-set-local 'org-todo-kwd-alist nil)
  4245. (org-set-local 'org-todo-key-alist nil)
  4246. (org-set-local 'org-todo-key-trigger nil)
  4247. (org-set-local 'org-todo-keywords-1 nil)
  4248. (org-set-local 'org-done-keywords nil)
  4249. (org-set-local 'org-todo-heads nil)
  4250. (org-set-local 'org-todo-sets nil)
  4251. (org-set-local 'org-todo-log-states nil)
  4252. (org-set-local 'org-file-properties nil)
  4253. (let ((re (org-make-options-regexp
  4254. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4255. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4256. "SETUPFILE" "OPTIONS")
  4257. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4258. (splitre "[ \t]+")
  4259. (scripts org-use-sub-superscripts)
  4260. kwds kws0 kwsa key log value cat arch const links hw dws
  4261. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4262. (start 0))
  4263. (save-excursion
  4264. (save-restriction
  4265. (widen)
  4266. (goto-char (point-min))
  4267. (while (or (and ext-setup-or-nil
  4268. (string-match re ext-setup-or-nil start)
  4269. (setq start (match-end 0)))
  4270. (and (setq ext-setup-or-nil nil start 0)
  4271. (re-search-forward re nil t)))
  4272. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4273. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4274. (if (stringp value) (setq value (org-trim value)))
  4275. (cond
  4276. ((equal key "CATEGORY")
  4277. (setq cat value))
  4278. ((member key '("SEQ_TODO" "TODO"))
  4279. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4280. ((equal key "TYP_TODO")
  4281. (push (cons 'type (org-split-string value splitre)) kwds))
  4282. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4283. ;; general TODO-like setup
  4284. (push (cons (intern (downcase (match-string 1 key)))
  4285. (org-split-string value splitre)) kwds))
  4286. ((equal key "COLUMNS")
  4287. (org-set-local 'org-columns-default-format value))
  4288. ((equal key "LINK")
  4289. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4290. (push (cons (match-string 1 value)
  4291. (org-trim (match-string 2 value)))
  4292. links)))
  4293. ((equal key "PRIORITIES")
  4294. (setq prio (org-split-string value " +")))
  4295. ((equal key "PROPERTY")
  4296. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4297. (setq props (org-update-property-plist (match-string 1 value)
  4298. (match-string 2 value)
  4299. props))))
  4300. ((equal key "DRAWERS")
  4301. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4302. ((equal key "CONSTANTS")
  4303. (org-table-set-constants))
  4304. ((equal key "STARTUP")
  4305. (let ((opts (org-split-string value splitre))
  4306. l var val)
  4307. (while (setq l (pop opts))
  4308. (when (setq l (assoc l org-startup-options))
  4309. (setq var (nth 1 l) val (nth 2 l))
  4310. (if (not (nth 3 l))
  4311. (set (make-local-variable var) val)
  4312. (if (not (listp (symbol-value var)))
  4313. (set (make-local-variable var) nil))
  4314. (set (make-local-variable var) (symbol-value var))
  4315. (add-to-list var val))))))
  4316. ((equal key "ARCHIVE")
  4317. (setq arch value)
  4318. (remove-text-properties 0 (length arch)
  4319. '(face t fontified t) arch))
  4320. ((equal key "OPTIONS")
  4321. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4322. (setq scripts (read (match-string 2 value)))))
  4323. ((and (equal key "SETUPFILE")
  4324. ;; Prevent checking in Gnus messages
  4325. (not buffer-read-only))
  4326. (setq setup-contents (org-file-contents
  4327. (expand-file-name
  4328. (org-remove-double-quotes value))
  4329. 'noerror))
  4330. (if (not ext-setup-or-nil)
  4331. (setq ext-setup-or-nil setup-contents start 0)
  4332. (setq ext-setup-or-nil
  4333. (concat (substring ext-setup-or-nil 0 start)
  4334. "\n" setup-contents "\n"
  4335. (substring ext-setup-or-nil start)))))))
  4336. ;; search for property blocks
  4337. (goto-char (point-min))
  4338. (while (re-search-forward org-block-regexp nil t)
  4339. (when (equal "PROPERTY" (upcase (match-string 1)))
  4340. (setq value (replace-regexp-in-string
  4341. "[\n\r]" " " (match-string 4)))
  4342. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4343. (setq props (org-update-property-plist (match-string 1 value)
  4344. (match-string 2 value)
  4345. props)))))))
  4346. (org-set-local 'org-use-sub-superscripts scripts)
  4347. (when cat
  4348. (org-set-local 'org-category (intern cat))
  4349. (push (cons "CATEGORY" cat) props))
  4350. (when prio
  4351. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4352. (setq prio (mapcar 'string-to-char prio))
  4353. (org-set-local 'org-highest-priority (nth 0 prio))
  4354. (org-set-local 'org-lowest-priority (nth 1 prio))
  4355. (org-set-local 'org-default-priority (nth 2 prio)))
  4356. (and props (org-set-local 'org-file-properties (nreverse props)))
  4357. (and drawers (org-set-local 'org-drawers drawers))
  4358. (and arch (org-set-local 'org-archive-location arch))
  4359. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4360. ;; Process the TODO keywords
  4361. (unless kwds
  4362. ;; Use the global values as if they had been given locally.
  4363. (setq kwds (default-value 'org-todo-keywords))
  4364. (if (stringp (car kwds))
  4365. (setq kwds (list (cons org-todo-interpretation
  4366. (default-value 'org-todo-keywords)))))
  4367. (setq kwds (reverse kwds)))
  4368. (setq kwds (nreverse kwds))
  4369. (let (inter kws kw)
  4370. (while (setq kws (pop kwds))
  4371. (let ((kws (or
  4372. (run-hook-with-args-until-success
  4373. 'org-todo-setup-filter-hook kws)
  4374. kws)))
  4375. (setq inter (pop kws) sep (member "|" kws)
  4376. kws0 (delete "|" (copy-sequence kws))
  4377. kwsa nil
  4378. kws1 (mapcar
  4379. (lambda (x)
  4380. ;; 1 2
  4381. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4382. (progn
  4383. (setq kw (match-string 1 x)
  4384. key (and (match-end 2) (match-string 2 x))
  4385. log (org-extract-log-state-settings x))
  4386. (push (cons kw (and key (string-to-char key))) kwsa)
  4387. (and log (push log org-todo-log-states))
  4388. kw)
  4389. (error "Invalid TODO keyword %s" x)))
  4390. kws0)
  4391. kwsa (if kwsa (append '((:startgroup))
  4392. (nreverse kwsa)
  4393. '((:endgroup))))
  4394. hw (car kws1)
  4395. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4396. tail (list inter hw (car dws) (org-last dws))))
  4397. (add-to-list 'org-todo-heads hw 'append)
  4398. (push kws1 org-todo-sets)
  4399. (setq org-done-keywords (append org-done-keywords dws nil))
  4400. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4401. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4402. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4403. (setq org-todo-sets (nreverse org-todo-sets)
  4404. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4405. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4406. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4407. ;; Compute the regular expressions and other local variables.
  4408. ;; Using `org-outline-regexp-bol' would complicate them much,
  4409. ;; because of the fixed white space at the end of that string.
  4410. (if (not org-done-keywords)
  4411. (setq org-done-keywords (and org-todo-keywords-1
  4412. (list (org-last org-todo-keywords-1)))))
  4413. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4414. (length org-scheduled-string)
  4415. (length org-clock-string)
  4416. (length org-closed-string)))
  4417. org-drawer-regexp
  4418. (concat "^[ \t]*:\\("
  4419. (mapconcat 'regexp-quote org-drawers "\\|")
  4420. "\\):[ \t]*$")
  4421. org-not-done-keywords
  4422. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4423. org-todo-regexp
  4424. (concat "\\("
  4425. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4426. "\\)")
  4427. org-not-done-regexp
  4428. (concat "\\("
  4429. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4430. "\\)")
  4431. org-not-done-heading-regexp
  4432. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4433. org-todo-line-regexp
  4434. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4435. org-complex-heading-regexp
  4436. (concat "^\\(\\*+\\)"
  4437. "\\(?: +" org-todo-regexp "\\)?"
  4438. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4439. "\\(?: +\\(.*?\\)\\)??"
  4440. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4441. "[ \t]*$")
  4442. org-complex-heading-regexp-format
  4443. (concat "^\\(\\*+\\)"
  4444. "\\(?: +" org-todo-regexp "\\)?"
  4445. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4446. "\\(?: +"
  4447. ;; Stats cookies can be stuck to body.
  4448. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4449. "\\(%s\\)"
  4450. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4451. "\\)"
  4452. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4453. "[ \t]*$")
  4454. org-todo-line-tags-regexp
  4455. (concat "^\\(\\*+\\)"
  4456. "\\(?: +" org-todo-regexp "\\)?"
  4457. "\\(?: +\\(.*?\\)\\)??"
  4458. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4459. "[ \t]*$")
  4460. org-deadline-regexp (concat "\\<" org-deadline-string)
  4461. org-deadline-time-regexp
  4462. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4463. org-deadline-time-hour-regexp
  4464. (concat "\\<" org-deadline-string
  4465. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4466. org-deadline-line-regexp
  4467. (concat "\\<\\(" org-deadline-string "\\).*")
  4468. org-scheduled-regexp
  4469. (concat "\\<" org-scheduled-string)
  4470. org-scheduled-time-regexp
  4471. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4472. org-scheduled-time-hour-regexp
  4473. (concat "\\<" org-scheduled-string
  4474. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4475. org-closed-time-regexp
  4476. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4477. org-keyword-time-regexp
  4478. (concat "\\<\\(" org-scheduled-string
  4479. "\\|" org-deadline-string
  4480. "\\|" org-closed-string
  4481. "\\|" org-clock-string "\\)"
  4482. " *[[<]\\([^]>]+\\)[]>]")
  4483. org-keyword-time-not-clock-regexp
  4484. (concat "\\<\\(" org-scheduled-string
  4485. "\\|" org-deadline-string
  4486. "\\|" org-closed-string
  4487. "\\)"
  4488. " *[[<]\\([^]>]+\\)[]>]")
  4489. org-maybe-keyword-time-regexp
  4490. (concat "\\(\\<\\(" org-scheduled-string
  4491. "\\|" org-deadline-string
  4492. "\\|" org-closed-string
  4493. "\\|" org-clock-string "\\)\\)?"
  4494. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4495. org-all-time-keywords
  4496. (mapcar (lambda (w) (substring w 0 -1))
  4497. (list org-scheduled-string org-deadline-string
  4498. org-clock-string org-closed-string)))
  4499. (org-compute-latex-and-related-regexp)
  4500. (org-set-font-lock-defaults))))
  4501. (defun org-file-contents (file &optional noerror)
  4502. "Return the contents of FILE, as a string."
  4503. (if (or (not file)
  4504. (not (file-readable-p file)))
  4505. (if noerror
  4506. (message "Cannot read file \"%s\"" file)
  4507. (error "Cannot read file \"%s\"" file))
  4508. (with-temp-buffer
  4509. (insert-file-contents file)
  4510. (buffer-string))))
  4511. (defun org-extract-log-state-settings (x)
  4512. "Extract the log state setting from a TODO keyword string.
  4513. This will extract info from a string like \"WAIT(w@/!)\"."
  4514. (let (kw key log1 log2)
  4515. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4516. (setq kw (match-string 1 x)
  4517. key (and (match-end 2) (match-string 2 x))
  4518. log1 (and (match-end 3) (match-string 3 x))
  4519. log2 (and (match-end 4) (match-string 4 x)))
  4520. (and (or log1 log2)
  4521. (list kw
  4522. (and log1 (if (equal log1 "!") 'time 'note))
  4523. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4524. (defun org-remove-keyword-keys (list)
  4525. "Remove a pair of parenthesis at the end of each string in LIST."
  4526. (mapcar (lambda (x)
  4527. (if (string-match "(.*)$" x)
  4528. (substring x 0 (match-beginning 0))
  4529. x))
  4530. list))
  4531. (defun org-assign-fast-keys (alist)
  4532. "Assign fast keys to a keyword-key alist.
  4533. Respect keys that are already there."
  4534. (let (new e (alt ?0))
  4535. (while (setq e (pop alist))
  4536. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4537. (cdr e)) ;; Key already assigned.
  4538. (push e new)
  4539. (let ((clist (string-to-list (downcase (car e))))
  4540. (used (append new alist)))
  4541. (when (= (car clist) ?@)
  4542. (pop clist))
  4543. (while (and clist (rassoc (car clist) used))
  4544. (pop clist))
  4545. (unless clist
  4546. (while (rassoc alt used)
  4547. (incf alt)))
  4548. (push (cons (car e) (or (car clist) alt)) new))))
  4549. (nreverse new)))
  4550. ;;; Some variables used in various places
  4551. (defvar org-window-configuration nil
  4552. "Used in various places to store a window configuration.")
  4553. (defvar org-selected-window nil
  4554. "Used in various places to store a window configuration.")
  4555. (defvar org-finish-function nil
  4556. "Function to be called when `C-c C-c' is used.
  4557. This is for getting out of special buffers like capture.")
  4558. ;; FIXME: Occasionally check by commenting these, to make sure
  4559. ;; no other functions uses these, forgetting to let-bind them.
  4560. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4561. (defvar org-last-state)
  4562. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4563. ;; Defined somewhere in this file, but used before definition.
  4564. (defvar org-entities) ;; defined in org-entities.el
  4565. (defvar org-struct-menu)
  4566. (defvar org-org-menu)
  4567. (defvar org-tbl-menu)
  4568. ;;;; Define the Org-mode
  4569. ;; We use a before-change function to check if a table might need
  4570. ;; an update.
  4571. (defvar org-table-may-need-update t
  4572. "Indicates that a table might need an update.
  4573. This variable is set by `org-before-change-function'.
  4574. `org-table-align' sets it back to nil.")
  4575. (defun org-before-change-function (beg end)
  4576. "Every change indicates that a table might need an update."
  4577. (setq org-table-may-need-update t))
  4578. (defvar org-mode-map)
  4579. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4580. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4581. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4582. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4583. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4584. (defvar org-table-buffer-is-an nil)
  4585. (defvar bidi-paragraph-direction)
  4586. (defvar buffer-face-mode-face)
  4587. (require 'outline)
  4588. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4589. (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"))
  4590. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4591. ;; Other stuff we need.
  4592. (require 'time-date)
  4593. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4594. (require 'easymenu)
  4595. (require 'overlay)
  4596. ;; (require 'org-macs) moved higher up in the file before it is first used
  4597. (require 'org-entities)
  4598. ;; (require 'org-compat) moved higher up in the file before it is first used
  4599. (require 'org-faces)
  4600. (require 'org-list)
  4601. (require 'org-pcomplete)
  4602. (require 'org-src)
  4603. (require 'org-footnote)
  4604. (require 'org-macro)
  4605. ;; babel
  4606. (require 'ob)
  4607. ;;;###autoload
  4608. (define-derived-mode org-mode outline-mode "Org"
  4609. "Outline-based notes management and organizer, alias
  4610. \"Carsten's outline-mode for keeping track of everything.\"
  4611. Org-mode develops organizational tasks around a NOTES file which
  4612. contains information about projects as plain text. Org-mode is
  4613. implemented on top of outline-mode, which is ideal to keep the content
  4614. of large files well structured. It supports ToDo items, deadlines and
  4615. time stamps, which magically appear in the diary listing of the Emacs
  4616. calendar. Tables are easily created with a built-in table editor.
  4617. Plain text URL-like links connect to websites, emails (VM), Usenet
  4618. messages (Gnus), BBDB entries, and any files related to the project.
  4619. For printing and sharing of notes, an Org-mode file (or a part of it)
  4620. can be exported as a structured ASCII or HTML file.
  4621. The following commands are available:
  4622. \\{org-mode-map}"
  4623. ;; Get rid of Outline menus, they are not needed
  4624. ;; Need to do this here because define-derived-mode sets up
  4625. ;; the keymap so late. Still, it is a waste to call this each time
  4626. ;; we switch another buffer into org-mode.
  4627. (if (featurep 'xemacs)
  4628. (when (boundp 'outline-mode-menu-heading)
  4629. ;; Assume this is Greg's port, it uses easymenu
  4630. (easy-menu-remove outline-mode-menu-heading)
  4631. (easy-menu-remove outline-mode-menu-show)
  4632. (easy-menu-remove outline-mode-menu-hide))
  4633. (define-key org-mode-map [menu-bar headings] 'undefined)
  4634. (define-key org-mode-map [menu-bar hide] 'undefined)
  4635. (define-key org-mode-map [menu-bar show] 'undefined))
  4636. (org-load-modules-maybe)
  4637. (easy-menu-add org-org-menu)
  4638. (easy-menu-add org-tbl-menu)
  4639. (org-install-agenda-files-menu)
  4640. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4641. (add-to-invisibility-spec '(org-cwidth))
  4642. (add-to-invisibility-spec '(org-hide-block . t))
  4643. (when (featurep 'xemacs)
  4644. (org-set-local 'line-move-ignore-invisible t))
  4645. (org-set-local 'outline-regexp org-outline-regexp)
  4646. (org-set-local 'outline-level 'org-outline-level)
  4647. (setq bidi-paragraph-direction 'left-to-right)
  4648. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4649. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4650. (when (and org-ellipsis
  4651. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4652. (fboundp 'make-glyph-code))
  4653. (unless org-display-table
  4654. (setq org-display-table (make-display-table)))
  4655. (set-display-table-slot
  4656. org-display-table 4
  4657. (vconcat (mapcar
  4658. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4659. org-ellipsis)))
  4660. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4661. (setq buffer-display-table org-display-table))
  4662. (org-set-regexps-and-options-for-tags)
  4663. (org-set-regexps-and-options)
  4664. (when (and org-tag-faces (not org-tags-special-faces-re))
  4665. ;; tag faces set outside customize.... force initialization.
  4666. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4667. ;; Calc embedded
  4668. (org-set-local 'calc-embedded-open-mode "# ")
  4669. ;; Modify a few syntax entries
  4670. (modify-syntax-entry ?@ "w")
  4671. (modify-syntax-entry ?\" "\"")
  4672. (if org-startup-truncated (setq truncate-lines t))
  4673. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4674. (org-set-local 'font-lock-unfontify-region-function
  4675. 'org-unfontify-region)
  4676. ;; Activate before-change-function
  4677. (org-set-local 'org-table-may-need-update t)
  4678. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4679. 'local)
  4680. ;; Check for running clock before killing a buffer
  4681. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4682. ;; Initialize macros templates.
  4683. (org-macro-initialize-templates)
  4684. ;; Initialize radio targets.
  4685. (org-update-radio-target-regexp)
  4686. ;; Indentation.
  4687. (org-set-local 'indent-line-function 'org-indent-line)
  4688. (org-set-local 'indent-region-function 'org-indent-region)
  4689. ;; Filling and auto-filling.
  4690. (org-setup-filling)
  4691. ;; Comments.
  4692. (org-setup-comments-handling)
  4693. ;; Beginning/end of defun
  4694. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4695. (org-set-local 'end-of-defun-function 'org-forward-element)
  4696. ;; Next error for sparse trees
  4697. (org-set-local 'next-error-function 'org-occur-next-match)
  4698. ;; Make sure dependence stuff works reliably, even for users who set it
  4699. ;; too late :-(
  4700. (if org-enforce-todo-dependencies
  4701. (add-hook 'org-blocker-hook
  4702. 'org-block-todo-from-children-or-siblings-or-parent)
  4703. (remove-hook 'org-blocker-hook
  4704. 'org-block-todo-from-children-or-siblings-or-parent))
  4705. (if org-enforce-todo-checkbox-dependencies
  4706. (add-hook 'org-blocker-hook
  4707. 'org-block-todo-from-checkboxes)
  4708. (remove-hook 'org-blocker-hook
  4709. 'org-block-todo-from-checkboxes))
  4710. ;; Align options lines
  4711. (org-set-local
  4712. 'align-mode-rules-list
  4713. '((org-in-buffer-settings
  4714. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4715. (modes . '(org-mode)))))
  4716. ;; Imenu
  4717. (org-set-local 'imenu-create-index-function
  4718. 'org-imenu-get-tree)
  4719. ;; Make isearch reveal context
  4720. (if (or (featurep 'xemacs)
  4721. (not (boundp 'outline-isearch-open-invisible-function)))
  4722. ;; Emacs 21 and XEmacs make use of the hook
  4723. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4724. ;; Emacs 22 deals with this through a special variable
  4725. (org-set-local 'outline-isearch-open-invisible-function
  4726. (lambda (&rest ignore) (org-show-context 'isearch)))
  4727. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4728. ;; Setup the pcomplete hooks
  4729. (set (make-local-variable 'pcomplete-command-completion-function)
  4730. 'org-pcomplete-initial)
  4731. (set (make-local-variable 'pcomplete-command-name-function)
  4732. 'org-command-at-point)
  4733. (set (make-local-variable 'pcomplete-default-completion-function)
  4734. 'ignore)
  4735. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4736. 'org-parse-arguments)
  4737. (set (make-local-variable 'pcomplete-termination-string) "")
  4738. (when (>= emacs-major-version 23)
  4739. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4740. ;; If empty file that did not turn on org-mode automatically, make it to.
  4741. (if (and org-insert-mode-line-in-empty-file
  4742. (org-called-interactively-p 'any)
  4743. (= (point-min) (point-max)))
  4744. (insert "# -*- mode: org -*-\n\n"))
  4745. (unless org-inhibit-startup
  4746. (org-unmodified
  4747. (and org-startup-with-beamer-mode (org-beamer-mode))
  4748. (when org-startup-align-all-tables
  4749. (org-table-map-tables 'org-table-align 'quietly))
  4750. (when org-startup-with-inline-images
  4751. (org-display-inline-images))
  4752. (when org-startup-with-latex-preview
  4753. (org-preview-latex-fragment))
  4754. (unless org-inhibit-startup-visibility-stuff
  4755. (org-set-startup-visibility))))
  4756. ;; Try to set org-hide correctly
  4757. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4758. ;; Update `customize-package-emacs-version-alist'
  4759. (add-to-list 'customize-package-emacs-version-alist
  4760. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4761. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4762. ("8.0" . "24.4")))
  4763. (defvar org-mode-transpose-word-syntax-table
  4764. (let ((st (make-syntax-table)))
  4765. (mapc (lambda(c) (modify-syntax-entry
  4766. (string-to-char (car c)) "w p" st))
  4767. org-emphasis-alist)
  4768. st))
  4769. (when (fboundp 'abbrev-table-put)
  4770. (abbrev-table-put org-mode-abbrev-table
  4771. :parents (list text-mode-abbrev-table)))
  4772. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4773. (defsubst org-fix-ellipsis-at-bol ()
  4774. (save-excursion (goto-char (window-start)) (recenter 0)))
  4775. (defun org-find-invisible-foreground ()
  4776. (let ((candidates (remove
  4777. "unspecified-bg"
  4778. (nconc
  4779. (list (face-background 'default)
  4780. (face-background 'org-default))
  4781. (mapcar
  4782. (lambda (alist)
  4783. (when (boundp alist)
  4784. (cdr (assoc 'background-color (symbol-value alist)))))
  4785. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4786. (list (face-foreground 'org-hide))))))
  4787. (car (remove nil candidates))))
  4788. (defun org-current-time (&optional rounding-minutes past)
  4789. "Current time, possibly rounded to ROUNDING-MINUTES.
  4790. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4791. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4792. the rounding returns a past time."
  4793. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4794. (car org-time-stamp-rounding-minutes)))
  4795. (time (decode-time)) res)
  4796. (if (< r 1)
  4797. (current-time)
  4798. (setq res
  4799. (apply 'encode-time
  4800. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4801. (nthcdr 2 time))))
  4802. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4803. (seconds-to-time (- (org-float-time res) (* r 60)))
  4804. res))))
  4805. (defun org-today ()
  4806. "Return today date, considering `org-extend-today-until'."
  4807. (time-to-days
  4808. (time-subtract (current-time)
  4809. (list 0 (* 3600 org-extend-today-until) 0))))
  4810. ;;;; Font-Lock stuff, including the activators
  4811. (defvar org-mouse-map (make-sparse-keymap))
  4812. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4813. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4814. (when org-mouse-1-follows-link
  4815. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4816. (when org-tab-follows-link
  4817. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4818. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4819. (require 'font-lock)
  4820. (defconst org-non-link-chars "]\t\n\r<>")
  4821. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4822. "shell" "elisp" "doi" "message"))
  4823. (defvar org-link-types-re nil
  4824. "Matches a link that has a url-like prefix like \"http:\"")
  4825. (defvar org-link-re-with-space nil
  4826. "Matches a link with spaces, optional angular brackets around it.")
  4827. (defvar org-link-re-with-space2 nil
  4828. "Matches a link with spaces, optional angular brackets around it.")
  4829. (defvar org-link-re-with-space3 nil
  4830. "Matches a link with spaces, only for internal part in bracket links.")
  4831. (defvar org-angle-link-re nil
  4832. "Matches link with angular brackets, spaces are allowed.")
  4833. (defvar org-plain-link-re nil
  4834. "Matches plain link, without spaces.")
  4835. (defvar org-bracket-link-regexp nil
  4836. "Matches a link in double brackets.")
  4837. (defvar org-bracket-link-analytic-regexp nil
  4838. "Regular expression used to analyze links.
  4839. Here is what the match groups contain after a match:
  4840. 1: http:
  4841. 2: http
  4842. 3: path
  4843. 4: [desc]
  4844. 5: desc")
  4845. (defvar org-bracket-link-analytic-regexp++ nil
  4846. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4847. (defvar org-any-link-re nil
  4848. "Regular expression matching any link.")
  4849. (defconst org-match-sexp-depth 3
  4850. "Number of stacked braces for sub/superscript matching.")
  4851. (defun org-create-multibrace-regexp (left right n)
  4852. "Create a regular expression which will match a balanced sexp.
  4853. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4854. as single character strings.
  4855. The regexp returned will match the entire expression including the
  4856. delimiters. It will also define a single group which contains the
  4857. match except for the outermost delimiters. The maximum depth of
  4858. stacked delimiters is N. Escaping delimiters is not possible."
  4859. (let* ((nothing (concat "[^" left right "]*?"))
  4860. (or "\\|")
  4861. (re nothing)
  4862. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4863. (while (> n 1)
  4864. (setq n (1- n)
  4865. re (concat re or next)
  4866. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4867. (concat left "\\(" re "\\)" right)))
  4868. (defvar org-match-substring-regexp
  4869. (concat
  4870. "\\(\\S-\\)\\([_^]\\)\\("
  4871. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4872. "\\|"
  4873. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4874. "\\|"
  4875. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4876. "The regular expression matching a sub- or superscript.")
  4877. (defvar org-match-substring-with-braces-regexp
  4878. (concat
  4879. "\\(\\S-\\)\\([_^]\\)\\("
  4880. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4881. "\\)")
  4882. "The regular expression matching a sub- or superscript, forcing braces.")
  4883. (defun org-make-link-regexps ()
  4884. "Update the link regular expressions.
  4885. This should be called after the variable `org-link-types' has changed."
  4886. (setq org-link-types-re
  4887. (concat
  4888. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4889. org-link-re-with-space
  4890. (concat
  4891. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4892. "\\([^" org-non-link-chars " ]"
  4893. "[^" org-non-link-chars "]*"
  4894. "[^" org-non-link-chars " ]\\)>?")
  4895. org-link-re-with-space2
  4896. (concat
  4897. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4898. "\\([^" org-non-link-chars " ]"
  4899. "[^\t\n\r]*"
  4900. "[^" org-non-link-chars " ]\\)>?")
  4901. org-link-re-with-space3
  4902. (concat
  4903. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4904. "\\([^" org-non-link-chars " ]"
  4905. "[^\t\n\r]*\\)")
  4906. org-angle-link-re
  4907. (concat
  4908. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4909. "\\([^" org-non-link-chars " ]"
  4910. "[^" org-non-link-chars "]*"
  4911. "\\)>")
  4912. org-plain-link-re
  4913. (concat
  4914. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4915. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4916. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4917. org-bracket-link-regexp
  4918. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4919. org-bracket-link-analytic-regexp
  4920. (concat
  4921. "\\[\\["
  4922. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4923. "\\([^]]+\\)"
  4924. "\\]"
  4925. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4926. "\\]")
  4927. org-bracket-link-analytic-regexp++
  4928. (concat
  4929. "\\[\\["
  4930. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4931. "\\([^]]+\\)"
  4932. "\\]"
  4933. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4934. "\\]")
  4935. org-any-link-re
  4936. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4937. org-angle-link-re "\\)\\|\\("
  4938. org-plain-link-re "\\)")))
  4939. (org-make-link-regexps)
  4940. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4941. "Regular expression for fast time stamp matching.")
  4942. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4943. "Regular expression for fast time stamp matching.")
  4944. (defconst org-ts-regexp0
  4945. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4946. "Regular expression matching time strings for analysis.
  4947. This one does not require the space after the date, so it can be used
  4948. on a string that terminates immediately after the date.")
  4949. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4950. "Regular expression matching time strings for analysis.")
  4951. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4952. "Regular expression matching time stamps, with groups.")
  4953. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4954. "Regular expression matching time stamps (also [..]), with groups.")
  4955. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4956. "Regular expression matching a time stamp range.")
  4957. (defconst org-tr-regexp-both
  4958. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4959. "Regular expression matching a time stamp range.")
  4960. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4961. org-ts-regexp "\\)?")
  4962. "Regular expression matching a time stamp or time stamp range.")
  4963. (defconst org-tsr-regexp-both
  4964. (concat org-ts-regexp-both "\\(--?-?"
  4965. org-ts-regexp-both "\\)?")
  4966. "Regular expression matching a time stamp or time stamp range.
  4967. The time stamps may be either active or inactive.")
  4968. (defvar org-emph-face nil)
  4969. (defun org-do-emphasis-faces (limit)
  4970. "Run through the buffer and add overlays to emphasized strings."
  4971. (let (rtn a)
  4972. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4973. (if (not (= (char-after (match-beginning 3))
  4974. (char-after (match-beginning 4))))
  4975. (progn
  4976. (setq rtn t)
  4977. (setq a (assoc (match-string 3) org-emphasis-alist))
  4978. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4979. 'face
  4980. (nth 1 a))
  4981. (and (nth 4 a)
  4982. (org-remove-flyspell-overlays-in
  4983. (match-beginning 0) (match-end 0)))
  4984. (add-text-properties (match-beginning 2) (match-end 2)
  4985. '(font-lock-multiline t org-emphasis t))
  4986. (when org-hide-emphasis-markers
  4987. (add-text-properties (match-end 4) (match-beginning 5)
  4988. '(invisible org-link))
  4989. (add-text-properties (match-beginning 3) (match-end 3)
  4990. '(invisible org-link)))))
  4991. (backward-char 1))
  4992. rtn))
  4993. (defun org-emphasize (&optional char)
  4994. "Insert or change an emphasis, i.e. a font like bold or italic.
  4995. If there is an active region, change that region to a new emphasis.
  4996. If there is no region, just insert the marker characters and position
  4997. the cursor between them.
  4998. CHAR should be either the marker character, or the first character of the
  4999. HTML tag associated with that emphasis. If CHAR is a space, the means
  5000. to remove the emphasis of the selected region.
  5001. If char is not given (for example in an interactive call) it
  5002. will be prompted for."
  5003. (interactive)
  5004. (let ((eal org-emphasis-alist) e det
  5005. (erc org-emphasis-regexp-components)
  5006. (prompt "")
  5007. (string "") beg end move tag c s)
  5008. (if (org-region-active-p)
  5009. (setq beg (region-beginning) end (region-end)
  5010. string (buffer-substring beg end))
  5011. (setq move t))
  5012. (while (setq e (pop eal))
  5013. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  5014. c (aref tag 0))
  5015. (push (cons c (string-to-char (car e))) det)
  5016. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  5017. (substring tag 1)))))
  5018. (setq det (nreverse det))
  5019. (unless char
  5020. (message "%s" (concat "Emphasis marker or tag:" prompt))
  5021. (setq char (read-char-exclusive)))
  5022. (setq char (or (cdr (assoc char det)) char))
  5023. (if (equal char ?\ )
  5024. (setq s "" move nil)
  5025. (unless (assoc (char-to-string char) org-emphasis-alist)
  5026. (error "No such emphasis marker: \"%c\"" char))
  5027. (setq s (char-to-string char)))
  5028. (while (and (> (length string) 1)
  5029. (equal (substring string 0 1) (substring string -1))
  5030. (assoc (substring string 0 1) org-emphasis-alist))
  5031. (setq string (substring string 1 -1)))
  5032. (setq string (concat s string s))
  5033. (if beg (delete-region beg end))
  5034. (unless (or (bolp)
  5035. (string-match (concat "[" (nth 0 erc) "\n]")
  5036. (char-to-string (char-before (point)))))
  5037. (insert " "))
  5038. (unless (or (eobp)
  5039. (string-match (concat "[" (nth 1 erc) "\n]")
  5040. (char-to-string (char-after (point)))))
  5041. (insert " ") (backward-char 1))
  5042. (insert string)
  5043. (and move (backward-char 1))))
  5044. (defconst org-nonsticky-props
  5045. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5046. (defsubst org-rear-nonsticky-at (pos)
  5047. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5048. (defun org-activate-plain-links (limit)
  5049. "Run through the buffer and add overlays to links."
  5050. (let (f hl)
  5051. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5052. (not (org-in-src-block-p)))
  5053. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5054. (setq f (get-text-property (match-beginning 0) 'face))
  5055. (setq hl (org-match-string-no-properties 0))
  5056. (if (or (eq f 'org-tag)
  5057. (and (listp f) (memq 'org-tag f)))
  5058. nil
  5059. (add-text-properties (match-beginning 0) (match-end 0)
  5060. (list 'mouse-face 'highlight
  5061. 'face 'org-link
  5062. 'htmlize-link `(:uri ,hl)
  5063. 'keymap org-mouse-map))
  5064. (org-rear-nonsticky-at (match-end 0)))
  5065. t)))
  5066. (defun org-activate-code (limit)
  5067. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5068. (progn
  5069. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5070. (remove-text-properties (match-beginning 0) (match-end 0)
  5071. '(display t invisible t intangible t))
  5072. t)))
  5073. (defcustom org-src-fontify-natively nil
  5074. "When non-nil, fontify code in code blocks."
  5075. :type 'boolean
  5076. :version "24.1"
  5077. :group 'org-appearance
  5078. :group 'org-babel)
  5079. (defcustom org-allow-promoting-top-level-subtree nil
  5080. "When non-nil, allow promoting a top level subtree.
  5081. The leading star of the top level headline will be replaced
  5082. by a #."
  5083. :type 'boolean
  5084. :version "24.1"
  5085. :group 'org-appearance)
  5086. (defun org-fontify-meta-lines-and-blocks (limit)
  5087. (condition-case nil
  5088. (org-fontify-meta-lines-and-blocks-1 limit)
  5089. (error (message "org-mode fontification error"))))
  5090. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5091. "Fontify #+ lines and blocks."
  5092. (let ((case-fold-search t))
  5093. (if (re-search-forward
  5094. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5095. limit t)
  5096. (let ((beg (match-beginning 0))
  5097. (block-start (match-end 0))
  5098. (block-end nil)
  5099. (lang (match-string 7))
  5100. (beg1 (line-beginning-position 2))
  5101. (dc1 (downcase (match-string 2)))
  5102. (dc3 (downcase (match-string 3)))
  5103. end end1 quoting block-type ovl)
  5104. (cond
  5105. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5106. ;; a single line of backend-specific content
  5107. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5108. (remove-text-properties (match-beginning 0) (match-end 0)
  5109. '(display t invisible t intangible t))
  5110. (add-text-properties (match-beginning 1) (match-end 3)
  5111. '(font-lock-fontified t face org-meta-line))
  5112. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5113. '(font-lock-fontified t face org-block))
  5114. ; for backend-specific code
  5115. t)
  5116. ((and (match-end 4) (equal dc3 "+begin"))
  5117. ;; Truly a block
  5118. (setq block-type (downcase (match-string 5))
  5119. quoting (member block-type org-protecting-blocks))
  5120. (when (re-search-forward
  5121. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5122. nil t) ;; on purpose, we look further than LIMIT
  5123. (setq end (min (point-max) (match-end 0))
  5124. end1 (min (point-max) (1- (match-beginning 0))))
  5125. (setq block-end (match-beginning 0))
  5126. (when quoting
  5127. (remove-text-properties beg end
  5128. '(display t invisible t intangible t)))
  5129. (add-text-properties
  5130. beg end
  5131. '(font-lock-fontified t font-lock-multiline t))
  5132. (add-text-properties beg beg1 '(face org-meta-line))
  5133. (add-text-properties end1 (min (point-max) (1+ end))
  5134. '(face org-meta-line)) ; for end_src
  5135. (cond
  5136. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5137. (org-src-font-lock-fontify-block lang block-start block-end)
  5138. ;; remove old background overlays
  5139. (mapc (lambda (ov)
  5140. (if (eq (overlay-get ov 'face) 'org-block-background)
  5141. (delete-overlay ov)))
  5142. (overlays-at (/ (+ beg1 block-end) 2)))
  5143. ;; add a background overlay
  5144. (setq ovl (make-overlay beg1 block-end))
  5145. (overlay-put ovl 'face 'org-block-background)
  5146. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5147. (quoting
  5148. (add-text-properties beg1 (min (point-max) (1+ end1))
  5149. '(face org-block))) ; end of source block
  5150. ((not org-fontify-quote-and-verse-blocks))
  5151. ((string= block-type "quote")
  5152. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5153. ((string= block-type "verse")
  5154. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5155. (add-text-properties beg beg1 '(face org-block-begin-line))
  5156. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5157. '(face org-block-end-line))
  5158. t))
  5159. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5160. (add-text-properties
  5161. beg (match-end 3)
  5162. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5163. '(font-lock-fontified t invisible t)
  5164. '(font-lock-fontified t face org-document-info-keyword)))
  5165. (add-text-properties
  5166. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5167. (if (string-equal dc1 "+title:")
  5168. '(font-lock-fontified t face org-document-title)
  5169. '(font-lock-fontified t face org-document-info))))
  5170. ((or (equal dc1 "+results")
  5171. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5172. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5173. "+call:" "+header:" "+headers:" "+name:"))
  5174. (and (match-end 4) (equal dc3 "+attr")))
  5175. (add-text-properties
  5176. beg (match-end 0)
  5177. '(font-lock-fontified t face org-meta-line))
  5178. t)
  5179. ((member dc3 '(" " ""))
  5180. (add-text-properties
  5181. beg (match-end 0)
  5182. '(font-lock-fontified t face font-lock-comment-face)))
  5183. ((not (member (char-after beg) '(?\ ?\t)))
  5184. ;; just any other in-buffer setting, but not indented
  5185. (add-text-properties
  5186. beg (match-end 0)
  5187. '(font-lock-fontified t face org-meta-line))
  5188. t)
  5189. (t nil))))))
  5190. (defun org-activate-angle-links (limit)
  5191. "Run through the buffer and add overlays to links."
  5192. (if (and (re-search-forward org-angle-link-re limit t)
  5193. (not (org-in-src-block-p)))
  5194. (progn
  5195. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5196. (add-text-properties (match-beginning 0) (match-end 0)
  5197. (list 'mouse-face 'highlight
  5198. 'keymap org-mouse-map))
  5199. (org-rear-nonsticky-at (match-end 0))
  5200. t)))
  5201. (defun org-activate-footnote-links (limit)
  5202. "Run through the buffer and add overlays to footnotes."
  5203. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5204. (when fn
  5205. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5206. (org-remove-flyspell-overlays-in beg end)
  5207. (add-text-properties beg end
  5208. (list 'mouse-face 'highlight
  5209. 'keymap org-mouse-map
  5210. 'help-echo
  5211. (if (= (point-at-bol) beg)
  5212. "Footnote definition"
  5213. "Footnote reference")
  5214. 'font-lock-fontified t
  5215. 'font-lock-multiline t
  5216. 'face 'org-footnote))))))
  5217. (defun org-activate-bracket-links (limit)
  5218. "Run through the buffer and add overlays to bracketed links."
  5219. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5220. (not (org-in-src-block-p)))
  5221. (let* ((hl (org-match-string-no-properties 1))
  5222. (help (concat "LINK: " hl))
  5223. ;; FIXME: Above we should remove the escapes. But that
  5224. ;; requires another match, protecting match data, a lot
  5225. ;; of overhead for font-lock.
  5226. (ip (org-maybe-intangible
  5227. (list 'invisible 'org-link
  5228. 'keymap org-mouse-map 'mouse-face 'highlight
  5229. 'font-lock-multiline t 'help-echo help
  5230. 'htmlize-link `(:uri ,hl))))
  5231. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5232. 'font-lock-multiline t 'help-echo help
  5233. 'htmlize-link `(:uri ,hl))))
  5234. ;; We need to remove the invisible property here. Table narrowing
  5235. ;; may have made some of this invisible.
  5236. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5237. (remove-text-properties (match-beginning 0) (match-end 0)
  5238. '(invisible nil))
  5239. (if (match-end 3)
  5240. (progn
  5241. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5242. (org-rear-nonsticky-at (match-beginning 3))
  5243. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5244. (org-rear-nonsticky-at (match-end 3))
  5245. (add-text-properties (match-end 3) (match-end 0) ip)
  5246. (org-rear-nonsticky-at (match-end 0)))
  5247. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5248. (org-rear-nonsticky-at (match-beginning 1))
  5249. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5250. (org-rear-nonsticky-at (match-end 1))
  5251. (add-text-properties (match-end 1) (match-end 0) ip)
  5252. (org-rear-nonsticky-at (match-end 0)))
  5253. t)))
  5254. (defun org-activate-dates (limit)
  5255. "Run through the buffer and add overlays to dates."
  5256. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5257. (not (equal (char-before (match-beginning 0)) 91)))
  5258. (progn
  5259. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5260. (add-text-properties (match-beginning 0) (match-end 0)
  5261. (list 'mouse-face 'highlight
  5262. 'keymap org-mouse-map))
  5263. (org-rear-nonsticky-at (match-end 0))
  5264. (when org-display-custom-times
  5265. (if (match-end 3)
  5266. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5267. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5268. t)))
  5269. (defvar org-target-link-regexp nil
  5270. "Regular expression matching radio targets in plain text.")
  5271. (make-variable-buffer-local 'org-target-link-regexp)
  5272. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5273. "Regular expression matching a link target.")
  5274. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5275. "Regular expression matching a radio target.")
  5276. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5277. "Regular expression matching any target.")
  5278. (defun org-activate-target-links (limit)
  5279. "Run through the buffer and add overlays to target matches."
  5280. (when org-target-link-regexp
  5281. (let ((case-fold-search t))
  5282. (if (re-search-forward org-target-link-regexp limit t)
  5283. (progn
  5284. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5285. (add-text-properties (match-beginning 0) (match-end 0)
  5286. (list 'mouse-face 'highlight
  5287. 'keymap org-mouse-map
  5288. 'help-echo "Radio target link"
  5289. 'org-linked-text t))
  5290. (org-rear-nonsticky-at (match-end 0))
  5291. t)))))
  5292. (defun org-update-radio-target-regexp ()
  5293. "Find all radio targets in this file and update the regular expression."
  5294. (interactive)
  5295. (when (memq 'radio org-activate-links)
  5296. (setq org-target-link-regexp
  5297. (org-make-target-link-regexp (org-all-targets 'radio)))
  5298. (org-restart-font-lock)))
  5299. (defun org-hide-wide-columns (limit)
  5300. (let (s e)
  5301. (setq s (text-property-any (point) (or limit (point-max))
  5302. 'org-cwidth t))
  5303. (when s
  5304. (setq e (next-single-property-change s 'org-cwidth))
  5305. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5306. (goto-char e)
  5307. t)))
  5308. (defvar org-latex-and-related-regexp nil
  5309. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5310. (defvar org-match-substring-regexp)
  5311. (defvar org-match-substring-with-braces-regexp)
  5312. (defun org-compute-latex-and-related-regexp ()
  5313. "Compute regular expression for LaTeX, entities and sub/superscript.
  5314. Result depends on variable `org-highlight-latex-and-related'."
  5315. (org-set-local
  5316. 'org-latex-and-related-regexp
  5317. (let* ((re-sub
  5318. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5319. ((eq org-use-sub-superscripts '{})
  5320. (list org-match-substring-with-braces-regexp))
  5321. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5322. (re-latex
  5323. (when (memq 'latex org-highlight-latex-and-related)
  5324. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5325. (delq nil
  5326. (mapcar (lambda (x)
  5327. (and (member (car x) matchers) (nth 1 x)))
  5328. org-latex-regexps)))))
  5329. (re-entities
  5330. (when (memq 'entities org-highlight-latex-and-related)
  5331. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5332. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5333. (defun org-do-latex-and-related (limit)
  5334. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5335. LIMIT bounds the search for syntax to highlight. Stop at first
  5336. highlighted object, if any. Return t if some highlighting was
  5337. done, nil otherwise."
  5338. (when (org-string-nw-p org-latex-and-related-regexp)
  5339. (catch 'found
  5340. (while (re-search-forward org-latex-and-related-regexp limit t)
  5341. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5342. 'face))
  5343. '(org-code org-verbatim underline))
  5344. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5345. '(?_ ?^))
  5346. 1
  5347. 0)))
  5348. (font-lock-prepend-text-property
  5349. (+ offset (match-beginning 0)) (match-end 0)
  5350. 'face 'org-latex-and-related)
  5351. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5352. '(font-lock-multiline t)))
  5353. (throw 'found t)))
  5354. nil)))
  5355. (defun org-restart-font-lock ()
  5356. "Restart `font-lock-mode', to force refontification."
  5357. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5358. (font-lock-mode -1)
  5359. (font-lock-mode 1)))
  5360. (defun org-all-targets (&optional radio)
  5361. "Return a list of all targets in this file.
  5362. When optional argument RADIO is non-nil, only find radio
  5363. targets."
  5364. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5365. (save-excursion
  5366. (goto-char (point-min))
  5367. (while (re-search-forward re nil t)
  5368. ;; Make sure point is really within the object.
  5369. (backward-char)
  5370. (let ((obj (org-element-context)))
  5371. (when (memq (org-element-type obj) '(radio-target target))
  5372. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5373. rtn)))
  5374. (defun org-make-target-link-regexp (targets)
  5375. "Make regular expression matching all strings in TARGETS.
  5376. The regular expression finds the targets also if there is a line break
  5377. between words."
  5378. (and targets
  5379. (concat
  5380. "\\<\\("
  5381. (mapconcat
  5382. (lambda (x)
  5383. (setq x (regexp-quote x))
  5384. (while (string-match " +" x)
  5385. (setq x (replace-match "\\s-+" t t x)))
  5386. x)
  5387. targets
  5388. "\\|")
  5389. "\\)\\>")))
  5390. (defun org-activate-tags (limit)
  5391. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5392. (progn
  5393. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5394. (add-text-properties (match-beginning 1) (match-end 1)
  5395. (list 'mouse-face 'highlight
  5396. 'keymap org-mouse-map))
  5397. (org-rear-nonsticky-at (match-end 1))
  5398. t)))
  5399. (defun org-outline-level ()
  5400. "Compute the outline level of the heading at point.
  5401. If this is called at a normal headline, the level is the number of stars.
  5402. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5403. (save-excursion
  5404. (if (not (condition-case nil
  5405. (org-back-to-heading t)
  5406. (error nil)))
  5407. 0
  5408. (looking-at org-outline-regexp)
  5409. (1- (- (match-end 0) (match-beginning 0))))))
  5410. (defvar org-font-lock-keywords nil)
  5411. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5412. "Regular expression matching a property line.")
  5413. (defvar org-font-lock-hook nil
  5414. "Functions to be called for special font lock stuff.")
  5415. (defvar org-font-lock-set-keywords-hook nil
  5416. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5417. This is called after `org-font-lock-extra-keywords' is defined, but before
  5418. it is installed to be used by font lock. This can be useful if something
  5419. needs to be inserted at a specific position in the font-lock sequence.")
  5420. (defun org-font-lock-hook (limit)
  5421. "Run `org-font-lock-hook' within LIMIT."
  5422. (run-hook-with-args 'org-font-lock-hook limit))
  5423. (defun org-set-font-lock-defaults ()
  5424. "Set font lock defaults for the current buffer."
  5425. (let* ((em org-fontify-emphasized-text)
  5426. (lk org-activate-links)
  5427. (org-font-lock-extra-keywords
  5428. (list
  5429. ;; Call the hook
  5430. '(org-font-lock-hook)
  5431. ;; Headlines
  5432. `(,(if org-fontify-whole-heading-line
  5433. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5434. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5435. (1 (org-get-level-face 1))
  5436. (2 (org-get-level-face 2))
  5437. (3 (org-get-level-face 3)))
  5438. ;; Table lines
  5439. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5440. (1 'org-table t))
  5441. ;; Table internals
  5442. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5443. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5444. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5445. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5446. ;; Drawers
  5447. (list org-drawer-regexp '(0 'org-special-keyword t))
  5448. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5449. ;; Properties
  5450. (list org-property-re
  5451. '(1 'org-special-keyword t)
  5452. '(3 'org-property-value t))
  5453. ;; Links
  5454. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5455. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5456. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5457. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5458. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5459. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5460. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5461. ;; Targets.
  5462. (list org-any-target-regexp '(0 'org-target t))
  5463. ;; Diary sexps.
  5464. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5465. ;; Macro
  5466. '("{{{.+}}}" (0 'org-macro t))
  5467. '(org-hide-wide-columns (0 nil append))
  5468. ;; TODO keyword
  5469. (list (format org-heading-keyword-regexp-format
  5470. org-todo-regexp)
  5471. '(2 (org-get-todo-face 2) t))
  5472. ;; DONE
  5473. (if org-fontify-done-headline
  5474. (list (format org-heading-keyword-regexp-format
  5475. (concat
  5476. "\\(?:"
  5477. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5478. "\\)"))
  5479. '(2 'org-headline-done t))
  5480. nil)
  5481. ;; Priorities
  5482. '(org-font-lock-add-priority-faces)
  5483. ;; Tags
  5484. '(org-font-lock-add-tag-faces)
  5485. ;; Tags groups
  5486. (if (and org-group-tags org-tag-groups-alist)
  5487. (list (concat org-outline-regexp-bol ".+\\(:"
  5488. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5489. ":\\).*$")
  5490. '(1 'org-tag-group prepend)))
  5491. ;; Special keywords
  5492. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5493. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5494. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5495. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5496. ;; Emphasis
  5497. (if em
  5498. (if (featurep 'xemacs)
  5499. '(org-do-emphasis-faces (0 nil append))
  5500. '(org-do-emphasis-faces)))
  5501. ;; Checkboxes
  5502. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5503. 1 'org-checkbox prepend)
  5504. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5505. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5506. (0 (org-get-checkbox-statistics-face) t)))
  5507. ;; Description list items
  5508. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5509. 1 'org-list-dt prepend)
  5510. ;; ARCHIVEd headings
  5511. (list (concat
  5512. org-outline-regexp-bol
  5513. "\\(.*:" org-archive-tag ":.*\\)")
  5514. '(1 'org-archived prepend))
  5515. ;; Specials
  5516. '(org-do-latex-and-related)
  5517. '(org-fontify-entities)
  5518. '(org-raise-scripts)
  5519. ;; Code
  5520. '(org-activate-code (1 'org-code t))
  5521. ;; COMMENT
  5522. (list (format org-heading-keyword-regexp-format
  5523. (concat "\\("
  5524. org-comment-string "\\|" org-quote-string
  5525. "\\)"))
  5526. '(2 'org-special-keyword t))
  5527. ;; Blocks and meta lines
  5528. '(org-fontify-meta-lines-and-blocks)
  5529. )))
  5530. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5531. (run-hooks 'org-font-lock-set-keywords-hook)
  5532. ;; Now set the full font-lock-keywords
  5533. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5534. (org-set-local 'font-lock-defaults
  5535. '(org-font-lock-keywords t nil nil backward-paragraph))
  5536. (kill-local-variable 'font-lock-keywords) nil))
  5537. (defun org-toggle-pretty-entities ()
  5538. "Toggle the composition display of entities as UTF8 characters."
  5539. (interactive)
  5540. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5541. (org-restart-font-lock)
  5542. (if org-pretty-entities
  5543. (message "Entities are now displayed as UTF8 characters")
  5544. (save-restriction
  5545. (widen)
  5546. (org-decompose-region (point-min) (point-max))
  5547. (message "Entities are now displayed as plain text"))))
  5548. (defvar org-custom-properties-overlays nil
  5549. "List of overlays used for custom properties.")
  5550. (make-variable-buffer-local 'org-custom-properties-overlays)
  5551. (defun org-toggle-custom-properties-visibility ()
  5552. "Display or hide properties in `org-custom-properties'."
  5553. (interactive)
  5554. (if org-custom-properties-overlays
  5555. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5556. (setq org-custom-properties-overlays nil))
  5557. (unless (not org-custom-properties)
  5558. (save-excursion
  5559. (save-restriction
  5560. (widen)
  5561. (goto-char (point-min))
  5562. (while (re-search-forward org-property-re nil t)
  5563. (mapc (lambda(p)
  5564. (when (equal p (substring (match-string 1) 1 -1))
  5565. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5566. (overlay-put o 'invisible t)
  5567. (overlay-put o 'org-custom-property t)
  5568. (push o org-custom-properties-overlays))))
  5569. org-custom-properties)))))))
  5570. (defun org-fontify-entities (limit)
  5571. "Find an entity to fontify."
  5572. (let (ee)
  5573. (when org-pretty-entities
  5574. (catch 'match
  5575. (while (re-search-forward
  5576. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5577. limit t)
  5578. (if (and (not (org-in-indented-comment-line))
  5579. (setq ee (org-entity-get (match-string 1)))
  5580. (= (length (nth 6 ee)) 1))
  5581. (let*
  5582. ((end (if (equal (match-string 2) "{}")
  5583. (match-end 2)
  5584. (match-end 1))))
  5585. (add-text-properties
  5586. (match-beginning 0) end
  5587. (list 'font-lock-fontified t))
  5588. (compose-region (match-beginning 0) end
  5589. (nth 6 ee) nil)
  5590. (backward-char 1)
  5591. (throw 'match t))))
  5592. nil))))
  5593. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5594. "Fontify string S like in Org-mode."
  5595. (with-temp-buffer
  5596. (insert s)
  5597. (let ((org-odd-levels-only odd-levels))
  5598. (org-mode)
  5599. (font-lock-fontify-buffer)
  5600. (buffer-string))))
  5601. (defvar org-m nil)
  5602. (defvar org-l nil)
  5603. (defvar org-f nil)
  5604. (defun org-get-level-face (n)
  5605. "Get the right face for match N in font-lock matching of headlines."
  5606. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5607. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5608. (if org-cycle-level-faces
  5609. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5610. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5611. (cond
  5612. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5613. ((eq n 2) org-f)
  5614. (t (if org-level-color-stars-only nil org-f))))
  5615. (defun org-get-todo-face (kwd)
  5616. "Get the right face for a TODO keyword KWD.
  5617. If KWD is a number, get the corresponding match group."
  5618. (if (numberp kwd) (setq kwd (match-string kwd)))
  5619. (or (org-face-from-face-or-color
  5620. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5621. (and (member kwd org-done-keywords) 'org-done)
  5622. 'org-todo))
  5623. (defun org-face-from-face-or-color (context inherit face-or-color)
  5624. "Create a face list that inherits INHERIT, but sets the foreground color.
  5625. When FACE-OR-COLOR is not a string, just return it."
  5626. (if (stringp face-or-color)
  5627. (list :inherit inherit
  5628. (cdr (assoc context org-faces-easy-properties))
  5629. face-or-color)
  5630. face-or-color))
  5631. (defun org-font-lock-add-tag-faces (limit)
  5632. "Add the special tag faces."
  5633. (when (and org-tag-faces org-tags-special-faces-re)
  5634. (while (re-search-forward org-tags-special-faces-re limit t)
  5635. (add-text-properties (match-beginning 1) (match-end 1)
  5636. (list 'face (org-get-tag-face 1)
  5637. 'font-lock-fontified t))
  5638. (backward-char 1))))
  5639. (defun org-font-lock-add-priority-faces (limit)
  5640. "Add the special priority faces."
  5641. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5642. (when (save-match-data (org-at-heading-p))
  5643. (add-text-properties
  5644. (match-beginning 0) (match-end 0)
  5645. (list 'face (or (org-face-from-face-or-color
  5646. 'priority 'org-priority
  5647. (cdr (assoc (char-after (match-beginning 1))
  5648. org-priority-faces)))
  5649. 'org-priority)
  5650. 'font-lock-fontified t)))))
  5651. (defun org-get-tag-face (kwd)
  5652. "Get the right face for a TODO keyword KWD.
  5653. If KWD is a number, get the corresponding match group."
  5654. (if (numberp kwd) (setq kwd (match-string kwd)))
  5655. (or (org-face-from-face-or-color
  5656. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5657. 'org-tag))
  5658. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5659. "Remove fontification and activation overlays from links."
  5660. (font-lock-default-unfontify-region beg end)
  5661. (let* ((buffer-undo-list t)
  5662. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5663. (inhibit-modification-hooks t)
  5664. deactivate-mark buffer-file-name buffer-file-truename)
  5665. (org-decompose-region beg end)
  5666. (remove-text-properties beg end
  5667. '(mouse-face t keymap t org-linked-text t
  5668. invisible t intangible t
  5669. org-no-flyspell t org-emphasis t))
  5670. (org-remove-font-lock-display-properties beg end)))
  5671. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5672. ((raise 0.3) (height 0.7))
  5673. ((raise -0.5))
  5674. ((raise 0.5)))
  5675. "Display properties for showing superscripts and subscripts.")
  5676. (defun org-remove-font-lock-display-properties (beg end)
  5677. "Remove specific display properties that have been added by font lock.
  5678. The will remove the raise properties that are used to show superscripts
  5679. and subscripts."
  5680. (let (next prop)
  5681. (while (< beg end)
  5682. (setq next (next-single-property-change beg 'display nil end)
  5683. prop (get-text-property beg 'display))
  5684. (if (member prop org-script-display)
  5685. (put-text-property beg next 'display nil))
  5686. (setq beg next))))
  5687. (defun org-raise-scripts (limit)
  5688. "Add raise properties to sub/superscripts."
  5689. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5690. (if (re-search-forward
  5691. (if (eq org-use-sub-superscripts t)
  5692. org-match-substring-regexp
  5693. org-match-substring-with-braces-regexp)
  5694. limit t)
  5695. (let* ((pos (point)) table-p comment-p
  5696. (mpos (match-beginning 3))
  5697. (emph-p (get-text-property mpos 'org-emphasis))
  5698. (link-p (get-text-property mpos 'mouse-face))
  5699. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5700. (goto-char (point-at-bol))
  5701. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5702. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5703. (goto-char pos)
  5704. ;; Handle a_b^c
  5705. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5706. (if (or comment-p emph-p link-p keyw-p)
  5707. t
  5708. (put-text-property (match-beginning 3) (match-end 0)
  5709. 'display
  5710. (if (equal (char-after (match-beginning 2)) ?^)
  5711. (nth (if table-p 3 1) org-script-display)
  5712. (nth (if table-p 2 0) org-script-display)))
  5713. (add-text-properties (match-beginning 2) (match-end 2)
  5714. (list 'invisible t
  5715. 'org-dwidth t 'org-dwidth-n 1))
  5716. (if (and (eq (char-after (match-beginning 3)) ?{)
  5717. (eq (char-before (match-end 3)) ?}))
  5718. (progn
  5719. (add-text-properties
  5720. (match-beginning 3) (1+ (match-beginning 3))
  5721. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5722. (add-text-properties
  5723. (1- (match-end 3)) (match-end 3)
  5724. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5725. t)))))
  5726. ;;;; Visibility cycling, including org-goto and indirect buffer
  5727. ;;; Cycling
  5728. (defvar org-cycle-global-status nil)
  5729. (make-variable-buffer-local 'org-cycle-global-status)
  5730. (put 'org-cycle-global-status 'org-state t)
  5731. (defvar org-cycle-subtree-status nil)
  5732. (make-variable-buffer-local 'org-cycle-subtree-status)
  5733. (put 'org-cycle-subtree-status 'org-state t)
  5734. (defvar org-inlinetask-min-level)
  5735. ;;;###autoload
  5736. (defun org-cycle (&optional arg)
  5737. "TAB-action and visibility cycling for Org-mode.
  5738. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5739. is outline visibility cycling, but it also invokes other actions
  5740. in special contexts.
  5741. - When this function is called with a prefix argument, rotate the entire
  5742. buffer through 3 states (global cycling)
  5743. 1. OVERVIEW: Show only top-level headlines.
  5744. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5745. 3. SHOW ALL: Show everything.
  5746. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5747. determined by the variable `org-startup-folded', and by any VISIBILITY
  5748. properties in the buffer.
  5749. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5750. including any drawers.
  5751. - When inside a table, re-align the table and move to the next field.
  5752. - When point is at the beginning of a headline, rotate the subtree started
  5753. by this line through 3 different states (local cycling)
  5754. 1. FOLDED: Only the main headline is shown.
  5755. 2. CHILDREN: The main headline and the direct children are shown.
  5756. From this state, you can move to one of the children
  5757. and zoom in further.
  5758. 3. SUBTREE: Show the entire subtree, including body text.
  5759. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5760. - When point is at the beginning of an empty headline and the variable
  5761. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5762. of the headline by demoting and promoting it to likely levels. This
  5763. speeds up creation document structure by pressing TAB once or several
  5764. times right after creating a new headline.
  5765. - When there is a numeric prefix, go up to a heading with level ARG, do
  5766. a `show-subtree' and return to the previous cursor position. If ARG
  5767. is negative, go up that many levels.
  5768. - When point is not at the beginning of a headline, execute the global
  5769. binding for TAB, which is re-indenting the line. See the option
  5770. `org-cycle-emulate-tab' for details.
  5771. - Special case: if point is at the beginning of the buffer and there is
  5772. no headline in line 1, this function will act as if called with prefix arg
  5773. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5774. But only if also the variable `org-cycle-global-at-bob' is t."
  5775. (interactive "P")
  5776. (org-load-modules-maybe)
  5777. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5778. (and org-cycle-level-after-item/entry-creation
  5779. (or (org-cycle-level)
  5780. (org-cycle-item-indentation))))
  5781. (let* (message-log-max ; Don't populate the *Messages* buffer
  5782. (limit-level
  5783. (or org-cycle-max-level
  5784. (and (boundp 'org-inlinetask-min-level)
  5785. org-inlinetask-min-level
  5786. (1- org-inlinetask-min-level))))
  5787. (nstars (and limit-level
  5788. (if org-odd-levels-only
  5789. (and limit-level (1- (* limit-level 2)))
  5790. limit-level)))
  5791. (org-outline-regexp
  5792. (if (not (derived-mode-p 'org-mode))
  5793. outline-regexp
  5794. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5795. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5796. (not (looking-at org-outline-regexp))))
  5797. (org-cycle-hook
  5798. (if bob-special
  5799. (delq 'org-optimize-window-after-visibility-change
  5800. (copy-sequence org-cycle-hook))
  5801. org-cycle-hook))
  5802. (pos (point)))
  5803. (if (or bob-special (equal arg '(4)))
  5804. ;; special case: use global cycling
  5805. (setq arg t))
  5806. (cond
  5807. ((equal arg '(16))
  5808. (setq last-command 'dummy)
  5809. (org-set-startup-visibility)
  5810. (message "Startup visibility, plus VISIBILITY properties"))
  5811. ((equal arg '(64))
  5812. (show-all)
  5813. (message "Entire buffer visible, including drawers"))
  5814. ;; Table: enter it or move to the next field.
  5815. ((org-at-table-p 'any)
  5816. (if (org-at-table.el-p)
  5817. (message "Use C-c ' to edit table.el tables")
  5818. (if arg (org-table-edit-field t)
  5819. (org-table-justify-field-maybe)
  5820. (call-interactively 'org-table-next-field))))
  5821. ((run-hook-with-args-until-success
  5822. 'org-tab-after-check-for-table-hook))
  5823. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5824. ((eq arg t) (org-cycle-internal-global))
  5825. ;; Drawers: delegate to `org-flag-drawer'.
  5826. ((and org-drawers org-drawer-regexp
  5827. (save-excursion
  5828. (beginning-of-line 1)
  5829. (looking-at org-drawer-regexp)))
  5830. (org-flag-drawer ; toggle block visibility
  5831. (not (get-char-property (match-end 0) 'invisible))))
  5832. ;; Show-subtree, ARG levels up from here.
  5833. ((integerp arg)
  5834. (save-excursion
  5835. (org-back-to-heading)
  5836. (outline-up-heading (if (< arg 0) (- arg)
  5837. (- (funcall outline-level) arg)))
  5838. (org-show-subtree)))
  5839. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5840. ((and (featurep 'org-inlinetask)
  5841. (org-inlinetask-at-task-p)
  5842. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5843. (org-inlinetask-toggle-visibility))
  5844. ((org-try-cdlatex-tab))
  5845. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5846. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5847. (save-excursion (beginning-of-line 1)
  5848. (looking-at org-outline-regexp)))
  5849. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5850. (org-cycle-internal-local))
  5851. ;; From there: TAB emulation and template completion.
  5852. (buffer-read-only (org-back-to-heading))
  5853. ((run-hook-with-args-until-success
  5854. 'org-tab-after-check-for-cycling-hook))
  5855. ((org-try-structure-completion))
  5856. ((run-hook-with-args-until-success
  5857. 'org-tab-before-tab-emulation-hook))
  5858. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5859. (or (not (bolp))
  5860. (not (looking-at org-outline-regexp))))
  5861. (call-interactively (global-key-binding "\t")))
  5862. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5863. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5864. (or (and (eq org-cycle-emulate-tab 'white)
  5865. (= (match-end 0) (point-at-eol)))
  5866. (and (eq org-cycle-emulate-tab 'whitestart)
  5867. (>= (match-end 0) pos))))
  5868. t
  5869. (eq org-cycle-emulate-tab t))
  5870. (call-interactively (global-key-binding "\t")))
  5871. (t (save-excursion
  5872. (org-back-to-heading)
  5873. (org-cycle)))))))
  5874. (defun org-cycle-internal-global ()
  5875. "Do the global cycling action."
  5876. ;; Hack to avoid display of messages for .org attachments in Gnus
  5877. (let (message-log-max ; Don't populate the *Messages* buffer
  5878. (ga (string-match "\\*fontification" (buffer-name))))
  5879. (cond
  5880. ((and (eq last-command this-command)
  5881. (eq org-cycle-global-status 'overview))
  5882. ;; We just created the overview - now do table of contents
  5883. ;; This can be slow in very large buffers, so indicate action
  5884. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5885. (unless ga (message "CONTENTS..."))
  5886. (org-content)
  5887. (unless ga (message "CONTENTS...done"))
  5888. (setq org-cycle-global-status 'contents)
  5889. (run-hook-with-args 'org-cycle-hook 'contents))
  5890. ((and (eq last-command this-command)
  5891. (eq org-cycle-global-status 'contents))
  5892. ;; We just showed the table of contents - now show everything
  5893. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5894. (show-all)
  5895. (unless ga (message "SHOW ALL"))
  5896. (setq org-cycle-global-status 'all)
  5897. (run-hook-with-args 'org-cycle-hook 'all))
  5898. (t
  5899. ;; Default action: go to overview
  5900. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5901. (org-overview)
  5902. (unless ga (message "OVERVIEW"))
  5903. (setq org-cycle-global-status 'overview)
  5904. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5905. (defun org-cycle-internal-local ()
  5906. "Do the local cycling action."
  5907. (let (message-log-max ; Don't populate the *Messages* buffer
  5908. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5909. ;; First, determine end of headline (EOH), end of subtree or item
  5910. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5911. (save-excursion
  5912. (if (org-at-item-p)
  5913. (progn
  5914. (beginning-of-line)
  5915. (setq struct (org-list-struct))
  5916. (setq eoh (point-at-eol))
  5917. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5918. (setq has-children (org-list-has-child-p (point) struct)))
  5919. (org-back-to-heading)
  5920. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5921. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5922. (setq has-children
  5923. (or (save-excursion
  5924. (let ((level (funcall outline-level)))
  5925. (outline-next-heading)
  5926. (and (org-at-heading-p t)
  5927. (> (funcall outline-level) level))))
  5928. (save-excursion
  5929. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5930. ;; Determine end invisible part of buffer (EOL)
  5931. (beginning-of-line 2)
  5932. ;; XEmacs doesn't have `next-single-char-property-change'
  5933. (if (featurep 'xemacs)
  5934. (while (and (not (eobp)) ;; this is like `next-line'
  5935. (get-char-property (1- (point)) 'invisible))
  5936. (beginning-of-line 2))
  5937. (while (and (not (eobp)) ;; this is like `next-line'
  5938. (get-char-property (1- (point)) 'invisible))
  5939. (goto-char (next-single-char-property-change (point) 'invisible))
  5940. (and (eolp) (beginning-of-line 2))))
  5941. (setq eol (point)))
  5942. ;; Find out what to do next and set `this-command'
  5943. (cond
  5944. ((= eos eoh)
  5945. ;; Nothing is hidden behind this heading
  5946. (unless (org-before-first-heading-p)
  5947. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5948. (message "EMPTY ENTRY")
  5949. (setq org-cycle-subtree-status nil)
  5950. (save-excursion
  5951. (goto-char eos)
  5952. (outline-next-heading)
  5953. (if (outline-invisible-p) (org-flag-heading nil))))
  5954. ((and (or (>= eol eos)
  5955. (not (string-match "\\S-" (buffer-substring eol eos))))
  5956. (or has-children
  5957. (not (setq children-skipped
  5958. org-cycle-skip-children-state-if-no-children))))
  5959. ;; Entire subtree is hidden in one line: children view
  5960. (unless (org-before-first-heading-p)
  5961. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5962. (if (org-at-item-p)
  5963. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5964. (org-show-entry)
  5965. (org-with-limited-levels (show-children))
  5966. ;; FIXME: This slows down the func way too much.
  5967. ;; How keep drawers hidden in subtree anyway?
  5968. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5969. ;; (org-cycle-hide-drawers 'subtree))
  5970. ;; Fold every list in subtree to top-level items.
  5971. (when (eq org-cycle-include-plain-lists 'integrate)
  5972. (save-excursion
  5973. (org-back-to-heading)
  5974. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5975. (beginning-of-line 1)
  5976. (let* ((struct (org-list-struct))
  5977. (prevs (org-list-prevs-alist struct))
  5978. (end (org-list-get-bottom-point struct)))
  5979. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5980. (org-list-get-all-items (point) struct prevs))
  5981. (goto-char (if (< end eos) end eos)))))))
  5982. (message "CHILDREN")
  5983. (save-excursion
  5984. (goto-char eos)
  5985. (outline-next-heading)
  5986. (if (outline-invisible-p) (org-flag-heading nil)))
  5987. (setq org-cycle-subtree-status 'children)
  5988. (unless (org-before-first-heading-p)
  5989. (run-hook-with-args 'org-cycle-hook 'children)))
  5990. ((or children-skipped
  5991. (and (eq last-command this-command)
  5992. (eq org-cycle-subtree-status 'children)))
  5993. ;; We just showed the children, or no children are there,
  5994. ;; now show everything.
  5995. (unless (org-before-first-heading-p)
  5996. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5997. (outline-flag-region eoh eos nil)
  5998. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5999. (setq org-cycle-subtree-status 'subtree)
  6000. (unless (org-before-first-heading-p)
  6001. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6002. (t
  6003. ;; Default action: hide the subtree.
  6004. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6005. (outline-flag-region eoh eos t)
  6006. (message "FOLDED")
  6007. (setq org-cycle-subtree-status 'folded)
  6008. (unless (org-before-first-heading-p)
  6009. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6010. ;;;###autoload
  6011. (defun org-global-cycle (&optional arg)
  6012. "Cycle the global visibility. For details see `org-cycle'.
  6013. With \\[universal-argument] prefix arg, switch to startup visibility.
  6014. With a numeric prefix, show all headlines up to that level."
  6015. (interactive "P")
  6016. (let ((org-cycle-include-plain-lists
  6017. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6018. (cond
  6019. ((integerp arg)
  6020. (show-all)
  6021. (hide-sublevels arg)
  6022. (setq org-cycle-global-status 'contents))
  6023. ((equal arg '(4))
  6024. (org-set-startup-visibility)
  6025. (message "Startup visibility, plus VISIBILITY properties."))
  6026. (t
  6027. (org-cycle '(4))))))
  6028. (defun org-set-startup-visibility ()
  6029. "Set the visibility required by startup options and properties."
  6030. (cond
  6031. ((eq org-startup-folded t)
  6032. (org-cycle '(4)))
  6033. ((eq org-startup-folded 'content)
  6034. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6035. (org-cycle '(4)) (org-cycle '(4)))))
  6036. (unless (eq org-startup-folded 'showeverything)
  6037. (if org-hide-block-startup (org-hide-block-all))
  6038. (org-set-visibility-according-to-property 'no-cleanup)
  6039. (org-cycle-hide-archived-subtrees 'all)
  6040. (org-cycle-hide-drawers 'all)
  6041. (org-cycle-show-empty-lines t)))
  6042. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6043. "Switch subtree visibilities according to :VISIBILITY: property."
  6044. (interactive)
  6045. (let (org-show-entry-below state)
  6046. (save-excursion
  6047. (goto-char (point-min))
  6048. (while (re-search-forward
  6049. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6050. nil t)
  6051. (setq state (match-string 1))
  6052. (save-excursion
  6053. (org-back-to-heading t)
  6054. (hide-subtree)
  6055. (org-reveal)
  6056. (cond
  6057. ((equal state '("fold" "folded"))
  6058. (hide-subtree))
  6059. ((equal state "children")
  6060. (org-show-hidden-entry)
  6061. (show-children))
  6062. ((equal state "content")
  6063. (save-excursion
  6064. (save-restriction
  6065. (org-narrow-to-subtree)
  6066. (org-content))))
  6067. ((member state '("all" "showall"))
  6068. (show-subtree)))))
  6069. (unless no-cleanup
  6070. (org-cycle-hide-archived-subtrees 'all)
  6071. (org-cycle-hide-drawers 'all)
  6072. (org-cycle-show-empty-lines 'all)))))
  6073. ;; This function uses outline-regexp instead of the more fundamental
  6074. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6075. ;; buffers, where outline-regexp is needed.
  6076. (defun org-overview ()
  6077. "Switch to overview mode, showing only top-level headlines.
  6078. Really, this shows all headlines with level equal or greater than the level
  6079. of the first headline in the buffer. This is important, because if the
  6080. first headline is not level one, then (hide-sublevels 1) gives confusing
  6081. results."
  6082. (interactive)
  6083. (let ((l (org-current-line))
  6084. (level (save-excursion
  6085. (goto-char (point-min))
  6086. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6087. (progn
  6088. (goto-char (match-beginning 0))
  6089. (funcall outline-level))))))
  6090. (and level (hide-sublevels level))
  6091. (recenter '(4))
  6092. (org-goto-line l)))
  6093. (defun org-content (&optional arg)
  6094. "Show all headlines in the buffer, like a table of contents.
  6095. With numerical argument N, show content up to level N."
  6096. (interactive "P")
  6097. (save-excursion
  6098. ;; Visit all headings and show their offspring
  6099. (and (integerp arg) (org-overview))
  6100. (goto-char (point-max))
  6101. (catch 'exit
  6102. (while (and (progn (condition-case nil
  6103. (outline-previous-visible-heading 1)
  6104. (error (goto-char (point-min))))
  6105. t)
  6106. (looking-at org-outline-regexp))
  6107. (if (integerp arg)
  6108. (show-children (1- arg))
  6109. (show-branches))
  6110. (if (bobp) (throw 'exit nil))))))
  6111. (defun org-optimize-window-after-visibility-change (state)
  6112. "Adjust the window after a change in outline visibility.
  6113. This function is the default value of the hook `org-cycle-hook'."
  6114. (when (get-buffer-window (current-buffer))
  6115. (cond
  6116. ((eq state 'content) nil)
  6117. ((eq state 'all) nil)
  6118. ((eq state 'folded) nil)
  6119. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6120. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6121. (defun org-remove-empty-overlays-at (pos)
  6122. "Remove outline overlays that do not contain non-white stuff."
  6123. (mapc
  6124. (lambda (o)
  6125. (and (eq 'outline (overlay-get o 'invisible))
  6126. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6127. (overlay-end o))))
  6128. (delete-overlay o)))
  6129. (overlays-at pos)))
  6130. (defun org-clean-visibility-after-subtree-move ()
  6131. "Fix visibility issues after moving a subtree."
  6132. ;; First, find a reasonable region to look at:
  6133. ;; Start two siblings above, end three below
  6134. (let* ((beg (save-excursion
  6135. (and (org-get-last-sibling)
  6136. (org-get-last-sibling))
  6137. (point)))
  6138. (end (save-excursion
  6139. (and (org-get-next-sibling)
  6140. (org-get-next-sibling)
  6141. (org-get-next-sibling))
  6142. (if (org-at-heading-p)
  6143. (point-at-eol)
  6144. (point))))
  6145. (level (looking-at "\\*+"))
  6146. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6147. (save-excursion
  6148. (save-restriction
  6149. (narrow-to-region beg end)
  6150. (when re
  6151. ;; Properly fold already folded siblings
  6152. (goto-char (point-min))
  6153. (while (re-search-forward re nil t)
  6154. (if (and (not (outline-invisible-p))
  6155. (save-excursion
  6156. (goto-char (point-at-eol)) (outline-invisible-p)))
  6157. (hide-entry))))
  6158. (org-cycle-show-empty-lines 'overview)
  6159. (org-cycle-hide-drawers 'overview)))))
  6160. (defun org-cycle-show-empty-lines (state)
  6161. "Show empty lines above all visible headlines.
  6162. The region to be covered depends on STATE when called through
  6163. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6164. entire buffer covered. Note that an empty line is only shown if there
  6165. are at least `org-cycle-separator-lines' empty lines before the headline."
  6166. (when (not (= org-cycle-separator-lines 0))
  6167. (save-excursion
  6168. (let* ((n (abs org-cycle-separator-lines))
  6169. (re (cond
  6170. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6171. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6172. (t (let ((ns (number-to-string (- n 2))))
  6173. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6174. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6175. beg end b e)
  6176. (cond
  6177. ((memq state '(overview contents t))
  6178. (setq beg (point-min) end (point-max)))
  6179. ((memq state '(children folded))
  6180. (setq beg (point) end (progn (org-end-of-subtree t t)
  6181. (beginning-of-line 2)
  6182. (point)))))
  6183. (when beg
  6184. (goto-char beg)
  6185. (while (re-search-forward re end t)
  6186. (unless (get-char-property (match-end 1) 'invisible)
  6187. (setq e (match-end 1))
  6188. (if (< org-cycle-separator-lines 0)
  6189. (setq b (save-excursion
  6190. (goto-char (match-beginning 0))
  6191. (org-back-over-empty-lines)
  6192. (if (save-excursion
  6193. (goto-char (max (point-min) (1- (point))))
  6194. (org-at-heading-p))
  6195. (1- (point))
  6196. (point))))
  6197. (setq b (match-beginning 1)))
  6198. (outline-flag-region b e nil)))))))
  6199. ;; Never hide empty lines at the end of the file.
  6200. (save-excursion
  6201. (goto-char (point-max))
  6202. (outline-previous-heading)
  6203. (outline-end-of-heading)
  6204. (if (and (looking-at "[ \t\n]+")
  6205. (= (match-end 0) (point-max)))
  6206. (outline-flag-region (point) (match-end 0) nil))))
  6207. (defun org-show-empty-lines-in-parent ()
  6208. "Move to the parent and re-show empty lines before visible headlines."
  6209. (save-excursion
  6210. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6211. (org-cycle-show-empty-lines context))))
  6212. (defun org-files-list ()
  6213. "Return `org-agenda-files' list, plus all open org-mode files.
  6214. This is useful for operations that need to scan all of a user's
  6215. open and agenda-wise Org files."
  6216. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6217. (dolist (buf (buffer-list))
  6218. (with-current-buffer buf
  6219. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6220. (let ((file (expand-file-name (buffer-file-name))))
  6221. (unless (member file files)
  6222. (push file files))))))
  6223. files))
  6224. (defsubst org-entry-beginning-position ()
  6225. "Return the beginning position of the current entry."
  6226. (save-excursion (outline-back-to-heading t) (point)))
  6227. (defsubst org-entry-end-position ()
  6228. "Return the end position of the current entry."
  6229. (save-excursion (outline-next-heading) (point)))
  6230. (defun org-cycle-hide-drawers (state)
  6231. "Re-hide all drawers after a visibility state change."
  6232. (when (and (derived-mode-p 'org-mode)
  6233. (not (memq state '(overview folded contents))))
  6234. (save-excursion
  6235. (let* ((globalp (memq state '(contents all)))
  6236. (beg (if globalp (point-min) (point)))
  6237. (end (if globalp (point-max)
  6238. (if (eq state 'children)
  6239. (save-excursion (outline-next-heading) (point))
  6240. (org-end-of-subtree t)))))
  6241. (goto-char beg)
  6242. (while (re-search-forward org-drawer-regexp end t)
  6243. (org-flag-drawer t))))))
  6244. (defun org-cycle-hide-inline-tasks (state)
  6245. "Re-hide inline task when switching to 'contents visibility state."
  6246. (when (and (eq state 'contents)
  6247. (boundp 'org-inlinetask-min-level)
  6248. org-inlinetask-min-level)
  6249. (hide-sublevels (1- org-inlinetask-min-level))))
  6250. (defun org-flag-drawer (flag)
  6251. "When FLAG is non-nil, hide the drawer we are within.
  6252. Otherwise make it visible."
  6253. (save-excursion
  6254. (beginning-of-line 1)
  6255. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6256. (let ((b (match-end 0)))
  6257. (if (re-search-forward
  6258. "^[ \t]*:END:"
  6259. (save-excursion (outline-next-heading) (point)) t)
  6260. (outline-flag-region b (point-at-eol) flag)
  6261. (error ":END: line missing at position %s" b))))))
  6262. (defun org-subtree-end-visible-p ()
  6263. "Is the end of the current subtree visible?"
  6264. (pos-visible-in-window-p
  6265. (save-excursion (org-end-of-subtree t) (point))))
  6266. (defun org-first-headline-recenter (&optional N)
  6267. "Move cursor to the first headline and recenter the headline.
  6268. Optional argument N means put the headline into the Nth line of the window."
  6269. (goto-char (point-min))
  6270. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6271. (beginning-of-line)
  6272. (recenter (prefix-numeric-value N))))
  6273. ;;; Saving and restoring visibility
  6274. (defun org-outline-overlay-data (&optional use-markers)
  6275. "Return a list of the locations of all outline overlays.
  6276. These are overlays with the `invisible' property value `outline'.
  6277. The return value is a list of cons cells, with start and stop
  6278. positions for each overlay.
  6279. If USE-MARKERS is set, return the positions as markers."
  6280. (let (beg end)
  6281. (save-excursion
  6282. (save-restriction
  6283. (widen)
  6284. (delq nil
  6285. (mapcar (lambda (o)
  6286. (when (eq (overlay-get o 'invisible) 'outline)
  6287. (setq beg (overlay-start o)
  6288. end (overlay-end o))
  6289. (and beg end (> end beg)
  6290. (if use-markers
  6291. (cons (move-marker (make-marker) beg)
  6292. (move-marker (make-marker) end))
  6293. (cons beg end)))))
  6294. (overlays-in (point-min) (point-max))))))))
  6295. (defun org-set-outline-overlay-data (data)
  6296. "Create visibility overlays for all positions in DATA.
  6297. DATA should have been made by `org-outline-overlay-data'."
  6298. (let (o)
  6299. (save-excursion
  6300. (save-restriction
  6301. (widen)
  6302. (show-all)
  6303. (mapc (lambda (c)
  6304. (outline-flag-region (car c) (cdr c) t))
  6305. data)))))
  6306. ;;; Folding of blocks
  6307. (defvar org-hide-block-overlays nil
  6308. "Overlays hiding blocks.")
  6309. (make-variable-buffer-local 'org-hide-block-overlays)
  6310. (defun org-block-map (function &optional start end)
  6311. "Call FUNCTION at the head of all source blocks in the current buffer.
  6312. Optional arguments START and END can be used to limit the range."
  6313. (let ((start (or start (point-min)))
  6314. (end (or end (point-max))))
  6315. (save-excursion
  6316. (goto-char start)
  6317. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6318. (save-excursion
  6319. (save-match-data
  6320. (goto-char (match-beginning 0))
  6321. (funcall function)))))))
  6322. (defun org-hide-block-toggle-all ()
  6323. "Toggle the visibility of all blocks in the current buffer."
  6324. (org-block-map #'org-hide-block-toggle))
  6325. (defun org-hide-block-all ()
  6326. "Fold all blocks in the current buffer."
  6327. (interactive)
  6328. (org-show-block-all)
  6329. (org-block-map #'org-hide-block-toggle-maybe))
  6330. (defun org-show-block-all ()
  6331. "Unfold all blocks in the current buffer."
  6332. (interactive)
  6333. (mapc 'delete-overlay org-hide-block-overlays)
  6334. (setq org-hide-block-overlays nil))
  6335. (defun org-hide-block-toggle-maybe ()
  6336. "Toggle visibility of block at point."
  6337. (interactive)
  6338. (let ((case-fold-search t))
  6339. (if (save-excursion
  6340. (beginning-of-line 1)
  6341. (looking-at org-block-regexp))
  6342. (progn (org-hide-block-toggle)
  6343. t) ;; to signal that we took action
  6344. nil))) ;; to signal that we did not
  6345. (defun org-hide-block-toggle (&optional force)
  6346. "Toggle the visibility of the current block."
  6347. (interactive)
  6348. (save-excursion
  6349. (beginning-of-line)
  6350. (if (re-search-forward org-block-regexp nil t)
  6351. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6352. (end (match-end 0)) ;; end of entire body
  6353. ov)
  6354. (if (memq t (mapcar (lambda (overlay)
  6355. (eq (overlay-get overlay 'invisible)
  6356. 'org-hide-block))
  6357. (overlays-at start)))
  6358. (if (or (not force) (eq force 'off))
  6359. (mapc (lambda (ov)
  6360. (when (member ov org-hide-block-overlays)
  6361. (setq org-hide-block-overlays
  6362. (delq ov org-hide-block-overlays)))
  6363. (when (eq (overlay-get ov 'invisible)
  6364. 'org-hide-block)
  6365. (delete-overlay ov)))
  6366. (overlays-at start)))
  6367. (setq ov (make-overlay start end))
  6368. (overlay-put ov 'invisible 'org-hide-block)
  6369. ;; make the block accessible to isearch
  6370. (overlay-put
  6371. ov 'isearch-open-invisible
  6372. (lambda (ov)
  6373. (when (member ov org-hide-block-overlays)
  6374. (setq org-hide-block-overlays
  6375. (delq ov org-hide-block-overlays)))
  6376. (when (eq (overlay-get ov 'invisible)
  6377. 'org-hide-block)
  6378. (delete-overlay ov))))
  6379. (push ov org-hide-block-overlays)))
  6380. (error "Not looking at a source block"))))
  6381. ;; org-tab-after-check-for-cycling-hook
  6382. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6383. ;; Remove overlays when changing major mode
  6384. (add-hook 'org-mode-hook
  6385. (lambda () (org-add-hook 'change-major-mode-hook
  6386. 'org-show-block-all 'append 'local)))
  6387. ;;; Org-goto
  6388. (defvar org-goto-window-configuration nil)
  6389. (defvar org-goto-marker nil)
  6390. (defvar org-goto-map)
  6391. (defun org-goto-map ()
  6392. "Set the keymap `org-goto'."
  6393. (setq org-goto-map
  6394. (let ((map (make-sparse-keymap)))
  6395. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6396. mouse-drag-region universal-argument org-occur))
  6397. cmd)
  6398. (while (setq cmd (pop cmds))
  6399. (substitute-key-definition cmd cmd map global-map)))
  6400. (suppress-keymap map)
  6401. (org-defkey map "\C-m" 'org-goto-ret)
  6402. (org-defkey map [(return)] 'org-goto-ret)
  6403. (org-defkey map [(left)] 'org-goto-left)
  6404. (org-defkey map [(right)] 'org-goto-right)
  6405. (org-defkey map [(control ?g)] 'org-goto-quit)
  6406. (org-defkey map "\C-i" 'org-cycle)
  6407. (org-defkey map [(tab)] 'org-cycle)
  6408. (org-defkey map [(down)] 'outline-next-visible-heading)
  6409. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6410. (if org-goto-auto-isearch
  6411. (if (fboundp 'define-key-after)
  6412. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6413. nil)
  6414. (org-defkey map "q" 'org-goto-quit)
  6415. (org-defkey map "n" 'outline-next-visible-heading)
  6416. (org-defkey map "p" 'outline-previous-visible-heading)
  6417. (org-defkey map "f" 'outline-forward-same-level)
  6418. (org-defkey map "b" 'outline-backward-same-level)
  6419. (org-defkey map "u" 'outline-up-heading))
  6420. (org-defkey map "/" 'org-occur)
  6421. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6422. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6423. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6424. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6425. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6426. map)))
  6427. (defconst org-goto-help
  6428. "Browse buffer copy, to find location or copy text.%s
  6429. RET=jump to location C-g=quit and return to previous location
  6430. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6431. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6432. (defun org-goto (&optional alternative-interface)
  6433. "Look up a different location in the current file, keeping current visibility.
  6434. When you want look-up or go to a different location in a
  6435. document, the fastest way is often to fold the entire buffer and
  6436. then dive into the tree. This method has the disadvantage, that
  6437. the previous location will be folded, which may not be what you
  6438. want.
  6439. This command works around this by showing a copy of the current
  6440. buffer in an indirect buffer, in overview mode. You can dive
  6441. into the tree in that copy, use org-occur and incremental search
  6442. to find a location. When pressing RET or `Q', the command
  6443. returns to the original buffer in which the visibility is still
  6444. unchanged. After RET it will also jump to the location selected
  6445. in the indirect buffer and expose the headline hierarchy above.
  6446. With a prefix argument, use the alternative interface: e.g. if
  6447. `org-goto-interface' is 'outline use 'outline-path-completion."
  6448. (interactive "P")
  6449. (org-goto-map)
  6450. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6451. (org-refile-use-outline-path t)
  6452. (org-refile-target-verify-function nil)
  6453. (interface
  6454. (if (not alternative-interface)
  6455. org-goto-interface
  6456. (if (eq org-goto-interface 'outline)
  6457. 'outline-path-completion
  6458. 'outline)))
  6459. (org-goto-start-pos (point))
  6460. (selected-point
  6461. (if (eq interface 'outline)
  6462. (car (org-get-location (current-buffer) org-goto-help))
  6463. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6464. (org-refile-check-position pa)
  6465. (nth 3 pa)))))
  6466. (if selected-point
  6467. (progn
  6468. (org-mark-ring-push org-goto-start-pos)
  6469. (goto-char selected-point)
  6470. (if (or (outline-invisible-p) (org-invisible-p2))
  6471. (org-show-context 'org-goto)))
  6472. (message "Quit"))))
  6473. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6474. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6475. (defvar org-goto-local-auto-isearch-map) ; defined below
  6476. (defun org-get-location (buf help)
  6477. "Let the user select a location in the Org-mode buffer BUF.
  6478. This function uses a recursive edit. It returns the selected position
  6479. or nil."
  6480. (org-no-popups
  6481. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6482. (isearch-hide-immediately nil)
  6483. (isearch-search-fun-function
  6484. (lambda () 'org-goto-local-search-headings))
  6485. (org-goto-selected-point org-goto-exit-command))
  6486. (save-excursion
  6487. (save-window-excursion
  6488. (delete-other-windows)
  6489. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6490. (org-pop-to-buffer-same-window
  6491. (condition-case nil
  6492. (make-indirect-buffer (current-buffer) "*org-goto*")
  6493. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6494. (with-output-to-temp-buffer "*Org Help*"
  6495. (princ (format help (if org-goto-auto-isearch
  6496. " Just type for auto-isearch."
  6497. " n/p/f/b/u to navigate, q to quit."))))
  6498. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6499. (setq buffer-read-only nil)
  6500. (let ((org-startup-truncated t)
  6501. (org-startup-folded nil)
  6502. (org-startup-align-all-tables nil))
  6503. (org-mode)
  6504. (org-overview))
  6505. (setq buffer-read-only t)
  6506. (if (and (boundp 'org-goto-start-pos)
  6507. (integer-or-marker-p org-goto-start-pos))
  6508. (let ((org-show-hierarchy-above t)
  6509. (org-show-siblings t)
  6510. (org-show-following-heading t))
  6511. (goto-char org-goto-start-pos)
  6512. (and (outline-invisible-p) (org-show-context)))
  6513. (goto-char (point-min)))
  6514. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6515. (message "Select location and press RET")
  6516. (use-local-map org-goto-map)
  6517. (recursive-edit)))
  6518. (kill-buffer "*org-goto*")
  6519. (cons org-goto-selected-point org-goto-exit-command))))
  6520. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6521. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6522. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6523. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6524. (defun org-goto-local-search-headings (string bound noerror)
  6525. "Search and make sure that any matches are in headlines."
  6526. (catch 'return
  6527. (while (if isearch-forward
  6528. (search-forward string bound noerror)
  6529. (search-backward string bound noerror))
  6530. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6531. (and (member :headline context)
  6532. (not (member :tags context))))
  6533. (throw 'return (point))))))
  6534. (defun org-goto-local-auto-isearch ()
  6535. "Start isearch."
  6536. (interactive)
  6537. (goto-char (point-min))
  6538. (let ((keys (this-command-keys)))
  6539. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6540. (isearch-mode t)
  6541. (isearch-process-search-char (string-to-char keys)))))
  6542. (defun org-goto-ret (&optional arg)
  6543. "Finish `org-goto' by going to the new location."
  6544. (interactive "P")
  6545. (setq org-goto-selected-point (point)
  6546. org-goto-exit-command 'return)
  6547. (throw 'exit nil))
  6548. (defun org-goto-left ()
  6549. "Finish `org-goto' by going to the new location."
  6550. (interactive)
  6551. (if (org-at-heading-p)
  6552. (progn
  6553. (beginning-of-line 1)
  6554. (setq org-goto-selected-point (point)
  6555. org-goto-exit-command 'left)
  6556. (throw 'exit nil))
  6557. (error "Not on a heading")))
  6558. (defun org-goto-right ()
  6559. "Finish `org-goto' by going to the new location."
  6560. (interactive)
  6561. (if (org-at-heading-p)
  6562. (progn
  6563. (setq org-goto-selected-point (point)
  6564. org-goto-exit-command 'right)
  6565. (throw 'exit nil))
  6566. (error "Not on a heading")))
  6567. (defun org-goto-quit ()
  6568. "Finish `org-goto' without cursor motion."
  6569. (interactive)
  6570. (setq org-goto-selected-point nil)
  6571. (setq org-goto-exit-command 'quit)
  6572. (throw 'exit nil))
  6573. ;;; Indirect buffer display of subtrees
  6574. (defvar org-indirect-dedicated-frame nil
  6575. "This is the frame being used for indirect tree display.")
  6576. (defvar org-last-indirect-buffer nil)
  6577. (defun org-tree-to-indirect-buffer (&optional arg)
  6578. "Create indirect buffer and narrow it to current subtree.
  6579. With a numerical prefix ARG, go up to this level and then take that tree.
  6580. If ARG is negative, go up that many levels.
  6581. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6582. indirect buffer previously made with this command, to avoid proliferation of
  6583. indirect buffers. However, when you call the command with a \
  6584. \\[universal-argument] prefix, or
  6585. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6586. is kept so that you can work with several indirect buffers at the same time.
  6587. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6588. \\[universal-argument] prefix also
  6589. requests that a new frame be made for the new buffer, so that the dedicated
  6590. frame is not changed."
  6591. (interactive "P")
  6592. (let ((cbuf (current-buffer))
  6593. (cwin (selected-window))
  6594. (pos (point))
  6595. beg end level heading ibuf)
  6596. (save-excursion
  6597. (org-back-to-heading t)
  6598. (when (numberp arg)
  6599. (setq level (org-outline-level))
  6600. (if (< arg 0) (setq arg (+ level arg)))
  6601. (while (> (setq level (org-outline-level)) arg)
  6602. (org-up-heading-safe)))
  6603. (setq beg (point)
  6604. heading (org-get-heading))
  6605. (org-end-of-subtree t t)
  6606. (if (org-at-heading-p) (backward-char 1))
  6607. (setq end (point)))
  6608. (if (and (buffer-live-p org-last-indirect-buffer)
  6609. (not (eq org-indirect-buffer-display 'new-frame))
  6610. (not arg))
  6611. (kill-buffer org-last-indirect-buffer))
  6612. (setq ibuf (org-get-indirect-buffer cbuf)
  6613. org-last-indirect-buffer ibuf)
  6614. (cond
  6615. ((or (eq org-indirect-buffer-display 'new-frame)
  6616. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6617. (select-frame (make-frame))
  6618. (delete-other-windows)
  6619. (org-pop-to-buffer-same-window ibuf)
  6620. (org-set-frame-title heading))
  6621. ((eq org-indirect-buffer-display 'dedicated-frame)
  6622. (raise-frame
  6623. (select-frame (or (and org-indirect-dedicated-frame
  6624. (frame-live-p org-indirect-dedicated-frame)
  6625. org-indirect-dedicated-frame)
  6626. (setq org-indirect-dedicated-frame (make-frame)))))
  6627. (delete-other-windows)
  6628. (org-pop-to-buffer-same-window ibuf)
  6629. (org-set-frame-title (concat "Indirect: " heading)))
  6630. ((eq org-indirect-buffer-display 'current-window)
  6631. (org-pop-to-buffer-same-window ibuf))
  6632. ((eq org-indirect-buffer-display 'other-window)
  6633. (pop-to-buffer ibuf))
  6634. (t (error "Invalid value")))
  6635. (if (featurep 'xemacs)
  6636. (save-excursion (org-mode) (turn-on-font-lock)))
  6637. (narrow-to-region beg end)
  6638. (show-all)
  6639. (goto-char pos)
  6640. (run-hook-with-args 'org-cycle-hook 'all)
  6641. (and (window-live-p cwin) (select-window cwin))))
  6642. (defun org-get-indirect-buffer (&optional buffer)
  6643. (setq buffer (or buffer (current-buffer)))
  6644. (let ((n 1) (base (buffer-name buffer)) bname)
  6645. (while (buffer-live-p
  6646. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6647. (setq n (1+ n)))
  6648. (condition-case nil
  6649. (make-indirect-buffer buffer bname 'clone)
  6650. (error (make-indirect-buffer buffer bname)))))
  6651. (defun org-set-frame-title (title)
  6652. "Set the title of the current frame to the string TITLE."
  6653. ;; FIXME: how to name a single frame in XEmacs???
  6654. (unless (featurep 'xemacs)
  6655. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6656. ;;;; Structure editing
  6657. ;;; Inserting headlines
  6658. (defun org-previous-line-empty-p (&optional next)
  6659. "Is the previous line a blank line?
  6660. When NEXT is non-nil, check the next line instead."
  6661. (save-excursion
  6662. (and (not (bobp))
  6663. (or (beginning-of-line (if next 2 0)) t)
  6664. (save-match-data
  6665. (looking-at "[ \t]*$")))))
  6666. (defun org-insert-heading (&optional arg invisible-ok)
  6667. "Insert a new heading or item with same depth at point.
  6668. If point is in a plain list and ARG is nil, create a new list item.
  6669. With one universal prefix argument, insert a heading even in lists.
  6670. With two universal prefix arguments, insert the heading at the end
  6671. of the parent subtree.
  6672. If point is at the beginning of a headline, insert a sibling before
  6673. the current headline. If point is not at the beginning, split the line
  6674. and create a new headline with the text in the current line after point
  6675. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6676. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6677. This is important for non-interactive uses of the command."
  6678. (interactive "P")
  6679. (cond
  6680. ((or (= (buffer-size) 0)
  6681. (and (not (save-excursion
  6682. (and (ignore-errors (org-back-to-heading invisible-ok))
  6683. (org-at-heading-p))))
  6684. (or arg (not (org-in-item-p)))))
  6685. (insert
  6686. (if (org-previous-line-empty-p) "" "\n")
  6687. (if (org-in-src-block-p) ",* " "* "))
  6688. (run-hooks 'org-insert-heading-hook))
  6689. ((or arg (not (org-insert-item
  6690. (save-excursion
  6691. (beginning-of-line)
  6692. (looking-at org-list-full-item-re)
  6693. (match-string 3)))))
  6694. (let (begn endn)
  6695. (when (org-buffer-narrowed-p)
  6696. (setq begn (point-min) endn (point-max))
  6697. (widen))
  6698. (let* ((empty-line-p nil)
  6699. (eops (equal arg '(16))) ; insert at end of parent subtree
  6700. (org-insert-heading-respect-content
  6701. (or (not (null arg)) org-insert-heading-respect-content))
  6702. (level nil)
  6703. (on-heading (org-at-heading-p))
  6704. (head (save-excursion
  6705. (condition-case nil
  6706. (progn
  6707. (org-back-to-heading invisible-ok)
  6708. (when (and (not on-heading)
  6709. (featurep 'org-inlinetask)
  6710. (integerp org-inlinetask-min-level)
  6711. (>= (length (match-string 0))
  6712. org-inlinetask-min-level))
  6713. ;; Find a heading level before the inline task
  6714. (while (and (setq level (org-up-heading-safe))
  6715. (>= level org-inlinetask-min-level)))
  6716. (if (org-at-heading-p)
  6717. (org-back-to-heading invisible-ok)
  6718. (error "This should not happen")))
  6719. (unless (and (save-excursion
  6720. (save-match-data
  6721. (org-backward-heading-same-level 1 invisible-ok))
  6722. (= (point) (match-beginning 0)))
  6723. (not (org-previous-line-empty-p t)))
  6724. (setq empty-line-p (org-previous-line-empty-p)))
  6725. (match-string 0))
  6726. (error "* "))))
  6727. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6728. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6729. pos hide-previous previous-pos)
  6730. (if ;; At the beginning of a heading, open a new line for insertiong
  6731. (and (bolp) (org-at-heading-p)
  6732. (not eops)
  6733. (or (bobp)
  6734. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6735. (open-line (if blank 2 1))
  6736. (save-excursion
  6737. (setq previous-pos (point-at-bol))
  6738. (end-of-line)
  6739. (setq hide-previous (outline-invisible-p)))
  6740. (and org-insert-heading-respect-content
  6741. (save-excursion
  6742. (while (outline-invisible-p)
  6743. (org-show-subtree)
  6744. (org-up-heading-safe))))
  6745. (let ((split
  6746. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6747. (save-excursion
  6748. (let ((p (point)))
  6749. (goto-char (point-at-bol))
  6750. (and (looking-at org-complex-heading-regexp)
  6751. (match-beginning 4)
  6752. (> p (match-beginning 4)))))))
  6753. tags pos)
  6754. (cond
  6755. ;; Insert a new line, possibly at end of parent subtree
  6756. (org-insert-heading-respect-content
  6757. (if (not eops)
  6758. (progn
  6759. (org-end-of-subtree nil t)
  6760. (and (looking-at "^\\*") (backward-char 1))
  6761. (while (and (not (bobp))
  6762. ;; Don't delete spaces in empty headlines
  6763. (not (looking-back org-outline-regexp))
  6764. (member (char-before) '(?\ ?\t ?\n)))
  6765. (backward-delete-char 1)))
  6766. (let ((p (point)))
  6767. (org-up-heading-safe)
  6768. (if (= p (point))
  6769. (goto-char (point-max))
  6770. (org-end-of-subtree nil t))))
  6771. (when (featurep 'org-inlinetask)
  6772. (while (and (not (eobp))
  6773. (looking-at "\\(\\*+\\)[ \t]+")
  6774. (>= (length (match-string 1))
  6775. org-inlinetask-min-level))
  6776. (org-end-of-subtree nil t)))
  6777. (or (bolp) (newline))
  6778. (or (org-previous-line-empty-p)
  6779. (and blank (newline)))
  6780. (if (or empty-line-p eops) (open-line 1)))
  6781. ;; Insert a headling containing text after point
  6782. ((org-at-heading-p)
  6783. (when hide-previous
  6784. (show-children)
  6785. (org-show-entry))
  6786. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6787. (setq tags (and (match-end 2) (match-string 2)))
  6788. (and (match-end 1)
  6789. (delete-region (match-beginning 1) (match-end 1)))
  6790. (setq pos (point-at-bol))
  6791. (or split (end-of-line 1))
  6792. (delete-horizontal-space)
  6793. (if (string-match "\\`\\*+\\'"
  6794. (buffer-substring (point-at-bol) (point)))
  6795. (insert " "))
  6796. (newline (if blank 2 1))
  6797. (when tags
  6798. (save-excursion
  6799. (goto-char pos)
  6800. (end-of-line 1)
  6801. (insert " " tags)
  6802. (org-set-tags nil 'align))))
  6803. (t
  6804. (or split (end-of-line 1))
  6805. (newline (if blank 2 1))))))
  6806. (insert head) (just-one-space)
  6807. (setq pos (point))
  6808. (end-of-line 1)
  6809. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6810. (when (and org-insert-heading-respect-content hide-previous)
  6811. (save-excursion
  6812. (goto-char previous-pos)
  6813. (hide-subtree)))
  6814. (when (and begn endn)
  6815. (narrow-to-region (min (point) begn) (max (point) endn)))
  6816. (run-hooks 'org-insert-heading-hook))))))
  6817. (defun org-get-heading (&optional no-tags no-todo)
  6818. "Return the heading of the current entry, without the stars.
  6819. When NO-TAGS is non-nil, don't include tags.
  6820. When NO-TODO is non-nil, don't include TODO keywords."
  6821. (save-excursion
  6822. (org-back-to-heading t)
  6823. (cond
  6824. ((and no-tags no-todo)
  6825. (looking-at org-complex-heading-regexp)
  6826. (match-string 4))
  6827. (no-tags
  6828. (looking-at (concat org-outline-regexp
  6829. "\\(.*?\\)"
  6830. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6831. (match-string 1))
  6832. (no-todo
  6833. (looking-at org-todo-line-regexp)
  6834. (match-string 3))
  6835. (t (looking-at org-heading-regexp)
  6836. (match-string 2)))))
  6837. (defvar orgstruct-mode) ; defined below
  6838. (defun org-heading-components ()
  6839. "Return the components of the current heading.
  6840. This is a list with the following elements:
  6841. - the level as an integer
  6842. - the reduced level, different if `org-odd-levels-only' is set.
  6843. - the TODO keyword, or nil
  6844. - the priority character, like ?A, or nil if no priority is given
  6845. - the headline text itself, or the tags string if no headline text
  6846. - the tags string, or nil."
  6847. (save-excursion
  6848. (org-back-to-heading t)
  6849. (if (let (case-fold-search)
  6850. (looking-at
  6851. (if orgstruct-mode
  6852. org-heading-regexp
  6853. org-complex-heading-regexp)))
  6854. (if orgstruct-mode
  6855. (list (length (match-string 1))
  6856. (org-reduced-level (length (match-string 1)))
  6857. nil
  6858. nil
  6859. (match-string 2)
  6860. nil)
  6861. (list (length (match-string 1))
  6862. (org-reduced-level (length (match-string 1)))
  6863. (org-match-string-no-properties 2)
  6864. (and (match-end 3) (aref (match-string 3) 2))
  6865. (org-match-string-no-properties 4)
  6866. (org-match-string-no-properties 5))))))
  6867. (defun org-get-entry ()
  6868. "Get the entry text, after heading, entire subtree."
  6869. (save-excursion
  6870. (org-back-to-heading t)
  6871. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6872. (defun org-insert-heading-after-current ()
  6873. "Insert a new heading with same level as current, after current subtree."
  6874. (interactive)
  6875. (org-back-to-heading)
  6876. (org-insert-heading)
  6877. (org-move-subtree-down)
  6878. (end-of-line 1))
  6879. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6880. "Insert heading with `org-insert-heading-respect-content' set to t."
  6881. (interactive "P")
  6882. (let ((org-insert-heading-respect-content t))
  6883. (org-insert-heading arg invisible-ok)))
  6884. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6885. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6886. (interactive "P")
  6887. (let ((org-insert-heading-respect-content t))
  6888. (org-insert-todo-heading force-state t)))
  6889. (defun org-insert-todo-heading (arg &optional force-heading)
  6890. "Insert a new heading with the same level and TODO state as current heading.
  6891. If the heading has no TODO state, or if the state is DONE, use the first
  6892. state (TODO by default). Also one prefix arg, force first state. With two
  6893. prefix args, force inserting at the end of the parent subtree."
  6894. (interactive "P")
  6895. (when (or force-heading (not (org-insert-item 'checkbox)))
  6896. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6897. force-heading))
  6898. (save-excursion
  6899. (org-back-to-heading)
  6900. (outline-previous-heading)
  6901. (looking-at org-todo-line-regexp))
  6902. (let*
  6903. ((new-mark-x
  6904. (if (or arg
  6905. (not (match-beginning 2))
  6906. (member (match-string 2) org-done-keywords))
  6907. (car org-todo-keywords-1)
  6908. (match-string 2)))
  6909. (new-mark
  6910. (or
  6911. (run-hook-with-args-until-success
  6912. 'org-todo-get-default-hook new-mark-x nil)
  6913. new-mark-x)))
  6914. (beginning-of-line 1)
  6915. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6916. (if org-treat-insert-todo-heading-as-state-change
  6917. (org-todo new-mark)
  6918. (insert new-mark " "))))
  6919. (when org-provide-todo-statistics
  6920. (org-update-parent-todo-statistics))))
  6921. (defun org-insert-subheading (arg)
  6922. "Insert a new subheading and demote it.
  6923. Works for outline headings and for plain lists alike."
  6924. (interactive "P")
  6925. (org-insert-heading arg)
  6926. (cond
  6927. ((org-at-heading-p) (org-do-demote))
  6928. ((org-at-item-p) (org-indent-item))))
  6929. (defun org-insert-todo-subheading (arg)
  6930. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6931. Works for outline headings and for plain lists alike."
  6932. (interactive "P")
  6933. (org-insert-todo-heading arg)
  6934. (cond
  6935. ((org-at-heading-p) (org-do-demote))
  6936. ((org-at-item-p) (org-indent-item))))
  6937. ;;; Promotion and Demotion
  6938. (defvar org-after-demote-entry-hook nil
  6939. "Hook run after an entry has been demoted.
  6940. The cursor will be at the beginning of the entry.
  6941. When a subtree is being demoted, the hook will be called for each node.")
  6942. (defvar org-after-promote-entry-hook nil
  6943. "Hook run after an entry has been promoted.
  6944. The cursor will be at the beginning of the entry.
  6945. When a subtree is being promoted, the hook will be called for each node.")
  6946. (defun org-promote-subtree ()
  6947. "Promote the entire subtree.
  6948. See also `org-promote'."
  6949. (interactive)
  6950. (save-excursion
  6951. (org-with-limited-levels (org-map-tree 'org-promote)))
  6952. (org-fix-position-after-promote))
  6953. (defun org-demote-subtree ()
  6954. "Demote the entire subtree. See `org-demote'.
  6955. See also `org-promote'."
  6956. (interactive)
  6957. (save-excursion
  6958. (org-with-limited-levels (org-map-tree 'org-demote)))
  6959. (org-fix-position-after-promote))
  6960. (defun org-do-promote ()
  6961. "Promote the current heading higher up the tree.
  6962. If the region is active in `transient-mark-mode', promote all headings
  6963. in the region."
  6964. (interactive)
  6965. (save-excursion
  6966. (if (org-region-active-p)
  6967. (org-map-region 'org-promote (region-beginning) (region-end))
  6968. (org-promote)))
  6969. (org-fix-position-after-promote))
  6970. (defun org-do-demote ()
  6971. "Demote the current heading lower down the tree.
  6972. If the region is active in `transient-mark-mode', demote all headings
  6973. in the region."
  6974. (interactive)
  6975. (save-excursion
  6976. (if (org-region-active-p)
  6977. (org-map-region 'org-demote (region-beginning) (region-end))
  6978. (org-demote)))
  6979. (org-fix-position-after-promote))
  6980. (defun org-fix-position-after-promote ()
  6981. "Make sure that after pro/demotion cursor position is right."
  6982. (let ((pos (point)))
  6983. (when (save-excursion
  6984. (beginning-of-line 1)
  6985. (looking-at org-todo-line-regexp)
  6986. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6987. (cond ((eobp) (insert " "))
  6988. ((eolp) (insert " "))
  6989. ((equal (char-after) ?\ ) (forward-char 1))))))
  6990. (defun org-current-level ()
  6991. "Return the level of the current entry, or nil if before the first headline.
  6992. The level is the number of stars at the beginning of the headline."
  6993. (save-excursion
  6994. (org-with-limited-levels
  6995. (if (ignore-errors (org-back-to-heading t))
  6996. (funcall outline-level)))))
  6997. (defun org-get-previous-line-level ()
  6998. "Return the outline depth of the last headline before the current line.
  6999. Returns 0 for the first headline in the buffer, and nil if before the
  7000. first headline."
  7001. (let ((current-level (org-current-level))
  7002. (prev-level (when (> (line-number-at-pos) 1)
  7003. (save-excursion
  7004. (beginning-of-line 0)
  7005. (org-current-level)))))
  7006. (cond ((null current-level) nil) ; Before first headline
  7007. ((null prev-level) 0) ; At first headline
  7008. (prev-level))))
  7009. (defun org-reduced-level (l)
  7010. "Compute the effective level of a heading.
  7011. This takes into account the setting of `org-odd-levels-only'."
  7012. (cond
  7013. ((zerop l) 0)
  7014. (org-odd-levels-only (1+ (floor (/ l 2))))
  7015. (t l)))
  7016. (defun org-level-increment ()
  7017. "Return the number of stars that will be added or removed at a
  7018. time to headlines when structure editing, based on the value of
  7019. `org-odd-levels-only'."
  7020. (if org-odd-levels-only 2 1))
  7021. (defun org-get-valid-level (level &optional change)
  7022. "Rectify a level change under the influence of `org-odd-levels-only'
  7023. LEVEL is a current level, CHANGE is by how much the level should be
  7024. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7025. even level numbers will become the next higher odd number."
  7026. (if org-odd-levels-only
  7027. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7028. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7029. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7030. (max 1 (+ level (or change 0)))))
  7031. (if (boundp 'define-obsolete-function-alias)
  7032. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7033. (define-obsolete-function-alias 'org-get-legal-level
  7034. 'org-get-valid-level)
  7035. (define-obsolete-function-alias 'org-get-legal-level
  7036. 'org-get-valid-level "23.1")))
  7037. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7038. ;; ̀org-with-limited-levels'
  7039. (defun org-promote ()
  7040. "Promote the current heading higher up the tree.
  7041. If the region is active in `transient-mark-mode', promote all headings
  7042. in the region."
  7043. (org-back-to-heading t)
  7044. (let* ((level (save-match-data (funcall outline-level)))
  7045. (after-change-functions (remove 'flyspell-after-change-function
  7046. after-change-functions))
  7047. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7048. (diff (abs (- level (length up-head) -1))))
  7049. (cond ((and (= level 1) org-called-with-limited-levels
  7050. org-allow-promoting-top-level-subtree)
  7051. (replace-match "# " nil t))
  7052. ((= level 1)
  7053. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  7054. (t (replace-match up-head nil t)))
  7055. ;; Fixup tag positioning
  7056. (unless (= level 1)
  7057. (and org-auto-align-tags (org-set-tags nil t))
  7058. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7059. (run-hooks 'org-after-promote-entry-hook)))
  7060. (defun org-demote ()
  7061. "Demote the current heading lower down the tree.
  7062. If the region is active in `transient-mark-mode', demote all headings
  7063. in the region."
  7064. (org-back-to-heading t)
  7065. (let* ((level (save-match-data (funcall outline-level)))
  7066. (after-change-functions (remove 'flyspell-after-change-function
  7067. after-change-functions))
  7068. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7069. (diff (abs (- level (length down-head) -1))))
  7070. (replace-match down-head nil t)
  7071. ;; Fixup tag positioning
  7072. (and org-auto-align-tags (org-set-tags nil t))
  7073. (if org-adapt-indentation (org-fixup-indentation diff))
  7074. (run-hooks 'org-after-demote-entry-hook)))
  7075. (defun org-cycle-level ()
  7076. "Cycle the level of an empty headline through possible states.
  7077. This goes first to child, then to parent, level, then up the hierarchy.
  7078. After top level, it switches back to sibling level."
  7079. (interactive)
  7080. (let ((org-adapt-indentation nil))
  7081. (when (org-point-at-end-of-empty-headline)
  7082. (setq this-command 'org-cycle-level) ; Only needed for caching
  7083. (let ((cur-level (org-current-level))
  7084. (prev-level (org-get-previous-line-level)))
  7085. (cond
  7086. ;; If first headline in file, promote to top-level.
  7087. ((= prev-level 0)
  7088. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7089. do (org-do-promote)))
  7090. ;; If same level as prev, demote one.
  7091. ((= prev-level cur-level)
  7092. (org-do-demote))
  7093. ;; If parent is top-level, promote to top level if not already.
  7094. ((= prev-level 1)
  7095. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7096. do (org-do-promote)))
  7097. ;; If top-level, return to prev-level.
  7098. ((= cur-level 1)
  7099. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7100. do (org-do-demote)))
  7101. ;; If less than prev-level, promote one.
  7102. ((< cur-level prev-level)
  7103. (org-do-promote))
  7104. ;; If deeper than prev-level, promote until higher than
  7105. ;; prev-level.
  7106. ((> cur-level prev-level)
  7107. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7108. do (org-do-promote))))
  7109. t))))
  7110. (defun org-map-tree (fun)
  7111. "Call FUN for every heading underneath the current one."
  7112. (org-back-to-heading)
  7113. (let ((level (funcall outline-level)))
  7114. (save-excursion
  7115. (funcall fun)
  7116. (while (and (progn
  7117. (outline-next-heading)
  7118. (> (funcall outline-level) level))
  7119. (not (eobp)))
  7120. (funcall fun)))))
  7121. (defun org-map-region (fun beg end)
  7122. "Call FUN for every heading between BEG and END."
  7123. (let ((org-ignore-region t))
  7124. (save-excursion
  7125. (setq end (copy-marker end))
  7126. (goto-char beg)
  7127. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7128. (< (point) end))
  7129. (funcall fun))
  7130. (while (and (progn
  7131. (outline-next-heading)
  7132. (< (point) end))
  7133. (not (eobp)))
  7134. (funcall fun)))))
  7135. (defvar org-property-end-re) ; silence byte-compiler
  7136. (defun org-fixup-indentation (diff)
  7137. "Change the indentation in the current entry by DIFF.
  7138. However, if any line in the current entry has no indentation, or if it
  7139. would end up with no indentation after the change, nothing at all is done."
  7140. (save-excursion
  7141. (let ((end (save-excursion (outline-next-heading)
  7142. (point-marker)))
  7143. (prohibit (if (> diff 0)
  7144. "^\\S-"
  7145. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7146. col)
  7147. (unless (save-excursion (end-of-line 1)
  7148. (re-search-forward prohibit end t))
  7149. (while (and (< (point) end)
  7150. (re-search-forward "^[ \t]+" end t))
  7151. (goto-char (match-end 0))
  7152. (setq col (current-column))
  7153. (if (< diff 0) (replace-match ""))
  7154. (org-indent-to-column (+ diff col))))
  7155. (move-marker end nil))))
  7156. (defun org-convert-to-odd-levels ()
  7157. "Convert an org-mode file with all levels allowed to one with odd levels.
  7158. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7159. level 5 etc."
  7160. (interactive)
  7161. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7162. (let ((outline-level 'org-outline-level)
  7163. (org-odd-levels-only nil) n)
  7164. (save-excursion
  7165. (goto-char (point-min))
  7166. (while (re-search-forward "^\\*\\*+ " nil t)
  7167. (setq n (- (length (match-string 0)) 2))
  7168. (while (>= (setq n (1- n)) 0)
  7169. (org-demote))
  7170. (end-of-line 1))))))
  7171. (defun org-convert-to-oddeven-levels ()
  7172. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7173. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7174. file contains a section with an even level, conversion would
  7175. destroy the structure of the file. An error is signaled in this
  7176. case."
  7177. (interactive)
  7178. (goto-char (point-min))
  7179. ;; First check if there are no even levels
  7180. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7181. (org-show-context t)
  7182. (error "Not all levels are odd in this file. Conversion not possible"))
  7183. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7184. (let ((outline-regexp org-outline-regexp)
  7185. (outline-level 'org-outline-level)
  7186. (org-odd-levels-only nil) n)
  7187. (save-excursion
  7188. (goto-char (point-min))
  7189. (while (re-search-forward "^\\*\\*+ " nil t)
  7190. (setq n (/ (1- (length (match-string 0))) 2))
  7191. (while (>= (setq n (1- n)) 0)
  7192. (org-promote))
  7193. (end-of-line 1))))))
  7194. (defun org-tr-level (n)
  7195. "Make N odd if required."
  7196. (if org-odd-levels-only (1+ (/ n 2)) n))
  7197. ;;; Vertical tree motion, cutting and pasting of subtrees
  7198. (defun org-move-subtree-up (&optional arg)
  7199. "Move the current subtree up past ARG headlines of the same level."
  7200. (interactive "p")
  7201. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7202. (defun org-move-subtree-down (&optional arg)
  7203. "Move the current subtree down past ARG headlines of the same level."
  7204. (interactive "p")
  7205. (setq arg (prefix-numeric-value arg))
  7206. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7207. 'org-get-last-sibling))
  7208. (ins-point (make-marker))
  7209. (cnt (abs arg))
  7210. (col (current-column))
  7211. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7212. ;; Select the tree
  7213. (org-back-to-heading)
  7214. (setq beg0 (point))
  7215. (save-excursion
  7216. (setq ne-beg (org-back-over-empty-lines))
  7217. (setq beg (point)))
  7218. (save-match-data
  7219. (save-excursion (outline-end-of-heading)
  7220. (setq folded (outline-invisible-p)))
  7221. (outline-end-of-subtree))
  7222. (outline-next-heading)
  7223. (setq ne-end (org-back-over-empty-lines))
  7224. (setq end (point))
  7225. (goto-char beg0)
  7226. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7227. ;; include less whitespace
  7228. (save-excursion
  7229. (goto-char beg)
  7230. (forward-line (- ne-beg ne-end))
  7231. (setq beg (point))))
  7232. ;; Find insertion point, with error handling
  7233. (while (> cnt 0)
  7234. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7235. (progn (goto-char beg0)
  7236. (user-error "Cannot move past superior level or buffer limit")))
  7237. (setq cnt (1- cnt)))
  7238. (if (> arg 0)
  7239. ;; Moving forward - still need to move over subtree
  7240. (progn (org-end-of-subtree t t)
  7241. (save-excursion
  7242. (org-back-over-empty-lines)
  7243. (or (bolp) (newline)))))
  7244. (setq ne-ins (org-back-over-empty-lines))
  7245. (move-marker ins-point (point))
  7246. (setq txt (buffer-substring beg end))
  7247. (org-save-markers-in-region beg end)
  7248. (delete-region beg end)
  7249. (org-remove-empty-overlays-at beg)
  7250. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7251. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7252. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7253. (let ((bbb (point)))
  7254. (insert-before-markers txt)
  7255. (org-reinstall-markers-in-region bbb)
  7256. (move-marker ins-point bbb))
  7257. (or (bolp) (insert "\n"))
  7258. (setq ins-end (point))
  7259. (goto-char ins-point)
  7260. (org-skip-whitespace)
  7261. (when (and (< arg 0)
  7262. (org-first-sibling-p)
  7263. (> ne-ins ne-beg))
  7264. ;; Move whitespace back to beginning
  7265. (save-excursion
  7266. (goto-char ins-end)
  7267. (let ((kill-whole-line t))
  7268. (kill-line (- ne-ins ne-beg)) (point)))
  7269. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7270. (move-marker ins-point nil)
  7271. (if folded
  7272. (hide-subtree)
  7273. (org-show-entry)
  7274. (show-children)
  7275. (org-cycle-hide-drawers 'children))
  7276. (org-clean-visibility-after-subtree-move)
  7277. ;; move back to the initial column we were at
  7278. (move-to-column col)))
  7279. (defvar org-subtree-clip ""
  7280. "Clipboard for cut and paste of subtrees.
  7281. This is actually only a copy of the kill, because we use the normal kill
  7282. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7283. (defvar org-subtree-clip-folded nil
  7284. "Was the last copied subtree folded?
  7285. This is used to fold the tree back after pasting.")
  7286. (defun org-cut-subtree (&optional n)
  7287. "Cut the current subtree into the clipboard.
  7288. With prefix arg N, cut this many sequential subtrees.
  7289. This is a short-hand for marking the subtree and then cutting it."
  7290. (interactive "p")
  7291. (org-copy-subtree n 'cut))
  7292. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7293. "Cut the current subtree into the clipboard.
  7294. With prefix arg N, cut this many sequential subtrees.
  7295. This is a short-hand for marking the subtree and then copying it.
  7296. If CUT is non-nil, actually cut the subtree.
  7297. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7298. of some markers in the region, even if CUT is non-nil. This is
  7299. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7300. (interactive "p")
  7301. (let (beg end folded (beg0 (point)))
  7302. (if (org-called-interactively-p 'any)
  7303. (org-back-to-heading nil) ; take what looks like a subtree
  7304. (org-back-to-heading t)) ; take what is really there
  7305. (setq beg (point))
  7306. (skip-chars-forward " \t\r\n")
  7307. (save-match-data
  7308. (if nosubtrees
  7309. (outline-next-heading)
  7310. (save-excursion (outline-end-of-heading)
  7311. (setq folded (outline-invisible-p)))
  7312. (condition-case nil
  7313. (org-forward-heading-same-level (1- n) t)
  7314. (error nil))
  7315. (org-end-of-subtree t t)))
  7316. (setq end (point))
  7317. (goto-char beg0)
  7318. (when (> end beg)
  7319. (setq org-subtree-clip-folded folded)
  7320. (when (or cut force-store-markers)
  7321. (org-save-markers-in-region beg end))
  7322. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7323. (setq org-subtree-clip (current-kill 0))
  7324. (message "%s: Subtree(s) with %d characters"
  7325. (if cut "Cut" "Copied")
  7326. (length org-subtree-clip)))))
  7327. (defun org-paste-subtree (&optional level tree for-yank)
  7328. "Paste the clipboard as a subtree, with modification of headline level.
  7329. The entire subtree is promoted or demoted in order to match a new headline
  7330. level.
  7331. If the cursor is at the beginning of a headline, the same level as
  7332. that headline is used to paste the tree.
  7333. If not, the new level is derived from the *visible* headings
  7334. before and after the insertion point, and taken to be the inferior headline
  7335. level of the two. So if the previous visible heading is level 3 and the
  7336. next is level 4 (or vice versa), level 4 will be used for insertion.
  7337. This makes sure that the subtree remains an independent subtree and does
  7338. not swallow low level entries.
  7339. You can also force a different level, either by using a numeric prefix
  7340. argument, or by inserting the heading marker by hand. For example, if the
  7341. cursor is after \"*****\", then the tree will be shifted to level 5.
  7342. If optional TREE is given, use this text instead of the kill ring.
  7343. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7344. move back over whitespace before inserting, and move point to the end of
  7345. the inserted text when done."
  7346. (interactive "P")
  7347. (setq tree (or tree (and kill-ring (current-kill 0))))
  7348. (unless (org-kill-is-subtree-p tree)
  7349. (error "%s"
  7350. (substitute-command-keys
  7351. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7352. (org-with-limited-levels
  7353. (let* ((visp (not (outline-invisible-p)))
  7354. (txt tree)
  7355. (^re_ "\\(\\*+\\)[ \t]*")
  7356. (old-level (if (string-match org-outline-regexp-bol txt)
  7357. (- (match-end 0) (match-beginning 0) 1)
  7358. -1))
  7359. (force-level (cond (level (prefix-numeric-value level))
  7360. ((and (looking-at "[ \t]*$")
  7361. (string-match
  7362. "^\\*+$" (buffer-substring
  7363. (point-at-bol) (point))))
  7364. (- (match-end 1) (match-beginning 1)))
  7365. ((and (bolp)
  7366. (looking-at org-outline-regexp))
  7367. (- (match-end 0) (point) 1))))
  7368. (previous-level (save-excursion
  7369. (condition-case nil
  7370. (progn
  7371. (outline-previous-visible-heading 1)
  7372. (if (looking-at ^re_)
  7373. (- (match-end 0) (match-beginning 0) 1)
  7374. 1))
  7375. (error 1))))
  7376. (next-level (save-excursion
  7377. (condition-case nil
  7378. (progn
  7379. (or (looking-at org-outline-regexp)
  7380. (outline-next-visible-heading 1))
  7381. (if (looking-at ^re_)
  7382. (- (match-end 0) (match-beginning 0) 1)
  7383. 1))
  7384. (error 1))))
  7385. (new-level (or force-level (max previous-level next-level)))
  7386. (shift (if (or (= old-level -1)
  7387. (= new-level -1)
  7388. (= old-level new-level))
  7389. 0
  7390. (- new-level old-level)))
  7391. (delta (if (> shift 0) -1 1))
  7392. (func (if (> shift 0) 'org-demote 'org-promote))
  7393. (org-odd-levels-only nil)
  7394. beg end newend)
  7395. ;; Remove the forced level indicator
  7396. (if force-level
  7397. (delete-region (point-at-bol) (point)))
  7398. ;; Paste
  7399. (beginning-of-line (if (bolp) 1 2))
  7400. (setq beg (point))
  7401. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7402. (insert-before-markers txt)
  7403. (unless (string-match "\n\\'" txt) (insert "\n"))
  7404. (setq newend (point))
  7405. (org-reinstall-markers-in-region beg)
  7406. (setq end (point))
  7407. (goto-char beg)
  7408. (skip-chars-forward " \t\n\r")
  7409. (setq beg (point))
  7410. (if (and (outline-invisible-p) visp)
  7411. (save-excursion (outline-show-heading)))
  7412. ;; Shift if necessary
  7413. (unless (= shift 0)
  7414. (save-restriction
  7415. (narrow-to-region beg end)
  7416. (while (not (= shift 0))
  7417. (org-map-region func (point-min) (point-max))
  7418. (setq shift (+ delta shift)))
  7419. (goto-char (point-min))
  7420. (setq newend (point-max))))
  7421. (when (or (org-called-interactively-p 'interactive) for-yank)
  7422. (message "Clipboard pasted as level %d subtree" new-level))
  7423. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7424. kill-ring
  7425. (eq org-subtree-clip (current-kill 0))
  7426. org-subtree-clip-folded)
  7427. ;; The tree was folded before it was killed/copied
  7428. (hide-subtree))
  7429. (and for-yank (goto-char newend)))))
  7430. (defun org-kill-is-subtree-p (&optional txt)
  7431. "Check if the current kill is an outline subtree, or a set of trees.
  7432. Returns nil if kill does not start with a headline, or if the first
  7433. headline level is not the largest headline level in the tree.
  7434. So this will actually accept several entries of equal levels as well,
  7435. which is OK for `org-paste-subtree'.
  7436. If optional TXT is given, check this string instead of the current kill."
  7437. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7438. (re (org-get-limited-outline-regexp))
  7439. (^re (concat "^" re))
  7440. (start-level (and kill
  7441. (string-match
  7442. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7443. kill)
  7444. (- (match-end 2) (match-beginning 2) 1)))
  7445. (start (1+ (or (match-beginning 2) -1))))
  7446. (if (not start-level)
  7447. (progn
  7448. nil) ;; does not even start with a heading
  7449. (catch 'exit
  7450. (while (setq start (string-match ^re kill (1+ start)))
  7451. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7452. (throw 'exit nil)))
  7453. t))))
  7454. (defvar org-markers-to-move nil
  7455. "Markers that should be moved with a cut-and-paste operation.
  7456. Those markers are stored together with their positions relative to
  7457. the start of the region.")
  7458. (defun org-save-markers-in-region (beg end)
  7459. "Check markers in region.
  7460. If these markers are between BEG and END, record their position relative
  7461. to BEG, so that after moving the block of text, we can put the markers back
  7462. into place.
  7463. This function gets called just before an entry or tree gets cut from the
  7464. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7465. called immediately, to move the markers with the entries."
  7466. (setq org-markers-to-move nil)
  7467. (when (featurep 'org-clock)
  7468. (org-clock-save-markers-for-cut-and-paste beg end))
  7469. (when (featurep 'org-agenda)
  7470. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7471. (defun org-check-and-save-marker (marker beg end)
  7472. "Check if MARKER is between BEG and END.
  7473. If yes, remember the marker and the distance to BEG."
  7474. (when (and (marker-buffer marker)
  7475. (equal (marker-buffer marker) (current-buffer)))
  7476. (if (and (>= marker beg) (< marker end))
  7477. (push (cons marker (- marker beg)) org-markers-to-move))))
  7478. (defun org-reinstall-markers-in-region (beg)
  7479. "Move all remembered markers to their position relative to BEG."
  7480. (mapc (lambda (x)
  7481. (move-marker (car x) (+ beg (cdr x))))
  7482. org-markers-to-move)
  7483. (setq org-markers-to-move nil))
  7484. (defun org-narrow-to-subtree ()
  7485. "Narrow buffer to the current subtree."
  7486. (interactive)
  7487. (save-excursion
  7488. (save-match-data
  7489. (org-with-limited-levels
  7490. (narrow-to-region
  7491. (progn (org-back-to-heading t) (point))
  7492. (progn (org-end-of-subtree t t)
  7493. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7494. (point)))))))
  7495. (defun org-narrow-to-block ()
  7496. "Narrow buffer to the current block."
  7497. (interactive)
  7498. (let* ((case-fold-search t)
  7499. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7500. "^[ \t]*#\\+end_.*")))
  7501. (if blockp
  7502. (narrow-to-region (car blockp) (cdr blockp))
  7503. (error "Not in a block"))))
  7504. (eval-when-compile
  7505. (defvar org-property-drawer-re))
  7506. (defvar org-property-start-re) ;; defined below
  7507. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7508. "Clone the task (subtree) at point N times.
  7509. The clones will be inserted as siblings.
  7510. In interactive use, the user will be prompted for the number of
  7511. clones to be produced. When called with a universal prefix argument
  7512. and if the entry has a timestamp, the user will also be prompted for
  7513. a time shift, which may be a repeater as used in time stamps, for
  7514. example `+3d'.
  7515. When a valid repeater is given and the entry contains any time
  7516. stamps, the clones will become a sequence in time, with time
  7517. stamps in the subtree shifted for each clone produced. If SHIFT
  7518. is nil or the empty string, time stamps will be left alone. The
  7519. ID property of the original subtree is removed.
  7520. If the original subtree did contain time stamps with a repeater,
  7521. the following will happen:
  7522. - the repeater will be removed in each clone
  7523. - an additional clone will be produced, with the current, unshifted
  7524. date(s) in the entry.
  7525. - the original entry will be placed *after* all the clones, with
  7526. repeater intact.
  7527. - the start days in the repeater in the original entry will be shifted
  7528. to past the last clone.
  7529. In this way you can spell out a number of instances of a repeating task,
  7530. and still retain the repeater to cover future instances of the task."
  7531. (interactive "nNumber of clones to produce: ")
  7532. (let ((shift
  7533. (or shift
  7534. (if (and (equal current-prefix-arg '(4))
  7535. (save-excursion
  7536. (re-search-forward org-ts-regexp-both
  7537. (save-excursion
  7538. (org-end-of-subtree t)
  7539. (point)) t)))
  7540. (read-from-minibuffer
  7541. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7542. ""))) ;; No time shift
  7543. (n-no-remove -1)
  7544. (drawer-re org-drawer-regexp)
  7545. beg end template task idprop
  7546. shift-n shift-what doshift nmin nmax)
  7547. (if (not (and (integerp n) (> n 0)))
  7548. (error "Invalid number of replications %s" n))
  7549. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7550. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7551. shift)))
  7552. (error "Invalid shift specification %s" shift))
  7553. (when doshift
  7554. (setq shift-n (string-to-number (match-string 1 shift))
  7555. shift-what (cdr (assoc (match-string 2 shift)
  7556. '(("d" . day) ("w" . week)
  7557. ("m" . month) ("y" . year))))))
  7558. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7559. (setq nmin 1 nmax n)
  7560. (org-back-to-heading t)
  7561. (setq beg (point))
  7562. (setq idprop (org-entry-get nil "ID"))
  7563. (org-end-of-subtree t t)
  7564. (or (bolp) (insert "\n"))
  7565. (setq end (point))
  7566. (setq template (buffer-substring beg end))
  7567. (when (and doshift
  7568. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7569. (delete-region beg end)
  7570. (setq end beg)
  7571. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7572. (goto-char end)
  7573. (loop for n from nmin to nmax do
  7574. ;; prepare clone
  7575. (with-temp-buffer
  7576. (insert template)
  7577. (org-mode)
  7578. (goto-char (point-min))
  7579. (org-show-subtree)
  7580. (and idprop (if org-clone-delete-id
  7581. (org-entry-delete nil "ID")
  7582. (org-id-get-create t)))
  7583. (unless (= n 0)
  7584. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7585. (kill-whole-line))
  7586. (goto-char (point-min))
  7587. (while (re-search-forward drawer-re nil t)
  7588. (mapc (lambda (d)
  7589. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7590. (goto-char (point-min))
  7591. (when doshift
  7592. (while (re-search-forward org-ts-regexp-both nil t)
  7593. (org-timestamp-change (* n shift-n) shift-what))
  7594. (unless (= n n-no-remove)
  7595. (goto-char (point-min))
  7596. (while (re-search-forward org-ts-regexp nil t)
  7597. (save-excursion
  7598. (goto-char (match-beginning 0))
  7599. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7600. (delete-region (match-beginning 1) (match-end 1)))))))
  7601. (setq task (buffer-string)))
  7602. (insert task))
  7603. (goto-char beg)))
  7604. ;;; Outline Sorting
  7605. (defun org-sort (with-case)
  7606. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7607. Optional argument WITH-CASE means sort case-sensitively."
  7608. (interactive "P")
  7609. (cond
  7610. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7611. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7612. (t
  7613. (org-call-with-arg 'org-sort-entries with-case))))
  7614. (defun org-sort-remove-invisible (s)
  7615. "Remove invisible links from string S."
  7616. (remove-text-properties 0 (length s) org-rm-props s)
  7617. (while (string-match org-bracket-link-regexp s)
  7618. (setq s (replace-match (if (match-end 2)
  7619. (match-string 3 s)
  7620. (match-string 1 s)) t t s)))
  7621. (let ((st (format " %s " s)))
  7622. (while (string-match org-emph-re st)
  7623. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7624. (setq s (substring st 1 -1)))
  7625. s)
  7626. (defvar org-priority-regexp) ; defined later in the file
  7627. (defvar org-after-sorting-entries-or-items-hook nil
  7628. "Hook that is run after a bunch of entries or items have been sorted.
  7629. When children are sorted, the cursor is in the parent line when this
  7630. hook gets called. When a region or a plain list is sorted, the cursor
  7631. will be in the first entry of the sorted region/list.")
  7632. (defun org-sort-entries
  7633. (&optional with-case sorting-type getkey-func compare-func property)
  7634. "Sort entries on a certain level of an outline tree.
  7635. If there is an active region, the entries in the region are sorted.
  7636. Else, if the cursor is before the first entry, sort the top-level items.
  7637. Else, the children of the entry at point are sorted.
  7638. Sorting can be alphabetically, numerically, by date/time as given by
  7639. a time stamp, by a property, by priority order, or by a custom function.
  7640. The command prompts for the sorting type unless it has been given to the
  7641. function through the SORTING-TYPE argument, which needs to be a character,
  7642. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7643. precise meaning of each character:
  7644. n Numerically, by converting the beginning of the entry/item to a number.
  7645. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7646. o By order of TODO keywords.
  7647. t By date/time, either the first active time stamp in the entry, or, if
  7648. none exist, by the first inactive one.
  7649. s By the scheduled date/time.
  7650. d By deadline date/time.
  7651. c By creation time, which is assumed to be the first inactive time stamp
  7652. at the beginning of a line.
  7653. p By priority according to the cookie.
  7654. r By the value of a property.
  7655. Capital letters will reverse the sort order.
  7656. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7657. called with point at the beginning of the record. It must return either
  7658. a string or a number that should serve as the sorting key for that record.
  7659. Comparing entries ignores case by default. However, with an optional argument
  7660. WITH-CASE, the sorting considers case as well.
  7661. Sorting is done against the visible part of the headlines, it ignores hidden
  7662. links."
  7663. (interactive "P")
  7664. (let ((case-func (if with-case 'identity 'downcase))
  7665. (cmstr
  7666. ;; The clock marker is lost when using `sort-subr', let's
  7667. ;; store the clocking string.
  7668. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7669. (save-excursion
  7670. (goto-char org-clock-marker)
  7671. (looking-back "^.*") (match-string-no-properties 0))))
  7672. start beg end stars re re2
  7673. txt what tmp)
  7674. ;; Find beginning and end of region to sort
  7675. (cond
  7676. ((org-region-active-p)
  7677. ;; we will sort the region
  7678. (setq end (region-end)
  7679. what "region")
  7680. (goto-char (region-beginning))
  7681. (if (not (org-at-heading-p)) (outline-next-heading))
  7682. (setq start (point)))
  7683. ((or (org-at-heading-p)
  7684. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7685. ;; we will sort the children of the current headline
  7686. (org-back-to-heading)
  7687. (setq start (point)
  7688. end (progn (org-end-of-subtree t t)
  7689. (or (bolp) (insert "\n"))
  7690. (org-back-over-empty-lines)
  7691. (point))
  7692. what "children")
  7693. (goto-char start)
  7694. (show-subtree)
  7695. (outline-next-heading))
  7696. (t
  7697. ;; we will sort the top-level entries in this file
  7698. (goto-char (point-min))
  7699. (or (org-at-heading-p) (outline-next-heading))
  7700. (setq start (point))
  7701. (goto-char (point-max))
  7702. (beginning-of-line 1)
  7703. (when (looking-at ".*?\\S-")
  7704. ;; File ends in a non-white line
  7705. (end-of-line 1)
  7706. (insert "\n"))
  7707. (setq end (point-max))
  7708. (setq what "top-level")
  7709. (goto-char start)
  7710. (show-all)))
  7711. (setq beg (point))
  7712. (if (>= beg end) (error "Nothing to sort"))
  7713. (looking-at "\\(\\*+\\)")
  7714. (setq stars (match-string 1)
  7715. re (concat "^" (regexp-quote stars) " +")
  7716. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7717. txt (buffer-substring beg end))
  7718. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7719. (if (and (not (equal stars "*")) (string-match re2 txt))
  7720. (error "Region to sort contains a level above the first entry"))
  7721. (unless sorting-type
  7722. (message
  7723. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7724. [t]ime [s]cheduled [d]eadline [c]reated
  7725. A/N/P/R/O/F/T/S/D/C means reversed:"
  7726. what)
  7727. (setq sorting-type (read-char-exclusive))
  7728. (unless getkey-func
  7729. (and (= (downcase sorting-type) ?f)
  7730. (setq getkey-func
  7731. (org-icompleting-read "Sort using function: "
  7732. obarray 'fboundp t nil nil))
  7733. (setq getkey-func (intern getkey-func))))
  7734. (and (= (downcase sorting-type) ?r)
  7735. (not property)
  7736. (setq property
  7737. (org-icompleting-read "Property: "
  7738. (mapcar 'list (org-buffer-property-keys t))
  7739. nil t))))
  7740. (message "Sorting entries...")
  7741. (save-restriction
  7742. (narrow-to-region start end)
  7743. (let ((dcst (downcase sorting-type))
  7744. (case-fold-search nil)
  7745. (now (current-time)))
  7746. (sort-subr
  7747. (/= dcst sorting-type)
  7748. ;; This function moves to the beginning character of the "record" to
  7749. ;; be sorted.
  7750. (lambda nil
  7751. (if (re-search-forward re nil t)
  7752. (goto-char (match-beginning 0))
  7753. (goto-char (point-max))))
  7754. ;; This function moves to the last character of the "record" being
  7755. ;; sorted.
  7756. (lambda nil
  7757. (save-match-data
  7758. (condition-case nil
  7759. (outline-forward-same-level 1)
  7760. (error
  7761. (goto-char (point-max))))))
  7762. ;; This function returns the value that gets sorted against.
  7763. (lambda nil
  7764. (cond
  7765. ((= dcst ?n)
  7766. (if (looking-at org-complex-heading-regexp)
  7767. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7768. nil))
  7769. ((= dcst ?a)
  7770. (if (looking-at org-complex-heading-regexp)
  7771. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7772. nil))
  7773. ((= dcst ?t)
  7774. (let ((end (save-excursion (outline-next-heading) (point))))
  7775. (if (or (re-search-forward org-ts-regexp end t)
  7776. (re-search-forward org-ts-regexp-both end t))
  7777. (org-time-string-to-seconds (match-string 0))
  7778. (org-float-time now))))
  7779. ((= dcst ?c)
  7780. (let ((end (save-excursion (outline-next-heading) (point))))
  7781. (if (re-search-forward
  7782. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7783. end t)
  7784. (org-time-string-to-seconds (match-string 0))
  7785. (org-float-time now))))
  7786. ((= dcst ?s)
  7787. (let ((end (save-excursion (outline-next-heading) (point))))
  7788. (if (re-search-forward org-scheduled-time-regexp end t)
  7789. (org-time-string-to-seconds (match-string 1))
  7790. (org-float-time now))))
  7791. ((= dcst ?d)
  7792. (let ((end (save-excursion (outline-next-heading) (point))))
  7793. (if (re-search-forward org-deadline-time-regexp end t)
  7794. (org-time-string-to-seconds (match-string 1))
  7795. (org-float-time now))))
  7796. ((= dcst ?p)
  7797. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7798. (string-to-char (match-string 2))
  7799. org-default-priority))
  7800. ((= dcst ?r)
  7801. (or (org-entry-get nil property) ""))
  7802. ((= dcst ?o)
  7803. (if (looking-at org-complex-heading-regexp)
  7804. (- 9999 (length (member (match-string 2)
  7805. org-todo-keywords-1)))))
  7806. ((= dcst ?f)
  7807. (if getkey-func
  7808. (progn
  7809. (setq tmp (funcall getkey-func))
  7810. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7811. tmp)
  7812. (error "Invalid key function `%s'" getkey-func)))
  7813. (t (error "Invalid sorting type `%c'" sorting-type))))
  7814. nil
  7815. (cond
  7816. ((= dcst ?a) 'string<)
  7817. ((= dcst ?f) compare-func)
  7818. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7819. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7820. ;; Reset the clock marker if needed
  7821. (when cmstr
  7822. (save-excursion
  7823. (goto-char start)
  7824. (search-forward cmstr nil t)
  7825. (move-marker org-clock-marker (point))))
  7826. (message "Sorting entries...done")))
  7827. (defun org-do-sort (table what &optional with-case sorting-type)
  7828. "Sort TABLE of WHAT according to SORTING-TYPE.
  7829. The user will be prompted for the SORTING-TYPE if the call to this
  7830. function does not specify it. WHAT is only for the prompt, to indicate
  7831. what is being sorted. The sorting key will be extracted from
  7832. the car of the elements of the table.
  7833. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7834. (unless sorting-type
  7835. (message
  7836. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7837. what)
  7838. (setq sorting-type (read-char-exclusive)))
  7839. (let ((dcst (downcase sorting-type))
  7840. extractfun comparefun)
  7841. ;; Define the appropriate functions
  7842. (cond
  7843. ((= dcst ?n)
  7844. (setq extractfun 'string-to-number
  7845. comparefun (if (= dcst sorting-type) '< '>)))
  7846. ((= dcst ?a)
  7847. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7848. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7849. comparefun (if (= dcst sorting-type)
  7850. 'string<
  7851. (lambda (a b) (and (not (string< a b))
  7852. (not (string= a b)))))))
  7853. ((= dcst ?t)
  7854. (setq extractfun
  7855. (lambda (x)
  7856. (if (or (string-match org-ts-regexp x)
  7857. (string-match org-ts-regexp-both x))
  7858. (org-float-time
  7859. (org-time-string-to-time (match-string 0 x)))
  7860. 0))
  7861. comparefun (if (= dcst sorting-type) '< '>)))
  7862. (t (error "Invalid sorting type `%c'" sorting-type)))
  7863. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7864. table)
  7865. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7866. ;;; The orgstruct minor mode
  7867. ;; Define a minor mode which can be used in other modes in order to
  7868. ;; integrate the org-mode structure editing commands.
  7869. ;; This is really a hack, because the org-mode structure commands use
  7870. ;; keys which normally belong to the major mode. Here is how it
  7871. ;; works: The minor mode defines all the keys necessary to operate the
  7872. ;; structure commands, but wraps the commands into a function which
  7873. ;; tests if the cursor is currently at a headline or a plain list
  7874. ;; item. If that is the case, the structure command is used,
  7875. ;; temporarily setting many Org-mode variables like regular
  7876. ;; expressions for filling etc. However, when any of those keys is
  7877. ;; used at a different location, function uses `key-binding' to look
  7878. ;; up if the key has an associated command in another currently active
  7879. ;; keymap (minor modes, major mode, global), and executes that
  7880. ;; command. There might be problems if any of the keys is otherwise
  7881. ;; used as a prefix key.
  7882. (defcustom orgstruct-heading-prefix-regexp nil
  7883. "Regexp that matches the custom prefix of Org headlines in
  7884. orgstruct(++)-mode."
  7885. :group 'org
  7886. :version "24.4"
  7887. :package-version '(Org . "8.0")
  7888. :type 'string)
  7889. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7890. (defcustom orgstruct-setup-hook nil
  7891. "Hook run after orgstruct-mode-map is filled."
  7892. :group 'org
  7893. :version "24.4"
  7894. :package-version '(Org . "8.0")
  7895. :type 'hook)
  7896. (defvar orgstruct-initialized nil)
  7897. (defvar org-local-vars nil
  7898. "List of local variables, for use by `orgstruct-mode'.")
  7899. ;;;###autoload
  7900. (define-minor-mode orgstruct-mode
  7901. "Toggle the minor mode `orgstruct-mode'.
  7902. This mode is for using Org-mode structure commands in other
  7903. modes. The following keys behave as if Org-mode were active, if
  7904. the cursor is on a headline, or on a plain list item (both as
  7905. defined by Org-mode)."
  7906. nil " OrgStruct" (make-sparse-keymap)
  7907. (funcall (if orgstruct-mode
  7908. 'add-to-invisibility-spec
  7909. 'remove-from-invisibility-spec)
  7910. '(outline . t))
  7911. (when orgstruct-mode
  7912. (org-load-modules-maybe)
  7913. (unless orgstruct-initialized
  7914. (orgstruct-setup)
  7915. (setq orgstruct-initialized t))))
  7916. ;;;###autoload
  7917. (defun turn-on-orgstruct ()
  7918. "Unconditionally turn on `orgstruct-mode'."
  7919. (orgstruct-mode 1))
  7920. (defvar org-fb-vars nil)
  7921. (make-variable-buffer-local 'org-fb-vars)
  7922. (defun orgstruct++-mode (&optional arg)
  7923. "Toggle `orgstruct-mode', the enhanced version of it.
  7924. In addition to setting orgstruct-mode, this also exports all
  7925. indentation and autofilling variables from org-mode into the
  7926. buffer. It will also recognize item context in multiline items."
  7927. (interactive "P")
  7928. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7929. (if (< arg 1)
  7930. (progn (orgstruct-mode -1)
  7931. (mapc (lambda(v)
  7932. (org-set-local (car v)
  7933. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7934. org-fb-vars))
  7935. (orgstruct-mode 1)
  7936. (setq org-fb-vars nil)
  7937. (let (var val)
  7938. (mapc
  7939. (lambda (x)
  7940. (when (string-match
  7941. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7942. (symbol-name (car x)))
  7943. (setq var (car x) val (nth 1 x))
  7944. (push (list var `(quote ,(eval var))) org-fb-vars)
  7945. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7946. org-local-vars)
  7947. (org-set-local 'orgstruct-is-++ t))))
  7948. (defvar orgstruct-is-++ nil
  7949. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7950. (make-variable-buffer-local 'orgstruct-is-++)
  7951. ;;;###autoload
  7952. (defun turn-on-orgstruct++ ()
  7953. "Unconditionally turn on `orgstruct++-mode'."
  7954. (orgstruct++-mode 1))
  7955. (defun orgstruct-error ()
  7956. "Error when there is no default binding for a structure key."
  7957. (interactive)
  7958. (funcall (if (fboundp 'user-error)
  7959. 'user-error
  7960. 'error)
  7961. "This key has no function outside structure elements"))
  7962. (defun orgstruct-setup ()
  7963. "Setup orgstruct keymap."
  7964. (dolist (cell '((org-demote . t)
  7965. (org-metaleft . t)
  7966. (org-metaright . t)
  7967. (org-promote . t)
  7968. (org-shiftmetaleft . t)
  7969. (org-shiftmetaright . t)
  7970. org-backward-element
  7971. org-backward-heading-same-level
  7972. org-ctrl-c-ret
  7973. org-ctrl-c-minus
  7974. org-ctrl-c-star
  7975. org-cycle
  7976. org-forward-heading-same-level
  7977. org-insert-heading
  7978. org-insert-heading-respect-content
  7979. org-kill-note-or-show-branches
  7980. org-mark-subtree
  7981. org-meta-return
  7982. org-metadown
  7983. org-metaup
  7984. org-narrow-to-subtree
  7985. org-promote-subtree
  7986. org-reveal
  7987. org-shiftdown
  7988. org-shiftleft
  7989. org-shiftmetadown
  7990. org-shiftmetaup
  7991. org-shiftright
  7992. org-shifttab
  7993. org-shifttab
  7994. org-shiftup
  7995. org-show-subtree
  7996. org-sort
  7997. org-up-element
  7998. outline-demote
  7999. outline-next-visible-heading
  8000. outline-previous-visible-heading
  8001. outline-promote
  8002. outline-up-heading
  8003. show-children))
  8004. (let ((f (or (car-safe cell) cell))
  8005. (disable-when-heading-prefix (cdr-safe cell)))
  8006. (when (fboundp f)
  8007. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8008. (where-is-internal f outline-mode-map)))
  8009. ;; TODO use local-function-key-map
  8010. (dolist (rep '(("<tab>" . "TAB")
  8011. ("<return>" . "RET")
  8012. ("<escape>" . "ESC")
  8013. ("<delete>" . "DEL")))
  8014. (setq binding (read-kbd-macro
  8015. (let ((case-fold-search))
  8016. (replace-regexp-in-string
  8017. (regexp-quote (cdr rep))
  8018. (car rep)
  8019. (key-description binding))))))
  8020. (let ((key (lookup-key orgstruct-mode-map binding)))
  8021. (when (or (not key) (numberp key))
  8022. (condition-case nil
  8023. (org-defkey orgstruct-mode-map
  8024. binding
  8025. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8026. (error nil))))))))
  8027. (run-hooks 'orgstruct-setup-hook))
  8028. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8029. "Create a function for binding in the structure minor mode.
  8030. FUN is the command to call inside a table. KEY is the key that
  8031. should be checked in for a command to execute outside of tables.
  8032. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8033. if `orgstruct-heading-prefix-regexp' is non-nil."
  8034. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8035. (let ((nname name)
  8036. (i 0))
  8037. (while (fboundp (intern nname))
  8038. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8039. (setq name (intern nname)))
  8040. (eval
  8041. (let ((bindings '((org-heading-regexp
  8042. (concat "^"
  8043. orgstruct-heading-prefix-regexp
  8044. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8045. (org-outline-regexp
  8046. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8047. (org-outline-regexp-bol
  8048. (concat "^" org-outline-regexp))
  8049. (outline-regexp org-outline-regexp)
  8050. (outline-heading-end-regexp "\n")
  8051. (outline-level 'org-outline-level)
  8052. (outline-heading-alist))))
  8053. `(defun ,name (arg)
  8054. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8055. "Outside of structure, run the binding of `"
  8056. (key-description key) "'."
  8057. (when disable-when-heading-prefix
  8058. (concat
  8059. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8060. "back to the default binding due to limitations of Org's implementation of\n"
  8061. "`" (symbol-name fun) "'.")))
  8062. (interactive "p")
  8063. (let* ((disable
  8064. ,(when disable-when-heading-prefix
  8065. '(and orgstruct-heading-prefix-regexp
  8066. (not (string= orgstruct-heading-prefix-regexp "")))))
  8067. (fallback
  8068. (or disable
  8069. (not
  8070. (let* ,bindings
  8071. (org-context-p 'headline 'item
  8072. ,(when (memq fun '(org-insert-heading))
  8073. '(when orgstruct-is-++
  8074. 'item-body))))))))
  8075. (if fallback
  8076. (let* ((orgstruct-mode)
  8077. (binding
  8078. (loop with key = ,key
  8079. for rep in
  8080. '(nil
  8081. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8082. ("<\\([^>]*\\)return>" . "\\1RET")
  8083. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8084. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8085. do
  8086. (when rep
  8087. (setq key (read-kbd-macro
  8088. (let ((case-fold-search))
  8089. (replace-regexp-in-string
  8090. (car rep)
  8091. (cdr rep)
  8092. (key-description key))))))
  8093. thereis (key-binding key))))
  8094. (if (keymapp binding)
  8095. (set-temporary-overlay-map binding)
  8096. (let ((func (or binding
  8097. (unless disable
  8098. 'orgstruct-error))))
  8099. (when func
  8100. (call-interactively func)))))
  8101. (org-run-like-in-org-mode
  8102. (lambda ()
  8103. (interactive)
  8104. (let* ,bindings
  8105. (call-interactively ',fun)))))))))
  8106. name))
  8107. (defun org-contextualize-keys (alist contexts)
  8108. "Return valid elements in ALIST depending on CONTEXTS.
  8109. `org-agenda-custom-commands' or `org-capture-templates' are the
  8110. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8111. or `org-capture-templates-contexts' are the associated contexts
  8112. definitions."
  8113. (let ((contexts
  8114. ;; normalize contexts
  8115. (mapcar
  8116. (lambda(c) (cond ((listp (cadr c))
  8117. (list (car c) (car c) (cadr c)))
  8118. ((string= "" (cadr c))
  8119. (list (car c) (car c) (caddr c)))
  8120. (t c))) contexts))
  8121. (a alist) c r s)
  8122. ;; loop over all commands or templates
  8123. (while (setq c (pop a))
  8124. (let (vrules repl)
  8125. (cond
  8126. ((not (assoc (car c) contexts))
  8127. (push c r))
  8128. ((and (assoc (car c) contexts)
  8129. (setq vrules (org-contextualize-validate-key
  8130. (car c) contexts)))
  8131. (mapc (lambda (vr)
  8132. (when (not (equal (car vr) (cadr vr)))
  8133. (setq repl vr))) vrules)
  8134. (if (not repl) (push c r)
  8135. (push (cadr repl) s)
  8136. (push
  8137. (cons (car c)
  8138. (cdr (or (assoc (cadr repl) alist)
  8139. (error "Undefined key `%s' as contextual replacement for `%s'"
  8140. (cadr repl) (car c)))))
  8141. r))))))
  8142. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8143. (delq
  8144. nil
  8145. (delete-dups
  8146. (mapcar (lambda (x)
  8147. (let ((tpl (car x)))
  8148. (when (not (delq
  8149. nil
  8150. (mapcar (lambda(y)
  8151. (equal y tpl)) s))) x)))
  8152. (reverse r))))))
  8153. (defun org-contextualize-validate-key (key contexts)
  8154. "Check CONTEXTS for agenda or capture KEY."
  8155. (let (r rr res)
  8156. (while (setq r (pop contexts))
  8157. (mapc
  8158. (lambda (rr)
  8159. (when
  8160. (and (equal key (car r))
  8161. (if (functionp rr) (funcall rr)
  8162. (or (and (eq (car rr) 'in-file)
  8163. (buffer-file-name)
  8164. (string-match (cdr rr) (buffer-file-name)))
  8165. (and (eq (car rr) 'in-mode)
  8166. (string-match (cdr rr) (symbol-name major-mode)))
  8167. (and (eq (car rr) 'in-buffer)
  8168. (string-match (cdr rr) (buffer-name)))
  8169. (when (and (eq (car rr) 'not-in-file)
  8170. (buffer-file-name))
  8171. (not (string-match (cdr rr) (buffer-file-name))))
  8172. (when (eq (car rr) 'not-in-mode)
  8173. (not (string-match (cdr rr) (symbol-name major-mode))))
  8174. (when (eq (car rr) 'not-in-buffer)
  8175. (not (string-match (cdr rr) (buffer-name)))))))
  8176. (push r res)))
  8177. (car (last r))))
  8178. (delete-dups (delq nil res))))
  8179. (defun org-context-p (&rest contexts)
  8180. "Check if local context is any of CONTEXTS.
  8181. Possible values in the list of contexts are `table', `headline', and `item'."
  8182. (let ((pos (point)))
  8183. (goto-char (point-at-bol))
  8184. (prog1 (or (and (memq 'table contexts)
  8185. (looking-at "[ \t]*|"))
  8186. (and (memq 'headline contexts)
  8187. (looking-at org-outline-regexp))
  8188. (and (memq 'item contexts)
  8189. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8190. (and (memq 'item-body contexts)
  8191. (org-in-item-p)))
  8192. (goto-char pos))))
  8193. (defun org-get-local-variables ()
  8194. "Return a list of all local variables in an Org mode buffer."
  8195. (let (varlist)
  8196. (with-current-buffer (get-buffer-create "*Org tmp*")
  8197. (erase-buffer)
  8198. (org-mode)
  8199. (setq varlist (buffer-local-variables)))
  8200. (kill-buffer "*Org tmp*")
  8201. (delq nil
  8202. (mapcar
  8203. (lambda (x)
  8204. (setq x
  8205. (if (symbolp x)
  8206. (list x)
  8207. (list (car x) (cdr x))))
  8208. (if (and (not (get (car x) 'org-state))
  8209. (string-match
  8210. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8211. (symbol-name (car x))))
  8212. x nil))
  8213. varlist))))
  8214. (defun org-clone-local-variables (from-buffer &optional regexp)
  8215. "Clone local variables from FROM-BUFFER.
  8216. Optional argument REGEXP selects variables to clone."
  8217. (mapc
  8218. (lambda (pair)
  8219. (and (symbolp (car pair))
  8220. (or (null regexp)
  8221. (string-match regexp (symbol-name (car pair))))
  8222. (set (make-local-variable (car pair))
  8223. (cdr pair))))
  8224. (buffer-local-variables from-buffer)))
  8225. ;;;###autoload
  8226. (defun org-run-like-in-org-mode (cmd)
  8227. "Run a command, pretending that the current buffer is in Org-mode.
  8228. This will temporarily bind local variables that are typically bound in
  8229. Org-mode to the values they have in Org-mode, and then interactively
  8230. call CMD."
  8231. (org-load-modules-maybe)
  8232. (unless org-local-vars
  8233. (setq org-local-vars (org-get-local-variables)))
  8234. (let (binds)
  8235. (dolist (var org-local-vars)
  8236. (when (or (not (boundp (car var)))
  8237. (eq (symbol-value (car var))
  8238. (default-value (car var))))
  8239. (push (list (car var) `(quote ,(cadr var))) binds)))
  8240. (eval `(let ,binds
  8241. (call-interactively (quote ,cmd))))))
  8242. ;;;; Archiving
  8243. (defun org-get-category (&optional pos force-refresh)
  8244. "Get the category applying to position POS."
  8245. (save-match-data
  8246. (if force-refresh (org-refresh-category-properties))
  8247. (let ((pos (or pos (point))))
  8248. (or (get-text-property pos 'org-category)
  8249. (progn (org-refresh-category-properties)
  8250. (get-text-property pos 'org-category))))))
  8251. (defun org-refresh-category-properties ()
  8252. "Refresh category text properties in the buffer."
  8253. (let ((case-fold-search t)
  8254. (inhibit-read-only t)
  8255. (def-cat (cond
  8256. ((null org-category)
  8257. (if buffer-file-name
  8258. (file-name-sans-extension
  8259. (file-name-nondirectory buffer-file-name))
  8260. "???"))
  8261. ((symbolp org-category) (symbol-name org-category))
  8262. (t org-category)))
  8263. beg end cat pos optionp)
  8264. (org-with-silent-modifications
  8265. (save-excursion
  8266. (save-restriction
  8267. (widen)
  8268. (goto-char (point-min))
  8269. (put-text-property (point) (point-max) 'org-category def-cat)
  8270. (while (re-search-forward
  8271. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8272. (setq pos (match-end 0)
  8273. optionp (equal (char-after (match-beginning 0)) ?#)
  8274. cat (org-trim (match-string 2)))
  8275. (if optionp
  8276. (setq beg (point-at-bol) end (point-max))
  8277. (org-back-to-heading t)
  8278. (setq beg (point) end (org-end-of-subtree t t)))
  8279. (put-text-property beg end 'org-category cat)
  8280. (put-text-property beg end 'org-category-position beg)
  8281. (goto-char pos)))))))
  8282. (defun org-refresh-properties (dprop tprop)
  8283. "Refresh buffer text properties.
  8284. DPROP is the drawer property and TPROP is the corresponding text
  8285. property to set."
  8286. (let ((case-fold-search t)
  8287. (inhibit-read-only t) p)
  8288. (org-with-silent-modifications
  8289. (save-excursion
  8290. (save-restriction
  8291. (widen)
  8292. (goto-char (point-min))
  8293. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8294. (setq p (org-match-string-no-properties 1))
  8295. (save-excursion
  8296. (org-back-to-heading t)
  8297. (put-text-property
  8298. (point-at-bol) (point-at-eol) tprop p))))))))
  8299. ;;;; Link Stuff
  8300. ;;; Link abbreviations
  8301. (defun org-link-expand-abbrev (link)
  8302. "Apply replacements as defined in `org-link-abbrev-alist'."
  8303. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8304. (let* ((key (match-string 1 link))
  8305. (as (or (assoc key org-link-abbrev-alist-local)
  8306. (assoc key org-link-abbrev-alist)))
  8307. (tag (and (match-end 2) (match-string 3 link)))
  8308. rpl)
  8309. (if (not as)
  8310. link
  8311. (setq rpl (cdr as))
  8312. (cond
  8313. ((symbolp rpl) (funcall rpl tag))
  8314. ((string-match "%(\\([^)]+\\))" rpl)
  8315. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8316. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8317. ((string-match "%h" rpl)
  8318. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8319. (t (concat rpl tag)))))
  8320. link))
  8321. ;;; Storing and inserting links
  8322. (defvar org-insert-link-history nil
  8323. "Minibuffer history for links inserted with `org-insert-link'.")
  8324. (defvar org-stored-links nil
  8325. "Contains the links stored with `org-store-link'.")
  8326. (defvar org-store-link-plist nil
  8327. "Plist with info about the most recently link created with `org-store-link'.")
  8328. (defvar org-link-protocols nil
  8329. "Link protocols added to Org-mode using `org-add-link-type'.")
  8330. (defvar org-store-link-functions nil
  8331. "List of functions that are called to create and store a link.
  8332. Each function will be called in turn until one returns a non-nil
  8333. value. Each function should check if it is responsible for creating
  8334. this link (for example by looking at the major mode).
  8335. If not, it must exit and return nil.
  8336. If yes, it should return a non-nil value after a calling
  8337. `org-store-link-props' with a list of properties and values.
  8338. Special properties are:
  8339. :type The link prefix, like \"http\". This must be given.
  8340. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8341. This is obligatory as well.
  8342. :description Optional default description for the second pair
  8343. of brackets in an Org-mode link. The user can still change
  8344. this when inserting this link into an Org-mode buffer.
  8345. In addition to these, any additional properties can be specified
  8346. and then used in capture templates.")
  8347. (defun org-add-link-type (type &optional follow export)
  8348. "Add TYPE to the list of `org-link-types'.
  8349. Re-compute all regular expressions depending on `org-link-types'
  8350. FOLLOW and EXPORT are two functions.
  8351. FOLLOW should take the link path as the single argument and do whatever
  8352. is necessary to follow the link, for example find a file or display
  8353. a mail message.
  8354. EXPORT should format the link path for export to one of the export formats.
  8355. It should be a function accepting three arguments:
  8356. path the path of the link, the text after the prefix (like \"http:\")
  8357. desc the description of the link, if any, or a description added by
  8358. org-export-normalize-links if there is none
  8359. format the export format, a symbol like `html' or `latex' or `ascii'..
  8360. The function may use the FORMAT information to return different values
  8361. depending on the format. The return value will be put literally into
  8362. the exported file. If the return value is nil, this means Org should
  8363. do what it normally does with links which do not have EXPORT defined.
  8364. Org-mode has a built-in default for exporting links. If you are happy with
  8365. this default, there is no need to define an export function for the link
  8366. type. For a simple example of an export function, see `org-bbdb.el'."
  8367. (add-to-list 'org-link-types type t)
  8368. (org-make-link-regexps)
  8369. (if (assoc type org-link-protocols)
  8370. (setcdr (assoc type org-link-protocols) (list follow export))
  8371. (push (list type follow export) org-link-protocols)))
  8372. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8373. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8374. ;;;###autoload
  8375. (defun org-store-link (arg)
  8376. "\\<org-mode-map>Store an org-link to the current location.
  8377. This link is added to `org-stored-links' and can later be inserted
  8378. into an org-buffer with \\[org-insert-link].
  8379. For some link types, a prefix arg is interpreted.
  8380. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8381. For file links, arg negates `org-context-in-file-links'.
  8382. A double prefix arg force skipping storing functions that are not
  8383. part of Org's core.
  8384. A triple prefix arg force storing a link for each line in the
  8385. active region."
  8386. (interactive "P")
  8387. (org-load-modules-maybe)
  8388. (if (and (equal arg '(64)) (org-region-active-p))
  8389. (save-excursion
  8390. (let ((end (region-end)))
  8391. (goto-char (region-beginning))
  8392. (set-mark (point))
  8393. (while (< (point-at-eol) end)
  8394. (move-end-of-line 1) (activate-mark)
  8395. (let (current-prefix-arg)
  8396. (call-interactively 'org-store-link))
  8397. (move-beginning-of-line 2)
  8398. (set-mark (point)))))
  8399. (org-with-limited-levels
  8400. (setq org-store-link-plist nil)
  8401. (let (link cpltxt desc description search
  8402. txt custom-id agenda-link sfuns sfunsn)
  8403. (cond
  8404. ;; Store a link using an external link type
  8405. ((and (not (equal arg '(16)))
  8406. (setq sfuns
  8407. (delq
  8408. nil (mapcar (lambda (f)
  8409. (let (fs) (if (funcall f) (push f fs))))
  8410. org-store-link-functions))
  8411. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8412. (or (and (cdr sfuns)
  8413. (funcall (intern
  8414. (completing-read
  8415. "Which function for creating the link? "
  8416. sfunsn t (car sfunsn)))))
  8417. (funcall (caar sfuns)))
  8418. (setq link (plist-get org-store-link-plist :link)
  8419. desc (or (plist-get org-store-link-plist
  8420. :description) link))))
  8421. ;; Store a link from a source code buffer
  8422. ((org-src-edit-buffer-p)
  8423. (let (label gc)
  8424. (while (or (not label)
  8425. (save-excursion
  8426. (save-restriction
  8427. (widen)
  8428. (goto-char (point-min))
  8429. (re-search-forward
  8430. (regexp-quote (format org-coderef-label-format label))
  8431. nil t))))
  8432. (when label (message "Label exists already") (sit-for 2))
  8433. (setq label (read-string "Code line label: " label)))
  8434. (end-of-line 1)
  8435. (setq link (format org-coderef-label-format label))
  8436. (setq gc (- 79 (length link)))
  8437. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8438. (insert link)
  8439. (setq link (concat "(" label ")") desc nil)))
  8440. ;; We are in the agenda, link to referenced location
  8441. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8442. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8443. (get-text-property (point) 'org-marker))))
  8444. (when m
  8445. (org-with-point-at m
  8446. (setq agenda-link
  8447. (if (org-called-interactively-p 'any)
  8448. (call-interactively 'org-store-link)
  8449. (org-store-link nil)))))))
  8450. ((eq major-mode 'calendar-mode)
  8451. (let ((cd (calendar-cursor-to-date)))
  8452. (setq link
  8453. (format-time-string
  8454. (car org-time-stamp-formats)
  8455. (apply 'encode-time
  8456. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8457. nil nil nil))))
  8458. (org-store-link-props :type "calendar" :date cd)))
  8459. ((eq major-mode 'help-mode)
  8460. (setq link (concat "help:" (save-excursion
  8461. (goto-char (point-min))
  8462. (looking-at "^[^ ]+")
  8463. (match-string 0))))
  8464. (org-store-link-props :type "help"))
  8465. ((eq major-mode 'w3-mode)
  8466. (setq cpltxt (if (and (buffer-name)
  8467. (not (string-match "Untitled" (buffer-name))))
  8468. (buffer-name)
  8469. (url-view-url t))
  8470. link (url-view-url t))
  8471. (org-store-link-props :type "w3" :url (url-view-url t)))
  8472. ((eq major-mode 'image-mode)
  8473. (setq cpltxt (concat "file:"
  8474. (abbreviate-file-name buffer-file-name))
  8475. link cpltxt)
  8476. (org-store-link-props :type "image" :file buffer-file-name))
  8477. ;; In dired, store a link to the file of the current line
  8478. ((eq major-mode 'dired-mode)
  8479. (let ((file (dired-get-filename nil t)))
  8480. (setq file (if file
  8481. (abbreviate-file-name
  8482. (expand-file-name (dired-get-filename nil t)))
  8483. ;; otherwise, no file so use current directory.
  8484. default-directory))
  8485. (setq cpltxt (concat "file:" file)
  8486. link cpltxt)))
  8487. ((setq search (run-hook-with-args-until-success
  8488. 'org-create-file-search-functions))
  8489. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8490. "::" search))
  8491. (setq cpltxt (or description link)))
  8492. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8493. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8494. (cond
  8495. ;; Store a link using the radio target at point
  8496. ((org-in-regexp "<<\\(.*?\\)>>")
  8497. (setq cpltxt
  8498. (concat "file:"
  8499. (abbreviate-file-name
  8500. (buffer-file-name (buffer-base-buffer)))
  8501. "::" (match-string 1))
  8502. link cpltxt))
  8503. ((and (featurep 'org-id)
  8504. (or (eq org-id-link-to-org-use-id t)
  8505. (and (org-called-interactively-p 'any)
  8506. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8507. (and (eq org-id-link-to-org-use-id
  8508. 'create-if-interactive-and-no-custom-id)
  8509. (not custom-id))))
  8510. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8511. ;; Store a link using the ID at point
  8512. (setq link (condition-case nil
  8513. (prog1 (org-id-store-link)
  8514. (setq desc (plist-get org-store-link-plist
  8515. :description)))
  8516. (error
  8517. ;; Probably before first headline, link only to file
  8518. (concat "file:"
  8519. (abbreviate-file-name
  8520. (buffer-file-name (buffer-base-buffer))))))))
  8521. (t
  8522. ;; Just link to current headline
  8523. (setq cpltxt (concat "file:"
  8524. (abbreviate-file-name
  8525. (buffer-file-name (buffer-base-buffer)))))
  8526. ;; Add a context search string
  8527. (when (org-xor org-context-in-file-links arg)
  8528. (let* ((ee (org-element-at-point))
  8529. (et (org-element-type ee))
  8530. (ev (plist-get (cadr ee) :value))
  8531. (ek (plist-get (cadr ee) :key))
  8532. (eok (and (stringp ek) (string-match "name\\|target" ek))))
  8533. (setq txt (cond
  8534. ((org-at-heading-p) nil)
  8535. ((and (eq et 'keyword) eok) ev)
  8536. ((org-region-active-p)
  8537. (buffer-substring (region-beginning) (region-end)))))
  8538. (when (or (null txt) (string-match "\\S-" txt))
  8539. (setq cpltxt
  8540. (concat cpltxt "::"
  8541. (condition-case nil
  8542. (org-make-org-heading-search-string txt)
  8543. (error "")))
  8544. desc (or (and (eq et 'keyword) eok ev)
  8545. (nth 4 (ignore-errors (org-heading-components)))
  8546. "NONE")))))
  8547. (if (string-match "::\\'" cpltxt)
  8548. (setq cpltxt (substring cpltxt 0 -2)))
  8549. (setq link cpltxt))))
  8550. ((buffer-file-name (buffer-base-buffer))
  8551. ;; Just link to this file here.
  8552. (setq cpltxt (concat "file:"
  8553. (abbreviate-file-name
  8554. (buffer-file-name (buffer-base-buffer)))))
  8555. ;; Add a context string.
  8556. (when (org-xor org-context-in-file-links arg)
  8557. (setq txt (if (org-region-active-p)
  8558. (buffer-substring (region-beginning) (region-end))
  8559. (buffer-substring (point-at-bol) (point-at-eol))))
  8560. ;; Only use search option if there is some text.
  8561. (when (string-match "\\S-" txt)
  8562. (setq cpltxt
  8563. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8564. desc "NONE")))
  8565. (setq link cpltxt))
  8566. ((org-called-interactively-p 'interactive)
  8567. (user-error "No method for storing a link from this buffer"))
  8568. (t (setq link nil)))
  8569. ;; We're done setting link and desc, clean up
  8570. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8571. (setq link (or link cpltxt)
  8572. desc (or desc cpltxt))
  8573. (cond ((equal desc "NONE") (setq desc nil))
  8574. ((string-match org-bracket-link-regexp desc)
  8575. (setq desc
  8576. (replace-regexp-in-string
  8577. org-bracket-link-regexp
  8578. (concat "\\3" (if (equal (length (match-string 0 desc))
  8579. (length desc)) "*" "")) desc))))
  8580. ;; Return the link
  8581. (if (not (and (or (org-called-interactively-p 'any)
  8582. executing-kbd-macro) link))
  8583. (or agenda-link (and link (org-make-link-string link desc)))
  8584. (push (list link desc) org-stored-links)
  8585. (message "Stored: %s" (or desc link))
  8586. (when custom-id
  8587. (setq link (concat "file:" (abbreviate-file-name
  8588. (buffer-file-name)) "::#" custom-id))
  8589. (push (list link desc) org-stored-links)))))))
  8590. (defun org-store-link-props (&rest plist)
  8591. "Store link properties, extract names and addresses."
  8592. (let (x adr)
  8593. (when (setq x (plist-get plist :from))
  8594. (setq adr (mail-extract-address-components x))
  8595. (setq plist (plist-put plist :fromname (car adr)))
  8596. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8597. (when (setq x (plist-get plist :to))
  8598. (setq adr (mail-extract-address-components x))
  8599. (setq plist (plist-put plist :toname (car adr)))
  8600. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8601. (let ((from (plist-get plist :from))
  8602. (to (plist-get plist :to)))
  8603. (when (and from to org-from-is-user-regexp)
  8604. (setq plist
  8605. (plist-put plist :fromto
  8606. (if (string-match org-from-is-user-regexp from)
  8607. (concat "to %t")
  8608. (concat "from %f"))))))
  8609. (setq org-store-link-plist plist))
  8610. (defun org-add-link-props (&rest plist)
  8611. "Add these properties to the link property list."
  8612. (let (key value)
  8613. (while plist
  8614. (setq key (pop plist) value (pop plist))
  8615. (setq org-store-link-plist
  8616. (plist-put org-store-link-plist key value)))))
  8617. (defun org-email-link-description (&optional fmt)
  8618. "Return the description part of an email link.
  8619. This takes information from `org-store-link-plist' and formats it
  8620. according to FMT (default from `org-email-link-description-format')."
  8621. (setq fmt (or fmt org-email-link-description-format))
  8622. (let* ((p org-store-link-plist)
  8623. (to (plist-get p :toaddress))
  8624. (from (plist-get p :fromaddress))
  8625. (table
  8626. (list
  8627. (cons "%c" (plist-get p :fromto))
  8628. (cons "%F" (plist-get p :from))
  8629. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8630. (cons "%T" (plist-get p :to))
  8631. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8632. (cons "%s" (plist-get p :subject))
  8633. (cons "%d" (plist-get p :date))
  8634. (cons "%m" (plist-get p :message-id)))))
  8635. (when (string-match "%c" fmt)
  8636. ;; Check if the user wrote this message
  8637. (if (and org-from-is-user-regexp from to
  8638. (save-match-data (string-match org-from-is-user-regexp from)))
  8639. (setq fmt (replace-match "to %t" t t fmt))
  8640. (setq fmt (replace-match "from %f" t t fmt))))
  8641. (org-replace-escapes fmt table)))
  8642. (defun org-make-org-heading-search-string (&optional string)
  8643. "Make search string for the current headline or STRING."
  8644. (let ((s (or string
  8645. (and (derived-mode-p 'org-mode)
  8646. (save-excursion
  8647. (org-back-to-heading t)
  8648. (org-element-property :raw-value (org-element-at-point))))))
  8649. (lines org-context-in-file-links))
  8650. (or string (setq s (concat "*" s))) ; Add * for headlines
  8651. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8652. (when (and string (integerp lines) (> lines 0))
  8653. (let ((slines (org-split-string s "\n")))
  8654. (when (< lines (length slines))
  8655. (setq s (mapconcat
  8656. 'identity
  8657. (reverse (nthcdr (- (length slines) lines)
  8658. (reverse slines))) "\n")))))
  8659. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8660. (defun org-make-link-string (link &optional description)
  8661. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8662. (unless (string-match "\\S-" link)
  8663. (error "Empty link"))
  8664. (when (and description
  8665. (stringp description)
  8666. (not (string-match "\\S-" description)))
  8667. (setq description nil))
  8668. (when (stringp description)
  8669. ;; Remove brackets from the description, they are fatal.
  8670. (while (string-match "\\[" description)
  8671. (setq description (replace-match "{" t t description)))
  8672. (while (string-match "\\]" description)
  8673. (setq description (replace-match "}" t t description))))
  8674. (when (equal link description)
  8675. ;; No description needed, it is identical
  8676. (setq description nil))
  8677. (when (and (not description)
  8678. (not (string-match (org-image-file-name-regexp) link))
  8679. (not (equal link (org-link-escape link))))
  8680. (setq description (org-extract-attributes link)))
  8681. (setq link
  8682. (cond ((string-match (org-image-file-name-regexp) link) link)
  8683. ((string-match org-link-types-re link)
  8684. (concat (match-string 1 link)
  8685. (org-link-escape (substring link (match-end 1)))))
  8686. (t (org-link-escape link))))
  8687. (concat "[[" link "]"
  8688. (if description (concat "[" description "]") "")
  8689. "]"))
  8690. (defconst org-link-escape-chars
  8691. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8692. "List of characters that should be escaped in link.
  8693. This is the list that is used for internal purposes.")
  8694. (defconst org-link-escape-chars-browser
  8695. '(?\ )
  8696. "List of escapes for characters that are problematic in links.
  8697. This is the list that is used before handing over to the browser.")
  8698. (defun org-link-escape (text &optional table merge)
  8699. "Return percent escaped representation of TEXT.
  8700. TEXT is a string with the text to escape.
  8701. Optional argument TABLE is a list with characters that should be
  8702. escaped. When nil, `org-link-escape-chars' is used.
  8703. If optional argument MERGE is set, merge TABLE into
  8704. `org-link-escape-chars'."
  8705. (cond
  8706. ((and table merge)
  8707. (mapc (lambda (defchr)
  8708. (unless (member defchr table)
  8709. (setq table (cons defchr table)))) org-link-escape-chars))
  8710. ((null table)
  8711. (setq table org-link-escape-chars)))
  8712. (mapconcat
  8713. (lambda (char)
  8714. (if (or (member char table)
  8715. (and (or (< char 32) (= char 37) (> char 126))
  8716. org-url-hexify-p))
  8717. (mapconcat (lambda (sequence-element)
  8718. (format "%%%.2X" sequence-element))
  8719. (or (encode-coding-char char 'utf-8)
  8720. (error "Unable to percent escape character: %s"
  8721. (char-to-string char))) "")
  8722. (char-to-string char))) text ""))
  8723. (defun org-link-unescape (str)
  8724. "Unhex hexified Unicode strings as returned from the JavaScript function
  8725. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8726. (unless (and (null str) (string= "" str))
  8727. (let ((pos 0) (case-fold-search t) unhexed)
  8728. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8729. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8730. (setq str (replace-match unhexed t t str))
  8731. (setq pos (+ pos (length unhexed))))))
  8732. str)
  8733. (defun org-link-unescape-compound (hex)
  8734. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8735. Note: this function also decodes single byte encodings like
  8736. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8737. (save-match-data
  8738. (let* ((bytes (cdr (split-string hex "%")))
  8739. (ret "")
  8740. (eat 0)
  8741. (sum 0))
  8742. (while bytes
  8743. (let* ((val (string-to-number (pop bytes) 16))
  8744. (shift-xor
  8745. (if (= 0 eat)
  8746. (cond
  8747. ((>= val 252) (cons 6 252))
  8748. ((>= val 248) (cons 5 248))
  8749. ((>= val 240) (cons 4 240))
  8750. ((>= val 224) (cons 3 224))
  8751. ((>= val 192) (cons 2 192))
  8752. (t (cons 0 0)))
  8753. (cons 6 128))))
  8754. (if (>= val 192) (setq eat (car shift-xor)))
  8755. (setq val (logxor val (cdr shift-xor)))
  8756. (setq sum (+ (lsh sum (car shift-xor)) val))
  8757. (if (> eat 0) (setq eat (- eat 1)))
  8758. (cond
  8759. ((= 0 eat) ;multi byte
  8760. (setq ret (concat ret (org-char-to-string sum)))
  8761. (setq sum 0))
  8762. ((not bytes) ; single byte(s)
  8763. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8764. )) ;; end (while bytes
  8765. ret )))
  8766. (defun org-link-unescape-single-byte-sequence (hex)
  8767. "Unhexify hex-encoded single byte character sequences."
  8768. (mapconcat (lambda (byte)
  8769. (char-to-string (string-to-number byte 16)))
  8770. (cdr (split-string hex "%")) ""))
  8771. (defun org-xor (a b)
  8772. "Exclusive or."
  8773. (if a (not b) b))
  8774. (defun org-fixup-message-id-for-http (s)
  8775. "Replace special characters in a message id, so it can be used in an http query."
  8776. (when (string-match "%" s)
  8777. (setq s (mapconcat (lambda (c)
  8778. (if (eq c ?%)
  8779. "%25"
  8780. (char-to-string c)))
  8781. s "")))
  8782. (while (string-match "<" s)
  8783. (setq s (replace-match "%3C" t t s)))
  8784. (while (string-match ">" s)
  8785. (setq s (replace-match "%3E" t t s)))
  8786. (while (string-match "@" s)
  8787. (setq s (replace-match "%40" t t s)))
  8788. s)
  8789. (defun org-link-prettify (link)
  8790. "Return a human-readable representation of LINK.
  8791. The car of LINK must be a raw link the cdr of LINK must be either
  8792. a link description or nil."
  8793. (let ((desc (or (cadr link) "<no description>")))
  8794. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8795. "<" (car link) ">")))
  8796. ;;;###autoload
  8797. (defun org-insert-link-global ()
  8798. "Insert a link like Org-mode does.
  8799. This command can be called in any mode to insert a link in Org-mode syntax."
  8800. (interactive)
  8801. (org-load-modules-maybe)
  8802. (org-run-like-in-org-mode 'org-insert-link))
  8803. (defun org-insert-all-links (&optional keep)
  8804. "Insert all links in `org-stored-links'."
  8805. (interactive "P")
  8806. (let ((links (copy-sequence org-stored-links)) l)
  8807. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8808. (insert "- ")
  8809. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8810. (insert "\n"))))
  8811. (defun org-link-fontify-links-to-this-file ()
  8812. "Fontify links to the current file in `org-stored-links'."
  8813. (let ((f (buffer-file-name)) a b)
  8814. (setq a (mapcar (lambda(l)
  8815. (let ((ll (car l)))
  8816. (when (and (string-match "^file:\\(.+\\)::" ll)
  8817. (equal f (expand-file-name (match-string 1 ll))))
  8818. ll)))
  8819. org-stored-links))
  8820. (when (featurep 'org-id)
  8821. (setq b (mapcar (lambda(l)
  8822. (let ((ll (car l)))
  8823. (when (and (string-match "^id:\\(.+\\)$" ll)
  8824. (equal f (expand-file-name
  8825. (or (org-id-find-id-file
  8826. (match-string 1 ll)) ""))))
  8827. ll)))
  8828. org-stored-links)))
  8829. (mapcar (lambda(l)
  8830. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8831. (delq nil (append a b)))))
  8832. (defvar org-link-links-in-this-file nil)
  8833. (defun org-insert-link (&optional complete-file link-location default-description)
  8834. "Insert a link. At the prompt, enter the link.
  8835. Completion can be used to insert any of the link protocol prefixes like
  8836. http or ftp in use.
  8837. The history can be used to select a link previously stored with
  8838. `org-store-link'. When the empty string is entered (i.e. if you just
  8839. press RET at the prompt), the link defaults to the most recently
  8840. stored link. As SPC triggers completion in the minibuffer, you need to
  8841. use M-SPC or C-q SPC to force the insertion of a space character.
  8842. You will also be prompted for a description, and if one is given, it will
  8843. be displayed in the buffer instead of the link.
  8844. If there is already a link at point, this command will allow you to edit link
  8845. and description parts.
  8846. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8847. be selected using completion. The path to the file will be relative to the
  8848. current directory if the file is in the current directory or a subdirectory.
  8849. Otherwise, the link will be the absolute path as completed in the minibuffer
  8850. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8851. option `org-link-file-path-type'.
  8852. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8853. the current directory or below.
  8854. With three \\[universal-argument] prefixes, negate the meaning of
  8855. `org-keep-stored-link-after-insertion'.
  8856. If `org-make-link-description-function' is non-nil, this function will be
  8857. called with the link target, and the result will be the default
  8858. link description.
  8859. If the LINK-LOCATION parameter is non-nil, this value will be
  8860. used as the link location instead of reading one interactively.
  8861. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8862. be used as the default description."
  8863. (interactive "P")
  8864. (let* ((wcf (current-window-configuration))
  8865. (origbuf (current-buffer))
  8866. (region (if (org-region-active-p)
  8867. (buffer-substring (region-beginning) (region-end))))
  8868. (remove (and region (list (region-beginning) (region-end))))
  8869. (desc region)
  8870. tmphist ; byte-compile incorrectly complains about this
  8871. (link link-location)
  8872. (abbrevs org-link-abbrev-alist-local)
  8873. entry file all-prefixes auto-desc)
  8874. (cond
  8875. (link-location) ; specified by arg, just use it.
  8876. ((org-in-regexp org-bracket-link-regexp 1)
  8877. ;; We do have a link at point, and we are going to edit it.
  8878. (setq remove (list (match-beginning 0) (match-end 0)))
  8879. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8880. (setq link (read-string "Link: "
  8881. (org-link-unescape
  8882. (org-match-string-no-properties 1)))))
  8883. ((or (org-in-regexp org-angle-link-re)
  8884. (org-in-regexp org-plain-link-re))
  8885. ;; Convert to bracket link
  8886. (setq remove (list (match-beginning 0) (match-end 0))
  8887. link (read-string "Link: "
  8888. (org-remove-angle-brackets (match-string 0)))))
  8889. ((member complete-file '((4) (16)))
  8890. ;; Completing read for file names.
  8891. (setq link (org-file-complete-link complete-file)))
  8892. (t
  8893. ;; Read link, with completion for stored links.
  8894. (org-link-fontify-links-to-this-file)
  8895. (org-switch-to-buffer-other-window "*Org Links*")
  8896. (with-current-buffer "*Org Links*"
  8897. (erase-buffer)
  8898. (insert "Insert a link.
  8899. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8900. (when org-stored-links
  8901. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8902. (insert (mapconcat 'org-link-prettify
  8903. (reverse org-stored-links) "\n")))
  8904. (goto-char (point-min)))
  8905. (let ((cw (selected-window)))
  8906. (select-window (get-buffer-window "*Org Links*" 'visible))
  8907. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8908. (unless (pos-visible-in-window-p (point-max))
  8909. (org-fit-window-to-buffer))
  8910. (and (window-live-p cw) (select-window cw)))
  8911. ;; Fake a link history, containing the stored links.
  8912. (setq tmphist (append (mapcar 'car org-stored-links)
  8913. org-insert-link-history))
  8914. (setq all-prefixes (append (mapcar 'car abbrevs)
  8915. (mapcar 'car org-link-abbrev-alist)
  8916. org-link-types))
  8917. (unwind-protect
  8918. (progn
  8919. (setq link
  8920. (org-completing-read
  8921. "Link: "
  8922. (append
  8923. (mapcar (lambda (x) (concat x ":"))
  8924. all-prefixes)
  8925. (mapcar 'car org-stored-links))
  8926. nil nil nil
  8927. 'tmphist
  8928. (caar org-stored-links)))
  8929. (if (not (string-match "\\S-" link))
  8930. (error "No link selected"))
  8931. (mapc (lambda(l)
  8932. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8933. org-stored-links)
  8934. (if (or (member link all-prefixes)
  8935. (and (equal ":" (substring link -1))
  8936. (member (substring link 0 -1) all-prefixes)
  8937. (setq link (substring link 0 -1))))
  8938. (setq link (with-current-buffer origbuf
  8939. (org-link-try-special-completion link)))))
  8940. (set-window-configuration wcf)
  8941. (kill-buffer "*Org Links*"))
  8942. (setq entry (assoc link org-stored-links))
  8943. (or entry (push link org-insert-link-history))
  8944. (setq desc (or desc (nth 1 entry)))))
  8945. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8946. (not org-keep-stored-link-after-insertion))
  8947. (setq org-stored-links (delq (assoc link org-stored-links)
  8948. org-stored-links)))
  8949. (if (string-match org-plain-link-re link)
  8950. ;; URL-like link, normalize the use of angular brackets.
  8951. (setq link (org-remove-angle-brackets link)))
  8952. ;; Check if we are linking to the current file with a search
  8953. ;; option If yes, simplify the link by using only the search
  8954. ;; option.
  8955. (when (and buffer-file-name
  8956. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8957. (let* ((path (match-string 1 link))
  8958. (case-fold-search nil)
  8959. (search (match-string 2 link)))
  8960. (save-match-data
  8961. (if (equal (file-truename buffer-file-name) (file-truename path))
  8962. ;; We are linking to this same file, with a search option
  8963. (setq link search)))))
  8964. ;; Check if we can/should use a relative path. If yes, simplify the link
  8965. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8966. (let* ((type (match-string 1 link))
  8967. (path (match-string 2 link))
  8968. (origpath path)
  8969. (case-fold-search nil))
  8970. (cond
  8971. ((or (eq org-link-file-path-type 'absolute)
  8972. (equal complete-file '(16)))
  8973. (setq path (abbreviate-file-name (expand-file-name path))))
  8974. ((eq org-link-file-path-type 'noabbrev)
  8975. (setq path (expand-file-name path)))
  8976. ((eq org-link-file-path-type 'relative)
  8977. (setq path (file-relative-name path)))
  8978. (t
  8979. (save-match-data
  8980. (if (string-match (concat "^" (regexp-quote
  8981. (expand-file-name
  8982. (file-name-as-directory
  8983. default-directory))))
  8984. (expand-file-name path))
  8985. ;; We are linking a file with relative path name.
  8986. (setq path (substring (expand-file-name path)
  8987. (match-end 0)))
  8988. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8989. (setq link (concat type path))
  8990. (if (equal desc origpath)
  8991. (setq desc path))))
  8992. (if org-make-link-description-function
  8993. (setq desc
  8994. (or (condition-case nil
  8995. (funcall org-make-link-description-function link desc)
  8996. (error (progn (message "Can't get link description from `%s'"
  8997. (symbol-name org-make-link-description-function))
  8998. (sit-for 2) nil)))
  8999. (read-string "Description: " default-description)))
  9000. (if default-description (setq desc default-description)
  9001. (setq desc (or (and auto-desc desc)
  9002. (read-string "Description: " desc)))))
  9003. (unless (string-match "\\S-" desc) (setq desc nil))
  9004. (if remove (apply 'delete-region remove))
  9005. (insert (org-make-link-string link desc))))
  9006. (defun org-link-try-special-completion (type)
  9007. "If there is completion support for link type TYPE, offer it."
  9008. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9009. (if (functionp fun)
  9010. (funcall fun)
  9011. (read-string "Link (no completion support): " (concat type ":")))))
  9012. (defun org-file-complete-link (&optional arg)
  9013. "Create a file link using completion."
  9014. (let (file link)
  9015. (setq file (org-iread-file-name "File: "))
  9016. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9017. (pwd1 (file-name-as-directory (abbreviate-file-name
  9018. (expand-file-name ".")))))
  9019. (cond
  9020. ((equal arg '(16))
  9021. (setq link (concat
  9022. "file:"
  9023. (abbreviate-file-name (expand-file-name file)))))
  9024. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9025. (setq link (concat "file:" (match-string 1 file))))
  9026. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9027. (expand-file-name file))
  9028. (setq link (concat
  9029. "file:" (match-string 1 (expand-file-name file)))))
  9030. (t (setq link (concat "file:" file)))))
  9031. link))
  9032. (defun org-iread-file-name (&rest args)
  9033. "Read-file-name using `ido-mode' speedup if available.
  9034. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9035. See `read-file-name' for a description of parameters."
  9036. (org-without-partial-completion
  9037. (if (and org-completion-use-ido
  9038. (fboundp 'ido-read-file-name)
  9039. (boundp 'ido-mode) ido-mode
  9040. (listp (second args)))
  9041. (let ((ido-enter-matching-directory nil))
  9042. (apply 'ido-read-file-name args))
  9043. (apply 'read-file-name args))))
  9044. (defun org-completing-read (&rest args)
  9045. "Completing-read with SPACE being a normal character."
  9046. (let ((enable-recursive-minibuffers t)
  9047. (minibuffer-local-completion-map
  9048. (copy-keymap minibuffer-local-completion-map)))
  9049. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9050. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9051. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9052. (apply 'org-icompleting-read args)))
  9053. (defun org-completing-read-no-i (&rest args)
  9054. (let (org-completion-use-ido org-completion-use-iswitchb)
  9055. (apply 'org-completing-read args)))
  9056. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9057. "Use iswitch as a completing-read replacement to choose from choices.
  9058. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9059. from."
  9060. (let* ((iswitchb-use-virtual-buffers nil)
  9061. (iswitchb-make-buflist-hook
  9062. (lambda ()
  9063. (setq iswitchb-temp-buflist choices))))
  9064. (iswitchb-read-buffer prompt)))
  9065. (defun org-icompleting-read (&rest args)
  9066. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9067. (org-without-partial-completion
  9068. (if (and org-completion-use-ido
  9069. (fboundp 'ido-completing-read)
  9070. (boundp 'ido-mode) ido-mode
  9071. (listp (second args)))
  9072. (let ((ido-enter-matching-directory nil))
  9073. (apply 'ido-completing-read (concat (car args))
  9074. (if (consp (car (nth 1 args)))
  9075. (mapcar 'car (nth 1 args))
  9076. (nth 1 args))
  9077. (cddr args)))
  9078. (if (and org-completion-use-iswitchb
  9079. (boundp 'iswitchb-mode) iswitchb-mode
  9080. (listp (second args)))
  9081. (apply 'org-iswitchb-completing-read (concat (car args))
  9082. (if (consp (car (nth 1 args)))
  9083. (mapcar 'car (nth 1 args))
  9084. (nth 1 args))
  9085. (cddr args))
  9086. (apply 'completing-read args)))))
  9087. (defun org-extract-attributes (s)
  9088. "Extract the attributes cookie from a string and set as text property."
  9089. (let (a attr (start 0) key value)
  9090. (save-match-data
  9091. (when (string-match "{{\\([^}]+\\)}}$" s)
  9092. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9093. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9094. (setq key (match-string 1 a) value (match-string 2 a)
  9095. start (match-end 0)
  9096. attr (plist-put attr (intern key) value))))
  9097. (org-add-props s nil 'org-attr attr))
  9098. s))
  9099. (defun org-extract-attributes-from-string (tag)
  9100. (let (key value attr)
  9101. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  9102. (setq key (match-string 1 tag) value (match-string 2 tag)
  9103. tag (replace-match "" t t tag)
  9104. attr (plist-put attr (intern key) value)))
  9105. (cons tag attr)))
  9106. (defun org-attributes-to-string (plist)
  9107. "Format a property list into an HTML attribute list."
  9108. (let ((s "") key value)
  9109. (while plist
  9110. (setq key (pop plist) value (pop plist))
  9111. (and value
  9112. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  9113. s))
  9114. ;;; Opening/following a link
  9115. (defvar org-link-search-failed nil)
  9116. (defvar org-open-link-functions nil
  9117. "Hook for functions finding a plain text link.
  9118. These functions must take a single argument, the link content.
  9119. They will be called for links that look like [[link text][description]]
  9120. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9121. like a filename (e.g. \"./blue.png\").
  9122. These functions will be called *before* Org attempts to resolve the
  9123. link by doing text searches in the current buffer - so if you want a
  9124. link \"[[target]]\" to still find \"<<target>>\", your function should
  9125. handle this as a special case.
  9126. When the function does handle the link, it must return a non-nil value.
  9127. If it decides that it is not responsible for this link, it must return
  9128. nil to indicate that that Org-mode can continue with other options
  9129. like exact and fuzzy text search.")
  9130. (defun org-next-link (&optional search-backward)
  9131. "Move forward to the next link.
  9132. If the link is in hidden text, expose it."
  9133. (interactive "P")
  9134. (when (and org-link-search-failed (eq this-command last-command))
  9135. (goto-char (point-min))
  9136. (message "Link search wrapped back to beginning of buffer"))
  9137. (setq org-link-search-failed nil)
  9138. (let* ((pos (point))
  9139. (ct (org-context))
  9140. (a (assoc :link ct))
  9141. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9142. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9143. ((looking-at org-any-link-re)
  9144. ;; Don't stay stuck at link without an org-link face
  9145. (forward-char (if search-backward -1 1))))
  9146. (if (funcall srch-fun org-any-link-re nil t)
  9147. (progn
  9148. (goto-char (match-beginning 0))
  9149. (if (outline-invisible-p) (org-show-context)))
  9150. (goto-char pos)
  9151. (setq org-link-search-failed t)
  9152. (message "No further link found"))))
  9153. (defun org-previous-link ()
  9154. "Move backward to the previous link.
  9155. If the link is in hidden text, expose it."
  9156. (interactive)
  9157. (funcall 'org-next-link t))
  9158. (defun org-translate-link (s)
  9159. "Translate a link string if a translation function has been defined."
  9160. (if (and org-link-translation-function
  9161. (fboundp org-link-translation-function)
  9162. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9163. (progn
  9164. (setq s (funcall org-link-translation-function
  9165. (match-string 1 s) (match-string 2 s)))
  9166. (concat (car s) ":" (cdr s)))
  9167. s))
  9168. (defun org-translate-link-from-planner (type path)
  9169. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9170. This is still an experimental function, your mileage may vary."
  9171. (cond
  9172. ((member type '("http" "https" "news" "ftp"))
  9173. ;; standard Internet links are the same.
  9174. nil)
  9175. ((and (equal type "irc") (string-match "^//" path))
  9176. ;; Planner has two / at the beginning of an irc link, we have 1.
  9177. ;; We should have zero, actually....
  9178. (setq path (substring path 1)))
  9179. ((and (equal type "lisp") (string-match "^/" path))
  9180. ;; Planner has a slash, we do not.
  9181. (setq type "elisp" path (substring path 1)))
  9182. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9183. ;; A typical message link. Planner has the id after the final slash,
  9184. ;; we separate it with a hash mark
  9185. (setq path (concat (match-string 1 path) "#"
  9186. (org-remove-angle-brackets (match-string 2 path)))))
  9187. )
  9188. (cons type path))
  9189. (defun org-find-file-at-mouse (ev)
  9190. "Open file link or URL at mouse."
  9191. (interactive "e")
  9192. (mouse-set-point ev)
  9193. (org-open-at-point 'in-emacs))
  9194. (defun org-open-at-mouse (ev)
  9195. "Open file link or URL at mouse.
  9196. See the docstring of `org-open-file' for details."
  9197. (interactive "e")
  9198. (mouse-set-point ev)
  9199. (if (eq major-mode 'org-agenda-mode)
  9200. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9201. (org-open-at-point))
  9202. (defvar org-window-config-before-follow-link nil
  9203. "The window configuration before following a link.
  9204. This is saved in case the need arises to restore it.")
  9205. (defvar org-open-link-marker (make-marker)
  9206. "Marker pointing to the location where `org-open-at-point; was called.")
  9207. ;;;###autoload
  9208. (defun org-open-at-point-global ()
  9209. "Follow a link like Org-mode does.
  9210. This command can be called in any mode to follow a link that has
  9211. Org-mode syntax."
  9212. (interactive)
  9213. (org-run-like-in-org-mode 'org-open-at-point))
  9214. ;;;###autoload
  9215. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9216. "Open a link in the string S, as if it was in Org-mode."
  9217. (interactive "sLink: \nP")
  9218. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9219. (with-temp-buffer
  9220. (let ((org-inhibit-startup (not reference-buffer)))
  9221. (org-mode)
  9222. (insert s)
  9223. (goto-char (point-min))
  9224. (when reference-buffer
  9225. (setq org-link-abbrev-alist-local
  9226. (with-current-buffer reference-buffer
  9227. org-link-abbrev-alist-local)))
  9228. (org-open-at-point arg reference-buffer)))))
  9229. (defvar org-open-at-point-functions nil
  9230. "Hook that is run when following a link at point.
  9231. Functions in this hook must return t if they identify and follow
  9232. a link at point. If they don't find anything interesting at point,
  9233. they must return nil.")
  9234. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9235. (defun org-open-at-point (&optional arg reference-buffer)
  9236. "Open link at or after point.
  9237. If there is no link at point, this function will search forward up to
  9238. the end of the current line.
  9239. Normally, files will be opened by an appropriate application. If the
  9240. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9241. With a double prefix argument, try to open outside of Emacs, in the
  9242. application the system uses for this file type."
  9243. (interactive "P")
  9244. ;; if in a code block, then open the block's results
  9245. (unless (call-interactively #'org-babel-open-src-block-result)
  9246. (org-load-modules-maybe)
  9247. (move-marker org-open-link-marker (point))
  9248. (setq org-window-config-before-follow-link (current-window-configuration))
  9249. (org-remove-occur-highlights nil nil t)
  9250. (cond
  9251. ((and (org-at-heading-p)
  9252. (not (org-at-timestamp-p t))
  9253. (not (org-in-regexp
  9254. (concat org-plain-link-re "\\|"
  9255. org-bracket-link-regexp "\\|"
  9256. org-angle-link-re "\\|"
  9257. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9258. (not (get-text-property (point) 'org-linked-text)))
  9259. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9260. (lk0 (car lkall))
  9261. (lk (if (stringp lk0) (list lk0) lk0))
  9262. (lkend (cdr lkall)))
  9263. (mapcar (lambda(l)
  9264. (search-forward l nil lkend)
  9265. (goto-char (match-beginning 0))
  9266. (org-open-at-point))
  9267. lk))
  9268. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9269. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9270. ((and (org-at-timestamp-p t)
  9271. (not (org-in-regexp org-bracket-link-regexp)))
  9272. (org-follow-timestamp-link))
  9273. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9274. (not (org-in-regexp org-any-link-re)))
  9275. (org-footnote-action))
  9276. (t
  9277. (let (type path link line search (pos (point)))
  9278. (catch 'match
  9279. (save-excursion
  9280. (skip-chars-forward "^]\n\r")
  9281. (when (org-in-regexp org-bracket-link-regexp 1)
  9282. (setq link (org-extract-attributes
  9283. (org-link-unescape (org-match-string-no-properties 1))))
  9284. (while (string-match " *\n *" link)
  9285. (setq link (replace-match " " t t link)))
  9286. (setq link (org-link-expand-abbrev link))
  9287. (cond
  9288. ((or (file-name-absolute-p link)
  9289. (string-match "^\\.\\.?/" link))
  9290. (setq type "file" path link))
  9291. ((string-match org-link-re-with-space3 link)
  9292. (setq type (match-string 1 link) path (match-string 2 link)))
  9293. ((string-match "^help:+\\(.+\\)" link)
  9294. (setq type "help" path (match-string 1 link)))
  9295. (t (setq type "thisfile" path link)))
  9296. (throw 'match t)))
  9297. (when (get-text-property (point) 'org-linked-text)
  9298. (setq type "thisfile"
  9299. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9300. (1+ (point)) (point))
  9301. path (buffer-substring
  9302. (or (previous-single-property-change pos 'org-linked-text)
  9303. (point-min))
  9304. (or (next-single-property-change pos 'org-linked-text)
  9305. (point-max))))
  9306. (throw 'match t))
  9307. (save-excursion
  9308. (when (or (org-in-regexp org-angle-link-re)
  9309. (let ((match (org-in-regexp org-plain-link-re)))
  9310. ;; Check a plain link is not within a bracket link
  9311. (and match
  9312. (save-excursion
  9313. (progn
  9314. (goto-char (car match))
  9315. (not (org-in-regexp org-bracket-link-regexp))))))
  9316. (let ((line_ending (save-excursion (end-of-line) (point))))
  9317. ;; We are in a line before a plain or bracket link
  9318. (or (re-search-forward org-plain-link-re line_ending t)
  9319. (re-search-forward org-bracket-link-regexp line_ending t))))
  9320. (setq type (match-string 1)
  9321. path (org-link-unescape (match-string 2)))
  9322. (throw 'match t)))
  9323. (save-excursion
  9324. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9325. (setq type "tags"
  9326. path (match-string 1))
  9327. (while (string-match ":" path)
  9328. (setq path (replace-match "+" t t path)))
  9329. (throw 'match t)))
  9330. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9331. (setq type "tree-match"
  9332. path (match-string 1))
  9333. (throw 'match t)))
  9334. (unless path
  9335. (user-error "No link found"))
  9336. ;; switch back to reference buffer
  9337. ;; needed when if called in a temporary buffer through
  9338. ;; org-open-link-from-string
  9339. (with-current-buffer (or reference-buffer (current-buffer))
  9340. ;; Remove any trailing spaces in path
  9341. (if (string-match " +\\'" path)
  9342. (setq path (replace-match "" t t path)))
  9343. (if (and org-link-translation-function
  9344. (fboundp org-link-translation-function))
  9345. ;; Check if we need to translate the link
  9346. (let ((tmp (funcall org-link-translation-function type path)))
  9347. (setq type (car tmp) path (cdr tmp))))
  9348. (cond
  9349. ((assoc type org-link-protocols)
  9350. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9351. ((equal type "help")
  9352. (let ((f-or-v (intern path)))
  9353. (cond ((fboundp f-or-v)
  9354. (describe-function f-or-v))
  9355. ((boundp f-or-v)
  9356. (describe-variable f-or-v))
  9357. (t (error "Not a known function or variable")))))
  9358. ((equal type "mailto")
  9359. (let ((cmd (car org-link-mailto-program))
  9360. (args (cdr org-link-mailto-program)) args1
  9361. (address path) (subject "") a)
  9362. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9363. (setq address (match-string 1 path)
  9364. subject (org-link-escape (match-string 2 path))))
  9365. (while args
  9366. (cond
  9367. ((not (stringp (car args))) (push (pop args) args1))
  9368. (t (setq a (pop args))
  9369. (if (string-match "%a" a)
  9370. (setq a (replace-match address t t a)))
  9371. (if (string-match "%s" a)
  9372. (setq a (replace-match subject t t a)))
  9373. (push a args1))))
  9374. (apply cmd (nreverse args1))))
  9375. ((member type '("http" "https" "ftp" "news"))
  9376. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9377. (org-link-escape
  9378. path org-link-escape-chars-browser)
  9379. path))))
  9380. ((string= type "doi")
  9381. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9382. (org-link-escape
  9383. path org-link-escape-chars-browser)
  9384. path))))
  9385. ((member type '("message"))
  9386. (browse-url (concat type ":" path)))
  9387. ((string= type "tags")
  9388. (org-tags-view arg path))
  9389. ((string= type "tree-match")
  9390. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9391. ((string= type "file")
  9392. (if (string-match "::\\([0-9]+\\)\\'" path)
  9393. (setq line (string-to-number (match-string 1 path))
  9394. path (substring path 0 (match-beginning 0)))
  9395. (if (string-match "::\\(.+\\)\\'" path)
  9396. (setq search (match-string 1 path)
  9397. path (substring path 0 (match-beginning 0)))))
  9398. (if (string-match "[*?{]" (file-name-nondirectory path))
  9399. (dired path)
  9400. (org-open-file path arg line search)))
  9401. ((string= type "shell")
  9402. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9403. (cmd path))
  9404. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9405. (string-match org-confirm-shell-link-not-regexp cmd))
  9406. (not org-confirm-shell-link-function)
  9407. (funcall org-confirm-shell-link-function
  9408. (format "Execute \"%s\" in shell? "
  9409. (org-add-props cmd nil
  9410. 'face 'org-warning))))
  9411. (progn
  9412. (message "Executing %s" cmd)
  9413. (shell-command cmd buf)
  9414. (if (featurep 'midnight)
  9415. (setq clean-buffer-list-kill-buffer-names
  9416. (cons buf clean-buffer-list-kill-buffer-names))))
  9417. (error "Abort"))))
  9418. ((string= type "elisp")
  9419. (let ((cmd path))
  9420. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9421. (string-match org-confirm-elisp-link-not-regexp cmd))
  9422. (not org-confirm-elisp-link-function)
  9423. (funcall org-confirm-elisp-link-function
  9424. (format "Execute \"%s\" as elisp? "
  9425. (org-add-props cmd nil
  9426. 'face 'org-warning))))
  9427. (message "%s => %s" cmd
  9428. (if (equal (string-to-char cmd) ?\()
  9429. (eval (read cmd))
  9430. (call-interactively (read cmd))))
  9431. (error "Abort"))))
  9432. ((and (string= type "thisfile")
  9433. (run-hook-with-args-until-success
  9434. 'org-open-link-functions path)))
  9435. ((string= type "thisfile")
  9436. (if arg
  9437. (switch-to-buffer-other-window
  9438. (org-get-buffer-for-internal-link (current-buffer)))
  9439. (org-mark-ring-push))
  9440. (let ((cmd `(org-link-search
  9441. ,path
  9442. ,(cond ((equal arg '(4)) ''occur)
  9443. ((equal arg '(16)) ''org-occur))
  9444. ,pos)))
  9445. (condition-case nil (let ((org-link-search-inhibit-query t))
  9446. (eval cmd))
  9447. (error (progn (widen) (eval cmd))))))
  9448. (t (browse-url-at-point)))))))
  9449. (move-marker org-open-link-marker nil)
  9450. (run-hook-with-args 'org-follow-link-hook)))
  9451. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9452. "Offer links in the current entry and return the selected link.
  9453. If there is only one link, return it.
  9454. If NTH is an integer, return the NTH link found.
  9455. If ZERO is a string, check also this string for a link, and if
  9456. there is one, return it."
  9457. (with-current-buffer buffer
  9458. (save-excursion
  9459. (save-restriction
  9460. (widen)
  9461. (goto-char marker)
  9462. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9463. "\\(" org-angle-link-re "\\)\\|"
  9464. "\\(" org-plain-link-re "\\)"))
  9465. (cnt ?0)
  9466. (in-emacs (if (integerp nth) nil nth))
  9467. have-zero end links link c)
  9468. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9469. (push (match-string 0 zero) links)
  9470. (setq cnt (1- cnt) have-zero t))
  9471. (save-excursion
  9472. (org-back-to-heading t)
  9473. (setq end (save-excursion (outline-next-heading) (point)))
  9474. (while (re-search-forward re end t)
  9475. (push (match-string 0) links))
  9476. (setq links (org-uniquify (reverse links))))
  9477. (cond
  9478. ((null links)
  9479. (message "No links"))
  9480. ((equal (length links) 1)
  9481. (setq link (car links)))
  9482. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9483. (setq link (nth (if have-zero nth (1- nth)) links)))
  9484. (t ; we have to select a link
  9485. (save-excursion
  9486. (save-window-excursion
  9487. (delete-other-windows)
  9488. (with-output-to-temp-buffer "*Select Link*"
  9489. (mapc (lambda (l)
  9490. (if (not (string-match org-bracket-link-regexp l))
  9491. (princ (format "[%c] %s\n" (incf cnt)
  9492. (org-remove-angle-brackets l)))
  9493. (if (match-end 3)
  9494. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9495. (match-string 3 l) (match-string 1 l)))
  9496. (princ (format "[%c] %s\n" (incf cnt)
  9497. (match-string 1 l))))))
  9498. links))
  9499. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9500. (message "Select link to open, RET to open all:")
  9501. (setq c (read-char-exclusive))
  9502. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9503. (when (equal c ?q) (error "Abort"))
  9504. (if (equal c ?\C-m)
  9505. (setq link links)
  9506. (setq nth (- c ?0))
  9507. (if have-zero (setq nth (1+ nth)))
  9508. (unless (and (integerp nth) (>= (length links) nth))
  9509. (error "Invalid link selection"))
  9510. (setq link (nth (1- nth) links)))))
  9511. (cons link end))))))
  9512. ;; Add special file links that specify the way of opening
  9513. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9514. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9515. (defun org-open-file-with-system (path)
  9516. "Open file at PATH using the system way of opening it."
  9517. (org-open-file path 'system))
  9518. (defun org-open-file-with-emacs (path)
  9519. "Open file at PATH in Emacs."
  9520. (org-open-file path 'emacs))
  9521. ;;; File search
  9522. (defvar org-create-file-search-functions nil
  9523. "List of functions to construct the right search string for a file link.
  9524. These functions are called in turn with point at the location to
  9525. which the link should point.
  9526. A function in the hook should first test if it would like to
  9527. handle this file type, for example by checking the `major-mode'
  9528. or the file extension. If it decides not to handle this file, it
  9529. should just return nil to give other functions a chance. If it
  9530. does handle the file, it must return the search string to be used
  9531. when following the link. The search string will be part of the
  9532. file link, given after a double colon, and `org-open-at-point'
  9533. will automatically search for it. If special measures must be
  9534. taken to make the search successful, another function should be
  9535. added to the companion hook `org-execute-file-search-functions',
  9536. which see.
  9537. A function in this hook may also use `setq' to set the variable
  9538. `description' to provide a suggestion for the descriptive text to
  9539. be used for this link when it gets inserted into an Org-mode
  9540. buffer with \\[org-insert-link].")
  9541. (defvar org-execute-file-search-functions nil
  9542. "List of functions to execute a file search triggered by a link.
  9543. Functions added to this hook must accept a single argument, the
  9544. search string that was part of the file link, the part after the
  9545. double colon. The function must first check if it would like to
  9546. handle this search, for example by checking the `major-mode' or
  9547. the file extension. If it decides not to handle this search, it
  9548. should just return nil to give other functions a chance. If it
  9549. does handle the search, it must return a non-nil value to keep
  9550. other functions from trying.
  9551. Each function can access the current prefix argument through the
  9552. variable `current-prefix-arg'. Note that a single prefix is used
  9553. to force opening a link in Emacs, so it may be good to only use a
  9554. numeric or double prefix to guide the search function.
  9555. In case this is needed, a function in this hook can also restore
  9556. the window configuration before `org-open-at-point' was called using:
  9557. (set-window-configuration org-window-config-before-follow-link)")
  9558. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9559. (defun org-link-search (s &optional type avoid-pos stealth)
  9560. "Search for a link search option.
  9561. If S is surrounded by forward slashes, it is interpreted as a
  9562. regular expression. In org-mode files, this will create an `org-occur'
  9563. sparse tree. In ordinary files, `occur' will be used to list matches.
  9564. If the current buffer is in `dired-mode', grep will be used to search
  9565. in all files. If AVOID-POS is given, ignore matches near that position.
  9566. When optional argument STEALTH is non-nil, do not modify
  9567. visibility around point, thus ignoring
  9568. `org-show-hierarchy-above', `org-show-following-heading' and
  9569. `org-show-siblings' variables."
  9570. (let ((case-fold-search t)
  9571. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9572. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9573. (append '(("") (" ") ("\t") ("\n"))
  9574. org-emphasis-alist)
  9575. "\\|") "\\)"))
  9576. (pos (point))
  9577. (pre nil) (post nil)
  9578. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9579. (cond
  9580. ;; First check if there are any special search functions
  9581. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9582. ;; Now try the builtin stuff
  9583. ((and (equal (string-to-char s0) ?#)
  9584. (> (length s0) 1)
  9585. (save-excursion
  9586. (goto-char (point-min))
  9587. (and
  9588. (re-search-forward
  9589. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9590. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9591. (setq type 'dedicated
  9592. pos (match-beginning 0))))
  9593. ;; There is an exact target for this
  9594. (goto-char pos)
  9595. (org-back-to-heading t)))
  9596. ((save-excursion
  9597. (goto-char (point-min))
  9598. (and
  9599. (re-search-forward
  9600. (concat "<<" (regexp-quote s0) ">>") nil t)
  9601. (setq type 'dedicated
  9602. pos (match-beginning 0))))
  9603. ;; There is an exact target for this
  9604. (goto-char pos))
  9605. ((save-excursion
  9606. (goto-char (point-min))
  9607. (and
  9608. (re-search-forward
  9609. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9610. (setq type 'dedicated pos (match-beginning 0))))
  9611. ;; Found an invisible target.
  9612. (goto-char pos))
  9613. ((save-excursion
  9614. (goto-char (point-min))
  9615. (and
  9616. (re-search-forward
  9617. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9618. (setq type 'dedicated pos (match-beginning 0))))
  9619. ;; Found an element with a matching #+name affiliated keyword.
  9620. (goto-char pos))
  9621. ((and (string-match "^(\\(.*\\))$" s0)
  9622. (save-excursion
  9623. (goto-char (point-min))
  9624. (and
  9625. (re-search-forward
  9626. (concat "[^[]" (regexp-quote
  9627. (format org-coderef-label-format
  9628. (match-string 1 s0))))
  9629. nil t)
  9630. (setq type 'dedicated
  9631. pos (1+ (match-beginning 0))))))
  9632. ;; There is a coderef target for this
  9633. (goto-char pos))
  9634. ((string-match "^/\\(.*\\)/$" s)
  9635. ;; A regular expression
  9636. (cond
  9637. ((derived-mode-p 'org-mode)
  9638. (org-occur (match-string 1 s)))
  9639. (t (org-do-occur (match-string 1 s)))))
  9640. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9641. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9642. (goto-char (point-min))
  9643. (cond
  9644. ((let (case-fold-search)
  9645. (re-search-forward (format org-complex-heading-regexp-format
  9646. (regexp-quote s))
  9647. nil t))
  9648. ;; OK, found a match
  9649. (setq type 'dedicated)
  9650. (goto-char (match-beginning 0)))
  9651. ((and (not org-link-search-inhibit-query)
  9652. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9653. (y-or-n-p "No match - create this as a new heading? "))
  9654. (goto-char (point-max))
  9655. (or (bolp) (newline))
  9656. (insert "* " s "\n")
  9657. (beginning-of-line 0))
  9658. (t
  9659. (goto-char pos)
  9660. (error "No match"))))
  9661. (t
  9662. ;; A normal search string
  9663. (when (equal (string-to-char s) ?*)
  9664. ;; Anchor on headlines, post may include tags.
  9665. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9666. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9667. s (substring s 1)))
  9668. (remove-text-properties
  9669. 0 (length s)
  9670. '(face nil mouse-face nil keymap nil fontified nil) s)
  9671. ;; Make a series of regular expressions to find a match
  9672. (setq words (org-split-string s "[ \n\r\t]+")
  9673. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9674. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9675. "\\)" markers)
  9676. re2a_ (concat "\\(" (mapconcat 'downcase words
  9677. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9678. re2a (concat "[ \t\r\n]" re2a_)
  9679. re4_ (concat "\\(" (mapconcat 'downcase words
  9680. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9681. re4 (concat "[^a-zA-Z_]" re4_)
  9682. re1 (concat pre re2 post)
  9683. re3 (concat pre (if pre re4_ re4) post)
  9684. re5 (concat pre ".*" re4)
  9685. re2 (concat pre re2)
  9686. re2a (concat pre (if pre re2a_ re2a))
  9687. re4 (concat pre (if pre re4_ re4))
  9688. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9689. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9690. re5 "\\)"
  9691. ))
  9692. (cond
  9693. ((eq type 'org-occur) (org-occur reall))
  9694. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9695. (t (goto-char (point-min))
  9696. (setq type 'fuzzy)
  9697. (if (or (and (org-search-not-self 1 re0 nil t)
  9698. (setq type 'dedicated))
  9699. (org-search-not-self 1 re1 nil t)
  9700. (org-search-not-self 1 re2 nil t)
  9701. (org-search-not-self 1 re2a nil t)
  9702. (org-search-not-self 1 re3 nil t)
  9703. (org-search-not-self 1 re4 nil t)
  9704. (org-search-not-self 1 re5 nil t))
  9705. (goto-char (match-beginning 1))
  9706. (goto-char pos)
  9707. (error "No match"))))))
  9708. (and (derived-mode-p 'org-mode)
  9709. (not stealth)
  9710. (org-show-context 'link-search))
  9711. type))
  9712. (defun org-search-not-self (group &rest args)
  9713. "Execute `re-search-forward', but only accept matches that do not
  9714. enclose the position of `org-open-link-marker'."
  9715. (let ((m org-open-link-marker))
  9716. (catch 'exit
  9717. (while (apply 're-search-forward args)
  9718. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9719. (goto-char (match-end group))
  9720. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9721. (> (match-beginning 0) (marker-position m))
  9722. (< (match-end 0) (marker-position m)))
  9723. (save-match-data
  9724. (or (not (org-in-regexp
  9725. org-bracket-link-analytic-regexp 1))
  9726. (not (match-end 4)) ; no description
  9727. (and (<= (match-beginning 4) (point))
  9728. (>= (match-end 4) (point))))))
  9729. (throw 'exit (point))))))))
  9730. (defun org-get-buffer-for-internal-link (buffer)
  9731. "Return a buffer to be used for displaying the link target of internal links."
  9732. (cond
  9733. ((not org-display-internal-link-with-indirect-buffer)
  9734. buffer)
  9735. ((string-match "(Clone)$" (buffer-name buffer))
  9736. (message "Buffer is already a clone, not making another one")
  9737. ;; we also do not modify visibility in this case
  9738. buffer)
  9739. (t ; make a new indirect buffer for displaying the link
  9740. (let* ((bn (buffer-name buffer))
  9741. (ibn (concat bn "(Clone)"))
  9742. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9743. (with-current-buffer ib (org-overview))
  9744. ib))))
  9745. (defun org-do-occur (regexp &optional cleanup)
  9746. "Call the Emacs command `occur'.
  9747. If CLEANUP is non-nil, remove the printout of the regular expression
  9748. in the *Occur* buffer. This is useful if the regex is long and not useful
  9749. to read."
  9750. (occur regexp)
  9751. (when cleanup
  9752. (let ((cwin (selected-window)) win beg end)
  9753. (when (setq win (get-buffer-window "*Occur*"))
  9754. (select-window win))
  9755. (goto-char (point-min))
  9756. (when (re-search-forward "match[a-z]+" nil t)
  9757. (setq beg (match-end 0))
  9758. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9759. (setq end (1- (match-beginning 0)))))
  9760. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9761. (goto-char (point-min))
  9762. (select-window cwin))))
  9763. ;;; The mark ring for links jumps
  9764. (defvar org-mark-ring nil
  9765. "Mark ring for positions before jumps in Org-mode.")
  9766. (defvar org-mark-ring-last-goto nil
  9767. "Last position in the mark ring used to go back.")
  9768. ;; Fill and close the ring
  9769. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9770. (loop for i from 1 to org-mark-ring-length do
  9771. (push (make-marker) org-mark-ring))
  9772. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9773. org-mark-ring)
  9774. (defun org-mark-ring-push (&optional pos buffer)
  9775. "Put the current position or POS into the mark ring and rotate it."
  9776. (interactive)
  9777. (setq pos (or pos (point)))
  9778. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9779. (move-marker (car org-mark-ring)
  9780. (or pos (point))
  9781. (or buffer (current-buffer)))
  9782. (message "%s"
  9783. (substitute-command-keys
  9784. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9785. (defun org-mark-ring-goto (&optional n)
  9786. "Jump to the previous position in the mark ring.
  9787. With prefix arg N, jump back that many stored positions. When
  9788. called several times in succession, walk through the entire ring.
  9789. Org-mode commands jumping to a different position in the current file,
  9790. or to another Org-mode file, automatically push the old position
  9791. onto the ring."
  9792. (interactive "p")
  9793. (let (p m)
  9794. (if (eq last-command this-command)
  9795. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9796. (setq p org-mark-ring))
  9797. (setq org-mark-ring-last-goto p)
  9798. (setq m (car p))
  9799. (org-pop-to-buffer-same-window (marker-buffer m))
  9800. (goto-char m)
  9801. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9802. (defun org-remove-angle-brackets (s)
  9803. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9804. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9805. s)
  9806. (defun org-add-angle-brackets (s)
  9807. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9808. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9809. s)
  9810. (defun org-remove-double-quotes (s)
  9811. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9812. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9813. s)
  9814. ;;; Following specific links
  9815. (defun org-follow-timestamp-link ()
  9816. "Open an agenda view for the time-stamp date/range at point."
  9817. (cond
  9818. ((org-at-date-range-p t)
  9819. (let ((org-agenda-start-on-weekday)
  9820. (t1 (match-string 1))
  9821. (t2 (match-string 2)) tt1 tt2)
  9822. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9823. tt2 (time-to-days (org-time-string-to-time t2)))
  9824. (let ((org-agenda-buffer-tmp-name
  9825. (format "*Org Agenda(a:%s)"
  9826. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9827. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9828. ((org-at-timestamp-p t)
  9829. (let ((org-agenda-buffer-tmp-name
  9830. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9831. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9832. (substring (match-string 1) 0 10)))
  9833. 1)))
  9834. (t (error "This should not happen"))))
  9835. ;;; Following file links
  9836. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9837. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9838. (declare-function mailcap-mime-info
  9839. "mailcap" (string &optional request no-decode))
  9840. (defvar org-wait nil)
  9841. (defun org-open-file (path &optional in-emacs line search)
  9842. "Open the file at PATH.
  9843. First, this expands any special file name abbreviations. Then the
  9844. configuration variable `org-file-apps' is checked if it contains an
  9845. entry for this file type, and if yes, the corresponding command is launched.
  9846. If no application is found, Emacs simply visits the file.
  9847. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9848. With a double \\[universal-argument] \\[universal-argument] \
  9849. prefix arg, Org tries to avoid opening in Emacs
  9850. and to use an external application to visit the file.
  9851. Optional LINE specifies a line to go to, optional SEARCH a string
  9852. to search for. If LINE or SEARCH is given, the file will be
  9853. opened in Emacs, unless an entry from org-file-apps that makes
  9854. use of groups in a regexp matches.
  9855. If you want to change the way frames are used when following a
  9856. link, please customize `org-link-frame-setup'.
  9857. If the file does not exist, an error is thrown."
  9858. (let* ((file (if (equal path "")
  9859. buffer-file-name
  9860. (substitute-in-file-name (expand-file-name path))))
  9861. (file-apps (append org-file-apps (org-default-apps)))
  9862. (apps (org-remove-if
  9863. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9864. (apps-dlink (org-remove-if-not
  9865. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9866. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9867. (dirp (if remp nil (file-directory-p file)))
  9868. (file (if (and dirp org-open-directory-means-index-dot-org)
  9869. (concat (file-name-as-directory file) "index.org")
  9870. file))
  9871. (a-m-a-p (assq 'auto-mode apps))
  9872. (dfile (downcase file))
  9873. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9874. (link (cond ((and (eq line nil)
  9875. (eq search nil))
  9876. file)
  9877. (line
  9878. (concat file "::" (number-to-string line)))
  9879. (search
  9880. (concat file "::" search))))
  9881. (dlink (downcase link))
  9882. (old-buffer (current-buffer))
  9883. (old-pos (point))
  9884. (old-mode major-mode)
  9885. ext cmd link-match-data)
  9886. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9887. (setq ext (match-string 1 dfile))
  9888. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9889. (setq ext (match-string 1 dfile))))
  9890. (cond
  9891. ((member in-emacs '((16) system))
  9892. (setq cmd (cdr (assoc 'system apps))))
  9893. (in-emacs (setq cmd 'emacs))
  9894. (t
  9895. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9896. (and dirp (cdr (assoc 'directory apps)))
  9897. ; first, try matching against apps-dlink
  9898. ; if we get a match here, store the match data for later
  9899. (let ((match (assoc-default dlink apps-dlink
  9900. 'string-match)))
  9901. (if match
  9902. (progn (setq link-match-data (match-data))
  9903. match)
  9904. (progn (setq in-emacs (or in-emacs line search))
  9905. nil))) ; if we have no match in apps-dlink,
  9906. ; always open the file in emacs if line or search
  9907. ; is given (for backwards compatibility)
  9908. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9909. 'string-match)
  9910. (cdr (assoc ext apps))
  9911. (cdr (assoc t apps))))))
  9912. (when (eq cmd 'system)
  9913. (setq cmd (cdr (assoc 'system apps))))
  9914. (when (eq cmd 'default)
  9915. (setq cmd (cdr (assoc t apps))))
  9916. (when (eq cmd 'mailcap)
  9917. (require 'mailcap)
  9918. (mailcap-parse-mailcaps)
  9919. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9920. (command (mailcap-mime-info mime-type)))
  9921. (if (stringp command)
  9922. (setq cmd command)
  9923. (setq cmd 'emacs))))
  9924. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9925. (not (file-exists-p file))
  9926. (not org-open-non-existing-files))
  9927. (error "No such file: %s" file))
  9928. (cond
  9929. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9930. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9931. (while (string-match "['\"]%s['\"]" cmd)
  9932. (setq cmd (replace-match "%s" t t cmd)))
  9933. (while (string-match "%s" cmd)
  9934. (setq cmd (replace-match
  9935. (save-match-data
  9936. (shell-quote-argument
  9937. (convert-standard-filename file)))
  9938. t t cmd)))
  9939. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9940. (save-match-data
  9941. (let ((match-index 1)
  9942. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9943. (set-match-data link-match-data)
  9944. (while (<= match-index number-of-groups)
  9945. (let ((regex (concat "%" (number-to-string match-index)))
  9946. (replace-with (match-string match-index dlink)))
  9947. (while (string-match regex cmd)
  9948. (setq cmd (replace-match replace-with t t cmd))))
  9949. (setq match-index (+ match-index 1)))))
  9950. (save-window-excursion
  9951. (message "Running %s...done" cmd)
  9952. (start-process-shell-command cmd nil cmd)
  9953. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9954. ((or (stringp cmd)
  9955. (eq cmd 'emacs))
  9956. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9957. (widen)
  9958. (if line (org-goto-line line)
  9959. (if search (org-link-search search))))
  9960. ((consp cmd)
  9961. (let ((file (convert-standard-filename file)))
  9962. (save-match-data
  9963. (set-match-data link-match-data)
  9964. (eval cmd))))
  9965. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9966. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9967. (or (not (equal old-buffer (current-buffer)))
  9968. (not (equal old-pos (point))))
  9969. (org-mark-ring-push old-pos old-buffer))))
  9970. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9971. "This function returns non-nil if `entry' uses a regular
  9972. expression which should be matched against the whole link by
  9973. org-open-file.
  9974. It assumes that is the case when the entry uses a regular
  9975. expression which has at least one grouping construct and the
  9976. action is either a lisp form or a command string containing
  9977. '%1', i.e. using at least one subexpression match as a
  9978. parameter."
  9979. (let ((selector (car entry))
  9980. (action (cdr entry)))
  9981. (if (stringp selector)
  9982. (and (> (regexp-opt-depth selector) 0)
  9983. (or (and (stringp action)
  9984. (string-match "%[0-9]" action))
  9985. (consp action)))
  9986. nil)))
  9987. (defun org-default-apps ()
  9988. "Return the default applications for this operating system."
  9989. (cond
  9990. ((eq system-type 'darwin)
  9991. org-file-apps-defaults-macosx)
  9992. ((eq system-type 'windows-nt)
  9993. org-file-apps-defaults-windowsnt)
  9994. (t org-file-apps-defaults-gnu)))
  9995. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9996. "Convert extensions to regular expressions in the cars of LIST.
  9997. Also, weed out any non-string entries, because the return value is used
  9998. only for regexp matching.
  9999. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10000. point to the symbol `emacs', indicating that the file should
  10001. be opened in Emacs."
  10002. (append
  10003. (delq nil
  10004. (mapcar (lambda (x)
  10005. (if (not (stringp (car x)))
  10006. nil
  10007. (if (string-match "\\W" (car x))
  10008. x
  10009. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10010. list))
  10011. (if add-auto-mode
  10012. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10013. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10014. (defun org-file-remote-p (file)
  10015. "Test whether FILE specifies a location on a remote system.
  10016. Return non-nil if the location is indeed remote.
  10017. For example, the filename \"/user@host:/foo\" specifies a location
  10018. on the system \"/user@host:\"."
  10019. (cond ((fboundp 'file-remote-p)
  10020. (file-remote-p file))
  10021. ((fboundp 'tramp-handle-file-remote-p)
  10022. (tramp-handle-file-remote-p file))
  10023. ((and (boundp 'ange-ftp-name-format)
  10024. (string-match (car ange-ftp-name-format) file))
  10025. t)))
  10026. ;;;; Refiling
  10027. (defun org-get-org-file ()
  10028. "Read a filename, with default directory `org-directory'."
  10029. (let ((default (or org-default-notes-file remember-data-file)))
  10030. (read-file-name (format "File name [%s]: " default)
  10031. (file-name-as-directory org-directory)
  10032. default)))
  10033. (defun org-notes-order-reversed-p ()
  10034. "Check if the current file should receive notes in reversed order."
  10035. (cond
  10036. ((not org-reverse-note-order) nil)
  10037. ((eq t org-reverse-note-order) t)
  10038. ((not (listp org-reverse-note-order)) nil)
  10039. (t (catch 'exit
  10040. (let ((all org-reverse-note-order)
  10041. entry)
  10042. (while (setq entry (pop all))
  10043. (if (string-match (car entry) buffer-file-name)
  10044. (throw 'exit (cdr entry))))
  10045. nil)))))
  10046. (defvar org-refile-target-table nil
  10047. "The list of refile targets, created by `org-refile'.")
  10048. (defvar org-agenda-new-buffers nil
  10049. "Buffers created to visit agenda files.")
  10050. (defvar org-refile-cache nil
  10051. "Cache for refile targets.")
  10052. (defvar org-refile-markers nil
  10053. "All the markers used for caching refile locations.")
  10054. (defun org-refile-marker (pos)
  10055. "Get a new refile marker, but only if caching is in use."
  10056. (if (not org-refile-use-cache)
  10057. pos
  10058. (let ((m (make-marker)))
  10059. (move-marker m pos)
  10060. (push m org-refile-markers)
  10061. m)))
  10062. (defun org-refile-cache-clear ()
  10063. "Clear the refile cache and disable all the markers."
  10064. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10065. (setq org-refile-markers nil)
  10066. (setq org-refile-cache nil)
  10067. (message "Refile cache has been cleared"))
  10068. (defun org-refile-cache-check-set (set)
  10069. "Check if all the markers in the cache still have live buffers."
  10070. (let (marker)
  10071. (catch 'exit
  10072. (while (and set (setq marker (nth 3 (pop set))))
  10073. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10074. (when (and marker (null (marker-buffer marker)))
  10075. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10076. (sit-for 3)
  10077. (throw 'exit nil)))
  10078. t)))
  10079. (defun org-refile-cache-put (set &rest identifiers)
  10080. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10081. (let* ((key (sha1 (prin1-to-string identifiers)))
  10082. (entry (assoc key org-refile-cache)))
  10083. (if entry
  10084. (setcdr entry set)
  10085. (push (cons key set) org-refile-cache))))
  10086. (defun org-refile-cache-get (&rest identifiers)
  10087. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10088. (cond
  10089. ((not org-refile-cache) nil)
  10090. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10091. (t
  10092. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10093. org-refile-cache))))
  10094. (and set (org-refile-cache-check-set set) set)))))
  10095. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10096. "Produce a table with refile targets."
  10097. (let ((case-fold-search nil)
  10098. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10099. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10100. targets tgs txt re files f desc descre fast-path-p level pos0)
  10101. (message "Getting targets...")
  10102. (with-current-buffer (or default-buffer (current-buffer))
  10103. (while (setq entry (pop entries))
  10104. (setq files (car entry) desc (cdr entry))
  10105. (setq fast-path-p nil)
  10106. (cond
  10107. ((null files) (setq files (list (current-buffer))))
  10108. ((eq files 'org-agenda-files)
  10109. (setq files (org-agenda-files 'unrestricted)))
  10110. ((and (symbolp files) (fboundp files))
  10111. (setq files (funcall files)))
  10112. ((and (symbolp files) (boundp files))
  10113. (setq files (symbol-value files))))
  10114. (if (stringp files) (setq files (list files)))
  10115. (cond
  10116. ((eq (car desc) :tag)
  10117. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10118. ((eq (car desc) :todo)
  10119. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10120. ((eq (car desc) :regexp)
  10121. (setq descre (cdr desc)))
  10122. ((eq (car desc) :level)
  10123. (setq descre (concat "^\\*\\{" (number-to-string
  10124. (if org-odd-levels-only
  10125. (1- (* 2 (cdr desc)))
  10126. (cdr desc)))
  10127. "\\}[ \t]")))
  10128. ((eq (car desc) :maxlevel)
  10129. (setq fast-path-p t)
  10130. (setq descre (concat "^\\*\\{1," (number-to-string
  10131. (if org-odd-levels-only
  10132. (1- (* 2 (cdr desc)))
  10133. (cdr desc)))
  10134. "\\}[ \t]")))
  10135. (t (error "Bad refiling target description %s" desc)))
  10136. (while (setq f (pop files))
  10137. (with-current-buffer
  10138. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10139. (or
  10140. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10141. (progn
  10142. (if (bufferp f) (setq f (buffer-file-name
  10143. (buffer-base-buffer f))))
  10144. (setq f (and f (expand-file-name f)))
  10145. (if (eq org-refile-use-outline-path 'file)
  10146. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10147. (save-excursion
  10148. (save-restriction
  10149. (widen)
  10150. (goto-char (point-min))
  10151. (while (re-search-forward descre nil t)
  10152. (goto-char (setq pos0 (point-at-bol)))
  10153. (catch 'next
  10154. (when org-refile-target-verify-function
  10155. (save-match-data
  10156. (or (funcall org-refile-target-verify-function)
  10157. (throw 'next t))))
  10158. (when (and (looking-at org-complex-heading-regexp)
  10159. (not (member (match-string 4) excluded-entries))
  10160. (match-string 4))
  10161. (setq level (org-reduced-level
  10162. (- (match-end 1) (match-beginning 1)))
  10163. txt (org-link-display-format (match-string 4))
  10164. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10165. re (format org-complex-heading-regexp-format
  10166. (regexp-quote (match-string 4))))
  10167. (when org-refile-use-outline-path
  10168. (setq txt (mapconcat
  10169. 'org-protect-slash
  10170. (append
  10171. (if (eq org-refile-use-outline-path
  10172. 'file)
  10173. (list (file-name-nondirectory
  10174. (buffer-file-name
  10175. (buffer-base-buffer))))
  10176. (if (eq org-refile-use-outline-path
  10177. 'full-file-path)
  10178. (list (buffer-file-name
  10179. (buffer-base-buffer)))))
  10180. (org-get-outline-path fast-path-p
  10181. level txt)
  10182. (list txt))
  10183. "/")))
  10184. (push (list txt f re (org-refile-marker (point)))
  10185. tgs)))
  10186. (when (= (point) pos0)
  10187. ;; verification function has not moved point
  10188. (goto-char (point-at-eol))))))))
  10189. (when org-refile-use-cache
  10190. (org-refile-cache-put tgs (buffer-file-name) descre))
  10191. (setq targets (append tgs targets))
  10192. ))))
  10193. (message "Getting targets...done")
  10194. (nreverse targets)))
  10195. (defun org-protect-slash (s)
  10196. (while (string-match "/" s)
  10197. (setq s (replace-match "\\" t t s)))
  10198. s)
  10199. (defvar org-olpa (make-vector 20 nil))
  10200. (defun org-get-outline-path (&optional fastp level heading)
  10201. "Return the outline path to the current entry, as a list.
  10202. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10203. routine which makes outline path derivations for an entire file,
  10204. avoiding backtracing. Refile target collection makes use of that."
  10205. (if fastp
  10206. (progn
  10207. (if (> level 19)
  10208. (error "Outline path failure, more than 19 levels"))
  10209. (loop for i from level upto 19 do
  10210. (aset org-olpa i nil))
  10211. (prog1
  10212. (delq nil (append org-olpa nil))
  10213. (aset org-olpa level heading)))
  10214. (let (rtn case-fold-search)
  10215. (save-excursion
  10216. (save-restriction
  10217. (widen)
  10218. (while (org-up-heading-safe)
  10219. (when (looking-at org-complex-heading-regexp)
  10220. (push (org-match-string-no-properties 4) rtn)))
  10221. rtn)))))
  10222. (defun org-format-outline-path (path &optional width prefix separator)
  10223. "Format the outline path PATH for display.
  10224. WIDTH is the maximum number of characters that is available.
  10225. PREFIX is a prefix to be included in the returned string,
  10226. such as the file name.
  10227. SEPARATOR is inserted between the different parts of the path,
  10228. the default is \"/\"."
  10229. (setq width (or width 79))
  10230. (if prefix (setq width (- width (length prefix))))
  10231. (if (not path)
  10232. (or prefix "")
  10233. (let* ((nsteps (length path))
  10234. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10235. (maxwidth (if (<= total-width width)
  10236. 10000 ;; everything fits
  10237. ;; we need to shorten the level headings
  10238. (/ (- width nsteps) nsteps)))
  10239. (org-odd-levels-only nil)
  10240. (n 0)
  10241. (total (1+ (length prefix))))
  10242. (setq maxwidth (max maxwidth 10))
  10243. (concat prefix
  10244. (if prefix (or separator "/"))
  10245. (mapconcat
  10246. (lambda (h)
  10247. (setq n (1+ n))
  10248. (if (and (= n nsteps) (< maxwidth 10000))
  10249. (setq maxwidth (- total-width total)))
  10250. (if (< (length h) maxwidth)
  10251. (progn (setq total (+ total (length h) 1)) h)
  10252. (setq h (substring h 0 (- maxwidth 2))
  10253. total (+ total maxwidth 1))
  10254. (if (string-match "[ \t]+\\'" h)
  10255. (setq h (substring h 0 (match-beginning 0))))
  10256. (setq h (concat h "..")))
  10257. (org-add-props h nil 'face
  10258. (nth (% (1- n) org-n-level-faces)
  10259. org-level-faces))
  10260. h)
  10261. path (or separator "/"))))))
  10262. (defun org-display-outline-path (&optional file current separator just-return-string)
  10263. "Display the current outline path in the echo area.
  10264. If FILE is non-nil, prepend the output with the file name.
  10265. If CURRENT is non-nil, append the current heading to the output.
  10266. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10267. the different parts of the path and defaults to \"/\".
  10268. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10269. (interactive "P")
  10270. (let* (case-fold-search
  10271. message-log-max ; Don't populate the *Messages* buffer
  10272. (bfn (buffer-file-name (buffer-base-buffer)))
  10273. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10274. res)
  10275. (if current (setq path (append path
  10276. (save-excursion
  10277. (org-back-to-heading t)
  10278. (if (looking-at org-complex-heading-regexp)
  10279. (list (match-string 4)))))))
  10280. (setq res
  10281. (org-format-outline-path
  10282. path
  10283. (1- (frame-width))
  10284. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10285. separator))
  10286. (if just-return-string
  10287. (org-no-properties res)
  10288. (message "%s" res))))
  10289. (defvar org-refile-history nil
  10290. "History for refiling operations.")
  10291. (defvar org-after-refile-insert-hook nil
  10292. "Hook run after `org-refile' has inserted its stuff at the new location.
  10293. Note that this is still *before* the stuff will be removed from
  10294. the *old* location.")
  10295. (defvar org-capture-last-stored-marker)
  10296. (defvar org-refile-keep nil
  10297. "Non-nil means `org-refile' will copy instead of refile.")
  10298. (defun org-copy ()
  10299. "Like `org-refile', but copy."
  10300. (interactive)
  10301. (let ((org-refile-keep t))
  10302. (funcall 'org-refile nil nil nil "Copy")))
  10303. (defun org-refile (&optional goto default-buffer rfloc msg)
  10304. "Move the entry or entries at point to another heading.
  10305. The list of target headings is compiled using the information in
  10306. `org-refile-targets', which see.
  10307. At the target location, the entry is filed as a subitem of the target
  10308. heading. Depending on `org-reverse-note-order', the new subitem will
  10309. either be the first or the last subitem.
  10310. If there is an active region, all entries in that region will be moved.
  10311. However, the region must fulfill the requirement that the first heading
  10312. is the first one sets the top-level of the moved text - at most siblings
  10313. below it are allowed.
  10314. With prefix arg GOTO, the command will only visit the target location
  10315. and not actually move anything.
  10316. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10317. go to the location where the last refiling operation has put the subtree.
  10318. With a prefix argument of `2', refile to the running clock.
  10319. RFLOC can be a refile location obtained in a different way.
  10320. MSG is a string to replace \"Refile\" in the default prompt with
  10321. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10322. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10323. If you are using target caching (see `org-refile-use-cache'),
  10324. you have to clear the target cache in order to find new targets.
  10325. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10326. prefix argument (`C-u C-u C-u C-c C-w')."
  10327. (interactive "P")
  10328. (if (member goto '(0 (64)))
  10329. (org-refile-cache-clear)
  10330. (let* ((actionmsg (or msg "Refile"))
  10331. (cbuf (current-buffer))
  10332. (regionp (org-region-active-p))
  10333. (region-start (and regionp (region-beginning)))
  10334. (region-end (and regionp (region-end)))
  10335. (region-length (and regionp (- region-end region-start)))
  10336. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10337. pos it nbuf file re level reversed)
  10338. (setq last-command nil)
  10339. (when regionp
  10340. (goto-char region-start)
  10341. (or (bolp) (goto-char (point-at-bol)))
  10342. (setq region-start (point))
  10343. (unless (or (org-kill-is-subtree-p
  10344. (buffer-substring region-start region-end))
  10345. (prog1 org-refile-active-region-within-subtree
  10346. (org-toggle-heading)))
  10347. (error "The region is not a (sequence of) subtree(s)")))
  10348. (if (equal goto '(16))
  10349. (org-refile-goto-last-stored)
  10350. (when (or
  10351. (and (equal goto 2)
  10352. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10353. (prog1
  10354. (setq it (list (or org-clock-heading "running clock")
  10355. (buffer-file-name
  10356. (marker-buffer org-clock-hd-marker))
  10357. ""
  10358. (marker-position org-clock-hd-marker)))
  10359. (setq goto nil)))
  10360. (setq it (or rfloc
  10361. (let (heading-text)
  10362. (save-excursion
  10363. (unless goto
  10364. (org-back-to-heading t)
  10365. (setq heading-text
  10366. (nth 4 (org-heading-components))))
  10367. (org-refile-get-location
  10368. (cond (goto "Goto")
  10369. (regionp (concat actionmsg " region to"))
  10370. (t (concat actionmsg " subtree \""
  10371. heading-text "\" to")))
  10372. default-buffer
  10373. (and (not (equal '(4) goto))
  10374. org-refile-allow-creating-parent-nodes)
  10375. goto))))))
  10376. (setq file (nth 1 it)
  10377. re (nth 2 it)
  10378. pos (nth 3 it))
  10379. (if (and (not goto)
  10380. pos
  10381. (equal (buffer-file-name) file)
  10382. (if regionp
  10383. (and (>= pos region-start)
  10384. (<= pos region-end))
  10385. (and (>= pos (point))
  10386. (< pos (save-excursion
  10387. (org-end-of-subtree t t))))))
  10388. (error "Cannot refile to position inside the tree or region"))
  10389. (setq nbuf (or (find-buffer-visiting file)
  10390. (find-file-noselect file)))
  10391. (if goto
  10392. (progn
  10393. (org-pop-to-buffer-same-window nbuf)
  10394. (goto-char pos)
  10395. (org-show-context 'org-goto))
  10396. (if regionp
  10397. (progn
  10398. (org-kill-new (buffer-substring region-start region-end))
  10399. (org-save-markers-in-region region-start region-end))
  10400. (org-copy-subtree 1 nil t))
  10401. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10402. (find-file-noselect file)))
  10403. (setq reversed (org-notes-order-reversed-p))
  10404. (save-excursion
  10405. (save-restriction
  10406. (widen)
  10407. (if pos
  10408. (progn
  10409. (goto-char pos)
  10410. (looking-at org-outline-regexp)
  10411. (setq level (org-get-valid-level (funcall outline-level) 1))
  10412. (goto-char
  10413. (if reversed
  10414. (or (outline-next-heading) (point-max))
  10415. (or (save-excursion (org-get-next-sibling))
  10416. (org-end-of-subtree t t)
  10417. (point-max)))))
  10418. (setq level 1)
  10419. (if (not reversed)
  10420. (goto-char (point-max))
  10421. (goto-char (point-min))
  10422. (or (outline-next-heading) (goto-char (point-max)))))
  10423. (if (not (bolp)) (newline))
  10424. (org-paste-subtree level)
  10425. (when org-log-refile
  10426. (org-add-log-setup 'refile nil nil 'findpos
  10427. org-log-refile)
  10428. (unless (eq org-log-refile 'note)
  10429. (save-excursion (org-add-log-note))))
  10430. (and org-auto-align-tags
  10431. (let ((org-loop-over-headlines-in-active-region nil))
  10432. (org-set-tags nil t)))
  10433. (with-demoted-errors
  10434. (bookmark-set "org-refile-last-stored"))
  10435. ;; If we are refiling for capture, make sure that the
  10436. ;; last-capture pointers point here
  10437. (when (org-bound-and-true-p org-refile-for-capture)
  10438. (with-demoted-errors
  10439. (bookmark-set "org-capture-last-stored-marker"))
  10440. (move-marker org-capture-last-stored-marker (point)))
  10441. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10442. (run-hooks 'org-after-refile-insert-hook))))
  10443. (unless org-refile-keep
  10444. (if regionp
  10445. (delete-region (point) (+ (point) region-length))
  10446. (org-cut-subtree)))
  10447. (when (featurep 'org-inlinetask)
  10448. (org-inlinetask-remove-END-maybe))
  10449. (setq org-markers-to-move nil)
  10450. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10451. (defun org-refile-goto-last-stored ()
  10452. "Go to the location where the last refile was stored."
  10453. (interactive)
  10454. (bookmark-jump "org-refile-last-stored")
  10455. (message "This is the location of the last refile"))
  10456. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10457. no-exclude)
  10458. "Prompt the user for a refile location, using PROMPT.
  10459. PROMPT should not be suffixed with a colon and a space, because
  10460. this function appends the default value from
  10461. `org-refile-history' automatically, if that is not empty.
  10462. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10463. this is used for the GOTO interface."
  10464. (let ((org-refile-targets org-refile-targets)
  10465. (org-refile-use-outline-path org-refile-use-outline-path)
  10466. excluded-entries)
  10467. (when (and (derived-mode-p 'org-mode)
  10468. (not org-refile-use-cache)
  10469. (not no-exclude))
  10470. (org-map-tree
  10471. (lambda()
  10472. (setq excluded-entries
  10473. (append excluded-entries (list (org-get-heading t t)))))))
  10474. (setq org-refile-target-table
  10475. (org-refile-get-targets default-buffer excluded-entries)))
  10476. (unless org-refile-target-table
  10477. (error "No refile targets"))
  10478. (let* ((cbuf (current-buffer))
  10479. (partial-completion-mode nil)
  10480. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10481. (cfunc (if (and org-refile-use-outline-path
  10482. org-outline-path-complete-in-steps)
  10483. 'org-olpath-completing-read
  10484. 'org-icompleting-read))
  10485. (extra (if org-refile-use-outline-path "/" ""))
  10486. (cbnex (concat (buffer-name) extra))
  10487. (filename (and cfn (expand-file-name cfn)))
  10488. (tbl (mapcar
  10489. (lambda (x)
  10490. (if (and (not (member org-refile-use-outline-path
  10491. '(file full-file-path)))
  10492. (not (equal filename (nth 1 x))))
  10493. (cons (concat (car x) extra " ("
  10494. (file-name-nondirectory (nth 1 x)) ")")
  10495. (cdr x))
  10496. (cons (concat (car x) extra) (cdr x))))
  10497. org-refile-target-table))
  10498. (completion-ignore-case t)
  10499. cdef
  10500. (prompt (concat prompt
  10501. (or (and (car org-refile-history)
  10502. (concat " (default " (car org-refile-history) ")"))
  10503. (and (assoc cbnex tbl) (setq cdef cbnex)
  10504. (concat " (default " cbnex ")"))) ": "))
  10505. pa answ parent-target child parent old-hist)
  10506. (setq old-hist org-refile-history)
  10507. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10508. nil 'org-refile-history (or cdef (car org-refile-history))))
  10509. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10510. (org-refile-check-position pa)
  10511. (if pa
  10512. (progn
  10513. (when (or (not org-refile-history)
  10514. (not (eq old-hist org-refile-history))
  10515. (not (equal (car pa) (car org-refile-history))))
  10516. (setq org-refile-history
  10517. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10518. org-refile-history
  10519. (cdr org-refile-history))))
  10520. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10521. (pop org-refile-history)))
  10522. pa)
  10523. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10524. (progn
  10525. (setq parent (match-string 1 answ)
  10526. child (match-string 2 answ))
  10527. (setq parent-target (or (assoc parent tbl)
  10528. (assoc (concat parent "/") tbl)))
  10529. (when (and parent-target
  10530. (or (eq new-nodes t)
  10531. (and (eq new-nodes 'confirm)
  10532. (y-or-n-p (format "Create new node \"%s\"? "
  10533. child)))))
  10534. (org-refile-new-child parent-target child)))
  10535. (error "Invalid target location")))))
  10536. (declare-function org-string-nw-p "org-macs" (s))
  10537. (defun org-refile-check-position (refile-pointer)
  10538. "Check if the refile pointer matches the headline to which it points."
  10539. (let* ((file (nth 1 refile-pointer))
  10540. (re (nth 2 refile-pointer))
  10541. (pos (nth 3 refile-pointer))
  10542. buffer)
  10543. (if (and (not (markerp pos)) (not file))
  10544. (error "Please save the buffer to a file before refiling")
  10545. (when (org-string-nw-p re)
  10546. (setq buffer (if (markerp pos)
  10547. (marker-buffer pos)
  10548. (or (find-buffer-visiting file)
  10549. (find-file-noselect file))))
  10550. (with-current-buffer buffer
  10551. (save-excursion
  10552. (save-restriction
  10553. (widen)
  10554. (goto-char pos)
  10555. (beginning-of-line 1)
  10556. (unless (org-looking-at-p re)
  10557. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10558. (defun org-refile-new-child (parent-target child)
  10559. "Use refile target PARENT-TARGET to add new CHILD below it."
  10560. (unless parent-target
  10561. (error "Cannot find parent for new node"))
  10562. (let ((file (nth 1 parent-target))
  10563. (pos (nth 3 parent-target))
  10564. level)
  10565. (with-current-buffer (or (find-buffer-visiting file)
  10566. (find-file-noselect file))
  10567. (save-excursion
  10568. (save-restriction
  10569. (widen)
  10570. (if pos
  10571. (goto-char pos)
  10572. (goto-char (point-max))
  10573. (if (not (bolp)) (newline)))
  10574. (when (looking-at org-outline-regexp)
  10575. (setq level (funcall outline-level))
  10576. (org-end-of-subtree t t))
  10577. (org-back-over-empty-lines)
  10578. (insert "\n" (make-string
  10579. (if pos (org-get-valid-level level 1) 1) ?*)
  10580. " " child "\n")
  10581. (beginning-of-line 0)
  10582. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10583. (defun org-olpath-completing-read (prompt collection &rest args)
  10584. "Read an outline path like a file name."
  10585. (let ((thetable collection)
  10586. (org-completion-use-ido nil) ; does not work with ido.
  10587. (org-completion-use-iswitchb nil)) ; or iswitchb
  10588. (apply
  10589. 'org-icompleting-read prompt
  10590. (lambda (string predicate &optional flag)
  10591. (let (rtn r f (l (length string)))
  10592. (cond
  10593. ((eq flag nil)
  10594. ;; try completion
  10595. (try-completion string thetable))
  10596. ((eq flag t)
  10597. ;; all-completions
  10598. (setq rtn (all-completions string thetable predicate))
  10599. (mapcar
  10600. (lambda (x)
  10601. (setq r (substring x l))
  10602. (if (string-match " ([^)]*)$" x)
  10603. (setq f (match-string 0 x))
  10604. (setq f ""))
  10605. (if (string-match "/" r)
  10606. (concat string (substring r 0 (match-end 0)) f)
  10607. x))
  10608. rtn))
  10609. ((eq flag 'lambda)
  10610. ;; exact match?
  10611. (assoc string thetable)))))
  10612. args)))
  10613. ;;;; Dynamic blocks
  10614. (defun org-find-dblock (name)
  10615. "Find the first dynamic block with name NAME in the buffer.
  10616. If not found, stay at current position and return nil."
  10617. (let ((case-fold-search t) pos)
  10618. (save-excursion
  10619. (goto-char (point-min))
  10620. (setq pos (and (re-search-forward
  10621. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10622. (match-beginning 0))))
  10623. (if pos (goto-char pos))
  10624. pos))
  10625. (defconst org-dblock-start-re
  10626. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10627. "Matches the start line of a dynamic block, with parameters.")
  10628. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10629. "Matches the end of a dynamic block.")
  10630. (defun org-create-dblock (plist)
  10631. "Create a dynamic block section, with parameters taken from PLIST.
  10632. PLIST must contain a :name entry which is used as name of the block."
  10633. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10634. (end-of-line 1)
  10635. (newline))
  10636. (let ((col (current-column))
  10637. (name (plist-get plist :name)))
  10638. (insert "#+BEGIN: " name)
  10639. (while plist
  10640. (if (eq (car plist) :name)
  10641. (setq plist (cddr plist))
  10642. (insert " " (prin1-to-string (pop plist)))))
  10643. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10644. (beginning-of-line -2)))
  10645. (defun org-prepare-dblock ()
  10646. "Prepare dynamic block for refresh.
  10647. This empties the block, puts the cursor at the insert position and returns
  10648. the property list including an extra property :name with the block name."
  10649. (unless (looking-at org-dblock-start-re)
  10650. (error "Not at a dynamic block"))
  10651. (let* ((begdel (1+ (match-end 0)))
  10652. (name (org-no-properties (match-string 1)))
  10653. (params (append (list :name name)
  10654. (read (concat "(" (match-string 3) ")")))))
  10655. (save-excursion
  10656. (beginning-of-line 1)
  10657. (skip-chars-forward " \t")
  10658. (setq params (plist-put params :indentation-column (current-column))))
  10659. (unless (re-search-forward org-dblock-end-re nil t)
  10660. (error "Dynamic block not terminated"))
  10661. (setq params
  10662. (append params
  10663. (list :content (buffer-substring
  10664. begdel (match-beginning 0)))))
  10665. (delete-region begdel (match-beginning 0))
  10666. (goto-char begdel)
  10667. (open-line 1)
  10668. params))
  10669. (defun org-map-dblocks (&optional command)
  10670. "Apply COMMAND to all dynamic blocks in the current buffer.
  10671. If COMMAND is not given, use `org-update-dblock'."
  10672. (let ((cmd (or command 'org-update-dblock)))
  10673. (save-excursion
  10674. (goto-char (point-min))
  10675. (while (re-search-forward org-dblock-start-re nil t)
  10676. (goto-char (match-beginning 0))
  10677. (save-excursion
  10678. (condition-case nil
  10679. (funcall cmd)
  10680. (error (message "Error during update of dynamic block"))))
  10681. (unless (re-search-forward org-dblock-end-re nil t)
  10682. (error "Dynamic block not terminated"))))))
  10683. (defun org-dblock-update (&optional arg)
  10684. "User command for updating dynamic blocks.
  10685. Update the dynamic block at point. With prefix ARG, update all dynamic
  10686. blocks in the buffer."
  10687. (interactive "P")
  10688. (if arg
  10689. (org-update-all-dblocks)
  10690. (or (looking-at org-dblock-start-re)
  10691. (org-beginning-of-dblock))
  10692. (org-update-dblock)))
  10693. (defun org-update-dblock ()
  10694. "Update the dynamic block at point.
  10695. This means to empty the block, parse for parameters and then call
  10696. the correct writing function."
  10697. (interactive)
  10698. (save-window-excursion
  10699. (let* ((pos (point))
  10700. (line (org-current-line))
  10701. (params (org-prepare-dblock))
  10702. (name (plist-get params :name))
  10703. (indent (plist-get params :indentation-column))
  10704. (cmd (intern (concat "org-dblock-write:" name))))
  10705. (message "Updating dynamic block `%s' at line %d..." name line)
  10706. (funcall cmd params)
  10707. (message "Updating dynamic block `%s' at line %d...done" name line)
  10708. (goto-char pos)
  10709. (when (and indent (> indent 0))
  10710. (setq indent (make-string indent ?\ ))
  10711. (save-excursion
  10712. (org-beginning-of-dblock)
  10713. (forward-line 1)
  10714. (while (not (looking-at org-dblock-end-re))
  10715. (insert indent)
  10716. (beginning-of-line 2))
  10717. (when (looking-at org-dblock-end-re)
  10718. (and (looking-at "[ \t]+")
  10719. (replace-match ""))
  10720. (insert indent)))))))
  10721. (defun org-beginning-of-dblock ()
  10722. "Find the beginning of the dynamic block at point.
  10723. Error if there is no such block at point."
  10724. (let ((pos (point))
  10725. beg)
  10726. (end-of-line 1)
  10727. (if (and (re-search-backward org-dblock-start-re nil t)
  10728. (setq beg (match-beginning 0))
  10729. (re-search-forward org-dblock-end-re nil t)
  10730. (> (match-end 0) pos))
  10731. (goto-char beg)
  10732. (goto-char pos)
  10733. (error "Not in a dynamic block"))))
  10734. (defun org-update-all-dblocks ()
  10735. "Update all dynamic blocks in the buffer.
  10736. This function can be used in a hook."
  10737. (interactive)
  10738. (when (derived-mode-p 'org-mode)
  10739. (org-map-dblocks 'org-update-dblock)))
  10740. ;;;; Completion
  10741. (defun org-get-export-keywords ()
  10742. "Return a list of all currently understood export keywords.
  10743. Export keywords include options, block names, attributes and
  10744. keywords relative to each registered export back-end."
  10745. (delq nil
  10746. (let (keywords)
  10747. (mapc
  10748. (lambda (back-end)
  10749. (let ((props (cdr back-end)))
  10750. ;; Back-end name (for keywords, like #+LATEX:)
  10751. (push (upcase (symbol-name (car back-end))) keywords)
  10752. ;; Back-end options.
  10753. (mapc (lambda (option) (push (cadr option) keywords))
  10754. (plist-get (cdr back-end) :options-alist))))
  10755. (org-bound-and-true-p org-export-registered-backends))
  10756. keywords)))
  10757. (defconst org-options-keywords
  10758. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10759. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10760. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10761. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10762. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "TARGET:"))
  10763. (defcustom org-structure-template-alist
  10764. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10765. "<src lang=\"?\">\n\n</src>")
  10766. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10767. "<example>\n?\n</example>")
  10768. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10769. "<quote>\n?\n</quote>")
  10770. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10771. "<verse>\n?\n</verse>")
  10772. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10773. "<verbatim>\n?\n</verbatim>")
  10774. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10775. "<center>\n?\n</center>")
  10776. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10777. "<literal style=\"latex\">\n?\n</literal>")
  10778. ("L" "#+LaTeX: "
  10779. "<literal style=\"latex\">?</literal>")
  10780. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10781. "<literal style=\"html\">\n?\n</literal>")
  10782. ("H" "#+HTML: "
  10783. "<literal style=\"html\">?</literal>")
  10784. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10785. ("A" "#+ASCII: ")
  10786. ("i" "#+INDEX: ?"
  10787. "#+INDEX: ?")
  10788. ("I" "#+INCLUDE: %file ?"
  10789. "<include file=%file markup=\"?\">"))
  10790. "Structure completion elements.
  10791. This is a list of abbreviation keys and values. The value gets inserted
  10792. if you type `<' followed by the key and then press the completion key,
  10793. usually `M-TAB'. %file will be replaced by a file name after prompting
  10794. for the file using completion. The cursor will be placed at the position
  10795. of the `?` in the template.
  10796. There are two templates for each key, the first uses the original Org syntax,
  10797. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10798. the default when the /org-mtags.el/ module has been loaded. See also the
  10799. variable `org-mtags-prefer-muse-templates'."
  10800. :group 'org-completion
  10801. :type '(repeat
  10802. (string :tag "Key")
  10803. (string :tag "Template")
  10804. (string :tag "Muse Template")))
  10805. (defun org-try-structure-completion ()
  10806. "Try to complete a structure template before point.
  10807. This looks for strings like \"<e\" on an otherwise empty line and
  10808. expands them."
  10809. (let ((l (buffer-substring (point-at-bol) (point)))
  10810. a)
  10811. (when (and (looking-at "[ \t]*$")
  10812. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10813. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10814. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10815. (match-beginning 1)) a)
  10816. t)))
  10817. (defun org-complete-expand-structure-template (start cell)
  10818. "Expand a structure template."
  10819. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10820. (rpl (nth (if musep 2 1) cell))
  10821. (ind ""))
  10822. (delete-region start (point))
  10823. (when (string-match "\\`#\\+" rpl)
  10824. (cond
  10825. ((bolp))
  10826. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10827. (setq ind (buffer-substring (point-at-bol) (point))))
  10828. (t (newline))))
  10829. (setq start (point))
  10830. (if (string-match "%file" rpl)
  10831. (setq rpl (replace-match
  10832. (concat
  10833. "\""
  10834. (save-match-data
  10835. (abbreviate-file-name (read-file-name "Include file: ")))
  10836. "\"")
  10837. t t rpl)))
  10838. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10839. (concat "\n" ind)))
  10840. (insert rpl)
  10841. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10842. ;;;; TODO, DEADLINE, Comments
  10843. (defun org-toggle-comment ()
  10844. "Change the COMMENT state of an entry."
  10845. (interactive)
  10846. (save-excursion
  10847. (org-back-to-heading)
  10848. (let (case-fold-search)
  10849. (cond
  10850. ((looking-at (format org-heading-keyword-regexp-format
  10851. org-comment-string))
  10852. (goto-char (match-end 1))
  10853. (looking-at (concat " +" org-comment-string))
  10854. (replace-match "" t t)
  10855. (when (eolp) (insert " ")))
  10856. ((looking-at org-outline-regexp)
  10857. (goto-char (match-end 0))
  10858. (insert org-comment-string " "))))))
  10859. (defvar org-last-todo-state-is-todo nil
  10860. "This is non-nil when the last TODO state change led to a TODO state.
  10861. If the last change removed the TODO tag or switched to DONE, then
  10862. this is nil.")
  10863. (defvar org-setting-tags nil) ; dynamically skipped
  10864. (defvar org-todo-setup-filter-hook nil
  10865. "Hook for functions that pre-filter todo specs.
  10866. Each function takes a todo spec and returns either nil or the spec
  10867. transformed into canonical form." )
  10868. (defvar org-todo-get-default-hook nil
  10869. "Hook for functions that get a default item for todo.
  10870. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10871. nil or a string to be used for the todo mark." )
  10872. (defvar org-agenda-headline-snapshot-before-repeat)
  10873. (defun org-current-effective-time ()
  10874. "Return current time adjusted for `org-extend-today-until' variable."
  10875. (let* ((ct (org-current-time))
  10876. (dct (decode-time ct))
  10877. (ct1
  10878. (cond
  10879. (org-use-last-clock-out-time-as-effective-time
  10880. (or (org-clock-get-last-clock-out-time) ct))
  10881. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10882. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10883. (t ct))))
  10884. ct1))
  10885. (defun org-todo-yesterday (&optional arg)
  10886. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10887. (interactive "P")
  10888. (if (eq major-mode 'org-agenda-mode)
  10889. (apply 'org-agenda-todo-yesterday arg)
  10890. (let* ((hour (third (decode-time
  10891. (org-current-time))))
  10892. (org-extend-today-until (1+ hour)))
  10893. (org-todo arg))))
  10894. (defvar org-block-entry-blocking ""
  10895. "First entry preventing the TODO state change.")
  10896. (defun org-todo (&optional arg)
  10897. "Change the TODO state of an item.
  10898. The state of an item is given by a keyword at the start of the heading,
  10899. like
  10900. *** TODO Write paper
  10901. *** DONE Call mom
  10902. The different keywords are specified in the variable `org-todo-keywords'.
  10903. By default the available states are \"TODO\" and \"DONE\".
  10904. So for this example: when the item starts with TODO, it is changed to DONE.
  10905. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10906. DONE are present, add TODO at the beginning of the heading.
  10907. With \\[universal-argument] prefix arg, use completion to determine the new \
  10908. state.
  10909. With numeric prefix arg, switch to that state.
  10910. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10911. keywords (nextset).
  10912. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10913. With a numeric prefix arg of 0, inhibit note taking for the change.
  10914. For calling through lisp, arg is also interpreted in the following way:
  10915. 'none -> empty state
  10916. \"\"(empty string) -> switch to empty state
  10917. 'done -> switch to DONE
  10918. 'nextset -> switch to the next set of keywords
  10919. 'previousset -> switch to the previous set of keywords
  10920. \"WAITING\" -> switch to the specified keyword, but only if it
  10921. really is a member of `org-todo-keywords'."
  10922. (interactive "P")
  10923. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10924. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10925. 'region-start-level 'region))
  10926. org-loop-over-headlines-in-active-region)
  10927. (org-map-entries
  10928. `(org-todo ,arg)
  10929. org-loop-over-headlines-in-active-region
  10930. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10931. (if (equal arg '(16)) (setq arg 'nextset))
  10932. (let ((org-blocker-hook org-blocker-hook)
  10933. commentp
  10934. case-fold-search)
  10935. (when (equal arg '(64))
  10936. (setq arg nil org-blocker-hook nil))
  10937. (when (and org-blocker-hook
  10938. (or org-inhibit-blocking
  10939. (org-entry-get nil "NOBLOCKING")))
  10940. (setq org-blocker-hook nil))
  10941. (save-excursion
  10942. (catch 'exit
  10943. (org-back-to-heading t)
  10944. (when (looking-at (concat "^\\*+ " org-comment-string))
  10945. (org-toggle-comment)
  10946. (setq commentp t))
  10947. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10948. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10949. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10950. (let* ((match-data (match-data))
  10951. (startpos (point-at-bol))
  10952. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10953. (org-log-done org-log-done)
  10954. (org-log-repeat org-log-repeat)
  10955. (org-todo-log-states org-todo-log-states)
  10956. (org-inhibit-logging
  10957. (if (equal arg 0)
  10958. (progn (setq arg nil) 'note) org-inhibit-logging))
  10959. (this (match-string 1))
  10960. (hl-pos (match-beginning 0))
  10961. (head (org-get-todo-sequence-head this))
  10962. (ass (assoc head org-todo-kwd-alist))
  10963. (interpret (nth 1 ass))
  10964. (done-word (nth 3 ass))
  10965. (final-done-word (nth 4 ass))
  10966. (org-last-state (or this ""))
  10967. (completion-ignore-case t)
  10968. (member (member this org-todo-keywords-1))
  10969. (tail (cdr member))
  10970. (org-state (cond
  10971. ((and org-todo-key-trigger
  10972. (or (and (equal arg '(4))
  10973. (eq org-use-fast-todo-selection 'prefix))
  10974. (and (not arg) org-use-fast-todo-selection
  10975. (not (eq org-use-fast-todo-selection
  10976. 'prefix)))))
  10977. ;; Use fast selection
  10978. (org-fast-todo-selection))
  10979. ((and (equal arg '(4))
  10980. (or (not org-use-fast-todo-selection)
  10981. (not org-todo-key-trigger)))
  10982. ;; Read a state with completion
  10983. (org-icompleting-read
  10984. "State: " (mapcar 'list org-todo-keywords-1)
  10985. nil t))
  10986. ((eq arg 'right)
  10987. (if this
  10988. (if tail (car tail) nil)
  10989. (car org-todo-keywords-1)))
  10990. ((eq arg 'left)
  10991. (if (equal member org-todo-keywords-1)
  10992. nil
  10993. (if this
  10994. (nth (- (length org-todo-keywords-1)
  10995. (length tail) 2)
  10996. org-todo-keywords-1)
  10997. (org-last org-todo-keywords-1))))
  10998. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10999. (setq arg nil))) ; hack to fall back to cycling
  11000. (arg
  11001. ;; user or caller requests a specific state
  11002. (cond
  11003. ((equal arg "") nil)
  11004. ((eq arg 'none) nil)
  11005. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11006. ((eq arg 'nextset)
  11007. (or (car (cdr (member head org-todo-heads)))
  11008. (car org-todo-heads)))
  11009. ((eq arg 'previousset)
  11010. (let ((org-todo-heads (reverse org-todo-heads)))
  11011. (or (car (cdr (member head org-todo-heads)))
  11012. (car org-todo-heads))))
  11013. ((car (member arg org-todo-keywords-1)))
  11014. ((stringp arg)
  11015. (error "State `%s' not valid in this file" arg))
  11016. ((nth (1- (prefix-numeric-value arg))
  11017. org-todo-keywords-1))))
  11018. ((null member) (or head (car org-todo-keywords-1)))
  11019. ((equal this final-done-word) nil) ;; -> make empty
  11020. ((null tail) nil) ;; -> first entry
  11021. ((memq interpret '(type priority))
  11022. (if (eq this-command last-command)
  11023. (car tail)
  11024. (if (> (length tail) 0)
  11025. (or done-word (car org-done-keywords))
  11026. nil)))
  11027. (t
  11028. (car tail))))
  11029. (org-state (or
  11030. (run-hook-with-args-until-success
  11031. 'org-todo-get-default-hook org-state org-last-state)
  11032. org-state))
  11033. (next (if org-state (concat " " org-state " ") " "))
  11034. (change-plist (list :type 'todo-state-change :from this :to org-state
  11035. :position startpos))
  11036. dolog now-done-p)
  11037. (when org-blocker-hook
  11038. (setq org-last-todo-state-is-todo
  11039. (not (member this org-done-keywords)))
  11040. (unless (save-excursion
  11041. (save-match-data
  11042. (org-with-wide-buffer
  11043. (run-hook-with-args-until-failure
  11044. 'org-blocker-hook change-plist))))
  11045. (if (org-called-interactively-p 'interactive)
  11046. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11047. this org-state org-block-entry-blocking)
  11048. ;; fail silently
  11049. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11050. this org-state org-block-entry-blocking)
  11051. (throw 'exit nil))))
  11052. (store-match-data match-data)
  11053. (replace-match next t t)
  11054. (unless (pos-visible-in-window-p hl-pos)
  11055. (message "TODO state changed to %s" (org-trim next)))
  11056. (unless head
  11057. (setq head (org-get-todo-sequence-head org-state)
  11058. ass (assoc head org-todo-kwd-alist)
  11059. interpret (nth 1 ass)
  11060. done-word (nth 3 ass)
  11061. final-done-word (nth 4 ass)))
  11062. (when (memq arg '(nextset previousset))
  11063. (message "Keyword-Set %d/%d: %s"
  11064. (- (length org-todo-sets) -1
  11065. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11066. (length org-todo-sets)
  11067. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11068. (setq org-last-todo-state-is-todo
  11069. (not (member org-state org-done-keywords)))
  11070. (setq now-done-p (and (member org-state org-done-keywords)
  11071. (not (member this org-done-keywords))))
  11072. (and logging (org-local-logging logging))
  11073. (when (and (or org-todo-log-states org-log-done)
  11074. (not (eq org-inhibit-logging t))
  11075. (not (memq arg '(nextset previousset))))
  11076. ;; we need to look at recording a time and note
  11077. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11078. (nth 2 (assoc this org-todo-log-states))))
  11079. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11080. (setq dolog 'time))
  11081. (when (and org-state
  11082. (member org-state org-not-done-keywords)
  11083. (not (member this org-not-done-keywords)))
  11084. ;; This is now a todo state and was not one before
  11085. ;; If there was a CLOSED time stamp, get rid of it.
  11086. (org-add-planning-info nil nil 'closed))
  11087. (when (and now-done-p org-log-done)
  11088. ;; It is now done, and it was not done before
  11089. (org-add-planning-info 'closed (org-current-effective-time))
  11090. (if (and (not dolog) (eq 'note org-log-done))
  11091. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11092. (when (and org-state dolog)
  11093. ;; This is a non-nil state, and we need to log it
  11094. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11095. ;; Fixup tag positioning
  11096. (org-todo-trigger-tag-changes org-state)
  11097. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11098. (when org-provide-todo-statistics
  11099. (org-update-parent-todo-statistics))
  11100. (run-hooks 'org-after-todo-state-change-hook)
  11101. (if (and arg (not (member org-state org-done-keywords)))
  11102. (setq head (org-get-todo-sequence-head org-state)))
  11103. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11104. ;; Do we need to trigger a repeat?
  11105. (when now-done-p
  11106. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11107. ;; This is for the agenda, take a snapshot of the headline.
  11108. (save-match-data
  11109. (setq org-agenda-headline-snapshot-before-repeat
  11110. (org-get-heading))))
  11111. (org-auto-repeat-maybe org-state))
  11112. ;; Fixup cursor location if close to the keyword
  11113. (if (and (outline-on-heading-p)
  11114. (not (bolp))
  11115. (save-excursion (beginning-of-line 1)
  11116. (looking-at org-todo-line-regexp))
  11117. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11118. (progn
  11119. (goto-char (or (match-end 2) (match-end 1)))
  11120. (and (looking-at " ") (just-one-space))))
  11121. (when org-trigger-hook
  11122. (save-excursion
  11123. (run-hook-with-args 'org-trigger-hook change-plist)))
  11124. (when commentp (org-toggle-comment))))))))
  11125. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11126. "Block turning an entry into a TODO, using the hierarchy.
  11127. This checks whether the current task should be blocked from state
  11128. changes. Such blocking occurs when:
  11129. 1. The task has children which are not all in a completed state.
  11130. 2. A task has a parent with the property :ORDERED:, and there
  11131. are siblings prior to the current task with incomplete
  11132. status.
  11133. 3. The parent of the task is blocked because it has siblings that should
  11134. be done first, or is child of a block grandparent TODO entry."
  11135. (if (not org-enforce-todo-dependencies)
  11136. t ; if locally turned off don't block
  11137. (catch 'dont-block
  11138. ;; If this is not a todo state change, or if this entry is already DONE,
  11139. ;; do not block
  11140. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11141. (member (plist-get change-plist :from)
  11142. (cons 'done org-done-keywords))
  11143. (member (plist-get change-plist :to)
  11144. (cons 'todo org-not-done-keywords))
  11145. (not (plist-get change-plist :to)))
  11146. (throw 'dont-block t))
  11147. ;; If this task has children, and any are undone, it's blocked
  11148. (save-excursion
  11149. (org-back-to-heading t)
  11150. (let ((this-level (funcall outline-level)))
  11151. (outline-next-heading)
  11152. (let ((child-level (funcall outline-level)))
  11153. (while (and (not (eobp))
  11154. (> child-level this-level))
  11155. ;; this todo has children, check whether they are all
  11156. ;; completed
  11157. (if (and (not (org-entry-is-done-p))
  11158. (org-entry-is-todo-p))
  11159. (progn (setq org-block-entry-blocking (org-get-heading))
  11160. (throw 'dont-block nil)))
  11161. (outline-next-heading)
  11162. (setq child-level (funcall outline-level))))))
  11163. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11164. ;; any previous siblings are undone, it's blocked
  11165. (save-excursion
  11166. (org-back-to-heading t)
  11167. (let* ((pos (point))
  11168. (parent-pos (and (org-up-heading-safe) (point))))
  11169. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11170. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11171. (forward-line 1)
  11172. (re-search-forward org-not-done-heading-regexp pos t))
  11173. (setq org-block-entry-blocking (match-string 0))
  11174. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11175. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11176. (while t
  11177. (goto-char parent-pos)
  11178. (if (not (looking-at org-not-done-heading-regexp))
  11179. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11180. (setq pos (point))
  11181. (setq parent-pos (and (org-up-heading-safe) (point)))
  11182. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11183. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11184. (forward-line 1)
  11185. (re-search-forward org-not-done-heading-regexp pos t)
  11186. (setq org-block-entry-blocking (org-get-heading)))
  11187. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11188. (defcustom org-track-ordered-property-with-tag nil
  11189. "Should the ORDERED property also be shown as a tag?
  11190. The ORDERED property decides if an entry should require subtasks to be
  11191. completed in sequence. Since a property is not very visible, setting
  11192. this option means that toggling the ORDERED property with the command
  11193. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11194. not relevant for the behavior, but it makes things more visible.
  11195. Note that toggling the tag with tags commands will not change the property
  11196. and therefore not influence behavior!
  11197. This can be t, meaning the tag ORDERED should be used, It can also be a
  11198. string to select a different tag for this task."
  11199. :group 'org-todo
  11200. :type '(choice
  11201. (const :tag "No tracking" nil)
  11202. (const :tag "Track with ORDERED tag" t)
  11203. (string :tag "Use other tag")))
  11204. (defun org-toggle-ordered-property ()
  11205. "Toggle the ORDERED property of the current entry.
  11206. For better visibility, you can track the value of this property with a tag.
  11207. See variable `org-track-ordered-property-with-tag'."
  11208. (interactive)
  11209. (let* ((t1 org-track-ordered-property-with-tag)
  11210. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11211. (save-excursion
  11212. (org-back-to-heading)
  11213. (if (org-entry-get nil "ORDERED")
  11214. (progn
  11215. (org-delete-property "ORDERED" "PROPERTIES")
  11216. (and tag (org-toggle-tag tag 'off))
  11217. (message "Subtasks can be completed in arbitrary order"))
  11218. (org-entry-put nil "ORDERED" "t")
  11219. (and tag (org-toggle-tag tag 'on))
  11220. (message "Subtasks must be completed in sequence")))))
  11221. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11222. (defun org-block-todo-from-checkboxes (change-plist)
  11223. "Block turning an entry into a TODO, using checkboxes.
  11224. This checks whether the current task should be blocked from state
  11225. changes because there are unchecked boxes in this entry."
  11226. (if (not org-enforce-todo-checkbox-dependencies)
  11227. t ; if locally turned off don't block
  11228. (catch 'dont-block
  11229. ;; If this is not a todo state change, or if this entry is already DONE,
  11230. ;; do not block
  11231. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11232. (member (plist-get change-plist :from)
  11233. (cons 'done org-done-keywords))
  11234. (member (plist-get change-plist :to)
  11235. (cons 'todo org-not-done-keywords))
  11236. (not (plist-get change-plist :to)))
  11237. (throw 'dont-block t))
  11238. ;; If this task has checkboxes that are not checked, it's blocked
  11239. (save-excursion
  11240. (org-back-to-heading t)
  11241. (let ((beg (point)) end)
  11242. (outline-next-heading)
  11243. (setq end (point))
  11244. (goto-char beg)
  11245. (if (org-list-search-forward
  11246. (concat (org-item-beginning-re)
  11247. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11248. "\\[[- ]\\]")
  11249. end t)
  11250. (progn
  11251. (if (boundp 'org-blocked-by-checkboxes)
  11252. (setq org-blocked-by-checkboxes t))
  11253. (throw 'dont-block nil)))))
  11254. t))) ; do not block
  11255. (defun org-entry-blocked-p ()
  11256. "Is the current entry blocked?"
  11257. (org-with-silent-modifications
  11258. (if (org-entry-get nil "NOBLOCKING")
  11259. nil ;; Never block this entry
  11260. (not (run-hook-with-args-until-failure
  11261. 'org-blocker-hook
  11262. (list :type 'todo-state-change
  11263. :position (point)
  11264. :from 'todo
  11265. :to 'done))))))
  11266. (defun org-update-statistics-cookies (all)
  11267. "Update the statistics cookie, either from TODO or from checkboxes.
  11268. This should be called with the cursor in a line with a statistics cookie."
  11269. (interactive "P")
  11270. (if all
  11271. (progn
  11272. (org-update-checkbox-count 'all)
  11273. (org-map-entries 'org-update-parent-todo-statistics))
  11274. (if (not (org-at-heading-p))
  11275. (org-update-checkbox-count)
  11276. (let ((pos (point-marker))
  11277. end l1 l2)
  11278. (ignore-errors (org-back-to-heading t))
  11279. (if (not (org-at-heading-p))
  11280. (org-update-checkbox-count)
  11281. (setq l1 (org-outline-level))
  11282. (setq end (save-excursion
  11283. (outline-next-heading)
  11284. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11285. (point)))
  11286. (if (and (save-excursion
  11287. (re-search-forward
  11288. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11289. (not (save-excursion (re-search-forward
  11290. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11291. (org-update-checkbox-count)
  11292. (if (and l2 (> l2 l1))
  11293. (progn
  11294. (goto-char end)
  11295. (org-update-parent-todo-statistics))
  11296. (goto-char pos)
  11297. (beginning-of-line 1)
  11298. (while (re-search-forward
  11299. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11300. (point-at-eol) t)
  11301. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11302. (goto-char pos)
  11303. (move-marker pos nil)))))
  11304. (defvar org-entry-property-inherited-from) ;; defined below
  11305. (defun org-update-parent-todo-statistics ()
  11306. "Update any statistics cookie in the parent of the current headline.
  11307. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11308. the entire subtree and this will travel up the hierarchy and update
  11309. statistics everywhere."
  11310. (let* ((prop (save-excursion (org-up-heading-safe)
  11311. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11312. (recursive (or (not org-hierarchical-todo-statistics)
  11313. (and prop (string-match "\\<recursive\\>" prop))))
  11314. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11315. 0))
  11316. (first t)
  11317. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11318. level ltoggle l1 new ndel
  11319. (cnt-all 0) (cnt-done 0) is-percent kwd
  11320. checkbox-beg ov ovs ove cookie-present)
  11321. (catch 'exit
  11322. (save-excursion
  11323. (beginning-of-line 1)
  11324. (setq ltoggle (funcall outline-level))
  11325. ;; Three situations are to consider:
  11326. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11327. ;; to the top-level ancestor on the headline;
  11328. ;; 2. If parent has "recursive" property, repeat up to the
  11329. ;; headline setting that property, taking inheritance into
  11330. ;; account;
  11331. ;; 3. Else, move up to direct parent and proceed only once.
  11332. (while (and (setq level (org-up-heading-safe))
  11333. (or recursive first)
  11334. (>= (point) lim))
  11335. (setq first nil cookie-present nil)
  11336. (unless (and level
  11337. (not (string-match
  11338. "\\<checkbox\\>"
  11339. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11340. "")))))
  11341. (throw 'exit nil))
  11342. (while (re-search-forward box-re (point-at-eol) t)
  11343. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11344. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11345. (save-match-data
  11346. (unless (outline-next-heading) (throw 'exit nil))
  11347. (while (and (looking-at org-complex-heading-regexp)
  11348. (> (setq l1 (length (match-string 1))) level))
  11349. (setq kwd (and (or recursive (= l1 ltoggle))
  11350. (match-string 2)))
  11351. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11352. (and (listp org-provide-todo-statistics)
  11353. (or (member kwd org-provide-todo-statistics)
  11354. (member kwd org-done-keywords))))
  11355. (setq cnt-all (1+ cnt-all))
  11356. (if (eq org-provide-todo-statistics t)
  11357. (and kwd (setq cnt-all (1+ cnt-all)))))
  11358. (and (member kwd org-done-keywords)
  11359. (setq cnt-done (1+ cnt-done)))
  11360. (outline-next-heading)))
  11361. (setq new
  11362. (if is-percent
  11363. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11364. (format "[%d/%d]" cnt-done cnt-all))
  11365. ndel (- (match-end 0) checkbox-beg))
  11366. ;; handle overlays when updating cookie from column view
  11367. (when (setq ov (car (overlays-at checkbox-beg)))
  11368. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11369. (delete-overlay ov))
  11370. (goto-char checkbox-beg)
  11371. (insert new)
  11372. (delete-region (point) (+ (point) ndel))
  11373. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11374. (when ov (move-overlay ov ovs ove)))
  11375. (when cookie-present
  11376. (run-hook-with-args 'org-after-todo-statistics-hook
  11377. cnt-done (- cnt-all cnt-done))))))
  11378. (run-hooks 'org-todo-statistics-hook)))
  11379. (defvar org-after-todo-statistics-hook nil
  11380. "Hook that is called after a TODO statistics cookie has been updated.
  11381. Each function is called with two arguments: the number of not-done entries
  11382. and the number of done entries.
  11383. For example, the following function, when added to this hook, will switch
  11384. an entry to DONE when all children are done, and back to TODO when new
  11385. entries are set to a TODO status. Note that this hook is only called
  11386. when there is a statistics cookie in the headline!
  11387. (defun org-summary-todo (n-done n-not-done)
  11388. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11389. (let (org-log-done org-log-states) ; turn off logging
  11390. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11391. ")
  11392. (defvar org-todo-statistics-hook nil
  11393. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11394. This hook runs even if there is no statistics cookie present, in which case
  11395. `org-after-todo-statistics-hook' would not run.")
  11396. (defun org-todo-trigger-tag-changes (state)
  11397. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11398. (let ((l org-todo-state-tags-triggers)
  11399. changes)
  11400. (when (or (not state) (equal state ""))
  11401. (setq changes (append changes (cdr (assoc "" l)))))
  11402. (when (and (stringp state) (> (length state) 0))
  11403. (setq changes (append changes (cdr (assoc state l)))))
  11404. (when (member state org-not-done-keywords)
  11405. (setq changes (append changes (cdr (assoc 'todo l)))))
  11406. (when (member state org-done-keywords)
  11407. (setq changes (append changes (cdr (assoc 'done l)))))
  11408. (dolist (c changes)
  11409. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11410. (defun org-local-logging (value)
  11411. "Get logging settings from a property VALUE."
  11412. (let* (words w a)
  11413. ;; directly set the variables, they are already local.
  11414. (setq org-log-done nil
  11415. org-log-repeat nil
  11416. org-todo-log-states nil)
  11417. (setq words (org-split-string value))
  11418. (while (setq w (pop words))
  11419. (cond
  11420. ((setq a (assoc w org-startup-options))
  11421. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11422. (set (nth 1 a) (nth 2 a))))
  11423. ((setq a (org-extract-log-state-settings w))
  11424. (and (member (car a) org-todo-keywords-1)
  11425. (push a org-todo-log-states)))))))
  11426. (defun org-get-todo-sequence-head (kwd)
  11427. "Return the head of the TODO sequence to which KWD belongs.
  11428. If KWD is not set, check if there is a text property remembering the
  11429. right sequence."
  11430. (let (p)
  11431. (cond
  11432. ((not kwd)
  11433. (or (get-text-property (point-at-bol) 'org-todo-head)
  11434. (progn
  11435. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11436. nil (point-at-eol)))
  11437. (get-text-property p 'org-todo-head))))
  11438. ((not (member kwd org-todo-keywords-1))
  11439. (car org-todo-keywords-1))
  11440. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11441. (defun org-fast-todo-selection ()
  11442. "Fast TODO keyword selection with single keys.
  11443. Returns the new TODO keyword, or nil if no state change should occur."
  11444. (let* ((fulltable org-todo-key-alist)
  11445. (done-keywords org-done-keywords) ;; needed for the faces.
  11446. (maxlen (apply 'max (mapcar
  11447. (lambda (x)
  11448. (if (stringp (car x)) (string-width (car x)) 0))
  11449. fulltable)))
  11450. (expert nil)
  11451. (fwidth (+ maxlen 3 1 3))
  11452. (ncol (/ (- (window-width) 4) fwidth))
  11453. tg cnt e c tbl
  11454. groups ingroup)
  11455. (save-excursion
  11456. (save-window-excursion
  11457. (if expert
  11458. (set-buffer (get-buffer-create " *Org todo*"))
  11459. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11460. (erase-buffer)
  11461. (org-set-local 'org-done-keywords done-keywords)
  11462. (setq tbl fulltable cnt 0)
  11463. (while (setq e (pop tbl))
  11464. (cond
  11465. ((equal e '(:startgroup))
  11466. (push '() groups) (setq ingroup t)
  11467. (when (not (= cnt 0))
  11468. (setq cnt 0)
  11469. (insert "\n"))
  11470. (insert "{ "))
  11471. ((equal e '(:endgroup))
  11472. (setq ingroup nil cnt 0)
  11473. (insert "}\n"))
  11474. ((equal e '(:newline))
  11475. (when (not (= cnt 0))
  11476. (setq cnt 0)
  11477. (insert "\n")
  11478. (setq e (car tbl))
  11479. (while (equal (car tbl) '(:newline))
  11480. (insert "\n")
  11481. (setq tbl (cdr tbl)))))
  11482. (t
  11483. (setq tg (car e) c (cdr e))
  11484. (if ingroup (push tg (car groups)))
  11485. (setq tg (org-add-props tg nil 'face
  11486. (org-get-todo-face tg)))
  11487. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11488. (insert "[" c "] " tg (make-string
  11489. (- fwidth 4 (length tg)) ?\ ))
  11490. (when (= (setq cnt (1+ cnt)) ncol)
  11491. (insert "\n")
  11492. (if ingroup (insert " "))
  11493. (setq cnt 0)))))
  11494. (insert "\n")
  11495. (goto-char (point-min))
  11496. (if (not expert) (org-fit-window-to-buffer))
  11497. (message "[a-z..]:Set [SPC]:clear")
  11498. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11499. (cond
  11500. ((or (= c ?\C-g)
  11501. (and (= c ?q) (not (rassoc c fulltable))))
  11502. (setq quit-flag t))
  11503. ((= c ?\ ) nil)
  11504. ((setq e (rassoc c fulltable) tg (car e))
  11505. tg)
  11506. (t (setq quit-flag t)))))))
  11507. (defun org-entry-is-todo-p ()
  11508. (member (org-get-todo-state) org-not-done-keywords))
  11509. (defun org-entry-is-done-p ()
  11510. (member (org-get-todo-state) org-done-keywords))
  11511. (defun org-get-todo-state ()
  11512. (save-excursion
  11513. (org-back-to-heading t)
  11514. (and (looking-at org-todo-line-regexp)
  11515. (match-end 2)
  11516. (match-string 2))))
  11517. (defun org-at-date-range-p (&optional inactive-ok)
  11518. "Is the cursor inside a date range?"
  11519. (interactive)
  11520. (save-excursion
  11521. (catch 'exit
  11522. (let ((pos (point)))
  11523. (skip-chars-backward "^[<\r\n")
  11524. (skip-chars-backward "<[")
  11525. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11526. (>= (match-end 0) pos)
  11527. (throw 'exit t))
  11528. (skip-chars-backward "^<[\r\n")
  11529. (skip-chars-backward "<[")
  11530. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11531. (>= (match-end 0) pos)
  11532. (throw 'exit t)))
  11533. nil)))
  11534. (defun org-get-repeat (&optional tagline)
  11535. "Check if there is a deadline/schedule with repeater in this entry."
  11536. (save-match-data
  11537. (save-excursion
  11538. (org-back-to-heading t)
  11539. (and (re-search-forward (if tagline
  11540. (concat tagline "\\s-*" org-repeat-re)
  11541. org-repeat-re)
  11542. (org-entry-end-position) t)
  11543. (match-string-no-properties 1)))))
  11544. (defvar org-last-changed-timestamp)
  11545. (defvar org-last-inserted-timestamp)
  11546. (defvar org-log-post-message)
  11547. (defvar org-log-note-purpose)
  11548. (defvar org-log-note-how)
  11549. (defvar org-log-note-extra)
  11550. (defun org-auto-repeat-maybe (done-word)
  11551. "Check if the current headline contains a repeated deadline/schedule.
  11552. If yes, set TODO state back to what it was and change the base date
  11553. of repeating deadline/scheduled time stamps to new date.
  11554. This function is run automatically after each state change to a DONE state."
  11555. ;; last-state is dynamically scoped into this function
  11556. (let* ((repeat (org-get-repeat))
  11557. (aa (assoc org-last-state org-todo-kwd-alist))
  11558. (interpret (nth 1 aa))
  11559. (head (nth 2 aa))
  11560. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11561. (msg "Entry repeats: ")
  11562. (org-log-done nil)
  11563. (org-todo-log-states nil)
  11564. re type n what ts time to-state)
  11565. (when repeat
  11566. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11567. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11568. org-todo-repeat-to-state))
  11569. (unless (and to-state (member to-state org-todo-keywords-1))
  11570. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11571. (org-todo to-state)
  11572. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11573. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11574. (org-time-stamp-format t t))))
  11575. (when org-log-repeat
  11576. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11577. (memq 'org-add-log-note post-command-hook))
  11578. ;; OK, we are already setup for some record
  11579. (if (eq org-log-repeat 'note)
  11580. ;; make sure we take a note, not only a time stamp
  11581. (setq org-log-note-how 'note))
  11582. ;; Set up for taking a record
  11583. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11584. org-last-state
  11585. 'findpos org-log-repeat)))
  11586. (org-back-to-heading t)
  11587. (org-add-planning-info nil nil 'closed)
  11588. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11589. org-deadline-time-regexp "\\)\\|\\("
  11590. org-ts-regexp "\\)"))
  11591. (while (re-search-forward
  11592. re (save-excursion (outline-next-heading) (point)) t)
  11593. (setq type (if (match-end 1) org-scheduled-string
  11594. (if (match-end 3) org-deadline-string "Plain:"))
  11595. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11596. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11597. (setq n (string-to-number (match-string 2 ts))
  11598. what (match-string 3 ts))
  11599. (if (equal what "w") (setq n (* n 7) what "d"))
  11600. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11601. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11602. ;; Preparation, see if we need to modify the start date for the change
  11603. (when (match-end 1)
  11604. (setq time (save-match-data (org-time-string-to-time ts)))
  11605. (cond
  11606. ((equal (match-string 1 ts) ".")
  11607. ;; Shift starting date to today
  11608. (org-timestamp-change
  11609. (- (org-today) (time-to-days time))
  11610. 'day))
  11611. ((equal (match-string 1 ts) "+")
  11612. (let ((nshiftmax 10) (nshift 0))
  11613. (while (or (= nshift 0)
  11614. (<= (time-to-days time)
  11615. (time-to-days (current-time))))
  11616. (when (= (incf nshift) nshiftmax)
  11617. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11618. (error "Abort")))
  11619. (org-timestamp-change n (cdr (assoc what whata)))
  11620. (org-at-timestamp-p t)
  11621. (setq ts (match-string 1))
  11622. (setq time (save-match-data (org-time-string-to-time ts)))))
  11623. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11624. ;; rematch, so that we have everything in place for the real shift
  11625. (org-at-timestamp-p t)
  11626. (setq ts (match-string 1))
  11627. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11628. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11629. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11630. (setq org-log-post-message msg)
  11631. (message "%s" msg))))
  11632. (defun org-show-todo-tree (arg)
  11633. "Make a compact tree which shows all headlines marked with TODO.
  11634. The tree will show the lines where the regexp matches, and all higher
  11635. headlines above the match.
  11636. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11637. With a numeric prefix N, construct a sparse tree for the Nth element
  11638. of `org-todo-keywords-1'."
  11639. (interactive "P")
  11640. (let ((case-fold-search nil)
  11641. (kwd-re
  11642. (cond ((null arg) org-not-done-regexp)
  11643. ((equal arg '(4))
  11644. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11645. (mapcar 'list org-todo-keywords-1))))
  11646. (concat "\\("
  11647. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11648. "\\)\\>")))
  11649. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11650. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11651. org-todo-keywords-1)))
  11652. (t (error "Invalid prefix argument: %s" arg)))))
  11653. (message "%d TODO entries found"
  11654. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11655. (defun org-deadline (arg &optional time)
  11656. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11657. With one universal prefix argument, remove any deadline from the item.
  11658. With two universal prefix arguments, prompt for a warning delay.
  11659. With argument TIME, set the deadline at the corresponding date. TIME
  11660. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11661. (interactive "P")
  11662. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11663. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11664. 'region-start-level 'region))
  11665. org-loop-over-headlines-in-active-region)
  11666. (org-map-entries
  11667. `(org-deadline ',arg ,time)
  11668. org-loop-over-headlines-in-active-region
  11669. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11670. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11671. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11672. (repeater (and old-date
  11673. (string-match
  11674. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11675. old-date)
  11676. (match-string 1 old-date))))
  11677. (cond
  11678. ((equal arg '(4))
  11679. (when (and old-date org-log-redeadline)
  11680. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11681. org-log-redeadline))
  11682. (org-remove-timestamp-with-keyword org-deadline-string)
  11683. (message "Item no longer has a deadline."))
  11684. ((equal arg '(16))
  11685. (save-excursion
  11686. (if (re-search-forward
  11687. org-deadline-time-regexp
  11688. (save-excursion (outline-next-heading) (point)) t)
  11689. (let* ((rpl0 (match-string 1))
  11690. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11691. (replace-match
  11692. (concat org-deadline-string
  11693. " <" rpl
  11694. (format " -%dd"
  11695. (abs
  11696. (- (time-to-days
  11697. (save-match-data
  11698. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11699. (time-to-days old-date-time))))
  11700. ">") t t))
  11701. (user-error "No deadline information to update"))))
  11702. (t
  11703. (org-add-planning-info 'deadline time 'closed)
  11704. (when (and old-date org-log-redeadline
  11705. (not (equal old-date
  11706. (substring org-last-inserted-timestamp 1 -1))))
  11707. (org-add-log-setup 'redeadline nil old-date 'findpos
  11708. org-log-redeadline))
  11709. (when repeater
  11710. (save-excursion
  11711. (org-back-to-heading t)
  11712. (when (re-search-forward (concat org-deadline-string " "
  11713. org-last-inserted-timestamp)
  11714. (save-excursion
  11715. (outline-next-heading) (point)) t)
  11716. (goto-char (1- (match-end 0)))
  11717. (insert " " repeater)
  11718. (setq org-last-inserted-timestamp
  11719. (concat (substring org-last-inserted-timestamp 0 -1)
  11720. " " repeater
  11721. (substring org-last-inserted-timestamp -1))))))
  11722. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11723. (defun org-schedule (arg &optional time)
  11724. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11725. With one universal prefix argument, remove any scheduling date from the item.
  11726. With two universal prefix arguments, prompt for a delay cookie.
  11727. With argument TIME, scheduled at the corresponding date. TIME can
  11728. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11729. (interactive "P")
  11730. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11731. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11732. 'region-start-level 'region))
  11733. org-loop-over-headlines-in-active-region)
  11734. (org-map-entries
  11735. `(org-schedule ',arg ,time)
  11736. org-loop-over-headlines-in-active-region
  11737. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11738. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11739. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11740. (repeater (and old-date
  11741. (string-match
  11742. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11743. old-date)
  11744. (match-string 1 old-date))))
  11745. (cond
  11746. ((equal arg '(4))
  11747. (progn
  11748. (when (and old-date org-log-reschedule)
  11749. (org-add-log-setup 'delschedule nil old-date 'findpos
  11750. org-log-reschedule))
  11751. (org-remove-timestamp-with-keyword org-scheduled-string)
  11752. (message "Item is no longer scheduled.")))
  11753. ((equal arg '(16))
  11754. (save-excursion
  11755. (if (re-search-forward
  11756. org-scheduled-time-regexp
  11757. (save-excursion (outline-next-heading) (point)) t)
  11758. (let* ((rpl0 (match-string 1))
  11759. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11760. (replace-match
  11761. (concat org-scheduled-string
  11762. " <" rpl
  11763. (format " -%dd"
  11764. (abs
  11765. (- (time-to-days
  11766. (save-match-data
  11767. (org-read-date nil t nil "Delay until" old-date-time)))
  11768. (time-to-days old-date-time))))
  11769. ">") t t))
  11770. (user-error "No scheduled information to update"))))
  11771. (t
  11772. (org-add-planning-info 'scheduled time 'closed)
  11773. (when (and old-date org-log-reschedule
  11774. (not (equal old-date
  11775. (substring org-last-inserted-timestamp 1 -1))))
  11776. (org-add-log-setup 'reschedule nil old-date 'findpos
  11777. org-log-reschedule))
  11778. (when repeater
  11779. (save-excursion
  11780. (org-back-to-heading t)
  11781. (when (re-search-forward (concat org-scheduled-string " "
  11782. org-last-inserted-timestamp)
  11783. (save-excursion
  11784. (outline-next-heading) (point)) t)
  11785. (goto-char (1- (match-end 0)))
  11786. (insert " " repeater)
  11787. (setq org-last-inserted-timestamp
  11788. (concat (substring org-last-inserted-timestamp 0 -1)
  11789. " " repeater
  11790. (substring org-last-inserted-timestamp -1))))))
  11791. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11792. (defun org-get-scheduled-time (pom &optional inherit)
  11793. "Get the scheduled time as a time tuple, of a format suitable
  11794. for calling org-schedule with, or if there is no scheduling,
  11795. returns nil."
  11796. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11797. (when time
  11798. (apply 'encode-time (org-parse-time-string time)))))
  11799. (defun org-get-deadline-time (pom &optional inherit)
  11800. "Get the deadline as a time tuple, of a format suitable for
  11801. calling org-deadline with, or if there is no scheduling, returns
  11802. nil."
  11803. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11804. (when time
  11805. (apply 'encode-time (org-parse-time-string time)))))
  11806. (defun org-remove-timestamp-with-keyword (keyword)
  11807. "Remove all time stamps with KEYWORD in the current entry."
  11808. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11809. beg)
  11810. (save-excursion
  11811. (org-back-to-heading t)
  11812. (setq beg (point))
  11813. (outline-next-heading)
  11814. (while (re-search-backward re beg t)
  11815. (replace-match "")
  11816. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11817. (equal (char-before) ?\ ))
  11818. (backward-delete-char 1)
  11819. (if (string-match "^[ \t]*$" (buffer-substring
  11820. (point-at-bol) (point-at-eol)))
  11821. (delete-region (point-at-bol)
  11822. (min (point-max) (1+ (point-at-eol))))))))))
  11823. (defun org-add-planning-info (what &optional time &rest remove)
  11824. "Insert new timestamp with keyword in the line directly after the headline.
  11825. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11826. If non is given, the user is prompted for a date.
  11827. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11828. be removed."
  11829. (interactive)
  11830. (let (org-time-was-given org-end-time-was-given ts
  11831. end default-time default-input)
  11832. (catch 'exit
  11833. (when (and (memq what '(scheduled deadline))
  11834. (or (not time)
  11835. (and (stringp time)
  11836. (string-match "^[-+]+[0-9]" time))))
  11837. ;; Try to get a default date/time from existing timestamp
  11838. (save-excursion
  11839. (org-back-to-heading t)
  11840. (setq end (save-excursion (outline-next-heading) (point)))
  11841. (when (re-search-forward (if (eq what 'scheduled)
  11842. org-scheduled-time-regexp
  11843. org-deadline-time-regexp)
  11844. end t)
  11845. (setq ts (match-string 1)
  11846. default-time
  11847. (apply 'encode-time (org-parse-time-string ts))
  11848. default-input (and ts (org-get-compact-tod ts))))))
  11849. (when what
  11850. (setq time
  11851. (if (stringp time)
  11852. ;; This is a string (relative or absolute), set proper date
  11853. (apply 'encode-time
  11854. (org-read-date-analyze
  11855. time default-time (decode-time default-time)))
  11856. ;; If necessary, get the time from the user
  11857. (or time (org-read-date nil 'to-time nil nil
  11858. default-time default-input)))))
  11859. (when (and org-insert-labeled-timestamps-at-point
  11860. (member what '(scheduled deadline)))
  11861. (insert
  11862. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11863. (org-insert-time-stamp time org-time-was-given
  11864. nil nil nil (list org-end-time-was-given))
  11865. (setq what nil))
  11866. (save-excursion
  11867. (save-restriction
  11868. (let (col list elt ts buffer-invisibility-spec)
  11869. (org-back-to-heading t)
  11870. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11871. (goto-char (match-end 1))
  11872. (setq col (current-column))
  11873. (goto-char (match-end 0))
  11874. (if (eobp) (insert "\n") (forward-char 1))
  11875. (when (and (not what)
  11876. (not (looking-at
  11877. (concat "[ \t]*"
  11878. org-keyword-time-not-clock-regexp))))
  11879. ;; Nothing to add, nothing to remove...... :-)
  11880. (throw 'exit nil))
  11881. (if (and (not (looking-at org-outline-regexp))
  11882. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11883. "[^\r\n]*"))
  11884. (not (equal (match-string 1) org-clock-string)))
  11885. (narrow-to-region (match-beginning 0) (match-end 0))
  11886. (insert-before-markers "\n")
  11887. (backward-char 1)
  11888. (narrow-to-region (point) (point))
  11889. (and org-adapt-indentation (org-indent-to-column col)))
  11890. ;; Check if we have to remove something.
  11891. (setq list (cons what remove))
  11892. (while list
  11893. (setq elt (pop list))
  11894. (when (or (and (eq elt 'scheduled)
  11895. (re-search-forward org-scheduled-time-regexp nil t))
  11896. (and (eq elt 'deadline)
  11897. (re-search-forward org-deadline-time-regexp nil t))
  11898. (and (eq elt 'closed)
  11899. (re-search-forward org-closed-time-regexp nil t)))
  11900. (replace-match "")
  11901. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11902. (and (looking-at "[ \t]+") (replace-match ""))
  11903. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11904. (when what
  11905. (insert
  11906. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11907. (cond ((eq what 'scheduled) org-scheduled-string)
  11908. ((eq what 'deadline) org-deadline-string)
  11909. ((eq what 'closed) org-closed-string))
  11910. " ")
  11911. (setq ts (org-insert-time-stamp
  11912. time
  11913. (or org-time-was-given
  11914. (and (eq what 'closed) org-log-done-with-time))
  11915. (eq what 'closed)
  11916. nil nil (list org-end-time-was-given)))
  11917. (insert
  11918. (if (not (or (bolp) (eq (char-before) ?\ )
  11919. (memq (char-after) '(32 10))
  11920. (eobp))) " " ""))
  11921. (end-of-line 1))
  11922. (goto-char (point-min))
  11923. (widen)
  11924. (if (and (looking-at "[ \t]*\n")
  11925. (equal (char-before) ?\n))
  11926. (delete-region (1- (point)) (point-at-eol)))
  11927. ts))))))
  11928. (defvar org-log-note-marker (make-marker))
  11929. (defvar org-log-note-purpose nil)
  11930. (defvar org-log-note-state nil)
  11931. (defvar org-log-note-previous-state nil)
  11932. (defvar org-log-note-how nil)
  11933. (defvar org-log-note-extra nil)
  11934. (defvar org-log-note-window-configuration nil)
  11935. (defvar org-log-note-return-to (make-marker))
  11936. (defvar org-log-note-effective-time nil
  11937. "Remembered current time so that dynamically scoped
  11938. `org-extend-today-until' affects tha timestamps in state change
  11939. log")
  11940. (defvar org-log-post-message nil
  11941. "Message to be displayed after a log note has been stored.
  11942. The auto-repeater uses this.")
  11943. (defun org-add-note ()
  11944. "Add a note to the current entry.
  11945. This is done in the same way as adding a state change note."
  11946. (interactive)
  11947. (org-add-log-setup 'note nil nil 'findpos nil))
  11948. (defvar org-property-end-re)
  11949. (defun org-add-log-setup (&optional purpose state prev-state
  11950. findpos how extra)
  11951. "Set up the post command hook to take a note.
  11952. If this is about to TODO state change, the new state is expected in STATE.
  11953. When FINDPOS is non-nil, find the correct position for the note in
  11954. the current entry. If not, assume that it can be inserted at point.
  11955. HOW is an indicator what kind of note should be created.
  11956. EXTRA is additional text that will be inserted into the notes buffer."
  11957. (let* ((org-log-into-drawer (org-log-into-drawer))
  11958. (drawer (cond ((stringp org-log-into-drawer)
  11959. org-log-into-drawer)
  11960. (org-log-into-drawer "LOGBOOK"))))
  11961. (save-restriction
  11962. (save-excursion
  11963. (when findpos
  11964. (org-back-to-heading t)
  11965. (narrow-to-region (point) (save-excursion
  11966. (outline-next-heading) (point)))
  11967. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11968. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11969. "[^\r\n]*\\)?"))
  11970. (goto-char (match-end 0))
  11971. (cond
  11972. (drawer
  11973. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11974. nil t)
  11975. (progn
  11976. (goto-char (match-end 0))
  11977. (or org-log-states-order-reversed
  11978. (and (re-search-forward org-property-end-re nil t)
  11979. (goto-char (1- (match-beginning 0))))))
  11980. (insert "\n:" drawer ":\n:END:")
  11981. (beginning-of-line 0)
  11982. (org-indent-line)
  11983. (beginning-of-line 2)
  11984. (org-indent-line)
  11985. (end-of-line 0)))
  11986. ((and org-log-state-notes-insert-after-drawers
  11987. (save-excursion
  11988. (forward-line) (looking-at org-drawer-regexp)))
  11989. (forward-line)
  11990. (while (looking-at org-drawer-regexp)
  11991. (goto-char (match-end 0))
  11992. (re-search-forward org-property-end-re (point-max) t)
  11993. (forward-line))
  11994. (forward-line -1)))
  11995. (unless org-log-states-order-reversed
  11996. (and (= (char-after) ?\n) (forward-char 1))
  11997. (org-skip-over-state-notes)
  11998. (skip-chars-backward " \t\n\r")))
  11999. (move-marker org-log-note-marker (point))
  12000. (setq org-log-note-purpose purpose
  12001. org-log-note-state state
  12002. org-log-note-previous-state prev-state
  12003. org-log-note-how how
  12004. org-log-note-extra extra
  12005. org-log-note-effective-time (org-current-effective-time))
  12006. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12007. (defun org-skip-over-state-notes ()
  12008. "Skip past the list of State notes in an entry."
  12009. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12010. (when (ignore-errors (goto-char (org-in-item-p)))
  12011. (let* ((struct (org-list-struct))
  12012. (prevs (org-list-prevs-alist struct)))
  12013. (while (looking-at "[ \t]*- State")
  12014. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12015. (org-list-get-item-end (point) struct)))))))
  12016. (defun org-add-log-note (&optional purpose)
  12017. "Pop up a window for taking a note, and add this note later at point."
  12018. (remove-hook 'post-command-hook 'org-add-log-note)
  12019. (setq org-log-note-window-configuration (current-window-configuration))
  12020. (delete-other-windows)
  12021. (move-marker org-log-note-return-to (point))
  12022. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12023. (goto-char org-log-note-marker)
  12024. (org-switch-to-buffer-other-window "*Org Note*")
  12025. (erase-buffer)
  12026. (if (memq org-log-note-how '(time state))
  12027. (let (current-prefix-arg) (org-store-log-note))
  12028. (let ((org-inhibit-startup t)) (org-mode))
  12029. (insert (format "# Insert note for %s.
  12030. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12031. (cond
  12032. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12033. ((eq org-log-note-purpose 'done) "closed todo item")
  12034. ((eq org-log-note-purpose 'state)
  12035. (format "state change from \"%s\" to \"%s\""
  12036. (or org-log-note-previous-state "")
  12037. (or org-log-note-state "")))
  12038. ((eq org-log-note-purpose 'reschedule)
  12039. "rescheduling")
  12040. ((eq org-log-note-purpose 'delschedule)
  12041. "no longer scheduled")
  12042. ((eq org-log-note-purpose 'redeadline)
  12043. "changing deadline")
  12044. ((eq org-log-note-purpose 'deldeadline)
  12045. "removing deadline")
  12046. ((eq org-log-note-purpose 'refile)
  12047. "refiling")
  12048. ((eq org-log-note-purpose 'note)
  12049. "this entry")
  12050. (t (error "This should not happen")))))
  12051. (if org-log-note-extra (insert org-log-note-extra))
  12052. (org-set-local 'org-finish-function 'org-store-log-note)
  12053. (run-hooks 'org-log-buffer-setup-hook)))
  12054. (defvar org-note-abort nil) ; dynamically scoped
  12055. (defun org-store-log-note ()
  12056. "Finish taking a log note, and insert it to where it belongs."
  12057. (let ((txt (buffer-string)))
  12058. (kill-buffer (current-buffer))
  12059. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12060. lines ind bul)
  12061. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12062. (setq txt (replace-match "" t t txt)))
  12063. (if (string-match "\\s-+\\'" txt)
  12064. (setq txt (replace-match "" t t txt)))
  12065. (setq lines (org-split-string txt "\n"))
  12066. (when (and note (string-match "\\S-" note))
  12067. (setq note
  12068. (org-replace-escapes
  12069. note
  12070. (list (cons "%u" (user-login-name))
  12071. (cons "%U" user-full-name)
  12072. (cons "%t" (format-time-string
  12073. (org-time-stamp-format 'long 'inactive)
  12074. org-log-note-effective-time))
  12075. (cons "%T" (format-time-string
  12076. (org-time-stamp-format 'long nil)
  12077. org-log-note-effective-time))
  12078. (cons "%d" (format-time-string
  12079. (org-time-stamp-format nil 'inactive)
  12080. org-log-note-effective-time))
  12081. (cons "%D" (format-time-string
  12082. (org-time-stamp-format nil nil)
  12083. org-log-note-effective-time))
  12084. (cons "%s" (if org-log-note-state
  12085. (concat "\"" org-log-note-state "\"")
  12086. ""))
  12087. (cons "%S" (if org-log-note-previous-state
  12088. (concat "\"" org-log-note-previous-state "\"")
  12089. "\"\"")))))
  12090. (if lines (setq note (concat note " \\\\")))
  12091. (push note lines))
  12092. (when (or current-prefix-arg org-note-abort)
  12093. (when org-log-into-drawer
  12094. (org-remove-empty-drawer-at
  12095. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12096. org-log-note-marker))
  12097. (setq lines nil))
  12098. (when lines
  12099. (with-current-buffer (marker-buffer org-log-note-marker)
  12100. (save-excursion
  12101. (goto-char org-log-note-marker)
  12102. (move-marker org-log-note-marker nil)
  12103. (end-of-line 1)
  12104. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12105. (setq ind (save-excursion
  12106. (if (ignore-errors (goto-char (org-in-item-p)))
  12107. (let ((struct (org-list-struct)))
  12108. (org-list-get-ind
  12109. (org-list-get-top-point struct) struct))
  12110. (skip-chars-backward " \r\t\n")
  12111. (cond
  12112. ((and (org-at-heading-p)
  12113. org-adapt-indentation)
  12114. (1+ (org-current-level)))
  12115. ((org-at-heading-p) 0)
  12116. (t (org-get-indentation))))))
  12117. (setq bul (org-list-bullet-string "-"))
  12118. (org-indent-line-to ind)
  12119. (insert bul (pop lines))
  12120. (let ((ind-body (+ (length bul) ind)))
  12121. (while lines
  12122. (insert "\n")
  12123. (org-indent-line-to ind-body)
  12124. (insert (pop lines))))
  12125. (message "Note stored")
  12126. (org-back-to-heading t)
  12127. (org-cycle-hide-drawers 'children))))))
  12128. (set-window-configuration org-log-note-window-configuration)
  12129. (with-current-buffer (marker-buffer org-log-note-return-to)
  12130. (goto-char org-log-note-return-to))
  12131. (move-marker org-log-note-return-to nil)
  12132. (and org-log-post-message (message "%s" org-log-post-message)))
  12133. (defun org-remove-empty-drawer-at (drawer pos)
  12134. "Remove an empty drawer DRAWER at position POS.
  12135. POS may also be a marker."
  12136. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12137. (save-excursion
  12138. (save-restriction
  12139. (widen)
  12140. (goto-char pos)
  12141. (if (org-in-regexp
  12142. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12143. (replace-match ""))))))
  12144. (defvar org-ts-type nil)
  12145. (defun org-sparse-tree (&optional arg type)
  12146. "Create a sparse tree, prompt for the details.
  12147. This command can create sparse trees. You first need to select the type
  12148. of match used to create the tree:
  12149. t Show all TODO entries.
  12150. T Show entries with a specific TODO keyword.
  12151. m Show entries selected by a tags/property match.
  12152. p Enter a property name and its value (both with completion on existing
  12153. names/values) and show entries with that property.
  12154. r Show entries matching a regular expression (`/' can be used as well).
  12155. b Show deadlines and scheduled items before a date.
  12156. a Show deadlines and scheduled items after a date.
  12157. d Show deadlines due within `org-deadline-warning-days'.
  12158. D Show deadlines and scheduled items between a date range."
  12159. (interactive "P")
  12160. (let (ans kwd value ts-type)
  12161. (setq type (or type org-sparse-tree-default-date-type))
  12162. (setq org-ts-type type)
  12163. (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"
  12164. (cond ((eq type 'all) "all timestamps")
  12165. ((eq type 'scheduled) "only scheduled")
  12166. ((eq type 'deadline) "only deadline")
  12167. ((eq type 'active) "only active timestamps")
  12168. ((eq type 'inactive) "only inactive timestamps")
  12169. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12170. ((eq type 'closed) "with a closed time-stamp")
  12171. (t "scheduled/deadline")))
  12172. (setq ans (read-char-exclusive))
  12173. (cond
  12174. ((equal ans ?c)
  12175. (org-sparse-tree
  12176. arg (cadr (member type '(scheduled-or-deadline
  12177. all scheduled deadline active inactive closed)))))
  12178. ((equal ans ?d)
  12179. (call-interactively 'org-check-deadlines))
  12180. ((equal ans ?b)
  12181. (call-interactively 'org-check-before-date))
  12182. ((equal ans ?a)
  12183. (call-interactively 'org-check-after-date))
  12184. ((equal ans ?D)
  12185. (call-interactively 'org-check-dates-range))
  12186. ((equal ans ?t)
  12187. (call-interactively 'org-show-todo-tree))
  12188. ((equal ans ?T)
  12189. (org-show-todo-tree '(4)))
  12190. ((member ans '(?T ?m))
  12191. (call-interactively 'org-match-sparse-tree))
  12192. ((member ans '(?p ?P))
  12193. (setq kwd (org-icompleting-read "Property: "
  12194. (mapcar 'list (org-buffer-property-keys))))
  12195. (setq value (org-icompleting-read "Value: "
  12196. (mapcar 'list (org-property-values kwd))))
  12197. (unless (string-match "\\`{.*}\\'" value)
  12198. (setq value (concat "\"" value "\"")))
  12199. (org-match-sparse-tree arg (concat kwd "=" value)))
  12200. ((member ans '(?r ?R ?/))
  12201. (call-interactively 'org-occur))
  12202. (t (error "No such sparse tree command \"%c\"" ans)))))
  12203. (defvar org-occur-highlights nil
  12204. "List of overlays used for occur matches.")
  12205. (make-variable-buffer-local 'org-occur-highlights)
  12206. (defvar org-occur-parameters nil
  12207. "Parameters of the active org-occur calls.
  12208. This is a list, each call to org-occur pushes as cons cell,
  12209. containing the regular expression and the callback, onto the list.
  12210. The list can contain several entries if `org-occur' has been called
  12211. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12212. will only contain one set of parameters. When the highlights are
  12213. removed (for example with `C-c C-c', or with the next edit (depending
  12214. on `org-remove-highlights-with-change'), this variable is emptied
  12215. as well.")
  12216. (make-variable-buffer-local 'org-occur-parameters)
  12217. (defun org-occur (regexp &optional keep-previous callback)
  12218. "Make a compact tree which shows all matches of REGEXP.
  12219. The tree will show the lines where the regexp matches, and all higher
  12220. headlines above the match. It will also show the heading after the match,
  12221. to make sure editing the matching entry is easy.
  12222. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12223. call to `org-occur' will be kept, to allow stacking of calls to this
  12224. command.
  12225. If CALLBACK is non-nil, it is a function which is called to confirm
  12226. that the match should indeed be shown."
  12227. (interactive "sRegexp: \nP")
  12228. (when (equal regexp "")
  12229. (error "Regexp cannot be empty"))
  12230. (unless keep-previous
  12231. (org-remove-occur-highlights nil nil t))
  12232. (push (cons regexp callback) org-occur-parameters)
  12233. (let ((cnt 0))
  12234. (save-excursion
  12235. (goto-char (point-min))
  12236. (if (or (not keep-previous) ; do not want to keep
  12237. (not org-occur-highlights)) ; no previous matches
  12238. ;; hide everything
  12239. (org-overview))
  12240. (while (re-search-forward regexp nil t)
  12241. (when (or (not callback)
  12242. (save-match-data (funcall callback)))
  12243. (setq cnt (1+ cnt))
  12244. (when org-highlight-sparse-tree-matches
  12245. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12246. (org-show-context 'occur-tree))))
  12247. (when org-remove-highlights-with-change
  12248. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12249. nil 'local))
  12250. (unless org-sparse-tree-open-archived-trees
  12251. (org-hide-archived-subtrees (point-min) (point-max)))
  12252. (run-hooks 'org-occur-hook)
  12253. (if (org-called-interactively-p 'interactive)
  12254. (message "%d match(es) for regexp %s" cnt regexp))
  12255. cnt))
  12256. (defun org-occur-next-match (&optional n reset)
  12257. "Function for `next-error-function' to find sparse tree matches.
  12258. N is the number of matches to move, when negative move backwards.
  12259. RESET is entirely ignored - this function always goes back to the
  12260. starting point when no match is found."
  12261. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12262. (search-func (if (< n 0)
  12263. 'previous-single-char-property-change
  12264. 'next-single-char-property-change))
  12265. (n (abs n))
  12266. (pos (point))
  12267. p1)
  12268. (catch 'exit
  12269. (while (setq p1 (funcall search-func (point) 'org-type))
  12270. (when (equal p1 limit)
  12271. (goto-char pos)
  12272. (error "No more matches"))
  12273. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12274. (setq n (1- n))
  12275. (when (= n 0)
  12276. (goto-char p1)
  12277. (throw 'exit (point))))
  12278. (goto-char p1))
  12279. (goto-char p1)
  12280. (error "No more matches"))))
  12281. (defun org-show-context (&optional key)
  12282. "Make sure point and context are visible.
  12283. How much context is shown depends upon the variables
  12284. `org-show-hierarchy-above', `org-show-following-heading',
  12285. `org-show-entry-below' and `org-show-siblings'."
  12286. (let ((heading-p (org-at-heading-p t))
  12287. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12288. (following-p (org-get-alist-option org-show-following-heading key))
  12289. (entry-p (org-get-alist-option org-show-entry-below key))
  12290. (siblings-p (org-get-alist-option org-show-siblings key)))
  12291. ;; Show heading or entry text
  12292. (if (and heading-p (not entry-p))
  12293. (org-flag-heading nil) ; only show the heading
  12294. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12295. (org-show-hidden-entry))) ; show entire entry
  12296. (when following-p
  12297. ;; Show next sibling, or heading below text
  12298. (save-excursion
  12299. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12300. (org-flag-heading nil))))
  12301. (when siblings-p (org-show-siblings))
  12302. (when hierarchy-p
  12303. ;; show all higher headings, possibly with siblings
  12304. (save-excursion
  12305. (while (and (condition-case nil
  12306. (progn (org-up-heading-all 1) t)
  12307. (error nil))
  12308. (not (bobp)))
  12309. (org-flag-heading nil)
  12310. (when siblings-p (org-show-siblings)))))
  12311. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12312. (defvar org-reveal-start-hook nil
  12313. "Hook run before revealing a location.")
  12314. (defun org-reveal (&optional siblings)
  12315. "Show current entry, hierarchy above it, and the following headline.
  12316. This can be used to show a consistent set of context around locations
  12317. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12318. not t for the search context.
  12319. With optional argument SIBLINGS, on each level of the hierarchy all
  12320. siblings are shown. This repairs the tree structure to what it would
  12321. look like when opened with hierarchical calls to `org-cycle'.
  12322. With double optional argument \\[universal-argument] \\[universal-argument], \
  12323. go to the parent and show the
  12324. entire tree."
  12325. (interactive "P")
  12326. (run-hooks 'org-reveal-start-hook)
  12327. (let ((org-show-hierarchy-above t)
  12328. (org-show-following-heading t)
  12329. (org-show-siblings (if siblings t org-show-siblings)))
  12330. (org-show-context nil))
  12331. (when (equal siblings '(16))
  12332. (save-excursion
  12333. (when (org-up-heading-safe)
  12334. (org-show-subtree)
  12335. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12336. (defun org-highlight-new-match (beg end)
  12337. "Highlight from BEG to END and mark the highlight is an occur headline."
  12338. (let ((ov (make-overlay beg end)))
  12339. (overlay-put ov 'face 'secondary-selection)
  12340. (overlay-put ov 'org-type 'org-occur)
  12341. (push ov org-occur-highlights)))
  12342. (defun org-remove-occur-highlights (&optional beg end noremove)
  12343. "Remove the occur highlights from the buffer.
  12344. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12345. from the `before-change-functions' in the current buffer."
  12346. (interactive)
  12347. (unless org-inhibit-highlight-removal
  12348. (mapc 'delete-overlay org-occur-highlights)
  12349. (setq org-occur-highlights nil)
  12350. (setq org-occur-parameters nil)
  12351. (unless noremove
  12352. (remove-hook 'before-change-functions
  12353. 'org-remove-occur-highlights 'local))))
  12354. ;;;; Priorities
  12355. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12356. "Regular expression matching the priority indicator.")
  12357. (defvar org-remove-priority-next-time nil)
  12358. (defun org-priority-up ()
  12359. "Increase the priority of the current item."
  12360. (interactive)
  12361. (org-priority 'up))
  12362. (defun org-priority-down ()
  12363. "Decrease the priority of the current item."
  12364. (interactive)
  12365. (org-priority 'down))
  12366. (defun org-priority (&optional action show)
  12367. "Change the priority of an item.
  12368. ACTION can be `set', `up', `down', or a character."
  12369. (interactive "P")
  12370. (if (equal action '(4))
  12371. (org-show-priority)
  12372. (unless org-enable-priority-commands
  12373. (error "Priority commands are disabled"))
  12374. (setq action (or action 'set))
  12375. (let (current new news have remove)
  12376. (save-excursion
  12377. (org-back-to-heading t)
  12378. (if (looking-at org-priority-regexp)
  12379. (setq current (string-to-char (match-string 2))
  12380. have t))
  12381. (cond
  12382. ((eq action 'remove)
  12383. (setq remove t new ?\ ))
  12384. ((or (eq action 'set)
  12385. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12386. (if (not (eq action 'set))
  12387. (setq new action)
  12388. (message "Priority %c-%c, SPC to remove: "
  12389. org-highest-priority org-lowest-priority)
  12390. (save-match-data
  12391. (setq new (read-char-exclusive))))
  12392. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12393. (= (upcase org-lowest-priority) org-lowest-priority))
  12394. (setq new (upcase new)))
  12395. (cond ((equal new ?\ ) (setq remove t))
  12396. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12397. (error "Priority must be between `%c' and `%c'"
  12398. org-highest-priority org-lowest-priority))))
  12399. ((eq action 'up)
  12400. (setq new (if have
  12401. (1- current) ; normal cycling
  12402. ;; last priority was empty
  12403. (if (eq last-command this-command)
  12404. org-lowest-priority ; wrap around empty to lowest
  12405. ;; default
  12406. (if org-priority-start-cycle-with-default
  12407. org-default-priority
  12408. (1- org-default-priority))))))
  12409. ((eq action 'down)
  12410. (setq new (if have
  12411. (1+ current) ; normal cycling
  12412. ;; last priority was empty
  12413. (if (eq last-command this-command)
  12414. org-highest-priority ; wrap around empty to highest
  12415. ;; default
  12416. (if org-priority-start-cycle-with-default
  12417. org-default-priority
  12418. (1+ org-default-priority))))))
  12419. (t (error "Invalid action")))
  12420. (if (or (< (upcase new) org-highest-priority)
  12421. (> (upcase new) org-lowest-priority))
  12422. (if (and (memq action '(up down))
  12423. (not have) (not (eq last-command this-command)))
  12424. ;; `new' is from default priority
  12425. (error
  12426. "The default can not be set, see `org-default-priority' why")
  12427. ;; normal cycling: `new' is beyond highest/lowest priority
  12428. ;; and is wrapped around to the empty priority
  12429. (setq remove t)))
  12430. (setq news (format "%c" new))
  12431. (if have
  12432. (if remove
  12433. (replace-match "" t t nil 1)
  12434. (replace-match news t t nil 2))
  12435. (if remove
  12436. (error "No priority cookie found in line")
  12437. (let ((case-fold-search nil))
  12438. (looking-at org-todo-line-regexp))
  12439. (if (match-end 2)
  12440. (progn
  12441. (goto-char (match-end 2))
  12442. (insert " [#" news "]"))
  12443. (goto-char (match-beginning 3))
  12444. (insert "[#" news "] "))))
  12445. (org-preserve-lc (org-set-tags nil 'align)))
  12446. (if remove
  12447. (message "Priority removed")
  12448. (message "Priority of current item set to %s" news)))))
  12449. (defun org-show-priority ()
  12450. "Show the priority of the current item.
  12451. This priority is composed of the main priority given with the [#A] cookies,
  12452. and by additional input from the age of a schedules or deadline entry."
  12453. (interactive)
  12454. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12455. (org-get-at-bol 'priority)
  12456. (save-excursion
  12457. (save-match-data
  12458. (beginning-of-line)
  12459. (and (looking-at org-heading-regexp)
  12460. (org-get-priority (match-string 0))))))))
  12461. (message "Priority is %d" (if pri pri -1000))))
  12462. (defun org-get-priority (s)
  12463. "Find priority cookie and return priority."
  12464. (save-match-data
  12465. (if (functionp org-get-priority-function)
  12466. (funcall org-get-priority-function)
  12467. (if (not (string-match org-priority-regexp s))
  12468. (* 1000 (- org-lowest-priority org-default-priority))
  12469. (* 1000 (- org-lowest-priority
  12470. (string-to-char (match-string 2 s))))))))
  12471. ;;;; Tags
  12472. (defvar org-agenda-archives-mode)
  12473. (defvar org-map-continue-from nil
  12474. "Position from where mapping should continue.
  12475. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12476. (defvar org-scanner-tags nil
  12477. "The current tag list while the tags scanner is running.")
  12478. (defvar org-trust-scanner-tags nil
  12479. "Should `org-get-tags-at' use the tags for the scanner.
  12480. This is for internal dynamical scoping only.
  12481. When this is non-nil, the function `org-get-tags-at' will return the value
  12482. of `org-scanner-tags' instead of building the list by itself. This
  12483. can lead to large speed-ups when the tags scanner is used in a file with
  12484. many entries, and when the list of tags is retrieved, for example to
  12485. obtain a list of properties. Building the tags list for each entry in such
  12486. a file becomes an N^2 operation - but with this variable set, it scales
  12487. as N.")
  12488. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12489. "Sca headline tags with inheritance and produce output ACTION.
  12490. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12491. or `agenda' to produce an entry list for an agenda view. It can also be
  12492. a Lisp form or a function that should be called at each matched headline, in
  12493. this case the return value is a list of all return values from these calls.
  12494. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12495. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12496. only lines with a not-done TODO keyword are included in the output.
  12497. This should be the same variable that was scoped into
  12498. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12499. START-LEVEL can be a string with asterisks, reducing the scope to
  12500. headlines matching this string."
  12501. (require 'org-agenda)
  12502. (let* ((re (concat "^"
  12503. (if start-level
  12504. ;; Get the correct level to match
  12505. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12506. org-outline-regexp)
  12507. " *\\(\\<\\("
  12508. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12509. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12510. (props (list 'face 'default
  12511. 'done-face 'org-agenda-done
  12512. 'undone-face 'default
  12513. 'mouse-face 'highlight
  12514. 'org-not-done-regexp org-not-done-regexp
  12515. 'org-todo-regexp org-todo-regexp
  12516. 'org-complex-heading-regexp org-complex-heading-regexp
  12517. 'help-echo
  12518. (format "mouse-2 or RET jump to org file %s"
  12519. (abbreviate-file-name
  12520. (or (buffer-file-name (buffer-base-buffer))
  12521. (buffer-name (buffer-base-buffer)))))))
  12522. (case-fold-search nil)
  12523. (org-map-continue-from nil)
  12524. lspos tags tags-list
  12525. (tags-alist (list (cons 0 org-file-tags)))
  12526. (llast 0) rtn rtn1 level category i txt
  12527. todo marker entry priority)
  12528. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12529. (setq action (list 'lambda nil action)))
  12530. (save-excursion
  12531. (goto-char (point-min))
  12532. (when (eq action 'sparse-tree)
  12533. (org-overview)
  12534. (org-remove-occur-highlights))
  12535. (while (re-search-forward re nil t)
  12536. (setq org-map-continue-from nil)
  12537. (catch :skip
  12538. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12539. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12540. (goto-char (setq lspos (match-beginning 0)))
  12541. (setq level (org-reduced-level (org-outline-level))
  12542. category (org-get-category))
  12543. (setq i llast llast level)
  12544. ;; remove tag lists from same and sublevels
  12545. (while (>= i level)
  12546. (when (setq entry (assoc i tags-alist))
  12547. (setq tags-alist (delete entry tags-alist)))
  12548. (setq i (1- i)))
  12549. ;; add the next tags
  12550. (when tags
  12551. (setq tags (org-split-string tags ":")
  12552. tags-alist
  12553. (cons (cons level tags) tags-alist)))
  12554. ;; compile tags for current headline
  12555. (setq tags-list
  12556. (if org-use-tag-inheritance
  12557. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12558. tags)
  12559. org-scanner-tags tags-list)
  12560. (when org-use-tag-inheritance
  12561. (setcdr (car tags-alist)
  12562. (mapcar (lambda (x)
  12563. (setq x (copy-sequence x))
  12564. (org-add-prop-inherited x))
  12565. (cdar tags-alist))))
  12566. (when (and tags org-use-tag-inheritance
  12567. (or (not (eq t org-use-tag-inheritance))
  12568. org-tags-exclude-from-inheritance))
  12569. ;; selective inheritance, remove uninherited ones
  12570. (setcdr (car tags-alist)
  12571. (org-remove-uninherited-tags (cdar tags-alist))))
  12572. (when (and
  12573. ;; eval matcher only when the todo condition is OK
  12574. (and (or (not todo-only) (member todo org-not-done-keywords))
  12575. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12576. (eval matcher)))
  12577. ;; Call the skipper, but return t if it does not skip,
  12578. ;; so that the `and' form continues evaluating
  12579. (progn
  12580. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12581. t)
  12582. ;; Check if timestamps are deselecting this entry
  12583. (or (not todo-only)
  12584. (and (member todo org-not-done-keywords)
  12585. (or (not org-agenda-tags-todo-honor-ignore-options)
  12586. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12587. ;; select this headline
  12588. (cond
  12589. ((eq action 'sparse-tree)
  12590. (and org-highlight-sparse-tree-matches
  12591. (org-get-heading) (match-end 0)
  12592. (org-highlight-new-match
  12593. (match-beginning 1) (match-end 1)))
  12594. (org-show-context 'tags-tree))
  12595. ((eq action 'agenda)
  12596. (setq txt (org-agenda-format-item
  12597. ""
  12598. (concat
  12599. (if (eq org-tags-match-list-sublevels 'indented)
  12600. (make-string (1- level) ?.) "")
  12601. (org-get-heading))
  12602. level category
  12603. tags-list)
  12604. priority (org-get-priority txt))
  12605. (goto-char lspos)
  12606. (setq marker (org-agenda-new-marker))
  12607. (org-add-props txt props
  12608. 'org-marker marker 'org-hd-marker marker 'org-category category
  12609. 'todo-state todo
  12610. 'priority priority 'type "tagsmatch")
  12611. (push txt rtn))
  12612. ((functionp action)
  12613. (setq org-map-continue-from nil)
  12614. (save-excursion
  12615. (setq rtn1 (funcall action))
  12616. (push rtn1 rtn)))
  12617. (t (error "Invalid action")))
  12618. ;; if we are to skip sublevels, jump to end of subtree
  12619. (unless org-tags-match-list-sublevels
  12620. (org-end-of-subtree t)
  12621. (backward-char 1))))
  12622. ;; Get the correct position from where to continue
  12623. (if org-map-continue-from
  12624. (goto-char org-map-continue-from)
  12625. (and (= (point) lspos) (end-of-line 1)))))
  12626. (when (and (eq action 'sparse-tree)
  12627. (not org-sparse-tree-open-archived-trees))
  12628. (org-hide-archived-subtrees (point-min) (point-max)))
  12629. (nreverse rtn)))
  12630. (defun org-remove-uninherited-tags (tags)
  12631. "Remove all tags that are not inherited from the list TAGS."
  12632. (cond
  12633. ((eq org-use-tag-inheritance t)
  12634. (if org-tags-exclude-from-inheritance
  12635. (org-delete-all org-tags-exclude-from-inheritance tags)
  12636. tags))
  12637. ((not org-use-tag-inheritance) nil)
  12638. ((stringp org-use-tag-inheritance)
  12639. (delq nil (mapcar
  12640. (lambda (x)
  12641. (if (and (string-match org-use-tag-inheritance x)
  12642. (not (member x org-tags-exclude-from-inheritance)))
  12643. x nil))
  12644. tags)))
  12645. ((listp org-use-tag-inheritance)
  12646. (delq nil (mapcar
  12647. (lambda (x)
  12648. (if (member x org-use-tag-inheritance) x nil))
  12649. tags)))))
  12650. (defun org-match-sparse-tree (&optional todo-only match)
  12651. "Create a sparse tree according to tags string MATCH.
  12652. MATCH can contain positive and negative selection of tags, like
  12653. \"+WORK+URGENT-WITHBOSS\".
  12654. If optional argument TODO-ONLY is non-nil, only select lines that are
  12655. also TODO lines."
  12656. (interactive "P")
  12657. (org-agenda-prepare-buffers (list (current-buffer)))
  12658. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12659. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12660. (defvar org-cached-props nil)
  12661. (defun org-cached-entry-get (pom property)
  12662. (if (or (eq t org-use-property-inheritance)
  12663. (and (stringp org-use-property-inheritance)
  12664. (string-match org-use-property-inheritance property))
  12665. (and (listp org-use-property-inheritance)
  12666. (member property org-use-property-inheritance)))
  12667. ;; Caching is not possible, check it directly
  12668. (org-entry-get pom property 'inherit)
  12669. ;; Get all properties, so that we can do complicated checks easily
  12670. (cdr (assoc property (or org-cached-props
  12671. (setq org-cached-props
  12672. (org-entry-properties pom)))))))
  12673. (defun org-global-tags-completion-table (&optional files)
  12674. "Return the list of all tags in all agenda buffer/files.
  12675. Optional FILES argument is a list of files which can be used
  12676. instead of the agenda files."
  12677. (save-excursion
  12678. (org-uniquify
  12679. (delq nil
  12680. (apply 'append
  12681. (mapcar
  12682. (lambda (file)
  12683. (set-buffer (find-file-noselect file))
  12684. (append (org-get-buffer-tags)
  12685. (mapcar (lambda (x) (if (stringp (car-safe x))
  12686. (list (car-safe x)) nil))
  12687. org-tag-alist)))
  12688. (if (and files (car files))
  12689. files
  12690. (org-agenda-files))))))))
  12691. (defun org-make-tags-matcher (match)
  12692. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12693. The variable `todo-only' is scoped dynamically into this function.
  12694. It will be set to t if the matcher restricts matching to TODO entries,
  12695. otherwise will not be touched.
  12696. Returns a cons of the selection string MATCH and the constructed
  12697. lisp form implementing the matcher. The matcher is to be evaluated
  12698. at an Org entry, with point on the headline, and returns t if the
  12699. entry matches the selection string MATCH. The returned lisp form
  12700. references two variables with information about the entry, which
  12701. must be bound around the form's evaluation: todo, the TODO keyword
  12702. at the entry (or nil of none); and tags-list, the list of all tags
  12703. at the entry including inherited ones. Additionally, the category
  12704. of the entry (if any) must be specified as the text property
  12705. 'org-category on the headline.
  12706. See also `org-scan-tags'.
  12707. "
  12708. (declare (special todo-only))
  12709. (unless (boundp 'todo-only)
  12710. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12711. (unless match
  12712. ;; Get a new match request, with completion
  12713. (let ((org-last-tags-completion-table
  12714. (org-global-tags-completion-table)))
  12715. (setq match (org-completing-read-no-i
  12716. "Match: " 'org-tags-completion-function nil nil nil
  12717. 'org-tags-history))))
  12718. ;; Parse the string and create a lisp form
  12719. (let ((match0 match)
  12720. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12721. minus tag mm
  12722. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12723. orterms term orlist re-p str-p level-p level-op time-p
  12724. prop-p pn pv po gv rest)
  12725. ;; Expand group tags
  12726. (setq match (org-tags-expand match))
  12727. (if (string-match "/+" match)
  12728. ;; match contains also a todo-matching request
  12729. (progn
  12730. (setq tagsmatch (substring match 0 (match-beginning 0))
  12731. todomatch (substring match (match-end 0)))
  12732. (if (string-match "^!" todomatch)
  12733. (setq todo-only t todomatch (substring todomatch 1)))
  12734. (if (string-match "^\\s-*$" todomatch)
  12735. (setq todomatch nil)))
  12736. ;; only matching tags
  12737. (setq tagsmatch match todomatch nil))
  12738. ;; Make the tags matcher
  12739. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12740. (setq tagsmatcher t)
  12741. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12742. (while (setq term (pop orterms))
  12743. (while (and (equal (substring term -1) "\\") orterms)
  12744. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12745. (while (string-match re term)
  12746. (setq rest (substring term (match-end 0))
  12747. minus (and (match-end 1)
  12748. (equal (match-string 1 term) "-"))
  12749. tag (save-match-data (replace-regexp-in-string
  12750. "\\\\-" "-"
  12751. (match-string 2 term)))
  12752. re-p (equal (string-to-char tag) ?{)
  12753. level-p (match-end 4)
  12754. prop-p (match-end 5)
  12755. mm (cond
  12756. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12757. (level-p
  12758. (setq level-op (org-op-to-function (match-string 3 term)))
  12759. `(,level-op level ,(string-to-number
  12760. (match-string 4 term))))
  12761. (prop-p
  12762. (setq pn (match-string 5 term)
  12763. po (match-string 6 term)
  12764. pv (match-string 7 term)
  12765. re-p (equal (string-to-char pv) ?{)
  12766. str-p (equal (string-to-char pv) ?\")
  12767. time-p (save-match-data
  12768. (string-match "^\"[[<].*[]>]\"$" pv))
  12769. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12770. (if time-p (setq pv (org-matcher-time pv)))
  12771. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12772. (cond
  12773. ((equal pn "CATEGORY")
  12774. (setq gv '(get-text-property (point) 'org-category)))
  12775. ((equal pn "TODO")
  12776. (setq gv 'todo))
  12777. (t
  12778. (setq gv `(org-cached-entry-get nil ,pn))))
  12779. (if re-p
  12780. (if (eq po 'org<>)
  12781. `(not (string-match ,pv (or ,gv "")))
  12782. `(string-match ,pv (or ,gv "")))
  12783. (if str-p
  12784. `(,po (or ,gv "") ,pv)
  12785. `(,po (string-to-number (or ,gv ""))
  12786. ,(string-to-number pv) ))))
  12787. (t `(member ,tag tags-list)))
  12788. mm (if minus (list 'not mm) mm)
  12789. term rest)
  12790. (push mm tagsmatcher))
  12791. (push (if (> (length tagsmatcher) 1)
  12792. (cons 'and tagsmatcher)
  12793. (car tagsmatcher))
  12794. orlist)
  12795. (setq tagsmatcher nil))
  12796. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12797. (setq tagsmatcher
  12798. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12799. ;; Make the todo matcher
  12800. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12801. (setq todomatcher t)
  12802. (setq orterms (org-split-string todomatch "|") orlist nil)
  12803. (while (setq term (pop orterms))
  12804. (while (string-match re term)
  12805. (setq minus (and (match-end 1)
  12806. (equal (match-string 1 term) "-"))
  12807. kwd (match-string 2 term)
  12808. re-p (equal (string-to-char kwd) ?{)
  12809. term (substring term (match-end 0))
  12810. mm (if re-p
  12811. `(string-match ,(substring kwd 1 -1) todo)
  12812. (list 'equal 'todo kwd))
  12813. mm (if minus (list 'not mm) mm))
  12814. (push mm todomatcher))
  12815. (push (if (> (length todomatcher) 1)
  12816. (cons 'and todomatcher)
  12817. (car todomatcher))
  12818. orlist)
  12819. (setq todomatcher nil))
  12820. (setq todomatcher (if (> (length orlist) 1)
  12821. (cons 'or orlist) (car orlist))))
  12822. ;; Return the string and lisp forms of the matcher
  12823. (setq matcher (if todomatcher
  12824. (list 'and tagsmatcher todomatcher)
  12825. tagsmatcher))
  12826. (when todo-only
  12827. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12828. matcher)))
  12829. (cons match0 matcher)))
  12830. (defun org-tags-expand (match &optional single-as-list downcased)
  12831. "Expand group tags in MATCH.
  12832. This replaces every group tag in MATCH with a regexp tag search.
  12833. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12834. will be replaced like this:
  12835. Work => {\(?:Work\|Lab\|Conf\}
  12836. +Work => +{\(?:Work\|Lab\|Conf\}
  12837. -Work => -{\(?:Work\|Lab\|Conf\}
  12838. Replacing by a regexp preserves the structure of the match.
  12839. E.g., this expansion
  12840. Work|Home => {\(?:Work\|Lab\|Conf\}|Home
  12841. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12842. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12843. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12844. assumed to be a single group tag, and the function will return
  12845. the list of tags in this group.
  12846. When DOWNCASE is non-nil, expand downcased TAGS."
  12847. (if org-group-tags
  12848. (let* ((case-fold-search t)
  12849. (stable org-mode-syntax-table)
  12850. (tal (or org-tag-groups-alist-for-agenda
  12851. org-tag-groups-alist))
  12852. (tal (if downcased (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12853. (tml (mapcar 'car tal))
  12854. (rtnmatch match) rpl)
  12855. ;; @ and _ are allowed as word-components in tags
  12856. (modify-syntax-entry ?@ "w" stable)
  12857. (modify-syntax-entry ?_ "w" stable)
  12858. (while (and tml (string-match
  12859. (concat "\\(?1:[+-]?\\)\\(?2:\\<" (regexp-opt tml) "\\>\\)")
  12860. rtnmatch))
  12861. (let* ((dir (match-string 1 rtnmatch))
  12862. (tag (match-string 2 rtnmatch))
  12863. (tag (if downcased (downcase tag) tag)))
  12864. (setq tml (delete tag tml))
  12865. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12866. (setq rtnmatch
  12867. (replace-match
  12868. (concat dir "{" (regexp-opt rpl) "}") t t rtnmatch))))
  12869. (if single-as-list
  12870. (or (reverse rpl) (list rtnmatch))
  12871. rtnmatch))
  12872. (if single-as-list (list (if downcased (downcase match) match))
  12873. match)))
  12874. (defun org-op-to-function (op &optional stringp)
  12875. "Turn an operator into the appropriate function."
  12876. (setq op
  12877. (cond
  12878. ((equal op "<" ) '(< string< org-time<))
  12879. ((equal op ">" ) '(> org-string> org-time>))
  12880. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12881. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12882. ((member op '("=" "==")) '(= string= org-time=))
  12883. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12884. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12885. (defun org<> (a b) (not (= a b)))
  12886. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12887. (defun org-string>= (a b) (not (string< a b)))
  12888. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12889. (defun org-string<> (a b) (not (string= a b)))
  12890. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12891. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12892. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12893. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12894. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12895. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12896. (defun org-2ft (s)
  12897. "Convert S to a floating point time.
  12898. If S is already a number, just return it. If it is a string, parse
  12899. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12900. (cond
  12901. ((numberp s) s)
  12902. ((stringp s)
  12903. (condition-case nil
  12904. (float-time (apply 'encode-time (org-parse-time-string s)))
  12905. (error 0.)))
  12906. (t 0.)))
  12907. (defun org-time-today ()
  12908. "Time in seconds today at 0:00.
  12909. Returns the float number of seconds since the beginning of the
  12910. epoch to the beginning of today (00:00)."
  12911. (float-time (apply 'encode-time
  12912. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12913. (defun org-matcher-time (s)
  12914. "Interpret a time comparison value."
  12915. (save-match-data
  12916. (cond
  12917. ((string= s "<now>") (float-time))
  12918. ((string= s "<today>") (org-time-today))
  12919. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12920. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12921. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12922. (+ (org-time-today)
  12923. (* (string-to-number (match-string 1 s))
  12924. (cdr (assoc (match-string 2 s)
  12925. '(("d" . 86400.0) ("w" . 604800.0)
  12926. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12927. (t (org-2ft s)))))
  12928. (defun org-match-any-p (re list)
  12929. "Does re match any element of list?"
  12930. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12931. (delq nil list))
  12932. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12933. (defvar org-tags-overlay (make-overlay 1 1))
  12934. (org-detach-overlay org-tags-overlay)
  12935. (defun org-get-local-tags-at (&optional pos)
  12936. "Get a list of tags defined in the current headline."
  12937. (org-get-tags-at pos 'local))
  12938. (defun org-get-local-tags ()
  12939. "Get a list of tags defined in the current headline."
  12940. (org-get-tags-at nil 'local))
  12941. (defun org-get-tags-at (&optional pos local)
  12942. "Get a list of all headline tags applicable at POS.
  12943. POS defaults to point. If tags are inherited, the list contains
  12944. the targets in the same sequence as the headlines appear, i.e.
  12945. the tags of the current headline come last.
  12946. When LOCAL is non-nil, only return tags from the current headline,
  12947. ignore inherited ones."
  12948. (interactive)
  12949. (if (and org-trust-scanner-tags
  12950. (or (not pos) (equal pos (point)))
  12951. (not local))
  12952. org-scanner-tags
  12953. (let (tags ltags lastpos parent)
  12954. (save-excursion
  12955. (save-restriction
  12956. (widen)
  12957. (goto-char (or pos (point)))
  12958. (save-match-data
  12959. (catch 'done
  12960. (condition-case nil
  12961. (progn
  12962. (org-back-to-heading t)
  12963. (while (not (equal lastpos (point)))
  12964. (setq lastpos (point))
  12965. (when (looking-at
  12966. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12967. (setq ltags (org-split-string
  12968. (org-match-string-no-properties 1) ":"))
  12969. (when parent
  12970. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12971. (setq tags (append
  12972. (if parent
  12973. (org-remove-uninherited-tags ltags)
  12974. ltags)
  12975. tags)))
  12976. (or org-use-tag-inheritance (throw 'done t))
  12977. (if local (throw 'done t))
  12978. (or (org-up-heading-safe) (error nil))
  12979. (setq parent t)))
  12980. (error nil)))))
  12981. (if local
  12982. tags
  12983. (reverse (delete-dups
  12984. (reverse (append
  12985. (org-remove-uninherited-tags
  12986. org-file-tags) tags)))))))))
  12987. (defun org-add-prop-inherited (s)
  12988. (add-text-properties 0 (length s) '(inherited t) s)
  12989. s)
  12990. (defun org-toggle-tag (tag &optional onoff)
  12991. "Toggle the tag TAG for the current line.
  12992. If ONOFF is `on' or `off', don't toggle but set to this state."
  12993. (let (res current)
  12994. (save-excursion
  12995. (org-back-to-heading t)
  12996. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12997. (point-at-eol) t)
  12998. (progn
  12999. (setq current (match-string 1))
  13000. (replace-match ""))
  13001. (setq current ""))
  13002. (setq current (nreverse (org-split-string current ":")))
  13003. (cond
  13004. ((eq onoff 'on)
  13005. (setq res t)
  13006. (or (member tag current) (push tag current)))
  13007. ((eq onoff 'off)
  13008. (or (not (member tag current)) (setq current (delete tag current))))
  13009. (t (if (member tag current)
  13010. (setq current (delete tag current))
  13011. (setq res t)
  13012. (push tag current))))
  13013. (end-of-line 1)
  13014. (if current
  13015. (progn
  13016. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13017. (org-set-tags nil t))
  13018. (delete-horizontal-space))
  13019. (run-hooks 'org-after-tags-change-hook))
  13020. res))
  13021. (defun org-align-tags-here (to-col)
  13022. ;; Assumes that this is a headline
  13023. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13024. (beginning-of-line 1)
  13025. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13026. (< pos (match-beginning 2)))
  13027. (progn
  13028. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13029. (goto-char (match-beginning 1))
  13030. (insert " ")
  13031. (delete-region (point) (1+ (match-beginning 2)))
  13032. (setq ncol (max (current-column)
  13033. (1+ col)
  13034. (if (> to-col 0)
  13035. to-col
  13036. (- (abs to-col) tags-l))))
  13037. (setq p (point))
  13038. (insert (make-string (- ncol (current-column)) ?\ ))
  13039. (setq ncol (current-column))
  13040. (when indent-tabs-mode (tabify p (point-at-eol)))
  13041. (org-move-to-column (min ncol col) t))
  13042. (goto-char pos))))
  13043. (defun org-set-tags-command (&optional arg just-align)
  13044. "Call the set-tags command for the current entry."
  13045. (interactive "P")
  13046. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13047. (org-set-tags arg just-align)
  13048. (save-excursion
  13049. (unless (and (org-region-active-p)
  13050. org-loop-over-headlines-in-active-region)
  13051. (org-back-to-heading t))
  13052. (org-set-tags arg just-align))))
  13053. (defun org-set-tags-to (data)
  13054. "Set the tags of the current entry to DATA, replacing the current tags.
  13055. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13056. If DATA is nil or the empty string, any tags will be removed."
  13057. (interactive "sTags: ")
  13058. (setq data
  13059. (cond
  13060. ((eq data nil) "")
  13061. ((equal data "") "")
  13062. ((stringp data)
  13063. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13064. ":"))
  13065. ((listp data)
  13066. (concat ":" (mapconcat 'identity data ":") ":"))))
  13067. (when data
  13068. (save-excursion
  13069. (org-back-to-heading t)
  13070. (when (looking-at org-complex-heading-regexp)
  13071. (if (match-end 5)
  13072. (progn
  13073. (goto-char (match-beginning 5))
  13074. (insert data)
  13075. (delete-region (point) (point-at-eol))
  13076. (org-set-tags nil 'align))
  13077. (goto-char (point-at-eol))
  13078. (insert " " data)
  13079. (org-set-tags nil 'align)))
  13080. (beginning-of-line 1)
  13081. (if (looking-at ".*?\\([ \t]+\\)$")
  13082. (delete-region (match-beginning 1) (match-end 1))))))
  13083. (defun org-align-all-tags ()
  13084. "Align the tags i all headings."
  13085. (interactive)
  13086. (save-excursion
  13087. (or (ignore-errors (org-back-to-heading t))
  13088. (outline-next-heading))
  13089. (if (org-at-heading-p)
  13090. (org-set-tags t)
  13091. (message "No headings"))))
  13092. (defvar org-indent-indentation-per-level)
  13093. (defun org-set-tags (&optional arg just-align)
  13094. "Set the tags for the current headline.
  13095. With prefix ARG, realign all tags in headings in the current buffer."
  13096. (interactive "P")
  13097. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13098. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13099. 'region-start-level 'region))
  13100. org-loop-over-headlines-in-active-region)
  13101. (org-map-entries
  13102. ;; We don't use ARG and JUST-ALIGN here these args are not
  13103. ;; useful when looping over headlines
  13104. `(org-set-tags)
  13105. org-loop-over-headlines-in-active-region
  13106. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13107. (let* ((re org-outline-regexp-bol)
  13108. (current (unless arg (org-get-tags-string)))
  13109. (col (current-column))
  13110. (org-setting-tags t)
  13111. table current-tags inherited-tags ; computed below when needed
  13112. tags p0 c0 c1 rpl di tc level)
  13113. (if arg
  13114. (save-excursion
  13115. (goto-char (point-min))
  13116. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13117. (while (re-search-forward re nil t)
  13118. (org-set-tags nil t)
  13119. (end-of-line 1)))
  13120. (message "All tags realigned to column %d" org-tags-column))
  13121. (if just-align
  13122. (setq tags current)
  13123. ;; Get a new set of tags from the user
  13124. (save-excursion
  13125. (setq table (append org-tag-persistent-alist
  13126. (or org-tag-alist (org-get-buffer-tags))
  13127. (and
  13128. org-complete-tags-always-offer-all-agenda-tags
  13129. (org-global-tags-completion-table
  13130. (org-agenda-files))))
  13131. org-last-tags-completion-table table
  13132. current-tags (org-split-string current ":")
  13133. inherited-tags (nreverse
  13134. (nthcdr (length current-tags)
  13135. (nreverse (org-get-tags-at))))
  13136. tags
  13137. (if (or (eq t org-use-fast-tag-selection)
  13138. (and org-use-fast-tag-selection
  13139. (delq nil (mapcar 'cdr table))))
  13140. (org-fast-tag-selection
  13141. current-tags inherited-tags table
  13142. (if org-fast-tag-selection-include-todo
  13143. org-todo-key-alist))
  13144. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13145. (org-trim
  13146. (org-icompleting-read "Tags: "
  13147. 'org-tags-completion-function
  13148. nil nil current 'org-tags-history))))))
  13149. (while (string-match "[-+&]+" tags)
  13150. ;; No boolean logic, just a list
  13151. (setq tags (replace-match ":" t t tags))))
  13152. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13153. (if org-tags-sort-function
  13154. (setq tags (mapconcat 'identity
  13155. (sort (org-split-string
  13156. tags (org-re "[^[:alnum:]_@#%]+"))
  13157. org-tags-sort-function) ":")))
  13158. (if (string-match "\\`[\t ]*\\'" tags)
  13159. (setq tags "")
  13160. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13161. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13162. ;; Insert new tags at the correct column
  13163. (beginning-of-line 1)
  13164. (setq level (or (and (looking-at org-outline-regexp)
  13165. (- (match-end 0) (point) 1))
  13166. 1))
  13167. (cond
  13168. ((and (equal current "") (equal tags "")))
  13169. ((re-search-forward
  13170. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13171. (point-at-eol) t)
  13172. (if (equal tags "")
  13173. (setq rpl "")
  13174. (goto-char (match-beginning 0))
  13175. (setq c0 (current-column)
  13176. ;; compute offset for the case of org-indent-mode active
  13177. di (if org-indent-mode
  13178. (* (1- org-indent-indentation-per-level) (1- level))
  13179. 0)
  13180. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13181. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13182. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13183. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13184. (replace-match rpl t t)
  13185. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13186. tags)
  13187. (t (error "Tags alignment failed")))
  13188. (org-move-to-column col)
  13189. (unless just-align
  13190. (run-hooks 'org-after-tags-change-hook))))))
  13191. (defun org-change-tag-in-region (beg end tag off)
  13192. "Add or remove TAG for each entry in the region.
  13193. This works in the agenda, and also in an org-mode buffer."
  13194. (interactive
  13195. (list (region-beginning) (region-end)
  13196. (let ((org-last-tags-completion-table
  13197. (if (derived-mode-p 'org-mode)
  13198. (org-get-buffer-tags)
  13199. (org-global-tags-completion-table))))
  13200. (org-icompleting-read
  13201. "Tag: " 'org-tags-completion-function nil nil nil
  13202. 'org-tags-history))
  13203. (progn
  13204. (message "[s]et or [r]emove? ")
  13205. (equal (read-char-exclusive) ?r))))
  13206. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13207. (let ((agendap (equal major-mode 'org-agenda-mode))
  13208. l1 l2 m buf pos newhead (cnt 0))
  13209. (goto-char end)
  13210. (setq l2 (1- (org-current-line)))
  13211. (goto-char beg)
  13212. (setq l1 (org-current-line))
  13213. (loop for l from l1 to l2 do
  13214. (org-goto-line l)
  13215. (setq m (get-text-property (point) 'org-hd-marker))
  13216. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13217. (and agendap m))
  13218. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13219. pos (if agendap m (point)))
  13220. (with-current-buffer buf
  13221. (save-excursion
  13222. (save-restriction
  13223. (goto-char pos)
  13224. (setq cnt (1+ cnt))
  13225. (org-toggle-tag tag (if off 'off 'on))
  13226. (setq newhead (org-get-heading)))))
  13227. (and agendap (org-agenda-change-all-lines newhead m))))
  13228. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13229. (defun org-tags-completion-function (string predicate &optional flag)
  13230. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13231. (confirm (lambda (x) (stringp (car x)))))
  13232. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13233. (setq s1 (match-string 1 string)
  13234. s2 (match-string 2 string))
  13235. (setq s1 "" s2 string))
  13236. (cond
  13237. ((eq flag nil)
  13238. ;; try completion
  13239. (setq rtn (try-completion s2 ctable confirm))
  13240. (if (stringp rtn)
  13241. (setq rtn
  13242. (concat s1 s2 (substring rtn (length s2))
  13243. (if (and org-add-colon-after-tag-completion
  13244. (assoc rtn ctable))
  13245. ":" ""))))
  13246. rtn)
  13247. ((eq flag t)
  13248. ;; all-completions
  13249. (all-completions s2 ctable confirm))
  13250. ((eq flag 'lambda)
  13251. ;; exact match?
  13252. (assoc s2 ctable)))))
  13253. (defun org-fast-tag-insert (kwd tags face &optional end)
  13254. "Insert KDW, and the TAGS, the latter with face FACE.
  13255. Also insert END."
  13256. (insert (format "%-12s" (concat kwd ":"))
  13257. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13258. (or end "")))
  13259. (defun org-fast-tag-show-exit (flag)
  13260. (save-excursion
  13261. (org-goto-line 3)
  13262. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13263. (replace-match ""))
  13264. (when flag
  13265. (end-of-line 1)
  13266. (org-move-to-column (- (window-width) 19) t)
  13267. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13268. (defun org-set-current-tags-overlay (current prefix)
  13269. "Add an overlay to CURRENT tag with PREFIX."
  13270. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13271. (if (featurep 'xemacs)
  13272. (org-overlay-display org-tags-overlay (concat prefix s)
  13273. 'secondary-selection)
  13274. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13275. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13276. (defvar org-last-tag-selection-key nil)
  13277. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13278. "Fast tag selection with single keys.
  13279. CURRENT is the current list of tags in the headline, INHERITED is the
  13280. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13281. possibly with grouping information. TODO-TABLE is a similar table with
  13282. TODO keywords, should these have keys assigned to them.
  13283. If the keys are nil, a-z are automatically assigned.
  13284. Returns the new tags string, or nil to not change the current settings."
  13285. (let* ((fulltable (append table todo-table))
  13286. (maxlen (apply 'max (mapcar
  13287. (lambda (x)
  13288. (if (stringp (car x)) (string-width (car x)) 0))
  13289. fulltable)))
  13290. (buf (current-buffer))
  13291. (expert (eq org-fast-tag-selection-single-key 'expert))
  13292. (buffer-tags nil)
  13293. (fwidth (+ maxlen 3 1 3))
  13294. (ncol (/ (- (window-width) 4) fwidth))
  13295. (i-face 'org-done)
  13296. (c-face 'org-todo)
  13297. tg cnt e c char c1 c2 ntable tbl rtn
  13298. ov-start ov-end ov-prefix
  13299. (exit-after-next org-fast-tag-selection-single-key)
  13300. (done-keywords org-done-keywords)
  13301. groups ingroup)
  13302. (save-excursion
  13303. (beginning-of-line 1)
  13304. (if (looking-at
  13305. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13306. (setq ov-start (match-beginning 1)
  13307. ov-end (match-end 1)
  13308. ov-prefix "")
  13309. (setq ov-start (1- (point-at-eol))
  13310. ov-end (1+ ov-start))
  13311. (skip-chars-forward "^\n\r")
  13312. (setq ov-prefix
  13313. (concat
  13314. (buffer-substring (1- (point)) (point))
  13315. (if (> (current-column) org-tags-column)
  13316. " "
  13317. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13318. (move-overlay org-tags-overlay ov-start ov-end)
  13319. (save-window-excursion
  13320. (if expert
  13321. (set-buffer (get-buffer-create " *Org tags*"))
  13322. (delete-other-windows)
  13323. (split-window-vertically)
  13324. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13325. (erase-buffer)
  13326. (org-set-local 'org-done-keywords done-keywords)
  13327. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13328. (org-fast-tag-insert "Current" current c-face "\n\n")
  13329. (org-fast-tag-show-exit exit-after-next)
  13330. (org-set-current-tags-overlay current ov-prefix)
  13331. (setq tbl fulltable char ?a cnt 0)
  13332. (while (setq e (pop tbl))
  13333. (cond
  13334. ((equal (car e) :startgroup)
  13335. (push '() groups) (setq ingroup t)
  13336. (when (not (= cnt 0))
  13337. (setq cnt 0)
  13338. (insert "\n"))
  13339. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13340. ((equal (car e) :endgroup)
  13341. (setq ingroup nil cnt 0)
  13342. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13343. ((equal e '(:newline))
  13344. (when (not (= cnt 0))
  13345. (setq cnt 0)
  13346. (insert "\n")
  13347. (setq e (car tbl))
  13348. (while (equal (car tbl) '(:newline))
  13349. (insert "\n")
  13350. (setq tbl (cdr tbl)))))
  13351. ((equal e '(:grouptags)) nil)
  13352. (t
  13353. (setq tg (copy-sequence (car e)) c2 nil)
  13354. (if (cdr e)
  13355. (setq c (cdr e))
  13356. ;; automatically assign a character.
  13357. (setq c1 (string-to-char
  13358. (downcase (substring
  13359. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13360. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13361. (while (or (rassoc char ntable) (rassoc char table))
  13362. (setq char (1+ char)))
  13363. (setq c2 c1))
  13364. (setq c (or c2 char)))
  13365. (if ingroup (push tg (car groups)))
  13366. (setq tg (org-add-props tg nil 'face
  13367. (cond
  13368. ((not (assoc tg table))
  13369. (org-get-todo-face tg))
  13370. ((member tg current) c-face)
  13371. ((member tg inherited) i-face))))
  13372. (if (equal (caar tbl) :grouptags)
  13373. (org-add-props tg nil 'face 'org-tag-group))
  13374. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13375. (insert "[" c "] " tg (make-string
  13376. (- fwidth 4 (length tg)) ?\ ))
  13377. (push (cons tg c) ntable)
  13378. (when (= (setq cnt (1+ cnt)) ncol)
  13379. (insert "\n")
  13380. (if ingroup (insert " "))
  13381. (setq cnt 0)))))
  13382. (setq ntable (nreverse ntable))
  13383. (insert "\n")
  13384. (goto-char (point-min))
  13385. (if (not expert) (org-fit-window-to-buffer))
  13386. (setq rtn
  13387. (catch 'exit
  13388. (while t
  13389. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13390. (if (not groups) "no " "")
  13391. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13392. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13393. (setq org-last-tag-selection-key c)
  13394. (cond
  13395. ((= c ?\r) (throw 'exit t))
  13396. ((= c ?!)
  13397. (setq groups (not groups))
  13398. (goto-char (point-min))
  13399. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13400. ((= c ?\C-c)
  13401. (if (not expert)
  13402. (org-fast-tag-show-exit
  13403. (setq exit-after-next (not exit-after-next)))
  13404. (setq expert nil)
  13405. (delete-other-windows)
  13406. (set-window-buffer (split-window-vertically) " *Org tags*")
  13407. (org-switch-to-buffer-other-window " *Org tags*")
  13408. (org-fit-window-to-buffer)))
  13409. ((or (= c ?\C-g)
  13410. (and (= c ?q) (not (rassoc c ntable))))
  13411. (org-detach-overlay org-tags-overlay)
  13412. (setq quit-flag t))
  13413. ((= c ?\ )
  13414. (setq current nil)
  13415. (if exit-after-next (setq exit-after-next 'now)))
  13416. ((= c ?\t)
  13417. (condition-case nil
  13418. (setq tg (org-icompleting-read
  13419. "Tag: "
  13420. (or buffer-tags
  13421. (with-current-buffer buf
  13422. (org-get-buffer-tags)))))
  13423. (quit (setq tg "")))
  13424. (when (string-match "\\S-" tg)
  13425. (add-to-list 'buffer-tags (list tg))
  13426. (if (member tg current)
  13427. (setq current (delete tg current))
  13428. (push tg current)))
  13429. (if exit-after-next (setq exit-after-next 'now)))
  13430. ((setq e (rassoc c todo-table) tg (car e))
  13431. (with-current-buffer buf
  13432. (save-excursion (org-todo tg)))
  13433. (if exit-after-next (setq exit-after-next 'now)))
  13434. ((setq e (rassoc c ntable) tg (car e))
  13435. (if (member tg current)
  13436. (setq current (delete tg current))
  13437. (loop for g in groups do
  13438. (if (member tg g)
  13439. (mapc (lambda (x)
  13440. (setq current (delete x current)))
  13441. g)))
  13442. (push tg current))
  13443. (if exit-after-next (setq exit-after-next 'now))))
  13444. ;; Create a sorted list
  13445. (setq current
  13446. (sort current
  13447. (lambda (a b)
  13448. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13449. (if (eq exit-after-next 'now) (throw 'exit t))
  13450. (goto-char (point-min))
  13451. (beginning-of-line 2)
  13452. (delete-region (point) (point-at-eol))
  13453. (org-fast-tag-insert "Current" current c-face)
  13454. (org-set-current-tags-overlay current ov-prefix)
  13455. (while (re-search-forward
  13456. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13457. (setq tg (match-string 1))
  13458. (add-text-properties
  13459. (match-beginning 1) (match-end 1)
  13460. (list 'face
  13461. (cond
  13462. ((member tg current) c-face)
  13463. ((member tg inherited) i-face)
  13464. (t (get-text-property (match-beginning 1) 'face))))))
  13465. (goto-char (point-min)))))
  13466. (org-detach-overlay org-tags-overlay)
  13467. (if rtn
  13468. (mapconcat 'identity current ":")
  13469. nil))))
  13470. (defun org-get-tags-string ()
  13471. "Get the TAGS string in the current headline."
  13472. (unless (org-at-heading-p t)
  13473. (error "Not on a heading"))
  13474. (save-excursion
  13475. (beginning-of-line 1)
  13476. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13477. (org-match-string-no-properties 1)
  13478. "")))
  13479. (defun org-get-tags ()
  13480. "Get the list of tags specified in the current headline."
  13481. (org-split-string (org-get-tags-string) ":"))
  13482. (defun org-get-buffer-tags ()
  13483. "Get a table of all tags used in the buffer, for completion."
  13484. (let (tags)
  13485. (save-excursion
  13486. (goto-char (point-min))
  13487. (while (re-search-forward
  13488. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13489. (when (equal (char-after (point-at-bol 0)) ?*)
  13490. (mapc (lambda (x) (add-to-list 'tags x))
  13491. (org-split-string (org-match-string-no-properties 1) ":")))))
  13492. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13493. (mapcar 'list tags)))
  13494. ;;;; The mapping API
  13495. (defun org-map-entries (func &optional match scope &rest skip)
  13496. "Call FUNC at each headline selected by MATCH in SCOPE.
  13497. FUNC is a function or a lisp form. The function will be called without
  13498. arguments, with the cursor positioned at the beginning of the headline.
  13499. The return values of all calls to the function will be collected and
  13500. returned as a list.
  13501. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13502. does not need to preserve point. After evaluation, the cursor will be
  13503. moved to the end of the line (presumably of the headline of the
  13504. processed entry) and search continues from there. Under some
  13505. circumstances, this may not produce the wanted results. For example,
  13506. if you have removed (e.g. archived) the current (sub)tree it could
  13507. mean that the next entry will be skipped entirely. In such cases, you
  13508. can specify the position from where search should continue by making
  13509. FUNC set the variable `org-map-continue-from' to the desired buffer
  13510. position.
  13511. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13512. Only headlines that are matched by this query will be considered during
  13513. the iteration. When MATCH is nil or t, all headlines will be
  13514. visited by the iteration.
  13515. SCOPE determines the scope of this command. It can be any of:
  13516. nil The current buffer, respecting the restriction if any
  13517. tree The subtree started with the entry at point
  13518. region The entries within the active region, if any
  13519. region-start-level
  13520. The entries within the active region, but only those at
  13521. the same level than the first one.
  13522. file The current buffer, without restriction
  13523. file-with-archives
  13524. The current buffer, and any archives associated with it
  13525. agenda All agenda files
  13526. agenda-with-archives
  13527. All agenda files with any archive files associated with them
  13528. \(file1 file2 ...)
  13529. If this is a list, all files in the list will be scanned
  13530. The remaining args are treated as settings for the skipping facilities of
  13531. the scanner. The following items can be given here:
  13532. archive skip trees with the archive tag
  13533. comment skip trees with the COMMENT keyword
  13534. function or Emacs Lisp form:
  13535. will be used as value for `org-agenda-skip-function', so
  13536. whenever the function returns a position, FUNC will not be
  13537. called for that entry and search will continue from the
  13538. position returned
  13539. If your function needs to retrieve the tags including inherited tags
  13540. at the *current* entry, you can use the value of the variable
  13541. `org-scanner-tags' which will be much faster than getting the value
  13542. with `org-get-tags-at'. If your function gets properties with
  13543. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13544. to t around the call to `org-entry-properties' to get the same speedup.
  13545. Note that if your function moves around to retrieve tags and properties at
  13546. a *different* entry, you cannot use these techniques."
  13547. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13548. (not (org-region-active-p)))
  13549. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13550. (org-agenda-skip-archived-trees (memq 'archive skip))
  13551. (org-agenda-skip-comment-trees (memq 'comment skip))
  13552. (org-agenda-skip-function
  13553. (car (org-delete-all '(comment archive) skip)))
  13554. (org-tags-match-list-sublevels t)
  13555. (start-level (eq scope 'region-start-level))
  13556. matcher file res
  13557. org-todo-keywords-for-agenda
  13558. org-done-keywords-for-agenda
  13559. org-todo-keyword-alist-for-agenda
  13560. org-drawers-for-agenda
  13561. org-tag-alist-for-agenda
  13562. todo-only)
  13563. (cond
  13564. ((eq match t) (setq matcher t))
  13565. ((eq match nil) (setq matcher t))
  13566. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13567. (save-window-excursion
  13568. (save-restriction
  13569. (cond ((eq scope 'tree)
  13570. (org-back-to-heading t)
  13571. (org-narrow-to-subtree)
  13572. (setq scope nil))
  13573. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13574. (org-region-active-p))
  13575. ;; If needed, set start-level to a string like "2"
  13576. (when start-level
  13577. (save-excursion
  13578. (goto-char (region-beginning))
  13579. (unless (org-at-heading-p) (outline-next-heading))
  13580. (setq start-level (org-current-level))))
  13581. (narrow-to-region (region-beginning)
  13582. (save-excursion
  13583. (goto-char (region-end))
  13584. (unless (and (bolp) (org-at-heading-p))
  13585. (outline-next-heading))
  13586. (point)))
  13587. (setq scope nil)))
  13588. (if (not scope)
  13589. (progn
  13590. (org-agenda-prepare-buffers
  13591. (list (buffer-file-name (current-buffer))))
  13592. (setq res (org-scan-tags func matcher todo-only start-level)))
  13593. ;; Get the right scope
  13594. (cond
  13595. ((and scope (listp scope) (symbolp (car scope)))
  13596. (setq scope (eval scope)))
  13597. ((eq scope 'agenda)
  13598. (setq scope (org-agenda-files t)))
  13599. ((eq scope 'agenda-with-archives)
  13600. (setq scope (org-agenda-files t))
  13601. (setq scope (org-add-archive-files scope)))
  13602. ((eq scope 'file)
  13603. (setq scope (list (buffer-file-name))))
  13604. ((eq scope 'file-with-archives)
  13605. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13606. (org-agenda-prepare-buffers scope)
  13607. (while (setq file (pop scope))
  13608. (with-current-buffer (org-find-base-buffer-visiting file)
  13609. (save-excursion
  13610. (save-restriction
  13611. (widen)
  13612. (goto-char (point-min))
  13613. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13614. res)))
  13615. ;;;; Properties
  13616. ;;; Setting and retrieving properties
  13617. (defconst org-special-properties
  13618. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13619. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13620. "The special properties valid in Org-mode.
  13621. These are properties that are not defined in the property drawer,
  13622. but in some other way.")
  13623. (defconst org-default-properties
  13624. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13625. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13626. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13627. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13628. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13629. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13630. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13631. "Some properties that are used by Org-mode for various purposes.
  13632. Being in this list makes sure that they are offered for completion.")
  13633. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13634. "Regular expression matching the first line of a property drawer.")
  13635. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13636. "Regular expression matching the last line of a property drawer.")
  13637. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13638. "Regular expression matching the first line of a property drawer.")
  13639. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13640. "Regular expression matching the first line of a property drawer.")
  13641. (defconst org-property-drawer-re
  13642. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13643. org-property-end-re "\\)\n?")
  13644. "Matches an entire property drawer.")
  13645. (defconst org-clock-drawer-re
  13646. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13647. org-property-end-re "\\)\n?")
  13648. "Matches an entire clock drawer.")
  13649. (defsubst org-re-property (property)
  13650. "Return a regexp matching a PROPERTY line.
  13651. Match group 1 will be set to the value."
  13652. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13653. (defsubst org-re-property-keyword (property)
  13654. "Return a regexp matching a PROPERTY line, possibly with no
  13655. value for the property."
  13656. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13657. (defun org-property-action ()
  13658. "Do an action on properties."
  13659. (interactive)
  13660. (let (c)
  13661. (org-at-property-p)
  13662. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13663. (setq c (read-char-exclusive))
  13664. (cond
  13665. ((equal c ?s)
  13666. (call-interactively 'org-set-property))
  13667. ((equal c ?d)
  13668. (call-interactively 'org-delete-property))
  13669. ((equal c ?D)
  13670. (call-interactively 'org-delete-property-globally))
  13671. ((equal c ?c)
  13672. (call-interactively 'org-compute-property-at-point))
  13673. (t (error "No such property action %c" c)))))
  13674. (defun org-inc-effort ()
  13675. "Increment the value of the effort property in the current entry."
  13676. (interactive)
  13677. (org-set-effort nil t))
  13678. (defvar org-clock-effort) ;; Defined in org-clock.el
  13679. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13680. (defun org-set-effort (&optional value increment)
  13681. "Set the effort property of the current entry.
  13682. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13683. 10th allowed value.
  13684. When INCREMENT is non-nil, set the property to the next allowed value."
  13685. (interactive "P")
  13686. (if (equal value 0) (setq value 10))
  13687. (let* ((completion-ignore-case t)
  13688. (prop org-effort-property)
  13689. (cur (org-entry-get nil prop))
  13690. (allowed (org-property-get-allowed-values nil prop 'table))
  13691. (existing (mapcar 'list (org-property-values prop)))
  13692. (heading (nth 4 (org-heading-components)))
  13693. rpl
  13694. (val (cond
  13695. ((stringp value) value)
  13696. ((and allowed (integerp value))
  13697. (or (car (nth (1- value) allowed))
  13698. (car (org-last allowed))))
  13699. ((and allowed increment)
  13700. (or (caadr (member (list cur) allowed))
  13701. (error "Allowed effort values are not set")))
  13702. (allowed
  13703. (message "Select 1-9,0, [RET%s]: %s"
  13704. (if cur (concat "=" cur) "")
  13705. (mapconcat 'car allowed " "))
  13706. (setq rpl (read-char-exclusive))
  13707. (if (equal rpl ?\r)
  13708. cur
  13709. (setq rpl (- rpl ?0))
  13710. (if (equal rpl 0) (setq rpl 10))
  13711. (if (and (> rpl 0) (<= rpl (length allowed)))
  13712. (car (nth (1- rpl) allowed))
  13713. (org-completing-read "Effort: " allowed nil))))
  13714. (t
  13715. (let (org-completion-use-ido org-completion-use-iswitchb)
  13716. (org-completing-read
  13717. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13718. (concat "[" cur "]") "")
  13719. ": ")
  13720. existing nil nil "" nil cur))))))
  13721. (unless (equal (org-entry-get nil prop) val)
  13722. (org-entry-put nil prop val))
  13723. (save-excursion
  13724. (org-back-to-heading t)
  13725. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13726. (when (string= heading org-clock-current-task)
  13727. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13728. (org-clock-update-mode-line))
  13729. (message "%s is now %s" prop val)))
  13730. (defun org-at-property-p ()
  13731. "Is cursor inside a property drawer?"
  13732. (save-excursion
  13733. (beginning-of-line 1)
  13734. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13735. (save-match-data ;; Used by calling procedures
  13736. (let ((p (point))
  13737. (range (unless (org-before-first-heading-p)
  13738. (org-get-property-block))))
  13739. (and range (<= (car range) p) (< p (cdr range))))))))
  13740. (defun org-get-property-block (&optional beg end force)
  13741. "Return the (beg . end) range of the body of the property drawer.
  13742. BEG and END are the beginning and end of the current subtree, or of
  13743. the part before the first headline. If they are not given, they will
  13744. be found. If the drawer does not exist and FORCE is non-nil, create
  13745. the drawer."
  13746. (catch 'exit
  13747. (save-excursion
  13748. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13749. (progn (org-back-to-heading t) (point))))
  13750. (end (or end (and (not (outline-next-heading)) (point-max))
  13751. (point))))
  13752. (goto-char beg)
  13753. (if (re-search-forward org-property-start-re end t)
  13754. (setq beg (1+ (match-end 0)))
  13755. (if force
  13756. (save-excursion
  13757. (org-insert-property-drawer)
  13758. (setq end (progn (outline-next-heading) (point))))
  13759. (throw 'exit nil))
  13760. (goto-char beg)
  13761. (if (re-search-forward org-property-start-re end t)
  13762. (setq beg (1+ (match-end 0)))))
  13763. (if (re-search-forward org-property-end-re end t)
  13764. (setq end (match-beginning 0))
  13765. (or force (throw 'exit nil))
  13766. (goto-char beg)
  13767. (setq end beg)
  13768. (org-indent-line)
  13769. (insert ":END:\n"))
  13770. (cons beg end)))))
  13771. (defun org-entry-properties (&optional pom which specific)
  13772. "Get all properties of the entry at point-or-marker POM.
  13773. This includes the TODO keyword, the tags, time strings for deadline,
  13774. scheduled, and clocking, and any additional properties defined in the
  13775. entry. The return value is an alist, keys may occur multiple times
  13776. if the property key was used several times.
  13777. POM may also be nil, in which case the current entry is used.
  13778. If WHICH is nil or `all', get all properties. If WHICH is
  13779. `special' or `standard', only get that subclass. If WHICH
  13780. is a string only get exactly this property. SPECIFIC can be a string, the
  13781. specific property we are interested in. Specifying it can speed
  13782. things up because then unnecessary parsing is avoided."
  13783. (setq which (or which 'all))
  13784. (org-with-point-at pom
  13785. (let ((clockstr (substring org-clock-string 0 -1))
  13786. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13787. (case-fold-search nil)
  13788. beg end range props sum-props key key1 value string clocksum clocksumt)
  13789. (save-excursion
  13790. (when (condition-case nil
  13791. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13792. (error nil))
  13793. (setq beg (point))
  13794. (setq sum-props (get-text-property (point) 'org-summaries))
  13795. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13796. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13797. (outline-next-heading)
  13798. (setq end (point))
  13799. (when (memq which '(all special))
  13800. ;; Get the special properties, like TODO and tags
  13801. (goto-char beg)
  13802. (when (and (or (not specific) (string= specific "TODO"))
  13803. (looking-at org-todo-line-regexp) (match-end 2))
  13804. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13805. (when (and (or (not specific) (string= specific "PRIORITY"))
  13806. (looking-at org-priority-regexp))
  13807. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13808. (when (or (not specific) (string= specific "FILE"))
  13809. (push (cons "FILE" buffer-file-name) props))
  13810. (when (and (or (not specific) (string= specific "TAGS"))
  13811. (setq value (org-get-tags-string))
  13812. (string-match "\\S-" value))
  13813. (push (cons "TAGS" value) props))
  13814. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13815. (setq value (org-get-tags-at)))
  13816. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13817. ":"))
  13818. props))
  13819. (when (or (not specific) (string= specific "BLOCKED"))
  13820. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13821. (when (or (not specific)
  13822. (member specific
  13823. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13824. "TIMESTAMP" "TIMESTAMP_IA")))
  13825. (catch 'match
  13826. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13827. (setq key (if (match-end 1)
  13828. (substring (org-match-string-no-properties 1)
  13829. 0 -1))
  13830. string (if (equal key clockstr)
  13831. (org-trim
  13832. (buffer-substring-no-properties
  13833. (match-beginning 3) (goto-char
  13834. (point-at-eol))))
  13835. (substring (org-match-string-no-properties 3)
  13836. 1 -1)))
  13837. ;; Get the correct property name from the key. This is
  13838. ;; necessary if the user has configured time keywords.
  13839. (setq key1 (concat key ":"))
  13840. (cond
  13841. ((not key)
  13842. (setq key
  13843. (if (= (char-after (match-beginning 3)) ?\[)
  13844. "TIMESTAMP_IA" "TIMESTAMP")))
  13845. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13846. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13847. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13848. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13849. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13850. (progn
  13851. (push (cons key string) props)
  13852. ;; no need to search further if match is found
  13853. (throw 'match t))
  13854. (when (or (equal key "CLOCK") (not (assoc key props)))
  13855. (push (cons key string) props)))))))
  13856. (when (memq which '(all standard))
  13857. ;; Get the standard properties, like :PROP: ...
  13858. (setq range (org-get-property-block beg end))
  13859. (when range
  13860. (goto-char (car range))
  13861. (while (re-search-forward
  13862. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13863. (cdr range) t)
  13864. (setq key (org-match-string-no-properties 1)
  13865. value (org-trim (or (org-match-string-no-properties 2) "")))
  13866. (unless (member key excluded)
  13867. (push (cons key (or value "")) props)))))
  13868. (if clocksum
  13869. (push (cons "CLOCKSUM"
  13870. (org-columns-number-to-string (/ (float clocksum) 60.)
  13871. 'add_times))
  13872. props))
  13873. (if clocksumt
  13874. (push (cons "CLOCKSUM_T"
  13875. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13876. 'add_times))
  13877. props))
  13878. (unless (assoc "CATEGORY" props)
  13879. (push (cons "CATEGORY" (org-get-category)) props))
  13880. (append sum-props (nreverse props)))))))
  13881. (defun org-entry-get (pom property &optional inherit literal-nil)
  13882. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13883. If INHERIT is non-nil and the entry does not have the property,
  13884. then also check higher levels of the hierarchy.
  13885. If INHERIT is the symbol `selective', use inheritance only if the setting
  13886. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13887. If the property is present but empty, the return value is the empty string.
  13888. If the property is not present at all, nil is returned.
  13889. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13890. do not interpret it as the list atom nil. This is used for inheritance
  13891. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13892. (org-with-point-at pom
  13893. (if (and inherit (if (eq inherit 'selective)
  13894. (org-property-inherit-p property)
  13895. t))
  13896. (org-entry-get-with-inheritance property literal-nil)
  13897. (if (member property org-special-properties)
  13898. ;; We need a special property. Use `org-entry-properties' to
  13899. ;; retrieve it, but specify the wanted property
  13900. (cdr (assoc property (org-entry-properties nil 'special property)))
  13901. (let ((range (org-get-property-block)))
  13902. (when (and range (not (eq (car range) (cdr range))))
  13903. (let* ((props (list (or (assoc property org-file-properties)
  13904. (assoc property org-global-properties)
  13905. (assoc property org-global-properties-fixed))))
  13906. (ap (lambda (key)
  13907. (when (re-search-forward
  13908. (org-re-property key) (cdr range) t)
  13909. (setq props
  13910. (org-update-property-plist
  13911. key
  13912. (if (match-end 1)
  13913. (org-match-string-no-properties 1) "")
  13914. props)))))
  13915. val)
  13916. (goto-char (car range))
  13917. (funcall ap property)
  13918. (goto-char (car range))
  13919. (while (funcall ap (concat property "+")))
  13920. (setq val (cdr (assoc property props)))
  13921. (when val (if literal-nil val (org-not-nil val))))))))))
  13922. (defun org-property-or-variable-value (var &optional inherit)
  13923. "Check if there is a property fixing the value of VAR.
  13924. If yes, return this value. If not, return the current value of the variable."
  13925. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13926. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13927. (read prop)
  13928. (symbol-value var))))
  13929. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13930. "Delete the property PROPERTY from entry at point-or-marker POM.
  13931. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13932. an empty drawer to delete."
  13933. (org-with-point-at pom
  13934. (if (member property org-special-properties)
  13935. nil ; cannot delete these properties.
  13936. (let ((range (org-get-property-block)))
  13937. (if (and range
  13938. (goto-char (car range))
  13939. (re-search-forward
  13940. (org-re-property property)
  13941. (cdr range) t))
  13942. (progn
  13943. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13944. (and delete-empty-drawer
  13945. (org-remove-empty-drawer-at
  13946. delete-empty-drawer (car range)))
  13947. t)
  13948. nil)))))
  13949. ;; Multi-values properties are properties that contain multiple values
  13950. ;; These values are assumed to be single words, separated by whitespace.
  13951. (defun org-entry-add-to-multivalued-property (pom property value)
  13952. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13953. (let* ((old (org-entry-get pom property))
  13954. (values (and old (org-split-string old "[ \t]"))))
  13955. (setq value (org-entry-protect-space value))
  13956. (unless (member value values)
  13957. (setq values (append values (list value)))
  13958. (org-entry-put pom property
  13959. (mapconcat 'identity values " ")))))
  13960. (defun org-entry-remove-from-multivalued-property (pom property value)
  13961. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13962. (let* ((old (org-entry-get pom property))
  13963. (values (and old (org-split-string old "[ \t]"))))
  13964. (setq value (org-entry-protect-space value))
  13965. (when (member value values)
  13966. (setq values (delete value values))
  13967. (org-entry-put pom property
  13968. (mapconcat 'identity values " ")))))
  13969. (defun org-entry-member-in-multivalued-property (pom property value)
  13970. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13971. (let* ((old (org-entry-get pom property))
  13972. (values (and old (org-split-string old "[ \t]"))))
  13973. (setq value (org-entry-protect-space value))
  13974. (member value values)))
  13975. (defun org-entry-get-multivalued-property (pom property)
  13976. "Return a list of values in a multivalued property."
  13977. (let* ((value (org-entry-get pom property))
  13978. (values (and value (org-split-string value "[ \t]"))))
  13979. (mapcar 'org-entry-restore-space values)))
  13980. (defun org-entry-put-multivalued-property (pom property &rest values)
  13981. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13982. VALUES should be a list of strings. Spaces will be protected."
  13983. (org-entry-put pom property
  13984. (mapconcat 'org-entry-protect-space values " "))
  13985. (let* ((value (org-entry-get pom property))
  13986. (values (and value (org-split-string value "[ \t]"))))
  13987. (mapcar 'org-entry-restore-space values)))
  13988. (defun org-entry-protect-space (s)
  13989. "Protect spaces and newline in string S."
  13990. (while (string-match " " s)
  13991. (setq s (replace-match "%20" t t s)))
  13992. (while (string-match "\n" s)
  13993. (setq s (replace-match "%0A" t t s)))
  13994. s)
  13995. (defun org-entry-restore-space (s)
  13996. "Restore spaces and newline in string S."
  13997. (while (string-match "%20" s)
  13998. (setq s (replace-match " " t t s)))
  13999. (while (string-match "%0A" s)
  14000. (setq s (replace-match "\n" t t s)))
  14001. s)
  14002. (defvar org-entry-property-inherited-from (make-marker)
  14003. "Marker pointing to the entry from where a property was inherited.
  14004. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14005. location of the entry where the inheritance search matched. If there was
  14006. no match, the marker will point nowhere.
  14007. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14008. is set.")
  14009. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14010. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14011. The search will stop at the first ancestor which has the property defined.
  14012. If the value found is \"nil\", return nil to show that the property
  14013. should be considered as undefined (this is the meaning of nil here).
  14014. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14015. (move-marker org-entry-property-inherited-from nil)
  14016. (let (tmp)
  14017. (save-excursion
  14018. (save-restriction
  14019. (widen)
  14020. (catch 'ex
  14021. (while t
  14022. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14023. (or (ignore-errors (org-back-to-heading t))
  14024. (goto-char (point-min)))
  14025. (move-marker org-entry-property-inherited-from (point))
  14026. (throw 'ex tmp))
  14027. (or (ignore-errors (org-up-heading-safe))
  14028. (throw 'ex nil))))))
  14029. (setq tmp (or tmp
  14030. (cdr (assoc property org-file-properties))
  14031. (cdr (assoc property org-global-properties))
  14032. (cdr (assoc property org-global-properties-fixed))))
  14033. (if literal-nil tmp (org-not-nil tmp))))
  14034. (defvar org-property-changed-functions nil
  14035. "Hook called when the value of a property has changed.
  14036. Each hook function should accept two arguments, the name of the property
  14037. and the new value.")
  14038. (defun org-entry-put (pom property value)
  14039. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14040. (org-with-point-at pom
  14041. (org-back-to-heading t)
  14042. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14043. range)
  14044. (cond
  14045. ((equal property "TODO")
  14046. (when (and (stringp value) (string-match "\\S-" value)
  14047. (not (member value org-todo-keywords-1)))
  14048. (error "\"%s\" is not a valid TODO state" value))
  14049. (if (or (not value)
  14050. (not (string-match "\\S-" value)))
  14051. (setq value 'none))
  14052. (org-todo value)
  14053. (org-set-tags nil 'align))
  14054. ((equal property "PRIORITY")
  14055. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14056. (string-to-char value) ?\ ))
  14057. (org-set-tags nil 'align))
  14058. ((equal property "CLOCKSUM")
  14059. (if (not (re-search-forward
  14060. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14061. (error "Cannot find a clock log")
  14062. (goto-char (- (match-end 1) 2))
  14063. (cond
  14064. ((eq value 'earlier) (org-timestamp-down))
  14065. ((eq value 'later) (org-timestamp-up)))
  14066. (org-clock-sum-current-item)))
  14067. ((equal property "SCHEDULED")
  14068. (if (re-search-forward org-scheduled-time-regexp end t)
  14069. (cond
  14070. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14071. ((eq value 'later) (org-timestamp-change 1 'day))
  14072. (t (call-interactively 'org-schedule)))
  14073. (call-interactively 'org-schedule)))
  14074. ((equal property "DEADLINE")
  14075. (if (re-search-forward org-deadline-time-regexp end t)
  14076. (cond
  14077. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14078. ((eq value 'later) (org-timestamp-change 1 'day))
  14079. (t (call-interactively 'org-deadline)))
  14080. (call-interactively 'org-deadline)))
  14081. ((member property org-special-properties)
  14082. (error "The %s property can not yet be set with `org-entry-put'"
  14083. property))
  14084. (t ; a non-special property
  14085. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14086. (setq range (org-get-property-block beg end 'force))
  14087. (goto-char (car range))
  14088. (if (re-search-forward
  14089. (org-re-property-keyword property) (cdr range) t)
  14090. (progn
  14091. (delete-region (match-beginning 0) (match-end 0))
  14092. (goto-char (match-beginning 0)))
  14093. (goto-char (cdr range))
  14094. (insert "\n")
  14095. (backward-char 1)
  14096. (org-indent-line))
  14097. (insert ":" property ":")
  14098. (and value (insert " " value))
  14099. (org-indent-line)))))
  14100. (run-hook-with-args 'org-property-changed-functions property value)))
  14101. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14102. "Get all property keys in the current buffer.
  14103. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14104. like tags and TODO state.
  14105. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14106. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14107. and others.
  14108. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14109. formats in the current buffer."
  14110. (let (rtn range cfmt s p)
  14111. (save-excursion
  14112. (save-restriction
  14113. (widen)
  14114. (goto-char (point-min))
  14115. (while (re-search-forward org-property-start-re nil t)
  14116. (setq range (org-get-property-block))
  14117. (goto-char (car range))
  14118. (while (re-search-forward
  14119. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14120. (cdr range) t)
  14121. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14122. (outline-next-heading))))
  14123. (when include-specials
  14124. (setq rtn (append org-special-properties rtn)))
  14125. (when include-defaults
  14126. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14127. (add-to-list 'rtn org-effort-property))
  14128. (when include-columns
  14129. (save-excursion
  14130. (save-restriction
  14131. (widen)
  14132. (goto-char (point-min))
  14133. (while (re-search-forward
  14134. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14135. nil t)
  14136. (setq cfmt (match-string 2) s 0)
  14137. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14138. cfmt s)
  14139. (setq s (match-end 0)
  14140. p (match-string 1 cfmt))
  14141. (unless (or (equal p "ITEM")
  14142. (member p org-special-properties))
  14143. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14144. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14145. (defun org-property-values (key)
  14146. "Return a list of all values of property KEY in the current buffer."
  14147. (save-excursion
  14148. (save-restriction
  14149. (widen)
  14150. (goto-char (point-min))
  14151. (let ((re (org-re-property key))
  14152. values)
  14153. (while (re-search-forward re nil t)
  14154. (add-to-list 'values (org-trim (match-string 1))))
  14155. (delete "" values)))))
  14156. (defun org-insert-property-drawer ()
  14157. "Insert a property drawer into the current entry."
  14158. (org-back-to-heading t)
  14159. (looking-at org-outline-regexp)
  14160. (let ((indent (if org-adapt-indentation
  14161. (- (match-end 0) (match-beginning 0))
  14162. 0))
  14163. (beg (point))
  14164. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14165. end hiddenp)
  14166. (outline-next-heading)
  14167. (setq end (point))
  14168. (goto-char beg)
  14169. (while (re-search-forward re end t))
  14170. (setq hiddenp (outline-invisible-p))
  14171. (end-of-line 1)
  14172. (and (equal (char-after) ?\n) (forward-char 1))
  14173. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14174. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14175. ;; just skip this line
  14176. (beginning-of-line 2)
  14177. ;; Drawer start, find the end
  14178. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14179. (beginning-of-line 1)))
  14180. (org-skip-over-state-notes)
  14181. (skip-chars-backward " \t\n\r")
  14182. (if (eq (char-before) ?*) (forward-char 1))
  14183. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14184. (beginning-of-line 0)
  14185. (org-indent-to-column indent)
  14186. (beginning-of-line 2)
  14187. (org-indent-to-column indent)
  14188. (beginning-of-line 0)
  14189. (if hiddenp
  14190. (save-excursion
  14191. (org-back-to-heading t)
  14192. (hide-entry))
  14193. (org-flag-drawer t))))
  14194. (defun org-insert-drawer (&optional arg drawer)
  14195. "Insert a drawer at point.
  14196. Optional argument DRAWER, when non-nil, is a string representing
  14197. drawer's name. Otherwise, the user is prompted for a name.
  14198. If a region is active, insert the drawer around that region
  14199. instead.
  14200. Point is left between drawer's boundaries."
  14201. (interactive "P")
  14202. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14203. "LOGBOOK"))
  14204. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14205. ;; internally by Org. They are meant to be inserted
  14206. ;; automatically.
  14207. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14208. ;; Remove system drawers from list. Note: For some reason,
  14209. ;; `org-completing-read' ignores the predicate while
  14210. ;; `completing-read' handles it fine.
  14211. (drawer (if arg "PROPERTIES"
  14212. (or drawer
  14213. (completing-read
  14214. "Drawer: " org-drawers
  14215. (lambda (d) (not (member d system-drawers))))))))
  14216. (cond
  14217. ;; With C-u, fall back on `org-insert-property-drawer'
  14218. (arg (org-insert-property-drawer))
  14219. ;; With an active region, insert a drawer at point.
  14220. ((not (org-region-active-p))
  14221. (progn
  14222. (unless (bolp) (insert "\n"))
  14223. (insert (format ":%s:\n\n:END:\n" drawer))
  14224. (forward-line -2)))
  14225. ;; Otherwise, insert the drawer at point
  14226. (t
  14227. (let ((rbeg (region-beginning))
  14228. (rend (copy-marker (region-end))))
  14229. (unwind-protect
  14230. (progn
  14231. (goto-char rbeg)
  14232. (beginning-of-line)
  14233. (when (save-excursion
  14234. (re-search-forward org-outline-regexp-bol rend t))
  14235. (error "Drawers cannot contain headlines"))
  14236. ;; Position point at the beginning of the first
  14237. ;; non-blank line in region. Insert drawer's opening
  14238. ;; there, then indent it.
  14239. (org-skip-whitespace)
  14240. (beginning-of-line)
  14241. (insert ":" drawer ":\n")
  14242. (forward-line -1)
  14243. (indent-for-tab-command)
  14244. ;; Move point to the beginning of the first blank line
  14245. ;; after the last non-blank line in region. Insert
  14246. ;; drawer's closing, then indent it.
  14247. (goto-char rend)
  14248. (skip-chars-backward " \r\t\n")
  14249. (insert "\n:END:")
  14250. (deactivate-mark t)
  14251. (indent-for-tab-command)
  14252. (unless (eolp) (insert "\n")))
  14253. ;; Clear marker, whatever the outcome of insertion is.
  14254. (set-marker rend nil)))))))
  14255. (defvar org-property-set-functions-alist nil
  14256. "Property set function alist.
  14257. Each entry should have the following format:
  14258. (PROPERTY . READ-FUNCTION)
  14259. The read function will be called with the same argument as
  14260. `org-completing-read'.")
  14261. (defun org-set-property-function (property)
  14262. "Get the function that should be used to set PROPERTY.
  14263. This is computed according to `org-property-set-functions-alist'."
  14264. (or (cdr (assoc property org-property-set-functions-alist))
  14265. 'org-completing-read))
  14266. (defun org-read-property-value (property)
  14267. "Read PROPERTY value from user."
  14268. (let* ((completion-ignore-case t)
  14269. (allowed (org-property-get-allowed-values nil property 'table))
  14270. (cur (org-entry-get nil property))
  14271. (prompt (concat property " value"
  14272. (if (and cur (string-match "\\S-" cur))
  14273. (concat " [" cur "]") "") ": "))
  14274. (set-function (org-set-property-function property))
  14275. (val (if allowed
  14276. (funcall set-function prompt allowed nil
  14277. (not (get-text-property 0 'org-unrestricted
  14278. (caar allowed))))
  14279. (let (org-completion-use-ido org-completion-use-iswitchb)
  14280. (funcall set-function prompt
  14281. (mapcar 'list (org-property-values property))
  14282. nil nil "" nil cur)))))
  14283. (if (equal val "")
  14284. cur
  14285. val)))
  14286. (defvar org-last-set-property nil)
  14287. (defvar org-last-set-property-value nil)
  14288. (defun org-read-property-name ()
  14289. "Read a property name."
  14290. (let* ((completion-ignore-case t)
  14291. (keys (org-buffer-property-keys nil t t))
  14292. (default-prop (or (save-excursion
  14293. (save-match-data
  14294. (beginning-of-line)
  14295. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14296. (null (string= (match-string 1) "END"))
  14297. (match-string 1))))
  14298. org-last-set-property))
  14299. (property (org-icompleting-read
  14300. (concat "Property"
  14301. (if default-prop (concat " [" default-prop "]") "")
  14302. ": ")
  14303. (mapcar 'list keys)
  14304. nil nil nil nil
  14305. default-prop)))
  14306. (if (member property keys)
  14307. property
  14308. (or (cdr (assoc (downcase property)
  14309. (mapcar (lambda (x) (cons (downcase x) x))
  14310. keys)))
  14311. property))))
  14312. (defun org-set-property-and-value (use-last)
  14313. "Allow to set [PROPERTY]: [value] direction from prompt.
  14314. When use-default, don't even ask, just use the last
  14315. \"[PROPERTY]: [value]\" string from the history."
  14316. (interactive "P")
  14317. (let* ((completion-ignore-case t)
  14318. (pv (or (and use-last org-last-set-property-value)
  14319. (org-completing-read
  14320. "Enter a \"[Property]: [value]\" pair: "
  14321. nil nil nil nil nil
  14322. org-last-set-property-value)))
  14323. prop val)
  14324. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14325. (setq prop (match-string 1 pv)
  14326. val (match-string 2 pv))
  14327. (org-set-property prop val))))
  14328. (defun org-set-property (property value)
  14329. "In the current entry, set PROPERTY to VALUE.
  14330. When called interactively, this will prompt for a property name, offering
  14331. completion on existing and default properties. And then it will prompt
  14332. for a value, offering completion either on allowed values (via an inherited
  14333. xxx_ALL property) or on existing values in other instances of this property
  14334. in the current file."
  14335. (interactive (list nil nil))
  14336. (let* ((property (or property (org-read-property-name)))
  14337. (value (or value (org-read-property-value property)))
  14338. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14339. (setq org-last-set-property property)
  14340. (setq org-last-set-property-value (concat property ": " value))
  14341. ;; Possibly postprocess the inserted value:
  14342. (when fn (setq value (funcall fn value)))
  14343. (unless (equal (org-entry-get nil property) value)
  14344. (org-entry-put nil property value))))
  14345. (defun org-delete-property (property &optional delete-empty-drawer)
  14346. "In the current entry, delete PROPERTY.
  14347. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14348. an empty drawer to delete."
  14349. (interactive
  14350. (let* ((completion-ignore-case t)
  14351. (prop (org-icompleting-read "Property: "
  14352. (org-entry-properties nil 'standard))))
  14353. (list prop)))
  14354. (message "Property %s %s" property
  14355. (if (org-entry-delete nil property delete-empty-drawer)
  14356. "deleted"
  14357. "was not present in the entry")))
  14358. (defun org-delete-property-globally (property)
  14359. "Remove PROPERTY globally, from all entries."
  14360. (interactive
  14361. (let* ((completion-ignore-case t)
  14362. (prop (org-icompleting-read
  14363. "Globally remove property: "
  14364. (mapcar 'list (org-buffer-property-keys)))))
  14365. (list prop)))
  14366. (save-excursion
  14367. (save-restriction
  14368. (widen)
  14369. (goto-char (point-min))
  14370. (let ((cnt 0))
  14371. (while (re-search-forward
  14372. (org-re-property property)
  14373. nil t)
  14374. (setq cnt (1+ cnt))
  14375. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14376. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14377. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14378. (defun org-compute-property-at-point ()
  14379. "Compute the property at point.
  14380. This looks for an enclosing column format, extracts the operator and
  14381. then applies it to the property in the column format's scope."
  14382. (interactive)
  14383. (unless (org-at-property-p)
  14384. (error "Not at a property"))
  14385. (let ((prop (org-match-string-no-properties 2)))
  14386. (org-columns-get-format-and-top-level)
  14387. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14388. (error "No operator defined for property %s" prop))
  14389. (org-columns-compute prop)))
  14390. (defvar org-property-allowed-value-functions nil
  14391. "Hook for functions supplying allowed values for a specific property.
  14392. The functions must take a single argument, the name of the property, and
  14393. return a flat list of allowed values. If \":ETC\" is one of
  14394. the values, this means that these values are intended as defaults for
  14395. completion, but that other values should be allowed too.
  14396. The functions must return nil if they are not responsible for this
  14397. property.")
  14398. (defun org-property-get-allowed-values (pom property &optional table)
  14399. "Get allowed values for the property PROPERTY.
  14400. When TABLE is non-nil, return an alist that can directly be used for
  14401. completion."
  14402. (let (vals)
  14403. (cond
  14404. ((equal property "TODO")
  14405. (setq vals (org-with-point-at pom
  14406. (append org-todo-keywords-1 '("")))))
  14407. ((equal property "PRIORITY")
  14408. (let ((n org-lowest-priority))
  14409. (while (>= n org-highest-priority)
  14410. (push (char-to-string n) vals)
  14411. (setq n (1- n)))))
  14412. ((member property org-special-properties))
  14413. ((setq vals (run-hook-with-args-until-success
  14414. 'org-property-allowed-value-functions property)))
  14415. (t
  14416. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14417. (when (and vals (string-match "\\S-" vals))
  14418. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14419. (setq vals (mapcar (lambda (x)
  14420. (cond ((stringp x) x)
  14421. ((numberp x) (number-to-string x))
  14422. ((symbolp x) (symbol-name x))
  14423. (t "???")))
  14424. vals)))))
  14425. (when (member ":ETC" vals)
  14426. (setq vals (remove ":ETC" vals))
  14427. (org-add-props (car vals) '(org-unrestricted t)))
  14428. (if table (mapcar 'list vals) vals)))
  14429. (defun org-property-previous-allowed-value (&optional previous)
  14430. "Switch to the next allowed value for this property."
  14431. (interactive)
  14432. (org-property-next-allowed-value t))
  14433. (defun org-property-next-allowed-value (&optional previous)
  14434. "Switch to the next allowed value for this property."
  14435. (interactive)
  14436. (unless (org-at-property-p)
  14437. (error "Not at a property"))
  14438. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14439. (key (match-string 2))
  14440. (value (match-string 3))
  14441. (allowed (or (org-property-get-allowed-values (point) key)
  14442. (and (member value '("[ ]" "[-]" "[X]"))
  14443. '("[ ]" "[X]"))))
  14444. (heading (save-match-data (nth 4 (org-heading-components))))
  14445. nval)
  14446. (unless allowed
  14447. (error "Allowed values for this property have not been defined"))
  14448. (if previous (setq allowed (reverse allowed)))
  14449. (if (member value allowed)
  14450. (setq nval (car (cdr (member value allowed)))))
  14451. (setq nval (or nval (car allowed)))
  14452. (if (equal nval value)
  14453. (error "Only one allowed value for this property"))
  14454. (org-at-property-p)
  14455. (replace-match (concat " :" key ": " nval) t t)
  14456. (org-indent-line)
  14457. (beginning-of-line 1)
  14458. (skip-chars-forward " \t")
  14459. (when (equal prop org-effort-property)
  14460. (save-excursion
  14461. (org-back-to-heading t)
  14462. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14463. (when (string= org-clock-current-task heading)
  14464. (setq org-clock-effort nval)
  14465. (org-clock-update-mode-line)))
  14466. (run-hook-with-args 'org-property-changed-functions key nval)))
  14467. (defun org-find-olp (path &optional this-buffer)
  14468. "Return a marker pointing to the entry at outline path OLP.
  14469. If anything goes wrong, throw an error.
  14470. You can wrap this call to catch the error like this:
  14471. (condition-case msg
  14472. (org-mobile-locate-entry (match-string 4))
  14473. (error (nth 1 msg)))
  14474. The return value will then be either a string with the error message,
  14475. or a marker if everything is OK.
  14476. If THIS-BUFFER is set, the outline path does not contain a file,
  14477. only headings."
  14478. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14479. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14480. (level 1)
  14481. (lmin 1)
  14482. (lmax 1)
  14483. limit re end found pos heading cnt flevel)
  14484. (unless buffer (error "File not found :%s" file))
  14485. (with-current-buffer buffer
  14486. (save-excursion
  14487. (save-restriction
  14488. (widen)
  14489. (setq limit (point-max))
  14490. (goto-char (point-min))
  14491. (while (setq heading (pop path))
  14492. (setq re (format org-complex-heading-regexp-format
  14493. (regexp-quote heading)))
  14494. (setq cnt 0 pos (point))
  14495. (while (re-search-forward re end t)
  14496. (setq level (- (match-end 1) (match-beginning 1)))
  14497. (if (and (>= level lmin) (<= level lmax))
  14498. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14499. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14500. lmax heading))
  14501. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14502. lmax heading))
  14503. (goto-char found)
  14504. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14505. (setq end (save-excursion (org-end-of-subtree t t))))
  14506. (when (org-at-heading-p)
  14507. (point-marker)))))))
  14508. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14509. "Find node HEADING in BUFFER.
  14510. Return a marker to the heading if it was found, or nil if not.
  14511. If POS-ONLY is set, return just the position instead of a marker.
  14512. The heading text must match exact, but it may have a TODO keyword,
  14513. a priority cookie and tags in the standard locations."
  14514. (with-current-buffer (or buffer (current-buffer))
  14515. (save-excursion
  14516. (save-restriction
  14517. (widen)
  14518. (goto-char (point-min))
  14519. (let (case-fold-search)
  14520. (if (re-search-forward
  14521. (format org-complex-heading-regexp-format
  14522. (regexp-quote heading)) nil t)
  14523. (if pos-only
  14524. (match-beginning 0)
  14525. (move-marker (make-marker) (match-beginning 0)))))))))
  14526. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14527. "Find Org node headline HEADING in all .org files in directory DIR.
  14528. When the target headline is found, return a marker to this location."
  14529. (let ((files (directory-files (or dir default-directory)
  14530. nil "\\`[^.#].*\\.org\\'"))
  14531. file visiting m buffer)
  14532. (catch 'found
  14533. (while (setq file (pop files))
  14534. (message "trying %s" file)
  14535. (setq visiting (org-find-base-buffer-visiting file))
  14536. (setq buffer (or visiting (find-file-noselect file)))
  14537. (setq m (org-find-exact-headline-in-buffer
  14538. heading buffer))
  14539. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14540. (and m (throw 'found m))))))
  14541. (defun org-find-entry-with-id (ident)
  14542. "Locate the entry that contains the ID property with exact value IDENT.
  14543. IDENT can be a string, a symbol or a number, this function will search for
  14544. the string representation of it.
  14545. Return the position where this entry starts, or nil if there is no such entry."
  14546. (interactive "sID: ")
  14547. (let ((id (cond
  14548. ((stringp ident) ident)
  14549. ((symbol-name ident) (symbol-name ident))
  14550. ((numberp ident) (number-to-string ident))
  14551. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14552. (case-fold-search nil))
  14553. (save-excursion
  14554. (save-restriction
  14555. (widen)
  14556. (goto-char (point-min))
  14557. (when (re-search-forward
  14558. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14559. nil t)
  14560. (org-back-to-heading t)
  14561. (point))))))
  14562. ;;;; Timestamps
  14563. (defvar org-last-changed-timestamp nil)
  14564. (defvar org-last-inserted-timestamp nil
  14565. "The last time stamp inserted with `org-insert-time-stamp'.")
  14566. (defvar org-time-was-given) ; dynamically scoped parameter
  14567. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14568. (defvar org-ts-what) ; dynamically scoped parameter
  14569. (defun org-time-stamp (arg &optional inactive)
  14570. "Prompt for a date/time and insert a time stamp.
  14571. If the user specifies a time like HH:MM or if this command is
  14572. called with at least one prefix argument, the time stamp contains
  14573. the date and the time. Otherwise, only the date is be included.
  14574. All parts of a date not specified by the user is filled in from
  14575. the current date/time. So if you just press return without
  14576. typing anything, the time stamp will represent the current
  14577. date/time.
  14578. If there is already a timestamp at the cursor, it will be
  14579. modified.
  14580. With two universal prefix arguments, insert an active timestamp
  14581. with the current time without prompting the user."
  14582. (interactive "P")
  14583. (let* ((ts nil)
  14584. (default-time
  14585. ;; Default time is either today, or, when entering a range,
  14586. ;; the range start.
  14587. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14588. (save-excursion
  14589. (re-search-backward
  14590. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14591. (- (point) 20) t)))
  14592. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14593. (current-time)))
  14594. (default-input (and ts (org-get-compact-tod ts)))
  14595. (repeater (save-excursion
  14596. (save-match-data
  14597. (beginning-of-line)
  14598. (when (re-search-forward
  14599. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14600. (save-excursion (progn (end-of-line) (point))) t)
  14601. (match-string 0)))))
  14602. org-time-was-given org-end-time-was-given time)
  14603. (cond
  14604. ((and (org-at-timestamp-p t)
  14605. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14606. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14607. (insert "--")
  14608. (setq time (let ((this-command this-command))
  14609. (org-read-date arg 'totime nil nil
  14610. default-time default-input inactive)))
  14611. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14612. ((org-at-timestamp-p t)
  14613. (setq time (let ((this-command this-command))
  14614. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14615. (when (org-at-timestamp-p t) ; just to get the match data
  14616. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14617. (replace-match "")
  14618. (setq org-last-changed-timestamp
  14619. (org-insert-time-stamp
  14620. time (or org-time-was-given arg)
  14621. inactive nil nil (list org-end-time-was-given)))
  14622. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14623. (setq org-last-changed-timestamp
  14624. (concat (substring org-last-inserted-timestamp 0 -1)
  14625. " " repeater ">"))))
  14626. (message "Timestamp updated"))
  14627. ((equal arg '(16))
  14628. (org-insert-time-stamp (current-time) t))
  14629. (t
  14630. (setq time (let ((this-command this-command))
  14631. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14632. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14633. nil nil (list org-end-time-was-given))))))
  14634. ;; FIXME: can we use this for something else, like computing time differences?
  14635. (defun org-get-compact-tod (s)
  14636. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14637. (let* ((t1 (match-string 1 s))
  14638. (h1 (string-to-number (match-string 2 s)))
  14639. (m1 (string-to-number (match-string 3 s)))
  14640. (t2 (and (match-end 4) (match-string 5 s)))
  14641. (h2 (and t2 (string-to-number (match-string 6 s))))
  14642. (m2 (and t2 (string-to-number (match-string 7 s))))
  14643. dh dm)
  14644. (if (not t2)
  14645. t1
  14646. (setq dh (- h2 h1) dm (- m2 m1))
  14647. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14648. (concat t1 "+" (number-to-string dh)
  14649. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14650. (defun org-time-stamp-inactive (&optional arg)
  14651. "Insert an inactive time stamp.
  14652. An inactive time stamp is enclosed in square brackets instead of angle
  14653. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14654. does not link to the calendar and cannot be changed with the S-cursor keys.
  14655. So these are more for recording a certain time/date."
  14656. (interactive "P")
  14657. (org-time-stamp arg 'inactive))
  14658. (defvar org-date-ovl (make-overlay 1 1))
  14659. (overlay-put org-date-ovl 'face 'org-date-selected)
  14660. (org-detach-overlay org-date-ovl)
  14661. (defvar org-ans1) ; dynamically scoped parameter
  14662. (defvar org-ans2) ; dynamically scoped parameter
  14663. (defvar org-plain-time-of-day-regexp) ; defined below
  14664. (defvar org-overriding-default-time nil) ; dynamically scoped
  14665. (defvar org-read-date-overlay nil)
  14666. (defvar org-dcst nil) ; dynamically scoped
  14667. (defvar org-read-date-history nil)
  14668. (defvar org-read-date-final-answer nil)
  14669. (defvar org-read-date-analyze-futurep nil)
  14670. (defvar org-read-date-analyze-forced-year nil)
  14671. (defvar org-read-date-inactive)
  14672. (defvar org-read-date-minibuffer-local-map
  14673. (let ((map (make-sparse-keymap)))
  14674. (set-keymap-parent map minibuffer-local-map)
  14675. (org-defkey map (kbd ".")
  14676. (lambda () (interactive)
  14677. (org-eval-in-calendar '(calendar-goto-today))))
  14678. (org-defkey map [(meta shift left)]
  14679. (lambda () (interactive)
  14680. (org-eval-in-calendar '(calendar-backward-month 1))))
  14681. (org-defkey map [(meta shift right)]
  14682. (lambda () (interactive)
  14683. (org-eval-in-calendar '(calendar-forward-month 1))))
  14684. (org-defkey map [(meta shift up)]
  14685. (lambda () (interactive)
  14686. (org-eval-in-calendar '(calendar-backward-year 1))))
  14687. (org-defkey map [(meta shift down)]
  14688. (lambda () (interactive)
  14689. (org-eval-in-calendar '(calendar-forward-year 1))))
  14690. (org-defkey map [?\e (shift left)]
  14691. (lambda () (interactive)
  14692. (org-eval-in-calendar '(calendar-backward-month 1))))
  14693. (org-defkey map [?\e (shift right)]
  14694. (lambda () (interactive)
  14695. (org-eval-in-calendar '(calendar-forward-month 1))))
  14696. (org-defkey map [?\e (shift up)]
  14697. (lambda () (interactive)
  14698. (org-eval-in-calendar '(calendar-backward-year 1))))
  14699. (org-defkey map [?\e (shift down)]
  14700. (lambda () (interactive)
  14701. (org-eval-in-calendar '(calendar-forward-year 1))))
  14702. (org-defkey map [(shift up)]
  14703. (lambda () (interactive)
  14704. (org-eval-in-calendar '(calendar-backward-week 1))))
  14705. (org-defkey map [(shift down)]
  14706. (lambda () (interactive)
  14707. (org-eval-in-calendar '(calendar-forward-week 1))))
  14708. (org-defkey map [(shift left)]
  14709. (lambda () (interactive)
  14710. (org-eval-in-calendar '(calendar-backward-day 1))))
  14711. (org-defkey map [(shift right)]
  14712. (lambda () (interactive)
  14713. (org-eval-in-calendar '(calendar-forward-day 1))))
  14714. (org-defkey map "!"
  14715. (lambda () (interactive)
  14716. (org-eval-in-calendar '(diary-view-entries))
  14717. (message "")))
  14718. (org-defkey map ">"
  14719. (lambda () (interactive)
  14720. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14721. (org-defkey map "<"
  14722. (lambda () (interactive)
  14723. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14724. (org-defkey map "\C-v"
  14725. (lambda () (interactive)
  14726. (org-eval-in-calendar
  14727. '(calendar-scroll-left-three-months 1))))
  14728. (org-defkey map "\M-v"
  14729. (lambda () (interactive)
  14730. (org-eval-in-calendar
  14731. '(calendar-scroll-right-three-months 1))))
  14732. map)
  14733. "Keymap for minibuffer commands when using `org-read-date'.")
  14734. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14735. default-time default-input inactive)
  14736. "Read a date, possibly a time, and make things smooth for the user.
  14737. The prompt will suggest to enter an ISO date, but you can also enter anything
  14738. which will at least partially be understood by `parse-time-string'.
  14739. Unrecognized parts of the date will default to the current day, month, year,
  14740. hour and minute. If this command is called to replace a timestamp at point,
  14741. or to enter the second timestamp of a range, the default time is taken
  14742. from the existing stamp. Furthermore, the command prefers the future,
  14743. so if you are giving a date where the year is not given, and the day-month
  14744. combination is already past in the current year, it will assume you
  14745. mean next year. For details, see the manual. A few examples:
  14746. 3-2-5 --> 2003-02-05
  14747. feb 15 --> currentyear-02-15
  14748. 2/15 --> currentyear-02-15
  14749. sep 12 9 --> 2009-09-12
  14750. 12:45 --> today 12:45
  14751. 22 sept 0:34 --> currentyear-09-22 0:34
  14752. 12 --> currentyear-currentmonth-12
  14753. Fri --> nearest Friday (today or later)
  14754. etc.
  14755. Furthermore you can specify a relative date by giving, as the *first* thing
  14756. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14757. change in days weeks, months, years.
  14758. With a single plus or minus, the date is relative to today. With a double
  14759. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14760. +4d --> four days from today
  14761. +4 --> same as above
  14762. +2w --> two weeks from today
  14763. ++5 --> five days from default date
  14764. The function understands only English month and weekday abbreviations.
  14765. While prompting, a calendar is popped up - you can also select the
  14766. date with the mouse (button 1). The calendar shows a period of three
  14767. months. To scroll it to other months, use the keys `>' and `<'.
  14768. If you don't like the calendar, turn it off with
  14769. \(setq org-read-date-popup-calendar nil)
  14770. With optional argument TO-TIME, the date will immediately be converted
  14771. to an internal time.
  14772. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14773. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14774. still enter a time, and this function will inform the calling routine
  14775. about this change. The calling routine may then choose to change the
  14776. format used to insert the time stamp into the buffer to include the time.
  14777. With optional argument FROM-STRING, read from this string instead from
  14778. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14779. the time/date that is used for everything that is not specified by the
  14780. user."
  14781. (require 'parse-time)
  14782. (let* ((org-time-stamp-rounding-minutes
  14783. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14784. (org-dcst org-display-custom-times)
  14785. (ct (org-current-time))
  14786. (org-def (or org-overriding-default-time default-time ct))
  14787. (org-defdecode (decode-time org-def))
  14788. (dummy (progn
  14789. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14790. (setcar (nthcdr 2 org-defdecode) -1)
  14791. (setcar (nthcdr 1 org-defdecode) 59)
  14792. (setq org-def (apply 'encode-time org-defdecode)
  14793. org-defdecode (decode-time org-def)))))
  14794. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14795. (calendar-frame-setup nil)
  14796. (calendar-setup nil)
  14797. (calendar-move-hook nil)
  14798. (calendar-view-diary-initially-flag nil)
  14799. (calendar-view-holidays-initially-flag nil)
  14800. (timestr (format-time-string
  14801. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14802. (prompt (concat (if prompt (concat prompt " ") "")
  14803. (format "Date+time [%s]: " timestr)))
  14804. ans (org-ans0 "") org-ans1 org-ans2 final)
  14805. (cond
  14806. (from-string (setq ans from-string))
  14807. (org-read-date-popup-calendar
  14808. (save-excursion
  14809. (save-window-excursion
  14810. (calendar)
  14811. (org-eval-in-calendar '(setq cursor-type nil) t)
  14812. (unwind-protect
  14813. (progn
  14814. (calendar-forward-day (- (time-to-days org-def)
  14815. (calendar-absolute-from-gregorian
  14816. (calendar-current-date))))
  14817. (org-eval-in-calendar nil t)
  14818. (let* ((old-map (current-local-map))
  14819. (map (copy-keymap calendar-mode-map))
  14820. (minibuffer-local-map
  14821. (copy-keymap org-read-date-minibuffer-local-map)))
  14822. (org-defkey map (kbd "RET") 'org-calendar-select)
  14823. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14824. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14825. (unwind-protect
  14826. (progn
  14827. (use-local-map map)
  14828. (setq org-read-date-inactive inactive)
  14829. (add-hook 'post-command-hook 'org-read-date-display)
  14830. (setq org-ans0 (read-string prompt default-input
  14831. 'org-read-date-history nil))
  14832. ;; org-ans0: from prompt
  14833. ;; org-ans1: from mouse click
  14834. ;; org-ans2: from calendar motion
  14835. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14836. (remove-hook 'post-command-hook 'org-read-date-display)
  14837. (use-local-map old-map)
  14838. (when org-read-date-overlay
  14839. (delete-overlay org-read-date-overlay)
  14840. (setq org-read-date-overlay nil)))))
  14841. (bury-buffer "*Calendar*")))))
  14842. (t ; Naked prompt only
  14843. (unwind-protect
  14844. (setq ans (read-string prompt default-input
  14845. 'org-read-date-history timestr))
  14846. (when org-read-date-overlay
  14847. (delete-overlay org-read-date-overlay)
  14848. (setq org-read-date-overlay nil)))))
  14849. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14850. (when org-read-date-analyze-forced-year
  14851. (message "Year was forced into %s"
  14852. (if org-read-date-force-compatible-dates
  14853. "compatible range (1970-2037)"
  14854. "range representable on this machine"))
  14855. (ding))
  14856. ;; One round trip to get rid of 34th of August and stuff like that....
  14857. (setq final (decode-time (apply 'encode-time final)))
  14858. (setq org-read-date-final-answer ans)
  14859. (if to-time
  14860. (apply 'encode-time final)
  14861. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14862. (format "%04d-%02d-%02d %02d:%02d"
  14863. (nth 5 final) (nth 4 final) (nth 3 final)
  14864. (nth 2 final) (nth 1 final))
  14865. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14866. (defvar org-def)
  14867. (defvar org-defdecode)
  14868. (defvar org-with-time)
  14869. (defun org-read-date-display ()
  14870. "Display the current date prompt interpretation in the minibuffer."
  14871. (when org-read-date-display-live
  14872. (when org-read-date-overlay
  14873. (delete-overlay org-read-date-overlay))
  14874. (when (minibufferp (current-buffer))
  14875. (save-excursion
  14876. (end-of-line 1)
  14877. (while (not (equal (buffer-substring
  14878. (max (point-min) (- (point) 4)) (point))
  14879. " "))
  14880. (insert " ")))
  14881. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14882. " " (or org-ans1 org-ans2)))
  14883. (org-end-time-was-given nil)
  14884. (f (org-read-date-analyze ans org-def org-defdecode))
  14885. (fmts (if org-dcst
  14886. org-time-stamp-custom-formats
  14887. org-time-stamp-formats))
  14888. (fmt (if (or org-with-time
  14889. (and (boundp 'org-time-was-given) org-time-was-given))
  14890. (cdr fmts)
  14891. (car fmts)))
  14892. (txt (format-time-string fmt (apply 'encode-time f)))
  14893. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14894. (txt (concat "=> " txt)))
  14895. (when (and org-end-time-was-given
  14896. (string-match org-plain-time-of-day-regexp txt))
  14897. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14898. org-end-time-was-given
  14899. (substring txt (match-end 0)))))
  14900. (when org-read-date-analyze-futurep
  14901. (setq txt (concat txt " (=>F)")))
  14902. (setq org-read-date-overlay
  14903. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14904. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14905. (defun org-read-date-analyze (ans org-def org-defdecode)
  14906. "Analyze the combined answer of the date prompt."
  14907. ;; FIXME: cleanup and comment
  14908. (let ((nowdecode (decode-time (current-time)))
  14909. delta deltan deltaw deltadef year month day
  14910. hour minute second wday pm h2 m2 tl wday1
  14911. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14912. (setq org-read-date-analyze-futurep nil
  14913. org-read-date-analyze-forced-year nil)
  14914. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14915. (setq ans "+0"))
  14916. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14917. (setq ans (replace-match "" t t ans)
  14918. deltan (car delta)
  14919. deltaw (nth 1 delta)
  14920. deltadef (nth 2 delta)))
  14921. ;; Check if there is an iso week date in there. If yes, store the
  14922. ;; info and postpone interpreting it until the rest of the parsing
  14923. ;; is done.
  14924. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14925. (setq iso-year (if (match-end 1)
  14926. (org-small-year-to-year
  14927. (string-to-number (match-string 1 ans))))
  14928. iso-weekday (if (match-end 3)
  14929. (string-to-number (match-string 3 ans)))
  14930. iso-week (string-to-number (match-string 2 ans)))
  14931. (setq ans (replace-match "" t t ans)))
  14932. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14933. (when (string-match
  14934. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14935. (setq year (if (match-end 2)
  14936. (string-to-number (match-string 2 ans))
  14937. (progn (setq kill-year t)
  14938. (string-to-number (format-time-string "%Y"))))
  14939. month (string-to-number (match-string 3 ans))
  14940. day (string-to-number (match-string 4 ans)))
  14941. (if (< year 100) (setq year (+ 2000 year)))
  14942. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14943. t nil ans)))
  14944. ;; Help matching dotted european dates
  14945. (when (string-match
  14946. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14947. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14948. (setq kill-year t)
  14949. (string-to-number (format-time-string "%Y")))
  14950. day (string-to-number (match-string 1 ans))
  14951. month (string-to-number (match-string 2 ans))
  14952. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14953. t nil ans)))
  14954. ;; Help matching american dates, like 5/30 or 5/30/7
  14955. (when (string-match
  14956. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14957. (setq year (if (match-end 4)
  14958. (string-to-number (match-string 4 ans))
  14959. (progn (setq kill-year t)
  14960. (string-to-number (format-time-string "%Y"))))
  14961. month (string-to-number (match-string 1 ans))
  14962. day (string-to-number (match-string 2 ans)))
  14963. (if (< year 100) (setq year (+ 2000 year)))
  14964. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14965. t nil ans)))
  14966. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14967. ;; If there is a time with am/pm, and *no* time without it, we convert
  14968. ;; so that matching will be successful.
  14969. (loop for i from 1 to 2 do ; twice, for end time as well
  14970. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14971. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14972. (setq hour (string-to-number (match-string 1 ans))
  14973. minute (if (match-end 3)
  14974. (string-to-number (match-string 3 ans))
  14975. 0)
  14976. pm (equal ?p
  14977. (string-to-char (downcase (match-string 4 ans)))))
  14978. (if (and (= hour 12) (not pm))
  14979. (setq hour 0)
  14980. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14981. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14982. t t ans))))
  14983. ;; Check if a time range is given as a duration
  14984. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14985. (setq hour (string-to-number (match-string 1 ans))
  14986. h2 (+ hour (string-to-number (match-string 3 ans)))
  14987. minute (string-to-number (match-string 2 ans))
  14988. m2 (+ minute (if (match-end 5) (string-to-number
  14989. (match-string 5 ans))0)))
  14990. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14991. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14992. t t ans)))
  14993. ;; Check if there is a time range
  14994. (when (boundp 'org-end-time-was-given)
  14995. (setq org-time-was-given nil)
  14996. (when (and (string-match org-plain-time-of-day-regexp ans)
  14997. (match-end 8))
  14998. (setq org-end-time-was-given (match-string 8 ans))
  14999. (setq ans (concat (substring ans 0 (match-beginning 7))
  15000. (substring ans (match-end 7))))))
  15001. (setq tl (parse-time-string ans)
  15002. day (or (nth 3 tl) (nth 3 org-defdecode))
  15003. month (or (nth 4 tl)
  15004. (if (and org-read-date-prefer-future
  15005. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15006. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15007. (nth 4 org-defdecode)))
  15008. year (or (and (not kill-year) (nth 5 tl))
  15009. (if (and org-read-date-prefer-future
  15010. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15011. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15012. (nth 5 org-defdecode)))
  15013. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15014. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15015. second (or (nth 0 tl) 0)
  15016. wday (nth 6 tl))
  15017. (when (and (eq org-read-date-prefer-future 'time)
  15018. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15019. (equal day (nth 3 nowdecode))
  15020. (equal month (nth 4 nowdecode))
  15021. (equal year (nth 5 nowdecode))
  15022. (nth 2 tl)
  15023. (or (< (nth 2 tl) (nth 2 nowdecode))
  15024. (and (= (nth 2 tl) (nth 2 nowdecode))
  15025. (nth 1 tl)
  15026. (< (nth 1 tl) (nth 1 nowdecode)))))
  15027. (setq day (1+ day)
  15028. futurep t))
  15029. ;; Special date definitions below
  15030. (cond
  15031. (iso-week
  15032. ;; There was an iso week
  15033. (require 'cal-iso)
  15034. (setq futurep nil)
  15035. (setq year (or iso-year year)
  15036. day (or iso-weekday wday 1)
  15037. wday nil ; to make sure that the trigger below does not match
  15038. iso-date (calendar-gregorian-from-absolute
  15039. (calendar-absolute-from-iso
  15040. (list iso-week day year))))
  15041. ; FIXME: Should we also push ISO weeks into the future?
  15042. ; (when (and org-read-date-prefer-future
  15043. ; (not iso-year)
  15044. ; (< (calendar-absolute-from-gregorian iso-date)
  15045. ; (time-to-days (current-time))))
  15046. ; (setq year (1+ year)
  15047. ; iso-date (calendar-gregorian-from-absolute
  15048. ; (calendar-absolute-from-iso
  15049. ; (list iso-week day year)))))
  15050. (setq month (car iso-date)
  15051. year (nth 2 iso-date)
  15052. day (nth 1 iso-date)))
  15053. (deltan
  15054. (setq futurep nil)
  15055. (unless deltadef
  15056. (let ((now (decode-time (current-time))))
  15057. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15058. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15059. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15060. ((equal deltaw "m") (setq month (+ month deltan)))
  15061. ((equal deltaw "y") (setq year (+ year deltan)))))
  15062. ((and wday (not (nth 3 tl)))
  15063. ;; Weekday was given, but no day, so pick that day in the week
  15064. ;; on or after the derived date.
  15065. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15066. (unless (equal wday wday1)
  15067. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15068. (if (and (boundp 'org-time-was-given)
  15069. (nth 2 tl))
  15070. (setq org-time-was-given t))
  15071. (if (< year 100) (setq year (+ 2000 year)))
  15072. ;; Check of the date is representable
  15073. (if org-read-date-force-compatible-dates
  15074. (progn
  15075. (if (< year 1970)
  15076. (setq year 1970 org-read-date-analyze-forced-year t))
  15077. (if (> year 2037)
  15078. (setq year 2037 org-read-date-analyze-forced-year t)))
  15079. (condition-case nil
  15080. (ignore (encode-time second minute hour day month year))
  15081. (error
  15082. (setq year (nth 5 org-defdecode))
  15083. (setq org-read-date-analyze-forced-year t))))
  15084. (setq org-read-date-analyze-futurep futurep)
  15085. (list second minute hour day month year)))
  15086. (defvar parse-time-weekdays)
  15087. (defun org-read-date-get-relative (s today default)
  15088. "Check string S for special relative date string.
  15089. TODAY and DEFAULT are internal times, for today and for a default.
  15090. Return shift list (N what def-flag)
  15091. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15092. N is the number of WHATs to shift.
  15093. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15094. the DEFAULT date rather than TODAY."
  15095. (require 'parse-time)
  15096. (when (and
  15097. (string-match
  15098. (concat
  15099. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15100. "\\([0-9]+\\)?"
  15101. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15102. "\\([ \t]\\|$\\)") s)
  15103. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15104. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15105. (string-to-char (substring (match-string 1 s) -1))
  15106. ?+))
  15107. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15108. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15109. (what (if (match-end 3) (match-string 3 s) "d"))
  15110. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15111. (date (if rel default today))
  15112. (wday (nth 6 (decode-time date)))
  15113. delta)
  15114. (if wday1
  15115. (progn
  15116. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15117. (if (= dir ?-) (setq delta (- delta 7)))
  15118. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15119. (list delta "d" rel))
  15120. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15121. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15122. "Turn a user-specified date into the internal representation.
  15123. The internal representation needed by the calendar is (month day year).
  15124. This is a wrapper to handle the brain-dead convention in calendar that
  15125. user function argument order change dependent on argument order."
  15126. (if (boundp 'calendar-date-style)
  15127. (cond
  15128. ((eq calendar-date-style 'american)
  15129. (list arg1 arg2 arg3))
  15130. ((eq calendar-date-style 'european)
  15131. (list arg2 arg1 arg3))
  15132. ((eq calendar-date-style 'iso)
  15133. (list arg2 arg3 arg1)))
  15134. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15135. (if (org-bound-and-true-p european-calendar-style)
  15136. (list arg2 arg1 arg3)
  15137. (list arg1 arg2 arg3)))))
  15138. (defun org-eval-in-calendar (form &optional keepdate)
  15139. "Eval FORM in the calendar window and return to current window.
  15140. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15141. otherwise stick to the current value of `org-ans2'."
  15142. (let ((sf (selected-frame))
  15143. (sw (selected-window)))
  15144. (select-window (get-buffer-window "*Calendar*" t))
  15145. (eval form)
  15146. (when (and (not keepdate) (calendar-cursor-to-date))
  15147. (let* ((date (calendar-cursor-to-date))
  15148. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15149. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15150. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15151. (select-window sw)
  15152. (org-select-frame-set-input-focus sf)))
  15153. (defun org-calendar-select ()
  15154. "Return to `org-read-date' with the date currently selected.
  15155. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15156. (interactive)
  15157. (when (calendar-cursor-to-date)
  15158. (let* ((date (calendar-cursor-to-date))
  15159. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15160. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15161. (if (active-minibuffer-window) (exit-minibuffer))))
  15162. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15163. "Insert a date stamp for the date given by the internal TIME.
  15164. WITH-HM means use the stamp format that includes the time of the day.
  15165. INACTIVE means use square brackets instead of angular ones, so that the
  15166. stamp will not contribute to the agenda.
  15167. PRE and POST are optional strings to be inserted before and after the
  15168. stamp.
  15169. The command returns the inserted time stamp."
  15170. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15171. stamp)
  15172. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15173. (insert-before-markers (or pre ""))
  15174. (when (listp extra)
  15175. (setq extra (car extra))
  15176. (if (and (stringp extra)
  15177. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15178. (setq extra (format "-%02d:%02d"
  15179. (string-to-number (match-string 1 extra))
  15180. (string-to-number (match-string 2 extra))))
  15181. (setq extra nil)))
  15182. (when extra
  15183. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15184. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15185. (insert-before-markers (or post ""))
  15186. (setq org-last-inserted-timestamp stamp)))
  15187. (defun org-toggle-time-stamp-overlays ()
  15188. "Toggle the use of custom time stamp formats."
  15189. (interactive)
  15190. (setq org-display-custom-times (not org-display-custom-times))
  15191. (unless org-display-custom-times
  15192. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15193. (while (setq p (next-single-property-change p 'display))
  15194. (if (and (get-text-property p 'display)
  15195. (eq (get-text-property p 'face) 'org-date))
  15196. (remove-text-properties
  15197. p (setq p (next-single-property-change p 'display))
  15198. '(display t))))
  15199. (set-buffer-modified-p bmp)))
  15200. (if (featurep 'xemacs)
  15201. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15202. (org-restart-font-lock)
  15203. (setq org-table-may-need-update t)
  15204. (if org-display-custom-times
  15205. (message "Time stamps are overlaid with custom format")
  15206. (message "Time stamp overlays removed")))
  15207. (defun org-display-custom-time (beg end)
  15208. "Overlay modified time stamp format over timestamp between BEG and END."
  15209. (let* ((ts (buffer-substring beg end))
  15210. t1 w1 with-hm tf time str w2 (off 0))
  15211. (save-match-data
  15212. (setq t1 (org-parse-time-string ts t))
  15213. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15214. (setq off (- (match-end 0) (match-beginning 0)))))
  15215. (setq end (- end off))
  15216. (setq w1 (- end beg)
  15217. with-hm (and (nth 1 t1) (nth 2 t1))
  15218. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15219. time (org-fix-decoded-time t1)
  15220. str (org-add-props
  15221. (format-time-string
  15222. (substring tf 1 -1) (apply 'encode-time time))
  15223. nil 'mouse-face 'highlight)
  15224. w2 (length str))
  15225. (if (not (= w2 w1))
  15226. (add-text-properties (1+ beg) (+ 2 beg)
  15227. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15228. (if (featurep 'xemacs)
  15229. (progn
  15230. (put-text-property beg end 'invisible t)
  15231. (put-text-property beg end 'end-glyph (make-glyph str)))
  15232. (put-text-property beg end 'display str))))
  15233. (defun org-translate-time (string)
  15234. "Translate all timestamps in STRING to custom format.
  15235. But do this only if the variable `org-display-custom-times' is set."
  15236. (when org-display-custom-times
  15237. (save-match-data
  15238. (let* ((start 0)
  15239. (re org-ts-regexp-both)
  15240. t1 with-hm inactive tf time str beg end)
  15241. (while (setq start (string-match re string start))
  15242. (setq beg (match-beginning 0)
  15243. end (match-end 0)
  15244. t1 (save-match-data
  15245. (org-parse-time-string (substring string beg end) t))
  15246. with-hm (and (nth 1 t1) (nth 2 t1))
  15247. inactive (equal (substring string beg (1+ beg)) "[")
  15248. tf (funcall (if with-hm 'cdr 'car)
  15249. org-time-stamp-custom-formats)
  15250. time (org-fix-decoded-time t1)
  15251. str (format-time-string
  15252. (concat
  15253. (if inactive "[" "<") (substring tf 1 -1)
  15254. (if inactive "]" ">"))
  15255. (apply 'encode-time time))
  15256. string (replace-match str t t string)
  15257. start (+ start (length str)))))))
  15258. string)
  15259. (defun org-fix-decoded-time (time)
  15260. "Set 0 instead of nil for the first 6 elements of time.
  15261. Don't touch the rest."
  15262. (let ((n 0))
  15263. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15264. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15265. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15266. "Difference between TIMESTAMP-STRING and now in days.
  15267. If SECONDS is non-nil, return the difference in seconds."
  15268. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15269. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15270. (funcall fdiff (current-time)))))
  15271. (defun org-deadline-close (timestamp-string &optional ndays)
  15272. "Is the time in TIMESTAMP-STRING close to the current date?"
  15273. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15274. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15275. (not (org-entry-is-done-p))))
  15276. (defun org-get-wdays (ts &optional delay zero-delay)
  15277. "Get the deadline lead time appropriate for timestring TS.
  15278. When DELAY is non-nil, get the delay time for scheduled items
  15279. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15280. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15281. don't try to find the delay cookie in the scheduled timestamp."
  15282. (let ((tv (if delay org-scheduled-delay-days
  15283. org-deadline-warning-days)))
  15284. (cond
  15285. ((or (and delay (< tv 0))
  15286. (and delay zero-delay (<= tv 0))
  15287. (and (not delay) (<= tv 0)))
  15288. ;; Enforce this value no matter what
  15289. (- tv))
  15290. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15291. ;; lead time is specified.
  15292. (floor (* (string-to-number (match-string 1 ts))
  15293. (cdr (assoc (match-string 2 ts)
  15294. '(("d" . 1) ("w" . 7)
  15295. ("m" . 30.4) ("y" . 365.25)
  15296. ("h" . 0.041667)))))))
  15297. ;; go for the default.
  15298. (t tv))))
  15299. (defun org-calendar-select-mouse (ev)
  15300. "Return to `org-read-date' with the date currently selected.
  15301. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15302. (interactive "e")
  15303. (mouse-set-point ev)
  15304. (when (calendar-cursor-to-date)
  15305. (let* ((date (calendar-cursor-to-date))
  15306. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15307. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15308. (if (active-minibuffer-window) (exit-minibuffer))))
  15309. (defun org-check-deadlines (ndays)
  15310. "Check if there are any deadlines due or past due.
  15311. A deadline is considered due if it happens within `org-deadline-warning-days'
  15312. days from today's date. If the deadline appears in an entry marked DONE,
  15313. it is not shown. The prefix arg NDAYS can be used to test that many
  15314. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15315. (interactive "P")
  15316. (let* ((org-warn-days
  15317. (cond
  15318. ((equal ndays '(4)) 100000)
  15319. (ndays (prefix-numeric-value ndays))
  15320. (t (abs org-deadline-warning-days))))
  15321. (case-fold-search nil)
  15322. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15323. (callback
  15324. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15325. (message "%d deadlines past-due or due within %d days"
  15326. (org-occur regexp nil callback)
  15327. org-warn-days)))
  15328. (defsubst org-re-timestamp (type)
  15329. "Return a regexp for timestamp TYPE.
  15330. Allowed values for TYPE are:
  15331. all: all timestamps
  15332. active: only active timestamps (<...>)
  15333. inactive: only inactive timestamps ([...])
  15334. scheduled: only scheduled timestamps
  15335. deadline: only deadline timestamps
  15336. closed: only closed time-stamps
  15337. When TYPE is nil, fall back on returning a regexp that matches
  15338. both scheduled and deadline timestamps."
  15339. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15340. ((eq type 'active) org-ts-regexp)
  15341. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15342. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15343. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15344. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15345. ((eq type 'scheduled-or-deadline)
  15346. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15347. (defun org-check-before-date (date)
  15348. "Check if there are deadlines or scheduled entries before DATE."
  15349. (interactive (list (org-read-date)))
  15350. (let ((case-fold-search nil)
  15351. (regexp (org-re-timestamp org-ts-type))
  15352. (callback
  15353. (lambda () (time-less-p
  15354. (org-time-string-to-time (match-string 1))
  15355. (org-time-string-to-time date)))))
  15356. (message "%d entries before %s"
  15357. (org-occur regexp nil callback) date)))
  15358. (defun org-check-after-date (date)
  15359. "Check if there are deadlines or scheduled entries after DATE."
  15360. (interactive (list (org-read-date)))
  15361. (let ((case-fold-search nil)
  15362. (regexp (org-re-timestamp org-ts-type))
  15363. (callback
  15364. (lambda () (not
  15365. (time-less-p
  15366. (org-time-string-to-time (match-string 1))
  15367. (org-time-string-to-time date))))))
  15368. (message "%d entries after %s"
  15369. (org-occur regexp nil callback) date)))
  15370. (defun org-check-dates-range (start-date end-date)
  15371. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15372. (interactive (list (org-read-date nil nil nil "Range starts")
  15373. (org-read-date nil nil nil "Range end")))
  15374. (let ((case-fold-search nil)
  15375. (regexp (org-re-timestamp org-ts-type))
  15376. (callback
  15377. (lambda ()
  15378. (let ((match (match-string 1)))
  15379. (and
  15380. (not (time-less-p
  15381. (org-time-string-to-time match)
  15382. (org-time-string-to-time start-date)))
  15383. (time-less-p
  15384. (org-time-string-to-time match)
  15385. (org-time-string-to-time end-date)))))))
  15386. (message "%d entries between %s and %s"
  15387. (org-occur regexp nil callback) start-date end-date)))
  15388. (defun org-evaluate-time-range (&optional to-buffer)
  15389. "Evaluate a time range by computing the difference between start and end.
  15390. Normally the result is just printed in the echo area, but with prefix arg
  15391. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15392. If the time range is actually in a table, the result is inserted into the
  15393. next column.
  15394. For time difference computation, a year is assumed to be exactly 365
  15395. days in order to avoid rounding problems."
  15396. (interactive "P")
  15397. (or
  15398. (org-clock-update-time-maybe)
  15399. (save-excursion
  15400. (unless (org-at-date-range-p t)
  15401. (goto-char (point-at-bol))
  15402. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15403. (if (not (org-at-date-range-p t))
  15404. (error "Not at a time-stamp range, and none found in current line")))
  15405. (let* ((ts1 (match-string 1))
  15406. (ts2 (match-string 2))
  15407. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15408. (match-end (match-end 0))
  15409. (time1 (org-time-string-to-time ts1))
  15410. (time2 (org-time-string-to-time ts2))
  15411. (t1 (org-float-time time1))
  15412. (t2 (org-float-time time2))
  15413. (diff (abs (- t2 t1)))
  15414. (negative (< (- t2 t1) 0))
  15415. ;; (ys (floor (* 365 24 60 60)))
  15416. (ds (* 24 60 60))
  15417. (hs (* 60 60))
  15418. (fy "%dy %dd %02d:%02d")
  15419. (fy1 "%dy %dd")
  15420. (fd "%dd %02d:%02d")
  15421. (fd1 "%dd")
  15422. (fh "%02d:%02d")
  15423. y d h m align)
  15424. (if havetime
  15425. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15426. y 0
  15427. d (floor (/ diff ds)) diff (mod diff ds)
  15428. h (floor (/ diff hs)) diff (mod diff hs)
  15429. m (floor (/ diff 60)))
  15430. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15431. y 0
  15432. d (floor (+ (/ diff ds) 0.5))
  15433. h 0 m 0))
  15434. (if (not to-buffer)
  15435. (message "%s" (org-make-tdiff-string y d h m))
  15436. (if (org-at-table-p)
  15437. (progn
  15438. (goto-char match-end)
  15439. (setq align t)
  15440. (and (looking-at " *|") (goto-char (match-end 0))))
  15441. (goto-char match-end))
  15442. (if (looking-at
  15443. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15444. (replace-match ""))
  15445. (if negative (insert " -"))
  15446. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15447. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15448. (insert " " (format fh h m))))
  15449. (if align (org-table-align))
  15450. (message "Time difference inserted")))))
  15451. (defun org-make-tdiff-string (y d h m)
  15452. (let ((fmt "")
  15453. (l nil))
  15454. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15455. l (push y l)))
  15456. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15457. l (push d l)))
  15458. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15459. l (push h l)))
  15460. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15461. l (push m l)))
  15462. (apply 'format fmt (nreverse l))))
  15463. (defun org-time-string-to-time (s &optional buffer pos)
  15464. "Convert a timestamp string into internal time."
  15465. (condition-case errdata
  15466. (apply 'encode-time (org-parse-time-string s))
  15467. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15468. s (if (not (and buffer pos))
  15469. ""
  15470. (format " at %d in buffer `%s'" pos buffer))
  15471. (cdr errdata)))))
  15472. (defun org-time-string-to-seconds (s)
  15473. "Convert a timestamp string to a number of seconds."
  15474. (org-float-time (org-time-string-to-time s)))
  15475. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15476. "Convert a time stamp to an absolute day number.
  15477. If there is a specifier for a cyclic time stamp, get the closest
  15478. date to DAYNR.
  15479. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15480. The variable `date' is bound by the calendar when this is called."
  15481. (cond
  15482. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15483. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15484. daynr
  15485. (+ daynr 1000)))
  15486. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15487. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15488. (time-to-days (current-time))) (match-string 0 s)
  15489. prefer show-all))
  15490. (t (time-to-days
  15491. (condition-case errdata
  15492. (apply 'encode-time (org-parse-time-string s))
  15493. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15494. s (if (not (and buffer pos))
  15495. ""
  15496. (format " at %d in buffer `%s'" pos buffer))
  15497. (cdr errdata))))))))
  15498. (defun org-days-to-iso-week (days)
  15499. "Return the iso week number."
  15500. (require 'cal-iso)
  15501. (car (calendar-iso-from-absolute days)))
  15502. (defun org-small-year-to-year (year)
  15503. "Convert 2-digit years into 4-digit years.
  15504. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15505. The year 2000 cannot be abbreviated. Any year larger than 99
  15506. is returned unchanged."
  15507. (if (< year 38)
  15508. (setq year (+ 2000 year))
  15509. (if (< year 100)
  15510. (setq year (+ 1900 year))))
  15511. year)
  15512. (defun org-time-from-absolute (d)
  15513. "Return the time corresponding to date D.
  15514. D may be an absolute day number, or a calendar-type list (month day year)."
  15515. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15516. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15517. (defun org-calendar-holiday ()
  15518. "List of holidays, for Diary display in Org-mode."
  15519. (require 'holidays)
  15520. (let ((hl (funcall
  15521. (if (fboundp 'calendar-check-holidays)
  15522. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15523. (if hl (mapconcat 'identity hl "; "))))
  15524. (defun org-diary-sexp-entry (sexp entry date)
  15525. "Process a SEXP diary ENTRY for DATE."
  15526. (require 'diary-lib)
  15527. (let ((result (if calendar-debug-sexp
  15528. (let ((stack-trace-on-error t))
  15529. (eval (car (read-from-string sexp))))
  15530. (condition-case nil
  15531. (eval (car (read-from-string sexp)))
  15532. (error
  15533. (beep)
  15534. (message "Bad sexp at line %d in %s: %s"
  15535. (org-current-line)
  15536. (buffer-file-name) sexp)
  15537. (sleep-for 2))))))
  15538. (cond ((stringp result) (split-string result "; "))
  15539. ((and (consp result)
  15540. (not (consp (cdr result)))
  15541. (stringp (cdr result))) (cdr result))
  15542. ((and (consp result)
  15543. (stringp (car result))) result)
  15544. (result entry))))
  15545. (defun org-diary-to-ical-string (frombuf)
  15546. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15547. This uses the icalendar.el library."
  15548. (let* ((tmpdir (if (featurep 'xemacs)
  15549. (temp-directory)
  15550. temporary-file-directory))
  15551. (tmpfile (make-temp-name
  15552. (expand-file-name "orgics" tmpdir)))
  15553. buf rtn b e)
  15554. (with-current-buffer frombuf
  15555. (icalendar-export-region (point-min) (point-max) tmpfile)
  15556. (setq buf (find-buffer-visiting tmpfile))
  15557. (set-buffer buf)
  15558. (goto-char (point-min))
  15559. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15560. (setq b (match-beginning 0)))
  15561. (goto-char (point-max))
  15562. (if (re-search-backward "^END:VEVENT" nil t)
  15563. (setq e (match-end 0)))
  15564. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15565. (kill-buffer buf)
  15566. (delete-file tmpfile)
  15567. rtn))
  15568. (defun org-closest-date (start current change prefer show-all)
  15569. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15570. When PREFER is `past', return a date that is either CURRENT or past.
  15571. When PREFER is `future', return a date that is either CURRENT or future.
  15572. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15573. ;; Make the proper lists from the dates
  15574. (catch 'exit
  15575. (let ((a1 '(("h" . hour)
  15576. ("d" . day)
  15577. ("w" . week)
  15578. ("m" . month)
  15579. ("y" . year)))
  15580. (shour (nth 2 (org-parse-time-string start)))
  15581. dn dw sday cday n1 n2 n0
  15582. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15583. (setq start (org-date-to-gregorian start)
  15584. current (org-date-to-gregorian
  15585. (if show-all
  15586. current
  15587. (time-to-days (current-time))))
  15588. sday (calendar-absolute-from-gregorian start)
  15589. cday (calendar-absolute-from-gregorian current))
  15590. (if (<= cday sday) (throw 'exit sday))
  15591. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15592. (setq dn (string-to-number (match-string 1 change))
  15593. dw (cdr (assoc (match-string 2 change) a1)))
  15594. (error "Invalid change specifier: %s" change))
  15595. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15596. (cond
  15597. ((eq dw 'hour)
  15598. (let ((missing-hours
  15599. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15600. dn)))
  15601. (setq n1 (if (zerop missing-hours) cday
  15602. (- cday (1+ (floor (/ missing-hours 24)))))
  15603. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15604. ((eq dw 'day)
  15605. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15606. n2 (+ n1 dn)))
  15607. ((eq dw 'year)
  15608. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15609. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15610. (setq date1 (list m d y1)
  15611. n1 (calendar-absolute-from-gregorian date1)
  15612. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15613. n2 (calendar-absolute-from-gregorian date2)))
  15614. ((eq dw 'month)
  15615. ;; approx number of month between the two dates
  15616. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15617. ;; How often does dn fit in there?
  15618. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15619. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15620. m (+ m nm)
  15621. ny (floor (/ m 12))
  15622. y (+ y ny)
  15623. m (- m (* ny 12)))
  15624. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15625. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15626. (setq m2 (+ m dn) y2 y)
  15627. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15628. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15629. (while (<= n2 cday)
  15630. (setq n1 n2 m m2 y y2)
  15631. (setq m2 (+ m dn) y2 y)
  15632. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15633. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15634. ;; Make sure n1 is the earlier date
  15635. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15636. (if show-all
  15637. (cond
  15638. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15639. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15640. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15641. (cond
  15642. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15643. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15644. (t (if (= cday n1) n1 n2)))))))
  15645. (defun org-date-to-gregorian (date)
  15646. "Turn any specification of DATE into a Gregorian date for the calendar."
  15647. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15648. ((and (listp date) (= (length date) 3)) date)
  15649. ((stringp date)
  15650. (setq date (org-parse-time-string date))
  15651. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15652. ((listp date)
  15653. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15654. (defun org-parse-time-string (s &optional nodefault)
  15655. "Parse the standard Org-mode time string.
  15656. This should be a lot faster than the normal `parse-time-string'.
  15657. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15658. hour and minute fields will be nil if not given."
  15659. (cond ((string-match org-ts-regexp0 s)
  15660. (list 0
  15661. (if (or (match-beginning 8) (not nodefault))
  15662. (string-to-number (or (match-string 8 s) "0")))
  15663. (if (or (match-beginning 7) (not nodefault))
  15664. (string-to-number (or (match-string 7 s) "0")))
  15665. (string-to-number (match-string 4 s))
  15666. (string-to-number (match-string 3 s))
  15667. (string-to-number (match-string 2 s))
  15668. nil nil nil))
  15669. ((string-match "^<[^>]+>$" s)
  15670. (decode-time (seconds-to-time (org-matcher-time s))))
  15671. (t (error "Not a standard Org-mode time string: %s" s))))
  15672. (defun org-timestamp-up (&optional arg)
  15673. "Increase the date item at the cursor by one.
  15674. If the cursor is on the year, change the year. If it is on the month,
  15675. the day or the time, change that.
  15676. With prefix ARG, change by that many units."
  15677. (interactive "p")
  15678. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15679. (defun org-timestamp-down (&optional arg)
  15680. "Decrease the date item at the cursor by one.
  15681. If the cursor is on the year, change the year. If it is on the month,
  15682. the day or the time, change that.
  15683. With prefix ARG, change by that many units."
  15684. (interactive "p")
  15685. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15686. (defun org-timestamp-up-day (&optional arg)
  15687. "Increase the date in the time stamp by one day.
  15688. With prefix ARG, change that many days."
  15689. (interactive "p")
  15690. (if (and (not (org-at-timestamp-p t))
  15691. (org-at-heading-p))
  15692. (org-todo 'up)
  15693. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15694. (defun org-timestamp-down-day (&optional arg)
  15695. "Decrease the date in the time stamp by one day.
  15696. With prefix ARG, change that many days."
  15697. (interactive "p")
  15698. (if (and (not (org-at-timestamp-p t))
  15699. (org-at-heading-p))
  15700. (org-todo 'down)
  15701. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15702. (defun org-at-timestamp-p (&optional inactive-ok)
  15703. "Determine if the cursor is in or at a timestamp."
  15704. (interactive)
  15705. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15706. (pos (point))
  15707. (ans (or (looking-at tsr)
  15708. (save-excursion
  15709. (skip-chars-backward "^[<\n\r\t")
  15710. (if (> (point) (point-min)) (backward-char 1))
  15711. (and (looking-at tsr)
  15712. (> (- (match-end 0) pos) -1))))))
  15713. (and ans
  15714. (boundp 'org-ts-what)
  15715. (setq org-ts-what
  15716. (cond
  15717. ((= pos (match-beginning 0)) 'bracket)
  15718. ;; Point is considered to be "on the bracket" whether
  15719. ;; it's really on it or right after it.
  15720. ((= pos (1- (match-end 0))) 'bracket)
  15721. ((= pos (match-end 0)) 'after)
  15722. ((org-pos-in-match-range pos 2) 'year)
  15723. ((org-pos-in-match-range pos 3) 'month)
  15724. ((org-pos-in-match-range pos 7) 'hour)
  15725. ((org-pos-in-match-range pos 8) 'minute)
  15726. ((or (org-pos-in-match-range pos 4)
  15727. (org-pos-in-match-range pos 5)) 'day)
  15728. ((and (> pos (or (match-end 8) (match-end 5)))
  15729. (< pos (match-end 0)))
  15730. (- pos (or (match-end 8) (match-end 5))))
  15731. (t 'day))))
  15732. ans))
  15733. (defun org-toggle-timestamp-type ()
  15734. "Toggle the type (<active> or [inactive]) of a time stamp."
  15735. (interactive)
  15736. (when (org-at-timestamp-p t)
  15737. (let ((beg (match-beginning 0)) (end (match-end 0))
  15738. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15739. (save-excursion
  15740. (goto-char beg)
  15741. (while (re-search-forward "[][<>]" end t)
  15742. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15743. t t)))
  15744. (message "Timestamp is now %sactive"
  15745. (if (equal (char-after beg) ?<) "" "in")))))
  15746. (defun org-at-clock-log-p nil
  15747. "Is the cursor on the clock log line?"
  15748. (save-excursion
  15749. (move-beginning-of-line 1)
  15750. (looking-at "^[ \t]*CLOCK:")))
  15751. (defvar org-clock-history) ; defined in org-clock.el
  15752. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15753. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15754. "Change the date in the time stamp at point.
  15755. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15756. `year', `minute', `second'. If WHAT is not given, the cursor position
  15757. in the timestamp determines what will be changed.
  15758. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15759. (let ((origin (point)) origin-cat
  15760. with-hm inactive
  15761. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15762. org-ts-what
  15763. extra rem
  15764. ts time time0 fixnext clrgx)
  15765. (if (not (org-at-timestamp-p t))
  15766. (error "Not at a timestamp"))
  15767. (if (and (not what) (eq org-ts-what 'bracket))
  15768. (org-toggle-timestamp-type)
  15769. ;; Point isn't on brackets. Remember the part of the time-stamp
  15770. ;; the point was in. Indeed, size of time-stamps may change,
  15771. ;; but point must be kept in the same category nonetheless.
  15772. (setq origin-cat org-ts-what)
  15773. (if (and (not what) (not (eq org-ts-what 'day))
  15774. org-display-custom-times
  15775. (get-text-property (point) 'display)
  15776. (not (get-text-property (1- (point)) 'display)))
  15777. (setq org-ts-what 'day))
  15778. (setq org-ts-what (or what org-ts-what)
  15779. inactive (= (char-after (match-beginning 0)) ?\[)
  15780. ts (match-string 0))
  15781. (replace-match "")
  15782. (when (string-match
  15783. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15784. ts)
  15785. (setq extra (match-string 1 ts))
  15786. (if suppress-tmp-delay
  15787. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15788. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15789. (setq with-hm t))
  15790. (setq time0 (org-parse-time-string ts))
  15791. (when (and updown
  15792. (eq org-ts-what 'minute)
  15793. (not current-prefix-arg))
  15794. ;; This looks like s-up and s-down. Change by one rounding step.
  15795. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15796. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15797. (setcar (cdr time0) (+ (nth 1 time0)
  15798. (if (> n 0) (- rem) (- dm rem))))))
  15799. (setq time
  15800. (encode-time (or (car time0) 0)
  15801. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15802. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15803. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15804. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15805. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15806. (nthcdr 6 time0)))
  15807. (when (and (member org-ts-what '(hour minute))
  15808. extra
  15809. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15810. (setq extra (org-modify-ts-extra
  15811. extra
  15812. (if (eq org-ts-what 'hour) 2 5)
  15813. n dm)))
  15814. (when (integerp org-ts-what)
  15815. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15816. (if (eq what 'calendar)
  15817. (let ((cal-date (org-get-date-from-calendar)))
  15818. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15819. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15820. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15821. (setcar time0 (or (car time0) 0))
  15822. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15823. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15824. (setq time (apply 'encode-time time0))))
  15825. ;; Insert the new time-stamp, and ensure point stays in the same
  15826. ;; category as before (i.e. not after the last position in that
  15827. ;; category).
  15828. (let ((pos (point)))
  15829. ;; Stay before inserted string. `save-excursion' is of no use.
  15830. (setq org-last-changed-timestamp
  15831. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15832. (goto-char pos))
  15833. (save-match-data
  15834. (looking-at org-ts-regexp3)
  15835. (goto-char (cond
  15836. ;; `day' category ends before `hour' if any, or at
  15837. ;; the end of the day name.
  15838. ((eq origin-cat 'day)
  15839. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15840. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15841. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15842. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15843. ;; `year' and `month' have both fixed size: point
  15844. ;; couldn't have moved into another part.
  15845. (t origin))))
  15846. ;; Update clock if on a CLOCK line.
  15847. (org-clock-update-time-maybe)
  15848. ;; Maybe adjust the closest clock in `org-clock-history'
  15849. (when org-clock-adjust-closest
  15850. (if (not (and (org-at-clock-log-p)
  15851. (< 1 (length (delq nil (mapcar 'marker-position
  15852. org-clock-history))))))
  15853. (message "No clock to adjust")
  15854. (cond ((save-excursion ; fix previous clock?
  15855. (re-search-backward org-ts-regexp0 nil t)
  15856. (org-looking-back (concat org-clock-string " \\[")))
  15857. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15858. ((save-excursion ; fix next clock?
  15859. (re-search-backward org-ts-regexp0 nil t)
  15860. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15861. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15862. (save-window-excursion
  15863. ;; Find closest clock to point, adjust the previous/next one in history
  15864. (let* ((p (save-excursion (org-back-to-heading t)))
  15865. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15866. (clfixnth
  15867. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15868. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15869. (if (not clfixpos)
  15870. (message "No clock to adjust")
  15871. (save-excursion
  15872. (org-goto-marker-or-bmk clfixpos)
  15873. (org-show-subtree)
  15874. (when (re-search-forward clrgx nil t)
  15875. (goto-char (match-beginning 1))
  15876. (let (org-clock-adjust-closest)
  15877. (org-timestamp-change n org-ts-what updown))
  15878. (message "Clock adjusted in %s for heading: %s"
  15879. (file-name-nondirectory (buffer-file-name))
  15880. (org-get-heading t t)))))))))
  15881. ;; Try to recenter the calendar window, if any.
  15882. (if (and org-calendar-follow-timestamp-change
  15883. (get-buffer-window "*Calendar*" t)
  15884. (memq org-ts-what '(day month year)))
  15885. (org-recenter-calendar (time-to-days time))))))
  15886. (defun org-modify-ts-extra (s pos n dm)
  15887. "Change the different parts of the lead-time and repeat fields in timestamp."
  15888. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15889. ng h m new rem)
  15890. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15891. (cond
  15892. ((or (org-pos-in-match-range pos 2)
  15893. (org-pos-in-match-range pos 3))
  15894. (setq m (string-to-number (match-string 3 s))
  15895. h (string-to-number (match-string 2 s)))
  15896. (if (org-pos-in-match-range pos 2)
  15897. (setq h (+ h n))
  15898. (setq n (* dm (org-no-warnings (signum n))))
  15899. (when (not (= 0 (setq rem (% m dm))))
  15900. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15901. (setq m (+ m n)))
  15902. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15903. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15904. (setq h (min 24 (max 0 h)))
  15905. (setq ng 1 new (format "-%02d:%02d" h m)))
  15906. ((org-pos-in-match-range pos 6)
  15907. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15908. ((org-pos-in-match-range pos 5)
  15909. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15910. ((org-pos-in-match-range pos 9)
  15911. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15912. ((org-pos-in-match-range pos 8)
  15913. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15914. (when ng
  15915. (setq s (concat
  15916. (substring s 0 (match-beginning ng))
  15917. new
  15918. (substring s (match-end ng))))))
  15919. s))
  15920. (defun org-recenter-calendar (date)
  15921. "If the calendar is visible, recenter it to DATE."
  15922. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15923. (when cwin
  15924. (let ((calendar-move-hook nil))
  15925. (with-selected-window cwin
  15926. (calendar-goto-date (if (listp date) date
  15927. (calendar-gregorian-from-absolute date))))))))
  15928. (defun org-goto-calendar (&optional arg)
  15929. "Go to the Emacs calendar at the current date.
  15930. If there is a time stamp in the current line, go to that date.
  15931. A prefix ARG can be used to force the current date."
  15932. (interactive "P")
  15933. (let ((tsr org-ts-regexp) diff
  15934. (calendar-move-hook nil)
  15935. (calendar-view-holidays-initially-flag nil)
  15936. (calendar-view-diary-initially-flag nil))
  15937. (if (or (org-at-timestamp-p)
  15938. (save-excursion
  15939. (beginning-of-line 1)
  15940. (looking-at (concat ".*" tsr))))
  15941. (let ((d1 (time-to-days (current-time)))
  15942. (d2 (time-to-days
  15943. (org-time-string-to-time (match-string 1)))))
  15944. (setq diff (- d2 d1))))
  15945. (calendar)
  15946. (calendar-goto-today)
  15947. (if (and diff (not arg)) (calendar-forward-day diff))))
  15948. (defun org-get-date-from-calendar ()
  15949. "Return a list (month day year) of date at point in calendar."
  15950. (with-current-buffer "*Calendar*"
  15951. (save-match-data
  15952. (calendar-cursor-to-date))))
  15953. (defun org-date-from-calendar ()
  15954. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15955. If there is already a time stamp at the cursor position, update it."
  15956. (interactive)
  15957. (if (org-at-timestamp-p t)
  15958. (org-timestamp-change 0 'calendar)
  15959. (let ((cal-date (org-get-date-from-calendar)))
  15960. (org-insert-time-stamp
  15961. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15962. (defcustom org-effort-durations
  15963. `(("h" . 60)
  15964. ("d" . ,(* 60 8))
  15965. ("w" . ,(* 60 8 5))
  15966. ("m" . ,(* 60 8 5 4))
  15967. ("y" . ,(* 60 8 5 40)))
  15968. "Conversion factor to minutes for an effort modifier.
  15969. Each entry has the form (MODIFIER . MINUTES).
  15970. In an effort string, a number followed by MODIFIER is multiplied
  15971. by the specified number of MINUTES to obtain an effort in
  15972. minutes.
  15973. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15974. effort string \"2hours\" is equivalent to 120 minutes."
  15975. :group 'org-agenda
  15976. :version "24.1"
  15977. :type '(alist :key-type (string :tag "Modifier")
  15978. :value-type (number :tag "Minutes")))
  15979. (defun org-minutes-to-clocksum-string (m)
  15980. "Format number of minutes as a clocksum string.
  15981. The format is determined by `org-time-clocksum-format',
  15982. `org-time-clocksum-use-fractional' and
  15983. `org-time-clocksum-fractional-format' and
  15984. `org-time-clocksum-use-effort-durations'."
  15985. (let ((clocksum "") h d w mo y fmt n)
  15986. (setq h (if org-time-clocksum-use-effort-durations
  15987. (cdr (assoc "h" org-effort-durations)) 60)
  15988. d (if org-time-clocksum-use-effort-durations
  15989. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15990. w (if org-time-clocksum-use-effort-durations
  15991. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15992. mo (if org-time-clocksum-use-effort-durations
  15993. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15994. y (if org-time-clocksum-use-effort-durations
  15995. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15996. ;; fractional format
  15997. (if org-time-clocksum-use-fractional
  15998. (cond
  15999. ;; single format string
  16000. ((stringp org-time-clocksum-fractional-format)
  16001. (format org-time-clocksum-fractional-format (/ m (float h))))
  16002. ;; choice of fractional formats for different time units
  16003. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16004. (> (/ (truncate m) (* y d h)) 0))
  16005. (format fmt (/ m (* y d (float h)))))
  16006. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16007. (> (/ (truncate m) (* mo d h)) 0))
  16008. (format fmt (/ m (* mo d (float h)))))
  16009. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16010. (> (/ (truncate m) (* w d h)) 0))
  16011. (format fmt (/ m (* w d (float h)))))
  16012. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16013. (> (/ (truncate m) (* d h)) 0))
  16014. (format fmt (/ m (* d (float h)))))
  16015. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16016. (> (/ (truncate m) h) 0))
  16017. (format fmt (/ m (float h))))
  16018. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16019. (format fmt m))
  16020. ;; fall back to smallest time unit with a format
  16021. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16022. (format fmt (/ m (float h))))
  16023. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16024. (format fmt (/ m (* d (float h)))))
  16025. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16026. (format fmt (/ m (* w d (float h)))))
  16027. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16028. (format fmt (/ m (* mo d (float h)))))
  16029. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16030. (format fmt (/ m (* y d (float h))))))
  16031. ;; standard (non-fractional) format, with single format string
  16032. (if (stringp org-time-clocksum-format)
  16033. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16034. ;; separate formats components
  16035. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16036. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16037. (plist-get org-time-clocksum-format :require-years))
  16038. (setq clocksum (concat clocksum (format fmt n))
  16039. m (- m (* n y d h))))
  16040. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16041. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16042. (plist-get org-time-clocksum-format :require-months))
  16043. (setq clocksum (concat clocksum (format fmt n))
  16044. m (- m (* n mo d h))))
  16045. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16046. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16047. (plist-get org-time-clocksum-format :require-weeks))
  16048. (setq clocksum (concat clocksum (format fmt n))
  16049. m (- m (* n w d h))))
  16050. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16051. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16052. (plist-get org-time-clocksum-format :require-days))
  16053. (setq clocksum (concat clocksum (format fmt n))
  16054. m (- m (* n d h))))
  16055. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16056. (or (> (setq n (/ (truncate m) h)) 0)
  16057. (plist-get org-time-clocksum-format :require-hours))
  16058. (setq clocksum (concat clocksum (format fmt n))
  16059. m (- m (* n h))))
  16060. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16061. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16062. (setq clocksum (concat clocksum (format fmt m))))
  16063. ;; return formatted time duration
  16064. clocksum))))
  16065. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16066. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16067. "Org mode version 8.0")
  16068. (defun org-hours-to-clocksum-string (n)
  16069. (org-minutes-to-clocksum-string (* n 60)))
  16070. (defun org-hh:mm-string-to-minutes (s)
  16071. "Convert a string H:MM to a number of minutes.
  16072. If the string is just a number, interpret it as minutes.
  16073. In fact, the first hh:mm or number in the string will be taken,
  16074. there can be extra stuff in the string.
  16075. If no number is found, the return value is 0."
  16076. (cond
  16077. ((integerp s) s)
  16078. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16079. (+ (* (string-to-number (match-string 1 s)) 60)
  16080. (string-to-number (match-string 2 s))))
  16081. ((string-match "\\([0-9]+\\)" s)
  16082. (string-to-number (match-string 1 s)))
  16083. (t 0)))
  16084. (defcustom org-image-actual-width t
  16085. "Should we use the actual width of images when inlining them?
  16086. When set to `t', always use the image width.
  16087. When set to a number, use imagemagick (when available) to set
  16088. the image's width to this value.
  16089. When set to a number in a list, try to get the width from any
  16090. #+ATTR.* keyword if it matches a width specification like
  16091. #+ATTR_HTML: :width 300px
  16092. and fall back on that number if none is found.
  16093. When set to nil, try to get the width from an #+ATTR.* keyword
  16094. and fall back on the original width if none is found.
  16095. This requires Emacs >= 24.1, build with imagemagick support."
  16096. :group 'org-appearance
  16097. :version "24.4"
  16098. :package-version '(Org . "8.0")
  16099. :type '(choice
  16100. (const :tag "Use the image width" t)
  16101. (integer :tag "Use a number of pixels")
  16102. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16103. (const :tag "Use #+ATTR* or don't resize" nil)))
  16104. (defcustom org-agenda-inhibit-startup nil
  16105. "Inhibit startup when preparing agenda buffers.
  16106. When this variable is `t' (the default), the initialization of
  16107. the Org agenda buffers is inhibited: e.g. the visibility state
  16108. is not set, the tables are not re-aligned, etc."
  16109. :type 'boolean
  16110. :version "24.3"
  16111. :group 'org-agenda)
  16112. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16113. "Convert a duration string S to minutes.
  16114. A bare number is interpreted as minutes, modifiers can be set by
  16115. customizing `org-effort-durations' (which see).
  16116. Entries containing a colon are interpreted as H:MM by
  16117. `org-hh:mm-string-to-minutes'."
  16118. (let ((result 0)
  16119. (re (concat "\\([0-9.]+\\) *\\("
  16120. (regexp-opt (mapcar 'car org-effort-durations))
  16121. "\\)")))
  16122. (while (string-match re s)
  16123. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16124. (string-to-number (match-string 1 s))))
  16125. (setq s (replace-match "" nil t s)))
  16126. (setq result (floor result))
  16127. (incf result (org-hh:mm-string-to-minutes s))
  16128. (if output-to-string (number-to-string result) result)))
  16129. ;;;; Files
  16130. (defun org-save-all-org-buffers ()
  16131. "Save all Org-mode buffers without user confirmation."
  16132. (interactive)
  16133. (message "Saving all Org-mode buffers...")
  16134. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16135. (when (featurep 'org-id) (org-id-locations-save))
  16136. (message "Saving all Org-mode buffers... done"))
  16137. (defun org-revert-all-org-buffers ()
  16138. "Revert all Org-mode buffers.
  16139. Prompt for confirmation when there are unsaved changes.
  16140. Be sure you know what you are doing before letting this function
  16141. overwrite your changes.
  16142. This function is useful in a setup where one tracks org files
  16143. with a version control system, to revert on one machine after pulling
  16144. changes from another. I believe the procedure must be like this:
  16145. 1. M-x org-save-all-org-buffers
  16146. 2. Pull changes from the other machine, resolve conflicts
  16147. 3. M-x org-revert-all-org-buffers"
  16148. (interactive)
  16149. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16150. (error "Abort"))
  16151. (save-excursion
  16152. (save-window-excursion
  16153. (mapc
  16154. (lambda (b)
  16155. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16156. (with-current-buffer b buffer-file-name))
  16157. (org-pop-to-buffer-same-window b)
  16158. (revert-buffer t 'no-confirm)))
  16159. (buffer-list))
  16160. (when (and (featurep 'org-id) org-id-track-globally)
  16161. (org-id-locations-load)))))
  16162. ;;;; Agenda files
  16163. ;;;###autoload
  16164. (defun org-switchb (&optional arg)
  16165. "Switch between Org buffers.
  16166. With one prefix argument, restrict available buffers to files.
  16167. With two prefix arguments, restrict available buffers to agenda files.
  16168. Defaults to `iswitchb' for buffer name completion.
  16169. Set `org-completion-use-ido' to make it use ido instead."
  16170. (interactive "P")
  16171. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16172. ((equal arg '(16)) (org-buffer-list 'agenda))
  16173. (t (org-buffer-list))))
  16174. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16175. (org-completion-use-ido org-completion-use-ido))
  16176. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16177. (setq org-completion-use-iswitchb t))
  16178. (org-pop-to-buffer-same-window
  16179. (org-icompleting-read "Org buffer: "
  16180. (mapcar 'list (mapcar 'buffer-name blist))
  16181. nil t))))
  16182. ;;; Define some older names previously used for this functionality
  16183. ;;;###autoload
  16184. (defalias 'org-ido-switchb 'org-switchb)
  16185. ;;;###autoload
  16186. (defalias 'org-iswitchb 'org-switchb)
  16187. (defun org-buffer-list (&optional predicate exclude-tmp)
  16188. "Return a list of Org buffers.
  16189. PREDICATE can be `export', `files' or `agenda'.
  16190. export restrict the list to Export buffers.
  16191. files restrict the list to buffers visiting Org files.
  16192. agenda restrict the list to buffers visiting agenda files.
  16193. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16194. (let* ((bfn nil)
  16195. (agenda-files (and (eq predicate 'agenda)
  16196. (mapcar 'file-truename (org-agenda-files t))))
  16197. (filter
  16198. (cond
  16199. ((eq predicate 'files)
  16200. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16201. ((eq predicate 'export)
  16202. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16203. ((eq predicate 'agenda)
  16204. (lambda (b)
  16205. (with-current-buffer b
  16206. (and (derived-mode-p 'org-mode)
  16207. (setq bfn (buffer-file-name b))
  16208. (member (file-truename bfn) agenda-files)))))
  16209. (t (lambda (b) (with-current-buffer b
  16210. (or (derived-mode-p 'org-mode)
  16211. (string-match "\*Org .*Export"
  16212. (buffer-name b)))))))))
  16213. (delq nil
  16214. (mapcar
  16215. (lambda(b)
  16216. (if (and (funcall filter b)
  16217. (or (not exclude-tmp)
  16218. (not (string-match "tmp" (buffer-name b)))))
  16219. b
  16220. nil))
  16221. (buffer-list)))))
  16222. (defun org-agenda-files (&optional unrestricted archives)
  16223. "Get the list of agenda files.
  16224. Optional UNRESTRICTED means return the full list even if a restriction
  16225. is currently in place.
  16226. When ARCHIVES is t, include all archive files that are really being
  16227. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16228. `org-agenda-archives-mode' is t."
  16229. (let ((files
  16230. (cond
  16231. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16232. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16233. ((listp org-agenda-files) org-agenda-files)
  16234. (t (error "Invalid value of `org-agenda-files'")))))
  16235. (setq files (apply 'append
  16236. (mapcar (lambda (f)
  16237. (if (file-directory-p f)
  16238. (directory-files
  16239. f t org-agenda-file-regexp)
  16240. (list f)))
  16241. files)))
  16242. (when org-agenda-skip-unavailable-files
  16243. (setq files (delq nil
  16244. (mapcar (function
  16245. (lambda (file)
  16246. (and (file-readable-p file) file)))
  16247. files))))
  16248. (when (or (eq archives t)
  16249. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16250. (setq files (org-add-archive-files files)))
  16251. files))
  16252. (defun org-agenda-file-p (&optional file)
  16253. "Return non-nil, if FILE is an agenda file.
  16254. If FILE is omitted, use the file associated with the current
  16255. buffer."
  16256. (member (or file (buffer-file-name))
  16257. (org-agenda-files t)))
  16258. (defun org-edit-agenda-file-list ()
  16259. "Edit the list of agenda files.
  16260. Depending on setup, this either uses customize to edit the variable
  16261. `org-agenda-files', or it visits the file that is holding the list. In the
  16262. latter case, the buffer is set up in a way that saving it automatically kills
  16263. the buffer and restores the previous window configuration."
  16264. (interactive)
  16265. (if (stringp org-agenda-files)
  16266. (let ((cw (current-window-configuration)))
  16267. (find-file org-agenda-files)
  16268. (org-set-local 'org-window-configuration cw)
  16269. (org-add-hook 'after-save-hook
  16270. (lambda ()
  16271. (set-window-configuration
  16272. (prog1 org-window-configuration
  16273. (kill-buffer (current-buffer))))
  16274. (org-install-agenda-files-menu)
  16275. (message "New agenda file list installed"))
  16276. nil 'local)
  16277. (message "%s" (substitute-command-keys
  16278. "Edit list and finish with \\[save-buffer]")))
  16279. (customize-variable 'org-agenda-files)))
  16280. (defun org-store-new-agenda-file-list (list)
  16281. "Set new value for the agenda file list and save it correctly."
  16282. (if (stringp org-agenda-files)
  16283. (let ((fe (org-read-agenda-file-list t)) b u)
  16284. (while (setq b (find-buffer-visiting org-agenda-files))
  16285. (kill-buffer b))
  16286. (with-temp-file org-agenda-files
  16287. (insert
  16288. (mapconcat
  16289. (lambda (f) ;; Keep un-expanded entries.
  16290. (if (setq u (assoc f fe))
  16291. (cdr u)
  16292. f))
  16293. list "\n")
  16294. "\n")))
  16295. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16296. (org-insert-mode-line-in-empty-file nil))
  16297. (setq org-agenda-files list)
  16298. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16299. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16300. "Read the list of agenda files from a file.
  16301. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16302. filenames, used by `org-store-new-agenda-file-list' to write back
  16303. un-expanded file names."
  16304. (when (file-directory-p org-agenda-files)
  16305. (error "`org-agenda-files' cannot be a single directory"))
  16306. (when (stringp org-agenda-files)
  16307. (with-temp-buffer
  16308. (insert-file-contents org-agenda-files)
  16309. (mapcar
  16310. (lambda (f)
  16311. (let ((e (expand-file-name (substitute-in-file-name f)
  16312. org-directory)))
  16313. (if pair-with-expansion
  16314. (cons e f)
  16315. e)))
  16316. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16317. ;;;###autoload
  16318. (defun org-cycle-agenda-files ()
  16319. "Cycle through the files in `org-agenda-files'.
  16320. If the current buffer visits an agenda file, find the next one in the list.
  16321. If the current buffer does not, find the first agenda file."
  16322. (interactive)
  16323. (let* ((fs (org-agenda-files t))
  16324. (files (append fs (list (car fs))))
  16325. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16326. file)
  16327. (unless files (error "No agenda files"))
  16328. (catch 'exit
  16329. (while (setq file (pop files))
  16330. (if (equal (file-truename file) tcf)
  16331. (when (car files)
  16332. (find-file (car files))
  16333. (throw 'exit t))))
  16334. (find-file (car fs)))
  16335. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16336. (defun org-agenda-file-to-front (&optional to-end)
  16337. "Move/add the current file to the top of the agenda file list.
  16338. If the file is not present in the list, it is added to the front. If it is
  16339. present, it is moved there. With optional argument TO-END, add/move to the
  16340. end of the list."
  16341. (interactive "P")
  16342. (let ((org-agenda-skip-unavailable-files nil)
  16343. (file-alist (mapcar (lambda (x)
  16344. (cons (file-truename x) x))
  16345. (org-agenda-files t)))
  16346. (ctf (file-truename
  16347. (or buffer-file-name
  16348. (error "Please save the current buffer to a file"))))
  16349. x had)
  16350. (setq x (assoc ctf file-alist) had x)
  16351. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16352. (if to-end
  16353. (setq file-alist (append (delq x file-alist) (list x)))
  16354. (setq file-alist (cons x (delq x file-alist))))
  16355. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16356. (org-install-agenda-files-menu)
  16357. (message "File %s to %s of agenda file list"
  16358. (if had "moved" "added") (if to-end "end" "front"))))
  16359. (defun org-remove-file (&optional file)
  16360. "Remove current file from the list of files in variable `org-agenda-files'.
  16361. These are the files which are being checked for agenda entries.
  16362. Optional argument FILE means use this file instead of the current."
  16363. (interactive)
  16364. (let* ((org-agenda-skip-unavailable-files nil)
  16365. (file (or file buffer-file-name
  16366. (error "Current buffer does not visit a file")))
  16367. (true-file (file-truename file))
  16368. (afile (abbreviate-file-name file))
  16369. (files (delq nil (mapcar
  16370. (lambda (x)
  16371. (if (equal true-file
  16372. (file-truename x))
  16373. nil x))
  16374. (org-agenda-files t)))))
  16375. (if (not (= (length files) (length (org-agenda-files t))))
  16376. (progn
  16377. (org-store-new-agenda-file-list files)
  16378. (org-install-agenda-files-menu)
  16379. (message "Removed file: %s" afile))
  16380. (message "File was not in list: %s (not removed)" afile))))
  16381. (defun org-file-menu-entry (file)
  16382. (vector file (list 'find-file file) t))
  16383. (defun org-check-agenda-file (file)
  16384. "Make sure FILE exists. If not, ask user what to do."
  16385. (when (not (file-exists-p file))
  16386. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16387. (abbreviate-file-name file))
  16388. (let ((r (downcase (read-char-exclusive))))
  16389. (cond
  16390. ((equal r ?r)
  16391. (org-remove-file file)
  16392. (throw 'nextfile t))
  16393. (t (error "Abort"))))))
  16394. (defun org-get-agenda-file-buffer (file)
  16395. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16396. it to the list of buffers which might be released later."
  16397. (let ((buf (org-find-base-buffer-visiting file)))
  16398. (if buf
  16399. buf ; just return it
  16400. ;; Make a new buffer and remember it
  16401. (setq buf (find-file-noselect file))
  16402. (if buf (push buf org-agenda-new-buffers))
  16403. buf)))
  16404. (defun org-release-buffers (blist)
  16405. "Release all buffers in list, asking the user for confirmation when needed.
  16406. When a buffer is unmodified, it is just killed. When modified, it is saved
  16407. \(if the user agrees) and then killed."
  16408. (let (buf file)
  16409. (while (setq buf (pop blist))
  16410. (setq file (buffer-file-name buf))
  16411. (when (and (buffer-modified-p buf)
  16412. file
  16413. (y-or-n-p (format "Save file %s? " file)))
  16414. (with-current-buffer buf (save-buffer)))
  16415. (kill-buffer buf))))
  16416. (defun org-agenda-prepare-buffers (files)
  16417. "Create buffers for all agenda files, protect archived trees and comments."
  16418. (interactive)
  16419. (let ((pa '(:org-archived t))
  16420. (pc '(:org-comment t))
  16421. (pall '(:org-archived t :org-comment t))
  16422. (inhibit-read-only t)
  16423. (org-inhibit-startup org-agenda-inhibit-startup)
  16424. (rea (concat ":" org-archive-tag ":"))
  16425. file re)
  16426. (setq org-tag-alist-for-agenda nil
  16427. org-tag-groups-alist-for-agenda nil)
  16428. (save-excursion
  16429. (save-restriction
  16430. (while (setq file (pop files))
  16431. (catch 'nextfile
  16432. (if (bufferp file)
  16433. (set-buffer file)
  16434. (org-check-agenda-file file)
  16435. (set-buffer (org-get-agenda-file-buffer file)))
  16436. (widen)
  16437. (org-set-regexps-and-options-for-tags)
  16438. (org-refresh-category-properties)
  16439. (org-refresh-properties org-effort-property 'org-effort)
  16440. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16441. (setq org-todo-keywords-for-agenda
  16442. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16443. (setq org-done-keywords-for-agenda
  16444. (append org-done-keywords-for-agenda org-done-keywords))
  16445. (setq org-todo-keyword-alist-for-agenda
  16446. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16447. (setq org-drawers-for-agenda
  16448. (append org-drawers-for-agenda org-drawers))
  16449. (unless (equal org-tag-alist-for-agenda org-tag-alist)
  16450. (setq org-tag-alist-for-agenda
  16451. (append org-tag-alist-for-agenda org-tag-alist)))
  16452. (if org-group-tags
  16453. (setq org-tag-groups-alist-for-agenda
  16454. (org-uniquify-alist
  16455. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16456. (org-with-silent-modifications
  16457. (save-excursion
  16458. (remove-text-properties (point-min) (point-max) pall)
  16459. (when org-agenda-skip-archived-trees
  16460. (goto-char (point-min))
  16461. (while (re-search-forward rea nil t)
  16462. (if (org-at-heading-p t)
  16463. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16464. (goto-char (point-min))
  16465. (setq re (format org-heading-keyword-regexp-format
  16466. org-comment-string))
  16467. (while (re-search-forward re nil t)
  16468. (add-text-properties
  16469. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16470. (setq org-todo-keywords-for-agenda
  16471. (org-uniquify org-todo-keywords-for-agenda))
  16472. (setq org-todo-keyword-alist-for-agenda
  16473. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16474. ;;;; CDLaTeX minor mode
  16475. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16476. "Keymap for the minor `org-cdlatex-mode'.")
  16477. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16478. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16479. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16480. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16481. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16482. (defvar org-cdlatex-texmathp-advice-is-done nil
  16483. "Flag remembering if we have applied the advice to texmathp already.")
  16484. (define-minor-mode org-cdlatex-mode
  16485. "Toggle the minor `org-cdlatex-mode'.
  16486. This mode supports entering LaTeX environment and math in LaTeX fragments
  16487. in Org-mode.
  16488. \\{org-cdlatex-mode-map}"
  16489. nil " OCDL" nil
  16490. (when org-cdlatex-mode
  16491. (require 'cdlatex)
  16492. (run-hooks 'cdlatex-mode-hook)
  16493. (cdlatex-compute-tables))
  16494. (unless org-cdlatex-texmathp-advice-is-done
  16495. (setq org-cdlatex-texmathp-advice-is-done t)
  16496. (defadvice texmathp (around org-math-always-on activate)
  16497. "Always return t in org-mode buffers.
  16498. This is because we want to insert math symbols without dollars even outside
  16499. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16500. an embedded LaTeX fragment, let texmathp do its job.
  16501. \\[org-cdlatex-mode-map]"
  16502. (interactive)
  16503. (let (p)
  16504. (cond
  16505. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16506. ((eq this-command 'cdlatex-math-symbol)
  16507. (setq ad-return-value t
  16508. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16509. (t
  16510. (let ((p (org-inside-LaTeX-fragment-p)))
  16511. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16512. (setq ad-return-value t
  16513. texmathp-why '("Org-mode embedded math" . 0))
  16514. (if p ad-do-it)))))))))
  16515. (defun turn-on-org-cdlatex ()
  16516. "Unconditionally turn on `org-cdlatex-mode'."
  16517. (org-cdlatex-mode 1))
  16518. (defun org-try-cdlatex-tab ()
  16519. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16520. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16521. - inside a LaTeX fragment, or
  16522. - after the first word in a line, where an abbreviation expansion could
  16523. insert a LaTeX environment."
  16524. (when org-cdlatex-mode
  16525. (cond
  16526. ;; Before any word on the line: No expansion possible.
  16527. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16528. ;; Just after first word on the line: Expand it. Make sure it
  16529. ;; cannot happen on headlines, though.
  16530. ((save-excursion
  16531. (skip-chars-backward "a-zA-Z0-9*")
  16532. (skip-chars-backward " \t")
  16533. (and (bolp) (not (org-at-heading-p))))
  16534. (cdlatex-tab) t)
  16535. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16536. (defun org-cdlatex-underscore-caret (&optional arg)
  16537. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16538. Revert to the normal definition outside of these fragments."
  16539. (interactive "P")
  16540. (if (org-inside-LaTeX-fragment-p)
  16541. (call-interactively 'cdlatex-sub-superscript)
  16542. (let (org-cdlatex-mode)
  16543. (call-interactively (key-binding (vector last-input-event))))))
  16544. (defun org-cdlatex-math-modify (&optional arg)
  16545. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16546. Revert to the normal definition outside of these fragments."
  16547. (interactive "P")
  16548. (if (org-inside-LaTeX-fragment-p)
  16549. (call-interactively 'cdlatex-math-modify)
  16550. (let (org-cdlatex-mode)
  16551. (call-interactively (key-binding (vector last-input-event))))))
  16552. ;;;; LaTeX fragments
  16553. (defvar org-latex-regexps
  16554. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16555. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16556. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16557. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16558. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16559. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16560. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16561. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16562. "Regular expressions for matching embedded LaTeX.")
  16563. (defun org-inside-LaTeX-fragment-p ()
  16564. "Test if point is inside a LaTeX fragment.
  16565. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16566. sequence appearing also before point.
  16567. Even though the matchers for math are configurable, this function assumes
  16568. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16569. delimiters are skipped when they have been removed by customization.
  16570. The return value is nil, or a cons cell with the delimiter and the
  16571. position of this delimiter.
  16572. This function does a reasonably good job, but can locally be fooled by
  16573. for example currency specifications. For example it will assume being in
  16574. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16575. fragments that are properly closed, but during editing, we have to live
  16576. with the uncertainty caused by missing closing delimiters. This function
  16577. looks only before point, not after."
  16578. (catch 'exit
  16579. (let ((pos (point))
  16580. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16581. (lim (progn
  16582. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16583. (point)))
  16584. dd-on str (start 0) m re)
  16585. (goto-char pos)
  16586. (when dodollar
  16587. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16588. re (nth 1 (assoc "$" org-latex-regexps)))
  16589. (while (string-match re str start)
  16590. (cond
  16591. ((= (match-end 0) (length str))
  16592. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16593. ((= (match-end 0) (- (length str) 5))
  16594. (throw 'exit nil))
  16595. (t (setq start (match-end 0))))))
  16596. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16597. (goto-char pos)
  16598. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16599. (and (match-beginning 2) (throw 'exit nil))
  16600. ;; count $$
  16601. (while (re-search-backward "\\$\\$" lim t)
  16602. (setq dd-on (not dd-on)))
  16603. (goto-char pos)
  16604. (if dd-on (cons "$$" m))))))
  16605. (defun org-inside-latex-macro-p ()
  16606. "Is point inside a LaTeX macro or its arguments?"
  16607. (save-match-data
  16608. (org-in-regexp
  16609. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16610. (defvar org-latex-fragment-image-overlays nil
  16611. "List of overlays carrying the images of latex fragments.")
  16612. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16613. (defun org-remove-latex-fragment-image-overlays ()
  16614. "Remove all overlays with LaTeX fragment images in current buffer."
  16615. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16616. (setq org-latex-fragment-image-overlays nil))
  16617. (defun org-preview-latex-fragment (&optional subtree)
  16618. "Preview the LaTeX fragment at point, or all locally or globally.
  16619. If the cursor is in a LaTeX fragment, create the image and overlay
  16620. it over the source code. If there is no fragment at point, display
  16621. all fragments in the current text, from one headline to the next. With
  16622. prefix SUBTREE, display all fragments in the current subtree. With a
  16623. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16624. the cursor is before the first headline,
  16625. display all fragments in the buffer.
  16626. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16627. (interactive "P")
  16628. (unless buffer-file-name
  16629. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16630. (org-remove-latex-fragment-image-overlays)
  16631. (save-excursion
  16632. (save-restriction
  16633. (let (beg end at msg)
  16634. (cond
  16635. ((or (equal subtree '(16))
  16636. (not (save-excursion
  16637. (re-search-backward org-outline-regexp-bol nil t))))
  16638. (setq beg (point-min) end (point-max)
  16639. msg "Creating images for buffer...%s"))
  16640. ((equal subtree '(4))
  16641. (org-back-to-heading)
  16642. (setq beg (point) end (org-end-of-subtree t)
  16643. msg "Creating images for subtree...%s"))
  16644. (t
  16645. (if (setq at (org-inside-LaTeX-fragment-p))
  16646. (goto-char (max (point-min) (- (cdr at) 2)))
  16647. (org-back-to-heading))
  16648. (setq beg (point) end (progn (outline-next-heading) (point))
  16649. msg (if at "Creating image...%s"
  16650. "Creating images for entry...%s"))))
  16651. (message msg "")
  16652. (narrow-to-region beg end)
  16653. (goto-char beg)
  16654. (org-format-latex
  16655. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16656. (file-name-nondirectory
  16657. buffer-file-name)))
  16658. default-directory 'overlays msg at 'forbuffer
  16659. org-latex-create-formula-image-program)
  16660. (message msg "done. Use `C-c C-c' to remove images.")))))
  16661. (defun org-format-latex (prefix &optional dir overlays msg at
  16662. forbuffer processing-type)
  16663. "Replace LaTeX fragments with links to an image, and produce images.
  16664. Some of the options can be changed using the variable
  16665. `org-format-latex-options'."
  16666. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16667. (let* ((prefixnodir (file-name-nondirectory prefix))
  16668. (absprefix (expand-file-name prefix dir))
  16669. (todir (file-name-directory absprefix))
  16670. (opt org-format-latex-options)
  16671. (optnew org-format-latex-options)
  16672. (matchers (plist-get opt :matchers))
  16673. (re-list org-latex-regexps)
  16674. (cnt 0) txt hash link beg end re e checkdir
  16675. string
  16676. m n block-type block linkfile movefile ov)
  16677. ;; Check the different regular expressions
  16678. (while (setq e (pop re-list))
  16679. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16680. block (if block-type "\n\n" ""))
  16681. (when (member m matchers)
  16682. (goto-char (point-min))
  16683. (while (re-search-forward re nil t)
  16684. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16685. (or (not overlays)
  16686. (not (eq (get-char-property (match-beginning n)
  16687. 'org-overlay-type)
  16688. 'org-latex-overlay))))
  16689. (cond
  16690. ((eq processing-type 'verbatim))
  16691. ((eq processing-type 'mathjax)
  16692. ;; Prepare for MathJax processing.
  16693. (setq string (match-string n))
  16694. (when (member m '("$" "$1"))
  16695. (save-excursion
  16696. (delete-region (match-beginning n) (match-end n))
  16697. (goto-char (match-beginning n))
  16698. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16699. ((or (eq processing-type 'dvipng)
  16700. (eq processing-type 'imagemagick))
  16701. ;; Process to an image.
  16702. (setq txt (match-string n)
  16703. beg (match-beginning n) end (match-end n)
  16704. cnt (1+ cnt))
  16705. (let ((face (face-at-point))
  16706. (fg (plist-get opt :foreground))
  16707. (bg (plist-get opt :background))
  16708. ;; Ensure full list is printed.
  16709. print-length print-level)
  16710. (when forbuffer
  16711. ;; Get the colors from the face at point.
  16712. (goto-char beg)
  16713. (when (eq fg 'auto)
  16714. (setq fg (face-attribute face :foreground nil 'default)))
  16715. (when (eq bg 'auto)
  16716. (setq bg (face-attribute face :background nil 'default)))
  16717. (setq optnew (copy-sequence opt))
  16718. (plist-put optnew :foreground fg)
  16719. (plist-put optnew :background bg))
  16720. (setq hash (sha1 (prin1-to-string
  16721. (list org-format-latex-header
  16722. org-latex-default-packages-alist
  16723. org-latex-packages-alist
  16724. org-format-latex-options
  16725. forbuffer txt fg bg)))
  16726. linkfile (format "%s_%s.png" prefix hash)
  16727. movefile (format "%s_%s.png" absprefix hash)))
  16728. (setq link (concat block "[[file:" linkfile "]]" block))
  16729. (if msg (message msg cnt))
  16730. (goto-char beg)
  16731. (unless checkdir ; Ensure the directory exists.
  16732. (setq checkdir t)
  16733. (or (file-directory-p todir) (make-directory todir t)))
  16734. (unless (file-exists-p movefile)
  16735. (org-create-formula-image
  16736. txt movefile optnew forbuffer processing-type))
  16737. (if overlays
  16738. (progn
  16739. (mapc (lambda (o)
  16740. (if (eq (overlay-get o 'org-overlay-type)
  16741. 'org-latex-overlay)
  16742. (delete-overlay o)))
  16743. (overlays-in beg end))
  16744. (setq ov (make-overlay beg end))
  16745. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16746. (if (featurep 'xemacs)
  16747. (progn
  16748. (overlay-put ov 'invisible t)
  16749. (overlay-put
  16750. ov 'end-glyph
  16751. (make-glyph (vector 'png :file movefile))))
  16752. (overlay-put
  16753. ov 'display
  16754. (list 'image :type 'png :file movefile :ascent 'center)))
  16755. (push ov org-latex-fragment-image-overlays)
  16756. (goto-char end))
  16757. (delete-region beg end)
  16758. (insert (org-add-props link
  16759. (list 'org-latex-src
  16760. (replace-regexp-in-string
  16761. "\"" "" txt)
  16762. 'org-latex-src-embed-type
  16763. (if block-type 'paragraph 'character))))))
  16764. ((eq processing-type 'mathml)
  16765. ;; Process to MathML
  16766. (unless (save-match-data (org-format-latex-mathml-available-p))
  16767. (error "LaTeX to MathML converter not configured"))
  16768. (setq txt (match-string n)
  16769. beg (match-beginning n) end (match-end n)
  16770. cnt (1+ cnt))
  16771. (if msg (message msg cnt))
  16772. (goto-char beg)
  16773. (delete-region beg end)
  16774. (insert (org-format-latex-as-mathml
  16775. txt block-type prefix dir)))
  16776. (t
  16777. (error "Unknown conversion type %s for latex fragments"
  16778. processing-type)))))))))
  16779. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16780. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16781. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16782. sucessful, return the portion between \"<math...> </math>\"
  16783. elements otherwise return nil. When MATHML-FILE is specified,
  16784. write the results in to that file. When invoked as an
  16785. interactive command, prompt for LATEX-FRAG, with initial value
  16786. set to the current active region and echo the results for user
  16787. inspection."
  16788. (interactive (list (let ((frag (when (org-region-active-p)
  16789. (buffer-substring-no-properties
  16790. (region-beginning) (region-end)))))
  16791. (read-string "LaTeX Fragment: " frag nil frag))))
  16792. (unless latex-frag (error "Invalid latex-frag"))
  16793. (let* ((tmp-in-file (file-relative-name
  16794. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16795. (ignore (write-region latex-frag nil tmp-in-file))
  16796. (tmp-out-file (file-relative-name
  16797. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16798. (cmd (format-spec
  16799. org-latex-to-mathml-convert-command
  16800. `((?j . ,(shell-quote-argument
  16801. (expand-file-name org-latex-to-mathml-jar-file)))
  16802. (?I . ,(shell-quote-argument tmp-in-file))
  16803. (?o . ,(shell-quote-argument tmp-out-file)))))
  16804. mathml shell-command-output)
  16805. (when (org-called-interactively-p 'any)
  16806. (unless (org-format-latex-mathml-available-p)
  16807. (error "LaTeX to MathML converter not configured")))
  16808. (message "Running %s" cmd)
  16809. (setq shell-command-output (shell-command-to-string cmd))
  16810. (setq mathml
  16811. (when (file-readable-p tmp-out-file)
  16812. (with-current-buffer (find-file-noselect tmp-out-file t)
  16813. (goto-char (point-min))
  16814. (when (re-search-forward
  16815. (concat
  16816. (regexp-quote
  16817. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16818. "\\(.\\|\n\\)*"
  16819. (regexp-quote "</math>")) nil t)
  16820. (prog1 (match-string 0) (kill-buffer))))))
  16821. (cond
  16822. (mathml
  16823. (setq mathml
  16824. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16825. (when mathml-file
  16826. (write-region mathml nil mathml-file))
  16827. (when (org-called-interactively-p 'any)
  16828. (message mathml)))
  16829. ((message "LaTeX to MathML conversion failed")
  16830. (message shell-command-output)))
  16831. (delete-file tmp-in-file)
  16832. (when (file-exists-p tmp-out-file)
  16833. (delete-file tmp-out-file))
  16834. mathml))
  16835. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16836. prefix &optional dir)
  16837. "Use `org-create-math-formula' but check local cache first."
  16838. (let* ((absprefix (expand-file-name prefix dir))
  16839. (print-length nil) (print-level nil)
  16840. (formula-id (concat
  16841. "formula-"
  16842. (sha1
  16843. (prin1-to-string
  16844. (list latex-frag
  16845. org-latex-to-mathml-convert-command)))))
  16846. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16847. (formula-cache-dir (file-name-directory formula-cache)))
  16848. (unless (file-directory-p formula-cache-dir)
  16849. (make-directory formula-cache-dir t))
  16850. (unless (file-exists-p formula-cache)
  16851. (org-create-math-formula latex-frag formula-cache))
  16852. (if (file-exists-p formula-cache)
  16853. ;; Successful conversion. Return the link to MathML file.
  16854. (org-add-props
  16855. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16856. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16857. 'org-latex-src-embed-type (if latex-frag-type
  16858. 'paragraph 'character)))
  16859. ;; Failed conversion. Return the LaTeX fragment verbatim
  16860. latex-frag)))
  16861. (defun org-create-formula-image (string tofile options buffer &optional type)
  16862. "Create an image from LaTeX source using dvipng or convert.
  16863. This function calls either `org-create-formula-image-with-dvipng'
  16864. or `org-create-formula-image-with-imagemagick' depending on the
  16865. value of `org-latex-create-formula-image-program' or on the value
  16866. of the optional TYPE variable.
  16867. Note: ultimately these two function should be combined as they
  16868. share a good deal of logic."
  16869. (org-check-external-command
  16870. "latex" "needed to convert LaTeX fragments to images")
  16871. (funcall
  16872. (case (or type org-latex-create-formula-image-program)
  16873. ('dvipng
  16874. (org-check-external-command
  16875. "dvipng" "needed to convert LaTeX fragments to images")
  16876. #'org-create-formula-image-with-dvipng)
  16877. ('imagemagick
  16878. (org-check-external-command
  16879. "convert" "you need to install imagemagick")
  16880. #'org-create-formula-image-with-imagemagick)
  16881. (t (error
  16882. "invalid value of `org-latex-create-formula-image-program'")))
  16883. string tofile options buffer))
  16884. (declare-function org-export--get-global-options "ox" (&optional backend))
  16885. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16886. (defun org-create-formula--latex-header ()
  16887. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16888. (org-latex-guess-inputenc
  16889. (org-splice-latex-header
  16890. org-format-latex-header
  16891. org-latex-default-packages-alist
  16892. org-latex-packages-alist t
  16893. (plist-get
  16894. (org-combine-plists
  16895. (org-export--get-global-options 'latex)
  16896. (org-export--get-inbuffer-options 'latex))
  16897. :latex-header))))
  16898. ;; This function borrows from Ganesh Swami's latex2png.el
  16899. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16900. "This calls dvipng."
  16901. (require 'ox-latex)
  16902. (let* ((tmpdir (if (featurep 'xemacs)
  16903. (temp-directory)
  16904. temporary-file-directory))
  16905. (texfilebase (make-temp-name
  16906. (expand-file-name "orgtex" tmpdir)))
  16907. (texfile (concat texfilebase ".tex"))
  16908. (dvifile (concat texfilebase ".dvi"))
  16909. (pngfile (concat texfilebase ".png"))
  16910. (fnh (if (featurep 'xemacs)
  16911. (font-height (face-font 'default))
  16912. (face-attribute 'default :height nil)))
  16913. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16914. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16915. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16916. "Black"))
  16917. (bg (or (plist-get options (if buffer :background :html-background))
  16918. "Transparent")))
  16919. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16920. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16921. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16922. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16923. (let ((latex-header (org-create-formula--latex-header)))
  16924. (with-temp-file texfile
  16925. (insert latex-header)
  16926. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16927. (let ((dir default-directory))
  16928. (condition-case nil
  16929. (progn
  16930. (cd tmpdir)
  16931. (call-process "latex" nil nil nil texfile))
  16932. (error nil))
  16933. (cd dir))
  16934. (if (not (file-exists-p dvifile))
  16935. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16936. (condition-case nil
  16937. (if (featurep 'xemacs)
  16938. (call-process "dvipng" nil nil nil
  16939. "-fg" fg "-bg" bg
  16940. "-T" "tight"
  16941. "-o" pngfile
  16942. dvifile)
  16943. (call-process "dvipng" nil nil nil
  16944. "-fg" fg "-bg" bg
  16945. "-D" dpi
  16946. ;;"-x" scale "-y" scale
  16947. "-T" "tight"
  16948. "-o" pngfile
  16949. dvifile))
  16950. (error nil))
  16951. (if (not (file-exists-p pngfile))
  16952. (if org-format-latex-signal-error
  16953. (error "Failed to create png file from %s" texfile)
  16954. (message "Failed to create png file from %s" texfile)
  16955. nil)
  16956. ;; Use the requested file name and clean up
  16957. (copy-file pngfile tofile 'replace)
  16958. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16959. (if (file-exists-p (concat texfilebase e))
  16960. (delete-file (concat texfilebase e))))
  16961. pngfile))))
  16962. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  16963. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16964. "This calls convert, which is included into imagemagick."
  16965. (require 'ox-latex)
  16966. (let* ((tmpdir (if (featurep 'xemacs)
  16967. (temp-directory)
  16968. temporary-file-directory))
  16969. (texfilebase (make-temp-name
  16970. (expand-file-name "orgtex" tmpdir)))
  16971. (texfile (concat texfilebase ".tex"))
  16972. (pdffile (concat texfilebase ".pdf"))
  16973. (pngfile (concat texfilebase ".png"))
  16974. (fnh (if (featurep 'xemacs)
  16975. (font-height (face-font 'default))
  16976. (face-attribute 'default :height nil)))
  16977. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16978. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16979. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16980. "black"))
  16981. (bg (or (plist-get options (if buffer :background :html-background))
  16982. "white")))
  16983. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16984. (setq fg (org-latex-color-format fg)))
  16985. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16986. (setq bg (org-latex-color-format
  16987. (if (string= bg "Transparent") "white" bg))))
  16988. (let ((latex-header (org-create-formula--latex-header)))
  16989. (with-temp-file texfile
  16990. (insert latex-header)
  16991. (insert "\n\\begin{document}\n"
  16992. "\\definecolor{fg}{rgb}{" fg "}\n"
  16993. "\\definecolor{bg}{rgb}{" bg "}\n"
  16994. "\n\\pagecolor{bg}\n"
  16995. "\n{\\color{fg}\n"
  16996. string
  16997. "\n}\n"
  16998. "\n\\end{document}\n")))
  16999. (org-latex-compile texfile t)
  17000. (if (not (file-exists-p pdffile))
  17001. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17002. (condition-case nil
  17003. (if (featurep 'xemacs)
  17004. (call-process "convert" nil nil nil
  17005. "-density" "96"
  17006. "-trim"
  17007. "-antialias"
  17008. pdffile
  17009. "-quality" "100"
  17010. ;; "-sharpen" "0x1.0"
  17011. pngfile)
  17012. (call-process "convert" nil nil nil
  17013. "-density" dpi
  17014. "-trim"
  17015. "-antialias"
  17016. pdffile
  17017. "-quality" "100"
  17018. ;; "-sharpen" "0x1.0"
  17019. pngfile))
  17020. (error nil))
  17021. (if (not (file-exists-p pngfile))
  17022. (if org-format-latex-signal-error
  17023. (error "Failed to create png file from %s" texfile)
  17024. (message "Failed to create png file from %s" texfile)
  17025. nil)
  17026. ;; Use the requested file name and clean up
  17027. (copy-file pngfile tofile 'replace)
  17028. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17029. (if (file-exists-p (concat texfilebase e))
  17030. (delete-file (concat texfilebase e))))
  17031. pngfile))))
  17032. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17033. "Fill a LaTeX header template TPL.
  17034. In the template, the following place holders will be recognized:
  17035. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17036. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17037. [PACKAGES] \\usepackage statements for PKG
  17038. [NO-PACKAGES] do not include PKG
  17039. [EXTRA] the string EXTRA
  17040. [NO-EXTRA] do not include EXTRA
  17041. For backward compatibility, if both the positive and the negative place
  17042. holder is missing, the positive one (without the \"NO-\") will be
  17043. assumed to be present at the end of the template.
  17044. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17045. EXTRA is a string.
  17046. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17047. (let (rpl (end ""))
  17048. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17049. (setq rpl (if (or (match-end 1) (not def-pkg))
  17050. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17051. tpl (replace-match rpl t t tpl))
  17052. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17053. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17054. (setq rpl (if (or (match-end 1) (not pkg))
  17055. "" (org-latex-packages-to-string pkg snippets-p t))
  17056. tpl (replace-match rpl t t tpl))
  17057. (if pkg (setq end
  17058. (concat end "\n"
  17059. (org-latex-packages-to-string pkg snippets-p)))))
  17060. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17061. (setq rpl (if (or (match-end 1) (not extra))
  17062. "" (concat extra "\n"))
  17063. tpl (replace-match rpl t t tpl))
  17064. (if (and extra (string-match "\\S-" extra))
  17065. (setq end (concat end "\n" extra))))
  17066. (if (string-match "\\S-" end)
  17067. (concat tpl "\n" end)
  17068. tpl)))
  17069. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17070. "Turn an alist of packages into a string with the \\usepackage macros."
  17071. (setq pkg (mapconcat (lambda(p)
  17072. (cond
  17073. ((stringp p) p)
  17074. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17075. (format "%% Package %s omitted" (cadr p)))
  17076. ((equal "" (car p))
  17077. (format "\\usepackage{%s}" (cadr p)))
  17078. (t
  17079. (format "\\usepackage[%s]{%s}"
  17080. (car p) (cadr p)))))
  17081. pkg
  17082. "\n"))
  17083. (if newline (concat pkg "\n") pkg))
  17084. (defun org-dvipng-color (attr)
  17085. "Return a RGB color specification for dvipng."
  17086. (apply 'format "rgb %s %s %s"
  17087. (mapcar 'org-normalize-color
  17088. (if (featurep 'xemacs)
  17089. (color-rgb-components
  17090. (face-property 'default
  17091. (cond ((eq attr :foreground) 'foreground)
  17092. ((eq attr :background) 'background))))
  17093. (color-values (face-attribute 'default attr nil))))))
  17094. (defun org-dvipng-color-format (color-name)
  17095. "Convert COLOR-NAME to a RGB color value for dvipng."
  17096. (apply 'format "rgb %s %s %s"
  17097. (mapcar 'org-normalize-color
  17098. (color-values color-name))))
  17099. (defun org-latex-color (attr)
  17100. "Return a RGB color for the LaTeX color package."
  17101. (apply 'format "%s,%s,%s"
  17102. (mapcar 'org-normalize-color
  17103. (if (featurep 'xemacs)
  17104. (color-rgb-components
  17105. (face-property 'default
  17106. (cond ((eq attr :foreground) 'foreground)
  17107. ((eq attr :background) 'background))))
  17108. (color-values (face-attribute 'default attr nil))))))
  17109. (defun org-latex-color-format (color-name)
  17110. "Convert COLOR-NAME to a RGB color value."
  17111. (apply 'format "%s,%s,%s"
  17112. (mapcar 'org-normalize-color
  17113. (color-values color-name))))
  17114. (defun org-normalize-color (value)
  17115. "Return string to be used as color value for an RGB component."
  17116. (format "%g" (/ value 65535.0)))
  17117. ;; Image display
  17118. (defvar org-inline-image-overlays nil)
  17119. (make-variable-buffer-local 'org-inline-image-overlays)
  17120. (defun org-toggle-inline-images (&optional include-linked)
  17121. "Toggle the display of inline images.
  17122. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17123. (interactive "P")
  17124. (if org-inline-image-overlays
  17125. (progn
  17126. (org-remove-inline-images)
  17127. (message "Inline image display turned off"))
  17128. (org-display-inline-images include-linked)
  17129. (if (and (org-called-interactively-p)
  17130. org-inline-image-overlays)
  17131. (message "%d images displayed inline"
  17132. (length org-inline-image-overlays))
  17133. (message "No images to display inline"))))
  17134. (defun org-redisplay-inline-images ()
  17135. "Refresh the display of inline images."
  17136. (interactive)
  17137. (if (not org-inline-image-overlays)
  17138. (org-toggle-inline-images)
  17139. (org-toggle-inline-images)
  17140. (org-toggle-inline-images)))
  17141. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17142. "Display inline images.
  17143. Normally only links without a description part are inlined, because this
  17144. is how it will work for export. When INCLUDE-LINKED is set, also links
  17145. with a description part will be inlined. This can be nice for a quick
  17146. look at those images, but it does not reflect what exported files will look
  17147. like.
  17148. When REFRESH is set, refresh existing images between BEG and END.
  17149. This will create new image displays only if necessary.
  17150. BEG and END default to the buffer boundaries."
  17151. (interactive "P")
  17152. (unless refresh
  17153. (org-remove-inline-images)
  17154. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17155. (save-excursion
  17156. (save-restriction
  17157. (widen)
  17158. (setq beg (or beg (point-min)) end (or end (point-max)))
  17159. (goto-char beg)
  17160. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17161. (substring (org-image-file-name-regexp) 0 -2)
  17162. "\\)\\]" (if include-linked "" "\\]")))
  17163. (case-fold-search t)
  17164. old file ov img type attrwidth width)
  17165. (while (re-search-forward re end t)
  17166. (setq old (get-char-property-and-overlay (match-beginning 1)
  17167. 'org-image-overlay)
  17168. file (expand-file-name
  17169. (concat (or (match-string 3) "") (match-string 4))))
  17170. (when (image-type-available-p 'imagemagick)
  17171. (setq attrwidth (if (or (listp org-image-actual-width)
  17172. (null org-image-actual-width))
  17173. (save-excursion
  17174. (save-match-data
  17175. (when (re-search-backward
  17176. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17177. (save-excursion
  17178. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17179. (string-to-number (match-string 1))))))
  17180. width (cond ((eq org-image-actual-width t) nil)
  17181. ((null org-image-actual-width) attrwidth)
  17182. ((numberp org-image-actual-width)
  17183. org-image-actual-width)
  17184. ((listp org-image-actual-width)
  17185. (or attrwidth (car org-image-actual-width))))
  17186. type (if width 'imagemagick)))
  17187. (when (file-exists-p file)
  17188. (if (and (car-safe old) refresh)
  17189. (image-refresh (overlay-get (cdr old) 'display))
  17190. (setq img (save-match-data (create-image file type nil :width width)))
  17191. (when img
  17192. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17193. (overlay-put ov 'display img)
  17194. (overlay-put ov 'face 'default)
  17195. (overlay-put ov 'org-image-overlay t)
  17196. (overlay-put ov 'modification-hooks
  17197. (list 'org-display-inline-remove-overlay))
  17198. (push ov org-inline-image-overlays)))))))))
  17199. (define-obsolete-function-alias
  17200. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17201. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17202. "Remove inline-display overlay if a corresponding region is modified."
  17203. (let ((inhibit-modification-hooks t))
  17204. (when (and ov after)
  17205. (delete ov org-inline-image-overlays)
  17206. (delete-overlay ov))))
  17207. (defun org-remove-inline-images ()
  17208. "Remove inline display of images."
  17209. (interactive)
  17210. (mapc 'delete-overlay org-inline-image-overlays)
  17211. (setq org-inline-image-overlays nil))
  17212. ;;;; Key bindings
  17213. ;; Outline functions from `outline-mode-prefix-map'
  17214. ;; that can be remapped in Org:
  17215. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17216. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17217. (define-key org-mode-map [remap outline-forward-same-level]
  17218. 'org-forward-heading-same-level)
  17219. (define-key org-mode-map [remap outline-backward-same-level]
  17220. 'org-backward-heading-same-level)
  17221. (define-key org-mode-map [remap show-branches]
  17222. 'org-kill-note-or-show-branches)
  17223. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17224. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17225. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17226. ;; Outline functions from `outline-mode-prefix-map' that can not
  17227. ;; be remapped in Org:
  17228. ;;
  17229. ;; - the column "key binding" shows whether the Outline function is still
  17230. ;; available in Org mode on the same key that it has been bound to in
  17231. ;; Outline mode:
  17232. ;; - "overridden": key used for a different functionality in Org mode
  17233. ;; - else: key still bound to the same Outline function in Org mode
  17234. ;;
  17235. ;; | Outline function | key binding | Org replacement |
  17236. ;; |------------------------------------+-------------+-----------------------|
  17237. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17238. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17239. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17240. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17241. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17242. ;; | `show-entry' | overridden | no replacement |
  17243. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17244. ;; | `show-branches' | `C-c C-k' | still same function |
  17245. ;; | `show-subtree' | overridden | visibility cycling |
  17246. ;; | `show-all' | overridden | no replacement |
  17247. ;; | `hide-subtree' | overridden | visibility cycling |
  17248. ;; | `hide-body' | overridden | no replacement |
  17249. ;; | `hide-entry' | overridden | visibility cycling |
  17250. ;; | `hide-leaves' | overridden | no replacement |
  17251. ;; | `hide-sublevels' | overridden | no replacement |
  17252. ;; | `hide-other' | overridden | no replacement |
  17253. ;; Make `C-c C-x' a prefix key
  17254. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17255. ;; TAB key with modifiers
  17256. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17257. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17258. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17259. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17260. ;; The following line is necessary under Suse GNU/Linux
  17261. (unless (featurep 'xemacs)
  17262. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17263. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17264. (define-key org-mode-map [backtab] 'org-shifttab)
  17265. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17266. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17267. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17268. ;; Cursor keys with modifiers
  17269. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17270. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17271. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17272. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17273. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17274. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17275. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17276. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17277. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17278. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17279. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17280. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17281. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17282. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17283. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17284. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17285. ;; Babel keys
  17286. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17287. (mapc (lambda (pair)
  17288. (define-key org-babel-map (car pair) (cdr pair)))
  17289. org-babel-key-bindings)
  17290. ;;; Extra keys for tty access.
  17291. ;; We only set them when really needed because otherwise the
  17292. ;; menus don't show the simple keys
  17293. (when (or org-use-extra-keys
  17294. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17295. (not window-system))
  17296. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17297. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17298. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17299. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17300. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17301. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17302. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17303. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17304. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17305. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17306. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17307. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17308. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17309. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17310. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17311. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17312. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17313. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17314. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17315. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17316. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17317. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17318. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17319. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17320. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17321. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17322. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17323. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17324. ;; All the other keys
  17325. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17326. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17327. (if (boundp 'narrow-map)
  17328. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17329. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17330. (if (boundp 'narrow-map)
  17331. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17332. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17333. (if (boundp 'narrow-map)
  17334. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17335. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17336. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17337. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17338. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17339. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17340. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17341. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17342. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17343. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17344. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17345. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17346. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17347. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17348. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17349. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17350. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17351. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17352. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17353. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17354. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17355. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17356. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17357. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17358. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17359. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17360. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17361. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17362. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17363. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17364. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17365. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17366. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17367. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17368. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17369. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17370. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17371. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17372. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17373. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17374. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17375. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17376. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17377. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17378. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17379. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17380. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17381. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17382. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17383. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17384. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17385. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17386. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17387. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17388. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17389. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17390. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17391. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17392. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17393. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17394. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17395. (org-defkey org-mode-map "\C-m" 'org-return)
  17396. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17397. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17398. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17399. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17400. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17401. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17402. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17403. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17404. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17405. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17406. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17407. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17408. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17409. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17410. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17411. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17412. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17413. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17414. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17415. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17416. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17417. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17418. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17419. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17420. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17421. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17422. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17423. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17424. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17425. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17426. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17427. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17428. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17429. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17430. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17431. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17432. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17433. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17434. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17435. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17436. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17437. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17438. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17439. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17440. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17441. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17442. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17443. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17444. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17445. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17446. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17447. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17448. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17449. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17450. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17451. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17452. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17453. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17454. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17455. (when (featurep 'xemacs)
  17456. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17457. (defconst org-speed-commands-default
  17458. '(
  17459. ("Outline Navigation")
  17460. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17461. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17462. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17463. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17464. ("F" . org-next-block)
  17465. ("B" . org-previous-block)
  17466. ("u" . (org-speed-move-safe 'outline-up-heading))
  17467. ("j" . org-goto)
  17468. ("g" . (org-refile t))
  17469. ("Outline Visibility")
  17470. ("c" . org-cycle)
  17471. ("C" . org-shifttab)
  17472. (" " . org-display-outline-path)
  17473. ("s" . org-narrow-to-subtree)
  17474. ("=" . org-columns)
  17475. ("Outline Structure Editing")
  17476. ("U" . org-shiftmetaup)
  17477. ("D" . org-shiftmetadown)
  17478. ("r" . org-metaright)
  17479. ("l" . org-metaleft)
  17480. ("R" . org-shiftmetaright)
  17481. ("L" . org-shiftmetaleft)
  17482. ("i" . (progn (forward-char 1) (call-interactively
  17483. 'org-insert-heading-respect-content)))
  17484. ("^" . org-sort)
  17485. ("w" . org-refile)
  17486. ("a" . org-archive-subtree-default-with-confirmation)
  17487. ("@" . org-mark-subtree)
  17488. ("#" . org-toggle-comment)
  17489. ("Clock Commands")
  17490. ("I" . org-clock-in)
  17491. ("O" . org-clock-out)
  17492. ("Meta Data Editing")
  17493. ("t" . org-todo)
  17494. ("," . (org-priority))
  17495. ("0" . (org-priority ?\ ))
  17496. ("1" . (org-priority ?A))
  17497. ("2" . (org-priority ?B))
  17498. ("3" . (org-priority ?C))
  17499. (":" . org-set-tags-command)
  17500. ("e" . org-set-effort)
  17501. ("E" . org-inc-effort)
  17502. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17503. (org-entry-put (point) "APPT_WARNTIME" m)))
  17504. ("Agenda Views etc")
  17505. ("v" . org-agenda)
  17506. ("/" . org-sparse-tree)
  17507. ("Misc")
  17508. ("o" . org-open-at-point)
  17509. ("?" . org-speed-command-help)
  17510. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17511. (">" . (org-agenda-remove-restriction-lock))
  17512. )
  17513. "The default speed commands.")
  17514. (defun org-print-speed-command (e)
  17515. (if (> (length (car e)) 1)
  17516. (progn
  17517. (princ "\n")
  17518. (princ (car e))
  17519. (princ "\n")
  17520. (princ (make-string (length (car e)) ?-))
  17521. (princ "\n"))
  17522. (princ (car e))
  17523. (princ " ")
  17524. (if (symbolp (cdr e))
  17525. (princ (symbol-name (cdr e)))
  17526. (prin1 (cdr e)))
  17527. (princ "\n")))
  17528. (defun org-speed-command-help ()
  17529. "Show the available speed commands."
  17530. (interactive)
  17531. (if (not org-use-speed-commands)
  17532. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17533. (with-output-to-temp-buffer "*Help*"
  17534. (princ "User-defined Speed commands\n===========================\n")
  17535. (mapc 'org-print-speed-command org-speed-commands-user)
  17536. (princ "\n")
  17537. (princ "Built-in Speed commands\n=======================\n")
  17538. (mapc 'org-print-speed-command org-speed-commands-default))
  17539. (with-current-buffer "*Help*"
  17540. (setq truncate-lines t))))
  17541. (defun org-speed-move-safe (cmd)
  17542. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17543. If not, return to the original position and throw an error."
  17544. (interactive)
  17545. (let ((pos (point)))
  17546. (call-interactively cmd)
  17547. (unless (and (bolp) (org-at-heading-p))
  17548. (goto-char pos)
  17549. (error "Boundary reached while executing %s" cmd))))
  17550. (defvar org-self-insert-command-undo-counter 0)
  17551. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17552. (defvar org-speed-command nil)
  17553. (define-obsolete-function-alias
  17554. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17555. (defun org-speed-command-activate (keys)
  17556. "Hook for activating single-letter speed commands.
  17557. `org-speed-commands-default' specifies a minimal command set.
  17558. Use `org-speed-commands-user' for further customization."
  17559. (when (or (and (bolp) (looking-at org-outline-regexp))
  17560. (and (functionp org-use-speed-commands)
  17561. (funcall org-use-speed-commands)))
  17562. (cdr (assoc keys (append org-speed-commands-user
  17563. org-speed-commands-default)))))
  17564. (define-obsolete-function-alias
  17565. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17566. (defun org-babel-speed-command-activate (keys)
  17567. "Hook for activating single-letter code block commands."
  17568. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17569. (cdr (assoc keys org-babel-key-bindings))))
  17570. (defcustom org-speed-command-hook
  17571. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17572. "Hook for activating speed commands at strategic locations.
  17573. Hook functions are called in sequence until a valid handler is
  17574. found.
  17575. Each hook takes a single argument, a user-pressed command key
  17576. which is also a `self-insert-command' from the global map.
  17577. Within the hook, examine the cursor position and the command key
  17578. and return nil or a valid handler as appropriate. Handler could
  17579. be one of an interactive command, a function, or a form.
  17580. Set `org-use-speed-commands' to non-nil value to enable this
  17581. hook. The default setting is `org-speed-command-activate'."
  17582. :group 'org-structure
  17583. :version "24.1"
  17584. :type 'hook)
  17585. (defun org-self-insert-command (N)
  17586. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17587. If the cursor is in a table looking at whitespace, the whitespace is
  17588. overwritten, and the table is not marked as requiring realignment."
  17589. (interactive "p")
  17590. (org-check-before-invisible-edit 'insert)
  17591. (cond
  17592. ((and org-use-speed-commands
  17593. (setq org-speed-command
  17594. (run-hook-with-args-until-success
  17595. 'org-speed-command-hook (this-command-keys))))
  17596. (cond
  17597. ((commandp org-speed-command)
  17598. (setq this-command org-speed-command)
  17599. (call-interactively org-speed-command))
  17600. ((functionp org-speed-command)
  17601. (funcall org-speed-command))
  17602. ((and org-speed-command (listp org-speed-command))
  17603. (eval org-speed-command))
  17604. (t (let (org-use-speed-commands)
  17605. (call-interactively 'org-self-insert-command)))))
  17606. ((and
  17607. (org-table-p)
  17608. (progn
  17609. ;; check if we blank the field, and if that triggers align
  17610. (and (featurep 'org-table) org-table-auto-blank-field
  17611. (member last-command
  17612. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17613. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17614. ;; got extra space, this field does not determine column width
  17615. (let (org-table-may-need-update) (org-table-blank-field))
  17616. ;; no extra space, this field may determine column width
  17617. (org-table-blank-field)))
  17618. t)
  17619. (eq N 1)
  17620. (looking-at "[^|\n]* |"))
  17621. (let (org-table-may-need-update)
  17622. (goto-char (1- (match-end 0)))
  17623. (backward-delete-char 1)
  17624. (goto-char (match-beginning 0))
  17625. (self-insert-command N)))
  17626. (t
  17627. (setq org-table-may-need-update t)
  17628. (self-insert-command N)
  17629. (org-fix-tags-on-the-fly)
  17630. (if org-self-insert-cluster-for-undo
  17631. (if (not (eq last-command 'org-self-insert-command))
  17632. (setq org-self-insert-command-undo-counter 1)
  17633. (if (>= org-self-insert-command-undo-counter 20)
  17634. (setq org-self-insert-command-undo-counter 1)
  17635. (and (> org-self-insert-command-undo-counter 0)
  17636. buffer-undo-list (listp buffer-undo-list)
  17637. (not (cadr buffer-undo-list)) ; remove nil entry
  17638. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17639. (setq org-self-insert-command-undo-counter
  17640. (1+ org-self-insert-command-undo-counter))))))))
  17641. (defun org-check-before-invisible-edit (kind)
  17642. "Check is editing if kind KIND would be dangerous with invisible text around.
  17643. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17644. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17645. (if (and org-catch-invisible-edits
  17646. (or (not (boundp 'visible-mode)) (not visible-mode))
  17647. (or (get-char-property (point) 'invisible)
  17648. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17649. ;; OK, we need to take a closer look
  17650. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17651. (invisible-before-point (if (bobp) nil (get-char-property
  17652. (1- (point)) 'invisible)))
  17653. (border-and-ok-direction
  17654. (or
  17655. ;; Check if we are acting predictably before invisible text
  17656. (and invisible-at-point (not invisible-before-point)
  17657. (memq kind '(insert delete-backward)))
  17658. ;; Check if we are acting predictably after invisible text
  17659. ;; This works not well, and I have turned it off. It seems
  17660. ;; better to always show and stop after invisible text.
  17661. ;; (and (not invisible-at-point) invisible-before-point
  17662. ;; (memq kind '(insert delete)))
  17663. )))
  17664. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17665. (memq invisible-before-point '(outline org-hide-block t)))
  17666. (if (eq org-catch-invisible-edits 'error)
  17667. (error "Editing in invisible areas is prohibited - make visible first"))
  17668. (if (and org-custom-properties-overlays
  17669. (y-or-n-p "Display invisible properties in this buffer? "))
  17670. (org-toggle-custom-properties-visibility)
  17671. ;; Make the area visible
  17672. (save-excursion
  17673. (if invisible-before-point
  17674. (goto-char (previous-single-char-property-change
  17675. (point) 'invisible)))
  17676. (org-cycle))
  17677. (cond
  17678. ((eq org-catch-invisible-edits 'show)
  17679. ;; That's it, we do the edit after showing
  17680. (message
  17681. "Unfolding invisible region around point before editing")
  17682. (sit-for 1))
  17683. ((and (eq org-catch-invisible-edits 'smart)
  17684. border-and-ok-direction)
  17685. (message "Unfolding invisible region around point before editing"))
  17686. (t
  17687. ;; Don't do the edit, make the user repeat it in full visibility
  17688. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17689. (defun org-fix-tags-on-the-fly ()
  17690. (when (and (equal (char-after (point-at-bol)) ?*)
  17691. (org-at-heading-p))
  17692. (org-align-tags-here org-tags-column)))
  17693. (defun org-delete-backward-char (N)
  17694. "Like `delete-backward-char', insert whitespace at field end in tables.
  17695. When deleting backwards, in tables this function will insert whitespace in
  17696. front of the next \"|\" separator, to keep the table aligned. The table will
  17697. still be marked for re-alignment if the field did fill the entire column,
  17698. because, in this case the deletion might narrow the column."
  17699. (interactive "p")
  17700. (save-match-data
  17701. (org-check-before-invisible-edit 'delete-backward)
  17702. (if (and (org-table-p)
  17703. (eq N 1)
  17704. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17705. (looking-at ".*?|"))
  17706. (let ((pos (point))
  17707. (noalign (looking-at "[^|\n\r]* |"))
  17708. (c org-table-may-need-update))
  17709. (backward-delete-char N)
  17710. (if (not overwrite-mode)
  17711. (progn
  17712. (skip-chars-forward "^|")
  17713. (insert " ")
  17714. (goto-char (1- pos))))
  17715. ;; noalign: if there were two spaces at the end, this field
  17716. ;; does not determine the width of the column.
  17717. (if noalign (setq org-table-may-need-update c)))
  17718. (backward-delete-char N)
  17719. (org-fix-tags-on-the-fly))))
  17720. (defun org-delete-char (N)
  17721. "Like `delete-char', but insert whitespace at field end in tables.
  17722. When deleting characters, in tables this function will insert whitespace in
  17723. front of the next \"|\" separator, to keep the table aligned. The table will
  17724. still be marked for re-alignment if the field did fill the entire column,
  17725. because, in this case the deletion might narrow the column."
  17726. (interactive "p")
  17727. (save-match-data
  17728. (org-check-before-invisible-edit 'delete)
  17729. (if (and (org-table-p)
  17730. (not (bolp))
  17731. (not (= (char-after) ?|))
  17732. (eq N 1))
  17733. (if (looking-at ".*?|")
  17734. (let ((pos (point))
  17735. (noalign (looking-at "[^|\n\r]* |"))
  17736. (c org-table-may-need-update))
  17737. (replace-match
  17738. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17739. (goto-char pos)
  17740. ;; noalign: if there were two spaces at the end, this field
  17741. ;; does not determine the width of the column.
  17742. (if noalign (setq org-table-may-need-update c)))
  17743. (delete-char N))
  17744. (delete-char N)
  17745. (org-fix-tags-on-the-fly))))
  17746. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17747. (put 'org-self-insert-command 'delete-selection t)
  17748. (put 'orgtbl-self-insert-command 'delete-selection t)
  17749. (put 'org-delete-char 'delete-selection 'supersede)
  17750. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17751. (put 'org-yank 'delete-selection 'yank)
  17752. ;; Make `flyspell-mode' delay after some commands
  17753. (put 'org-self-insert-command 'flyspell-delayed t)
  17754. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17755. (put 'org-delete-char 'flyspell-delayed t)
  17756. (put 'org-delete-backward-char 'flyspell-delayed t)
  17757. ;; Make pabbrev-mode expand after org-mode commands
  17758. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17759. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17760. ;; How to do this: Measure non-white length of current string
  17761. ;; If equal to column width, we should realign.
  17762. (defun org-remap (map &rest commands)
  17763. "In MAP, remap the functions given in COMMANDS.
  17764. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17765. (let (new old)
  17766. (while commands
  17767. (setq old (pop commands) new (pop commands))
  17768. (if (fboundp 'command-remapping)
  17769. (org-defkey map (vector 'remap old) new)
  17770. (substitute-key-definition old new map global-map)))))
  17771. (defun org-transpose-words ()
  17772. "Transpose words for Org.
  17773. This uses the `org-mode-transpose-word-syntax-table' syntax
  17774. table, which interprets characters in `org-emphasis-alist' as
  17775. word constituants."
  17776. (interactive)
  17777. (with-syntax-table org-mode-transpose-word-syntax-table
  17778. (call-interactively 'transpose-words)))
  17779. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17780. (when (eq org-enable-table-editor 'optimized)
  17781. ;; If the user wants maximum table support, we need to hijack
  17782. ;; some standard editing functions
  17783. (org-remap org-mode-map
  17784. 'self-insert-command 'org-self-insert-command
  17785. 'delete-char 'org-delete-char
  17786. 'delete-backward-char 'org-delete-backward-char)
  17787. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17788. (defvar org-ctrl-c-ctrl-c-hook nil
  17789. "Hook for functions attaching themselves to `C-c C-c'.
  17790. This can be used to add additional functionality to the C-c C-c
  17791. key which executes context-dependent commands. This hook is run
  17792. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17793. run after the last test.
  17794. Each function will be called with no arguments. The function
  17795. must check if the context is appropriate for it to act. If yes,
  17796. it should do its thing and then return a non-nil value. If the
  17797. context is wrong, just do nothing and return nil.")
  17798. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17799. "Hook for functions attaching themselves to `C-c C-c'.
  17800. This can be used to add additional functionality to the C-c C-c
  17801. key which executes context-dependent commands. This hook is run
  17802. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17803. before the first test.
  17804. Each function will be called with no arguments. The function
  17805. must check if the context is appropriate for it to act. If yes,
  17806. it should do its thing and then return a non-nil value. If the
  17807. context is wrong, just do nothing and return nil.")
  17808. (defvar org-tab-first-hook nil
  17809. "Hook for functions to attach themselves to TAB.
  17810. See `org-ctrl-c-ctrl-c-hook' for more information.
  17811. This hook runs as the first action when TAB is pressed, even before
  17812. `org-cycle' messes around with the `outline-regexp' to cater for
  17813. inline tasks and plain list item folding.
  17814. If any function in this hook returns t, any other actions that
  17815. would have been caused by TAB (such as table field motion or visibility
  17816. cycling) will not occur.")
  17817. (defvar org-tab-after-check-for-table-hook nil
  17818. "Hook for functions to attach themselves to TAB.
  17819. See `org-ctrl-c-ctrl-c-hook' for more information.
  17820. This hook runs after it has been established that the cursor is not in a
  17821. table, but before checking if the cursor is in a headline or if global cycling
  17822. should be done.
  17823. If any function in this hook returns t, not other actions like visibility
  17824. cycling will be done.")
  17825. (defvar org-tab-after-check-for-cycling-hook nil
  17826. "Hook for functions to attach themselves to TAB.
  17827. See `org-ctrl-c-ctrl-c-hook' for more information.
  17828. This hook runs after it has been established that not table field motion and
  17829. not visibility should be done because of current context. This is probably
  17830. the place where a package like yasnippets can hook in.")
  17831. (defvar org-tab-before-tab-emulation-hook nil
  17832. "Hook for functions to attach themselves to TAB.
  17833. See `org-ctrl-c-ctrl-c-hook' for more information.
  17834. This hook runs after every other options for TAB have been exhausted, but
  17835. before indentation and \t insertion takes place.")
  17836. (defvar org-metaleft-hook nil
  17837. "Hook for functions attaching themselves to `M-left'.
  17838. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17839. (defvar org-metaright-hook nil
  17840. "Hook for functions attaching themselves to `M-right'.
  17841. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17842. (defvar org-metaup-hook nil
  17843. "Hook for functions attaching themselves to `M-up'.
  17844. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17845. (defvar org-metadown-hook nil
  17846. "Hook for functions attaching themselves to `M-down'.
  17847. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17848. (defvar org-shiftmetaleft-hook nil
  17849. "Hook for functions attaching themselves to `M-S-left'.
  17850. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17851. (defvar org-shiftmetaright-hook nil
  17852. "Hook for functions attaching themselves to `M-S-right'.
  17853. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17854. (defvar org-shiftmetaup-hook nil
  17855. "Hook for functions attaching themselves to `M-S-up'.
  17856. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17857. (defvar org-shiftmetadown-hook nil
  17858. "Hook for functions attaching themselves to `M-S-down'.
  17859. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17860. (defvar org-metareturn-hook nil
  17861. "Hook for functions attaching themselves to `M-RET'.
  17862. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17863. (defvar org-shiftup-hook nil
  17864. "Hook for functions attaching themselves to `S-up'.
  17865. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17866. (defvar org-shiftup-final-hook nil
  17867. "Hook for functions attaching themselves to `S-up'.
  17868. This one runs after all other options except shift-select have been excluded.
  17869. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17870. (defvar org-shiftdown-hook nil
  17871. "Hook for functions attaching themselves to `S-down'.
  17872. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17873. (defvar org-shiftdown-final-hook nil
  17874. "Hook for functions attaching themselves to `S-down'.
  17875. This one runs after all other options except shift-select have been excluded.
  17876. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17877. (defvar org-shiftleft-hook nil
  17878. "Hook for functions attaching themselves to `S-left'.
  17879. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17880. (defvar org-shiftleft-final-hook nil
  17881. "Hook for functions attaching themselves to `S-left'.
  17882. This one runs after all other options except shift-select have been excluded.
  17883. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17884. (defvar org-shiftright-hook nil
  17885. "Hook for functions attaching themselves to `S-right'.
  17886. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17887. (defvar org-shiftright-final-hook nil
  17888. "Hook for functions attaching themselves to `S-right'.
  17889. This one runs after all other options except shift-select have been excluded.
  17890. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17891. (defun org-modifier-cursor-error ()
  17892. "Throw an error, a modified cursor command was applied in wrong context."
  17893. (error "This command is active in special context like tables, headlines or items"))
  17894. (defun org-shiftselect-error ()
  17895. "Throw an error because Shift-Cursor command was applied in wrong context."
  17896. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17897. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17898. (error "This command works only in special context like headlines or timestamps")))
  17899. (defun org-call-for-shift-select (cmd)
  17900. (let ((this-command-keys-shift-translated t))
  17901. (call-interactively cmd)))
  17902. (defun org-shifttab (&optional arg)
  17903. "Global visibility cycling or move to previous table field.
  17904. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17905. on context.
  17906. See the individual commands for more information."
  17907. (interactive "P")
  17908. (cond
  17909. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17910. ((integerp arg)
  17911. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17912. (message "Content view to level: %d" arg)
  17913. (org-content (prefix-numeric-value arg2))
  17914. (setq org-cycle-global-status 'overview)))
  17915. (t (call-interactively 'org-global-cycle))))
  17916. (defun org-shiftmetaleft ()
  17917. "Promote subtree or delete table column.
  17918. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17919. `org-table-delete-column', depending on context. See the
  17920. individual commands for more information."
  17921. (interactive)
  17922. (cond
  17923. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17924. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17925. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17926. ((if (not (org-region-active-p)) (org-at-item-p)
  17927. (save-excursion (goto-char (region-beginning))
  17928. (org-at-item-p)))
  17929. (call-interactively 'org-outdent-item-tree))
  17930. (t (org-modifier-cursor-error))))
  17931. (defun org-shiftmetaright ()
  17932. "Demote subtree or insert table column.
  17933. Calls `org-demote-subtree', `org-indent-item-tree', or
  17934. `org-table-insert-column', depending on context. See the
  17935. individual commands for more information."
  17936. (interactive)
  17937. (cond
  17938. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17939. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17940. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17941. ((if (not (org-region-active-p)) (org-at-item-p)
  17942. (save-excursion (goto-char (region-beginning))
  17943. (org-at-item-p)))
  17944. (call-interactively 'org-indent-item-tree))
  17945. (t (org-modifier-cursor-error))))
  17946. (defun org-shiftmetaup (&optional arg)
  17947. "Move subtree up or kill table row.
  17948. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17949. `org-move-item-up' or `org-timestamp-up', depending on context.
  17950. See the individual commands for more information."
  17951. (interactive "P")
  17952. (cond
  17953. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17954. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17955. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17956. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17957. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17958. (call-interactively 'org-timestamp-up)))
  17959. (t (call-interactively 'org-drag-line-backward))))
  17960. (defun org-shiftmetadown (&optional arg)
  17961. "Move subtree down or insert table row.
  17962. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17963. `org-move-item-down' or `org-timestamp-up', depending on context.
  17964. See the individual commands for more information."
  17965. (interactive "P")
  17966. (cond
  17967. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17968. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17969. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17970. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17971. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17972. (call-interactively 'org-timestamp-down)))
  17973. (t (call-interactively 'org-drag-line-forward))))
  17974. (defsubst org-hidden-tree-error ()
  17975. (error
  17976. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17977. (defun org-metaleft (&optional arg)
  17978. "Promote heading or move table column to left.
  17979. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17980. With no specific context, calls the Emacs default `backward-word'.
  17981. See the individual commands for more information."
  17982. (interactive "P")
  17983. (cond
  17984. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17985. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17986. ((org-with-limited-levels
  17987. (or (org-at-heading-p)
  17988. (and (org-region-active-p)
  17989. (save-excursion
  17990. (goto-char (region-beginning))
  17991. (org-at-heading-p)))))
  17992. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17993. (call-interactively 'org-do-promote))
  17994. ;; At an inline task.
  17995. ((org-at-heading-p)
  17996. (call-interactively 'org-inlinetask-promote))
  17997. ((or (org-at-item-p)
  17998. (and (org-region-active-p)
  17999. (save-excursion
  18000. (goto-char (region-beginning))
  18001. (org-at-item-p))))
  18002. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18003. (call-interactively 'org-outdent-item))
  18004. (t (call-interactively 'backward-word))))
  18005. (defun org-metaright (&optional arg)
  18006. "Demote a subtree, a list item or move table column to right.
  18007. In front of a drawer or a block keyword, indent it correctly.
  18008. With no specific context, calls the Emacs default `forward-word'.
  18009. See the individual commands for more information."
  18010. (interactive "P")
  18011. (cond
  18012. ((run-hook-with-args-until-success 'org-metaright-hook))
  18013. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18014. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18015. ((org-at-block-p) (call-interactively 'org-indent-block))
  18016. ((org-with-limited-levels
  18017. (or (org-at-heading-p)
  18018. (and (org-region-active-p)
  18019. (save-excursion
  18020. (goto-char (region-beginning))
  18021. (org-at-heading-p)))))
  18022. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18023. (call-interactively 'org-do-demote))
  18024. ;; At an inline task.
  18025. ((org-at-heading-p)
  18026. (call-interactively 'org-inlinetask-demote))
  18027. ((or (org-at-item-p)
  18028. (and (org-region-active-p)
  18029. (save-excursion
  18030. (goto-char (region-beginning))
  18031. (org-at-item-p))))
  18032. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18033. (call-interactively 'org-indent-item))
  18034. (t (call-interactively 'forward-word))))
  18035. (defun org-check-for-hidden (what)
  18036. "Check if there are hidden headlines/items in the current visual line.
  18037. WHAT can be either `headlines' or `items'. If the current line is
  18038. an outline or item heading and it has a folded subtree below it,
  18039. this function returns t, nil otherwise."
  18040. (let ((re (cond
  18041. ((eq what 'headlines) org-outline-regexp-bol)
  18042. ((eq what 'items) (org-item-beginning-re))
  18043. (t (error "This should not happen"))))
  18044. beg end)
  18045. (save-excursion
  18046. (catch 'exit
  18047. (unless (org-region-active-p)
  18048. (setq beg (point-at-bol))
  18049. (beginning-of-line 2)
  18050. (while (and (not (eobp)) ;; this is like `next-line'
  18051. (get-char-property (1- (point)) 'invisible))
  18052. (beginning-of-line 2))
  18053. (setq end (point))
  18054. (goto-char beg)
  18055. (goto-char (point-at-eol))
  18056. (setq end (max end (point)))
  18057. (while (re-search-forward re end t)
  18058. (if (get-char-property (match-beginning 0) 'invisible)
  18059. (throw 'exit t))))
  18060. nil))))
  18061. (defun org-metaup (&optional arg)
  18062. "Move subtree up or move table row up.
  18063. Calls `org-move-subtree-up' or `org-table-move-row' or
  18064. `org-move-item-up', depending on context. See the individual commands
  18065. for more information."
  18066. (interactive "P")
  18067. (cond
  18068. ((run-hook-with-args-until-success 'org-metaup-hook))
  18069. ((org-region-active-p)
  18070. (let* ((a (min (region-beginning) (region-end)))
  18071. (b (1- (max (region-beginning) (region-end))))
  18072. (c (save-excursion (goto-char a)
  18073. (move-beginning-of-line 0)))
  18074. (d (save-excursion (goto-char a)
  18075. (move-end-of-line 0) (point))))
  18076. (transpose-regions a b c d)
  18077. (goto-char c)))
  18078. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18079. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18080. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18081. (t (org-drag-element-backward))))
  18082. (defun org-metadown (&optional arg)
  18083. "Move subtree down or move table row down.
  18084. Calls `org-move-subtree-down' or `org-table-move-row' or
  18085. `org-move-item-down', depending on context. See the individual
  18086. commands for more information."
  18087. (interactive "P")
  18088. (cond
  18089. ((run-hook-with-args-until-success 'org-metadown-hook))
  18090. ((org-region-active-p)
  18091. (let* ((a (min (region-beginning) (region-end)))
  18092. (b (max (region-beginning) (region-end)))
  18093. (c (save-excursion (goto-char b)
  18094. (move-beginning-of-line 1)))
  18095. (d (save-excursion (goto-char b)
  18096. (move-end-of-line 1) (1+ (point)))))
  18097. (transpose-regions a b c d)
  18098. (goto-char d)))
  18099. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18100. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18101. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18102. (t (org-drag-element-forward))))
  18103. (defun org-shiftup (&optional arg)
  18104. "Increase item in timestamp or increase priority of current headline.
  18105. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18106. depending on context. See the individual commands for more information."
  18107. (interactive "P")
  18108. (cond
  18109. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18110. ((and org-support-shift-select (org-region-active-p))
  18111. (org-call-for-shift-select 'previous-line))
  18112. ((org-at-timestamp-p t)
  18113. (call-interactively (if org-edit-timestamp-down-means-later
  18114. 'org-timestamp-down 'org-timestamp-up)))
  18115. ((and (not (eq org-support-shift-select 'always))
  18116. org-enable-priority-commands
  18117. (org-at-heading-p))
  18118. (call-interactively 'org-priority-up))
  18119. ((and (not org-support-shift-select) (org-at-item-p))
  18120. (call-interactively 'org-previous-item))
  18121. ((org-clocktable-try-shift 'up arg))
  18122. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18123. (org-support-shift-select
  18124. (org-call-for-shift-select 'previous-line))
  18125. (t (org-shiftselect-error))))
  18126. (defun org-shiftdown (&optional arg)
  18127. "Decrease item in timestamp or decrease priority of current headline.
  18128. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18129. depending on context. See the individual commands for more information."
  18130. (interactive "P")
  18131. (cond
  18132. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18133. ((and org-support-shift-select (org-region-active-p))
  18134. (org-call-for-shift-select 'next-line))
  18135. ((org-at-timestamp-p t)
  18136. (call-interactively (if org-edit-timestamp-down-means-later
  18137. 'org-timestamp-up 'org-timestamp-down)))
  18138. ((and (not (eq org-support-shift-select 'always))
  18139. org-enable-priority-commands
  18140. (org-at-heading-p))
  18141. (call-interactively 'org-priority-down))
  18142. ((and (not org-support-shift-select) (org-at-item-p))
  18143. (call-interactively 'org-next-item))
  18144. ((org-clocktable-try-shift 'down arg))
  18145. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18146. (org-support-shift-select
  18147. (org-call-for-shift-select 'next-line))
  18148. (t (org-shiftselect-error))))
  18149. (defun org-shiftright (&optional arg)
  18150. "Cycle the thing at point or in the current line, depending on context.
  18151. Depending on context, this does one of the following:
  18152. - switch a timestamp at point one day into the future
  18153. - on a headline, switch to the next TODO keyword.
  18154. - on an item, switch entire list to the next bullet type
  18155. - on a property line, switch to the next allowed value
  18156. - on a clocktable definition line, move time block into the future"
  18157. (interactive "P")
  18158. (cond
  18159. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18160. ((and org-support-shift-select (org-region-active-p))
  18161. (org-call-for-shift-select 'forward-char))
  18162. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18163. ((and (not (eq org-support-shift-select 'always))
  18164. (org-at-heading-p))
  18165. (let ((org-inhibit-logging
  18166. (not org-treat-S-cursor-todo-selection-as-state-change))
  18167. (org-inhibit-blocking
  18168. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18169. (org-call-with-arg 'org-todo 'right)))
  18170. ((or (and org-support-shift-select
  18171. (not (eq org-support-shift-select 'always))
  18172. (org-at-item-bullet-p))
  18173. (and (not org-support-shift-select) (org-at-item-p)))
  18174. (org-call-with-arg 'org-cycle-list-bullet nil))
  18175. ((and (not (eq org-support-shift-select 'always))
  18176. (org-at-property-p))
  18177. (call-interactively 'org-property-next-allowed-value))
  18178. ((org-clocktable-try-shift 'right arg))
  18179. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18180. (org-support-shift-select
  18181. (org-call-for-shift-select 'forward-char))
  18182. (t (org-shiftselect-error))))
  18183. (defun org-shiftleft (&optional arg)
  18184. "Cycle the thing at point or in the current line, depending on context.
  18185. Depending on context, this does one of the following:
  18186. - switch a timestamp at point one day into the past
  18187. - on a headline, switch to the previous TODO keyword.
  18188. - on an item, switch entire list to the previous bullet type
  18189. - on a property line, switch to the previous allowed value
  18190. - on a clocktable definition line, move time block into the past"
  18191. (interactive "P")
  18192. (cond
  18193. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18194. ((and org-support-shift-select (org-region-active-p))
  18195. (org-call-for-shift-select 'backward-char))
  18196. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18197. ((and (not (eq org-support-shift-select 'always))
  18198. (org-at-heading-p))
  18199. (let ((org-inhibit-logging
  18200. (not org-treat-S-cursor-todo-selection-as-state-change))
  18201. (org-inhibit-blocking
  18202. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18203. (org-call-with-arg 'org-todo 'left)))
  18204. ((or (and org-support-shift-select
  18205. (not (eq org-support-shift-select 'always))
  18206. (org-at-item-bullet-p))
  18207. (and (not org-support-shift-select) (org-at-item-p)))
  18208. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18209. ((and (not (eq org-support-shift-select 'always))
  18210. (org-at-property-p))
  18211. (call-interactively 'org-property-previous-allowed-value))
  18212. ((org-clocktable-try-shift 'left arg))
  18213. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18214. (org-support-shift-select
  18215. (org-call-for-shift-select 'backward-char))
  18216. (t (org-shiftselect-error))))
  18217. (defun org-shiftcontrolright ()
  18218. "Switch to next TODO set."
  18219. (interactive)
  18220. (cond
  18221. ((and org-support-shift-select (org-region-active-p))
  18222. (org-call-for-shift-select 'forward-word))
  18223. ((and (not (eq org-support-shift-select 'always))
  18224. (org-at-heading-p))
  18225. (org-call-with-arg 'org-todo 'nextset))
  18226. (org-support-shift-select
  18227. (org-call-for-shift-select 'forward-word))
  18228. (t (org-shiftselect-error))))
  18229. (defun org-shiftcontrolleft ()
  18230. "Switch to previous TODO set."
  18231. (interactive)
  18232. (cond
  18233. ((and org-support-shift-select (org-region-active-p))
  18234. (org-call-for-shift-select 'backward-word))
  18235. ((and (not (eq org-support-shift-select 'always))
  18236. (org-at-heading-p))
  18237. (org-call-with-arg 'org-todo 'previousset))
  18238. (org-support-shift-select
  18239. (org-call-for-shift-select 'backward-word))
  18240. (t (org-shiftselect-error))))
  18241. (defun org-shiftcontrolup (&optional n)
  18242. "Change timestamps synchronously up in CLOCK log lines.
  18243. Optional argument N tells to change by that many units."
  18244. (interactive "P")
  18245. (cond ((and (not org-support-shift-select)
  18246. (org-at-clock-log-p)
  18247. (org-at-timestamp-p t))
  18248. (org-clock-timestamps-up n))
  18249. (t (org-shiftselect-error))))
  18250. (defun org-shiftcontroldown (&optional n)
  18251. "Change timestamps synchronously down in CLOCK log lines.
  18252. Optional argument N tells to change by that many units."
  18253. (interactive "P")
  18254. (cond ((and (not org-support-shift-select)
  18255. (org-at-clock-log-p)
  18256. (org-at-timestamp-p t))
  18257. (org-clock-timestamps-down n))
  18258. (t (org-shiftselect-error))))
  18259. (defun org-ctrl-c-ret ()
  18260. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18261. (interactive)
  18262. (cond
  18263. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18264. (t (call-interactively 'org-insert-heading))))
  18265. (defun org-find-visible ()
  18266. (let ((s (point)))
  18267. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18268. (get-char-property s 'invisible)))
  18269. s))
  18270. (defun org-find-invisible ()
  18271. (let ((s (point)))
  18272. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18273. (not (get-char-property s 'invisible))))
  18274. s))
  18275. (defun org-copy-visible (beg end)
  18276. "Copy the visible parts of the region."
  18277. (interactive "r")
  18278. (let (snippets s)
  18279. (save-excursion
  18280. (save-restriction
  18281. (narrow-to-region beg end)
  18282. (setq s (goto-char (point-min)))
  18283. (while (not (= (point) (point-max)))
  18284. (goto-char (org-find-invisible))
  18285. (push (buffer-substring s (point)) snippets)
  18286. (setq s (goto-char (org-find-visible))))))
  18287. (kill-new (apply 'concat (nreverse snippets)))))
  18288. (defun org-copy-special ()
  18289. "Copy region in table or copy current subtree.
  18290. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18291. See the individual commands for more information."
  18292. (interactive)
  18293. (call-interactively
  18294. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18295. (defun org-cut-special ()
  18296. "Cut region in table or cut current subtree.
  18297. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18298. See the individual commands for more information."
  18299. (interactive)
  18300. (call-interactively
  18301. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18302. (defun org-paste-special (arg)
  18303. "Paste rectangular region into table, or past subtree relative to level.
  18304. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18305. See the individual commands for more information."
  18306. (interactive "P")
  18307. (if (org-at-table-p)
  18308. (org-table-paste-rectangle)
  18309. (org-paste-subtree arg)))
  18310. (defsubst org-in-fixed-width-region-p ()
  18311. "Is point in a fixed-width region?"
  18312. (save-match-data
  18313. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18314. (defun org-edit-special (&optional arg)
  18315. "Call a special editor for the element at point.
  18316. When at a table, call the formula editor with `org-table-edit-formulas'.
  18317. When in a source code block, call `org-edit-src-code'.
  18318. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18319. When at an #+INCLUDE keyword, visit the included file.
  18320. On a link, call `ffap' to visit the link at point.
  18321. Otherwise, return a user error."
  18322. (interactive "P")
  18323. (let ((element (org-element-at-point)))
  18324. (assert (not buffer-read-only) nil
  18325. "Buffer is read-only: %s" (buffer-name))
  18326. (case (org-element-type element)
  18327. (src-block
  18328. (if (not arg) (org-edit-src-code)
  18329. (let* ((info (org-babel-get-src-block-info))
  18330. (lang (nth 0 info))
  18331. (params (nth 2 info))
  18332. (session (cdr (assq :session params))))
  18333. (if (not session) (org-edit-src-code)
  18334. ;; At a src-block with a session and function called with
  18335. ;; an ARG: switch to the buffer related to the inferior
  18336. ;; process.
  18337. (switch-to-buffer
  18338. (funcall (intern (concat "org-babel-prep-session:" lang))
  18339. session params))))))
  18340. (keyword
  18341. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18342. (find-file
  18343. (org-remove-double-quotes
  18344. (car (org-split-string (org-element-property :value element)))))
  18345. (user-error "No special environment to edit here")))
  18346. (table
  18347. (if (eq (org-element-property :type element) 'table.el)
  18348. (org-edit-src-code)
  18349. (call-interactively 'org-table-edit-formulas)))
  18350. ;; Only Org tables contain `table-row' type elements.
  18351. (table-row (call-interactively 'org-table-edit-formulas))
  18352. ((example-block export-block) (org-edit-src-code))
  18353. (fixed-width (org-edit-fixed-width-region))
  18354. (otherwise
  18355. ;; No notable element at point. Though, we may be at a link,
  18356. ;; which is an object. Thus, scan deeper.
  18357. (if (eq (org-element-type (org-element-context element)) 'link)
  18358. (call-interactively 'ffap)
  18359. (user-error "No special environment to edit here"))))))
  18360. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18361. (defun org-ctrl-c-ctrl-c (&optional arg)
  18362. "Set tags in headline, or update according to changed information at point.
  18363. This command does many different things, depending on context:
  18364. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18365. this is what we do.
  18366. - If the cursor is on a statistics cookie, update it.
  18367. - If the cursor is in a headline, prompt for tags and insert them
  18368. into the current line, aligned to `org-tags-column'. When called
  18369. with prefix arg, realign all tags in the current buffer.
  18370. - If the cursor is in one of the special #+KEYWORD lines, this
  18371. triggers scanning the buffer for these lines and updating the
  18372. information.
  18373. - If the cursor is inside a table, realign the table. This command
  18374. works even if the automatic table editor has been turned off.
  18375. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18376. the entire table.
  18377. - If the cursor is at a footnote reference or definition, jump to
  18378. the corresponding definition or references, respectively.
  18379. - If the cursor is a the beginning of a dynamic block, update it.
  18380. - If the current buffer is a capture buffer, close note and file it.
  18381. - If the cursor is on a <<<target>>>, update radio targets and
  18382. corresponding links in this buffer.
  18383. - If the cursor is on a numbered item in a plain list, renumber the
  18384. ordered list.
  18385. - If the cursor is on a checkbox, toggle it.
  18386. - If the cursor is on a code block, evaluate it. The variable
  18387. `org-confirm-babel-evaluate' can be used to control prompting
  18388. before code block evaluation, by default every code block
  18389. evaluation requires confirmation. Code block evaluation can be
  18390. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18391. (interactive "P")
  18392. (cond
  18393. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18394. org-occur-highlights
  18395. org-latex-fragment-image-overlays)
  18396. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18397. (org-remove-occur-highlights)
  18398. (org-remove-latex-fragment-image-overlays)
  18399. (message "Temporary highlights/overlays removed from current buffer"))
  18400. ((and (local-variable-p 'org-finish-function (current-buffer))
  18401. (fboundp org-finish-function))
  18402. (funcall org-finish-function))
  18403. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18404. (t
  18405. (let* ((context (org-element-context)) (type (org-element-type context)))
  18406. ;; Test if point is within a blank line.
  18407. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18408. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18409. (user-error "C-c C-c can do nothing useful at this location"))
  18410. ;; For convenience: at the first line of a paragraph on the
  18411. ;; same line as an item, apply function on that item instead.
  18412. (when (eq type 'paragraph)
  18413. (let ((parent (org-element-property :parent context)))
  18414. (when (and (eq (org-element-type parent) 'item)
  18415. (= (point-at-bol) (org-element-property :begin parent)))
  18416. (setq context parent type 'item))))
  18417. ;; Act according to type of element or object at point.
  18418. (case type
  18419. (clock (org-clock-update-time-maybe))
  18420. (dynamic-block
  18421. (save-excursion
  18422. (goto-char (org-element-property :post-affiliated context))
  18423. (org-update-dblock)))
  18424. (footnote-definition
  18425. (goto-char (org-element-property :post-affiliated context))
  18426. (call-interactively 'org-footnote-action))
  18427. (footnote-reference (call-interactively 'org-footnote-action))
  18428. ((headline inlinetask)
  18429. (save-excursion (goto-char (org-element-property :begin context))
  18430. (call-interactively 'org-set-tags)))
  18431. (item
  18432. ;; At an item: a double C-u set checkbox to "[-]"
  18433. ;; unconditionally, whereas a single one will toggle its
  18434. ;; presence. Without an universal argument, if the item
  18435. ;; has a checkbox, toggle it. Otherwise repair the list.
  18436. (let* ((box (org-element-property :checkbox context))
  18437. (struct (org-element-property :structure context))
  18438. (old-struct (copy-tree struct))
  18439. (parents (org-list-parents-alist struct))
  18440. (prevs (org-list-prevs-alist struct))
  18441. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18442. (org-list-set-checkbox
  18443. (org-element-property :begin context) struct
  18444. (cond ((equal arg '(16)) "[-]")
  18445. ((and (not box) (equal arg '(4))) "[ ]")
  18446. ((or (not box) (equal arg '(4))) nil)
  18447. ((eq box 'on) "[ ]")
  18448. (t "[X]")))
  18449. ;; Mimic `org-list-write-struct' but with grabbing
  18450. ;; a return value from `org-list-struct-fix-box'.
  18451. (org-list-struct-fix-ind struct parents 2)
  18452. (org-list-struct-fix-item-end struct)
  18453. (org-list-struct-fix-bul struct prevs)
  18454. (org-list-struct-fix-ind struct parents)
  18455. (let ((block-item
  18456. (org-list-struct-fix-box struct parents prevs orderedp)))
  18457. (if (and box (equal struct old-struct))
  18458. (if (equal arg '(16))
  18459. (message "Checkboxes already reset")
  18460. (user-error "Cannot toggle this checkbox: %s"
  18461. (if (eq box 'on)
  18462. "all subitems checked"
  18463. "unchecked subitems")))
  18464. (org-list-struct-apply-struct struct old-struct)
  18465. (org-update-checkbox-count-maybe))
  18466. (when block-item
  18467. (message "Checkboxes were removed due to empty box at line %d"
  18468. (org-current-line block-item))))))
  18469. (keyword
  18470. (let ((org-inhibit-startup-visibility-stuff t)
  18471. (org-startup-align-all-tables nil))
  18472. (when (boundp 'org-table-coordinate-overlays)
  18473. (mapc 'delete-overlay org-table-coordinate-overlays)
  18474. (setq org-table-coordinate-overlays nil))
  18475. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18476. (message "Local setup has been refreshed"))
  18477. (plain-list
  18478. ;; At a plain list, with a double C-u argument, set
  18479. ;; checkboxes of each item to "[-]", whereas a single one
  18480. ;; will toggle their presence according to the state of the
  18481. ;; first item in the list. Without an argument, repair the
  18482. ;; list.
  18483. (let* ((begin (org-element-property :contents-begin context))
  18484. (struct (org-element-property :structure context))
  18485. (old-struct (copy-tree struct))
  18486. (first-box (save-excursion
  18487. (goto-char begin)
  18488. (looking-at org-list-full-item-re)
  18489. (match-string-no-properties 3)))
  18490. (new-box (cond ((equal arg '(16)) "[-]")
  18491. ((equal arg '(4)) (unless first-box "[ ]"))
  18492. ((equal first-box "[X]") "[ ]")
  18493. (t "[X]"))))
  18494. (cond
  18495. (arg
  18496. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18497. (org-list-get-all-items
  18498. begin struct (org-list-prevs-alist struct))))
  18499. ((and first-box (eq (point) begin))
  18500. ;; For convenience, when point is at bol on the first
  18501. ;; item of the list and no argument is provided, simply
  18502. ;; toggle checkbox of that item, if any.
  18503. (org-list-set-checkbox begin struct new-box)))
  18504. (org-list-write-struct
  18505. struct (org-list-parents-alist struct) old-struct)
  18506. (org-update-checkbox-count-maybe)
  18507. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18508. ((property-drawer node-property)
  18509. (call-interactively 'org-property-action))
  18510. ((radio-target target)
  18511. (call-interactively 'org-update-radio-target-regexp))
  18512. (statistics-cookie
  18513. (call-interactively 'org-update-statistics-cookies))
  18514. ((table table-cell table-row)
  18515. ;; At a table, recalculate every field and align it. Also
  18516. ;; send the table if necessary. If the table has
  18517. ;; a `table.el' type, just give up. At a table row or
  18518. ;; cell, maybe recalculate line but always align table.
  18519. (if (eq (org-element-property :type context) 'table.el)
  18520. (message "Use C-c ' to edit table.el tables")
  18521. (let ((org-enable-table-editor t))
  18522. (if (or (eq type 'table)
  18523. ;; Check if point is at a TBLFM line.
  18524. (and (eq type 'table-row)
  18525. (= (point) (org-element-property :end context))))
  18526. (save-excursion
  18527. (goto-char (org-element-property :contents-begin context))
  18528. (org-call-with-arg 'org-table-recalculate (or arg t))
  18529. (orgtbl-send-table 'maybe))
  18530. (org-table-maybe-eval-formula)
  18531. (cond (arg (call-interactively 'org-table-recalculate))
  18532. ((org-table-maybe-recalculate-line))
  18533. (t (org-table-align)))))))
  18534. (timestamp (org-timestamp-change 0 'day))
  18535. (otherwise
  18536. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18537. (user-error
  18538. "C-c C-c can do nothing useful at this location")))))))))
  18539. (defun org-mode-restart ()
  18540. "Restart Org-mode, to scan again for special lines.
  18541. Also updates the keyword regular expressions."
  18542. (interactive)
  18543. (org-mode)
  18544. (message "Org-mode restarted"))
  18545. (defun org-kill-note-or-show-branches ()
  18546. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18547. (interactive)
  18548. (if (not org-finish-function)
  18549. (progn
  18550. (hide-subtree)
  18551. (call-interactively 'show-branches))
  18552. (let ((org-note-abort t))
  18553. (funcall org-finish-function))))
  18554. (defun org-return (&optional indent)
  18555. "Goto next table row or insert a newline.
  18556. Calls `org-table-next-row' or `newline', depending on context.
  18557. See the individual commands for more information."
  18558. (interactive)
  18559. (let (org-ts-what)
  18560. (cond
  18561. ((or (bobp) (org-in-src-block-p))
  18562. (if indent (newline-and-indent) (newline)))
  18563. ((org-at-table-p)
  18564. (org-table-justify-field-maybe)
  18565. (call-interactively 'org-table-next-row))
  18566. ;; when `newline-and-indent' is called within a list, make sure
  18567. ;; text moved stays inside the item.
  18568. ((and (org-in-item-p) indent)
  18569. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18570. (progn
  18571. (save-match-data (newline))
  18572. (org-indent-line-to (length (match-string 0))))
  18573. (let ((ind (org-get-indentation)))
  18574. (newline)
  18575. (if (org-looking-back org-list-end-re)
  18576. (org-indent-line)
  18577. (org-indent-line-to ind)))))
  18578. ((and org-return-follows-link
  18579. (org-at-timestamp-p t)
  18580. (not (eq org-ts-what 'after)))
  18581. (org-follow-timestamp-link))
  18582. ((and org-return-follows-link
  18583. (let ((tprop (get-text-property (point) 'face)))
  18584. (or (eq tprop 'org-link)
  18585. (and (listp tprop) (memq 'org-link tprop)))))
  18586. (call-interactively 'org-open-at-point))
  18587. ((and (org-at-heading-p)
  18588. (looking-at
  18589. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18590. (org-show-entry)
  18591. (end-of-line 1)
  18592. (newline))
  18593. (t (if indent (newline-and-indent) (newline))))))
  18594. (defun org-return-indent ()
  18595. "Goto next table row or insert a newline and indent.
  18596. Calls `org-table-next-row' or `newline-and-indent', depending on
  18597. context. See the individual commands for more information."
  18598. (interactive)
  18599. (org-return t))
  18600. (defun org-ctrl-c-star ()
  18601. "Compute table, or change heading status of lines.
  18602. Calls `org-table-recalculate' or `org-toggle-heading',
  18603. depending on context."
  18604. (interactive)
  18605. (cond
  18606. ((org-at-table-p)
  18607. (call-interactively 'org-table-recalculate))
  18608. (t
  18609. ;; Convert all lines in region to list items
  18610. (call-interactively 'org-toggle-heading))))
  18611. (defun org-ctrl-c-minus ()
  18612. "Insert separator line in table or modify bullet status of line.
  18613. Also turns a plain line or a region of lines into list items.
  18614. Calls `org-table-insert-hline', `org-toggle-item', or
  18615. `org-cycle-list-bullet', depending on context."
  18616. (interactive)
  18617. (cond
  18618. ((org-at-table-p)
  18619. (call-interactively 'org-table-insert-hline))
  18620. ((org-region-active-p)
  18621. (call-interactively 'org-toggle-item))
  18622. ((org-in-item-p)
  18623. (call-interactively 'org-cycle-list-bullet))
  18624. (t
  18625. (call-interactively 'org-toggle-item))))
  18626. (defun org-toggle-item (arg)
  18627. "Convert headings or normal lines to items, items to normal lines.
  18628. If there is no active region, only the current line is considered.
  18629. If the first non blank line in the region is a headline, convert
  18630. all headlines to items, shifting text accordingly.
  18631. If it is an item, convert all items to normal lines.
  18632. If it is normal text, change region into a list of items.
  18633. With a prefix argument ARG, change the region in a single item."
  18634. (interactive "P")
  18635. (let ((shift-text
  18636. (function
  18637. ;; Shift text in current section to IND, from point to END.
  18638. ;; The function leaves point to END line.
  18639. (lambda (ind end)
  18640. (let ((min-i 1000) (end (copy-marker end)))
  18641. ;; First determine the minimum indentation (MIN-I) of
  18642. ;; the text.
  18643. (save-excursion
  18644. (catch 'exit
  18645. (while (< (point) end)
  18646. (let ((i (org-get-indentation)))
  18647. (cond
  18648. ;; Skip blank lines and inline tasks.
  18649. ((looking-at "^[ \t]*$"))
  18650. ((looking-at org-outline-regexp-bol))
  18651. ;; We can't find less than 0 indentation.
  18652. ((zerop i) (throw 'exit (setq min-i 0)))
  18653. ((< i min-i) (setq min-i i))))
  18654. (forward-line))))
  18655. ;; Then indent each line so that a line indented to
  18656. ;; MIN-I becomes indented to IND. Ignore blank lines
  18657. ;; and inline tasks in the process.
  18658. (let ((delta (- ind min-i)))
  18659. (while (< (point) end)
  18660. (unless (or (looking-at "^[ \t]*$")
  18661. (looking-at org-outline-regexp-bol))
  18662. (org-indent-line-to (+ (org-get-indentation) delta)))
  18663. (forward-line)))))))
  18664. (skip-blanks
  18665. (function
  18666. ;; Return beginning of first non-blank line, starting from
  18667. ;; line at POS.
  18668. (lambda (pos)
  18669. (save-excursion
  18670. (goto-char pos)
  18671. (skip-chars-forward " \r\t\n")
  18672. (point-at-bol)))))
  18673. beg end)
  18674. ;; Determine boundaries of changes.
  18675. (if (org-region-active-p)
  18676. (setq beg (funcall skip-blanks (region-beginning))
  18677. end (copy-marker (region-end)))
  18678. (setq beg (funcall skip-blanks (point-at-bol))
  18679. end (copy-marker (point-at-eol))))
  18680. ;; Depending on the starting line, choose an action on the text
  18681. ;; between BEG and END.
  18682. (org-with-limited-levels
  18683. (save-excursion
  18684. (goto-char beg)
  18685. (cond
  18686. ;; Case 1. Start at an item: de-itemize. Note that it only
  18687. ;; happens when a region is active: `org-ctrl-c-minus'
  18688. ;; would call `org-cycle-list-bullet' otherwise.
  18689. ((org-at-item-p)
  18690. (while (< (point) end)
  18691. (when (org-at-item-p)
  18692. (skip-chars-forward " \t")
  18693. (delete-region (point) (match-end 0)))
  18694. (forward-line)))
  18695. ;; Case 2. Start at an heading: convert to items.
  18696. ((org-at-heading-p)
  18697. (let* ((bul (org-list-bullet-string "-"))
  18698. (bul-len (length bul))
  18699. ;; Indentation of the first heading. It should be
  18700. ;; relative to the indentation of its parent, if any.
  18701. (start-ind (save-excursion
  18702. (cond
  18703. ((not org-adapt-indentation) 0)
  18704. ((not (outline-previous-heading)) 0)
  18705. (t (length (match-string 0))))))
  18706. ;; Level of first heading. Further headings will be
  18707. ;; compared to it to determine hierarchy in the list.
  18708. (ref-level (org-reduced-level (org-outline-level))))
  18709. (while (< (point) end)
  18710. (let* ((level (org-reduced-level (org-outline-level)))
  18711. (delta (max 0 (- level ref-level))))
  18712. ;; If current headline is less indented than the first
  18713. ;; one, set it as reference, in order to preserve
  18714. ;; subtrees.
  18715. (when (< level ref-level) (setq ref-level level))
  18716. (replace-match bul t t)
  18717. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18718. ;; Ensure all text down to END (or SECTION-END) belongs
  18719. ;; to the newly created item.
  18720. (let ((section-end (save-excursion
  18721. (or (outline-next-heading) (point)))))
  18722. (forward-line)
  18723. (funcall shift-text
  18724. (+ start-ind (* (1+ delta) bul-len))
  18725. (min end section-end)))))))
  18726. ;; Case 3. Normal line with ARG: make the first line of region
  18727. ;; an item, and shift indentation of others lines to
  18728. ;; set them as item's body.
  18729. (arg (let* ((bul (org-list-bullet-string "-"))
  18730. (bul-len (length bul))
  18731. (ref-ind (org-get-indentation)))
  18732. (skip-chars-forward " \t")
  18733. (insert bul)
  18734. (forward-line)
  18735. (while (< (point) end)
  18736. ;; Ensure that lines less indented than first one
  18737. ;; still get included in item body.
  18738. (funcall shift-text
  18739. (+ ref-ind bul-len)
  18740. (min end (save-excursion (or (outline-next-heading)
  18741. (point)))))
  18742. (forward-line))))
  18743. ;; Case 4. Normal line without ARG: turn each non-item line
  18744. ;; into an item.
  18745. (t
  18746. (while (< (point) end)
  18747. (unless (or (org-at-heading-p) (org-at-item-p))
  18748. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18749. (replace-match
  18750. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18751. (forward-line))))))))
  18752. (defun org-toggle-heading (&optional nstars)
  18753. "Convert headings to normal text, or items or text to headings.
  18754. If there is no active region, only convert the current line.
  18755. With a \\[universal-argument] prefix, convert the whole list at
  18756. point into heading.
  18757. In a region:
  18758. - If the first non blank line is a headline, remove the stars
  18759. from all headlines in the region.
  18760. - If it is a normal line, turn each and every normal line (i.e.,
  18761. not an heading or an item) in the region into headings. If you
  18762. want to convert only the first line of this region, use one
  18763. universal prefix argument.
  18764. - If it is a plain list item, turn all plain list items into headings.
  18765. When converting a line into a heading, the number of stars is chosen
  18766. such that the lines become children of the current entry. However,
  18767. when a numeric prefix argument is given, its value determines the
  18768. number of stars to add."
  18769. (interactive "P")
  18770. (let ((skip-blanks
  18771. (function
  18772. ;; Return beginning of first non-blank line, starting from
  18773. ;; line at POS.
  18774. (lambda (pos)
  18775. (save-excursion
  18776. (goto-char pos)
  18777. (while (org-at-comment-p) (forward-line))
  18778. (skip-chars-forward " \r\t\n")
  18779. (point-at-bol)))))
  18780. beg end toggled)
  18781. ;; Determine boundaries of changes. If a universal prefix has
  18782. ;; been given, put the list in a region. If region ends at a bol,
  18783. ;; do not consider the last line to be in the region.
  18784. (when (and current-prefix-arg (org-at-item-p))
  18785. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18786. (org-mark-element))
  18787. (if (org-region-active-p)
  18788. (setq beg (funcall skip-blanks (region-beginning))
  18789. end (copy-marker (save-excursion
  18790. (goto-char (region-end))
  18791. (if (bolp) (point) (point-at-eol)))))
  18792. (setq beg (funcall skip-blanks (point-at-bol))
  18793. end (copy-marker (point-at-eol))))
  18794. ;; Ensure inline tasks don't count as headings.
  18795. (org-with-limited-levels
  18796. (save-excursion
  18797. (goto-char beg)
  18798. (cond
  18799. ;; Case 1. Started at an heading: de-star headings.
  18800. ((org-at-heading-p)
  18801. (while (< (point) end)
  18802. (when (org-at-heading-p t)
  18803. (looking-at org-outline-regexp) (replace-match "")
  18804. (setq toggled t))
  18805. (forward-line)))
  18806. ;; Case 2. Started at an item: change items into headlines.
  18807. ;; One star will be added by `org-list-to-subtree'.
  18808. ((org-at-item-p)
  18809. (let* ((stars (make-string
  18810. ;; subtract the star that will be added again by
  18811. ;; `org-list-to-subtree'
  18812. (if (numberp nstars) (1- nstars)
  18813. (or (org-current-level) 0))
  18814. ?*))
  18815. (add-stars
  18816. (cond (nstars "") ; stars from prefix only
  18817. ((equal stars "") "") ; before first heading
  18818. (org-odd-levels-only "*") ; inside heading, odd
  18819. (t "")))) ; inside heading, oddeven
  18820. (while (< (point) end)
  18821. (when (org-at-item-p)
  18822. ;; Pay attention to cases when region ends before list.
  18823. (let* ((struct (org-list-struct))
  18824. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18825. (save-restriction
  18826. (narrow-to-region (point) list-end)
  18827. (insert
  18828. (org-list-to-subtree
  18829. (org-list-parse-list t)
  18830. '(:istart (concat stars add-stars (funcall get-stars depth))
  18831. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18832. (setq toggled t))
  18833. (forward-line))))
  18834. ;; Case 3. Started at normal text: make every line an heading,
  18835. ;; skipping headlines and items.
  18836. (t (let* ((stars
  18837. (make-string
  18838. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18839. (add-stars
  18840. (cond (nstars "") ; stars from prefix only
  18841. ((equal stars "") "*") ; before first heading
  18842. (org-odd-levels-only "**") ; inside heading, odd
  18843. (t "*"))) ; inside heading, oddeven
  18844. (rpl (concat stars add-stars " "))
  18845. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18846. (while (< (point) (if (equal nstars '(4)) lend end))
  18847. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18848. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18849. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18850. (forward-line)))))))
  18851. (unless toggled (message "Cannot toggle heading from here"))))
  18852. (defun org-meta-return (&optional arg)
  18853. "Insert a new heading or wrap a region in a table.
  18854. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18855. See the individual commands for more information."
  18856. (interactive "P")
  18857. (cond
  18858. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18859. ((or (org-at-drawer-p) (org-at-property-p))
  18860. (newline-and-indent))
  18861. ((org-at-table-p)
  18862. (call-interactively 'org-table-wrap-region))
  18863. (t (call-interactively 'org-insert-heading))))
  18864. ;;; Menu entries
  18865. (defsubst org-in-subtree-not-table-p ()
  18866. "Are we in a subtree and not in a table?"
  18867. (and (not (org-before-first-heading-p))
  18868. (not (org-at-table-p))))
  18869. ;; Define the Org-mode menus
  18870. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18871. '("Tbl"
  18872. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18873. ["Next Field" org-cycle (org-at-table-p)]
  18874. ["Previous Field" org-shifttab (org-at-table-p)]
  18875. ["Next Row" org-return (org-at-table-p)]
  18876. "--"
  18877. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18878. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18879. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18880. "--"
  18881. ("Column"
  18882. ["Move Column Left" org-metaleft (org-at-table-p)]
  18883. ["Move Column Right" org-metaright (org-at-table-p)]
  18884. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18885. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18886. ("Row"
  18887. ["Move Row Up" org-metaup (org-at-table-p)]
  18888. ["Move Row Down" org-metadown (org-at-table-p)]
  18889. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18890. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18891. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18892. "--"
  18893. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18894. ("Rectangle"
  18895. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18896. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18897. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18898. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18899. "--"
  18900. ("Calculate"
  18901. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18902. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18903. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18904. "--"
  18905. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18906. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18907. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18908. "--"
  18909. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18910. "--"
  18911. ["Sum Column/Rectangle" org-table-sum
  18912. (or (org-at-table-p) (org-region-active-p))]
  18913. ["Which Column?" org-table-current-column (org-at-table-p)])
  18914. ["Debug Formulas"
  18915. org-table-toggle-formula-debugger
  18916. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18917. ["Show Col/Row Numbers"
  18918. org-table-toggle-coordinate-overlays
  18919. :style toggle
  18920. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18921. "--"
  18922. ["Create" org-table-create (and (not (org-at-table-p))
  18923. org-enable-table-editor)]
  18924. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18925. ["Import from File" org-table-import (not (org-at-table-p))]
  18926. ["Export to File" org-table-export (org-at-table-p)]
  18927. "--"
  18928. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18929. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18930. '("Org"
  18931. ("Show/Hide"
  18932. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18933. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18934. ["Sparse Tree..." org-sparse-tree t]
  18935. ["Reveal Context" org-reveal t]
  18936. ["Show All" show-all t]
  18937. "--"
  18938. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18939. "--"
  18940. ["New Heading" org-insert-heading t]
  18941. ("Navigate Headings"
  18942. ["Up" outline-up-heading t]
  18943. ["Next" outline-next-visible-heading t]
  18944. ["Previous" outline-previous-visible-heading t]
  18945. ["Next Same Level" outline-forward-same-level t]
  18946. ["Previous Same Level" outline-backward-same-level t]
  18947. "--"
  18948. ["Jump" org-goto t])
  18949. ("Edit Structure"
  18950. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18951. "--"
  18952. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18953. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18954. "--"
  18955. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18956. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18957. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18958. "--"
  18959. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18960. "--"
  18961. ["Copy visible text" org-copy-visible t]
  18962. "--"
  18963. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18964. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18965. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18966. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18967. "--"
  18968. ["Sort Region/Children" org-sort t]
  18969. "--"
  18970. ["Convert to odd levels" org-convert-to-odd-levels t]
  18971. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18972. ("Editing"
  18973. ["Emphasis..." org-emphasize t]
  18974. ["Edit Source Example" org-edit-special t]
  18975. "--"
  18976. ["Footnote new/jump" org-footnote-action t]
  18977. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18978. ("Archive"
  18979. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18980. "--"
  18981. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18982. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18983. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18984. )
  18985. "--"
  18986. ("Hyperlinks"
  18987. ["Store Link (Global)" org-store-link t]
  18988. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18989. ["Insert Link" org-insert-link t]
  18990. ["Follow Link" org-open-at-point t]
  18991. "--"
  18992. ["Next link" org-next-link t]
  18993. ["Previous link" org-previous-link t]
  18994. "--"
  18995. ["Descriptive Links"
  18996. org-toggle-link-display
  18997. :style radio
  18998. :selected org-descriptive-links
  18999. ]
  19000. ["Literal Links"
  19001. org-toggle-link-display
  19002. :style radio
  19003. :selected (not org-descriptive-links)])
  19004. "--"
  19005. ("TODO Lists"
  19006. ["TODO/DONE/-" org-todo t]
  19007. ("Select keyword"
  19008. ["Next keyword" org-shiftright (org-at-heading-p)]
  19009. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19010. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19011. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19012. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19013. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19014. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19015. "--"
  19016. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19017. :selected org-enforce-todo-dependencies :style toggle :active t]
  19018. "Settings for tree at point"
  19019. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19020. :selected (org-entry-get nil "ORDERED")
  19021. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19022. ["Do Children parallel" org-toggle-ordered-property :style radio
  19023. :selected (not (org-entry-get nil "ORDERED"))
  19024. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19025. "--"
  19026. ["Set Priority" org-priority t]
  19027. ["Priority Up" org-shiftup t]
  19028. ["Priority Down" org-shiftdown t]
  19029. "--"
  19030. ["Get news from all feeds" org-feed-update-all t]
  19031. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19032. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19033. ("TAGS and Properties"
  19034. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19035. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19036. "--"
  19037. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19038. ["Column view of properties" org-columns t]
  19039. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19040. ("Dates and Scheduling"
  19041. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19042. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19043. ("Change Date"
  19044. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19045. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19046. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19047. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19048. ["Compute Time Range" org-evaluate-time-range t]
  19049. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19050. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19051. "--"
  19052. ["Custom time format" org-toggle-time-stamp-overlays
  19053. :style radio :selected org-display-custom-times]
  19054. "--"
  19055. ["Goto Calendar" org-goto-calendar t]
  19056. ["Date from Calendar" org-date-from-calendar t]
  19057. "--"
  19058. ["Start/Restart Timer" org-timer-start t]
  19059. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19060. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19061. ["Insert Timer String" org-timer t]
  19062. ["Insert Timer Item" org-timer-item t])
  19063. ("Logging work"
  19064. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19065. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19066. ["Clock out" org-clock-out t]
  19067. ["Clock cancel" org-clock-cancel t]
  19068. "--"
  19069. ["Mark as default task" org-clock-mark-default-task t]
  19070. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19071. ["Goto running clock" org-clock-goto t]
  19072. "--"
  19073. ["Display times" org-clock-display t]
  19074. ["Create clock table" org-clock-report t]
  19075. "--"
  19076. ["Record DONE time"
  19077. (progn (setq org-log-done (not org-log-done))
  19078. (message "Switching to %s will %s record a timestamp"
  19079. (car org-done-keywords)
  19080. (if org-log-done "automatically" "not")))
  19081. :style toggle :selected org-log-done])
  19082. "--"
  19083. ["Agenda Command..." org-agenda t]
  19084. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19085. ("File List for Agenda")
  19086. ("Special views current file"
  19087. ["TODO Tree" org-show-todo-tree t]
  19088. ["Check Deadlines" org-check-deadlines t]
  19089. ["Timeline" org-timeline t]
  19090. ["Tags/Property tree" org-match-sparse-tree t])
  19091. "--"
  19092. ["Export/Publish..." org-export-dispatch t]
  19093. ("LaTeX"
  19094. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19095. :selected org-cdlatex-mode]
  19096. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19097. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19098. ["Modify math symbol" org-cdlatex-math-modify
  19099. (org-inside-LaTeX-fragment-p)]
  19100. ["Insert citation" org-reftex-citation t]
  19101. "--"
  19102. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19103. "--"
  19104. ("MobileOrg"
  19105. ["Push Files and Views" org-mobile-push t]
  19106. ["Get Captured and Flagged" org-mobile-pull t]
  19107. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19108. "--"
  19109. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19110. "--"
  19111. ("Documentation"
  19112. ["Show Version" org-version t]
  19113. ["Info Documentation" org-info t])
  19114. ("Customize"
  19115. ["Browse Org Group" org-customize t]
  19116. "--"
  19117. ["Expand This Menu" org-create-customize-menu
  19118. (fboundp 'customize-menu-create)])
  19119. ["Send bug report" org-submit-bug-report t]
  19120. "--"
  19121. ("Refresh/Reload"
  19122. ["Refresh setup current buffer" org-mode-restart t]
  19123. ["Reload Org (after update)" org-reload t]
  19124. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19125. ))
  19126. (defun org-info (&optional node)
  19127. "Read documentation for Org-mode in the info system.
  19128. With optional NODE, go directly to that node."
  19129. (interactive)
  19130. (info (format "(org)%s" (or node ""))))
  19131. ;;;###autoload
  19132. (defun org-submit-bug-report ()
  19133. "Submit a bug report on Org-mode via mail.
  19134. Don't hesitate to report any problems or inaccurate documentation.
  19135. If you don't have setup sending mail from (X)Emacs, please copy the
  19136. output buffer into your mail program, as it gives us important
  19137. information about your Org-mode version and configuration."
  19138. (interactive)
  19139. (require 'reporter)
  19140. (org-load-modules-maybe)
  19141. (org-require-autoloaded-modules)
  19142. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19143. (reporter-submit-bug-report
  19144. "emacs-orgmode@gnu.org"
  19145. (org-version nil 'full)
  19146. (let (list)
  19147. (save-window-excursion
  19148. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19149. (delete-other-windows)
  19150. (erase-buffer)
  19151. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19152. We would like to add your full Org-mode and Outline configuration to the
  19153. bug report. This greatly simplifies the work of the maintainer and
  19154. other experts on the mailing list.
  19155. HOWEVER, some variables you have customized may contain private
  19156. information. The names of customers, colleagues, or friends, might
  19157. appear in the form of file names, tags, todo states, or search strings.
  19158. If you answer yes to the prompt, you might want to check and remove
  19159. such private information before sending the email.")
  19160. (add-text-properties (point-min) (point-max) '(face org-warning))
  19161. (when (yes-or-no-p "Include your Org-mode configuration ")
  19162. (mapatoms
  19163. (lambda (v)
  19164. (and (boundp v)
  19165. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19166. (or (and (symbol-value v)
  19167. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19168. (and
  19169. (get v 'custom-type) (get v 'standard-value)
  19170. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19171. (push v list)))))
  19172. (kill-buffer (get-buffer "*Warn about privacy*"))
  19173. list))
  19174. nil nil
  19175. "Remember to cover the basics, that is, what you expected to happen and
  19176. what in fact did happen. You don't know how to make a good report? See
  19177. http://orgmode.org/manual/Feedback.html#Feedback
  19178. Your bug report will be posted to the Org-mode mailing list.
  19179. ------------------------------------------------------------------------")
  19180. (save-excursion
  19181. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19182. (replace-match "\\1Bug: \\3 [\\2]")))))
  19183. (defun org-install-agenda-files-menu ()
  19184. (let ((bl (buffer-list)))
  19185. (save-excursion
  19186. (while bl
  19187. (set-buffer (pop bl))
  19188. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19189. (when (derived-mode-p 'org-mode)
  19190. (easy-menu-change
  19191. '("Org") "File List for Agenda"
  19192. (append
  19193. (list
  19194. ["Edit File List" (org-edit-agenda-file-list) t]
  19195. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19196. ["Remove Current File from List" org-remove-file t]
  19197. ["Cycle through agenda files" org-cycle-agenda-files t]
  19198. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19199. "--")
  19200. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19201. ;;;; Documentation
  19202. (defun org-require-autoloaded-modules ()
  19203. (interactive)
  19204. (mapc 'require
  19205. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19206. org-remember org-table org-timer)))
  19207. ;;;###autoload
  19208. (defun org-reload (&optional uncompiled)
  19209. "Reload all org lisp files.
  19210. With prefix arg UNCOMPILED, load the uncompiled versions."
  19211. (interactive "P")
  19212. (require 'loadhist)
  19213. (let* ((org-dir (org-find-library-dir "org"))
  19214. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19215. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19216. (remove-re (mapconcat 'identity
  19217. (mapcar (lambda (f) (concat "^" f "$"))
  19218. (list (if (featurep 'xemacs)
  19219. "org-colview"
  19220. "org-colview-xemacs")
  19221. "org" "org-loaddefs" "org-version"))
  19222. "\\|"))
  19223. (feats (delete-dups
  19224. (mapcar 'file-name-sans-extension
  19225. (mapcar 'file-name-nondirectory
  19226. (delq nil
  19227. (mapcar 'feature-file
  19228. features))))))
  19229. (lfeat (append
  19230. (sort
  19231. (setq feats
  19232. (delq nil (mapcar
  19233. (lambda (f)
  19234. (if (and (string-match feature-re f)
  19235. (not (string-match remove-re f)))
  19236. f nil))
  19237. feats)))
  19238. 'string-lessp)
  19239. (list "org-version" "org")))
  19240. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19241. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19242. load-uncore load-misses)
  19243. (setq load-misses
  19244. (delq 't
  19245. (mapcar (lambda (f)
  19246. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19247. (and (string= org-dir contrib-dir)
  19248. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19249. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19250. (add-to-list 'load-uncore f 'append)
  19251. 't)
  19252. f))
  19253. lfeat)))
  19254. (if load-uncore
  19255. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19256. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19257. (if load-misses
  19258. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19259. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19260. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19261. ;;;###autoload
  19262. (defun org-customize ()
  19263. "Call the customize function with org as argument."
  19264. (interactive)
  19265. (org-load-modules-maybe)
  19266. (org-require-autoloaded-modules)
  19267. (customize-browse 'org))
  19268. (defun org-create-customize-menu ()
  19269. "Create a full customization menu for Org-mode, insert it into the menu."
  19270. (interactive)
  19271. (org-load-modules-maybe)
  19272. (org-require-autoloaded-modules)
  19273. (if (fboundp 'customize-menu-create)
  19274. (progn
  19275. (easy-menu-change
  19276. '("Org") "Customize"
  19277. `(["Browse Org group" org-customize t]
  19278. "--"
  19279. ,(customize-menu-create 'org)
  19280. ["Set" Custom-set t]
  19281. ["Save" Custom-save t]
  19282. ["Reset to Current" Custom-reset-current t]
  19283. ["Reset to Saved" Custom-reset-saved t]
  19284. ["Reset to Standard Settings" Custom-reset-standard t]))
  19285. (message "\"Org\"-menu now contains full customization menu"))
  19286. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19287. ;;;; Miscellaneous stuff
  19288. ;;; Generally useful functions
  19289. (defun org-get-at-bol (property)
  19290. "Get text property PROPERTY at beginning of line."
  19291. (get-text-property (point-at-bol) property))
  19292. (defun org-find-text-property-in-string (prop s)
  19293. "Return the first non-nil value of property PROP in string S."
  19294. (or (get-text-property 0 prop s)
  19295. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19296. prop s)))
  19297. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19298. "Display the given MESSAGE as a warning."
  19299. (if (fboundp 'display-warning)
  19300. (display-warning 'org message
  19301. (if (featurep 'xemacs) 'warning :warning))
  19302. (let ((buf (get-buffer-create "*Org warnings*")))
  19303. (with-current-buffer buf
  19304. (goto-char (point-max))
  19305. (insert "Warning (Org): " message)
  19306. (unless (bolp)
  19307. (newline)))
  19308. (display-buffer buf)
  19309. (sit-for 0))))
  19310. (defun org-eval (form)
  19311. "Eval FORM and return result."
  19312. (condition-case error
  19313. (eval form)
  19314. (error (format "%%![Error: %s]" error))))
  19315. (defun org-in-clocktable-p ()
  19316. "Check if the cursor is in a clocktable."
  19317. (let ((pos (point)) start)
  19318. (save-excursion
  19319. (end-of-line 1)
  19320. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19321. (setq start (match-beginning 0))
  19322. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19323. (>= (match-end 0) pos)
  19324. start))))
  19325. (defun org-in-commented-line ()
  19326. "Is point in a line starting with `#'?"
  19327. (equal (char-after (point-at-bol)) ?#))
  19328. (defun org-in-indented-comment-line ()
  19329. "Is point in a line starting with `#' after some white space?"
  19330. (save-excursion
  19331. (save-match-data
  19332. (goto-char (point-at-bol))
  19333. (looking-at "[ \t]*#"))))
  19334. (defun org-in-verbatim-emphasis ()
  19335. (save-match-data
  19336. (and (org-in-regexp org-emph-re 2)
  19337. (>= (point) (match-beginning 3))
  19338. (<= (point) (match-end 4))
  19339. (member (match-string 3) '("=" "~")))))
  19340. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19341. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19342. (if (and marker (marker-buffer marker)
  19343. (buffer-live-p (marker-buffer marker)))
  19344. (progn
  19345. (org-pop-to-buffer-same-window (marker-buffer marker))
  19346. (if (or (> marker (point-max)) (< marker (point-min)))
  19347. (widen))
  19348. (goto-char marker)
  19349. (org-show-context 'org-goto))
  19350. (if bookmark
  19351. (bookmark-jump bookmark)
  19352. (error "Cannot find location"))))
  19353. (defun org-quote-csv-field (s)
  19354. "Quote field for inclusion in CSV material."
  19355. (if (string-match "[\",]" s)
  19356. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19357. s))
  19358. (defun org-force-self-insert (N)
  19359. "Needed to enforce self-insert under remapping."
  19360. (interactive "p")
  19361. (self-insert-command N))
  19362. (defun org-string-width (s)
  19363. "Compute width of string, ignoring invisible characters.
  19364. This ignores character with invisibility property `org-link', and also
  19365. characters with property `org-cwidth', because these will become invisible
  19366. upon the next fontification round."
  19367. (let (b l)
  19368. (when (or (eq t buffer-invisibility-spec)
  19369. (assq 'org-link buffer-invisibility-spec))
  19370. (while (setq b (text-property-any 0 (length s)
  19371. 'invisible 'org-link s))
  19372. (setq s (concat (substring s 0 b)
  19373. (substring s (or (next-single-property-change
  19374. b 'invisible s) (length s)))))))
  19375. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19376. (setq s (concat (substring s 0 b)
  19377. (substring s (or (next-single-property-change
  19378. b 'org-cwidth s) (length s))))))
  19379. (setq l (string-width s) b -1)
  19380. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19381. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19382. l))
  19383. (defun org-shorten-string (s maxlength)
  19384. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19385. If the string is shorter or has length MAXLENGTH, just return the
  19386. original string. If it is longer, the functions finds a space in the
  19387. string, breaks this string off at that locations and adds three dots
  19388. as ellipsis. Including the ellipsis, the string will not be longer
  19389. than MAXLENGTH. If finding a good breaking point in the string does
  19390. not work, the string is just chopped off in the middle of a word
  19391. if necessary."
  19392. (if (<= (length s) maxlength)
  19393. s
  19394. (let* ((n (max (- maxlength 4) 1))
  19395. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19396. (if (string-match re s)
  19397. (concat (match-string 1 s) "...")
  19398. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19399. (defun org-get-indentation (&optional line)
  19400. "Get the indentation of the current line, interpreting tabs.
  19401. When LINE is given, assume it represents a line and compute its indentation."
  19402. (if line
  19403. (if (string-match "^ *" (org-remove-tabs line))
  19404. (match-end 0))
  19405. (save-excursion
  19406. (beginning-of-line 1)
  19407. (skip-chars-forward " \t")
  19408. (current-column))))
  19409. (defun org-get-string-indentation (s)
  19410. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19411. (let ((n -1) (i 0) (w tab-width) c)
  19412. (catch 'exit
  19413. (while (< (setq n (1+ n)) (length s))
  19414. (setq c (aref s n))
  19415. (cond ((= c ?\ ) (setq i (1+ i)))
  19416. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19417. (t (throw 'exit t)))))
  19418. i))
  19419. (defun org-remove-tabs (s &optional width)
  19420. "Replace tabulators in S with spaces.
  19421. Assumes that s is a single line, starting in column 0."
  19422. (setq width (or width tab-width))
  19423. (while (string-match "\t" s)
  19424. (setq s (replace-match
  19425. (make-string
  19426. (- (* width (/ (+ (match-beginning 0) width) width))
  19427. (match-beginning 0)) ?\ )
  19428. t t s)))
  19429. s)
  19430. (defun org-fix-indentation (line ind)
  19431. "Fix indentation in LINE.
  19432. IND is a cons cell with target and minimum indentation.
  19433. If the current indentation in LINE is smaller than the minimum,
  19434. leave it alone. If it is larger than ind, set it to the target."
  19435. (let* ((l (org-remove-tabs line))
  19436. (i (org-get-indentation l))
  19437. (i1 (car ind)) (i2 (cdr ind)))
  19438. (if (>= i i2) (setq l (substring line i2)))
  19439. (if (> i1 0)
  19440. (concat (make-string i1 ?\ ) l)
  19441. l)))
  19442. (defun org-remove-indentation (code &optional n)
  19443. "Remove the maximum common indentation from the lines in CODE.
  19444. N may optionally be the number of spaces to remove."
  19445. (with-temp-buffer
  19446. (insert code)
  19447. (org-do-remove-indentation n)
  19448. (buffer-string)))
  19449. (defun org-do-remove-indentation (&optional n)
  19450. "Remove the maximum common indentation from the buffer."
  19451. (untabify (point-min) (point-max))
  19452. (let ((min 10000) re)
  19453. (if n
  19454. (setq min n)
  19455. (goto-char (point-min))
  19456. (while (re-search-forward "^ *[^ \n]" nil t)
  19457. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19458. (unless (or (= min 0) (= min 10000))
  19459. (setq re (format "^ \\{%d\\}" min))
  19460. (goto-char (point-min))
  19461. (while (re-search-forward re nil t)
  19462. (replace-match "")
  19463. (end-of-line 1))
  19464. min)))
  19465. (defun org-fill-template (template alist)
  19466. "Find each %key of ALIST in TEMPLATE and replace it."
  19467. (let ((case-fold-search nil)
  19468. entry key value)
  19469. (setq alist (sort (copy-sequence alist)
  19470. (lambda (a b) (< (length (car a)) (length (car b))))))
  19471. (while (setq entry (pop alist))
  19472. (setq template
  19473. (replace-regexp-in-string
  19474. (concat "%" (regexp-quote (car entry)))
  19475. (or (cdr entry) "") template t t)))
  19476. template))
  19477. (defun org-base-buffer (buffer)
  19478. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19479. (if (not buffer)
  19480. buffer
  19481. (or (buffer-base-buffer buffer)
  19482. buffer)))
  19483. (defun org-trim (s)
  19484. "Remove whitespace at beginning and end of string."
  19485. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19486. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19487. s)
  19488. (defun org-wrap (string &optional width lines)
  19489. "Wrap string to either a number of lines, or a width in characters.
  19490. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19491. that costs. If there is a word longer than WIDTH, the text is actually
  19492. wrapped to the length of that word.
  19493. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19494. many lines, whatever width that takes.
  19495. The return value is a list of lines, without newlines at the end."
  19496. (let* ((words (org-split-string string "[ \t\n]+"))
  19497. (maxword (apply 'max (mapcar 'org-string-width words)))
  19498. w ll)
  19499. (cond (width
  19500. (org-do-wrap words (max maxword width)))
  19501. (lines
  19502. (setq w maxword)
  19503. (setq ll (org-do-wrap words maxword))
  19504. (if (<= (length ll) lines)
  19505. ll
  19506. (setq ll words)
  19507. (while (> (length ll) lines)
  19508. (setq w (1+ w))
  19509. (setq ll (org-do-wrap words w)))
  19510. ll))
  19511. (t (error "Cannot wrap this")))))
  19512. (defun org-do-wrap (words width)
  19513. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19514. (let (lines line)
  19515. (while words
  19516. (setq line (pop words))
  19517. (while (and words (< (+ (length line) (length (car words))) width))
  19518. (setq line (concat line " " (pop words))))
  19519. (setq lines (push line lines)))
  19520. (nreverse lines)))
  19521. (defun org-split-string (string &optional separators)
  19522. "Splits STRING into substrings at SEPARATORS.
  19523. No empty strings are returned if there are matches at the beginning
  19524. and end of string."
  19525. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19526. (start 0)
  19527. notfirst
  19528. (list nil))
  19529. (while (and (string-match rexp string
  19530. (if (and notfirst
  19531. (= start (match-beginning 0))
  19532. (< start (length string)))
  19533. (1+ start) start))
  19534. (< (match-beginning 0) (length string)))
  19535. (setq notfirst t)
  19536. (or (eq (match-beginning 0) 0)
  19537. (and (eq (match-beginning 0) (match-end 0))
  19538. (eq (match-beginning 0) start))
  19539. (setq list
  19540. (cons (substring string start (match-beginning 0))
  19541. list)))
  19542. (setq start (match-end 0)))
  19543. (or (eq start (length string))
  19544. (setq list
  19545. (cons (substring string start)
  19546. list)))
  19547. (nreverse list)))
  19548. (defun org-quote-vert (s)
  19549. "Replace \"|\" with \"\\vert\"."
  19550. (while (string-match "|" s)
  19551. (setq s (replace-match "\\vert" t t s)))
  19552. s)
  19553. (defun org-uuidgen-p (s)
  19554. "Is S an ID created by UUIDGEN?"
  19555. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19556. (defun org-in-src-block-p (&optional inside)
  19557. "Whether point is in a code source block.
  19558. When INSIDE is non-nil, don't consider we are within a src block
  19559. when point is at #+BEGIN_SRC or #+END_SRC."
  19560. (let ((case-fold-search t) ov)
  19561. (or (and (setq ov (overlays-at (point)))
  19562. (memq 'org-block-background
  19563. (overlay-properties (car ov))))
  19564. (and (not inside)
  19565. (save-match-data
  19566. (save-excursion
  19567. (beginning-of-line)
  19568. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19569. (defun org-context ()
  19570. "Return a list of contexts of the current cursor position.
  19571. If several contexts apply, all are returned.
  19572. Each context entry is a list with a symbol naming the context, and
  19573. two positions indicating start and end of the context. Possible
  19574. contexts are:
  19575. :headline anywhere in a headline
  19576. :headline-stars on the leading stars in a headline
  19577. :todo-keyword on a TODO keyword (including DONE) in a headline
  19578. :tags on the TAGS in a headline
  19579. :priority on the priority cookie in a headline
  19580. :item on the first line of a plain list item
  19581. :item-bullet on the bullet/number of a plain list item
  19582. :checkbox on the checkbox in a plain list item
  19583. :table in an org-mode table
  19584. :table-special on a special filed in a table
  19585. :table-table in a table.el table
  19586. :clocktable in a clocktable
  19587. :src-block in a source block
  19588. :link on a hyperlink
  19589. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19590. :target on a <<target>>
  19591. :radio-target on a <<<radio-target>>>
  19592. :latex-fragment on a LaTeX fragment
  19593. :latex-preview on a LaTeX fragment with overlaid preview image
  19594. This function expects the position to be visible because it uses font-lock
  19595. faces as a help to recognize the following contexts: :table-special, :link,
  19596. and :keyword."
  19597. (let* ((f (get-text-property (point) 'face))
  19598. (faces (if (listp f) f (list f)))
  19599. (case-fold-search t)
  19600. (p (point)) clist o)
  19601. ;; First the large context
  19602. (cond
  19603. ((org-at-heading-p t)
  19604. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19605. (when (progn
  19606. (beginning-of-line 1)
  19607. (looking-at org-todo-line-tags-regexp))
  19608. (push (org-point-in-group p 1 :headline-stars) clist)
  19609. (push (org-point-in-group p 2 :todo-keyword) clist)
  19610. (push (org-point-in-group p 4 :tags) clist))
  19611. (goto-char p)
  19612. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19613. (if (looking-at "\\[#[A-Z0-9]\\]")
  19614. (push (org-point-in-group p 0 :priority) clist)))
  19615. ((org-at-item-p)
  19616. (push (org-point-in-group p 2 :item-bullet) clist)
  19617. (push (list :item (point-at-bol)
  19618. (save-excursion (org-end-of-item) (point)))
  19619. clist)
  19620. (and (org-at-item-checkbox-p)
  19621. (push (org-point-in-group p 0 :checkbox) clist)))
  19622. ((org-at-table-p)
  19623. (push (list :table (org-table-begin) (org-table-end)) clist)
  19624. (if (memq 'org-formula faces)
  19625. (push (list :table-special
  19626. (previous-single-property-change p 'face)
  19627. (next-single-property-change p 'face)) clist)))
  19628. ((org-at-table-p 'any)
  19629. (push (list :table-table) clist)))
  19630. (goto-char p)
  19631. (let ((case-fold-search t))
  19632. ;; New the "medium" contexts: clocktables, source blocks
  19633. (cond ((org-in-clocktable-p)
  19634. (push (list :clocktable
  19635. (and (or (looking-at "#\\+BEGIN: clocktable")
  19636. (search-backward "#+BEGIN: clocktable" nil t))
  19637. (match-beginning 0))
  19638. (and (re-search-forward "#\\+END:?" nil t)
  19639. (match-end 0))) clist))
  19640. ((org-in-src-block-p)
  19641. (push (list :src-block
  19642. (and (or (looking-at "#\\+BEGIN_SRC")
  19643. (search-backward "#+BEGIN_SRC" nil t))
  19644. (match-beginning 0))
  19645. (and (search-forward "#+END_SRC" nil t)
  19646. (match-beginning 0))) clist))))
  19647. (goto-char p)
  19648. ;; Now the small context
  19649. (cond
  19650. ((org-at-timestamp-p)
  19651. (push (org-point-in-group p 0 :timestamp) clist))
  19652. ((memq 'org-link faces)
  19653. (push (list :link
  19654. (previous-single-property-change p 'face)
  19655. (next-single-property-change p 'face)) clist))
  19656. ((memq 'org-special-keyword faces)
  19657. (push (list :keyword
  19658. (previous-single-property-change p 'face)
  19659. (next-single-property-change p 'face)) clist))
  19660. ((org-at-target-p)
  19661. (push (org-point-in-group p 0 :target) clist)
  19662. (goto-char (1- (match-beginning 0)))
  19663. (if (looking-at org-radio-target-regexp)
  19664. (push (org-point-in-group p 0 :radio-target) clist))
  19665. (goto-char p))
  19666. ((setq o (car (delq nil
  19667. (mapcar
  19668. (lambda (x)
  19669. (if (memq x org-latex-fragment-image-overlays) x))
  19670. (overlays-at (point))))))
  19671. (push (list :latex-fragment
  19672. (overlay-start o) (overlay-end o)) clist)
  19673. (push (list :latex-preview
  19674. (overlay-start o) (overlay-end o)) clist))
  19675. ((org-inside-LaTeX-fragment-p)
  19676. ;; FIXME: positions wrong.
  19677. (push (list :latex-fragment (point) (point)) clist)))
  19678. (setq clist (nreverse (delq nil clist)))
  19679. clist))
  19680. ;; FIXME: Compare with at-regexp-p Do we need both?
  19681. (defun org-in-regexp (re &optional nlines visually)
  19682. "Check if point is inside a match of regexp.
  19683. Normally only the current line is checked, but you can include NLINES extra
  19684. lines both before and after point into the search.
  19685. If VISUALLY is set, require that the cursor is not after the match but
  19686. really on, so that the block visually is on the match."
  19687. (catch 'exit
  19688. (let ((pos (point))
  19689. (eol (point-at-eol (+ 1 (or nlines 0))))
  19690. (inc (if visually 1 0)))
  19691. (save-excursion
  19692. (beginning-of-line (- 1 (or nlines 0)))
  19693. (while (re-search-forward re eol t)
  19694. (if (and (<= (match-beginning 0) pos)
  19695. (>= (+ inc (match-end 0)) pos))
  19696. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19697. (defun org-at-regexp-p (regexp)
  19698. "Is point inside a match of REGEXP in the current line?"
  19699. (catch 'exit
  19700. (save-excursion
  19701. (let ((pos (point)) (end (point-at-eol)))
  19702. (beginning-of-line 1)
  19703. (while (re-search-forward regexp end t)
  19704. (if (and (<= (match-beginning 0) pos)
  19705. (>= (match-end 0) pos))
  19706. (throw 'exit t)))
  19707. nil))))
  19708. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19709. "Non-nil when point is between matches of START-RE and END-RE.
  19710. Also return a non-nil value when point is on one of the matches.
  19711. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19712. buffer positions. Default values are the positions of headlines
  19713. surrounding the point.
  19714. The functions returns a cons cell whose car (resp. cdr) is the
  19715. position before START-RE (resp. after END-RE)."
  19716. (save-match-data
  19717. (let ((pos (point))
  19718. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19719. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19720. beg end)
  19721. (save-excursion
  19722. ;; Point is on a block when on START-RE or if START-RE can be
  19723. ;; found before it...
  19724. (and (or (org-at-regexp-p start-re)
  19725. (re-search-backward start-re limit-up t))
  19726. (setq beg (match-beginning 0))
  19727. ;; ... and END-RE after it...
  19728. (goto-char (match-end 0))
  19729. (re-search-forward end-re limit-down t)
  19730. (> (setq end (match-end 0)) pos)
  19731. ;; ... without another START-RE in-between.
  19732. (goto-char (match-beginning 0))
  19733. (not (re-search-backward start-re (1+ beg) t))
  19734. ;; Return value.
  19735. (cons beg end))))))
  19736. (defun org-in-block-p (names)
  19737. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19738. NAMES is a list of strings containing names of blocks.
  19739. Return first block name matched, or nil. Beware that in case of
  19740. nested blocks, the returned name may not belong to the closest
  19741. block from point."
  19742. (save-match-data
  19743. (catch 'exit
  19744. (let ((case-fold-search t)
  19745. (lim-up (save-excursion (outline-previous-heading)))
  19746. (lim-down (save-excursion (outline-next-heading))))
  19747. (mapc (lambda (name)
  19748. (let ((n (regexp-quote name)))
  19749. (when (org-between-regexps-p
  19750. (concat "^[ \t]*#\\+begin_" n)
  19751. (concat "^[ \t]*#\\+end_" n)
  19752. lim-up lim-down)
  19753. (throw 'exit n))))
  19754. names))
  19755. nil)))
  19756. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19757. "Call `multi-occur' with buffers for all agenda files."
  19758. (interactive "sOrg-files matching: \np")
  19759. (let* ((files (org-agenda-files))
  19760. (tnames (mapcar 'file-truename files))
  19761. (extra org-agenda-text-search-extra-files)
  19762. f)
  19763. (when (eq (car extra) 'agenda-archives)
  19764. (setq extra (cdr extra))
  19765. (setq files (org-add-archive-files files)))
  19766. (while (setq f (pop extra))
  19767. (unless (member (file-truename f) tnames)
  19768. (add-to-list 'files f 'append)
  19769. (add-to-list 'tnames (file-truename f) 'append)))
  19770. (multi-occur
  19771. (mapcar (lambda (x)
  19772. (with-current-buffer
  19773. (or (get-file-buffer x) (find-file-noselect x))
  19774. (widen)
  19775. (current-buffer)))
  19776. files)
  19777. regexp)))
  19778. (if (boundp 'occur-mode-find-occurrence-hook)
  19779. ;; Emacs 23
  19780. (add-hook 'occur-mode-find-occurrence-hook
  19781. (lambda ()
  19782. (when (derived-mode-p 'org-mode)
  19783. (org-reveal))))
  19784. ;; Emacs 22
  19785. (defadvice occur-mode-goto-occurrence
  19786. (after org-occur-reveal activate)
  19787. (and (derived-mode-p 'org-mode) (org-reveal)))
  19788. (defadvice occur-mode-goto-occurrence-other-window
  19789. (after org-occur-reveal activate)
  19790. (and (derived-mode-p 'org-mode) (org-reveal)))
  19791. (defadvice occur-mode-display-occurrence
  19792. (after org-occur-reveal activate)
  19793. (when (derived-mode-p 'org-mode)
  19794. (let ((pos (occur-mode-find-occurrence)))
  19795. (with-current-buffer (marker-buffer pos)
  19796. (save-excursion
  19797. (goto-char pos)
  19798. (org-reveal)))))))
  19799. (defun org-occur-link-in-agenda-files ()
  19800. "Create a link and search for it in the agendas.
  19801. The link is not stored in `org-stored-links', it is just created
  19802. for the search purpose."
  19803. (interactive)
  19804. (let ((link (condition-case nil
  19805. (org-store-link nil)
  19806. (error "Unable to create a link to here"))))
  19807. (org-occur-in-agenda-files (regexp-quote link))))
  19808. (defun org-reverse-string (string)
  19809. "Return the reverse of STRING."
  19810. (apply 'string (reverse (string-to-list string))))
  19811. (defun org-uniquify (list)
  19812. "Remove duplicate elements from LIST."
  19813. (let (res)
  19814. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19815. res))
  19816. (defun org-uniquify-alist (alist)
  19817. "Merge duplicate elements of an alist.
  19818. For example, in this alist:
  19819. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19820. => '((a 1 3) (b 2))
  19821. merge (a 1) and (a 3) into (a 1 3) and return the new alist."
  19822. (let (rtn)
  19823. (mapc
  19824. (lambda (e)
  19825. (let (n)
  19826. (if (not (assoc (car e) rtn))
  19827. (push e rtn)
  19828. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19829. (setq rtn (assq-delete-all (car e) rtn))
  19830. (push n rtn))))
  19831. alist)
  19832. rtn))
  19833. (defun org-delete-all (elts list)
  19834. "Remove all elements in ELTS from LIST."
  19835. (while elts
  19836. (setq list (delete (pop elts) list)))
  19837. list)
  19838. (defun org-count (cl-item cl-seq)
  19839. "Count the number of occurrences of ITEM in SEQ.
  19840. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19841. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19842. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19843. (while (< cl-start cl-end)
  19844. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19845. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19846. (setq cl-start (1+ cl-start)))
  19847. cl-count))
  19848. (defun org-remove-if (predicate seq)
  19849. "Remove everything from SEQ that fulfills PREDICATE."
  19850. (let (res e)
  19851. (while seq
  19852. (setq e (pop seq))
  19853. (if (not (funcall predicate e)) (push e res)))
  19854. (nreverse res)))
  19855. (defun org-remove-if-not (predicate seq)
  19856. "Remove everything from SEQ that does not fulfill PREDICATE."
  19857. (let (res e)
  19858. (while seq
  19859. (setq e (pop seq))
  19860. (if (funcall predicate e) (push e res)))
  19861. (nreverse res)))
  19862. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19863. "Reduce two-argument FUNCTION across SEQ.
  19864. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19865. \":initial-value\" removed."
  19866. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19867. (cadr (memq :initial-value cl-keys)))
  19868. (cl-seq (pop cl-seq))
  19869. (t (funcall cl-func)))))
  19870. (while cl-seq
  19871. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19872. cl-accum))
  19873. (defun org-back-over-empty-lines ()
  19874. "Move backwards over whitespace, to the beginning of the first empty line.
  19875. Returns the number of empty lines passed."
  19876. (let ((pos (point)))
  19877. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19878. (skip-chars-backward " \t\n\r")
  19879. (unless (eobp)
  19880. (forward-line -1)))
  19881. (beginning-of-line 2)
  19882. (goto-char (min (point) pos))
  19883. (count-lines (point) pos)))
  19884. (defun org-skip-whitespace ()
  19885. (skip-chars-forward " \t\n\r"))
  19886. (defun org-point-in-group (point group &optional context)
  19887. "Check if POINT is in match-group GROUP.
  19888. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19889. match. If the match group does not exist or point is not inside it,
  19890. return nil."
  19891. (and (match-beginning group)
  19892. (>= point (match-beginning group))
  19893. (<= point (match-end group))
  19894. (if context
  19895. (list context (match-beginning group) (match-end group))
  19896. t)))
  19897. (defun org-switch-to-buffer-other-window (&rest args)
  19898. "Switch to buffer in a second window on the current frame.
  19899. In particular, do not allow pop-up frames.
  19900. Returns the newly created buffer."
  19901. (org-no-popups
  19902. (apply 'switch-to-buffer-other-window args)))
  19903. (defun org-combine-plists (&rest plists)
  19904. "Create a single property list from all plists in PLISTS.
  19905. The process starts by copying the first list, and then setting properties
  19906. from the other lists. Settings in the last list are the most significant
  19907. ones and overrule settings in the other lists."
  19908. (let ((rtn (copy-sequence (pop plists)))
  19909. p v ls)
  19910. (while plists
  19911. (setq ls (pop plists))
  19912. (while ls
  19913. (setq p (pop ls) v (pop ls))
  19914. (setq rtn (plist-put rtn p v))))
  19915. rtn))
  19916. (defun org-replace-escapes (string table)
  19917. "Replace %-escapes in STRING with values in TABLE.
  19918. TABLE is an association list with keys like \"%a\" and string values.
  19919. The sequences in STRING may contain normal field width and padding information,
  19920. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19921. so values can contain further %-escapes if they are define later in TABLE."
  19922. (let ((tbl (copy-alist table))
  19923. (case-fold-search nil)
  19924. (pchg 0)
  19925. e re rpl)
  19926. (while (setq e (pop tbl))
  19927. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19928. (when (and (cdr e) (string-match re (cdr e)))
  19929. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19930. (safe "SREF"))
  19931. (add-text-properties 0 3 (list 'sref sref) safe)
  19932. (setcdr e (replace-match safe t t (cdr e)))))
  19933. (while (string-match re string)
  19934. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19935. (cdr e)))
  19936. (setq string (replace-match rpl t t string))))
  19937. (while (setq pchg (next-property-change pchg string))
  19938. (let ((sref (get-text-property pchg 'sref string)))
  19939. (when (and sref (string-match "SREF" string pchg))
  19940. (setq string (replace-match sref t t string)))))
  19941. string))
  19942. (defun org-sublist (list start end)
  19943. "Return a section of LIST, from START to END.
  19944. Counting starts at 1."
  19945. (let (rtn (c start))
  19946. (setq list (nthcdr (1- start) list))
  19947. (while (and list (<= c end))
  19948. (push (pop list) rtn)
  19949. (setq c (1+ c)))
  19950. (nreverse rtn)))
  19951. (defun org-find-base-buffer-visiting (file)
  19952. "Like `find-buffer-visiting' but always return the base buffer and
  19953. not an indirect buffer."
  19954. (let ((buf (or (get-file-buffer file)
  19955. (find-buffer-visiting file))))
  19956. (if buf
  19957. (or (buffer-base-buffer buf) buf)
  19958. nil)))
  19959. (defun org-image-file-name-regexp (&optional extensions)
  19960. "Return regexp matching the file names of images.
  19961. If EXTENSIONS is given, only match these."
  19962. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19963. (image-file-name-regexp)
  19964. (let ((image-file-name-extensions
  19965. (or extensions
  19966. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19967. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19968. (concat "\\."
  19969. (regexp-opt (nconc (mapcar 'upcase
  19970. image-file-name-extensions)
  19971. image-file-name-extensions)
  19972. t)
  19973. "\\'"))))
  19974. (defun org-file-image-p (file &optional extensions)
  19975. "Return non-nil if FILE is an image."
  19976. (save-match-data
  19977. (string-match (org-image-file-name-regexp extensions) file)))
  19978. (defun org-get-cursor-date (&optional with-time)
  19979. "Return the date at cursor in as a time.
  19980. This works in the calendar and in the agenda, anywhere else it just
  19981. returns the current time.
  19982. If WITH-TIME is non-nil, returns the time of the event at point (in
  19983. the agenda) or the current time of the day."
  19984. (let (date day defd tp tm hod mod)
  19985. (when with-time
  19986. (setq tp (get-text-property (point) 'time))
  19987. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19988. (setq hod (string-to-number (match-string 1 tp))
  19989. mod (string-to-number (match-string 2 tp))))
  19990. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19991. mod (nth 1 (decode-time (current-time))))))
  19992. (cond
  19993. ((eq major-mode 'calendar-mode)
  19994. (setq date (calendar-cursor-to-date)
  19995. defd (encode-time 0 (or mod 0) (or hod 0)
  19996. (nth 1 date) (nth 0 date) (nth 2 date))))
  19997. ((eq major-mode 'org-agenda-mode)
  19998. (setq day (get-text-property (point) 'day))
  19999. (if day
  20000. (setq date (calendar-gregorian-from-absolute day)
  20001. defd (encode-time 0 (or mod 0) (or hod 0)
  20002. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20003. (or defd (current-time))))
  20004. (defun org-mark-subtree (&optional up)
  20005. "Mark the current subtree.
  20006. This puts point at the start of the current subtree, and mark at
  20007. the end. If a numeric prefix UP is given, move up into the
  20008. hierarchy of headlines by UP levels before marking the subtree."
  20009. (interactive "P")
  20010. (org-with-limited-levels
  20011. (cond ((org-at-heading-p) (beginning-of-line))
  20012. ((org-before-first-heading-p) (error "Not in a subtree"))
  20013. (t (outline-previous-visible-heading 1))))
  20014. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20015. (if (org-called-interactively-p 'any)
  20016. (call-interactively 'org-mark-element)
  20017. (org-mark-element)))
  20018. ;;; Indentation
  20019. (defun org-indent-line ()
  20020. "Indent line depending on context."
  20021. (interactive)
  20022. (let* ((pos (point))
  20023. (itemp (org-at-item-p))
  20024. (case-fold-search t)
  20025. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20026. (inline-task-p (and (featurep 'org-inlinetask)
  20027. (org-inlinetask-in-task-p)))
  20028. (inline-re (and inline-task-p
  20029. (org-inlinetask-outline-regexp)))
  20030. column)
  20031. (if (and orgstruct-is-++ (eq pos (point)))
  20032. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20033. (indent-according-to-mode))
  20034. (beginning-of-line 1)
  20035. (cond
  20036. ;; Headings
  20037. ((looking-at org-outline-regexp) (setq column 0))
  20038. ;; Footnote definition
  20039. ((looking-at org-footnote-definition-re) (setq column 0))
  20040. ;; Literal examples
  20041. ((looking-at "[ \t]*:\\( \\|$\\)")
  20042. (setq column (org-get-indentation))) ; do nothing
  20043. ;; Lists
  20044. ((ignore-errors (goto-char (org-in-item-p)))
  20045. (setq column (if itemp
  20046. (org-get-indentation)
  20047. (org-list-item-body-column (point))))
  20048. (goto-char pos))
  20049. ;; Drawers
  20050. ((and (looking-at "[ \t]*:END:")
  20051. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20052. (save-excursion
  20053. (goto-char (1- (match-beginning 1)))
  20054. (setq column (current-column))))
  20055. ;; Special blocks
  20056. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20057. (save-excursion
  20058. (re-search-backward
  20059. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20060. (setq column (org-get-indentation (match-string 0))))
  20061. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20062. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20063. (save-excursion
  20064. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20065. (setq column
  20066. (cond ((equal (downcase (match-string 1)) "src")
  20067. ;; src blocks: let `org-edit-src-exit' handle them
  20068. (org-get-indentation))
  20069. ((equal (downcase (match-string 1)) "example")
  20070. (max (org-get-indentation)
  20071. (org-get-indentation (match-string 0))))
  20072. (t
  20073. (org-get-indentation (match-string 0))))))
  20074. ;; This line has nothing special, look at the previous relevant
  20075. ;; line to compute indentation
  20076. (t
  20077. (beginning-of-line 0)
  20078. (while (and (not (bobp))
  20079. (not (looking-at org-table-line-regexp))
  20080. (not (looking-at org-drawer-regexp))
  20081. ;; When point started in an inline task, do not move
  20082. ;; above task starting line.
  20083. (not (and inline-task-p (looking-at inline-re)))
  20084. ;; Skip drawers, blocks, empty lines, verbatim,
  20085. ;; comments, tables, footnotes definitions, lists,
  20086. ;; inline tasks.
  20087. (or (and (looking-at "[ \t]*:END:")
  20088. (re-search-backward org-drawer-regexp nil t))
  20089. (and (looking-at "[ \t]*#\\+end_")
  20090. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20091. (looking-at "[ \t]*[\n:#|]")
  20092. (looking-at org-footnote-definition-re)
  20093. (and (not inline-task-p)
  20094. (featurep 'org-inlinetask)
  20095. (org-inlinetask-in-task-p)
  20096. (or (org-inlinetask-goto-beginning) t))))
  20097. (beginning-of-line 0))
  20098. (cond
  20099. ;; There was a list item above.
  20100. ((save-excursion
  20101. (and (ignore-errors (goto-char (org-in-item-p)))
  20102. (goto-char
  20103. (org-list-get-top-point (org-list-struct)))))
  20104. (looking-at org-list-full-item-re)
  20105. (setq column (length (match-string 0))))
  20106. ;; There was an heading above.
  20107. ((looking-at "\\*+[ \t]+")
  20108. (if (not org-adapt-indentation)
  20109. (setq column 0)
  20110. (goto-char (match-end 0))
  20111. (setq column (current-column))))
  20112. ;; A drawer had started and is unfinished
  20113. ((looking-at org-drawer-regexp)
  20114. (goto-char (1- (match-beginning 1)))
  20115. (setq column (current-column)))
  20116. ;; Else, nothing noticeable found: get indentation and go on.
  20117. (t (setq column (org-get-indentation))))))
  20118. ;; Now apply indentation and move cursor accordingly
  20119. (goto-char pos)
  20120. (if (<= (current-column) (current-indentation))
  20121. (org-indent-line-to column)
  20122. (save-excursion (org-indent-line-to column)))
  20123. ;; Special polishing for properties, see `org-property-format'
  20124. (setq column (current-column))
  20125. (beginning-of-line 1)
  20126. (if (looking-at
  20127. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20128. (replace-match (concat (match-string 1)
  20129. (format org-property-format
  20130. (match-string 2) (match-string 3)))
  20131. t t))
  20132. (org-move-to-column column))))
  20133. (defun org-indent-drawer ()
  20134. "Indent the drawer at point."
  20135. (interactive)
  20136. (let ((p (point))
  20137. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20138. (match-end 0)))
  20139. (folded
  20140. (save-excursion
  20141. (end-of-line)
  20142. (when (overlays-at (point))
  20143. (member 'invisible (overlay-properties
  20144. (car (overlays-at (point)))))))))
  20145. (when folded (org-cycle))
  20146. (indent-for-tab-command)
  20147. (while (and (move-beginning-of-line 2) (< (point) e))
  20148. (indent-for-tab-command))
  20149. (goto-char p)
  20150. (when folded (org-cycle)))
  20151. (message "Drawer at point indented"))
  20152. (defun org-indent-block ()
  20153. "Indent the block at point."
  20154. (interactive)
  20155. (let ((p (point))
  20156. (case-fold-search t)
  20157. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20158. (match-end 0)))
  20159. (folded
  20160. (save-excursion
  20161. (end-of-line)
  20162. (when (overlays-at (point))
  20163. (member 'invisible (overlay-properties
  20164. (car (overlays-at (point)))))))))
  20165. (when folded (org-cycle))
  20166. (indent-for-tab-command)
  20167. (while (and (move-beginning-of-line 2) (< (point) e))
  20168. (indent-for-tab-command))
  20169. (goto-char p)
  20170. (when folded (org-cycle)))
  20171. (message "Block at point indented"))
  20172. (defun org-indent-region (start end)
  20173. "Indent region."
  20174. (interactive "r")
  20175. (save-excursion
  20176. (let ((line-end (org-current-line end)))
  20177. (goto-char start)
  20178. (while (< (org-current-line) line-end)
  20179. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20180. (t (call-interactively 'org-indent-line)))
  20181. (move-beginning-of-line 2)))))
  20182. ;;; Filling
  20183. ;; We use our own fill-paragraph and auto-fill functions.
  20184. ;; `org-fill-paragraph' relies on adaptive filling and context
  20185. ;; checking. Appropriate `fill-prefix' is computed with
  20186. ;; `org-adaptive-fill-function'.
  20187. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20188. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20189. ;; `org-adaptive-fill-function' value. Internally,
  20190. ;; `org-comment-line-break-function' breaks the line.
  20191. ;; `org-setup-filling' installs filling and auto-filling related
  20192. ;; variables during `org-mode' initialization.
  20193. (defun org-setup-filling ()
  20194. (interactive)
  20195. ;; Prevent auto-fill from inserting unwanted new items.
  20196. (when (boundp 'fill-nobreak-predicate)
  20197. (org-set-local
  20198. 'fill-nobreak-predicate
  20199. (org-uniquify
  20200. (append fill-nobreak-predicate
  20201. '(org-fill-paragraph-separate-nobreak-p
  20202. org-fill-line-break-nobreak-p
  20203. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20204. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20205. (org-set-local 'auto-fill-inhibit-regexp nil)
  20206. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20207. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20208. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20209. (defvar org-element-paragraph-separate) ; org-element.el
  20210. (defun org-fill-paragraph-separate-nobreak-p ()
  20211. "Non-nil when a line break at point would insert a new item."
  20212. (looking-at (substring org-element-paragraph-separate 1)))
  20213. (defun org-fill-line-break-nobreak-p ()
  20214. "Non-nil when a line break at point would create an Org line break."
  20215. (save-excursion
  20216. (skip-chars-backward "[ \t]")
  20217. (skip-chars-backward "\\\\")
  20218. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20219. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20220. "Non-nil when a line break at point would insert a new item."
  20221. (and (org-at-timestamp-p t)
  20222. (not (looking-at org-ts-regexp-both))))
  20223. (declare-function message-in-body-p "message" ())
  20224. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20225. (defun org-adaptive-fill-function ()
  20226. "Compute a fill prefix for the current line.
  20227. Return fill prefix, as a string, or nil if current line isn't
  20228. meant to be filled."
  20229. (let (prefix)
  20230. (catch 'exit
  20231. (when (derived-mode-p 'message-mode)
  20232. (save-excursion
  20233. (beginning-of-line)
  20234. (cond ((or (not (message-in-body-p))
  20235. (looking-at orgtbl-line-start-regexp))
  20236. (throw 'exit nil))
  20237. ((looking-at message-cite-prefix-regexp)
  20238. (throw 'exit (match-string-no-properties 0)))
  20239. ((looking-at org-outline-regexp)
  20240. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20241. (org-with-wide-buffer
  20242. (let* ((p (line-beginning-position))
  20243. (element (save-excursion (beginning-of-line)
  20244. (org-element-at-point)))
  20245. (type (org-element-type element))
  20246. (post-affiliated (org-element-property :post-affiliated element)))
  20247. (unless (and post-affiliated (< p post-affiliated))
  20248. (case type
  20249. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20250. (footnote-definition "")
  20251. ((item plain-list)
  20252. (make-string (org-list-item-body-column
  20253. (or post-affiliated
  20254. (org-element-property :begin element)))
  20255. ? ))
  20256. (paragraph
  20257. ;; Fill prefix is usually the same as the current line,
  20258. ;; except if the paragraph is at the beginning of an item.
  20259. (let ((parent (org-element-property :parent element)))
  20260. (cond ((eq (org-element-type parent) 'item)
  20261. (make-string (org-list-item-body-column
  20262. (org-element-property :begin parent))
  20263. ? ))
  20264. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20265. (match-string 0))
  20266. (t ""))))
  20267. (comment-block
  20268. ;; Only fill contents if P is within block boundaries.
  20269. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20270. (forward-line)
  20271. (point)))
  20272. (cend (save-excursion
  20273. (goto-char (org-element-property :end element))
  20274. (skip-chars-backward " \r\t\n")
  20275. (line-beginning-position))))
  20276. (when (and (>= p cbeg) (< p cend))
  20277. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20278. (match-string 0)
  20279. "")))))))))))
  20280. (declare-function message-goto-body "message" ())
  20281. (defvar message-cite-prefix-regexp) ; From message.el
  20282. (defun org-fill-paragraph (&optional justify)
  20283. "Fill element at point, when applicable.
  20284. This function only applies to comment blocks, comments, example
  20285. blocks and paragraphs. Also, as a special case, re-align table
  20286. when point is at one.
  20287. If JUSTIFY is non-nil (interactively, with prefix argument),
  20288. justify as well. If `sentence-end-double-space' is non-nil, then
  20289. period followed by one space does not end a sentence, so don't
  20290. break a line there. The variable `fill-column' controls the
  20291. width for filling.
  20292. For convenience, when point is at a plain list, an item or
  20293. a footnote definition, try to fill the first paragraph within."
  20294. (interactive)
  20295. (if (and (derived-mode-p 'message-mode)
  20296. (or (not (message-in-body-p))
  20297. (save-excursion (move-beginning-of-line 1)
  20298. (looking-at message-cite-prefix-regexp))))
  20299. ;; First ensure filling is correct in message-mode.
  20300. (let ((fill-paragraph-function
  20301. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20302. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20303. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20304. (paragraph-separate
  20305. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20306. (fill-paragraph nil))
  20307. (with-syntax-table org-mode-transpose-word-syntax-table
  20308. ;; Move to end of line in order to get the first paragraph
  20309. ;; within a plain list or a footnote definition.
  20310. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20311. ;; First check if point is in a blank line at the beginning of
  20312. ;; the buffer. In that case, ignore filling.
  20313. (case (org-element-type element)
  20314. ;; Use major mode filling function is src blocks.
  20315. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20316. ;; Align Org tables, leave table.el tables as-is.
  20317. (table-row (org-table-align) t)
  20318. (table
  20319. (when (eq (org-element-property :type element) 'org)
  20320. (org-table-align))
  20321. t)
  20322. (paragraph
  20323. ;; Paragraphs may contain `line-break' type objects.
  20324. (let ((beg (max (point-min)
  20325. (org-element-property :contents-begin element)))
  20326. (end (min (point-max)
  20327. (org-element-property :contents-end element))))
  20328. ;; Do nothing if point is at an affiliated keyword.
  20329. (if (< (line-end-position) beg) t
  20330. (when (derived-mode-p 'message-mode)
  20331. ;; In `message-mode', do not fill following citation
  20332. ;; in current paragraph nor text before message body.
  20333. (let ((body-start (save-excursion (message-goto-body))))
  20334. (when body-start (setq beg (max body-start beg))))
  20335. (when (save-excursion
  20336. (re-search-forward
  20337. (concat "^" message-cite-prefix-regexp) end t))
  20338. (setq end (match-beginning 0))))
  20339. ;; Fill paragraph, taking line breaks into account.
  20340. ;; For that, slice the paragraph using line breaks as
  20341. ;; separators, and fill the parts in reverse order to
  20342. ;; avoid messing with markers.
  20343. (save-excursion
  20344. (goto-char end)
  20345. (mapc
  20346. (lambda (pos)
  20347. (fill-region-as-paragraph pos (point) justify)
  20348. (goto-char pos))
  20349. ;; Find the list of ending positions for line breaks
  20350. ;; in the current paragraph. Add paragraph
  20351. ;; beginning to include first slice.
  20352. (nreverse
  20353. (cons beg
  20354. (org-element-map
  20355. (org-element--parse-objects
  20356. beg end nil (org-element-restriction 'paragraph))
  20357. 'line-break
  20358. (lambda (lb) (org-element-property :end lb)))))))
  20359. t)))
  20360. ;; Contents of `comment-block' type elements should be
  20361. ;; filled as plain text, but only if point is within block
  20362. ;; markers.
  20363. (comment-block
  20364. (let* ((case-fold-search t)
  20365. (beg (save-excursion
  20366. (goto-char (org-element-property :begin element))
  20367. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20368. (forward-line)
  20369. (point)))
  20370. (end (save-excursion
  20371. (goto-char (org-element-property :end element))
  20372. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20373. (line-beginning-position))))
  20374. (when (and (>= (point) beg) (< (point) end))
  20375. (fill-region-as-paragraph
  20376. (save-excursion
  20377. (end-of-line)
  20378. (re-search-backward "^[ \t]*$" beg 'move)
  20379. (line-beginning-position))
  20380. (save-excursion
  20381. (beginning-of-line)
  20382. (re-search-forward "^[ \t]*$" end 'move)
  20383. (line-beginning-position))
  20384. justify)))
  20385. t)
  20386. ;; Fill comments.
  20387. (comment (fill-comment-paragraph justify))
  20388. ;; Ignore every other element.
  20389. (otherwise t))))))
  20390. (defun org-auto-fill-function ()
  20391. "Auto-fill function."
  20392. ;; Check if auto-filling is meaningful.
  20393. (let ((fc (current-fill-column)))
  20394. (when (and fc (> (current-column) fc))
  20395. (let* ((fill-prefix (org-adaptive-fill-function))
  20396. ;; Enforce empty fill prefix, if required. Otherwise, it
  20397. ;; will be computed again.
  20398. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20399. (when fill-prefix (do-auto-fill))))))
  20400. (defun org-comment-line-break-function (&optional soft)
  20401. "Break line at point and indent, continuing comment if within one.
  20402. The inserted newline is marked hard if variable
  20403. `use-hard-newlines' is true, unless optional argument SOFT is
  20404. non-nil."
  20405. (if soft (insert-and-inherit ?\n) (newline 1))
  20406. (save-excursion (forward-char -1) (delete-horizontal-space))
  20407. (delete-horizontal-space)
  20408. (indent-to-left-margin)
  20409. (insert-before-markers-and-inherit fill-prefix))
  20410. ;;; Comments
  20411. ;; Org comments syntax is quite complex. It requires the entire line
  20412. ;; to be just a comment. Also, even with the right syntax at the
  20413. ;; beginning of line, some some elements (i.e. verse-block or
  20414. ;; example-block) don't accept comments. Usual Emacs comment commands
  20415. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20416. ;; Org still relies on `comment-dwim', but cannot trust
  20417. ;; `comment-only-p'. So, `comment-region-function' and
  20418. ;; `uncomment-region-function' both point
  20419. ;; to`org-comment-or-uncomment-region'. Eventually,
  20420. ;; `org-insert-comment' takes care of insertion of comments at the
  20421. ;; beginning of line.
  20422. ;; `org-setup-comments-handling' install comments related variables
  20423. ;; during `org-mode' initialization.
  20424. (defun org-setup-comments-handling ()
  20425. (interactive)
  20426. (org-set-local 'comment-use-syntax nil)
  20427. (org-set-local 'comment-start "# ")
  20428. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20429. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20430. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20431. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20432. (defun org-insert-comment ()
  20433. "Insert an empty comment above current line.
  20434. If the line is empty, insert comment at its beginning."
  20435. (beginning-of-line)
  20436. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20437. (org-indent-line)
  20438. (insert "# "))
  20439. (defvar comment-empty-lines) ; From newcomment.el.
  20440. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20441. "Comment or uncomment each non-blank line in the region.
  20442. Uncomment each non-blank line between BEG and END if it only
  20443. contains commented lines. Otherwise, comment them."
  20444. (save-restriction
  20445. ;; Restrict region
  20446. (narrow-to-region (save-excursion (goto-char beg)
  20447. (skip-chars-forward " \r\t\n" end)
  20448. (line-beginning-position))
  20449. (save-excursion (goto-char end)
  20450. (skip-chars-backward " \r\t\n" beg)
  20451. (line-end-position)))
  20452. (let ((uncommentp
  20453. ;; UNCOMMENTP is non-nil when every non blank line between
  20454. ;; BEG and END is a comment.
  20455. (save-excursion
  20456. (goto-char (point-min))
  20457. (while (and (not (eobp))
  20458. (let ((element (org-element-at-point)))
  20459. (and (eq (org-element-type element) 'comment)
  20460. (goto-char (min (point-max)
  20461. (org-element-property
  20462. :end element)))))))
  20463. (eobp))))
  20464. (if uncommentp
  20465. ;; Only blank lines and comments in region: uncomment it.
  20466. (save-excursion
  20467. (goto-char (point-min))
  20468. (while (not (eobp))
  20469. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20470. (replace-match "" nil nil nil 1))
  20471. (forward-line)))
  20472. ;; Comment each line in region.
  20473. (let ((min-indent (point-max)))
  20474. ;; First find the minimum indentation across all lines.
  20475. (save-excursion
  20476. (goto-char (point-min))
  20477. (while (and (not (eobp)) (not (zerop min-indent)))
  20478. (unless (looking-at "[ \t]*$")
  20479. (setq min-indent (min min-indent (current-indentation))))
  20480. (forward-line)))
  20481. ;; Then loop over all lines.
  20482. (save-excursion
  20483. (goto-char (point-min))
  20484. (while (not (eobp))
  20485. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20486. ;; Don't get fooled by invisible text (e.g. link path)
  20487. ;; when moving to column MIN-INDENT.
  20488. (let ((buffer-invisibility-spec nil))
  20489. (org-move-to-column min-indent t))
  20490. (insert comment-start))
  20491. (forward-line))))))))
  20492. ;;; Planning
  20493. ;; This section contains tools to operate on timestamp objects, as
  20494. ;; returned by, e.g. `org-element-context'.
  20495. (defun org-timestamp-has-time-p (timestamp)
  20496. "Non-nil when TIMESTAMP has a time specified."
  20497. (org-element-property :hour-start timestamp))
  20498. (defun org-timestamp-format (timestamp format &optional end utc)
  20499. "Format a TIMESTAMP element into a string.
  20500. FORMAT is a format specifier to be passed to
  20501. `format-time-string'.
  20502. When optional argument END is non-nil, use end of date-range or
  20503. time-range, if possible.
  20504. When optional argument UTC is non-nil, time will be expressed as
  20505. Universal Time."
  20506. (format-time-string
  20507. format
  20508. (apply 'encode-time
  20509. (cons 0
  20510. (mapcar
  20511. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20512. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20513. '(:minute-start :hour-start :day-start :month-start
  20514. :year-start)))))
  20515. utc))
  20516. (defun org-timestamp-split-range (timestamp &optional end)
  20517. "Extract a timestamp object from a date or time range.
  20518. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20519. the end of the range. Otherwise, extract its start.
  20520. Return a new timestamp object sharing the same parent as
  20521. TIMESTAMP."
  20522. (let ((type (org-element-property :type timestamp)))
  20523. (if (memq type '(active inactive diary)) timestamp
  20524. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20525. ;; Set new type.
  20526. (org-element-put-property
  20527. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20528. ;; Copy start properties over end properties if END is
  20529. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20530. (let ((p-alist '((:minute-start . :minute-end)
  20531. (:hour-start . :hour-end)
  20532. (:day-start . :day-end)
  20533. (:month-start . :month-end)
  20534. (:year-start . :year-end))))
  20535. (dolist (p-cell p-alist)
  20536. (org-element-put-property
  20537. split-ts
  20538. (funcall (if end 'car 'cdr) p-cell)
  20539. (org-element-property
  20540. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20541. ;; Eventually refresh `:raw-value'.
  20542. (org-element-put-property split-ts :raw-value nil)
  20543. (org-element-put-property
  20544. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20545. (defun org-timestamp-translate (timestamp &optional boundary)
  20546. "Apply `org-translate-time' on a TIMESTAMP object.
  20547. When optional argument BOUNDARY is non-nil, it is either the
  20548. symbol `start' or `end'. In this case, only translate the
  20549. starting or ending part of TIMESTAMP if it is a date or time
  20550. range. Otherwise, translate both parts."
  20551. (if (and (not boundary)
  20552. (memq (org-element-property :type timestamp)
  20553. '(active-range inactive-range)))
  20554. (concat
  20555. (org-translate-time
  20556. (org-element-property :raw-value
  20557. (org-timestamp-split-range timestamp)))
  20558. "--"
  20559. (org-translate-time
  20560. (org-element-property :raw-value
  20561. (org-timestamp-split-range timestamp t))))
  20562. (org-translate-time
  20563. (org-element-property
  20564. :raw-value
  20565. (if (not boundary) timestamp
  20566. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20567. ;;; Other stuff.
  20568. (defun org-toggle-fixed-width-section (arg)
  20569. "Toggle the fixed-width export.
  20570. If there is no active region, the QUOTE keyword at the current headline is
  20571. inserted or removed. When present, it causes the text between this headline
  20572. and the next to be exported as fixed-width text, and unmodified.
  20573. If there is an active region, this command adds or removes a colon as the
  20574. first character of this line. If the first character of a line is a colon,
  20575. this line is also exported in fixed-width font."
  20576. (interactive "P")
  20577. (let* ((cc 0)
  20578. (regionp (org-region-active-p))
  20579. (beg (if regionp (region-beginning) (point)))
  20580. (end (if regionp (region-end)))
  20581. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20582. (case-fold-search nil)
  20583. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20584. off)
  20585. (if regionp
  20586. (save-excursion
  20587. (goto-char beg)
  20588. (setq cc (current-column))
  20589. (beginning-of-line 1)
  20590. (setq off (looking-at re))
  20591. (while (> nlines 0)
  20592. (setq nlines (1- nlines))
  20593. (beginning-of-line 1)
  20594. (cond
  20595. (arg
  20596. (org-move-to-column cc t)
  20597. (insert ": \n")
  20598. (forward-line -1))
  20599. ((and off (looking-at re))
  20600. (replace-match "" t t nil 1))
  20601. ((not off) (org-move-to-column cc t) (insert ": ")))
  20602. (forward-line 1)))
  20603. (save-excursion
  20604. (org-back-to-heading)
  20605. (cond
  20606. ((looking-at (format org-heading-keyword-regexp-format
  20607. org-quote-string))
  20608. (goto-char (match-end 1))
  20609. (looking-at (concat " +" org-quote-string))
  20610. (replace-match "" t t)
  20611. (when (eolp) (insert " ")))
  20612. ((looking-at org-outline-regexp)
  20613. (goto-char (match-end 0))
  20614. (insert org-quote-string " ")))))))
  20615. (defun org-reftex-citation ()
  20616. "Use reftex-citation to insert a citation into the buffer.
  20617. This looks for a line like
  20618. #+BIBLIOGRAPHY: foo plain option:-d
  20619. and derives from it that foo.bib is the bibliography file relevant
  20620. for this document. It then installs the necessary environment for RefTeX
  20621. to work in this buffer and calls `reftex-citation' to insert a citation
  20622. into the buffer.
  20623. Export of such citations to both LaTeX and HTML is handled by the contributed
  20624. package org-exp-bibtex by Taru Karttunen."
  20625. (interactive)
  20626. (let ((reftex-docstruct-symbol 'rds)
  20627. (reftex-cite-format "\\cite{%l}")
  20628. rds bib)
  20629. (save-excursion
  20630. (save-restriction
  20631. (widen)
  20632. (let ((case-fold-search t)
  20633. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20634. (if (not (save-excursion
  20635. (or (re-search-forward re nil t)
  20636. (re-search-backward re nil t))))
  20637. (error "No bibliography defined in file")
  20638. (setq bib (concat (match-string 1) ".bib")
  20639. rds (list (list 'bib bib)))))))
  20640. (call-interactively 'reftex-citation)))
  20641. ;;;; Functions extending outline functionality
  20642. (defun org-beginning-of-line (&optional arg)
  20643. "Go to the beginning of the current line. If that is invisible, continue
  20644. to a visible line beginning. This makes the function of C-a more intuitive.
  20645. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20646. first attempt, and only move to after the tags when the cursor is already
  20647. beyond the end of the headline."
  20648. (interactive "P")
  20649. (let ((pos (point))
  20650. (special (if (consp org-special-ctrl-a/e)
  20651. (car org-special-ctrl-a/e)
  20652. org-special-ctrl-a/e))
  20653. refpos)
  20654. (if (org-bound-and-true-p visual-line-mode)
  20655. (beginning-of-visual-line 1)
  20656. (beginning-of-line 1))
  20657. (if (and arg (fboundp 'move-beginning-of-line))
  20658. (call-interactively 'move-beginning-of-line)
  20659. (if (bobp)
  20660. nil
  20661. (backward-char 1)
  20662. (if (org-truely-invisible-p)
  20663. (while (and (not (bobp)) (org-truely-invisible-p))
  20664. (backward-char 1)
  20665. (beginning-of-line 1))
  20666. (forward-char 1))))
  20667. (when special
  20668. (cond
  20669. ((and (looking-at org-complex-heading-regexp)
  20670. (= (char-after (match-end 1)) ?\ ))
  20671. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20672. (point-at-eol)))
  20673. (goto-char
  20674. (if (eq special t)
  20675. (cond ((> pos refpos) refpos)
  20676. ((= pos (point)) refpos)
  20677. (t (point)))
  20678. (cond ((> pos (point)) (point))
  20679. ((not (eq last-command this-command)) (point))
  20680. (t refpos)))))
  20681. ((org-at-item-p)
  20682. ;; Being at an item and not looking at an the item means point
  20683. ;; was previously moved to beginning of a visual line, which
  20684. ;; doesn't contain the item. Therefore, do nothing special,
  20685. ;; just stay here.
  20686. (when (looking-at org-list-full-item-re)
  20687. ;; Set special position at first white space character after
  20688. ;; bullet, and check-box, if any.
  20689. (let ((after-bullet
  20690. (let ((box (match-end 3)))
  20691. (if (not box) (match-end 1)
  20692. (let ((after (char-after box)))
  20693. (if (and after (= after ? )) (1+ box) box))))))
  20694. ;; Special case: Move point to special position when
  20695. ;; currently after it or at beginning of line.
  20696. (if (eq special t)
  20697. (when (or (> pos after-bullet) (= (point) pos))
  20698. (goto-char after-bullet))
  20699. ;; Reversed case: Move point to special position when
  20700. ;; point was already at beginning of line and command is
  20701. ;; repeated.
  20702. (when (and (= (point) pos) (eq last-command this-command))
  20703. (goto-char after-bullet))))))))
  20704. (org-no-warnings
  20705. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20706. (defun org-end-of-line (&optional arg)
  20707. "Go to the end of the line.
  20708. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20709. tags on the first attempt, and only move to after the tags when
  20710. the cursor is already beyond the end of the headline."
  20711. (interactive "P")
  20712. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20713. org-special-ctrl-a/e))
  20714. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20715. 'end-of-visual-line)
  20716. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20717. (t 'end-of-line))))
  20718. (if (or (not special) arg) (call-interactively move-fun)
  20719. (let* ((element (save-excursion (beginning-of-line)
  20720. (org-element-at-point)))
  20721. (type (org-element-type element)))
  20722. (cond
  20723. ((memq type '(headline inlinetask))
  20724. (let ((pos (point)))
  20725. (beginning-of-line 1)
  20726. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20727. (if (eq special t)
  20728. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20729. (goto-char (match-beginning 1))
  20730. (goto-char (match-end 0)))
  20731. (if (or (< pos (match-end 0))
  20732. (not (eq this-command last-command)))
  20733. (goto-char (match-end 0))
  20734. (goto-char (match-beginning 1))))
  20735. (call-interactively move-fun))))
  20736. ((org-element-property :hiddenp element)
  20737. ;; If element is hidden, `move-end-of-line' would put point
  20738. ;; after it. Use `end-of-line' to stay on current line.
  20739. (call-interactively 'end-of-line))
  20740. (t (call-interactively move-fun)))))
  20741. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20742. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20743. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20744. (defun org-backward-sentence (&optional arg)
  20745. "Go to beginning of sentence, or beginning of table field.
  20746. This will call `backward-sentence' or `org-table-beginning-of-field',
  20747. depending on context."
  20748. (interactive "P")
  20749. (cond
  20750. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20751. (t (call-interactively 'backward-sentence))))
  20752. (defun org-forward-sentence (&optional arg)
  20753. "Go to end of sentence, or end of table field.
  20754. This will call `forward-sentence' or `org-table-end-of-field',
  20755. depending on context."
  20756. (interactive "P")
  20757. (cond
  20758. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20759. (t (call-interactively 'forward-sentence))))
  20760. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20761. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20762. (defun org-kill-line (&optional arg)
  20763. "Kill line, to tags or end of line."
  20764. (interactive "P")
  20765. (cond
  20766. ((or (not org-special-ctrl-k)
  20767. (bolp)
  20768. (not (org-at-heading-p)))
  20769. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20770. org-ctrl-k-protect-subtree)
  20771. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20772. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20773. (error "C-k aborted - would kill hidden subtree")))
  20774. (call-interactively
  20775. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20776. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20777. (kill-region (point) (match-beginning 1))
  20778. (org-set-tags nil t))
  20779. (t (kill-region (point) (point-at-eol)))))
  20780. (define-key org-mode-map "\C-k" 'org-kill-line)
  20781. (defun org-yank (&optional arg)
  20782. "Yank. If the kill is a subtree, treat it specially.
  20783. This command will look at the current kill and check if is a single
  20784. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20785. cursor is at the beginning of a line or after the stars of a currently
  20786. empty headline, then the yank is handled specially. How exactly depends
  20787. on the value of the following variables, both set by default.
  20788. org-yank-folded-subtrees
  20789. When set, the subtree(s) will be folded after insertion, but only
  20790. if doing so would now swallow text after the yanked text.
  20791. org-yank-adjusted-subtrees
  20792. When set, the subtree will be promoted or demoted in order to
  20793. fit into the local outline tree structure, which means that the level
  20794. will be adjusted so that it becomes the smaller one of the two
  20795. *visible* surrounding headings.
  20796. Any prefix to this command will cause `yank' to be called directly with
  20797. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20798. will just
  20799. plainly yank the text as it is.
  20800. \[1] The test checks if the first non-white line is a heading
  20801. and if there are no other headings with fewer stars."
  20802. (interactive "P")
  20803. (org-yank-generic 'yank arg))
  20804. (defun org-yank-generic (command arg)
  20805. "Perform some yank-like command.
  20806. This function implements the behavior described in the `org-yank'
  20807. documentation. However, it has been generalized to work for any
  20808. interactive command with similar behavior."
  20809. ;; pretend to be command COMMAND
  20810. (setq this-command command)
  20811. (if arg
  20812. (call-interactively command)
  20813. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20814. (and (org-kill-is-subtree-p)
  20815. (or (bolp)
  20816. (and (looking-at "[ \t]*$")
  20817. (string-match
  20818. "\\`\\*+\\'"
  20819. (buffer-substring (point-at-bol) (point)))))))
  20820. swallowp)
  20821. (cond
  20822. ((and subtreep org-yank-folded-subtrees)
  20823. (let ((beg (point))
  20824. end)
  20825. (if (and subtreep org-yank-adjusted-subtrees)
  20826. (org-paste-subtree nil nil 'for-yank)
  20827. (call-interactively command))
  20828. (setq end (point))
  20829. (goto-char beg)
  20830. (when (and (bolp) subtreep
  20831. (not (setq swallowp
  20832. (org-yank-folding-would-swallow-text beg end))))
  20833. (org-with-limited-levels
  20834. (or (looking-at org-outline-regexp)
  20835. (re-search-forward org-outline-regexp-bol end t))
  20836. (while (and (< (point) end) (looking-at org-outline-regexp))
  20837. (hide-subtree)
  20838. (org-cycle-show-empty-lines 'folded)
  20839. (condition-case nil
  20840. (outline-forward-same-level 1)
  20841. (error (goto-char end))))))
  20842. (when swallowp
  20843. (message
  20844. "Inserted text not folded because that would swallow text"))
  20845. (goto-char end)
  20846. (skip-chars-forward " \t\n\r")
  20847. (beginning-of-line 1)
  20848. (push-mark beg 'nomsg)))
  20849. ((and subtreep org-yank-adjusted-subtrees)
  20850. (let ((beg (point-at-bol)))
  20851. (org-paste-subtree nil nil 'for-yank)
  20852. (push-mark beg 'nomsg)))
  20853. (t
  20854. (call-interactively command))))))
  20855. (defun org-yank-folding-would-swallow-text (beg end)
  20856. "Would hide-subtree at BEG swallow any text after END?"
  20857. (let (level)
  20858. (org-with-limited-levels
  20859. (save-excursion
  20860. (goto-char beg)
  20861. (when (or (looking-at org-outline-regexp)
  20862. (re-search-forward org-outline-regexp-bol end t))
  20863. (setq level (org-outline-level)))
  20864. (goto-char end)
  20865. (skip-chars-forward " \t\r\n\v\f")
  20866. (if (or (eobp)
  20867. (and (bolp) (looking-at org-outline-regexp)
  20868. (<= (org-outline-level) level)))
  20869. nil ; Nothing would be swallowed
  20870. t))))) ; something would swallow
  20871. (define-key org-mode-map "\C-y" 'org-yank)
  20872. (defun org-truely-invisible-p ()
  20873. "Check if point is at a character currently not visible.
  20874. This version does not only check the character property, but also
  20875. `visible-mode'."
  20876. ;; Early versions of noutline don't have `outline-invisible-p'.
  20877. (if (org-bound-and-true-p visible-mode)
  20878. nil
  20879. (outline-invisible-p)))
  20880. (defun org-invisible-p2 ()
  20881. "Check if point is at a character currently not visible."
  20882. (save-excursion
  20883. (if (and (eolp) (not (bobp))) (backward-char 1))
  20884. ;; Early versions of noutline don't have `outline-invisible-p'.
  20885. (outline-invisible-p)))
  20886. (defun org-back-to-heading (&optional invisible-ok)
  20887. "Call `outline-back-to-heading', but provide a better error message."
  20888. (condition-case nil
  20889. (outline-back-to-heading invisible-ok)
  20890. (error (error "Before first headline at position %d in buffer %s"
  20891. (point) (current-buffer)))))
  20892. (defun org-before-first-heading-p ()
  20893. "Before first heading?"
  20894. (save-excursion
  20895. (end-of-line)
  20896. (null (re-search-backward org-outline-regexp-bol nil t))))
  20897. (defun org-at-heading-p (&optional ignored)
  20898. (outline-on-heading-p t))
  20899. ;; Compatibility alias with Org versions < 7.8.03
  20900. (defalias 'org-on-heading-p 'org-at-heading-p)
  20901. (defun org-at-comment-p nil
  20902. "Is cursor in a line starting with a # character?"
  20903. (save-excursion
  20904. (beginning-of-line)
  20905. (looking-at "^#")))
  20906. (defun org-at-drawer-p nil
  20907. "Is cursor at a drawer keyword?"
  20908. (save-excursion
  20909. (move-beginning-of-line 1)
  20910. (looking-at org-drawer-regexp)))
  20911. (defun org-at-block-p nil
  20912. "Is cursor at a block keyword?"
  20913. (save-excursion
  20914. (move-beginning-of-line 1)
  20915. (looking-at org-block-regexp)))
  20916. (defun org-point-at-end-of-empty-headline ()
  20917. "If point is at the end of an empty headline, return t, else nil.
  20918. If the heading only contains a TODO keyword, it is still still considered
  20919. empty."
  20920. (and (looking-at "[ \t]*$")
  20921. (when org-todo-line-regexp
  20922. (save-excursion
  20923. (beginning-of-line 1)
  20924. (let ((case-fold-search nil))
  20925. (looking-at org-todo-line-regexp)
  20926. (string= (match-string 3) ""))))))
  20927. (defun org-at-heading-or-item-p ()
  20928. (or (org-at-heading-p) (org-at-item-p)))
  20929. (defun org-at-target-p ()
  20930. (or (org-in-regexp org-radio-target-regexp)
  20931. (org-in-regexp org-target-regexp)))
  20932. ;; Compatibility alias with Org versions < 7.8.03
  20933. (defalias 'org-on-target-p 'org-at-target-p)
  20934. (defun org-up-heading-all (arg)
  20935. "Move to the heading line of which the present line is a subheading.
  20936. This function considers both visible and invisible heading lines.
  20937. With argument, move up ARG levels."
  20938. (if (fboundp 'outline-up-heading-all)
  20939. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20940. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20941. (defun org-up-heading-safe ()
  20942. "Move to the heading line of which the present line is a subheading.
  20943. This version will not throw an error. It will return the level of the
  20944. headline found, or nil if no higher level is found.
  20945. Also, this function will be a lot faster than `outline-up-heading',
  20946. because it relies on stars being the outline starters. This can really
  20947. make a significant difference in outlines with very many siblings."
  20948. (let (start-level re)
  20949. (org-back-to-heading t)
  20950. (setq start-level (funcall outline-level))
  20951. (if (equal start-level 1)
  20952. nil
  20953. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20954. (if (re-search-backward re nil t)
  20955. (funcall outline-level)))))
  20956. (defun org-first-sibling-p ()
  20957. "Is this heading the first child of its parents?"
  20958. (interactive)
  20959. (let ((re org-outline-regexp-bol)
  20960. level l)
  20961. (unless (org-at-heading-p t)
  20962. (error "Not at a heading"))
  20963. (setq level (funcall outline-level))
  20964. (save-excursion
  20965. (if (not (re-search-backward re nil t))
  20966. t
  20967. (setq l (funcall outline-level))
  20968. (< l level)))))
  20969. (defun org-goto-sibling (&optional previous)
  20970. "Goto the next sibling, even if it is invisible.
  20971. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20972. when a sibling was found. When none is found, return nil and don't
  20973. move point."
  20974. (let ((fun (if previous 're-search-backward 're-search-forward))
  20975. (pos (point))
  20976. (re org-outline-regexp-bol)
  20977. level l)
  20978. (when (condition-case nil (org-back-to-heading t) (error nil))
  20979. (setq level (funcall outline-level))
  20980. (catch 'exit
  20981. (or previous (forward-char 1))
  20982. (while (funcall fun re nil t)
  20983. (setq l (funcall outline-level))
  20984. (when (< l level) (goto-char pos) (throw 'exit nil))
  20985. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20986. (goto-char pos)
  20987. nil))))
  20988. (defun org-show-siblings ()
  20989. "Show all siblings of the current headline."
  20990. (save-excursion
  20991. (while (org-goto-sibling) (org-flag-heading nil)))
  20992. (save-excursion
  20993. (while (org-goto-sibling 'previous)
  20994. (org-flag-heading nil))))
  20995. (defun org-goto-first-child ()
  20996. "Goto the first child, even if it is invisible.
  20997. Return t when a child was found. Otherwise don't move point and
  20998. return nil."
  20999. (let (level (pos (point)) (re org-outline-regexp-bol))
  21000. (when (condition-case nil (org-back-to-heading t) (error nil))
  21001. (setq level (outline-level))
  21002. (forward-char 1)
  21003. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21004. (progn (goto-char (match-beginning 0)) t)
  21005. (goto-char pos) nil))))
  21006. (defun org-show-hidden-entry ()
  21007. "Show an entry where even the heading is hidden."
  21008. (save-excursion
  21009. (org-show-entry)))
  21010. (defun org-flag-heading (flag &optional entry)
  21011. "Flag the current heading. FLAG non-nil means make invisible.
  21012. When ENTRY is non-nil, show the entire entry."
  21013. (save-excursion
  21014. (org-back-to-heading t)
  21015. ;; Check if we should show the entire entry
  21016. (if entry
  21017. (progn
  21018. (org-show-entry)
  21019. (save-excursion
  21020. (and (outline-next-heading)
  21021. (org-flag-heading nil))))
  21022. (outline-flag-region (max (point-min) (1- (point)))
  21023. (save-excursion (outline-end-of-heading) (point))
  21024. flag))))
  21025. (defun org-get-next-sibling ()
  21026. "Move to next heading of the same level, and return point.
  21027. If there is no such heading, return nil.
  21028. This is like outline-next-sibling, but invisible headings are ok."
  21029. (let ((level (funcall outline-level)))
  21030. (outline-next-heading)
  21031. (while (and (not (eobp)) (> (funcall outline-level) level))
  21032. (outline-next-heading))
  21033. (if (or (eobp) (< (funcall outline-level) level))
  21034. nil
  21035. (point))))
  21036. (defun org-get-last-sibling ()
  21037. "Move to previous heading of the same level, and return point.
  21038. If there is no such heading, return nil."
  21039. (let ((opoint (point))
  21040. (level (funcall outline-level)))
  21041. (outline-previous-heading)
  21042. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21043. (while (and (> (funcall outline-level) level)
  21044. (not (bobp)))
  21045. (outline-previous-heading))
  21046. (if (< (funcall outline-level) level)
  21047. nil
  21048. (point)))))
  21049. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21050. "Goto to the end of a subtree."
  21051. ;; This contains an exact copy of the original function, but it uses
  21052. ;; `org-back-to-heading', to make it work also in invisible
  21053. ;; trees. And is uses an invisible-ok argument.
  21054. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21055. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21056. ;; with many children and grandchildren etc, this can be much faster
  21057. ;; than the outline version.
  21058. (org-back-to-heading invisible-ok)
  21059. (let ((first t)
  21060. (level (funcall outline-level)))
  21061. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21062. ;; A true heading (not a plain list item), in Org-mode
  21063. ;; This means we can easily find the end by looking
  21064. ;; only for the right number of stars. Using a regexp to do
  21065. ;; this is so much faster than using a Lisp loop.
  21066. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21067. (forward-char 1)
  21068. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21069. ;; something else, do it the slow way
  21070. (while (and (not (eobp))
  21071. (or first (> (funcall outline-level) level)))
  21072. (setq first nil)
  21073. (outline-next-heading)))
  21074. (unless to-heading
  21075. (if (memq (preceding-char) '(?\n ?\^M))
  21076. (progn
  21077. ;; Go to end of line before heading
  21078. (forward-char -1)
  21079. (if (memq (preceding-char) '(?\n ?\^M))
  21080. ;; leave blank line before heading
  21081. (forward-char -1))))))
  21082. (point))
  21083. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21084. "Use Org version in org-mode, for dramatic speed-up."
  21085. (if (derived-mode-p 'org-mode)
  21086. (progn
  21087. (org-end-of-subtree nil t)
  21088. (unless (eobp) (backward-char 1)))
  21089. ad-do-it))
  21090. (defun org-end-of-meta-data-and-drawers ()
  21091. "Jump to the first text after meta data and drawers in the current entry.
  21092. This will move over empty lines, lines with planning time stamps,
  21093. clocking lines, and drawers."
  21094. (org-back-to-heading t)
  21095. (let ((end (save-excursion (outline-next-heading) (point)))
  21096. (re (concat "\\(" org-drawer-regexp "\\)"
  21097. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21098. (forward-line 1)
  21099. (while (re-search-forward re end t)
  21100. (if (not (match-end 1))
  21101. ;; empty or planning line
  21102. (forward-line 1)
  21103. ;; a drawer, find the end
  21104. (re-search-forward "^[ \t]*:END:" end 'move)
  21105. (forward-line 1)))
  21106. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21107. (point)))
  21108. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21109. "Move forward to the ARG'th subheading at same level as this one.
  21110. Stop at the first and last subheadings of a superior heading.
  21111. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21112. non-nil it will also look at invisible ones."
  21113. (interactive "p")
  21114. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21115. (if (and arg (< arg 0))
  21116. (goto-char (point-min))
  21117. (outline-next-heading))
  21118. (org-at-heading-p)
  21119. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21120. (f (if (and arg (< arg 0))
  21121. 're-search-backward
  21122. 're-search-forward))
  21123. (count (if arg (abs arg) 1))
  21124. (result (point)))
  21125. (forward-char (if (and arg (< arg 0)) -1 1))
  21126. (while (and (> count 0)
  21127. (funcall f org-outline-regexp-bol nil 'move))
  21128. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21129. (cond ((< l level) (setq count 0))
  21130. ((and (= l level)
  21131. (or invisible-ok
  21132. (progn
  21133. (goto-char (line-beginning-position))
  21134. (not (outline-invisible-p)))))
  21135. (setq count (1- count))
  21136. (when (eq l level)
  21137. (setq result (point)))))))
  21138. (goto-char result))
  21139. (beginning-of-line 1)))
  21140. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21141. "Move backward to the ARG'th subheading at same level as this one.
  21142. Stop at the first and last subheadings of a superior heading."
  21143. (interactive "p")
  21144. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21145. (defun org-next-block (arg &optional backward block-regexp)
  21146. "Jump to the next block.
  21147. With a prefix argument ARG, jump forward ARG many source blocks.
  21148. When BACKWARD is non-nil, jump to the previous block.
  21149. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21150. (interactive "p")
  21151. (let ((re (or block-regexp org-block-regexp))
  21152. (re-search-fn (or (and backward 're-search-backward)
  21153. 're-search-forward)))
  21154. (if (looking-at re) (forward-char 1))
  21155. (condition-case nil
  21156. (funcall re-search-fn re nil nil arg)
  21157. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21158. (goto-char (match-beginning 0)) (org-show-context)))
  21159. (defun org-previous-block (arg &optional block-regexp)
  21160. "Jump to the previous block.
  21161. With a prefix argument ARG, jump backward ARG many source blocks.
  21162. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21163. (interactive "p")
  21164. (org-next-block arg t block-regexp))
  21165. (defun org-forward-element ()
  21166. "Move forward by one element.
  21167. Move to the next element at the same level, when possible."
  21168. (interactive)
  21169. (cond ((eobp) (user-error "Cannot move further down"))
  21170. ((org-with-limited-levels (org-at-heading-p))
  21171. (let ((origin (point)))
  21172. (goto-char (org-end-of-subtree nil t))
  21173. (unless (org-with-limited-levels (org-at-heading-p))
  21174. (goto-char origin)
  21175. (user-error "Cannot move further down"))))
  21176. (t
  21177. (let* ((elem (org-element-at-point))
  21178. (end (org-element-property :end elem))
  21179. (parent (org-element-property :parent elem)))
  21180. (if (and parent (= (org-element-property :contents-end parent) end))
  21181. (goto-char (org-element-property :end parent))
  21182. (goto-char end))))))
  21183. (defun org-backward-element ()
  21184. "Move backward by one element.
  21185. Move to the previous element at the same level, when possible."
  21186. (interactive)
  21187. (cond ((bobp) (user-error "Cannot move further up"))
  21188. ((org-with-limited-levels (org-at-heading-p))
  21189. ;; At a headline, move to the previous one, if any, or stay
  21190. ;; here.
  21191. (let ((origin (point)))
  21192. (org-with-limited-levels (org-backward-heading-same-level 1))
  21193. ;; When current headline has no sibling above, move to its
  21194. ;; parent.
  21195. (when (= (point) origin)
  21196. (or (org-with-limited-levels (org-up-heading-safe))
  21197. (progn (goto-char origin)
  21198. (user-error "Cannot move further up"))))))
  21199. (t
  21200. (let* ((trail (org-element-at-point 'keep-trail))
  21201. (elem (car trail))
  21202. (prev-elem (nth 1 trail))
  21203. (beg (org-element-property :begin elem)))
  21204. (cond
  21205. ;; Move to beginning of current element if point isn't
  21206. ;; there already.
  21207. ((/= (point) beg) (goto-char beg))
  21208. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21209. ((org-before-first-heading-p) (goto-char (point-min)))
  21210. (t (org-back-to-heading)))))))
  21211. (defun org-up-element ()
  21212. "Move to upper element."
  21213. (interactive)
  21214. (if (org-with-limited-levels (org-at-heading-p))
  21215. (unless (org-up-heading-safe) (error "No surrounding element"))
  21216. (let* ((elem (org-element-at-point))
  21217. (parent (org-element-property :parent elem)))
  21218. (if parent (goto-char (org-element-property :begin parent))
  21219. (if (org-with-limited-levels (org-before-first-heading-p))
  21220. (error "No surrounding element")
  21221. (org-with-limited-levels (org-back-to-heading)))))))
  21222. (defvar org-element-greater-elements)
  21223. (defun org-down-element ()
  21224. "Move to inner element."
  21225. (interactive)
  21226. (let ((element (org-element-at-point)))
  21227. (cond
  21228. ((memq (org-element-type element) '(plain-list table))
  21229. (goto-char (org-element-property :contents-begin element))
  21230. (forward-char))
  21231. ((memq (org-element-type element) org-element-greater-elements)
  21232. ;; If contents are hidden, first disclose them.
  21233. (when (org-element-property :hiddenp element) (org-cycle))
  21234. (goto-char (or (org-element-property :contents-begin element)
  21235. (error "No content for this element"))))
  21236. (t (error "No inner element")))))
  21237. (defun org-drag-element-backward ()
  21238. "Move backward element at point."
  21239. (interactive)
  21240. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21241. (let* ((trail (org-element-at-point 'keep-trail))
  21242. (elem (car trail))
  21243. (prev-elem (nth 1 trail)))
  21244. ;; Error out if no previous element or previous element is
  21245. ;; a parent of the current one.
  21246. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21247. (error "Cannot drag element backward")
  21248. (let ((pos (point)))
  21249. (org-element-swap-A-B prev-elem elem)
  21250. (goto-char (+ (org-element-property :begin prev-elem)
  21251. (- pos (org-element-property :begin elem)))))))))
  21252. (defun org-drag-element-forward ()
  21253. "Move forward element at point."
  21254. (interactive)
  21255. (let* ((pos (point))
  21256. (elem (org-element-at-point)))
  21257. (when (= (point-max) (org-element-property :end elem))
  21258. (error "Cannot drag element forward"))
  21259. (goto-char (org-element-property :end elem))
  21260. (let ((next-elem (org-element-at-point)))
  21261. (when (or (org-element-nested-p elem next-elem)
  21262. (and (eq (org-element-type next-elem) 'headline)
  21263. (not (eq (org-element-type elem) 'headline))))
  21264. (goto-char pos)
  21265. (error "Cannot drag element forward"))
  21266. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21267. ;; body's length (without final blanks) and by the length of
  21268. ;; blanks between ELEM and NEXT-ELEM.
  21269. (let ((size-next (- (save-excursion
  21270. (goto-char (org-element-property :end next-elem))
  21271. (skip-chars-backward " \r\t\n")
  21272. (forward-line)
  21273. ;; Small correction if buffer doesn't end
  21274. ;; with a newline character.
  21275. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21276. (org-element-property :begin next-elem)))
  21277. (size-blank (- (org-element-property :end elem)
  21278. (save-excursion
  21279. (goto-char (org-element-property :end elem))
  21280. (skip-chars-backward " \r\t\n")
  21281. (forward-line)
  21282. (point)))))
  21283. (org-element-swap-A-B elem next-elem)
  21284. (goto-char (+ pos size-next size-blank))))))
  21285. (defun org-drag-line-forward (arg)
  21286. "Drag the line at point ARG lines forward."
  21287. (interactive "p")
  21288. (dotimes (n (abs arg))
  21289. (let ((c (current-column)))
  21290. (if (< 0 arg)
  21291. (progn
  21292. (beginning-of-line 2)
  21293. (transpose-lines 1)
  21294. (beginning-of-line 0))
  21295. (transpose-lines 1)
  21296. (beginning-of-line -1))
  21297. (org-move-to-column c))))
  21298. (defun org-drag-line-backward (arg)
  21299. "Drag the line at point ARG lines backward."
  21300. (interactive "p")
  21301. (org-drag-line-forward (- arg)))
  21302. (defun org-mark-element ()
  21303. "Put point at beginning of this element, mark at end.
  21304. Interactively, if this command is repeated or (in Transient Mark
  21305. mode) if the mark is active, it marks the next element after the
  21306. ones already marked."
  21307. (interactive)
  21308. (let (deactivate-mark)
  21309. (if (and (org-called-interactively-p 'any)
  21310. (or (and (eq last-command this-command) (mark t))
  21311. (and transient-mark-mode mark-active)))
  21312. (set-mark
  21313. (save-excursion
  21314. (goto-char (mark))
  21315. (goto-char (org-element-property :end (org-element-at-point)))))
  21316. (let ((element (org-element-at-point)))
  21317. (end-of-line)
  21318. (push-mark (org-element-property :end element) t t)
  21319. (goto-char (org-element-property :begin element))))))
  21320. (defun org-narrow-to-element ()
  21321. "Narrow buffer to current element."
  21322. (interactive)
  21323. (let ((elem (org-element-at-point)))
  21324. (cond
  21325. ((eq (car elem) 'headline)
  21326. (narrow-to-region
  21327. (org-element-property :begin elem)
  21328. (org-element-property :end elem)))
  21329. ((memq (car elem) org-element-greater-elements)
  21330. (narrow-to-region
  21331. (org-element-property :contents-begin elem)
  21332. (org-element-property :contents-end elem)))
  21333. (t
  21334. (narrow-to-region
  21335. (org-element-property :begin elem)
  21336. (org-element-property :end elem))))))
  21337. (defun org-transpose-element ()
  21338. "Transpose current and previous elements, keeping blank lines between.
  21339. Point is moved after both elements."
  21340. (interactive)
  21341. (org-skip-whitespace)
  21342. (let ((end (org-element-property :end (org-element-at-point))))
  21343. (org-drag-element-backward)
  21344. (goto-char end)))
  21345. (defun org-unindent-buffer ()
  21346. "Un-indent the visible part of the buffer.
  21347. Relative indentation (between items, inside blocks, etc.) isn't
  21348. modified."
  21349. (interactive)
  21350. (unless (eq major-mode 'org-mode)
  21351. (error "Cannot un-indent a buffer not in Org mode"))
  21352. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21353. unindent-tree ; For byte-compiler.
  21354. (unindent-tree
  21355. (function
  21356. (lambda (contents)
  21357. (mapc
  21358. (lambda (element)
  21359. (if (memq (org-element-type element) '(headline section))
  21360. (funcall unindent-tree (org-element-contents element))
  21361. (save-excursion
  21362. (save-restriction
  21363. (narrow-to-region
  21364. (org-element-property :begin element)
  21365. (org-element-property :end element))
  21366. (org-do-remove-indentation)))))
  21367. (reverse contents))))))
  21368. (funcall unindent-tree (org-element-contents parse-tree))))
  21369. (defun org-show-subtree ()
  21370. "Show everything after this heading at deeper levels."
  21371. (interactive)
  21372. (outline-flag-region
  21373. (point)
  21374. (save-excursion
  21375. (org-end-of-subtree t t))
  21376. nil))
  21377. (defun org-show-entry ()
  21378. "Show the body directly following this heading.
  21379. Show the heading too, if it is currently invisible."
  21380. (interactive)
  21381. (save-excursion
  21382. (condition-case nil
  21383. (progn
  21384. (org-back-to-heading t)
  21385. (outline-flag-region
  21386. (max (point-min) (1- (point)))
  21387. (save-excursion
  21388. (if (re-search-forward
  21389. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21390. (match-beginning 1)
  21391. (point-max)))
  21392. nil)
  21393. (org-cycle-hide-drawers 'children))
  21394. (error nil))))
  21395. (defun org-make-options-regexp (kwds &optional extra)
  21396. "Make a regular expression for keyword lines."
  21397. (concat
  21398. "^#\\+\\("
  21399. (mapconcat 'regexp-quote kwds "\\|")
  21400. (if extra (concat "\\|" extra))
  21401. "\\):[ \t]*\\(.*\\)"))
  21402. ;; Make isearch reveal the necessary context
  21403. (defun org-isearch-end ()
  21404. "Reveal context after isearch exits."
  21405. (when isearch-success ; only if search was successful
  21406. (if (featurep 'xemacs)
  21407. ;; Under XEmacs, the hook is run in the correct place,
  21408. ;; we directly show the context.
  21409. (org-show-context 'isearch)
  21410. ;; In Emacs the hook runs *before* restoring the overlays.
  21411. ;; So we have to use a one-time post-command-hook to do this.
  21412. ;; (Emacs 22 has a special variable, see function `org-mode')
  21413. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21414. isearch-mode-end-hook-quit)
  21415. ;; Only when the isearch was not quitted.
  21416. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21417. 'append 'local)))
  21418. (org-fix-ellipsis-at-bol)))
  21419. (defun org-isearch-post-command ()
  21420. "Remove self from hook, and show context."
  21421. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21422. (org-show-context 'isearch))
  21423. ;;;; Integration with and fixes for other packages
  21424. ;;; Imenu support
  21425. (defvar org-imenu-markers nil
  21426. "All markers currently used by Imenu.")
  21427. (make-variable-buffer-local 'org-imenu-markers)
  21428. (defun org-imenu-new-marker (&optional pos)
  21429. "Return a new marker for use by Imenu, and remember the marker."
  21430. (let ((m (make-marker)))
  21431. (move-marker m (or pos (point)))
  21432. (push m org-imenu-markers)
  21433. m))
  21434. (defun org-imenu-get-tree ()
  21435. "Produce the index for Imenu."
  21436. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21437. (setq org-imenu-markers nil)
  21438. (let* ((n org-imenu-depth)
  21439. (re (concat "^" (org-get-limited-outline-regexp)))
  21440. (subs (make-vector (1+ n) nil))
  21441. (last-level 0)
  21442. m level head0 head)
  21443. (save-excursion
  21444. (save-restriction
  21445. (widen)
  21446. (goto-char (point-max))
  21447. (while (re-search-backward re nil t)
  21448. (setq level (org-reduced-level (funcall outline-level)))
  21449. (when (and (<= level n)
  21450. (looking-at org-complex-heading-regexp)
  21451. (setq head0 (org-match-string-no-properties 4)))
  21452. (setq head (org-link-display-format head0)
  21453. m (org-imenu-new-marker))
  21454. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21455. (if (>= level last-level)
  21456. (push (cons head m) (aref subs level))
  21457. (push (cons head (aref subs (1+ level))) (aref subs level))
  21458. (loop for i from (1+ level) to n do (aset subs i nil)))
  21459. (setq last-level level)))))
  21460. (aref subs 1)))
  21461. (eval-after-load "imenu"
  21462. '(progn
  21463. (add-hook 'imenu-after-jump-hook
  21464. (lambda ()
  21465. (if (derived-mode-p 'org-mode)
  21466. (org-show-context 'org-goto))))))
  21467. (defun org-link-display-format (link)
  21468. "Replace a link with its the description.
  21469. If there is no description, use the link target."
  21470. (save-match-data
  21471. (if (string-match org-bracket-link-analytic-regexp link)
  21472. (replace-match (if (match-end 5)
  21473. (match-string 5 link)
  21474. (concat (match-string 1 link)
  21475. (match-string 3 link)))
  21476. nil t link)
  21477. link)))
  21478. (defun org-toggle-link-display ()
  21479. "Toggle the literal or descriptive display of links."
  21480. (interactive)
  21481. (if org-descriptive-links
  21482. (progn (org-remove-from-invisibility-spec '(org-link))
  21483. (org-restart-font-lock)
  21484. (setq org-descriptive-links nil))
  21485. (progn (add-to-invisibility-spec '(org-link))
  21486. (org-restart-font-lock)
  21487. (setq org-descriptive-links t))))
  21488. ;; Speedbar support
  21489. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21490. "Overlay marking the agenda restriction line in speedbar.")
  21491. (overlay-put org-speedbar-restriction-lock-overlay
  21492. 'face 'org-agenda-restriction-lock)
  21493. (overlay-put org-speedbar-restriction-lock-overlay
  21494. 'help-echo "Agendas are currently limited to this item.")
  21495. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21496. (defun org-speedbar-set-agenda-restriction ()
  21497. "Restrict future agenda commands to the location at point in speedbar.
  21498. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21499. (interactive)
  21500. (require 'org-agenda)
  21501. (let (p m tp np dir txt)
  21502. (cond
  21503. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21504. 'org-imenu t))
  21505. (setq m (get-text-property p 'org-imenu-marker))
  21506. (with-current-buffer (marker-buffer m)
  21507. (goto-char m)
  21508. (org-agenda-set-restriction-lock 'subtree)))
  21509. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21510. 'speedbar-function 'speedbar-find-file))
  21511. (setq tp (previous-single-property-change
  21512. (1+ p) 'speedbar-function)
  21513. np (next-single-property-change
  21514. tp 'speedbar-function)
  21515. dir (speedbar-line-directory)
  21516. txt (buffer-substring-no-properties (or tp (point-min))
  21517. (or np (point-max))))
  21518. (with-current-buffer (find-file-noselect
  21519. (let ((default-directory dir))
  21520. (expand-file-name txt)))
  21521. (unless (derived-mode-p 'org-mode)
  21522. (error "Cannot restrict to non-Org-mode file"))
  21523. (org-agenda-set-restriction-lock 'file)))
  21524. (t (error "Don't know how to restrict Org-mode's agenda")))
  21525. (move-overlay org-speedbar-restriction-lock-overlay
  21526. (point-at-bol) (point-at-eol))
  21527. (setq current-prefix-arg nil)
  21528. (org-agenda-maybe-redo)))
  21529. (eval-after-load "speedbar"
  21530. '(progn
  21531. (speedbar-add-supported-extension ".org")
  21532. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21533. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21534. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21535. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21536. (add-hook 'speedbar-visiting-tag-hook
  21537. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21538. ;;; Fixes and Hacks for problems with other packages
  21539. ;; Make flyspell not check words in links, to not mess up our keymap
  21540. (defvar org-element-affiliated-keywords) ; From org-element.el
  21541. (defvar org-element-block-name-alist) ; From org-element.el
  21542. (defun org-mode-flyspell-verify ()
  21543. "Don't let flyspell put overlays at active buttons, or on
  21544. {todo,all-time,additional-option-like}-keywords."
  21545. (require 'org-element) ; For `org-element-affiliated-keywords'
  21546. (let ((pos (max (1- (point)) (point-min)))
  21547. (word (thing-at-point 'word)))
  21548. (and (not (get-text-property pos 'keymap))
  21549. (not (get-text-property pos 'org-no-flyspell))
  21550. (not (member word org-todo-keywords-1))
  21551. (not (member word org-all-time-keywords))
  21552. (not (member word org-options-keywords))
  21553. (not (member word (mapcar 'car org-startup-options)))
  21554. (not (member-ignore-case word org-element-affiliated-keywords))
  21555. (not (member-ignore-case word (org-get-export-keywords)))
  21556. (not (member-ignore-case
  21557. word (mapcar 'car org-element-block-name-alist)))
  21558. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21559. (defun org-remove-flyspell-overlays-in (beg end)
  21560. "Remove flyspell overlays in region."
  21561. (and (org-bound-and-true-p flyspell-mode)
  21562. (fboundp 'flyspell-delete-region-overlays)
  21563. (flyspell-delete-region-overlays beg end))
  21564. (add-text-properties beg end '(org-no-flyspell t)))
  21565. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21566. (eval-after-load "bookmark"
  21567. '(if (boundp 'bookmark-after-jump-hook)
  21568. ;; We can use the hook
  21569. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21570. ;; Hook not available, use advice
  21571. (defadvice bookmark-jump (after org-make-visible activate)
  21572. "Make the position visible."
  21573. (org-bookmark-jump-unhide))))
  21574. ;; Make sure saveplace shows the location if it was hidden
  21575. (eval-after-load "saveplace"
  21576. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21577. "Make the position visible."
  21578. (org-bookmark-jump-unhide)))
  21579. ;; Make sure ecb shows the location if it was hidden
  21580. (eval-after-load "ecb"
  21581. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21582. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21583. (if (derived-mode-p 'org-mode)
  21584. (org-show-context))))
  21585. (defun org-bookmark-jump-unhide ()
  21586. "Unhide the current position, to show the bookmark location."
  21587. (and (derived-mode-p 'org-mode)
  21588. (or (outline-invisible-p)
  21589. (save-excursion (goto-char (max (point-min) (1- (point))))
  21590. (outline-invisible-p)))
  21591. (org-show-context 'bookmark-jump)))
  21592. ;; Make session.el ignore our circular variable
  21593. (eval-after-load "session"
  21594. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21595. ;;;; Finish up
  21596. (provide 'org)
  21597. (run-hooks 'org-load-hook)
  21598. ;;; org.el ends here