org.el 888 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494
  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 " mhe: Links to MHE folders/messages" org-mhe)
  330. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  331. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  332. (const :tag " mouse: Additional mouse support" org-mouse)
  333. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  334. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  335. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  336. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  337. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  338. (const :tag "C collector: Collect properties into tables" org-collector)
  339. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  340. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  341. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  342. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  343. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  344. (const :tag "C eval: Include command output as text" org-eval)
  345. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  346. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  347. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  348. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  349. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  350. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  351. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  352. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  353. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  354. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  355. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  356. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  357. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  358. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  359. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  360. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  361. (const :tag "C registry: A registry for Org-mode links" org-registry)
  362. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  363. (const :tag "C secretary: Team management with org-mode" org-secretary)
  364. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  365. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  366. (const :tag "C track: Keep up with Org-mode development" org-track)
  367. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  368. (const :tag "C vm: Links to VM folders/messages" org-vm)
  369. (const :tag "C w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  370. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  371. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  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 " texinfo Export buffer to Texinfo format" texinfo)
  453. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  454. (const :tag "C deck Export buffer to deck.js presentations" deck)
  455. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  456. (const :tag "C groff Export buffer to Groff format" groff)
  457. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  458. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  459. (const :tag "C s5 Export buffer to s5 presentations" s5)
  460. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  461. (eval-after-load 'ox
  462. '(mapc
  463. (lambda (backend)
  464. (condition-case nil (require (intern (format "ox-%s" backend)))
  465. (error (message "Problems while trying to load export back-end `%s'"
  466. backend))))
  467. org-export-backends))
  468. (defcustom org-support-shift-select nil
  469. "Non-nil means make shift-cursor commands select text when possible.
  470. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  471. start selecting a region, or enlarge regions started in this way.
  472. In Org-mode, in special contexts, these same keys are used for
  473. other purposes, important enough to compete with shift selection.
  474. Org tries to balance these needs by supporting `shift-select-mode'
  475. outside these special contexts, under control of this variable.
  476. The default of this variable is nil, to avoid confusing behavior. Shifted
  477. cursor keys will then execute Org commands in the following contexts:
  478. - on a headline, changing TODO state (left/right) and priority (up/down)
  479. - on a time stamp, changing the time
  480. - in a plain list item, changing the bullet type
  481. - in a property definition line, switching between allowed values
  482. - in the BEGIN line of a clock table (changing the time block).
  483. Outside these contexts, the commands will throw an error.
  484. When this variable is t and the cursor is not in a special
  485. context, Org-mode will support shift-selection for making and
  486. enlarging regions. To make this more effective, the bullet
  487. cycling will no longer happen anywhere in an item line, but only
  488. if the cursor is exactly on the bullet.
  489. If you set this variable to the symbol `always', then the keys
  490. will not be special in headlines, property lines, and item lines,
  491. to make shift selection work there as well. If this is what you
  492. want, you can use the following alternative commands: `C-c C-t'
  493. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  494. can be used to switch TODO sets, `C-c -' to cycle item bullet
  495. types, and properties can be edited by hand or in column view.
  496. However, when the cursor is on a timestamp, shift-cursor commands
  497. will still edit the time stamp - this is just too good to give up.
  498. XEmacs user should have this variable set to nil, because
  499. `shift-select-mode' is in Emacs 23 or later only."
  500. :group 'org
  501. :type '(choice
  502. (const :tag "Never" nil)
  503. (const :tag "When outside special context" t)
  504. (const :tag "Everywhere except timestamps" always)))
  505. (defcustom org-loop-over-headlines-in-active-region nil
  506. "Shall some commands act upon headlines in the active region?
  507. When set to `t', some commands will be performed in all headlines
  508. within the active region.
  509. When set to `start-level', some commands will be performed in all
  510. headlines within the active region, provided that these headlines
  511. are of the same level than the first one.
  512. When set to a string, those commands will be performed on the
  513. matching headlines within the active region. Such string must be
  514. a tags/property/todo match as it is used in the agenda tags view.
  515. The list of commands is: `org-schedule', `org-deadline',
  516. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  517. `org-archive-to-archive-sibling'. The archiving commands skip
  518. already archived entries."
  519. :type '(choice (const :tag "Don't loop" nil)
  520. (const :tag "All headlines in active region" t)
  521. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  522. (string :tag "Tags/Property/Todo matcher"))
  523. :version "24.1"
  524. :group 'org-todo
  525. :group 'org-archive)
  526. (defgroup org-startup nil
  527. "Options concerning startup of Org-mode."
  528. :tag "Org Startup"
  529. :group 'org)
  530. (defcustom org-startup-folded t
  531. "Non-nil means entering Org-mode will switch to OVERVIEW.
  532. This can also be configured on a per-file basis by adding one of
  533. the following lines anywhere in the buffer:
  534. #+STARTUP: fold (or `overview', this is equivalent)
  535. #+STARTUP: nofold (or `showall', this is equivalent)
  536. #+STARTUP: content
  537. #+STARTUP: showeverything
  538. By default, this option is ignored when Org opens agenda files
  539. for the first time. If you want the agenda to honor the startup
  540. option, set `org-agenda-inhibit-startup' to nil."
  541. :group 'org-startup
  542. :type '(choice
  543. (const :tag "nofold: show all" nil)
  544. (const :tag "fold: overview" t)
  545. (const :tag "content: all headlines" content)
  546. (const :tag "show everything, even drawers" showeverything)))
  547. (defcustom org-startup-truncated t
  548. "Non-nil means entering Org-mode will set `truncate-lines'.
  549. This is useful since some lines containing links can be very long and
  550. uninteresting. Also tables look terrible when wrapped."
  551. :group 'org-startup
  552. :type 'boolean)
  553. (defcustom org-startup-indented nil
  554. "Non-nil means turn on `org-indent-mode' on startup.
  555. This can also be configured on a per-file basis by adding one of
  556. the following lines anywhere in the buffer:
  557. #+STARTUP: indent
  558. #+STARTUP: noindent"
  559. :group 'org-structure
  560. :type '(choice
  561. (const :tag "Not" nil)
  562. (const :tag "Globally (slow on startup in large files)" t)))
  563. (defcustom org-use-sub-superscripts t
  564. "Non-nil means interpret \"_\" and \"^\" for display.
  565. When this option is turned on, you can use TeX-like syntax for sub- and
  566. superscripts. Several characters after \"_\" or \"^\" will be
  567. considered as a single item - so grouping with {} is normally not
  568. needed. For example, the following things will be parsed as single
  569. sub- or superscripts.
  570. 10^24 or 10^tau several digits will be considered 1 item.
  571. 10^-12 or 10^-tau a leading sign with digits or a word
  572. x^2-y^3 will be read as x^2 - y^3, because items are
  573. terminated by almost any nonword/nondigit char.
  574. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  575. Still, ambiguity is possible - so when in doubt use {} to enclose
  576. the sub/superscript. If you set this variable to the symbol
  577. `{}', the braces are *required* in order to trigger
  578. interpretations as sub/superscript. This can be helpful in
  579. documents that need \"_\" frequently in plain text."
  580. :group 'org-startup
  581. :version "24.1"
  582. :type '(choice
  583. (const :tag "Always interpret" t)
  584. (const :tag "Only with braces" {})
  585. (const :tag "Never interpret" nil)))
  586. (defcustom org-startup-with-beamer-mode nil
  587. "Non-nil means turn on `org-beamer-mode' on startup.
  588. This can also be configured on a per-file basis by adding one of
  589. the following lines anywhere in the buffer:
  590. #+STARTUP: beamer"
  591. :group 'org-startup
  592. :version "24.1"
  593. :type 'boolean)
  594. (defcustom org-startup-align-all-tables nil
  595. "Non-nil means align all tables when visiting a file.
  596. This is useful when the column width in tables is forced with <N> cookies
  597. in table fields. Such tables will look correct only after the first re-align.
  598. This can also be configured on a per-file basis by adding one of
  599. the following lines anywhere in the buffer:
  600. #+STARTUP: align
  601. #+STARTUP: noalign"
  602. :group 'org-startup
  603. :type 'boolean)
  604. (defcustom org-startup-with-inline-images nil
  605. "Non-nil means show inline images when loading a new Org file.
  606. This can also be configured on a per-file basis by adding one of
  607. the following lines anywhere in the buffer:
  608. #+STARTUP: inlineimages
  609. #+STARTUP: noinlineimages"
  610. :group 'org-startup
  611. :version "24.1"
  612. :type 'boolean)
  613. (defcustom org-startup-with-latex-preview nil
  614. "Non-nil means preview LaTeX fragments when loading a new Org file.
  615. This can also be configured on a per-file basis by adding one of
  616. the followinglines anywhere in the buffer:
  617. #+STARTUP: latexpreview
  618. #+STARTUP: nolatexpreview"
  619. :group 'org-startup
  620. :version "24.4"
  621. :package-version '(Org . "8.0")
  622. :type 'boolean)
  623. (defcustom org-insert-mode-line-in-empty-file nil
  624. "Non-nil means insert the first line setting Org-mode in empty files.
  625. When the function `org-mode' is called interactively in an empty file, this
  626. normally means that the file name does not automatically trigger Org-mode.
  627. To ensure that the file will always be in Org-mode in the future, a
  628. line enforcing Org-mode will be inserted into the buffer, if this option
  629. has been set."
  630. :group 'org-startup
  631. :type 'boolean)
  632. (defcustom org-replace-disputed-keys nil
  633. "Non-nil means use alternative key bindings for some keys.
  634. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  635. These keys are also used by other packages like shift-selection-mode'
  636. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  637. If you want to use Org-mode together with one of these other modes,
  638. or more generally if you would like to move some Org-mode commands to
  639. other keys, set this variable and configure the keys with the variable
  640. `org-disputed-keys'.
  641. This option is only relevant at load-time of Org-mode, and must be set
  642. *before* org.el is loaded. Changing it requires a restart of Emacs to
  643. become effective."
  644. :group 'org-startup
  645. :type 'boolean)
  646. (defcustom org-use-extra-keys nil
  647. "Non-nil means use extra key sequence definitions for certain commands.
  648. This happens automatically if you run XEmacs or if `window-system'
  649. is nil. This variable lets you do the same manually. You must
  650. set it before loading org.
  651. Example: on Carbon Emacs 22 running graphically, with an external
  652. keyboard on a Powerbook, the default way of setting M-left might
  653. not work for either Alt or ESC. Setting this variable will make
  654. it work for ESC."
  655. :group 'org-startup
  656. :type 'boolean)
  657. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  658. (defcustom org-disputed-keys
  659. '(([(shift up)] . [(meta p)])
  660. ([(shift down)] . [(meta n)])
  661. ([(shift left)] . [(meta -)])
  662. ([(shift right)] . [(meta +)])
  663. ([(control shift right)] . [(meta shift +)])
  664. ([(control shift left)] . [(meta shift -)]))
  665. "Keys for which Org-mode and other modes compete.
  666. This is an alist, cars are the default keys, second element specifies
  667. the alternative to use when `org-replace-disputed-keys' is t.
  668. Keys can be specified in any syntax supported by `define-key'.
  669. The value of this option takes effect only at Org-mode's startup,
  670. therefore you'll have to restart Emacs to apply it after changing."
  671. :group 'org-startup
  672. :type 'alist)
  673. (defun org-key (key)
  674. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  675. Or return the original if not disputed.
  676. Also apply the translations defined in `org-xemacs-key-equivalents'."
  677. (when org-replace-disputed-keys
  678. (let* ((nkey (key-description key))
  679. (x (org-find-if (lambda (x)
  680. (equal (key-description (car x)) nkey))
  681. org-disputed-keys)))
  682. (setq key (if x (cdr x) key))))
  683. (when (featurep 'xemacs)
  684. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  685. key)
  686. (defun org-find-if (predicate seq)
  687. (catch 'exit
  688. (while seq
  689. (if (funcall predicate (car seq))
  690. (throw 'exit (car seq))
  691. (pop seq)))))
  692. (defun org-defkey (keymap key def)
  693. "Define a key, possibly translated, as returned by `org-key'."
  694. (define-key keymap (org-key key) def))
  695. (defcustom org-ellipsis nil
  696. "The ellipsis to use in the Org-mode outline.
  697. When nil, just use the standard three dots. When a string, use that instead,
  698. When a face, use the standard 3 dots, but with the specified face.
  699. The change affects only Org-mode (which will then use its own display table).
  700. Changing this requires executing `M-x org-mode' in a buffer to become
  701. effective."
  702. :group 'org-startup
  703. :type '(choice (const :tag "Default" nil)
  704. (face :tag "Face" :value org-warning)
  705. (string :tag "String" :value "...#")))
  706. (defvar org-display-table nil
  707. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  708. (defgroup org-keywords nil
  709. "Keywords in Org-mode."
  710. :tag "Org Keywords"
  711. :group 'org)
  712. (defcustom org-deadline-string "DEADLINE:"
  713. "String to mark deadline entries.
  714. A deadline is this string, followed by a time stamp. Should be a word,
  715. terminated by a colon. You can insert a schedule keyword and
  716. a timestamp with \\[org-deadline].
  717. Changes become only effective after restarting Emacs."
  718. :group 'org-keywords
  719. :type 'string)
  720. (defcustom org-scheduled-string "SCHEDULED:"
  721. "String to mark scheduled TODO entries.
  722. A schedule is this string, followed by a time stamp. Should be a word,
  723. terminated by a colon. You can insert a schedule keyword and
  724. a timestamp with \\[org-schedule].
  725. Changes become only effective after restarting Emacs."
  726. :group 'org-keywords
  727. :type 'string)
  728. (defcustom org-closed-string "CLOSED:"
  729. "String used as the prefix for timestamps logging closing a TODO entry."
  730. :group 'org-keywords
  731. :type 'string)
  732. (defcustom org-clock-string "CLOCK:"
  733. "String used as prefix for timestamps clocking work hours on an item."
  734. :group 'org-keywords
  735. :type 'string)
  736. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  737. org-scheduled-string "\\|"
  738. org-deadline-string "\\|"
  739. org-closed-string "\\|"
  740. org-clock-string "\\)")
  741. "Matches a line with planning or clock info.")
  742. (defcustom org-comment-string "COMMENT"
  743. "Entries starting with this keyword will never be exported.
  744. An entry can be toggled between COMMENT and normal with
  745. \\[org-toggle-comment].
  746. Changes become only effective after restarting Emacs."
  747. :group 'org-keywords
  748. :type 'string)
  749. (defcustom org-quote-string "QUOTE"
  750. "Entries starting with this keyword will be exported in fixed-width font.
  751. Quoting applies only to the text in the entry following the headline, and does
  752. not extend beyond the next headline, even if that is lower level.
  753. An entry can be toggled between QUOTE and normal with
  754. \\[org-toggle-fixed-width-section]."
  755. :group 'org-keywords
  756. :type 'string)
  757. (defconst org-repeat-re
  758. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  759. "Regular expression for specifying repeated events.
  760. After a match, group 1 contains the repeat expression.")
  761. (defgroup org-structure nil
  762. "Options concerning the general structure of Org-mode files."
  763. :tag "Org Structure"
  764. :group 'org)
  765. (defgroup org-reveal-location nil
  766. "Options about how to make context of a location visible."
  767. :tag "Org Reveal Location"
  768. :group 'org-structure)
  769. (defconst org-context-choice
  770. '(choice
  771. (const :tag "Always" t)
  772. (const :tag "Never" nil)
  773. (repeat :greedy t :tag "Individual contexts"
  774. (cons
  775. (choice :tag "Context"
  776. (const agenda)
  777. (const org-goto)
  778. (const occur-tree)
  779. (const tags-tree)
  780. (const link-search)
  781. (const mark-goto)
  782. (const bookmark-jump)
  783. (const isearch)
  784. (const default))
  785. (boolean))))
  786. "Contexts for the reveal options.")
  787. (defcustom org-show-hierarchy-above '((default . t))
  788. "Non-nil means show full hierarchy when revealing a location.
  789. Org-mode often shows locations in an org-mode file which might have
  790. been invisible before. When this is set, the hierarchy of headings
  791. above the exposed location is shown.
  792. Turning this off for example for sparse trees makes them very compact.
  793. Instead of t, this can also be an alist specifying this option for different
  794. contexts. Valid contexts are
  795. agenda when exposing an entry from the agenda
  796. org-goto when using the command `org-goto' on key C-c C-j
  797. occur-tree when using the command `org-occur' on key C-c /
  798. tags-tree when constructing a sparse tree based on tags matches
  799. link-search when exposing search matches associated with a link
  800. mark-goto when exposing the jump goal of a mark
  801. bookmark-jump when exposing a bookmark location
  802. isearch when exiting from an incremental search
  803. default default for all contexts not set explicitly"
  804. :group 'org-reveal-location
  805. :type org-context-choice)
  806. (defcustom org-show-following-heading '((default . nil))
  807. "Non-nil means show following heading when revealing a location.
  808. Org-mode often shows locations in an org-mode file which might have
  809. been invisible before. When this is set, the heading following the
  810. match is shown.
  811. Turning this off for example for sparse trees makes them very compact,
  812. but makes it harder to edit the location of the match. In such a case,
  813. use the command \\[org-reveal] to show more context.
  814. Instead of t, this can also be an alist specifying this option for different
  815. contexts. See `org-show-hierarchy-above' for valid contexts."
  816. :group 'org-reveal-location
  817. :type org-context-choice)
  818. (defcustom org-show-siblings '((default . nil) (isearch t))
  819. "Non-nil means show all sibling heading when revealing a location.
  820. Org-mode often shows locations in an org-mode file which might have
  821. been invisible before. When this is set, the sibling of the current entry
  822. heading are all made visible. If `org-show-hierarchy-above' is t,
  823. the same happens on each level of the hierarchy above the current entry.
  824. By default this is on for the isearch context, off for all other contexts.
  825. Turning this off for example for sparse trees makes them very compact,
  826. but makes it harder to edit the location of the match. In such a case,
  827. use the command \\[org-reveal] to show more context.
  828. Instead of t, this can also be an alist specifying this option for different
  829. contexts. See `org-show-hierarchy-above' for valid contexts."
  830. :group 'org-reveal-location
  831. :type org-context-choice)
  832. (defcustom org-show-entry-below '((default . nil))
  833. "Non-nil means show the entry below a headline when revealing a location.
  834. Org-mode often shows locations in an org-mode file which might have
  835. been invisible before. When this is set, the text below the headline that is
  836. exposed is also shown.
  837. By default this is off for all contexts.
  838. Instead of t, this can also be an alist specifying this option for different
  839. contexts. See `org-show-hierarchy-above' for valid contexts."
  840. :group 'org-reveal-location
  841. :type org-context-choice)
  842. (defcustom org-indirect-buffer-display 'other-window
  843. "How should indirect tree buffers be displayed?
  844. This applies to indirect buffers created with the commands
  845. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  846. Valid values are:
  847. current-window Display in the current window
  848. other-window Just display in another window.
  849. dedicated-frame Create one new frame, and re-use it each time.
  850. new-frame Make a new frame each time. Note that in this case
  851. previously-made indirect buffers are kept, and you need to
  852. kill these buffers yourself."
  853. :group 'org-structure
  854. :group 'org-agenda-windows
  855. :type '(choice
  856. (const :tag "In current window" current-window)
  857. (const :tag "In current frame, other window" other-window)
  858. (const :tag "Each time a new frame" new-frame)
  859. (const :tag "One dedicated frame" dedicated-frame)))
  860. (defcustom org-use-speed-commands nil
  861. "Non-nil means activate single letter commands at beginning of a headline.
  862. This may also be a function to test for appropriate locations where speed
  863. commands should be active."
  864. :group 'org-structure
  865. :type '(choice
  866. (const :tag "Never" nil)
  867. (const :tag "At beginning of headline stars" t)
  868. (function)))
  869. (defcustom org-speed-commands-user nil
  870. "Alist of additional speed commands.
  871. This list will be checked before `org-speed-commands-default'
  872. when the variable `org-use-speed-commands' is non-nil
  873. and when the cursor is at the beginning of a headline.
  874. The car if each entry is a string with a single letter, which must
  875. be assigned to `self-insert-command' in the global map.
  876. The cdr is either a command to be called interactively, a function
  877. to be called, or a form to be evaluated.
  878. An entry that is just a list with a single string will be interpreted
  879. as a descriptive headline that will be added when listing the speed
  880. commands in the Help buffer using the `?' speed command."
  881. :group 'org-structure
  882. :type '(repeat :value ("k" . ignore)
  883. (choice :value ("k" . ignore)
  884. (list :tag "Descriptive Headline" (string :tag "Headline"))
  885. (cons :tag "Letter and Command"
  886. (string :tag "Command letter")
  887. (choice
  888. (function)
  889. (sexp))))))
  890. (defgroup org-cycle nil
  891. "Options concerning visibility cycling in Org-mode."
  892. :tag "Org Cycle"
  893. :group 'org-structure)
  894. (defcustom org-cycle-skip-children-state-if-no-children t
  895. "Non-nil means skip CHILDREN state in entries that don't have any."
  896. :group 'org-cycle
  897. :type 'boolean)
  898. (defcustom org-cycle-max-level nil
  899. "Maximum level which should still be subject to visibility cycling.
  900. Levels higher than this will, for cycling, be treated as text, not a headline.
  901. When `org-odd-levels-only' is set, a value of N in this variable actually
  902. means 2N-1 stars as the limiting headline.
  903. When nil, cycle all levels.
  904. Note that the limiting level of cycling is also influenced by
  905. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  906. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  907. than its value."
  908. :group 'org-cycle
  909. :type '(choice
  910. (const :tag "No limit" nil)
  911. (integer :tag "Maximum level")))
  912. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  913. "Names of drawers. Drawers are not opened by cycling on the headline above.
  914. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  915. this:
  916. :DRAWERNAME:
  917. .....
  918. :END:
  919. The drawer \"PROPERTIES\" is special for capturing properties through
  920. the property API.
  921. Drawers can be defined on the per-file basis with a line like:
  922. #+DRAWERS: HIDDEN STATE PROPERTIES"
  923. :group 'org-structure
  924. :group 'org-cycle
  925. :type '(repeat (string :tag "Drawer Name")))
  926. (defcustom org-hide-block-startup nil
  927. "Non-nil means entering Org-mode will fold all blocks.
  928. This can also be set in on a per-file basis with
  929. #+STARTUP: hideblocks
  930. #+STARTUP: showblocks"
  931. :group 'org-startup
  932. :group 'org-cycle
  933. :type 'boolean)
  934. (defcustom org-cycle-global-at-bob nil
  935. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  936. This makes it possible to do global cycling without having to use S-TAB or
  937. \\[universal-argument] TAB. For this special case to work, the first line
  938. of the buffer must not be a headline -- it may be empty or some other text.
  939. When used in this way, `org-cycle-hook' is disabled temporarily to make
  940. sure the cursor stays at the beginning of the buffer. When this option is
  941. nil, don't do anything special at the beginning of the buffer."
  942. :group 'org-cycle
  943. :type 'boolean)
  944. (defcustom org-cycle-level-after-item/entry-creation t
  945. "Non-nil means cycle entry level or item indentation in new empty entries.
  946. When the cursor is at the end of an empty headline, i.e., with only stars
  947. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  948. and then all possible ancestor states, before returning to the original state.
  949. This makes data entry extremely fast: M-RET to create a new headline,
  950. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  951. When the cursor is at the end of an empty plain list item, one TAB will
  952. make it a subitem, two or more tabs will back up to make this an item
  953. higher up in the item hierarchy."
  954. :group 'org-cycle
  955. :type 'boolean)
  956. (defcustom org-cycle-emulate-tab t
  957. "Where should `org-cycle' emulate TAB.
  958. nil Never
  959. white Only in completely white lines
  960. whitestart Only at the beginning of lines, before the first non-white char
  961. t Everywhere except in headlines
  962. exc-hl-bol Everywhere except at the start of a headline
  963. If TAB is used in a place where it does not emulate TAB, the current subtree
  964. visibility is cycled."
  965. :group 'org-cycle
  966. :type '(choice (const :tag "Never" nil)
  967. (const :tag "Only in completely white lines" white)
  968. (const :tag "Before first char in a line" whitestart)
  969. (const :tag "Everywhere except in headlines" t)
  970. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  971. ))
  972. (defcustom org-cycle-separator-lines 2
  973. "Number of empty lines needed to keep an empty line between collapsed trees.
  974. If you leave an empty line between the end of a subtree and the following
  975. headline, this empty line is hidden when the subtree is folded.
  976. Org-mode will leave (exactly) one empty line visible if the number of
  977. empty lines is equal or larger to the number given in this variable.
  978. So the default 2 means at least 2 empty lines after the end of a subtree
  979. are needed to produce free space between a collapsed subtree and the
  980. following headline.
  981. If the number is negative, and the number of empty lines is at least -N,
  982. all empty lines are shown.
  983. Special case: when 0, never leave empty lines in collapsed view."
  984. :group 'org-cycle
  985. :type 'integer)
  986. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  987. (defcustom org-pre-cycle-hook nil
  988. "Hook that is run before visibility cycling is happening.
  989. The function(s) in this hook must accept a single argument which indicates
  990. the new state that will be set right after running this hook. The
  991. argument is a symbol. Before a global state change, it can have the values
  992. `overview', `content', or `all'. Before a local state change, it can have
  993. the values `folded', `children', or `subtree'."
  994. :group 'org-cycle
  995. :type 'hook)
  996. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  997. org-cycle-hide-drawers
  998. org-cycle-hide-inline-tasks
  999. org-cycle-show-empty-lines
  1000. org-optimize-window-after-visibility-change)
  1001. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1002. The function(s) in this hook must accept a single argument which indicates
  1003. the new state that was set by the most recent `org-cycle' command. The
  1004. argument is a symbol. After a global state change, it can have the values
  1005. `overview', `contents', or `all'. After a local state change, it can have
  1006. the values `folded', `children', or `subtree'."
  1007. :group 'org-cycle
  1008. :type 'hook)
  1009. (defgroup org-edit-structure nil
  1010. "Options concerning structure editing in Org-mode."
  1011. :tag "Org Edit Structure"
  1012. :group 'org-structure)
  1013. (defcustom org-odd-levels-only nil
  1014. "Non-nil means skip even levels and only use odd levels for the outline.
  1015. This has the effect that two stars are being added/taken away in
  1016. promotion/demotion commands. It also influences how levels are
  1017. handled by the exporters.
  1018. Changing it requires restart of `font-lock-mode' to become effective
  1019. for fontification also in regions already fontified.
  1020. You may also set this on a per-file basis by adding one of the following
  1021. lines to the buffer:
  1022. #+STARTUP: odd
  1023. #+STARTUP: oddeven"
  1024. :group 'org-edit-structure
  1025. :group 'org-appearance
  1026. :type 'boolean)
  1027. (defcustom org-adapt-indentation t
  1028. "Non-nil means adapt indentation to outline node level.
  1029. When this variable is set, Org assumes that you write outlines by
  1030. indenting text in each node to align with the headline (after the stars).
  1031. The following issues are influenced by this variable:
  1032. - When this is set and the *entire* text in an entry is indented, the
  1033. indentation is increased by one space in a demotion command, and
  1034. decreased by one in a promotion command. If any line in the entry
  1035. body starts with text at column 0, indentation is not changed at all.
  1036. - Property drawers and planning information is inserted indented when
  1037. this variable s set. When nil, they will not be indented.
  1038. - TAB indents a line relative to context. The lines below a headline
  1039. will be indented when this variable is set.
  1040. Note that this is all about true indentation, by adding and removing
  1041. space characters. See also `org-indent.el' which does level-dependent
  1042. indentation in a virtual way, i.e. at display time in Emacs."
  1043. :group 'org-edit-structure
  1044. :type 'boolean)
  1045. (defcustom org-special-ctrl-a/e nil
  1046. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1047. When t, `C-a' will bring back the cursor to the beginning of the
  1048. headline text, i.e. after the stars and after a possible TODO
  1049. keyword. In an item, this will be the position after bullet and
  1050. check-box, if any. When the cursor is already at that position,
  1051. another `C-a' will bring it to the beginning of the line.
  1052. `C-e' will jump to the end of the headline, ignoring the presence
  1053. of tags in the headline. A second `C-e' will then jump to the
  1054. true end of the line, after any tags. This also means that, when
  1055. this variable is non-nil, `C-e' also will never jump beyond the
  1056. end of the heading of a folded section, i.e. not after the
  1057. ellipses.
  1058. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1059. normally, going to the true line boundary first. Only a directly
  1060. following, identical keypress will bring the cursor to the
  1061. special positions.
  1062. This may also be a cons cell where the behavior for `C-a' and
  1063. `C-e' is set separately."
  1064. :group 'org-edit-structure
  1065. :type '(choice
  1066. (const :tag "off" nil)
  1067. (const :tag "on: after stars/bullet and before tags first" t)
  1068. (const :tag "reversed: true line boundary first" reversed)
  1069. (cons :tag "Set C-a and C-e separately"
  1070. (choice :tag "Special C-a"
  1071. (const :tag "off" nil)
  1072. (const :tag "on: after stars/bullet first" t)
  1073. (const :tag "reversed: before stars/bullet first" reversed))
  1074. (choice :tag "Special C-e"
  1075. (const :tag "off" nil)
  1076. (const :tag "on: before tags first" t)
  1077. (const :tag "reversed: after tags first" reversed)))))
  1078. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1079. (defcustom org-special-ctrl-k nil
  1080. "Non-nil means `C-k' will behave specially in headlines.
  1081. When nil, `C-k' will call the default `kill-line' command.
  1082. When t, the following will happen while the cursor is in the headline:
  1083. - When the cursor is at the beginning of a headline, kill the entire
  1084. line and possible the folded subtree below the line.
  1085. - When in the middle of the headline text, kill the headline up to the tags.
  1086. - When after the headline text, kill the tags."
  1087. :group 'org-edit-structure
  1088. :type 'boolean)
  1089. (defcustom org-ctrl-k-protect-subtree nil
  1090. "Non-nil means, do not delete a hidden subtree with C-k.
  1091. When set to the symbol `error', simply throw an error when C-k is
  1092. used to kill (part-of) a headline that has hidden text behind it.
  1093. Any other non-nil value will result in a query to the user, if it is
  1094. OK to kill that hidden subtree. When nil, kill without remorse."
  1095. :group 'org-edit-structure
  1096. :version "24.1"
  1097. :type '(choice
  1098. (const :tag "Do not protect hidden subtrees" nil)
  1099. (const :tag "Protect hidden subtrees with a security query" t)
  1100. (const :tag "Never kill a hidden subtree with C-k" error)))
  1101. (defcustom org-catch-invisible-edits nil
  1102. "Check if in invisible region before inserting or deleting a character.
  1103. Valid values are:
  1104. nil Do not check, so just do invisible edits.
  1105. error Throw an error and do nothing.
  1106. show Make point visible, and do the requested edit.
  1107. show-and-error Make point visible, then throw an error and abort the edit.
  1108. smart Make point visible, and do insertion/deletion if it is
  1109. adjacent to visible text and the change feels predictable.
  1110. Never delete a previously invisible character or add in the
  1111. middle or right after an invisible region. Basically, this
  1112. allows insertion and backward-delete right before ellipses.
  1113. FIXME: maybe in this case we should not even show?"
  1114. :group 'org-edit-structure
  1115. :version "24.1"
  1116. :type '(choice
  1117. (const :tag "Do not check" nil)
  1118. (const :tag "Throw error when trying to edit" error)
  1119. (const :tag "Unhide, but do not do the edit" show-and-error)
  1120. (const :tag "Show invisible part and do the edit" show)
  1121. (const :tag "Be smart and do the right thing" smart)))
  1122. (defcustom org-yank-folded-subtrees t
  1123. "Non-nil means when yanking subtrees, fold them.
  1124. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1125. it starts with a heading and all other headings in it are either children
  1126. or siblings, then fold all the subtrees. However, do this only if no
  1127. text after the yank would be swallowed into a folded tree by this action."
  1128. :group 'org-edit-structure
  1129. :type 'boolean)
  1130. (defcustom org-yank-adjusted-subtrees nil
  1131. "Non-nil means when yanking subtrees, adjust the level.
  1132. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1133. this function for details."
  1134. :group 'org-edit-structure
  1135. :type 'boolean)
  1136. (defcustom org-M-RET-may-split-line '((default . t))
  1137. "Non-nil means M-RET will split the line at the cursor position.
  1138. When nil, it will go to the end of the line before making a
  1139. new line.
  1140. You may also set this option in a different way for different
  1141. contexts. Valid contexts are:
  1142. headline when creating a new headline
  1143. item when creating a new item
  1144. table in a table field
  1145. default the value to be used for all contexts not explicitly
  1146. customized"
  1147. :group 'org-structure
  1148. :group 'org-table
  1149. :type '(choice
  1150. (const :tag "Always" t)
  1151. (const :tag "Never" nil)
  1152. (repeat :greedy t :tag "Individual contexts"
  1153. (cons
  1154. (choice :tag "Context"
  1155. (const headline)
  1156. (const item)
  1157. (const table)
  1158. (const default))
  1159. (boolean)))))
  1160. (defcustom org-insert-heading-respect-content nil
  1161. "Non-nil means insert new headings after the current subtree.
  1162. When nil, the new heading is created directly after the current line.
  1163. The commands \\[org-insert-heading-respect-content] and
  1164. \\[org-insert-todo-heading-respect-content] turn this variable on
  1165. for the duration of the command."
  1166. :group 'org-structure
  1167. :type 'boolean)
  1168. (defcustom org-blank-before-new-entry '((heading . auto)
  1169. (plain-list-item . auto))
  1170. "Should `org-insert-heading' leave a blank line before new heading/item?
  1171. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1172. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1173. which case Org will look at the surrounding headings/items and try to
  1174. make an intelligent decision whether to insert a blank line or not.
  1175. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1176. the setting here is ignored and no empty line is inserted to avoid breaking
  1177. the list structure."
  1178. :group 'org-edit-structure
  1179. :type '(list
  1180. (cons (const heading)
  1181. (choice (const :tag "Never" nil)
  1182. (const :tag "Always" t)
  1183. (const :tag "Auto" auto)))
  1184. (cons (const plain-list-item)
  1185. (choice (const :tag "Never" nil)
  1186. (const :tag "Always" t)
  1187. (const :tag "Auto" auto)))))
  1188. (defcustom org-insert-heading-hook nil
  1189. "Hook being run after inserting a new heading."
  1190. :group 'org-edit-structure
  1191. :type 'hook)
  1192. (defcustom org-enable-fixed-width-editor t
  1193. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1194. This currently only means they are never auto-wrapped.
  1195. When nil, such lines will be treated like ordinary lines.
  1196. See also the QUOTE keyword."
  1197. :group 'org-edit-structure
  1198. :type 'boolean)
  1199. (defcustom org-goto-auto-isearch t
  1200. "Non-nil means typing characters in `org-goto' starts incremental search.
  1201. When nil, you can use these keybindings to navigate the buffer:
  1202. q Quit the org-goto interface
  1203. n Go to the next visible heading
  1204. p Go to the previous visible heading
  1205. f Go one heading forward on same level
  1206. b Go one heading backward on same level
  1207. u Go one heading up"
  1208. :group 'org-edit-structure
  1209. :type 'boolean)
  1210. (defgroup org-sparse-trees nil
  1211. "Options concerning sparse trees in Org-mode."
  1212. :tag "Org Sparse Trees"
  1213. :group 'org-structure)
  1214. (defcustom org-highlight-sparse-tree-matches t
  1215. "Non-nil means highlight all matches that define a sparse tree.
  1216. The highlights will automatically disappear the next time the buffer is
  1217. changed by an edit command."
  1218. :group 'org-sparse-trees
  1219. :type 'boolean)
  1220. (defcustom org-remove-highlights-with-change t
  1221. "Non-nil means any change to the buffer will remove temporary highlights.
  1222. Such highlights are created by `org-occur' and `org-clock-display'.
  1223. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1224. The highlights created by `org-preview-latex-fragment' always need
  1225. `C-c C-c' to be removed."
  1226. :group 'org-sparse-trees
  1227. :group 'org-time
  1228. :type 'boolean)
  1229. (defcustom org-occur-hook '(org-first-headline-recenter)
  1230. "Hook that is run after `org-occur' has constructed a sparse tree.
  1231. This can be used to recenter the window to show as much of the structure
  1232. as possible."
  1233. :group 'org-sparse-trees
  1234. :type 'hook)
  1235. (defgroup org-imenu-and-speedbar nil
  1236. "Options concerning imenu and speedbar in Org-mode."
  1237. :tag "Org Imenu and Speedbar"
  1238. :group 'org-structure)
  1239. (defcustom org-imenu-depth 2
  1240. "The maximum level for Imenu access to Org-mode headlines.
  1241. This also applied for speedbar access."
  1242. :group 'org-imenu-and-speedbar
  1243. :type 'integer)
  1244. (defgroup org-table nil
  1245. "Options concerning tables in Org-mode."
  1246. :tag "Org Table"
  1247. :group 'org)
  1248. (defcustom org-enable-table-editor 'optimized
  1249. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1250. When nil, such lines will be treated like ordinary lines.
  1251. When equal to the symbol `optimized', the table editor will be optimized to
  1252. do the following:
  1253. - Automatic overwrite mode in front of whitespace in table fields.
  1254. This makes the structure of the table stay in tact as long as the edited
  1255. field does not exceed the column width.
  1256. - Minimize the number of realigns. Normally, the table is aligned each time
  1257. TAB or RET are pressed to move to another field. With optimization this
  1258. happens only if changes to a field might have changed the column width.
  1259. Optimization requires replacing the functions `self-insert-command',
  1260. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1261. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1262. very good at guessing when a re-align will be necessary, but you can always
  1263. force one with \\[org-ctrl-c-ctrl-c].
  1264. If you would like to use the optimized version in Org-mode, but the
  1265. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1266. This variable can be used to turn on and off the table editor during a session,
  1267. but in order to toggle optimization, a restart is required.
  1268. See also the variable `org-table-auto-blank-field'."
  1269. :group 'org-table
  1270. :type '(choice
  1271. (const :tag "off" nil)
  1272. (const :tag "on" t)
  1273. (const :tag "on, optimized" optimized)))
  1274. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1275. (version<= emacs-version "24.1"))
  1276. "Non-nil means cluster self-insert commands for undo when possible.
  1277. If this is set, then, like in the Emacs command loop, 20 consecutive
  1278. characters will be undone together.
  1279. This is configurable, because there is some impact on typing performance."
  1280. :group 'org-table
  1281. :type 'boolean)
  1282. (defcustom org-table-tab-recognizes-table.el t
  1283. "Non-nil means TAB will automatically notice a table.el table.
  1284. When it sees such a table, it moves point into it and - if necessary -
  1285. calls `table-recognize-table'."
  1286. :group 'org-table-editing
  1287. :type 'boolean)
  1288. (defgroup org-link nil
  1289. "Options concerning links in Org-mode."
  1290. :tag "Org Link"
  1291. :group 'org)
  1292. (defvar org-link-abbrev-alist-local nil
  1293. "Buffer-local version of `org-link-abbrev-alist', which see.
  1294. The value of this is taken from the #+LINK lines.")
  1295. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1296. (defcustom org-link-abbrev-alist nil
  1297. "Alist of link abbreviations.
  1298. The car of each element is a string, to be replaced at the start of a link.
  1299. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1300. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1301. [[linkkey:tag][description]]
  1302. The 'linkkey' must be a word word, starting with a letter, followed
  1303. by letters, numbers, '-' or '_'.
  1304. If REPLACE is a string, the tag will simply be appended to create the link.
  1305. If the string contains \"%s\", the tag will be inserted there. If the string
  1306. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1307. at that point (see the function `url-hexify-string'). If the string contains
  1308. the specifier \"%(my-function)\", then the custom function `my-function' will
  1309. be invoked: this function takes the tag as its only argument and must return
  1310. a string.
  1311. REPLACE may also be a function that will be called with the tag as the
  1312. only argument to create the link, which should be returned as a string.
  1313. See the manual for examples."
  1314. :group 'org-link
  1315. :type '(repeat
  1316. (cons
  1317. (string :tag "Protocol")
  1318. (choice
  1319. (string :tag "Format")
  1320. (function)))))
  1321. (defcustom org-descriptive-links t
  1322. "Non-nil means Org will display descriptive links.
  1323. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1324. \"Org Website\", hiding the link itself and just displaying its
  1325. description. When set to `nil', Org will display the full links
  1326. literally.
  1327. You can interactively set the value of this variable by calling
  1328. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1329. :group 'org-link
  1330. :type 'boolean)
  1331. (defcustom org-link-file-path-type 'adaptive
  1332. "How the path name in file links should be stored.
  1333. Valid values are:
  1334. relative Relative to the current directory, i.e. the directory of the file
  1335. into which the link is being inserted.
  1336. absolute Absolute path, if possible with ~ for home directory.
  1337. noabbrev Absolute path, no abbreviation of home directory.
  1338. adaptive Use relative path for files in the current directory and sub-
  1339. directories of it. For other files, use an absolute path."
  1340. :group 'org-link
  1341. :type '(choice
  1342. (const relative)
  1343. (const absolute)
  1344. (const noabbrev)
  1345. (const adaptive)))
  1346. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1347. "Types of links that should be activated in Org-mode files.
  1348. This is a list of symbols, each leading to the activation of a certain link
  1349. type. In principle, it does not hurt to turn on most link types - there may
  1350. be a small gain when turning off unused link types. The types are:
  1351. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1352. angle Links in angular brackets that may contain whitespace like
  1353. <bbdb:Carsten Dominik>.
  1354. plain Plain links in normal text, no whitespace, like http://google.com.
  1355. radio Text that is matched by a radio target, see manual for details.
  1356. tag Tag settings in a headline (link to tag search).
  1357. date Time stamps (link to calendar).
  1358. footnote Footnote labels.
  1359. Changing this variable requires a restart of Emacs to become effective."
  1360. :group 'org-link
  1361. :type '(set :greedy t
  1362. (const :tag "Double bracket links" bracket)
  1363. (const :tag "Angular bracket links" angle)
  1364. (const :tag "Plain text links" plain)
  1365. (const :tag "Radio target matches" radio)
  1366. (const :tag "Tags" tag)
  1367. (const :tag "Timestamps" date)
  1368. (const :tag "Footnotes" footnote)))
  1369. (defcustom org-make-link-description-function nil
  1370. "Function to use for generating link descriptions from links.
  1371. When nil, the link location will be used. This function must take
  1372. two parameters: the first one is the link, the second one is the
  1373. description generated by `org-insert-link'. The function should
  1374. return the description to use."
  1375. :group 'org-link
  1376. :type 'function)
  1377. (defgroup org-link-store nil
  1378. "Options concerning storing links in Org-mode."
  1379. :tag "Org Store Link"
  1380. :group 'org-link)
  1381. (defcustom org-url-hexify-p t
  1382. "When non-nil, hexify URL when creating a link."
  1383. :type 'boolean
  1384. :version "24.3"
  1385. :group 'org-link-store)
  1386. (defcustom org-email-link-description-format "Email %c: %.30s"
  1387. "Format of the description part of a link to an email or usenet message.
  1388. The following %-escapes will be replaced by corresponding information:
  1389. %F full \"From\" field
  1390. %f name, taken from \"From\" field, address if no name
  1391. %T full \"To\" field
  1392. %t first name in \"To\" field, address if no name
  1393. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1394. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1395. %s subject
  1396. %d date
  1397. %m message-id.
  1398. You may use normal field width specification between the % and the letter.
  1399. This is for example useful to limit the length of the subject.
  1400. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1401. :group 'org-link-store
  1402. :type 'string)
  1403. (defcustom org-from-is-user-regexp
  1404. (let (r1 r2)
  1405. (when (and user-mail-address (not (string= user-mail-address "")))
  1406. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1407. (when (and user-full-name (not (string= user-full-name "")))
  1408. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1409. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1410. "Regexp matched against the \"From:\" header of an email or usenet message.
  1411. It should match if the message is from the user him/herself."
  1412. :group 'org-link-store
  1413. :type 'regexp)
  1414. (defcustom org-context-in-file-links t
  1415. "Non-nil means file links from `org-store-link' contain context.
  1416. A search string will be added to the file name with :: as separator and
  1417. used to find the context when the link is activated by the command
  1418. `org-open-at-point'. When this option is t, the entire active region
  1419. will be placed in the search string of the file link. If set to a
  1420. positive integer, only the first n lines of context will be stored.
  1421. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1422. negates this setting for the duration of the command."
  1423. :group 'org-link-store
  1424. :type '(choice boolean integer))
  1425. (defcustom org-keep-stored-link-after-insertion nil
  1426. "Non-nil means keep link in list for entire session.
  1427. The command `org-store-link' adds a link pointing to the current
  1428. location to an internal list. These links accumulate during a session.
  1429. The command `org-insert-link' can be used to insert links into any
  1430. Org-mode file (offering completion for all stored links). When this
  1431. option is nil, every link which has been inserted once using \\[org-insert-link]
  1432. will be removed from the list, to make completing the unused links
  1433. more efficient."
  1434. :group 'org-link-store
  1435. :type 'boolean)
  1436. (defgroup org-link-follow nil
  1437. "Options concerning following links in Org-mode."
  1438. :tag "Org Follow Link"
  1439. :group 'org-link)
  1440. (defcustom org-link-translation-function nil
  1441. "Function to translate links with different syntax to Org syntax.
  1442. This can be used to translate links created for example by the Planner
  1443. or emacs-wiki packages to Org syntax.
  1444. The function must accept two parameters, a TYPE containing the link
  1445. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1446. which is everything after the link protocol. It should return a cons
  1447. with possibly modified values of type and path.
  1448. Org contains a function for this, so if you set this variable to
  1449. `org-translate-link-from-planner', you should be able follow many
  1450. links created by planner."
  1451. :group 'org-link-follow
  1452. :type 'function)
  1453. (defcustom org-follow-link-hook nil
  1454. "Hook that is run after a link has been followed."
  1455. :group 'org-link-follow
  1456. :type 'hook)
  1457. (defcustom org-tab-follows-link nil
  1458. "Non-nil means on links TAB will follow the link.
  1459. Needs to be set before org.el is loaded.
  1460. This really should not be used, it does not make sense, and the
  1461. implementation is bad."
  1462. :group 'org-link-follow
  1463. :type 'boolean)
  1464. (defcustom org-return-follows-link nil
  1465. "Non-nil means on links RET will follow the link.
  1466. In tables, the special behavior of RET has precedence."
  1467. :group 'org-link-follow
  1468. :type 'boolean)
  1469. (defcustom org-mouse-1-follows-link
  1470. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1471. "Non-nil means mouse-1 on a link will follow the link.
  1472. A longer mouse click will still set point. Does not work on XEmacs.
  1473. Needs to be set before org.el is loaded."
  1474. :group 'org-link-follow
  1475. :type 'boolean)
  1476. (defcustom org-mark-ring-length 4
  1477. "Number of different positions to be recorded in the ring.
  1478. Changing this requires a restart of Emacs to work correctly."
  1479. :group 'org-link-follow
  1480. :type 'integer)
  1481. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1482. "Non-nil means internal links in Org files must exactly match a headline.
  1483. When nil, the link search tries to match a phrase with all words
  1484. in the search text."
  1485. :group 'org-link-follow
  1486. :version "24.1"
  1487. :type '(choice
  1488. (const :tag "Use fuzzy text search" nil)
  1489. (const :tag "Match only exact headline" t)
  1490. (const :tag "Match exact headline or query to create it"
  1491. query-to-create)))
  1492. (defcustom org-link-frame-setup
  1493. '((vm . vm-visit-folder-other-frame)
  1494. (vm-imap . vm-visit-imap-folder-other-frame)
  1495. (gnus . org-gnus-no-new-news)
  1496. (file . find-file-other-window)
  1497. (wl . wl-other-frame))
  1498. "Setup the frame configuration for following links.
  1499. When following a link with Emacs, it may often be useful to display
  1500. this link in another window or frame. This variable can be used to
  1501. set this up for the different types of links.
  1502. For VM, use any of
  1503. `vm-visit-folder'
  1504. `vm-visit-folder-other-window'
  1505. `vm-visit-folder-other-frame'
  1506. For Gnus, use any of
  1507. `gnus'
  1508. `gnus-other-frame'
  1509. `org-gnus-no-new-news'
  1510. For FILE, use any of
  1511. `find-file'
  1512. `find-file-other-window'
  1513. `find-file-other-frame'
  1514. For Wanderlust use any of
  1515. `wl'
  1516. `wl-other-frame'
  1517. For the calendar, use the variable `calendar-setup'.
  1518. For BBDB, it is currently only possible to display the matches in
  1519. another window."
  1520. :group 'org-link-follow
  1521. :type '(list
  1522. (cons (const vm)
  1523. (choice
  1524. (const vm-visit-folder)
  1525. (const vm-visit-folder-other-window)
  1526. (const vm-visit-folder-other-frame)))
  1527. (cons (const gnus)
  1528. (choice
  1529. (const gnus)
  1530. (const gnus-other-frame)
  1531. (const org-gnus-no-new-news)))
  1532. (cons (const file)
  1533. (choice
  1534. (const find-file)
  1535. (const find-file-other-window)
  1536. (const find-file-other-frame)))
  1537. (cons (const wl)
  1538. (choice
  1539. (const wl)
  1540. (const wl-other-frame)))))
  1541. (defcustom org-display-internal-link-with-indirect-buffer nil
  1542. "Non-nil means use indirect buffer to display infile links.
  1543. Activating internal links (from one location in a file to another location
  1544. in the same file) normally just jumps to the location. When the link is
  1545. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1546. is displayed in
  1547. another window. When this option is set, the other window actually displays
  1548. an indirect buffer clone of the current buffer, to avoid any visibility
  1549. changes to the current buffer."
  1550. :group 'org-link-follow
  1551. :type 'boolean)
  1552. (defcustom org-open-non-existing-files nil
  1553. "Non-nil means `org-open-file' will open non-existing files.
  1554. When nil, an error will be generated.
  1555. This variable applies only to external applications because they
  1556. might choke on non-existing files. If the link is to a file that
  1557. will be opened in Emacs, the variable is ignored."
  1558. :group 'org-link-follow
  1559. :type 'boolean)
  1560. (defcustom org-open-directory-means-index-dot-org nil
  1561. "Non-nil means a link to a directory really means to index.org.
  1562. When nil, following a directory link will run dired or open a finder/explorer
  1563. window on that directory."
  1564. :group 'org-link-follow
  1565. :type 'boolean)
  1566. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1567. "Function and arguments to call for following mailto links.
  1568. This is a list with the first element being a Lisp function, and the
  1569. remaining elements being arguments to the function. In string arguments,
  1570. %a will be replaced by the address, and %s will be replaced by the subject
  1571. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1572. :group 'org-link-follow
  1573. :type '(choice
  1574. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1575. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1576. (const :tag "message-mail" (message-mail "%a" "%s"))
  1577. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1578. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1579. "Non-nil means ask for confirmation before executing shell links.
  1580. Shell links can be dangerous: just think about a link
  1581. [[shell:rm -rf ~/*][Google Search]]
  1582. This link would show up in your Org-mode document as \"Google Search\",
  1583. but really it would remove your entire home directory.
  1584. Therefore we advise against setting this variable to nil.
  1585. Just change it to `y-or-n-p' if you want to confirm with a
  1586. single keystroke rather than having to type \"yes\"."
  1587. :group 'org-link-follow
  1588. :type '(choice
  1589. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1590. (const :tag "with y-or-n (faster)" y-or-n-p)
  1591. (const :tag "no confirmation (dangerous)" nil)))
  1592. (put 'org-confirm-shell-link-function
  1593. 'safe-local-variable
  1594. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1595. (defcustom org-confirm-shell-link-not-regexp ""
  1596. "A regexp to skip confirmation for shell links."
  1597. :group 'org-link-follow
  1598. :version "24.1"
  1599. :type 'regexp)
  1600. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1601. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1602. Elisp links can be dangerous: just think about a link
  1603. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1604. This link would show up in your Org-mode document as \"Google Search\",
  1605. but really it would remove your entire home directory.
  1606. Therefore we advise against setting this variable to nil.
  1607. Just change it to `y-or-n-p' if you want to confirm with a
  1608. single keystroke rather than having to type \"yes\"."
  1609. :group 'org-link-follow
  1610. :type '(choice
  1611. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1612. (const :tag "with y-or-n (faster)" y-or-n-p)
  1613. (const :tag "no confirmation (dangerous)" nil)))
  1614. (put 'org-confirm-shell-link-function
  1615. 'safe-local-variable
  1616. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1617. (defcustom org-confirm-elisp-link-not-regexp ""
  1618. "A regexp to skip confirmation for Elisp links."
  1619. :group 'org-link-follow
  1620. :version "24.1"
  1621. :type 'regexp)
  1622. (defconst org-file-apps-defaults-gnu
  1623. '((remote . emacs)
  1624. (system . mailcap)
  1625. (t . mailcap))
  1626. "Default file applications on a UNIX or GNU/Linux system.
  1627. See `org-file-apps'.")
  1628. (defconst org-file-apps-defaults-macosx
  1629. '((remote . emacs)
  1630. (t . "open %s")
  1631. (system . "open %s")
  1632. ("ps.gz" . "gv %s")
  1633. ("eps.gz" . "gv %s")
  1634. ("dvi" . "xdvi %s")
  1635. ("fig" . "xfig %s"))
  1636. "Default file applications on a MacOS X system.
  1637. The system \"open\" is known as a default, but we use X11 applications
  1638. for some files for which the OS does not have a good default.
  1639. See `org-file-apps'.")
  1640. (defconst org-file-apps-defaults-windowsnt
  1641. (list
  1642. '(remote . emacs)
  1643. (cons t
  1644. (list (if (featurep 'xemacs)
  1645. 'mswindows-shell-execute
  1646. 'w32-shell-execute)
  1647. "open" 'file))
  1648. (cons 'system
  1649. (list (if (featurep 'xemacs)
  1650. 'mswindows-shell-execute
  1651. 'w32-shell-execute)
  1652. "open" 'file)))
  1653. "Default file applications on a Windows NT system.
  1654. The system \"open\" is used for most files.
  1655. See `org-file-apps'.")
  1656. (defcustom org-file-apps
  1657. '(
  1658. (auto-mode . emacs)
  1659. ("\\.mm\\'" . default)
  1660. ("\\.x?html?\\'" . default)
  1661. ("\\.pdf\\'" . default)
  1662. )
  1663. "External applications for opening `file:path' items in a document.
  1664. Org-mode uses system defaults for different file types, but
  1665. you can use this variable to set the application for a given file
  1666. extension. The entries in this list are cons cells where the car identifies
  1667. files and the cdr the corresponding command. Possible values for the
  1668. file identifier are
  1669. \"string\" A string as a file identifier can be interpreted in different
  1670. ways, depending on its contents:
  1671. - Alphanumeric characters only:
  1672. Match links with this file extension.
  1673. Example: (\"pdf\" . \"evince %s\")
  1674. to open PDFs with evince.
  1675. - Regular expression: Match links where the
  1676. filename matches the regexp. If you want to
  1677. use groups here, use shy groups.
  1678. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1679. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1680. to open *.html and *.xhtml with firefox.
  1681. - Regular expression which contains (non-shy) groups:
  1682. Match links where the whole link, including \"::\", and
  1683. anything after that, matches the regexp.
  1684. In a custom command string, %1, %2, etc. are replaced with
  1685. the parts of the link that were matched by the groups.
  1686. For backwards compatibility, if a command string is given
  1687. that does not use any of the group matches, this case is
  1688. handled identically to the second one (i.e. match against
  1689. file name only).
  1690. In a custom lisp form, you can access the group matches with
  1691. (match-string n link).
  1692. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1693. to open [[file:document.pdf::5]] with evince at page 5.
  1694. `directory' Matches a directory
  1695. `remote' Matches a remote file, accessible through tramp or efs.
  1696. Remote files most likely should be visited through Emacs
  1697. because external applications cannot handle such paths.
  1698. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1699. so all files Emacs knows how to handle. Using this with
  1700. command `emacs' will open most files in Emacs. Beware that this
  1701. will also open html files inside Emacs, unless you add
  1702. (\"html\" . default) to the list as well.
  1703. t Default for files not matched by any of the other options.
  1704. `system' The system command to open files, like `open' on Windows
  1705. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1706. that will be selected if you call `C-c C-o' with a double
  1707. \\[universal-argument] \\[universal-argument] prefix.
  1708. Possible values for the command are:
  1709. `emacs' The file will be visited by the current Emacs process.
  1710. `default' Use the default application for this file type, which is the
  1711. association for t in the list, most likely in the system-specific
  1712. part.
  1713. This can be used to overrule an unwanted setting in the
  1714. system-specific variable.
  1715. `system' Use the system command for opening files, like \"open\".
  1716. This command is specified by the entry whose car is `system'.
  1717. Most likely, the system-specific version of this variable
  1718. does define this command, but you can overrule/replace it
  1719. here.
  1720. string A command to be executed by a shell; %s will be replaced
  1721. by the path to the file.
  1722. sexp A Lisp form which will be evaluated. The file path will
  1723. be available in the Lisp variable `file'.
  1724. For more examples, see the system specific constants
  1725. `org-file-apps-defaults-macosx'
  1726. `org-file-apps-defaults-windowsnt'
  1727. `org-file-apps-defaults-gnu'."
  1728. :group 'org-link-follow
  1729. :type '(repeat
  1730. (cons (choice :value ""
  1731. (string :tag "Extension")
  1732. (const :tag "System command to open files" system)
  1733. (const :tag "Default for unrecognized files" t)
  1734. (const :tag "Remote file" remote)
  1735. (const :tag "Links to a directory" directory)
  1736. (const :tag "Any files that have Emacs modes"
  1737. auto-mode))
  1738. (choice :value ""
  1739. (const :tag "Visit with Emacs" emacs)
  1740. (const :tag "Use default" default)
  1741. (const :tag "Use the system command" system)
  1742. (string :tag "Command")
  1743. (sexp :tag "Lisp form")))))
  1744. (defcustom org-doi-server-url "http://dx.doi.org/"
  1745. "The URL of the DOI server."
  1746. :type 'string
  1747. :version "24.3"
  1748. :group 'org-link-follow)
  1749. (defgroup org-refile nil
  1750. "Options concerning refiling entries in Org-mode."
  1751. :tag "Org Refile"
  1752. :group 'org)
  1753. (defcustom org-directory "~/org"
  1754. "Directory with org files.
  1755. This is just a default location to look for Org files. There is no need
  1756. at all to put your files into this directory. It is only used in the
  1757. following situations:
  1758. 1. When a capture template specifies a target file that is not an
  1759. absolute path. The path will then be interpreted relative to
  1760. `org-directory'
  1761. 2. When a capture note is filed away in an interactive way (when exiting the
  1762. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1763. with `org-directory' as the default path."
  1764. :group 'org-refile
  1765. :group 'org-remember
  1766. :group 'org-capture
  1767. :type 'directory)
  1768. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1769. "Default target for storing notes.
  1770. Used as a fall back file for org-remember.el and org-capture.el, for
  1771. templates that do not specify a target file."
  1772. :group 'org-refile
  1773. :group 'org-remember
  1774. :group 'org-capture
  1775. :type '(choice
  1776. (const :tag "Default from remember-data-file" nil)
  1777. file))
  1778. (defcustom org-goto-interface 'outline
  1779. "The default interface to be used for `org-goto'.
  1780. Allowed values are:
  1781. outline The interface shows an outline of the relevant file
  1782. and the correct heading is found by moving through
  1783. the outline or by searching with incremental search.
  1784. outline-path-completion Headlines in the current buffer are offered via
  1785. completion. This is the interface also used by
  1786. the refile command."
  1787. :group 'org-refile
  1788. :type '(choice
  1789. (const :tag "Outline" outline)
  1790. (const :tag "Outline-path-completion" outline-path-completion)))
  1791. (defcustom org-goto-max-level 5
  1792. "Maximum target level when running `org-goto' with refile interface."
  1793. :group 'org-refile
  1794. :type 'integer)
  1795. (defcustom org-reverse-note-order nil
  1796. "Non-nil means store new notes at the beginning of a file or entry.
  1797. When nil, new notes will be filed to the end of a file or entry.
  1798. This can also be a list with cons cells of regular expressions that
  1799. are matched against file names, and values."
  1800. :group 'org-remember
  1801. :group 'org-capture
  1802. :group 'org-refile
  1803. :type '(choice
  1804. (const :tag "Reverse always" t)
  1805. (const :tag "Reverse never" nil)
  1806. (repeat :tag "By file name regexp"
  1807. (cons regexp boolean))))
  1808. (defcustom org-log-refile nil
  1809. "Information to record when a task is refiled.
  1810. Possible values are:
  1811. nil Don't add anything
  1812. time Add a time stamp to the task
  1813. note Prompt for a note and add it with template `org-log-note-headings'
  1814. This option can also be set with on a per-file-basis with
  1815. #+STARTUP: nologrefile
  1816. #+STARTUP: logrefile
  1817. #+STARTUP: lognoterefile
  1818. You can have local logging settings for a subtree by setting the LOGGING
  1819. property to one or more of these keywords.
  1820. When bulk-refiling from the agenda, the value `note' is forbidden and
  1821. will temporarily be changed to `time'."
  1822. :group 'org-refile
  1823. :group 'org-progress
  1824. :version "24.1"
  1825. :type '(choice
  1826. (const :tag "No logging" nil)
  1827. (const :tag "Record timestamp" time)
  1828. (const :tag "Record timestamp with note." note)))
  1829. (defcustom org-refile-targets nil
  1830. "Targets for refiling entries with \\[org-refile].
  1831. This is a list of cons cells. Each cell contains:
  1832. - a specification of the files to be considered, either a list of files,
  1833. or a symbol whose function or variable value will be used to retrieve
  1834. a file name or a list of file names. If you use `org-agenda-files' for
  1835. that, all agenda files will be scanned for targets. Nil means consider
  1836. headings in the current buffer.
  1837. - A specification of how to find candidate refile targets. This may be
  1838. any of:
  1839. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1840. This tag has to be present in all target headlines, inheritance will
  1841. not be considered.
  1842. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1843. todo keyword.
  1844. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1845. headlines that are refiling targets.
  1846. - a cons cell (:level . N). Any headline of level N is considered a target.
  1847. Note that, when `org-odd-levels-only' is set, level corresponds to
  1848. order in hierarchy, not to the number of stars.
  1849. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1850. Note that, when `org-odd-levels-only' is set, level corresponds to
  1851. order in hierarchy, not to the number of stars.
  1852. Each element of this list generates a set of possible targets.
  1853. The union of these sets is presented (with completion) to
  1854. the user by `org-refile'.
  1855. You can set the variable `org-refile-target-verify-function' to a function
  1856. to verify each headline found by the simple criteria above.
  1857. When this variable is nil, all top-level headlines in the current buffer
  1858. are used, equivalent to the value `((nil . (:level . 1))'."
  1859. :group 'org-refile
  1860. :type '(repeat
  1861. (cons
  1862. (choice :value org-agenda-files
  1863. (const :tag "All agenda files" org-agenda-files)
  1864. (const :tag "Current buffer" nil)
  1865. (function) (variable) (file))
  1866. (choice :tag "Identify target headline by"
  1867. (cons :tag "Specific tag" (const :value :tag) (string))
  1868. (cons :tag "TODO keyword" (const :value :todo) (string))
  1869. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1870. (cons :tag "Level number" (const :value :level) (integer))
  1871. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1872. (defcustom org-refile-target-verify-function nil
  1873. "Function to verify if the headline at point should be a refile target.
  1874. The function will be called without arguments, with point at the
  1875. beginning of the headline. It should return t and leave point
  1876. where it is if the headline is a valid target for refiling.
  1877. If the target should not be selected, the function must return nil.
  1878. In addition to this, it may move point to a place from where the search
  1879. should be continued. For example, the function may decide that the entire
  1880. subtree of the current entry should be excluded and move point to the end
  1881. of the subtree."
  1882. :group 'org-refile
  1883. :type 'function)
  1884. (defcustom org-refile-use-cache nil
  1885. "Non-nil means cache refile targets to speed up the process.
  1886. The cache for a particular file will be updated automatically when
  1887. the buffer has been killed, or when any of the marker used for flagging
  1888. refile targets no longer points at a live buffer.
  1889. If you have added new entries to a buffer that might themselves be targets,
  1890. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1891. find that easier, `C-u C-u C-u C-c C-w'."
  1892. :group 'org-refile
  1893. :version "24.1"
  1894. :type 'boolean)
  1895. (defcustom org-refile-use-outline-path nil
  1896. "Non-nil means provide refile targets as paths.
  1897. So a level 3 headline will be available as level1/level2/level3.
  1898. When the value is `file', also include the file name (without directory)
  1899. into the path. In this case, you can also stop the completion after
  1900. the file name, to get entries inserted as top level in the file.
  1901. When `full-file-path', include the full file path."
  1902. :group 'org-refile
  1903. :type '(choice
  1904. (const :tag "Not" nil)
  1905. (const :tag "Yes" t)
  1906. (const :tag "Start with file name" file)
  1907. (const :tag "Start with full file path" full-file-path)))
  1908. (defcustom org-outline-path-complete-in-steps t
  1909. "Non-nil means complete the outline path in hierarchical steps.
  1910. When Org-mode uses the refile interface to select an outline path
  1911. \(see variable `org-refile-use-outline-path'), the completion of
  1912. the path can be done is a single go, or if can be done in steps down
  1913. the headline hierarchy. Going in steps is probably the best if you
  1914. do not use a special completion package like `ido' or `icicles'.
  1915. However, when using these packages, going in one step can be very
  1916. fast, while still showing the whole path to the entry."
  1917. :group 'org-refile
  1918. :type 'boolean)
  1919. (defcustom org-refile-allow-creating-parent-nodes nil
  1920. "Non-nil means allow to create new nodes as refile targets.
  1921. New nodes are then created by adding \"/new node name\" to the completion
  1922. of an existing node. When the value of this variable is `confirm',
  1923. new node creation must be confirmed by the user (recommended)
  1924. When nil, the completion must match an existing entry.
  1925. Note that, if the new heading is not seen by the criteria
  1926. listed in `org-refile-targets', multiple instances of the same
  1927. heading would be created by trying again to file under the new
  1928. heading."
  1929. :group 'org-refile
  1930. :type '(choice
  1931. (const :tag "Never" nil)
  1932. (const :tag "Always" t)
  1933. (const :tag "Prompt for confirmation" confirm)))
  1934. (defcustom org-refile-active-region-within-subtree nil
  1935. "Non-nil means also refile active region within a subtree.
  1936. By default `org-refile' doesn't allow refiling regions if they
  1937. don't contain a set of subtrees, but it might be convenient to
  1938. do so sometimes: in that case, the first line of the region is
  1939. converted to a headline before refiling."
  1940. :group 'org-refile
  1941. :version "24.1"
  1942. :type 'boolean)
  1943. (defgroup org-todo nil
  1944. "Options concerning TODO items in Org-mode."
  1945. :tag "Org TODO"
  1946. :group 'org)
  1947. (defgroup org-progress nil
  1948. "Options concerning Progress logging in Org-mode."
  1949. :tag "Org Progress"
  1950. :group 'org-time)
  1951. (defvar org-todo-interpretation-widgets
  1952. '((:tag "Sequence (cycling hits every state)" sequence)
  1953. (:tag "Type (cycling directly to DONE)" type))
  1954. "The available interpretation symbols for customizing `org-todo-keywords'.
  1955. Interested libraries should add to this list.")
  1956. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1957. "List of TODO entry keyword sequences and their interpretation.
  1958. \\<org-mode-map>This is a list of sequences.
  1959. Each sequence starts with a symbol, either `sequence' or `type',
  1960. indicating if the keywords should be interpreted as a sequence of
  1961. action steps, or as different types of TODO items. The first
  1962. keywords are states requiring action - these states will select a headline
  1963. for inclusion into the global TODO list Org-mode produces. If one of
  1964. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1965. signify that no further action is necessary. If \"|\" is not found,
  1966. the last keyword is treated as the only DONE state of the sequence.
  1967. The command \\[org-todo] cycles an entry through these states, and one
  1968. additional state where no keyword is present. For details about this
  1969. cycling, see the manual.
  1970. TODO keywords and interpretation can also be set on a per-file basis with
  1971. the special #+SEQ_TODO and #+TYP_TODO lines.
  1972. Each keyword can optionally specify a character for fast state selection
  1973. \(in combination with the variable `org-use-fast-todo-selection')
  1974. and specifiers for state change logging, using the same syntax that
  1975. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1976. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1977. indicates to record a time stamp each time this state is selected.
  1978. Each keyword may also specify if a timestamp or a note should be
  1979. recorded when entering or leaving the state, by adding additional
  1980. characters in the parenthesis after the keyword. This looks like this:
  1981. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1982. record only the time of the state change. With X and Y being either
  1983. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1984. Y when leaving the state if and only if the *target* state does not
  1985. define X. You may omit any of the fast-selection key or X or /Y,
  1986. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1987. For backward compatibility, this variable may also be just a list
  1988. of keywords. In this case the interpretation (sequence or type) will be
  1989. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1990. :group 'org-todo
  1991. :group 'org-keywords
  1992. :type '(choice
  1993. (repeat :tag "Old syntax, just keywords"
  1994. (string :tag "Keyword"))
  1995. (repeat :tag "New syntax"
  1996. (cons
  1997. (choice
  1998. :tag "Interpretation"
  1999. ;;Quick and dirty way to see
  2000. ;;`org-todo-interpretations'. This takes the
  2001. ;;place of item arguments
  2002. :convert-widget
  2003. (lambda (widget)
  2004. (widget-put widget
  2005. :args (mapcar
  2006. #'(lambda (x)
  2007. (widget-convert
  2008. (cons 'const x)))
  2009. org-todo-interpretation-widgets))
  2010. widget))
  2011. (repeat
  2012. (string :tag "Keyword"))))))
  2013. (defvar org-todo-keywords-1 nil
  2014. "All TODO and DONE keywords active in a buffer.")
  2015. (make-variable-buffer-local 'org-todo-keywords-1)
  2016. (defvar org-todo-keywords-for-agenda nil)
  2017. (defvar org-done-keywords-for-agenda nil)
  2018. (defvar org-drawers-for-agenda nil)
  2019. (defvar org-todo-keyword-alist-for-agenda nil)
  2020. (defvar org-tag-alist-for-agenda nil
  2021. "Alist of all tags from all agenda files.")
  2022. (defvar org-tag-groups-alist-for-agenda nil
  2023. "Alist of all groups tags from all current agenda files.")
  2024. (defvar org-tag-groups-alist nil)
  2025. (make-variable-buffer-local 'org-tag-groups-alist)
  2026. (defvar org-agenda-contributing-files nil)
  2027. (defvar org-not-done-keywords nil)
  2028. (make-variable-buffer-local 'org-not-done-keywords)
  2029. (defvar org-done-keywords nil)
  2030. (make-variable-buffer-local 'org-done-keywords)
  2031. (defvar org-todo-heads nil)
  2032. (make-variable-buffer-local 'org-todo-heads)
  2033. (defvar org-todo-sets nil)
  2034. (make-variable-buffer-local 'org-todo-sets)
  2035. (defvar org-todo-log-states nil)
  2036. (make-variable-buffer-local 'org-todo-log-states)
  2037. (defvar org-todo-kwd-alist nil)
  2038. (make-variable-buffer-local 'org-todo-kwd-alist)
  2039. (defvar org-todo-key-alist nil)
  2040. (make-variable-buffer-local 'org-todo-key-alist)
  2041. (defvar org-todo-key-trigger nil)
  2042. (make-variable-buffer-local 'org-todo-key-trigger)
  2043. (defcustom org-todo-interpretation 'sequence
  2044. "Controls how TODO keywords are interpreted.
  2045. This variable is in principle obsolete and is only used for
  2046. backward compatibility, if the interpretation of todo keywords is
  2047. not given already in `org-todo-keywords'. See that variable for
  2048. more information."
  2049. :group 'org-todo
  2050. :group 'org-keywords
  2051. :type '(choice (const sequence)
  2052. (const type)))
  2053. (defcustom org-use-fast-todo-selection t
  2054. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2055. This variable describes if and under what circumstances the cycling
  2056. mechanism for TODO keywords will be replaced by a single-key, direct
  2057. selection scheme.
  2058. When nil, fast selection is never used.
  2059. When the symbol `prefix', it will be used when `org-todo' is called
  2060. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2061. `C-u t' in an agenda buffer.
  2062. When t, fast selection is used by default. In this case, the prefix
  2063. argument forces cycling instead.
  2064. In all cases, the special interface is only used if access keys have
  2065. actually been assigned by the user, i.e. if keywords in the configuration
  2066. are followed by a letter in parenthesis, like TODO(t)."
  2067. :group 'org-todo
  2068. :type '(choice
  2069. (const :tag "Never" nil)
  2070. (const :tag "By default" t)
  2071. (const :tag "Only with C-u C-c C-t" prefix)))
  2072. (defcustom org-provide-todo-statistics t
  2073. "Non-nil means update todo statistics after insert and toggle.
  2074. ALL-HEADLINES means update todo statistics by including headlines
  2075. with no TODO keyword as well, counting them as not done.
  2076. A list of TODO keywords means the same, but skip keywords that are
  2077. not in this list.
  2078. When this is set, todo statistics is updated in the parent of the
  2079. current entry each time a todo state is changed."
  2080. :group 'org-todo
  2081. :type '(choice
  2082. (const :tag "Yes, only for TODO entries" t)
  2083. (const :tag "Yes, including all entries" 'all-headlines)
  2084. (repeat :tag "Yes, for TODOs in this list"
  2085. (string :tag "TODO keyword"))
  2086. (other :tag "No TODO statistics" nil)))
  2087. (defcustom org-hierarchical-todo-statistics t
  2088. "Non-nil means TODO statistics covers just direct children.
  2089. When nil, all entries in the subtree are considered.
  2090. This has only an effect if `org-provide-todo-statistics' is set.
  2091. To set this to nil for only a single subtree, use a COOKIE_DATA
  2092. property and include the word \"recursive\" into the value."
  2093. :group 'org-todo
  2094. :type 'boolean)
  2095. (defcustom org-after-todo-state-change-hook nil
  2096. "Hook which is run after the state of a TODO item was changed.
  2097. The new state (a string with a TODO keyword, or nil) is available in the
  2098. Lisp variable `org-state'."
  2099. :group 'org-todo
  2100. :type 'hook)
  2101. (defvar org-blocker-hook nil
  2102. "Hook for functions that are allowed to block a state change.
  2103. Functions in this hook should not modify the buffer.
  2104. Each function gets as its single argument a property list,
  2105. see `org-trigger-hook' for more information about this list.
  2106. If any of the functions in this hook returns nil, the state change
  2107. is blocked.")
  2108. (defvar org-trigger-hook nil
  2109. "Hook for functions that are triggered by a state change.
  2110. Each function gets as its single argument a property list with at
  2111. least the following elements:
  2112. (:type type-of-change :position pos-at-entry-start
  2113. :from old-state :to new-state)
  2114. Depending on the type, more properties may be present.
  2115. This mechanism is currently implemented for:
  2116. TODO state changes
  2117. ------------------
  2118. :type todo-state-change
  2119. :from previous state (keyword as a string), or nil, or a symbol
  2120. 'todo' or 'done', to indicate the general type of state.
  2121. :to new state, like in :from")
  2122. (defcustom org-enforce-todo-dependencies nil
  2123. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2124. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2125. be blocked if any prior sibling is not yet done.
  2126. Finally, if the parent is blocked because of ordered siblings of its own,
  2127. the child will also be blocked."
  2128. :set (lambda (var val)
  2129. (set var val)
  2130. (if val
  2131. (add-hook 'org-blocker-hook
  2132. 'org-block-todo-from-children-or-siblings-or-parent)
  2133. (remove-hook 'org-blocker-hook
  2134. 'org-block-todo-from-children-or-siblings-or-parent)))
  2135. :group 'org-todo
  2136. :type 'boolean)
  2137. (defcustom org-enforce-todo-checkbox-dependencies nil
  2138. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2139. When this is nil, checkboxes have no influence on switching TODO states.
  2140. When non-nil, you first need to check off all check boxes before the TODO
  2141. entry can be switched to DONE.
  2142. This variable needs to be set before org.el is loaded, and you need to
  2143. restart Emacs after a change to make the change effective. The only way
  2144. to change is while Emacs is running is through the customize interface."
  2145. :set (lambda (var val)
  2146. (set var val)
  2147. (if val
  2148. (add-hook 'org-blocker-hook
  2149. 'org-block-todo-from-checkboxes)
  2150. (remove-hook 'org-blocker-hook
  2151. 'org-block-todo-from-checkboxes)))
  2152. :group 'org-todo
  2153. :type 'boolean)
  2154. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2155. "Non-nil means inserting a TODO heading is treated as state change.
  2156. So when the command \\[org-insert-todo-heading] is used, state change
  2157. logging will apply if appropriate. When nil, the new TODO item will
  2158. be inserted directly, and no logging will take place."
  2159. :group 'org-todo
  2160. :type 'boolean)
  2161. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2162. "Non-nil means switching TODO states with S-cursor counts as state change.
  2163. This is the default behavior. However, setting this to nil allows a
  2164. convenient way to select a TODO state and bypass any logging associated
  2165. with that."
  2166. :group 'org-todo
  2167. :type 'boolean)
  2168. (defcustom org-todo-state-tags-triggers nil
  2169. "Tag changes that should be triggered by TODO state changes.
  2170. This is a list. Each entry is
  2171. (state-change (tag . flag) .......)
  2172. State-change can be a string with a state, and empty string to indicate the
  2173. state that has no TODO keyword, or it can be one of the symbols `todo'
  2174. or `done', meaning any not-done or done state, respectively."
  2175. :group 'org-todo
  2176. :group 'org-tags
  2177. :type '(repeat
  2178. (cons (choice :tag "When changing to"
  2179. (const :tag "Not-done state" todo)
  2180. (const :tag "Done state" done)
  2181. (string :tag "State"))
  2182. (repeat
  2183. (cons :tag "Tag action"
  2184. (string :tag "Tag")
  2185. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2186. (defcustom org-log-done nil
  2187. "Information to record when a task moves to the DONE state.
  2188. Possible values are:
  2189. nil Don't add anything, just change the keyword
  2190. time Add a time stamp to the task
  2191. note Prompt for a note and add it with template `org-log-note-headings'
  2192. This option can also be set with on a per-file-basis with
  2193. #+STARTUP: nologdone
  2194. #+STARTUP: logdone
  2195. #+STARTUP: lognotedone
  2196. You can have local logging settings for a subtree by setting the LOGGING
  2197. property to one or more of these keywords."
  2198. :group 'org-todo
  2199. :group 'org-progress
  2200. :type '(choice
  2201. (const :tag "No logging" nil)
  2202. (const :tag "Record CLOSED timestamp" time)
  2203. (const :tag "Record CLOSED timestamp with note." note)))
  2204. ;; Normalize old uses of org-log-done.
  2205. (cond
  2206. ((eq org-log-done t) (setq org-log-done 'time))
  2207. ((and (listp org-log-done) (memq 'done org-log-done))
  2208. (setq org-log-done 'note)))
  2209. (defcustom org-log-reschedule nil
  2210. "Information to record when the scheduling date of a tasks is modified.
  2211. Possible values are:
  2212. nil Don't add anything, just change the date
  2213. time Add a time stamp to the task
  2214. note Prompt for a note and add it with template `org-log-note-headings'
  2215. This option can also be set with on a per-file-basis with
  2216. #+STARTUP: nologreschedule
  2217. #+STARTUP: logreschedule
  2218. #+STARTUP: lognotereschedule"
  2219. :group 'org-todo
  2220. :group 'org-progress
  2221. :type '(choice
  2222. (const :tag "No logging" nil)
  2223. (const :tag "Record timestamp" time)
  2224. (const :tag "Record timestamp with note." note)))
  2225. (defcustom org-log-redeadline nil
  2226. "Information to record when the deadline date of a tasks is modified.
  2227. Possible values are:
  2228. nil Don't add anything, just change the date
  2229. time Add a time stamp to the task
  2230. note Prompt for a note and add it with template `org-log-note-headings'
  2231. This option can also be set with on a per-file-basis with
  2232. #+STARTUP: nologredeadline
  2233. #+STARTUP: logredeadline
  2234. #+STARTUP: lognoteredeadline
  2235. You can have local logging settings for a subtree by setting the LOGGING
  2236. property to one or more of these keywords."
  2237. :group 'org-todo
  2238. :group 'org-progress
  2239. :type '(choice
  2240. (const :tag "No logging" nil)
  2241. (const :tag "Record timestamp" time)
  2242. (const :tag "Record timestamp with note." note)))
  2243. (defcustom org-log-note-clock-out nil
  2244. "Non-nil means record a note when clocking out of an item.
  2245. This can also be configured on a per-file basis by adding one of
  2246. the following lines anywhere in the buffer:
  2247. #+STARTUP: lognoteclock-out
  2248. #+STARTUP: nolognoteclock-out"
  2249. :group 'org-todo
  2250. :group 'org-progress
  2251. :type 'boolean)
  2252. (defcustom org-log-done-with-time t
  2253. "Non-nil means the CLOSED time stamp will contain date and time.
  2254. When nil, only the date will be recorded."
  2255. :group 'org-progress
  2256. :type 'boolean)
  2257. (defcustom org-log-note-headings
  2258. '((done . "CLOSING NOTE %t")
  2259. (state . "State %-12s from %-12S %t")
  2260. (note . "Note taken on %t")
  2261. (reschedule . "Rescheduled from %S on %t")
  2262. (delschedule . "Not scheduled, was %S on %t")
  2263. (redeadline . "New deadline from %S on %t")
  2264. (deldeadline . "Removed deadline, was %S on %t")
  2265. (refile . "Refiled on %t")
  2266. (clock-out . ""))
  2267. "Headings for notes added to entries.
  2268. The value is an alist, with the car being a symbol indicating the note
  2269. context, and the cdr is the heading to be used. The heading may also be the
  2270. empty string.
  2271. %t in the heading will be replaced by a time stamp.
  2272. %T will be an active time stamp instead the default inactive one
  2273. %d will be replaced by a short-format time stamp.
  2274. %D will be replaced by an active short-format time stamp.
  2275. %s will be replaced by the new TODO state, in double quotes.
  2276. %S will be replaced by the old TODO state, in double quotes.
  2277. %u will be replaced by the user name.
  2278. %U will be replaced by the full user name.
  2279. In fact, it is not a good idea to change the `state' entry, because
  2280. agenda log mode depends on the format of these entries."
  2281. :group 'org-todo
  2282. :group 'org-progress
  2283. :type '(list :greedy t
  2284. (cons (const :tag "Heading when closing an item" done) string)
  2285. (cons (const :tag
  2286. "Heading when changing todo state (todo sequence only)"
  2287. state) string)
  2288. (cons (const :tag "Heading when just taking a note" note) string)
  2289. (cons (const :tag "Heading when clocking out" clock-out) string)
  2290. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2291. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2292. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2293. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2294. (cons (const :tag "Heading when refiling" refile) string)))
  2295. (unless (assq 'note org-log-note-headings)
  2296. (push '(note . "%t") org-log-note-headings))
  2297. (defcustom org-log-into-drawer nil
  2298. "Non-nil means insert state change notes and time stamps into a drawer.
  2299. When nil, state changes notes will be inserted after the headline and
  2300. any scheduling and clock lines, but not inside a drawer.
  2301. The value of this variable should be the name of the drawer to use.
  2302. LOGBOOK is proposed as the default drawer for this purpose, you can
  2303. also set this to a string to define the drawer of your choice.
  2304. A value of t is also allowed, representing \"LOGBOOK\".
  2305. A value of t or nil can also be set with on a per-file-basis with
  2306. #+STARTUP: logdrawer
  2307. #+STARTUP: nologdrawer
  2308. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2309. will be ignored.
  2310. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2311. a subtree."
  2312. :group 'org-todo
  2313. :group 'org-progress
  2314. :type '(choice
  2315. (const :tag "Not into a drawer" nil)
  2316. (const :tag "LOGBOOK" t)
  2317. (string :tag "Other")))
  2318. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2319. (defun org-log-into-drawer ()
  2320. "Return the value of `org-log-into-drawer', but let properties overrule.
  2321. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2322. used instead of the default value."
  2323. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2324. (cond
  2325. ((not p) org-log-into-drawer)
  2326. ((equal p "nil") nil)
  2327. ((equal p "t") "LOGBOOK")
  2328. (t p))))
  2329. (defcustom org-log-state-notes-insert-after-drawers nil
  2330. "Non-nil means insert state change notes after any drawers in entry.
  2331. Only the drawers that *immediately* follow the headline and the
  2332. deadline/scheduled line are skipped.
  2333. When nil, insert notes right after the heading and perhaps the line
  2334. with deadline/scheduling if present.
  2335. This variable will have no effect if `org-log-into-drawer' is
  2336. set."
  2337. :group 'org-todo
  2338. :group 'org-progress
  2339. :type 'boolean)
  2340. (defcustom org-log-states-order-reversed t
  2341. "Non-nil means the latest state note will be directly after heading.
  2342. When nil, the state change notes will be ordered according to time.
  2343. This option can also be set with on a per-file-basis with
  2344. #+STARTUP: logstatesreversed
  2345. #+STARTUP: nologstatesreversed"
  2346. :group 'org-todo
  2347. :group 'org-progress
  2348. :type 'boolean)
  2349. (defcustom org-todo-repeat-to-state nil
  2350. "The TODO state to which a repeater should return the repeating task.
  2351. By default this is the first task in a TODO sequence, or the previous state
  2352. in a TODO_TYP set. But you can specify another task here.
  2353. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2354. :group 'org-todo
  2355. :version "24.1"
  2356. :type '(choice (const :tag "Head of sequence" nil)
  2357. (string :tag "Specific state")))
  2358. (defcustom org-log-repeat 'time
  2359. "Non-nil means record moving through the DONE state when triggering repeat.
  2360. An auto-repeating task is immediately switched back to TODO when
  2361. marked DONE. If you are not logging state changes (by adding \"@\"
  2362. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2363. record a closing note, there will be no record of the task moving
  2364. through DONE. This variable forces taking a note anyway.
  2365. nil Don't force a record
  2366. time Record a time stamp
  2367. note Prompt for a note and add it with template `org-log-note-headings'
  2368. This option can also be set with on a per-file-basis with
  2369. #+STARTUP: nologrepeat
  2370. #+STARTUP: logrepeat
  2371. #+STARTUP: lognoterepeat
  2372. You can have local logging settings for a subtree by setting the LOGGING
  2373. property to one or more of these keywords."
  2374. :group 'org-todo
  2375. :group 'org-progress
  2376. :type '(choice
  2377. (const :tag "Don't force a record" nil)
  2378. (const :tag "Force recording the DONE state" time)
  2379. (const :tag "Force recording a note with the DONE state" note)))
  2380. (defgroup org-priorities nil
  2381. "Priorities in Org-mode."
  2382. :tag "Org Priorities"
  2383. :group 'org-todo)
  2384. (defcustom org-enable-priority-commands t
  2385. "Non-nil means priority commands are active.
  2386. When nil, these commands will be disabled, so that you never accidentally
  2387. set a priority."
  2388. :group 'org-priorities
  2389. :type 'boolean)
  2390. (defcustom org-highest-priority ?A
  2391. "The highest priority of TODO items. A character like ?A, ?B etc.
  2392. Must have a smaller ASCII number than `org-lowest-priority'."
  2393. :group 'org-priorities
  2394. :type 'character)
  2395. (defcustom org-lowest-priority ?C
  2396. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2397. Must have a larger ASCII number than `org-highest-priority'."
  2398. :group 'org-priorities
  2399. :type 'character)
  2400. (defcustom org-default-priority ?B
  2401. "The default priority of TODO items.
  2402. This is the priority an item gets if no explicit priority is given.
  2403. When starting to cycle on an empty priority the first step in the cycle
  2404. depends on `org-priority-start-cycle-with-default'. The resulting first
  2405. step priority must not exceed the range from `org-highest-priority' to
  2406. `org-lowest-priority' which means that `org-default-priority' has to be
  2407. in this range exclusive or inclusive the range boundaries. Else the
  2408. first step refuses to set the default and the second will fall back
  2409. to (depending on the command used) the highest or lowest priority."
  2410. :group 'org-priorities
  2411. :type 'character)
  2412. (defcustom org-priority-start-cycle-with-default t
  2413. "Non-nil means start with default priority when starting to cycle.
  2414. When this is nil, the first step in the cycle will be (depending on the
  2415. command used) one higher or lower than the default priority.
  2416. See also `org-default-priority'."
  2417. :group 'org-priorities
  2418. :type 'boolean)
  2419. (defcustom org-get-priority-function nil
  2420. "Function to extract the priority from a string.
  2421. The string is normally the headline. If this is nil Org computes the
  2422. priority from the priority cookie like [#A] in the headline. It returns
  2423. an integer, increasing by 1000 for each priority level.
  2424. The user can set a different function here, which should take a string
  2425. as an argument and return the numeric priority."
  2426. :group 'org-priorities
  2427. :version "24.1"
  2428. :type 'function)
  2429. (defgroup org-time nil
  2430. "Options concerning time stamps and deadlines in Org-mode."
  2431. :tag "Org Time"
  2432. :group 'org)
  2433. (defcustom org-insert-labeled-timestamps-at-point nil
  2434. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2435. When nil, these labeled time stamps are forces into the second line of an
  2436. entry, just after the headline. When scheduling from the global TODO list,
  2437. the time stamp will always be forced into the second line."
  2438. :group 'org-time
  2439. :type 'boolean)
  2440. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2441. "Formats for `format-time-string' which are used for time stamps.
  2442. It is not recommended to change this constant.")
  2443. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2444. "Number of minutes to round time stamps to.
  2445. These are two values, the first applies when first creating a time stamp.
  2446. The second applies when changing it with the commands `S-up' and `S-down'.
  2447. When changing the time stamp, this means that it will change in steps
  2448. of N minutes, as given by the second value.
  2449. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2450. numbers should be factors of 60, so for example 5, 10, 15.
  2451. When this is larger than 1, you can still force an exact time stamp by using
  2452. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2453. and by using a prefix arg to `S-up/down' to specify the exact number
  2454. of minutes to shift."
  2455. :group 'org-time
  2456. :get #'(lambda (var) ; Make sure both elements are there
  2457. (if (integerp (default-value var))
  2458. (list (default-value var) 5)
  2459. (default-value var)))
  2460. :type '(list
  2461. (integer :tag "when inserting times")
  2462. (integer :tag "when modifying times")))
  2463. ;; Normalize old customizations of this variable.
  2464. (when (integerp org-time-stamp-rounding-minutes)
  2465. (setq org-time-stamp-rounding-minutes
  2466. (list org-time-stamp-rounding-minutes
  2467. org-time-stamp-rounding-minutes)))
  2468. (defcustom org-display-custom-times nil
  2469. "Non-nil means overlay custom formats over all time stamps.
  2470. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2471. To turn this on on a per-file basis, insert anywhere in the file:
  2472. #+STARTUP: customtime"
  2473. :group 'org-time
  2474. :set 'set-default
  2475. :type 'sexp)
  2476. (make-variable-buffer-local 'org-display-custom-times)
  2477. (defcustom org-time-stamp-custom-formats
  2478. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2479. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2480. These are overlaid over the default ISO format if the variable
  2481. `org-display-custom-times' is set. Time like %H:%M should be at the
  2482. end of the second format. The custom formats are also honored by export
  2483. commands, if custom time display is turned on at the time of export."
  2484. :group 'org-time
  2485. :type 'sexp)
  2486. (defun org-time-stamp-format (&optional long inactive)
  2487. "Get the right format for a time string."
  2488. (let ((f (if long (cdr org-time-stamp-formats)
  2489. (car org-time-stamp-formats))))
  2490. (if inactive
  2491. (concat "[" (substring f 1 -1) "]")
  2492. f)))
  2493. (defcustom org-time-clocksum-format
  2494. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2495. "The format string used when creating CLOCKSUM lines.
  2496. This is also used when Org mode generates a time duration.
  2497. The value can be a single format string containing two
  2498. %-sequences, which will be filled with the number of hours and
  2499. minutes in that order.
  2500. Alternatively, the value can be a plist associating any of the
  2501. keys :years, :months, :weeks, :days, :hours or :minutes with
  2502. format strings. The time duration is formatted using only the
  2503. time components that are needed and concatenating the results.
  2504. If a time unit in absent, it falls back to the next smallest
  2505. unit.
  2506. The keys :require-years, :require-months, :require-days,
  2507. :require-weeks, :require-hours, :require-minutes are also
  2508. meaningful. A non-nil value for these keys indicates that the
  2509. corresponding time component should always be included, even if
  2510. its value is 0.
  2511. For example,
  2512. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2513. :require-minutes t)
  2514. means durations longer than a day will be expressed in days,
  2515. hours and minutes, and durations less than a day will always be
  2516. expressed in hours and minutes (even for durations less than an
  2517. hour).
  2518. The value
  2519. \(:days \"%dd\" :minutes \"%dm\")
  2520. means durations longer than a day will be expressed in days and
  2521. minutes, and durations less than a day will be expressed entirely
  2522. in minutes (even for durations longer than an hour)."
  2523. :group 'org-time
  2524. :group 'org-clock
  2525. :version "24.4"
  2526. :package-version '(Org . "8.0")
  2527. :type '(choice (string :tag "Format string")
  2528. (set :tag "Plist"
  2529. (group :inline t (const :tag "Years" :years)
  2530. (string :tag "Format string"))
  2531. (group :inline t
  2532. (const :tag "Always show years" :require-years)
  2533. (const t))
  2534. (group :inline t (const :tag "Months" :months)
  2535. (string :tag "Format string"))
  2536. (group :inline t
  2537. (const :tag "Always show months" :require-months)
  2538. (const t))
  2539. (group :inline t (const :tag "Weeks" :weeks)
  2540. (string :tag "Format string"))
  2541. (group :inline t
  2542. (const :tag "Always show weeks" :require-weeks)
  2543. (const t))
  2544. (group :inline t (const :tag "Days" :days)
  2545. (string :tag "Format string"))
  2546. (group :inline t
  2547. (const :tag "Always show days" :require-days)
  2548. (const t))
  2549. (group :inline t (const :tag "Hours" :hours)
  2550. (string :tag "Format string"))
  2551. (group :inline t
  2552. (const :tag "Always show hours" :require-hours)
  2553. (const t))
  2554. (group :inline t (const :tag "Minutes" :minutes)
  2555. (string :tag "Format string"))
  2556. (group :inline t
  2557. (const :tag "Always show minutes" :require-minutes)
  2558. (const t)))))
  2559. (defcustom org-time-clocksum-use-fractional nil
  2560. "When non-nil, \\[org-clock-display] uses fractional times.
  2561. See `org-time-clocksum-format' for more on time clock formats."
  2562. :group 'org-time
  2563. :group 'org-clock
  2564. :version "24.3"
  2565. :type 'boolean)
  2566. (defcustom org-time-clocksum-use-effort-durations nil
  2567. "When non-nil, \\[org-clock-display] uses effort durations.
  2568. E.g. by default, one day is considered to be a 8 hours effort,
  2569. so a task that has been clocked for 16 hours will be displayed
  2570. as during 2 days in the clock display or in the clocktable.
  2571. See `org-effort-durations' on how to set effort durations
  2572. and `org-time-clocksum-format' for more on time clock formats."
  2573. :group 'org-time
  2574. :group 'org-clock
  2575. :version "24.4"
  2576. :package-version '(Org . "8.0")
  2577. :type 'boolean)
  2578. (defcustom org-time-clocksum-fractional-format "%.2f"
  2579. "The format string used when creating CLOCKSUM lines,
  2580. or when Org mode generates a time duration, if
  2581. `org-time-clocksum-use-fractional' is enabled.
  2582. The value can be a single format string containing one
  2583. %-sequence, which will be filled with the number of hours as
  2584. a float.
  2585. Alternatively, the value can be a plist associating any of the
  2586. keys :years, :months, :weeks, :days, :hours or :minutes with
  2587. a format string. The time duration is formatted using the
  2588. largest time unit which gives a non-zero integer part. If all
  2589. specified formats have zero integer part, the smallest time unit
  2590. is used."
  2591. :group 'org-time
  2592. :type '(choice (string :tag "Format string")
  2593. (set (group :inline t (const :tag "Years" :years)
  2594. (string :tag "Format string"))
  2595. (group :inline t (const :tag "Months" :months)
  2596. (string :tag "Format string"))
  2597. (group :inline t (const :tag "Weeks" :weeks)
  2598. (string :tag "Format string"))
  2599. (group :inline t (const :tag "Days" :days)
  2600. (string :tag "Format string"))
  2601. (group :inline t (const :tag "Hours" :hours)
  2602. (string :tag "Format string"))
  2603. (group :inline t (const :tag "Minutes" :minutes)
  2604. (string :tag "Format string")))))
  2605. (defcustom org-deadline-warning-days 14
  2606. "Number of days before expiration during which a deadline becomes active.
  2607. This variable governs the display in sparse trees and in the agenda.
  2608. When 0 or negative, it means use this number (the absolute value of it)
  2609. even if a deadline has a different individual lead time specified.
  2610. Custom commands can set this variable in the options section."
  2611. :group 'org-time
  2612. :group 'org-agenda-daily/weekly
  2613. :type 'integer)
  2614. (defcustom org-scheduled-delay-days 0
  2615. "Number of days before a scheduled item becomes active.
  2616. This variable governs the display in sparse trees and in the agenda.
  2617. The default value (i.e. 0) means: don't delay scheduled item.
  2618. When negative, it means use this number (the absolute value of it)
  2619. even if a scheduled item has a different individual delay time
  2620. specified.
  2621. Custom commands can set this variable in the options section."
  2622. :group 'org-time
  2623. :group 'org-agenda-daily/weekly
  2624. :version "24.4"
  2625. :package-version '(Org . "8.0")
  2626. :type 'integer)
  2627. (defcustom org-read-date-prefer-future t
  2628. "Non-nil means assume future for incomplete date input from user.
  2629. This affects the following situations:
  2630. 1. The user gives a month but not a year.
  2631. For example, if it is April and you enter \"feb 2\", this will be read
  2632. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2633. 2. The user gives a day, but no month.
  2634. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2635. read this as the third of *next* month. However, if you enter \"17\",
  2636. it will be considered as *this* month.
  2637. If you set this variable to the symbol `time', then also the following
  2638. will work:
  2639. 3. If the user gives a time.
  2640. If the time is before now, it will be interpreted as tomorrow.
  2641. Currently none of this works for ISO week specifications.
  2642. When this option is nil, the current day, month and year will always be
  2643. used as defaults.
  2644. See also `org-agenda-jump-prefer-future'."
  2645. :group 'org-time
  2646. :type '(choice
  2647. (const :tag "Never" nil)
  2648. (const :tag "Check month and day" t)
  2649. (const :tag "Check month, day, and time" time)))
  2650. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2651. "Should the agenda jump command prefer the future for incomplete dates?
  2652. The default is to do the same as configured in `org-read-date-prefer-future'.
  2653. But you can also set a deviating value here.
  2654. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2655. :group 'org-agenda
  2656. :group 'org-time
  2657. :version "24.1"
  2658. :type '(choice
  2659. (const :tag "Use org-read-date-prefer-future"
  2660. org-read-date-prefer-future)
  2661. (const :tag "Never" nil)
  2662. (const :tag "Always" t)))
  2663. (defcustom org-read-date-force-compatible-dates t
  2664. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2665. Depending on the system Emacs is running on, certain dates cannot
  2666. be represented with the type used internally to represent time.
  2667. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2668. correctly. Some systems allow for earlier dates, some for later,
  2669. some for both. One way to find out it to insert any date into an
  2670. Org buffer, putting the cursor on the year and hitting S-up and
  2671. S-down to test the range.
  2672. When this variable is set to t, the date/time prompt will not let
  2673. you specify dates outside the 1970-2037 range, so it is certain that
  2674. these dates will work in whatever version of Emacs you are
  2675. running, and also that you can move a file from one Emacs implementation
  2676. to another. WHenever Org is forcing the year for you, it will display
  2677. a message and beep.
  2678. When this variable is nil, Org will check if the date is
  2679. representable in the specific Emacs implementation you are using.
  2680. If not, it will force a year, usually the current year, and beep
  2681. to remind you. Currently this setting is not recommended because
  2682. the likelihood that you will open your Org files in an Emacs that
  2683. has limited date range is not negligible.
  2684. A workaround for this problem is to use diary sexp dates for time
  2685. stamps outside of this range."
  2686. :group 'org-time
  2687. :version "24.1"
  2688. :type 'boolean)
  2689. (defcustom org-read-date-display-live t
  2690. "Non-nil means display current interpretation of date prompt live.
  2691. This display will be in an overlay, in the minibuffer."
  2692. :group 'org-time
  2693. :type 'boolean)
  2694. (defcustom org-read-date-popup-calendar t
  2695. "Non-nil means pop up a calendar when prompting for a date.
  2696. In the calendar, the date can be selected with mouse-1. However, the
  2697. minibuffer will also be active, and you can simply enter the date as well.
  2698. When nil, only the minibuffer will be available."
  2699. :group 'org-time
  2700. :type 'boolean)
  2701. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2702. 'org-read-date-popup-calendar)
  2703. (make-obsolete-variable
  2704. 'org-read-date-minibuffer-setup-hook
  2705. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2706. (defcustom org-read-date-minibuffer-setup-hook nil
  2707. "Hook to be used to set up keys for the date/time interface.
  2708. Add key definitions to `minibuffer-local-map', which will be a
  2709. temporary copy.
  2710. WARNING: This option is obsolete, you should use
  2711. `org-read-date-minibuffer-local-map' to set up keys."
  2712. :group 'org-time
  2713. :type 'hook)
  2714. (defcustom org-extend-today-until 0
  2715. "The hour when your day really ends. Must be an integer.
  2716. This has influence for the following applications:
  2717. - When switching the agenda to \"today\". It it is still earlier than
  2718. the time given here, the day recognized as TODAY is actually yesterday.
  2719. - When a date is read from the user and it is still before the time given
  2720. here, the current date and time will be assumed to be yesterday, 23:59.
  2721. Also, timestamps inserted in capture templates follow this rule.
  2722. IMPORTANT: This is a feature whose implementation is and likely will
  2723. remain incomplete. Really, it is only here because past midnight seems to
  2724. be the favorite working time of John Wiegley :-)"
  2725. :group 'org-time
  2726. :type 'integer)
  2727. (defcustom org-use-effective-time nil
  2728. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2729. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2730. \"effective time\" of any timestamps between midnight and 8am will be
  2731. 23:59 of the previous day."
  2732. :group 'org-time
  2733. :version "24.1"
  2734. :type 'boolean)
  2735. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2736. "When non-nil, use the last clock out time for `org-todo'.
  2737. Note that this option has precedence over the combined use of
  2738. `org-use-effective-time' and `org-extend-today-until'."
  2739. :group 'org-time
  2740. :version "24.4"
  2741. :package-version '(Org . "8.0")
  2742. :type 'boolean)
  2743. (defcustom org-edit-timestamp-down-means-later nil
  2744. "Non-nil means S-down will increase the time in a time stamp.
  2745. When nil, S-up will increase."
  2746. :group 'org-time
  2747. :type 'boolean)
  2748. (defcustom org-calendar-follow-timestamp-change t
  2749. "Non-nil means make the calendar window follow timestamp changes.
  2750. When a timestamp is modified and the calendar window is visible, it will be
  2751. moved to the new date."
  2752. :group 'org-time
  2753. :type 'boolean)
  2754. (defgroup org-tags nil
  2755. "Options concerning tags in Org-mode."
  2756. :tag "Org Tags"
  2757. :group 'org)
  2758. (defcustom org-tag-alist nil
  2759. "List of tags allowed in Org-mode files.
  2760. When this list is nil, Org-mode will base TAG input on what is already in the
  2761. buffer.
  2762. The value of this variable is an alist, the car of each entry must be a
  2763. keyword as a string, the cdr may be a character that is used to select
  2764. that tag through the fast-tag-selection interface.
  2765. See the manual for details."
  2766. :group 'org-tags
  2767. :type '(repeat
  2768. (choice
  2769. (cons (string :tag "Tag name")
  2770. (character :tag "Access char"))
  2771. (list :tag "Start radio group"
  2772. (const :startgroup)
  2773. (option (string :tag "Group description")))
  2774. (list :tag "Group tags delimiter"
  2775. (const :grouptags))
  2776. (list :tag "End radio group"
  2777. (const :endgroup)
  2778. (option (string :tag "Group description")))
  2779. (const :tag "New line" (:newline)))))
  2780. (defcustom org-tag-persistent-alist nil
  2781. "List of tags that will always appear in all Org-mode files.
  2782. This is in addition to any in buffer settings or customizations
  2783. of `org-tag-alist'.
  2784. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2785. The value of this variable is an alist, the car of each entry must be a
  2786. keyword as a string, the cdr may be a character that is used to select
  2787. that tag through the fast-tag-selection interface.
  2788. See the manual for details.
  2789. To disable these tags on a per-file basis, insert anywhere in the file:
  2790. #+STARTUP: noptag"
  2791. :group 'org-tags
  2792. :type '(repeat
  2793. (choice
  2794. (cons (string :tag "Tag name")
  2795. (character :tag "Access char"))
  2796. (const :tag "Start radio group" (:startgroup))
  2797. (const :tag "Group tags delimiter" (:grouptags))
  2798. (const :tag "End radio group" (:endgroup))
  2799. (const :tag "New line" (:newline)))))
  2800. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2801. "If non-nil, always offer completion for all tags of all agenda files.
  2802. Instead of customizing this variable directly, you might want to
  2803. set it locally for capture buffers, because there no list of
  2804. tags in that file can be created dynamically (there are none).
  2805. (add-hook 'org-capture-mode-hook
  2806. (lambda ()
  2807. (set (make-local-variable
  2808. 'org-complete-tags-always-offer-all-agenda-tags)
  2809. t)))"
  2810. :group 'org-tags
  2811. :version "24.1"
  2812. :type 'boolean)
  2813. (defvar org-file-tags nil
  2814. "List of tags that can be inherited by all entries in the file.
  2815. The tags will be inherited if the variable `org-use-tag-inheritance'
  2816. says they should be.
  2817. This variable is populated from #+FILETAGS lines.")
  2818. (defcustom org-use-fast-tag-selection 'auto
  2819. "Non-nil means use fast tag selection scheme.
  2820. This is a special interface to select and deselect tags with single keys.
  2821. When nil, fast selection is never used.
  2822. When the symbol `auto', fast selection is used if and only if selection
  2823. characters for tags have been configured, either through the variable
  2824. `org-tag-alist' or through a #+TAGS line in the buffer.
  2825. When t, fast selection is always used and selection keys are assigned
  2826. automatically if necessary."
  2827. :group 'org-tags
  2828. :type '(choice
  2829. (const :tag "Always" t)
  2830. (const :tag "Never" nil)
  2831. (const :tag "When selection characters are configured" 'auto)))
  2832. (defcustom org-fast-tag-selection-single-key nil
  2833. "Non-nil means fast tag selection exits after first change.
  2834. When nil, you have to press RET to exit it.
  2835. During fast tag selection, you can toggle this flag with `C-c'.
  2836. This variable can also have the value `expert'. In this case, the window
  2837. displaying the tags menu is not even shown, until you press C-c again."
  2838. :group 'org-tags
  2839. :type '(choice
  2840. (const :tag "No" nil)
  2841. (const :tag "Yes" t)
  2842. (const :tag "Expert" expert)))
  2843. (defvar org-fast-tag-selection-include-todo nil
  2844. "Non-nil means fast tags selection interface will also offer TODO states.
  2845. This is an undocumented feature, you should not rely on it.")
  2846. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2847. "The column to which tags should be indented in a headline.
  2848. If this number is positive, it specifies the column. If it is negative,
  2849. it means that the tags should be flushright to that column. For example,
  2850. -80 works well for a normal 80 character screen.
  2851. When 0, place tags directly after headline text, with only one space in
  2852. between."
  2853. :group 'org-tags
  2854. :type 'integer)
  2855. (defcustom org-auto-align-tags t
  2856. "Non-nil keeps tags aligned when modifying headlines.
  2857. Some operations (i.e. demoting) change the length of a headline and
  2858. therefore shift the tags around. With this option turned on, after
  2859. each such operation the tags are again aligned to `org-tags-column'."
  2860. :group 'org-tags
  2861. :type 'boolean)
  2862. (defcustom org-use-tag-inheritance t
  2863. "Non-nil means tags in levels apply also for sublevels.
  2864. When nil, only the tags directly given in a specific line apply there.
  2865. This may also be a list of tags that should be inherited, or a regexp that
  2866. matches tags that should be inherited. Additional control is possible
  2867. with the variable `org-tags-exclude-from-inheritance' which gives an
  2868. explicit list of tags to be excluded from inheritance, even if the value of
  2869. `org-use-tag-inheritance' would select it for inheritance.
  2870. If this option is t, a match early-on in a tree can lead to a large
  2871. number of matches in the subtree when constructing the agenda or creating
  2872. a sparse tree. If you only want to see the first match in a tree during
  2873. a search, check out the variable `org-tags-match-list-sublevels'."
  2874. :group 'org-tags
  2875. :type '(choice
  2876. (const :tag "Not" nil)
  2877. (const :tag "Always" t)
  2878. (repeat :tag "Specific tags" (string :tag "Tag"))
  2879. (regexp :tag "Tags matched by regexp")))
  2880. (defcustom org-tags-exclude-from-inheritance nil
  2881. "List of tags that should never be inherited.
  2882. This is a way to exclude a few tags from inheritance. For way to do
  2883. the opposite, to actively allow inheritance for selected tags,
  2884. see the variable `org-use-tag-inheritance'."
  2885. :group 'org-tags
  2886. :type '(repeat (string :tag "Tag")))
  2887. (defun org-tag-inherit-p (tag)
  2888. "Check if TAG is one that should be inherited."
  2889. (cond
  2890. ((member tag org-tags-exclude-from-inheritance) nil)
  2891. ((eq org-use-tag-inheritance t) t)
  2892. ((not org-use-tag-inheritance) nil)
  2893. ((stringp org-use-tag-inheritance)
  2894. (string-match org-use-tag-inheritance tag))
  2895. ((listp org-use-tag-inheritance)
  2896. (member tag org-use-tag-inheritance))
  2897. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2898. (defcustom org-tags-match-list-sublevels t
  2899. "Non-nil means list also sublevels of headlines matching a search.
  2900. This variable applies to tags/property searches, and also to stuck
  2901. projects because this search is based on a tags match as well.
  2902. When set to the symbol `indented', sublevels are indented with
  2903. leading dots.
  2904. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2905. the sublevels of a headline matching a tag search often also match
  2906. the same search. Listing all of them can create very long lists.
  2907. Setting this variable to nil causes subtrees of a match to be skipped.
  2908. This variable is semi-obsolete and probably should always be true. It
  2909. is better to limit inheritance to certain tags using the variables
  2910. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2911. :group 'org-tags
  2912. :type '(choice
  2913. (const :tag "No, don't list them" nil)
  2914. (const :tag "Yes, do list them" t)
  2915. (const :tag "List them, indented with leading dots" indented)))
  2916. (defcustom org-tags-sort-function nil
  2917. "When set, tags are sorted using this function as a comparator."
  2918. :group 'org-tags
  2919. :type '(choice
  2920. (const :tag "No sorting" nil)
  2921. (const :tag "Alphabetical" string<)
  2922. (const :tag "Reverse alphabetical" string>)
  2923. (function :tag "Custom function" nil)))
  2924. (defvar org-tags-history nil
  2925. "History of minibuffer reads for tags.")
  2926. (defvar org-last-tags-completion-table nil
  2927. "The last used completion table for tags.")
  2928. (defvar org-after-tags-change-hook nil
  2929. "Hook that is run after the tags in a line have changed.")
  2930. (defgroup org-properties nil
  2931. "Options concerning properties in Org-mode."
  2932. :tag "Org Properties"
  2933. :group 'org)
  2934. (defcustom org-property-format "%-10s %s"
  2935. "How property key/value pairs should be formatted by `indent-line'.
  2936. When `indent-line' hits a property definition, it will format the line
  2937. according to this format, mainly to make sure that the values are
  2938. lined-up with respect to each other."
  2939. :group 'org-properties
  2940. :type 'string)
  2941. (defcustom org-properties-postprocess-alist nil
  2942. "Alist of properties and functions to adjust inserted values.
  2943. Elements of this alist must be of the form
  2944. ([string] [function])
  2945. where [string] must be a property name and [function] must be a
  2946. lambda expression: this lambda expression must take one argument,
  2947. the value to adjust, and return the new value as a string.
  2948. For example, this element will allow the property \"Remaining\"
  2949. to be updated wrt the relation between the \"Effort\" property
  2950. and the clock summary:
  2951. ((\"Remaining\" (lambda(value)
  2952. (let ((clocksum (org-clock-sum-current-item))
  2953. (effort (org-duration-string-to-minutes
  2954. (org-entry-get (point) \"Effort\"))))
  2955. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2956. :group 'org-properties
  2957. :version "24.1"
  2958. :type '(alist :key-type (string :tag "Property")
  2959. :value-type (function :tag "Function")))
  2960. (defcustom org-use-property-inheritance nil
  2961. "Non-nil means properties apply also for sublevels.
  2962. This setting is chiefly used during property searches. Turning it on can
  2963. cause significant overhead when doing a search, which is why it is not
  2964. on by default.
  2965. When nil, only the properties directly given in the current entry count.
  2966. When t, every property is inherited. The value may also be a list of
  2967. properties that should have inheritance, or a regular expression matching
  2968. properties that should be inherited.
  2969. However, note that some special properties use inheritance under special
  2970. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2971. and the properties ending in \"_ALL\" when they are used as descriptor
  2972. for valid values of a property.
  2973. Note for programmers:
  2974. When querying an entry with `org-entry-get', you can control if inheritance
  2975. should be used. By default, `org-entry-get' looks only at the local
  2976. properties. You can request inheritance by setting the inherit argument
  2977. to t (to force inheritance) or to `selective' (to respect the setting
  2978. in this variable)."
  2979. :group 'org-properties
  2980. :type '(choice
  2981. (const :tag "Not" nil)
  2982. (const :tag "Always" t)
  2983. (repeat :tag "Specific properties" (string :tag "Property"))
  2984. (regexp :tag "Properties matched by regexp")))
  2985. (defun org-property-inherit-p (property)
  2986. "Check if PROPERTY is one that should be inherited."
  2987. (cond
  2988. ((eq org-use-property-inheritance t) t)
  2989. ((not org-use-property-inheritance) nil)
  2990. ((stringp org-use-property-inheritance)
  2991. (string-match org-use-property-inheritance property))
  2992. ((listp org-use-property-inheritance)
  2993. (member property org-use-property-inheritance))
  2994. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2995. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2996. "The default column format, if no other format has been defined.
  2997. This variable can be set on the per-file basis by inserting a line
  2998. #+COLUMNS: %25ITEM ....."
  2999. :group 'org-properties
  3000. :type 'string)
  3001. (defcustom org-columns-ellipses ".."
  3002. "The ellipses to be used when a field in column view is truncated.
  3003. When this is the empty string, as many characters as possible are shown,
  3004. but then there will be no visual indication that the field has been truncated.
  3005. When this is a string of length N, the last N characters of a truncated
  3006. field are replaced by this string. If the column is narrower than the
  3007. ellipses string, only part of the ellipses string will be shown."
  3008. :group 'org-properties
  3009. :type 'string)
  3010. (defcustom org-columns-modify-value-for-display-function nil
  3011. "Function that modifies values for display in column view.
  3012. For example, it can be used to cut out a certain part from a time stamp.
  3013. The function must take 2 arguments:
  3014. column-title The title of the column (*not* the property name)
  3015. value The value that should be modified.
  3016. The function should return the value that should be displayed,
  3017. or nil if the normal value should be used."
  3018. :group 'org-properties
  3019. :type 'function)
  3020. (defcustom org-effort-property "Effort"
  3021. "The property that is being used to keep track of effort estimates.
  3022. Effort estimates given in this property need to have the format H:MM."
  3023. :group 'org-properties
  3024. :group 'org-progress
  3025. :type '(string :tag "Property"))
  3026. (defconst org-global-properties-fixed
  3027. '(("VISIBILITY_ALL" . "folded children content all")
  3028. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3029. "List of property/value pairs that can be inherited by any entry.
  3030. These are fixed values, for the preset properties. The user variable
  3031. that can be used to add to this list is `org-global-properties'.
  3032. The entries in this list are cons cells where the car is a property
  3033. name and cdr is a string with the value. If the value represents
  3034. multiple items like an \"_ALL\" property, separate the items by
  3035. spaces.")
  3036. (defcustom org-global-properties nil
  3037. "List of property/value pairs that can be inherited by any entry.
  3038. This list will be combined with the constant `org-global-properties-fixed'.
  3039. The entries in this list are cons cells where the car is a property
  3040. name and cdr is a string with the value.
  3041. You can set buffer-local values for the same purpose in the variable
  3042. `org-file-properties' this by adding lines like
  3043. #+PROPERTY: NAME VALUE"
  3044. :group 'org-properties
  3045. :type '(repeat
  3046. (cons (string :tag "Property")
  3047. (string :tag "Value"))))
  3048. (defvar org-file-properties nil
  3049. "List of property/value pairs that can be inherited by any entry.
  3050. Valid for the current buffer.
  3051. This variable is populated from #+PROPERTY lines.")
  3052. (make-variable-buffer-local 'org-file-properties)
  3053. (defgroup org-agenda nil
  3054. "Options concerning agenda views in Org-mode."
  3055. :tag "Org Agenda"
  3056. :group 'org)
  3057. (defvar org-category nil
  3058. "Variable used by org files to set a category for agenda display.
  3059. Such files should use a file variable to set it, for example
  3060. # -*- mode: org; org-category: \"ELisp\"
  3061. or contain a special line
  3062. #+CATEGORY: ELisp
  3063. If the file does not specify a category, then file's base name
  3064. is used instead.")
  3065. (make-variable-buffer-local 'org-category)
  3066. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3067. (defcustom org-agenda-files nil
  3068. "The files to be used for agenda display.
  3069. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3070. \\[org-remove-file]. You can also use customize to edit the list.
  3071. If an entry is a directory, all files in that directory that are matched by
  3072. `org-agenda-file-regexp' will be part of the file list.
  3073. If the value of the variable is not a list but a single file name, then
  3074. the list of agenda files is actually stored and maintained in that file, one
  3075. agenda file per line. In this file paths can be given relative to
  3076. `org-directory'. Tilde expansion and environment variable substitution
  3077. are also made."
  3078. :group 'org-agenda
  3079. :type '(choice
  3080. (repeat :tag "List of files and directories" file)
  3081. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3082. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3083. "Regular expression to match files for `org-agenda-files'.
  3084. If any element in the list in that variable contains a directory instead
  3085. of a normal file, all files in that directory that are matched by this
  3086. regular expression will be included."
  3087. :group 'org-agenda
  3088. :type 'regexp)
  3089. (defcustom org-agenda-text-search-extra-files nil
  3090. "List of extra files to be searched by text search commands.
  3091. These files will be search in addition to the agenda files by the
  3092. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3093. Note that these files will only be searched for text search commands,
  3094. not for the other agenda views like todo lists, tag searches or the weekly
  3095. agenda. This variable is intended to list notes and possibly archive files
  3096. that should also be searched by these two commands.
  3097. In fact, if the first element in the list is the symbol `agenda-archives',
  3098. than all archive files of all agenda files will be added to the search
  3099. scope."
  3100. :group 'org-agenda
  3101. :type '(set :greedy t
  3102. (const :tag "Agenda Archives" agenda-archives)
  3103. (repeat :inline t (file))))
  3104. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3105. 'org-agenda-text-search-extra-files)
  3106. (defcustom org-agenda-skip-unavailable-files nil
  3107. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3108. A nil value means to remove them, after a query, from the list."
  3109. :group 'org-agenda
  3110. :type 'boolean)
  3111. (defcustom org-calendar-to-agenda-key [?c]
  3112. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3113. The command `org-calendar-goto-agenda' will be bound to this key. The
  3114. default is the character `c' because then `c' can be used to switch back and
  3115. forth between agenda and calendar."
  3116. :group 'org-agenda
  3117. :type 'sexp)
  3118. (defcustom org-calendar-insert-diary-entry-key [?i]
  3119. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3120. This option is irrelevant until `org-agenda-diary-file' has been configured
  3121. to point to an Org-mode file. When that is the case, the command
  3122. `org-agenda-diary-entry' will be bound to the key given here, by default
  3123. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3124. if you want to continue doing this, you need to change this to a different
  3125. key."
  3126. :group 'org-agenda
  3127. :type 'sexp)
  3128. (defcustom org-agenda-diary-file 'diary-file
  3129. "File to which to add new entries with the `i' key in agenda and calendar.
  3130. When this is the symbol `diary-file', the functionality in the Emacs
  3131. calendar will be used to add entries to the `diary-file'. But when this
  3132. points to a file, `org-agenda-diary-entry' will be used instead."
  3133. :group 'org-agenda
  3134. :type '(choice
  3135. (const :tag "The standard Emacs diary file" diary-file)
  3136. (file :tag "Special Org file diary entries")))
  3137. (eval-after-load "calendar"
  3138. '(progn
  3139. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3140. 'org-calendar-goto-agenda)
  3141. (add-hook 'calendar-mode-hook
  3142. (lambda ()
  3143. (unless (eq org-agenda-diary-file 'diary-file)
  3144. (define-key calendar-mode-map
  3145. org-calendar-insert-diary-entry-key
  3146. 'org-agenda-diary-entry))))))
  3147. (defgroup org-latex nil
  3148. "Options for embedding LaTeX code into Org-mode."
  3149. :tag "Org LaTeX"
  3150. :group 'org)
  3151. (defcustom org-format-latex-options
  3152. '(:foreground default :background default :scale 1.0
  3153. :html-foreground "Black" :html-background "Transparent"
  3154. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3155. "Options for creating images from LaTeX fragments.
  3156. This is a property list with the following properties:
  3157. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3158. `default' means use the foreground of the default face.
  3159. `auto' means use the foreground from the text face.
  3160. :background the background color, or \"Transparent\".
  3161. `default' means use the background of the default face.
  3162. `auto' means use the background from the text face.
  3163. :scale a scaling factor for the size of the images, to get more pixels
  3164. :html-foreground, :html-background, :html-scale
  3165. the same numbers for HTML export.
  3166. :matchers a list indicating which matchers should be used to
  3167. find LaTeX fragments. Valid members of this list are:
  3168. \"begin\" find environments
  3169. \"$1\" find single characters surrounded by $.$
  3170. \"$\" find math expressions surrounded by $...$
  3171. \"$$\" find math expressions surrounded by $$....$$
  3172. \"\\(\" find math expressions surrounded by \\(...\\)
  3173. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3174. :group 'org-latex
  3175. :type 'plist)
  3176. (defcustom org-format-latex-signal-error t
  3177. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3178. When nil, just push out a message."
  3179. :group 'org-latex
  3180. :version "24.1"
  3181. :type 'boolean)
  3182. (defcustom org-latex-to-mathml-jar-file nil
  3183. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3184. Use this to specify additional executable file say a jar file.
  3185. When using MathToWeb as the converter, specify the full-path to
  3186. your mathtoweb.jar file."
  3187. :group 'org-latex
  3188. :version "24.1"
  3189. :type '(choice
  3190. (const :tag "None" nil)
  3191. (file :tag "JAR file" :must-match t)))
  3192. (defcustom org-latex-to-mathml-convert-command nil
  3193. "Command to convert LaTeX fragments to MathML.
  3194. Replace format-specifiers in the command as noted below and use
  3195. `shell-command' to convert LaTeX to MathML.
  3196. %j: Executable file in fully expanded form as specified by
  3197. `org-latex-to-mathml-jar-file'.
  3198. %I: Input LaTeX file in fully expanded form
  3199. %o: Output MathML file
  3200. This command is used by `org-create-math-formula'.
  3201. When using MathToWeb as the converter, set this to
  3202. \"java -jar %j -unicode -force -df %o %I\"."
  3203. :group 'org-latex
  3204. :version "24.1"
  3205. :type '(choice
  3206. (const :tag "None" nil)
  3207. (string :tag "\nShell command")))
  3208. (defcustom org-latex-create-formula-image-program 'dvipng
  3209. "Program to convert LaTeX fragments with.
  3210. dvipng Process the LaTeX fragments to dvi file, then convert
  3211. dvi files to png files using dvipng.
  3212. This will also include processing of non-math environments.
  3213. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3214. to convert pdf files to png files"
  3215. :group 'org-latex
  3216. :version "24.1"
  3217. :type '(choice
  3218. (const :tag "dvipng" dvipng)
  3219. (const :tag "imagemagick" imagemagick)))
  3220. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3221. "Path to store latex preview images.
  3222. A relative path here creates many directories relative to the
  3223. processed org files paths. An absolute path puts all preview
  3224. images at the same place."
  3225. :group 'org-latex
  3226. :version "24.3"
  3227. :type 'string)
  3228. (defun org-format-latex-mathml-available-p ()
  3229. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3230. (save-match-data
  3231. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3232. org-latex-to-mathml-convert-command)
  3233. (let ((executable (car (split-string
  3234. org-latex-to-mathml-convert-command))))
  3235. (when (executable-find executable)
  3236. (if (string-match
  3237. "%j" org-latex-to-mathml-convert-command)
  3238. (file-readable-p org-latex-to-mathml-jar-file)
  3239. t))))))
  3240. (defcustom org-format-latex-header "\\documentclass{article}
  3241. \\usepackage[usenames]{color}
  3242. \\usepackage{amsmath}
  3243. \\usepackage[mathscr]{eucal}
  3244. \\pagestyle{empty} % do not remove
  3245. \[PACKAGES]
  3246. \[DEFAULT-PACKAGES]
  3247. % The settings below are copied from fullpage.sty
  3248. \\setlength{\\textwidth}{\\paperwidth}
  3249. \\addtolength{\\textwidth}{-3cm}
  3250. \\setlength{\\oddsidemargin}{1.5cm}
  3251. \\addtolength{\\oddsidemargin}{-2.54cm}
  3252. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3253. \\setlength{\\textheight}{\\paperheight}
  3254. \\addtolength{\\textheight}{-\\headheight}
  3255. \\addtolength{\\textheight}{-\\headsep}
  3256. \\addtolength{\\textheight}{-\\footskip}
  3257. \\addtolength{\\textheight}{-3cm}
  3258. \\setlength{\\topmargin}{1.5cm}
  3259. \\addtolength{\\topmargin}{-2.54cm}"
  3260. "The document header used for processing LaTeX fragments.
  3261. It is imperative that this header make sure that no page number
  3262. appears on the page. The package defined in the variables
  3263. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3264. will either replace the placeholder \"[PACKAGES]\" in this
  3265. header, or they will be appended."
  3266. :group 'org-latex
  3267. :type 'string)
  3268. (defun org-set-packages-alist (var val)
  3269. "Set the packages alist and make sure it has 3 elements per entry."
  3270. (set var (mapcar (lambda (x)
  3271. (if (and (consp x) (= (length x) 2))
  3272. (list (car x) (nth 1 x) t)
  3273. x))
  3274. val)))
  3275. (defun org-get-packages-alist (var)
  3276. "Get the packages alist and make sure it has 3 elements per entry."
  3277. (mapcar (lambda (x)
  3278. (if (and (consp x) (= (length x) 2))
  3279. (list (car x) (nth 1 x) t)
  3280. x))
  3281. (default-value var)))
  3282. (defcustom org-latex-default-packages-alist
  3283. '(("AUTO" "inputenc" t)
  3284. ("T1" "fontenc" t)
  3285. ("" "fixltx2e" nil)
  3286. ("" "graphicx" t)
  3287. ("" "longtable" nil)
  3288. ("" "float" nil)
  3289. ("" "wrapfig" nil)
  3290. ("" "soul" t)
  3291. ("" "textcomp" t)
  3292. ("" "marvosym" t)
  3293. ("" "wasysym" t)
  3294. ("" "latexsym" t)
  3295. ("" "amssymb" t)
  3296. ("" "hyperref" nil)
  3297. "\\tolerance=1000")
  3298. "Alist of default packages to be inserted in the header.
  3299. Change this only if one of the packages here causes an
  3300. incompatibility with another package you are using.
  3301. The packages in this list are needed by one part or another of
  3302. Org mode to function properly:
  3303. - inputenc, fontenc: for basic font and character selection
  3304. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3305. symbols used for interpreting the entities in `org-entities'.
  3306. You can skip some of these packages if you don't use any of the
  3307. symbols in it.
  3308. - graphicx: for including images
  3309. - float, wrapfig: for figure placement
  3310. - longtable: for long tables
  3311. - hyperref: for cross references
  3312. Therefore you should not modify this variable unless you know
  3313. what you are doing. The one reason to change it anyway is that
  3314. you might be loading some other package that conflicts with one
  3315. of the default packages. Each cell is of the format
  3316. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3317. the package also needs to be included when compiling LaTeX
  3318. snippets into images for inclusion into non-LaTeX output."
  3319. :group 'org-latex
  3320. :group 'org-export-latex
  3321. :set 'org-set-packages-alist
  3322. :get 'org-get-packages-alist
  3323. :version "24.1"
  3324. :type '(repeat
  3325. (choice
  3326. (list :tag "options/package pair"
  3327. (string :tag "options")
  3328. (string :tag "package")
  3329. (boolean :tag "Snippet"))
  3330. (string :tag "A line of LaTeX"))))
  3331. (defcustom org-latex-packages-alist nil
  3332. "Alist of packages to be inserted in every LaTeX header.
  3333. These will be inserted after `org-latex-default-packages-alist'.
  3334. Each cell is of the format:
  3335. \(\"options\" \"package\" snippet-flag)
  3336. SNIPPET-FLAG, when t, indicates that this package is also needed
  3337. when turning LaTeX snippets into images for inclusion into
  3338. non-LaTeX output.
  3339. Make sure that you only list packages here which:
  3340. - you want in every file
  3341. - do not conflict with the setup in `org-format-latex-header'.
  3342. - do not conflict with the default packages in
  3343. `org-latex-default-packages-alist'."
  3344. :group 'org-latex
  3345. :group 'org-export-latex
  3346. :set 'org-set-packages-alist
  3347. :get 'org-get-packages-alist
  3348. :type '(repeat
  3349. (choice
  3350. (list :tag "options/package pair"
  3351. (string :tag "options")
  3352. (string :tag "package")
  3353. (boolean :tag "Snippet"))
  3354. (string :tag "A line of LaTeX"))))
  3355. (defgroup org-appearance nil
  3356. "Settings for Org-mode appearance."
  3357. :tag "Org Appearance"
  3358. :group 'org)
  3359. (defcustom org-level-color-stars-only nil
  3360. "Non-nil means fontify only the stars in each headline.
  3361. When nil, the entire headline is fontified.
  3362. Changing it requires restart of `font-lock-mode' to become effective
  3363. also in regions already fontified."
  3364. :group 'org-appearance
  3365. :type 'boolean)
  3366. (defcustom org-hide-leading-stars nil
  3367. "Non-nil means hide the first N-1 stars in a headline.
  3368. This works by using the face `org-hide' for these stars. This
  3369. face is white for a light background, and black for a dark
  3370. background. You may have to customize the face `org-hide' to
  3371. make this work.
  3372. Changing it requires restart of `font-lock-mode' to become effective
  3373. also in regions already fontified.
  3374. You may also set this on a per-file basis by adding one of the following
  3375. lines to the buffer:
  3376. #+STARTUP: hidestars
  3377. #+STARTUP: showstars"
  3378. :group 'org-appearance
  3379. :type 'boolean)
  3380. (defcustom org-hidden-keywords nil
  3381. "List of symbols corresponding to keywords to be hidden the org buffer.
  3382. For example, a value '(title) for this list will make the document's title
  3383. appear in the buffer without the initial #+TITLE: keyword."
  3384. :group 'org-appearance
  3385. :version "24.1"
  3386. :type '(set (const :tag "#+AUTHOR" author)
  3387. (const :tag "#+DATE" date)
  3388. (const :tag "#+EMAIL" email)
  3389. (const :tag "#+TITLE" title)))
  3390. (defcustom org-custom-properties nil
  3391. "List of properties (as strings) with a special meaning.
  3392. The default use of these custom properties is to let the user
  3393. hide them with `org-toggle-custom-properties-visibility'."
  3394. :group 'org-properties
  3395. :group 'org-appearance
  3396. :version "24.3"
  3397. :type '(repeat (string :tag "Property Name")))
  3398. (defcustom org-fontify-done-headline nil
  3399. "Non-nil means change the face of a headline if it is marked DONE.
  3400. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3401. When this is non-nil, the headline after the keyword is set to the
  3402. `org-headline-done' as an additional indication."
  3403. :group 'org-appearance
  3404. :type 'boolean)
  3405. (defcustom org-fontify-emphasized-text t
  3406. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3407. Changing this variable requires a restart of Emacs to take effect."
  3408. :group 'org-appearance
  3409. :type 'boolean)
  3410. (defcustom org-fontify-whole-heading-line nil
  3411. "Non-nil means fontify the whole line for headings.
  3412. This is useful when setting a background color for the
  3413. org-level-* faces."
  3414. :group 'org-appearance
  3415. :type 'boolean)
  3416. (defcustom org-highlight-latex-and-related nil
  3417. "Non-nil means highlight LaTeX related syntax in the buffer.
  3418. When non nil, the value should be a list containing any of the
  3419. following symbols:
  3420. `latex' Highlight LaTeX snippets and environments.
  3421. `script' Highlight subscript and superscript.
  3422. `entities' Highlight entities."
  3423. :group 'org-appearance
  3424. :version "24.4"
  3425. :package-version '(Org . "8.0")
  3426. :type '(choice
  3427. (const :tag "No highlighting" nil)
  3428. (set :greedy t :tag "Highlight"
  3429. (const :tag "LaTeX snippets and environments" latex)
  3430. (const :tag "Subscript and superscript" script)
  3431. (const :tag "Entities" entities))))
  3432. (defcustom org-hide-emphasis-markers nil
  3433. "Non-nil mean font-lock should hide the emphasis marker characters."
  3434. :group 'org-appearance
  3435. :type 'boolean)
  3436. (defcustom org-pretty-entities nil
  3437. "Non-nil means show entities as UTF8 characters.
  3438. When nil, the \\name form remains in the buffer."
  3439. :group 'org-appearance
  3440. :version "24.1"
  3441. :type 'boolean)
  3442. (defcustom org-pretty-entities-include-sub-superscripts t
  3443. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3444. :group 'org-appearance
  3445. :version "24.1"
  3446. :type 'boolean)
  3447. (defvar org-emph-re nil
  3448. "Regular expression for matching emphasis.
  3449. After a match, the match groups contain these elements:
  3450. 0 The match of the full regular expression, including the characters
  3451. before and after the proper match
  3452. 1 The character before the proper match, or empty at beginning of line
  3453. 2 The proper match, including the leading and trailing markers
  3454. 3 The leading marker like * or /, indicating the type of highlighting
  3455. 4 The text between the emphasis markers, not including the markers
  3456. 5 The character after the match, empty at the end of a line")
  3457. (defvar org-verbatim-re nil
  3458. "Regular expression for matching verbatim text.")
  3459. (defvar org-emphasis-regexp-components) ; defined just below
  3460. (defvar org-emphasis-alist) ; defined just below
  3461. (defun org-set-emph-re (var val)
  3462. "Set variable and compute the emphasis regular expression."
  3463. (set var val)
  3464. (when (and (boundp 'org-emphasis-alist)
  3465. (boundp 'org-emphasis-regexp-components)
  3466. org-emphasis-alist org-emphasis-regexp-components)
  3467. (let* ((e org-emphasis-regexp-components)
  3468. (pre (car e))
  3469. (post (nth 1 e))
  3470. (border (nth 2 e))
  3471. (body (nth 3 e))
  3472. (nl (nth 4 e))
  3473. (body1 (concat body "*?"))
  3474. (markers (mapconcat 'car org-emphasis-alist ""))
  3475. (vmarkers (mapconcat
  3476. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3477. org-emphasis-alist "")))
  3478. ;; make sure special characters appear at the right position in the class
  3479. (if (string-match "\\^" markers)
  3480. (setq markers (concat (replace-match "" t t markers) "^")))
  3481. (if (string-match "-" markers)
  3482. (setq markers (concat (replace-match "" t t markers) "-")))
  3483. (if (string-match "\\^" vmarkers)
  3484. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3485. (if (string-match "-" vmarkers)
  3486. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3487. (if (> nl 0)
  3488. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3489. (int-to-string nl) "\\}")))
  3490. ;; Make the regexp
  3491. (setq org-emph-re
  3492. (concat "\\([" pre "]\\|^\\)"
  3493. "\\("
  3494. "\\([" markers "]\\)"
  3495. "\\("
  3496. "[^" border "]\\|"
  3497. "[^" border "]"
  3498. body1
  3499. "[^" border "]"
  3500. "\\)"
  3501. "\\3\\)"
  3502. "\\([" post "]\\|$\\)"))
  3503. (setq org-verbatim-re
  3504. (concat "\\([" pre "]\\|^\\)"
  3505. "\\("
  3506. "\\([" vmarkers "]\\)"
  3507. "\\("
  3508. "[^" border "]\\|"
  3509. "[^" border "]"
  3510. body1
  3511. "[^" border "]"
  3512. "\\)"
  3513. "\\3\\)"
  3514. "\\([" post "]\\|$\\)")))))
  3515. (defcustom org-emphasis-regexp-components
  3516. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3517. "Components used to build the regular expression for emphasis.
  3518. This is a list with five entries. Terminology: In an emphasis string
  3519. like \" *strong word* \", we call the initial space PREMATCH, the final
  3520. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3521. and \"trong wor\" is the body. The different components in this variable
  3522. specify what is allowed/forbidden in each part:
  3523. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3524. post Chars allowed as postmatch. End of line will be allowed too.
  3525. border The chars *forbidden* as border characters.
  3526. body-regexp A regexp like \".\" to match a body character. Don't use
  3527. non-shy groups here, and don't allow newline here.
  3528. newline The maximum number of newlines allowed in an emphasis exp.
  3529. Use customize to modify this, or restart Emacs after changing it."
  3530. :group 'org-appearance
  3531. :set 'org-set-emph-re
  3532. :type '(list
  3533. (sexp :tag "Allowed chars in pre ")
  3534. (sexp :tag "Allowed chars in post ")
  3535. (sexp :tag "Forbidden chars in border ")
  3536. (sexp :tag "Regexp for body ")
  3537. (integer :tag "number of newlines allowed")
  3538. (option (boolean :tag "Please ignore this button"))))
  3539. (defcustom org-emphasis-alist
  3540. `(("*" bold "<b>" "</b>")
  3541. ("/" italic "<i>" "</i>")
  3542. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3543. ("=" org-code "<code>" "</code>" verbatim)
  3544. ("~" org-verbatim "<code>" "</code>" verbatim)
  3545. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3546. "<del>" "</del>")
  3547. )
  3548. "Special syntax for emphasized text.
  3549. Text starting and ending with a special character will be emphasized, for
  3550. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3551. characters, the face to be used by font-lock for highlighting in Org-mode
  3552. Emacs buffers, and the HTML tags to be used for this.
  3553. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3554. Use customize to modify this, or restart Emacs after changing it."
  3555. :group 'org-appearance
  3556. :set 'org-set-emph-re
  3557. :type '(repeat
  3558. (list
  3559. (string :tag "Marker character")
  3560. (choice
  3561. (face :tag "Font-lock-face")
  3562. (plist :tag "Face property list"))
  3563. (string :tag "HTML start tag")
  3564. (string :tag "HTML end tag")
  3565. (option (const verbatim)))))
  3566. (defvar org-protecting-blocks
  3567. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3568. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3569. This is needed for font-lock setup.")
  3570. ;;; Miscellaneous options
  3571. (defgroup org-completion nil
  3572. "Completion in Org-mode."
  3573. :tag "Org Completion"
  3574. :group 'org)
  3575. (defcustom org-completion-use-ido nil
  3576. "Non-nil means use ido completion wherever possible.
  3577. Note that `ido-mode' must be active for this variable to be relevant.
  3578. If you decide to turn this variable on, you might well want to turn off
  3579. `org-outline-path-complete-in-steps'.
  3580. See also `org-completion-use-iswitchb'."
  3581. :group 'org-completion
  3582. :type 'boolean)
  3583. (defcustom org-completion-use-iswitchb nil
  3584. "Non-nil means use iswitchb completion wherever possible.
  3585. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3586. If you decide to turn this variable on, you might well want to turn off
  3587. `org-outline-path-complete-in-steps'.
  3588. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3589. :group 'org-completion
  3590. :type 'boolean)
  3591. (defcustom org-completion-fallback-command 'hippie-expand
  3592. "The expansion command called by \\[pcomplete] in normal context.
  3593. Normal means, no org-mode-specific context."
  3594. :group 'org-completion
  3595. :type 'function)
  3596. ;;; Functions and variables from their packages
  3597. ;; Declared here to avoid compiler warnings
  3598. ;; XEmacs only
  3599. (defvar outline-mode-menu-heading)
  3600. (defvar outline-mode-menu-show)
  3601. (defvar outline-mode-menu-hide)
  3602. (defvar zmacs-regions) ; XEmacs regions
  3603. ;; Emacs only
  3604. (defvar mark-active)
  3605. ;; Various packages
  3606. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3607. (declare-function calendar-forward-day "cal-move" (arg))
  3608. (declare-function calendar-goto-date "cal-move" (date))
  3609. (declare-function calendar-goto-today "cal-move" ())
  3610. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3611. (defvar calc-embedded-close-formula)
  3612. (defvar calc-embedded-open-formula)
  3613. (declare-function cdlatex-tab "ext:cdlatex" ())
  3614. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3615. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3616. (defvar font-lock-unfontify-region-function)
  3617. (declare-function iswitchb-read-buffer "iswitchb"
  3618. (prompt &optional default require-match start matches-set))
  3619. (defvar iswitchb-temp-buflist)
  3620. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3621. (defvar org-agenda-tags-todo-honor-ignore-options)
  3622. (declare-function org-agenda-skip "org-agenda" ())
  3623. (declare-function
  3624. org-agenda-format-item "org-agenda"
  3625. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3626. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3627. (declare-function org-agenda-change-all-lines "org-agenda"
  3628. (newhead hdmarker &optional fixface just-this))
  3629. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3630. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3631. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3632. (beg end))
  3633. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3634. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3635. "org-agenda" (&optional end))
  3636. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3637. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3638. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3639. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3640. (declare-function org-indent-mode "org-indent" (&optional arg))
  3641. (declare-function parse-time-string "parse-time" (string))
  3642. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3643. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3644. (defvar remember-data-file)
  3645. (defvar texmathp-why)
  3646. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3647. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3648. (defvar org-latex-regexps)
  3649. ;;; Autoload and prepare some org modules
  3650. ;; Some table stuff that needs to be defined here, because it is used
  3651. ;; by the functions setting up org-mode or checking for table context.
  3652. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3653. "Detect an org-type or table-type table.")
  3654. (defconst org-table-line-regexp "^[ \t]*|"
  3655. "Detect an org-type table line.")
  3656. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3657. "Detect an org-type table line.")
  3658. (defconst org-table-hline-regexp "^[ \t]*|-"
  3659. "Detect an org-type table hline.")
  3660. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3661. "Detect a table-type table hline.")
  3662. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3663. "Detect the first line outside a table when searching from within it.
  3664. This works for both table types.")
  3665. ;; Autoload the functions in org-table.el that are needed by functions here.
  3666. (eval-and-compile
  3667. (org-autoload "org-table"
  3668. '(org-table-begin org-table-blank-field org-table-end)))
  3669. ;;;###autoload
  3670. (defun turn-on-orgtbl ()
  3671. "Unconditionally turn on `orgtbl-mode'."
  3672. (require 'org-table)
  3673. (orgtbl-mode 1))
  3674. (defun org-at-table-p (&optional table-type)
  3675. "Return t if the cursor is inside an org-type table.
  3676. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3677. (if org-enable-table-editor
  3678. (save-excursion
  3679. (beginning-of-line 1)
  3680. (looking-at (if table-type org-table-any-line-regexp
  3681. org-table-line-regexp)))
  3682. nil))
  3683. (defsubst org-table-p () (org-at-table-p))
  3684. (defun org-at-table.el-p ()
  3685. "Return t if and only if we are at a table.el table."
  3686. (and (org-at-table-p 'any)
  3687. (save-excursion
  3688. (goto-char (org-table-begin 'any))
  3689. (looking-at org-table1-hline-regexp))))
  3690. (defun org-table-recognize-table.el ()
  3691. "If there is a table.el table nearby, recognize it and move into it."
  3692. (if org-table-tab-recognizes-table.el
  3693. (if (org-at-table.el-p)
  3694. (progn
  3695. (beginning-of-line 1)
  3696. (if (looking-at org-table-dataline-regexp)
  3697. nil
  3698. (if (looking-at org-table1-hline-regexp)
  3699. (progn
  3700. (beginning-of-line 2)
  3701. (if (looking-at org-table-any-border-regexp)
  3702. (beginning-of-line -1)))))
  3703. (if (re-search-forward "|" (org-table-end t) t)
  3704. (progn
  3705. (require 'table)
  3706. (if (table--at-cell-p (point))
  3707. t
  3708. (message "recognizing table.el table...")
  3709. (table-recognize-table)
  3710. (message "recognizing table.el table...done")))
  3711. (error "This should not happen"))
  3712. t)
  3713. nil)
  3714. nil))
  3715. (defun org-at-table-hline-p ()
  3716. "Return t if the cursor is inside a hline in a table."
  3717. (if org-enable-table-editor
  3718. (save-excursion
  3719. (beginning-of-line 1)
  3720. (looking-at org-table-hline-regexp))
  3721. nil))
  3722. (defvar org-table-clean-did-remove-column nil)
  3723. (defun org-table-map-tables (function &optional quietly)
  3724. "Apply FUNCTION to the start of all tables in the buffer."
  3725. (save-excursion
  3726. (save-restriction
  3727. (widen)
  3728. (goto-char (point-min))
  3729. (while (re-search-forward org-table-any-line-regexp nil t)
  3730. (unless quietly
  3731. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3732. (beginning-of-line 1)
  3733. (when (and (looking-at org-table-line-regexp)
  3734. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3735. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3736. (save-excursion (funcall function))
  3737. (or (looking-at org-table-line-regexp)
  3738. (forward-char 1)))
  3739. (re-search-forward org-table-any-border-regexp nil 1))))
  3740. (unless quietly (message "Mapping tables: done")))
  3741. ;; Declare and autoload functions from ox.el and al.
  3742. (declare-function org-export-get-environment "ox"
  3743. (&optional backend subtreep ext-plist))
  3744. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3745. ;; Declare and autoload functions from org-agenda.el
  3746. (eval-and-compile
  3747. (org-autoload "org-agenda"
  3748. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3749. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3750. (declare-function org-clock-update-mode-line "org-clock" ())
  3751. (declare-function org-resolve-clocks "org-clock"
  3752. (&optional also-non-dangling-p prompt last-valid))
  3753. (defvar org-clock-start-time)
  3754. (defvar org-clock-marker (make-marker)
  3755. "Marker recording the last clock-in.")
  3756. (defvar org-clock-hd-marker (make-marker)
  3757. "Marker recording the last clock-in, but the headline position.")
  3758. (defvar org-clock-heading ""
  3759. "The heading of the current clock entry.")
  3760. (defun org-clock-is-active ()
  3761. "Return non-nil if clock is currently running.
  3762. The return value is actually the clock marker."
  3763. (marker-buffer org-clock-marker))
  3764. (eval-and-compile
  3765. (org-autoload "org-clock" '(org-clock-remove-overlays
  3766. org-clock-update-time-maybe
  3767. org-clocktable-shift)))
  3768. (defun org-check-running-clock ()
  3769. "Check if the current buffer contains the running clock.
  3770. If yes, offer to stop it and to save the buffer with the changes."
  3771. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3772. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3773. (buffer-name))))
  3774. (org-clock-out)
  3775. (when (y-or-n-p "Save changed buffer?")
  3776. (save-buffer))))
  3777. (defun org-clocktable-try-shift (dir n)
  3778. "Check if this line starts a clock table, if yes, shift the time block."
  3779. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3780. (org-clocktable-shift dir n)))
  3781. ;;;###autoload
  3782. (defun org-clock-persistence-insinuate ()
  3783. "Set up hooks for clock persistence."
  3784. (require 'org-clock)
  3785. (add-hook 'org-mode-hook 'org-clock-load)
  3786. (add-hook 'kill-emacs-hook 'org-clock-save))
  3787. ;; Define the variable already here, to make sure we have it.
  3788. (defvar org-indent-mode nil
  3789. "Non-nil if Org-Indent mode is enabled.
  3790. Use the command `org-indent-mode' to change this variable.")
  3791. ;; Autoload archiving code
  3792. ;; The stuff that is needed for cycling and tags has to be defined here.
  3793. (defgroup org-archive nil
  3794. "Options concerning archiving in Org-mode."
  3795. :tag "Org Archive"
  3796. :group 'org-structure)
  3797. (defcustom org-archive-location "%s_archive::"
  3798. "The location where subtrees should be archived.
  3799. The value of this variable is a string, consisting of two parts,
  3800. separated by a double-colon. The first part is a filename and
  3801. the second part is a headline.
  3802. When the filename is omitted, archiving happens in the same file.
  3803. %s in the filename will be replaced by the current file
  3804. name (without the directory part). Archiving to a different file
  3805. is useful to keep archived entries from contributing to the
  3806. Org-mode Agenda.
  3807. The archived entries will be filed as subtrees of the specified
  3808. headline. When the headline is omitted, the subtrees are simply
  3809. filed away at the end of the file, as top-level entries. Also in
  3810. the heading you can use %s to represent the file name, this can be
  3811. useful when using the same archive for a number of different files.
  3812. Here are a few examples:
  3813. \"%s_archive::\"
  3814. If the current file is Projects.org, archive in file
  3815. Projects.org_archive, as top-level trees. This is the default.
  3816. \"::* Archived Tasks\"
  3817. Archive in the current file, under the top-level headline
  3818. \"* Archived Tasks\".
  3819. \"~/org/archive.org::\"
  3820. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3821. \"~/org/archive.org::* From %s\"
  3822. Archive in file ~/org/archive.org (absolute path), under headlines
  3823. \"From FILENAME\" where file name is the current file name.
  3824. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3825. The \"datetree/\" string is special, signifying to archive
  3826. items to the datetree. Items are placed in either the CLOSED
  3827. date of the item, or the current date if there is no CLOSED date.
  3828. The heading will be a subentry to the current date. There doesn't
  3829. need to be a heading, but there always needs to be a slash after
  3830. datetree. For example, to store archived items directly in the
  3831. datetree, use \"~/org/datetree.org::datetree/\".
  3832. \"basement::** Finished Tasks\"
  3833. Archive in file ./basement (relative path), as level 3 trees
  3834. below the level 2 heading \"** Finished Tasks\".
  3835. You may set this option on a per-file basis by adding to the buffer a
  3836. line like
  3837. #+ARCHIVE: basement::** Finished Tasks
  3838. You may also define it locally for a subtree by setting an ARCHIVE property
  3839. in the entry. If such a property is found in an entry, or anywhere up
  3840. the hierarchy, it will be used."
  3841. :group 'org-archive
  3842. :type 'string)
  3843. (defcustom org-archive-tag "ARCHIVE"
  3844. "The tag that marks a subtree as archived.
  3845. An archived subtree does not open during visibility cycling, and does
  3846. not contribute to the agenda listings.
  3847. After changing this, font-lock must be restarted in the relevant buffers to
  3848. get the proper fontification."
  3849. :group 'org-archive
  3850. :group 'org-keywords
  3851. :type 'string)
  3852. (defcustom org-agenda-skip-archived-trees t
  3853. "Non-nil means the agenda will skip any items located in archived trees.
  3854. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3855. variable is no longer recommended, you should leave it at the value t.
  3856. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3857. :group 'org-archive
  3858. :group 'org-agenda-skip
  3859. :type 'boolean)
  3860. (defcustom org-columns-skip-archived-trees t
  3861. "Non-nil means ignore archived trees when creating column view."
  3862. :group 'org-archive
  3863. :group 'org-properties
  3864. :type 'boolean)
  3865. (defcustom org-cycle-open-archived-trees nil
  3866. "Non-nil means `org-cycle' will open archived trees.
  3867. An archived tree is a tree marked with the tag ARCHIVE.
  3868. When nil, archived trees will stay folded. You can still open them with
  3869. normal outline commands like `show-all', but not with the cycling commands."
  3870. :group 'org-archive
  3871. :group 'org-cycle
  3872. :type 'boolean)
  3873. (defcustom org-sparse-tree-open-archived-trees nil
  3874. "Non-nil means sparse tree construction shows matches in archived trees.
  3875. When nil, matches in these trees are highlighted, but the trees are kept in
  3876. collapsed state."
  3877. :group 'org-archive
  3878. :group 'org-sparse-trees
  3879. :type 'boolean)
  3880. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3881. "The default date type when building a sparse tree.
  3882. When this is nil, a date is a scheduled or a deadline timestamp.
  3883. Otherwise, these types are allowed:
  3884. all: all timestamps
  3885. active: only active timestamps (<...>)
  3886. inactive: only inactive timestamps (<...)
  3887. scheduled: only scheduled timestamps
  3888. deadline: only deadline timestamps"
  3889. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3890. (const :tag "All timestamps" all)
  3891. (const :tag "Only active timestamps" active)
  3892. (const :tag "Only inactive timestamps" inactive)
  3893. (const :tag "Only scheduled timestamps" scheduled)
  3894. (const :tag "Only deadline timestamps" deadline)
  3895. (const :tag "Only closed timestamps" closed))
  3896. :version "24.3"
  3897. :group 'org-sparse-trees)
  3898. (defun org-cycle-hide-archived-subtrees (state)
  3899. "Re-hide all archived subtrees after a visibility state change."
  3900. (when (and (not org-cycle-open-archived-trees)
  3901. (not (memq state '(overview folded))))
  3902. (save-excursion
  3903. (let* ((globalp (memq state '(contents all)))
  3904. (beg (if globalp (point-min) (point)))
  3905. (end (if globalp (point-max) (org-end-of-subtree t))))
  3906. (org-hide-archived-subtrees beg end)
  3907. (goto-char beg)
  3908. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3909. (message "%s" (substitute-command-keys
  3910. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3911. (defun org-force-cycle-archived ()
  3912. "Cycle subtree even if it is archived."
  3913. (interactive)
  3914. (setq this-command 'org-cycle)
  3915. (let ((org-cycle-open-archived-trees t))
  3916. (call-interactively 'org-cycle)))
  3917. (defun org-hide-archived-subtrees (beg end)
  3918. "Re-hide all archived subtrees after a visibility state change."
  3919. (save-excursion
  3920. (let* ((re (concat ":" org-archive-tag ":")))
  3921. (goto-char beg)
  3922. (while (re-search-forward re end t)
  3923. (when (org-at-heading-p)
  3924. (org-flag-subtree t)
  3925. (org-end-of-subtree t))))))
  3926. (declare-function outline-end-of-heading "outline" ())
  3927. (declare-function outline-flag-region "outline" (from to flag))
  3928. (defun org-flag-subtree (flag)
  3929. (save-excursion
  3930. (org-back-to-heading t)
  3931. (outline-end-of-heading)
  3932. (outline-flag-region (point)
  3933. (progn (org-end-of-subtree t) (point))
  3934. flag)))
  3935. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3936. (eval-and-compile
  3937. (org-autoload "org-archive"
  3938. '(org-add-archive-files)))
  3939. ;; Autoload Column View Code
  3940. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3941. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3942. (declare-function org-columns-compute "org-colview" (property))
  3943. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3944. '(org-columns-number-to-string
  3945. org-columns-get-format-and-top-level
  3946. org-columns-compute
  3947. org-columns-remove-overlays))
  3948. ;; Autoload ID code
  3949. (declare-function org-id-store-link "org-id")
  3950. (declare-function org-id-locations-load "org-id")
  3951. (declare-function org-id-locations-save "org-id")
  3952. (defvar org-id-track-globally)
  3953. (org-autoload "org-id"
  3954. '(org-id-new
  3955. org-id-copy
  3956. org-id-get-with-outline-path-completion
  3957. org-id-get-with-outline-drilling))
  3958. ;;; Variables for pre-computed regular expressions, all buffer local
  3959. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3960. "Matches first line of a hidden block.")
  3961. (make-variable-buffer-local 'org-drawer-regexp)
  3962. (defvar org-todo-regexp nil
  3963. "Matches any of the TODO state keywords.")
  3964. (make-variable-buffer-local 'org-todo-regexp)
  3965. (defvar org-not-done-regexp nil
  3966. "Matches any of the TODO state keywords except the last one.")
  3967. (make-variable-buffer-local 'org-not-done-regexp)
  3968. (defvar org-not-done-heading-regexp nil
  3969. "Matches a TODO headline that is not done.")
  3970. (make-variable-buffer-local 'org-not-done-regexp)
  3971. (defvar org-todo-line-regexp nil
  3972. "Matches a headline and puts TODO state into group 2 if present.")
  3973. (make-variable-buffer-local 'org-todo-line-regexp)
  3974. (defvar org-complex-heading-regexp nil
  3975. "Matches a headline and puts everything into groups:
  3976. group 1: the stars
  3977. group 2: The todo keyword, maybe
  3978. group 3: Priority cookie
  3979. group 4: True headline
  3980. group 5: Tags")
  3981. (make-variable-buffer-local 'org-complex-heading-regexp)
  3982. (defvar org-complex-heading-regexp-format nil
  3983. "Printf format to make regexp to match an exact headline.
  3984. This regexp will match the headline of any node which has the
  3985. exact headline text that is put into the format, but may have any
  3986. TODO state, priority and tags.")
  3987. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3988. (defvar org-todo-line-tags-regexp nil
  3989. "Matches a headline and puts TODO state into group 2 if present.
  3990. Also put tags into group 4 if tags are present.")
  3991. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3992. (defvar org-ds-keyword-length 12
  3993. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3994. (make-variable-buffer-local 'org-ds-keyword-length)
  3995. (defvar org-deadline-regexp nil
  3996. "Matches the DEADLINE keyword.")
  3997. (make-variable-buffer-local 'org-deadline-regexp)
  3998. (defvar org-deadline-time-regexp nil
  3999. "Matches the DEADLINE keyword together with a time stamp.")
  4000. (make-variable-buffer-local 'org-deadline-time-regexp)
  4001. (defvar org-deadline-time-hour-regexp nil
  4002. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4003. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4004. (defvar org-deadline-line-regexp nil
  4005. "Matches the DEADLINE keyword and the rest of the line.")
  4006. (make-variable-buffer-local 'org-deadline-line-regexp)
  4007. (defvar org-scheduled-regexp nil
  4008. "Matches the SCHEDULED keyword.")
  4009. (make-variable-buffer-local 'org-scheduled-regexp)
  4010. (defvar org-scheduled-time-regexp nil
  4011. "Matches the SCHEDULED keyword together with a time stamp.")
  4012. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4013. (defvar org-scheduled-time-hour-regexp nil
  4014. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4015. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4016. (defvar org-closed-time-regexp nil
  4017. "Matches the CLOSED keyword together with a time stamp.")
  4018. (make-variable-buffer-local 'org-closed-time-regexp)
  4019. (defvar org-keyword-time-regexp nil
  4020. "Matches any of the 4 keywords, together with the time stamp.")
  4021. (make-variable-buffer-local 'org-keyword-time-regexp)
  4022. (defvar org-keyword-time-not-clock-regexp nil
  4023. "Matches any of the 3 keywords, together with the time stamp.")
  4024. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4025. (defvar org-maybe-keyword-time-regexp nil
  4026. "Matches a timestamp, possibly preceded by a keyword.")
  4027. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4028. (defvar org-all-time-keywords nil
  4029. "List of time keywords.")
  4030. (make-variable-buffer-local 'org-all-time-keywords)
  4031. (defconst org-plain-time-of-day-regexp
  4032. (concat
  4033. "\\(\\<[012]?[0-9]"
  4034. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4035. "\\(--?"
  4036. "\\(\\<[012]?[0-9]"
  4037. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4038. "\\)?")
  4039. "Regular expression to match a plain time or time range.
  4040. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4041. groups carry important information:
  4042. 0 the full match
  4043. 1 the first time, range or not
  4044. 8 the second time, if it is a range.")
  4045. (defconst org-plain-time-extension-regexp
  4046. (concat
  4047. "\\(\\<[012]?[0-9]"
  4048. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4049. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4050. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4051. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4052. groups carry important information:
  4053. 0 the full match
  4054. 7 hours of duration
  4055. 9 minutes of duration")
  4056. (defconst org-stamp-time-of-day-regexp
  4057. (concat
  4058. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4059. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4060. "\\(--?"
  4061. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4062. "Regular expression to match a timestamp time or time range.
  4063. After a match, the following groups carry important information:
  4064. 0 the full match
  4065. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4066. 2 the first time, range or not
  4067. 4 the second time, if it is a range.")
  4068. (defconst org-startup-options
  4069. '(("fold" org-startup-folded t)
  4070. ("overview" org-startup-folded t)
  4071. ("nofold" org-startup-folded nil)
  4072. ("showall" org-startup-folded nil)
  4073. ("showeverything" org-startup-folded showeverything)
  4074. ("content" org-startup-folded content)
  4075. ("indent" org-startup-indented t)
  4076. ("noindent" org-startup-indented nil)
  4077. ("hidestars" org-hide-leading-stars t)
  4078. ("showstars" org-hide-leading-stars nil)
  4079. ("odd" org-odd-levels-only t)
  4080. ("oddeven" org-odd-levels-only nil)
  4081. ("align" org-startup-align-all-tables t)
  4082. ("noalign" org-startup-align-all-tables nil)
  4083. ("inlineimages" org-startup-with-inline-images t)
  4084. ("noinlineimages" org-startup-with-inline-images nil)
  4085. ("latexpreview" org-startup-with-latex-preview t)
  4086. ("nolatexpreview" org-startup-with-latex-preview nil)
  4087. ("customtime" org-display-custom-times t)
  4088. ("logdone" org-log-done time)
  4089. ("lognotedone" org-log-done note)
  4090. ("nologdone" org-log-done nil)
  4091. ("lognoteclock-out" org-log-note-clock-out t)
  4092. ("nolognoteclock-out" org-log-note-clock-out nil)
  4093. ("logrepeat" org-log-repeat state)
  4094. ("lognoterepeat" org-log-repeat note)
  4095. ("logdrawer" org-log-into-drawer t)
  4096. ("nologdrawer" org-log-into-drawer nil)
  4097. ("logstatesreversed" org-log-states-order-reversed t)
  4098. ("nologstatesreversed" org-log-states-order-reversed nil)
  4099. ("nologrepeat" org-log-repeat nil)
  4100. ("logreschedule" org-log-reschedule time)
  4101. ("lognotereschedule" org-log-reschedule note)
  4102. ("nologreschedule" org-log-reschedule nil)
  4103. ("logredeadline" org-log-redeadline time)
  4104. ("lognoteredeadline" org-log-redeadline note)
  4105. ("nologredeadline" org-log-redeadline nil)
  4106. ("logrefile" org-log-refile time)
  4107. ("lognoterefile" org-log-refile note)
  4108. ("nologrefile" org-log-refile nil)
  4109. ("fninline" org-footnote-define-inline t)
  4110. ("nofninline" org-footnote-define-inline nil)
  4111. ("fnlocal" org-footnote-section nil)
  4112. ("fnauto" org-footnote-auto-label t)
  4113. ("fnprompt" org-footnote-auto-label nil)
  4114. ("fnconfirm" org-footnote-auto-label confirm)
  4115. ("fnplain" org-footnote-auto-label plain)
  4116. ("fnadjust" org-footnote-auto-adjust t)
  4117. ("nofnadjust" org-footnote-auto-adjust nil)
  4118. ("constcgs" constants-unit-system cgs)
  4119. ("constSI" constants-unit-system SI)
  4120. ("noptag" org-tag-persistent-alist nil)
  4121. ("hideblocks" org-hide-block-startup t)
  4122. ("nohideblocks" org-hide-block-startup nil)
  4123. ("beamer" org-startup-with-beamer-mode t)
  4124. ("entitiespretty" org-pretty-entities t)
  4125. ("entitiesplain" org-pretty-entities nil))
  4126. "Variable associated with STARTUP options for org-mode.
  4127. Each element is a list of three items: the startup options (as written
  4128. in the #+STARTUP line), the corresponding variable, and the value to set
  4129. this variable to if the option is found. An optional forth element PUSH
  4130. means to push this value onto the list in the variable.")
  4131. (defun org-update-property-plist (key val props)
  4132. "Update PROPS with KEY and VAL."
  4133. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4134. (key (if appending (substring key 0 (- (length key) 1)) key))
  4135. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4136. (previous (cdr (assoc key props))))
  4137. (if appending
  4138. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4139. (cons (cons key val) remainder))))
  4140. (defconst org-block-regexp
  4141. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4142. "Regular expression for hiding blocks.")
  4143. (defconst org-heading-keyword-regexp-format
  4144. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4145. "Printf format for a regexp matching a headline with some keyword.
  4146. This regexp will match the headline of any node which has the
  4147. exact keyword that is put into the format. The keyword isn't in
  4148. any group by default, but the stars and the body are.")
  4149. (defconst org-heading-keyword-maybe-regexp-format
  4150. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4151. "Printf format for a regexp matching a headline, possibly with some keyword.
  4152. This regexp can match any headline with the specified keyword, or
  4153. without a keyword. The keyword isn't in any group by default,
  4154. but the stars and the body are.")
  4155. (defcustom org-group-tags t
  4156. "When non-nil (the default), use group tags.
  4157. This can be turned on/off through `org-toggle-tags-groups'."
  4158. :group 'org-tags
  4159. :group 'org-startup
  4160. :type 'boolean)
  4161. (defun org-toggle-tags-groups ()
  4162. "Toggle support for group tags.
  4163. Support for group tags is controlled by the option
  4164. `org-group-tags', which is non-nil by default."
  4165. (interactive)
  4166. (setq org-group-tags (not org-group-tags))
  4167. (cond ((and (derived-mode-p 'org-agenda-mode)
  4168. org-group-tags)
  4169. (org-agenda-redo))
  4170. ((derived-mode-p 'org-mode)
  4171. (let ((org-inhibit-startup t)) (org-mode))))
  4172. (message "Groups tags support has been turned %s"
  4173. (if org-group-tags "on" "off")))
  4174. (defun org-set-regexps-and-options-for-tags ()
  4175. "Precompute regular expressions used for tags in the current buffer."
  4176. (when (derived-mode-p 'org-mode)
  4177. (org-set-local 'org-file-tags nil)
  4178. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4179. (splitre "[ \t]+")
  4180. tags ftags key value
  4181. (start 0))
  4182. (save-excursion
  4183. (save-restriction
  4184. (widen)
  4185. (goto-char (point-min))
  4186. (while (re-search-forward re nil t)
  4187. (setq key (upcase (org-match-string-no-properties 1))
  4188. value (org-match-string-no-properties 2))
  4189. (if (stringp value) (setq value (org-trim value)))
  4190. (cond
  4191. ((equal key "TAGS")
  4192. (setq tags (append tags (if tags '("\\n") nil)
  4193. (org-split-string value splitre))))
  4194. ((equal key "FILETAGS")
  4195. (when (string-match "\\S-" value)
  4196. (setq ftags
  4197. (append
  4198. ftags
  4199. (apply 'append
  4200. (mapcar (lambda (x) (org-split-string x ":"))
  4201. (org-split-string value)))))))))))
  4202. ;; Process the file tags.
  4203. (and ftags (org-set-local 'org-file-tags
  4204. (mapcar 'org-add-prop-inherited ftags)))
  4205. (org-set-local 'org-tag-groups-alist nil)
  4206. ;; Process the tags.
  4207. ;; FIXME
  4208. (when tags
  4209. (let (e tgs g)
  4210. (while (setq e (pop tags))
  4211. (cond
  4212. ((equal e "{")
  4213. (progn (push '(:startgroup) tgs)
  4214. (when (equal (nth 1 tags) ":")
  4215. (push (list (replace-regexp-in-string
  4216. "(.+)$" "" (nth 0 tags)))
  4217. org-tag-groups-alist)
  4218. (setq g 0))))
  4219. ((equal e ":") (push '(:grouptags) tgs))
  4220. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4221. ((equal e "\\n") (push '(:newline) tgs))
  4222. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4223. (push (cons (match-string 1 e)
  4224. (string-to-char (match-string 2 e))) tgs)
  4225. (if (and g (> g 0))
  4226. (setcar org-tag-groups-alist
  4227. (append (car org-tag-groups-alist)
  4228. (list (match-string 1 e)))))
  4229. (if g (setq g (1+ g))))
  4230. (t (push (list e) tgs)
  4231. (if (and g (> g 0))
  4232. (setcar org-tag-groups-alist
  4233. (append (car org-tag-groups-alist) (list e))))
  4234. (if g (setq g (1+ g))))))
  4235. (org-set-local 'org-tag-alist nil)
  4236. (while (setq e (pop tgs))
  4237. (or (and (stringp (car e))
  4238. (assoc (car e) org-tag-alist))
  4239. (push e org-tag-alist))))))))
  4240. (defun org-set-regexps-and-options ()
  4241. "Precompute regular expressions used in the current buffer."
  4242. (when (derived-mode-p 'org-mode)
  4243. (org-set-local 'org-todo-kwd-alist nil)
  4244. (org-set-local 'org-todo-key-alist nil)
  4245. (org-set-local 'org-todo-key-trigger nil)
  4246. (org-set-local 'org-todo-keywords-1 nil)
  4247. (org-set-local 'org-done-keywords nil)
  4248. (org-set-local 'org-todo-heads nil)
  4249. (org-set-local 'org-todo-sets nil)
  4250. (org-set-local 'org-todo-log-states nil)
  4251. (org-set-local 'org-file-properties nil)
  4252. (let ((re (org-make-options-regexp
  4253. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4254. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4255. "SETUPFILE" "OPTIONS")
  4256. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4257. (splitre "[ \t]+")
  4258. (scripts org-use-sub-superscripts)
  4259. kwds kws0 kwsa key log value cat arch const links hw dws
  4260. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4261. (start 0))
  4262. (save-excursion
  4263. (save-restriction
  4264. (widen)
  4265. (goto-char (point-min))
  4266. (while (or (and ext-setup-or-nil
  4267. (string-match re ext-setup-or-nil start)
  4268. (setq start (match-end 0)))
  4269. (and (setq ext-setup-or-nil nil start 0)
  4270. (re-search-forward re nil t)))
  4271. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4272. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4273. (if (stringp value) (setq value (org-trim value)))
  4274. (cond
  4275. ((equal key "CATEGORY")
  4276. (setq cat value))
  4277. ((member key '("SEQ_TODO" "TODO"))
  4278. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4279. ((equal key "TYP_TODO")
  4280. (push (cons 'type (org-split-string value splitre)) kwds))
  4281. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4282. ;; general TODO-like setup
  4283. (push (cons (intern (downcase (match-string 1 key)))
  4284. (org-split-string value splitre)) kwds))
  4285. ((equal key "COLUMNS")
  4286. (org-set-local 'org-columns-default-format value))
  4287. ((equal key "LINK")
  4288. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4289. (push (cons (match-string 1 value)
  4290. (org-trim (match-string 2 value)))
  4291. links)))
  4292. ((equal key "PRIORITIES")
  4293. (setq prio (org-split-string value " +")))
  4294. ((equal key "PROPERTY")
  4295. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4296. (setq props (org-update-property-plist (match-string 1 value)
  4297. (match-string 2 value)
  4298. props))))
  4299. ((equal key "DRAWERS")
  4300. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4301. ((equal key "CONSTANTS")
  4302. (org-table-set-constants))
  4303. ((equal key "STARTUP")
  4304. (let ((opts (org-split-string value splitre))
  4305. l var val)
  4306. (while (setq l (pop opts))
  4307. (when (setq l (assoc l org-startup-options))
  4308. (setq var (nth 1 l) val (nth 2 l))
  4309. (if (not (nth 3 l))
  4310. (set (make-local-variable var) val)
  4311. (if (not (listp (symbol-value var)))
  4312. (set (make-local-variable var) nil))
  4313. (set (make-local-variable var) (symbol-value var))
  4314. (add-to-list var val))))))
  4315. ((equal key "ARCHIVE")
  4316. (setq arch value)
  4317. (remove-text-properties 0 (length arch)
  4318. '(face t fontified t) arch))
  4319. ((equal key "OPTIONS")
  4320. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4321. (setq scripts (read (match-string 2 value)))))
  4322. ((and (equal key "SETUPFILE")
  4323. ;; Prevent checking in Gnus messages
  4324. (not buffer-read-only))
  4325. (setq setup-contents (org-file-contents
  4326. (expand-file-name
  4327. (org-remove-double-quotes value))
  4328. 'noerror))
  4329. (if (not ext-setup-or-nil)
  4330. (setq ext-setup-or-nil setup-contents start 0)
  4331. (setq ext-setup-or-nil
  4332. (concat (substring ext-setup-or-nil 0 start)
  4333. "\n" setup-contents "\n"
  4334. (substring ext-setup-or-nil start)))))))
  4335. ;; search for property blocks
  4336. (goto-char (point-min))
  4337. (while (re-search-forward org-block-regexp nil t)
  4338. (when (equal "PROPERTY" (upcase (match-string 1)))
  4339. (setq value (replace-regexp-in-string
  4340. "[\n\r]" " " (match-string 4)))
  4341. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4342. (setq props (org-update-property-plist (match-string 1 value)
  4343. (match-string 2 value)
  4344. props)))))))
  4345. (org-set-local 'org-use-sub-superscripts scripts)
  4346. (when cat
  4347. (org-set-local 'org-category (intern cat))
  4348. (push (cons "CATEGORY" cat) props))
  4349. (when prio
  4350. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4351. (setq prio (mapcar 'string-to-char prio))
  4352. (org-set-local 'org-highest-priority (nth 0 prio))
  4353. (org-set-local 'org-lowest-priority (nth 1 prio))
  4354. (org-set-local 'org-default-priority (nth 2 prio)))
  4355. (and props (org-set-local 'org-file-properties (nreverse props)))
  4356. (and drawers (org-set-local 'org-drawers drawers))
  4357. (and arch (org-set-local 'org-archive-location arch))
  4358. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4359. ;; Process the TODO keywords
  4360. (unless kwds
  4361. ;; Use the global values as if they had been given locally.
  4362. (setq kwds (default-value 'org-todo-keywords))
  4363. (if (stringp (car kwds))
  4364. (setq kwds (list (cons org-todo-interpretation
  4365. (default-value 'org-todo-keywords)))))
  4366. (setq kwds (reverse kwds)))
  4367. (setq kwds (nreverse kwds))
  4368. (let (inter kws kw)
  4369. (while (setq kws (pop kwds))
  4370. (let ((kws (or
  4371. (run-hook-with-args-until-success
  4372. 'org-todo-setup-filter-hook kws)
  4373. kws)))
  4374. (setq inter (pop kws) sep (member "|" kws)
  4375. kws0 (delete "|" (copy-sequence kws))
  4376. kwsa nil
  4377. kws1 (mapcar
  4378. (lambda (x)
  4379. ;; 1 2
  4380. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4381. (progn
  4382. (setq kw (match-string 1 x)
  4383. key (and (match-end 2) (match-string 2 x))
  4384. log (org-extract-log-state-settings x))
  4385. (push (cons kw (and key (string-to-char key))) kwsa)
  4386. (and log (push log org-todo-log-states))
  4387. kw)
  4388. (error "Invalid TODO keyword %s" x)))
  4389. kws0)
  4390. kwsa (if kwsa (append '((:startgroup))
  4391. (nreverse kwsa)
  4392. '((:endgroup))))
  4393. hw (car kws1)
  4394. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4395. tail (list inter hw (car dws) (org-last dws))))
  4396. (add-to-list 'org-todo-heads hw 'append)
  4397. (push kws1 org-todo-sets)
  4398. (setq org-done-keywords (append org-done-keywords dws nil))
  4399. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4400. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4401. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4402. (setq org-todo-sets (nreverse org-todo-sets)
  4403. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4404. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4405. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4406. ;; Compute the regular expressions and other local variables.
  4407. ;; Using `org-outline-regexp-bol' would complicate them much,
  4408. ;; because of the fixed white space at the end of that string.
  4409. (if (not org-done-keywords)
  4410. (setq org-done-keywords (and org-todo-keywords-1
  4411. (list (org-last org-todo-keywords-1)))))
  4412. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4413. (length org-scheduled-string)
  4414. (length org-clock-string)
  4415. (length org-closed-string)))
  4416. org-drawer-regexp
  4417. (concat "^[ \t]*:\\("
  4418. (mapconcat 'regexp-quote org-drawers "\\|")
  4419. "\\):[ \t]*$")
  4420. org-not-done-keywords
  4421. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4422. org-todo-regexp
  4423. (concat "\\("
  4424. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4425. "\\)")
  4426. org-not-done-regexp
  4427. (concat "\\("
  4428. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4429. "\\)")
  4430. org-not-done-heading-regexp
  4431. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4432. org-todo-line-regexp
  4433. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4434. org-complex-heading-regexp
  4435. (concat "^\\(\\*+\\)"
  4436. "\\(?: +" org-todo-regexp "\\)?"
  4437. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4438. "\\(?: +\\(.*?\\)\\)??"
  4439. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4440. "[ \t]*$")
  4441. org-complex-heading-regexp-format
  4442. (concat "^\\(\\*+\\)"
  4443. "\\(?: +" org-todo-regexp "\\)?"
  4444. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4445. "\\(?: +"
  4446. ;; Stats cookies can be stuck to body.
  4447. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4448. "\\(%s\\)"
  4449. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4450. "\\)"
  4451. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4452. "[ \t]*$")
  4453. org-todo-line-tags-regexp
  4454. (concat "^\\(\\*+\\)"
  4455. "\\(?: +" org-todo-regexp "\\)?"
  4456. "\\(?: +\\(.*?\\)\\)??"
  4457. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4458. "[ \t]*$")
  4459. org-deadline-regexp (concat "\\<" org-deadline-string)
  4460. org-deadline-time-regexp
  4461. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4462. org-deadline-time-hour-regexp
  4463. (concat "\\<" org-deadline-string
  4464. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4465. org-deadline-line-regexp
  4466. (concat "\\<\\(" org-deadline-string "\\).*")
  4467. org-scheduled-regexp
  4468. (concat "\\<" org-scheduled-string)
  4469. org-scheduled-time-regexp
  4470. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4471. org-scheduled-time-hour-regexp
  4472. (concat "\\<" org-scheduled-string
  4473. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4474. org-closed-time-regexp
  4475. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4476. org-keyword-time-regexp
  4477. (concat "\\<\\(" org-scheduled-string
  4478. "\\|" org-deadline-string
  4479. "\\|" org-closed-string
  4480. "\\|" org-clock-string "\\)"
  4481. " *[[<]\\([^]>]+\\)[]>]")
  4482. org-keyword-time-not-clock-regexp
  4483. (concat "\\<\\(" org-scheduled-string
  4484. "\\|" org-deadline-string
  4485. "\\|" org-closed-string
  4486. "\\)"
  4487. " *[[<]\\([^]>]+\\)[]>]")
  4488. org-maybe-keyword-time-regexp
  4489. (concat "\\(\\<\\(" org-scheduled-string
  4490. "\\|" org-deadline-string
  4491. "\\|" org-closed-string
  4492. "\\|" org-clock-string "\\)\\)?"
  4493. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4494. org-all-time-keywords
  4495. (mapcar (lambda (w) (substring w 0 -1))
  4496. (list org-scheduled-string org-deadline-string
  4497. org-clock-string org-closed-string)))
  4498. (org-compute-latex-and-related-regexp)
  4499. (org-set-font-lock-defaults))))
  4500. (defun org-file-contents (file &optional noerror)
  4501. "Return the contents of FILE, as a string."
  4502. (if (or (not file)
  4503. (not (file-readable-p file)))
  4504. (if noerror
  4505. (message "Cannot read file \"%s\"" file)
  4506. (error "Cannot read file \"%s\"" file))
  4507. (with-temp-buffer
  4508. (insert-file-contents file)
  4509. (buffer-string))))
  4510. (defun org-extract-log-state-settings (x)
  4511. "Extract the log state setting from a TODO keyword string.
  4512. This will extract info from a string like \"WAIT(w@/!)\"."
  4513. (let (kw key log1 log2)
  4514. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4515. (setq kw (match-string 1 x)
  4516. key (and (match-end 2) (match-string 2 x))
  4517. log1 (and (match-end 3) (match-string 3 x))
  4518. log2 (and (match-end 4) (match-string 4 x)))
  4519. (and (or log1 log2)
  4520. (list kw
  4521. (and log1 (if (equal log1 "!") 'time 'note))
  4522. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4523. (defun org-remove-keyword-keys (list)
  4524. "Remove a pair of parenthesis at the end of each string in LIST."
  4525. (mapcar (lambda (x)
  4526. (if (string-match "(.*)$" x)
  4527. (substring x 0 (match-beginning 0))
  4528. x))
  4529. list))
  4530. (defun org-assign-fast-keys (alist)
  4531. "Assign fast keys to a keyword-key alist.
  4532. Respect keys that are already there."
  4533. (let (new e (alt ?0))
  4534. (while (setq e (pop alist))
  4535. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4536. (cdr e)) ;; Key already assigned.
  4537. (push e new)
  4538. (let ((clist (string-to-list (downcase (car e))))
  4539. (used (append new alist)))
  4540. (when (= (car clist) ?@)
  4541. (pop clist))
  4542. (while (and clist (rassoc (car clist) used))
  4543. (pop clist))
  4544. (unless clist
  4545. (while (rassoc alt used)
  4546. (incf alt)))
  4547. (push (cons (car e) (or (car clist) alt)) new))))
  4548. (nreverse new)))
  4549. ;;; Some variables used in various places
  4550. (defvar org-window-configuration nil
  4551. "Used in various places to store a window configuration.")
  4552. (defvar org-selected-window nil
  4553. "Used in various places to store a window configuration.")
  4554. (defvar org-finish-function nil
  4555. "Function to be called when `C-c C-c' is used.
  4556. This is for getting out of special buffers like capture.")
  4557. ;; FIXME: Occasionally check by commenting these, to make sure
  4558. ;; no other functions uses these, forgetting to let-bind them.
  4559. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4560. (defvar org-last-state)
  4561. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4562. ;; Defined somewhere in this file, but used before definition.
  4563. (defvar org-entities) ;; defined in org-entities.el
  4564. (defvar org-struct-menu)
  4565. (defvar org-org-menu)
  4566. (defvar org-tbl-menu)
  4567. ;;;; Define the Org-mode
  4568. ;; We use a before-change function to check if a table might need
  4569. ;; an update.
  4570. (defvar org-table-may-need-update t
  4571. "Indicates that a table might need an update.
  4572. This variable is set by `org-before-change-function'.
  4573. `org-table-align' sets it back to nil.")
  4574. (defun org-before-change-function (beg end)
  4575. "Every change indicates that a table might need an update."
  4576. (setq org-table-may-need-update t))
  4577. (defvar org-mode-map)
  4578. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4579. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4580. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4581. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4582. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4583. (defvar org-table-buffer-is-an nil)
  4584. (defvar bidi-paragraph-direction)
  4585. (defvar buffer-face-mode-face)
  4586. (require 'outline)
  4587. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4588. (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"))
  4589. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4590. ;; Other stuff we need.
  4591. (require 'time-date)
  4592. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4593. (require 'easymenu)
  4594. (require 'overlay)
  4595. ;; (require 'org-macs) moved higher up in the file before it is first used
  4596. (require 'org-entities)
  4597. ;; (require 'org-compat) moved higher up in the file before it is first used
  4598. (require 'org-faces)
  4599. (require 'org-list)
  4600. (require 'org-pcomplete)
  4601. (require 'org-src)
  4602. (require 'org-footnote)
  4603. (require 'org-macro)
  4604. ;; babel
  4605. (require 'ob)
  4606. ;;;###autoload
  4607. (define-derived-mode org-mode outline-mode "Org"
  4608. "Outline-based notes management and organizer, alias
  4609. \"Carsten's outline-mode for keeping track of everything.\"
  4610. Org-mode develops organizational tasks around a NOTES file which
  4611. contains information about projects as plain text. Org-mode is
  4612. implemented on top of outline-mode, which is ideal to keep the content
  4613. of large files well structured. It supports ToDo items, deadlines and
  4614. time stamps, which magically appear in the diary listing of the Emacs
  4615. calendar. Tables are easily created with a built-in table editor.
  4616. Plain text URL-like links connect to websites, emails (VM), Usenet
  4617. messages (Gnus), BBDB entries, and any files related to the project.
  4618. For printing and sharing of notes, an Org-mode file (or a part of it)
  4619. can be exported as a structured ASCII or HTML file.
  4620. The following commands are available:
  4621. \\{org-mode-map}"
  4622. ;; Get rid of Outline menus, they are not needed
  4623. ;; Need to do this here because define-derived-mode sets up
  4624. ;; the keymap so late. Still, it is a waste to call this each time
  4625. ;; we switch another buffer into org-mode.
  4626. (if (featurep 'xemacs)
  4627. (when (boundp 'outline-mode-menu-heading)
  4628. ;; Assume this is Greg's port, it uses easymenu
  4629. (easy-menu-remove outline-mode-menu-heading)
  4630. (easy-menu-remove outline-mode-menu-show)
  4631. (easy-menu-remove outline-mode-menu-hide))
  4632. (define-key org-mode-map [menu-bar headings] 'undefined)
  4633. (define-key org-mode-map [menu-bar hide] 'undefined)
  4634. (define-key org-mode-map [menu-bar show] 'undefined))
  4635. (org-load-modules-maybe)
  4636. (easy-menu-add org-org-menu)
  4637. (easy-menu-add org-tbl-menu)
  4638. (org-install-agenda-files-menu)
  4639. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4640. (add-to-invisibility-spec '(org-cwidth))
  4641. (add-to-invisibility-spec '(org-hide-block . t))
  4642. (when (featurep 'xemacs)
  4643. (org-set-local 'line-move-ignore-invisible t))
  4644. (org-set-local 'outline-regexp org-outline-regexp)
  4645. (org-set-local 'outline-level 'org-outline-level)
  4646. (setq bidi-paragraph-direction 'left-to-right)
  4647. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4648. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4649. (when (and org-ellipsis
  4650. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4651. (fboundp 'make-glyph-code))
  4652. (unless org-display-table
  4653. (setq org-display-table (make-display-table)))
  4654. (set-display-table-slot
  4655. org-display-table 4
  4656. (vconcat (mapcar
  4657. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4658. org-ellipsis)))
  4659. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4660. (setq buffer-display-table org-display-table))
  4661. (org-set-regexps-and-options-for-tags)
  4662. (org-set-regexps-and-options)
  4663. (when (and org-tag-faces (not org-tags-special-faces-re))
  4664. ;; tag faces set outside customize.... force initialization.
  4665. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4666. ;; Calc embedded
  4667. (org-set-local 'calc-embedded-open-mode "# ")
  4668. ;; Modify a few syntax entries
  4669. (modify-syntax-entry ?@ "w")
  4670. (modify-syntax-entry ?\" "\"")
  4671. (if org-startup-truncated (setq truncate-lines t))
  4672. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4673. (org-set-local 'font-lock-unfontify-region-function
  4674. 'org-unfontify-region)
  4675. ;; Activate before-change-function
  4676. (org-set-local 'org-table-may-need-update t)
  4677. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4678. 'local)
  4679. ;; Check for running clock before killing a buffer
  4680. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4681. ;; Initialize macros templates.
  4682. (org-macro-initialize-templates)
  4683. ;; Initialize radio targets.
  4684. (org-update-radio-target-regexp)
  4685. ;; Indentation.
  4686. (org-set-local 'indent-line-function 'org-indent-line)
  4687. (org-set-local 'indent-region-function 'org-indent-region)
  4688. ;; Filling and auto-filling.
  4689. (org-setup-filling)
  4690. ;; Comments.
  4691. (org-setup-comments-handling)
  4692. ;; Beginning/end of defun
  4693. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4694. (org-set-local 'end-of-defun-function 'org-forward-element)
  4695. ;; Next error for sparse trees
  4696. (org-set-local 'next-error-function 'org-occur-next-match)
  4697. ;; Make sure dependence stuff works reliably, even for users who set it
  4698. ;; too late :-(
  4699. (if org-enforce-todo-dependencies
  4700. (add-hook 'org-blocker-hook
  4701. 'org-block-todo-from-children-or-siblings-or-parent)
  4702. (remove-hook 'org-blocker-hook
  4703. 'org-block-todo-from-children-or-siblings-or-parent))
  4704. (if org-enforce-todo-checkbox-dependencies
  4705. (add-hook 'org-blocker-hook
  4706. 'org-block-todo-from-checkboxes)
  4707. (remove-hook 'org-blocker-hook
  4708. 'org-block-todo-from-checkboxes))
  4709. ;; Align options lines
  4710. (org-set-local
  4711. 'align-mode-rules-list
  4712. '((org-in-buffer-settings
  4713. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4714. (modes . '(org-mode)))))
  4715. ;; Imenu
  4716. (org-set-local 'imenu-create-index-function
  4717. 'org-imenu-get-tree)
  4718. ;; Make isearch reveal context
  4719. (if (or (featurep 'xemacs)
  4720. (not (boundp 'outline-isearch-open-invisible-function)))
  4721. ;; Emacs 21 and XEmacs make use of the hook
  4722. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4723. ;; Emacs 22 deals with this through a special variable
  4724. (org-set-local 'outline-isearch-open-invisible-function
  4725. (lambda (&rest ignore) (org-show-context 'isearch)))
  4726. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4727. ;; Setup the pcomplete hooks
  4728. (set (make-local-variable 'pcomplete-command-completion-function)
  4729. 'org-pcomplete-initial)
  4730. (set (make-local-variable 'pcomplete-command-name-function)
  4731. 'org-command-at-point)
  4732. (set (make-local-variable 'pcomplete-default-completion-function)
  4733. 'ignore)
  4734. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4735. 'org-parse-arguments)
  4736. (set (make-local-variable 'pcomplete-termination-string) "")
  4737. (when (>= emacs-major-version 23)
  4738. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4739. ;; If empty file that did not turn on org-mode automatically, make it to.
  4740. (if (and org-insert-mode-line-in-empty-file
  4741. (org-called-interactively-p 'any)
  4742. (= (point-min) (point-max)))
  4743. (insert "# -*- mode: org -*-\n\n"))
  4744. (unless org-inhibit-startup
  4745. (org-unmodified
  4746. (and org-startup-with-beamer-mode (org-beamer-mode))
  4747. (when org-startup-align-all-tables
  4748. (org-table-map-tables 'org-table-align 'quietly))
  4749. (when org-startup-with-inline-images
  4750. (org-display-inline-images))
  4751. (when org-startup-with-latex-preview
  4752. (org-preview-latex-fragment))
  4753. (unless org-inhibit-startup-visibility-stuff
  4754. (org-set-startup-visibility))))
  4755. ;; Try to set org-hide correctly
  4756. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4757. ;; Update `customize-package-emacs-version-alist'
  4758. (add-to-list 'customize-package-emacs-version-alist
  4759. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4760. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4761. ("8.0" . "24.4")))
  4762. (defvar org-mode-transpose-word-syntax-table
  4763. (let ((st (make-syntax-table)))
  4764. (mapc (lambda(c) (modify-syntax-entry
  4765. (string-to-char (car c)) "w p" st))
  4766. org-emphasis-alist)
  4767. st))
  4768. (when (fboundp 'abbrev-table-put)
  4769. (abbrev-table-put org-mode-abbrev-table
  4770. :parents (list text-mode-abbrev-table)))
  4771. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4772. (defsubst org-fix-ellipsis-at-bol ()
  4773. (save-excursion (goto-char (window-start)) (recenter 0)))
  4774. (defun org-find-invisible-foreground ()
  4775. (let ((candidates (remove
  4776. "unspecified-bg"
  4777. (nconc
  4778. (list (face-background 'default)
  4779. (face-background 'org-default))
  4780. (mapcar
  4781. (lambda (alist)
  4782. (when (boundp alist)
  4783. (cdr (assoc 'background-color (symbol-value alist)))))
  4784. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4785. (list (face-foreground 'org-hide))))))
  4786. (car (remove nil candidates))))
  4787. (defun org-current-time (&optional rounding-minutes past)
  4788. "Current time, possibly rounded to ROUNDING-MINUTES.
  4789. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4790. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4791. the rounding returns a past time."
  4792. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4793. (car org-time-stamp-rounding-minutes)))
  4794. (time (decode-time)) res)
  4795. (if (< r 1)
  4796. (current-time)
  4797. (setq res
  4798. (apply 'encode-time
  4799. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4800. (nthcdr 2 time))))
  4801. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4802. (seconds-to-time (- (org-float-time res) (* r 60)))
  4803. res))))
  4804. (defun org-today ()
  4805. "Return today date, considering `org-extend-today-until'."
  4806. (time-to-days
  4807. (time-subtract (current-time)
  4808. (list 0 (* 3600 org-extend-today-until) 0))))
  4809. ;;;; Font-Lock stuff, including the activators
  4810. (defvar org-mouse-map (make-sparse-keymap))
  4811. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4812. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4813. (when org-mouse-1-follows-link
  4814. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4815. (when org-tab-follows-link
  4816. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4817. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4818. (require 'font-lock)
  4819. (defconst org-non-link-chars "]\t\n\r<>")
  4820. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4821. "shell" "elisp" "doi" "message"))
  4822. (defvar org-link-types-re nil
  4823. "Matches a link that has a url-like prefix like \"http:\"")
  4824. (defvar org-link-re-with-space nil
  4825. "Matches a link with spaces, optional angular brackets around it.")
  4826. (defvar org-link-re-with-space2 nil
  4827. "Matches a link with spaces, optional angular brackets around it.")
  4828. (defvar org-link-re-with-space3 nil
  4829. "Matches a link with spaces, only for internal part in bracket links.")
  4830. (defvar org-angle-link-re nil
  4831. "Matches link with angular brackets, spaces are allowed.")
  4832. (defvar org-plain-link-re nil
  4833. "Matches plain link, without spaces.")
  4834. (defvar org-bracket-link-regexp nil
  4835. "Matches a link in double brackets.")
  4836. (defvar org-bracket-link-analytic-regexp nil
  4837. "Regular expression used to analyze links.
  4838. Here is what the match groups contain after a match:
  4839. 1: http:
  4840. 2: http
  4841. 3: path
  4842. 4: [desc]
  4843. 5: desc")
  4844. (defvar org-bracket-link-analytic-regexp++ nil
  4845. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4846. (defvar org-any-link-re nil
  4847. "Regular expression matching any link.")
  4848. (defconst org-match-sexp-depth 3
  4849. "Number of stacked braces for sub/superscript matching.")
  4850. (defun org-create-multibrace-regexp (left right n)
  4851. "Create a regular expression which will match a balanced sexp.
  4852. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4853. as single character strings.
  4854. The regexp returned will match the entire expression including the
  4855. delimiters. It will also define a single group which contains the
  4856. match except for the outermost delimiters. The maximum depth of
  4857. stacked delimiters is N. Escaping delimiters is not possible."
  4858. (let* ((nothing (concat "[^" left right "]*?"))
  4859. (or "\\|")
  4860. (re nothing)
  4861. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4862. (while (> n 1)
  4863. (setq n (1- n)
  4864. re (concat re or next)
  4865. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4866. (concat left "\\(" re "\\)" right)))
  4867. (defvar org-match-substring-regexp
  4868. (concat
  4869. "\\(\\S-\\)\\([_^]\\)\\("
  4870. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4871. "\\|"
  4872. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4873. "\\|"
  4874. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4875. "The regular expression matching a sub- or superscript.")
  4876. (defvar org-match-substring-with-braces-regexp
  4877. (concat
  4878. "\\(\\S-\\)\\([_^]\\)\\("
  4879. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4880. "\\)")
  4881. "The regular expression matching a sub- or superscript, forcing braces.")
  4882. (defun org-make-link-regexps ()
  4883. "Update the link regular expressions.
  4884. This should be called after the variable `org-link-types' has changed."
  4885. (setq org-link-types-re
  4886. (concat
  4887. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4888. org-link-re-with-space
  4889. (concat
  4890. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4891. "\\([^" org-non-link-chars " ]"
  4892. "[^" org-non-link-chars "]*"
  4893. "[^" org-non-link-chars " ]\\)>?")
  4894. org-link-re-with-space2
  4895. (concat
  4896. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4897. "\\([^" org-non-link-chars " ]"
  4898. "[^\t\n\r]*"
  4899. "[^" org-non-link-chars " ]\\)>?")
  4900. org-link-re-with-space3
  4901. (concat
  4902. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4903. "\\([^" org-non-link-chars " ]"
  4904. "[^\t\n\r]*\\)")
  4905. org-angle-link-re
  4906. (concat
  4907. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4908. "\\([^" org-non-link-chars " ]"
  4909. "[^" org-non-link-chars "]*"
  4910. "\\)>")
  4911. org-plain-link-re
  4912. (concat
  4913. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4914. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4915. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4916. org-bracket-link-regexp
  4917. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4918. org-bracket-link-analytic-regexp
  4919. (concat
  4920. "\\[\\["
  4921. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4922. "\\([^]]+\\)"
  4923. "\\]"
  4924. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4925. "\\]")
  4926. org-bracket-link-analytic-regexp++
  4927. (concat
  4928. "\\[\\["
  4929. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4930. "\\([^]]+\\)"
  4931. "\\]"
  4932. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4933. "\\]")
  4934. org-any-link-re
  4935. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4936. org-angle-link-re "\\)\\|\\("
  4937. org-plain-link-re "\\)")))
  4938. (org-make-link-regexps)
  4939. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4940. "Regular expression for fast time stamp matching.")
  4941. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4942. "Regular expression for fast time stamp matching.")
  4943. (defconst org-ts-regexp0
  4944. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4945. "Regular expression matching time strings for analysis.
  4946. This one does not require the space after the date, so it can be used
  4947. on a string that terminates immediately after the date.")
  4948. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4949. "Regular expression matching time strings for analysis.")
  4950. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4951. "Regular expression matching time stamps, with groups.")
  4952. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4953. "Regular expression matching time stamps (also [..]), with groups.")
  4954. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4955. "Regular expression matching a time stamp range.")
  4956. (defconst org-tr-regexp-both
  4957. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4958. "Regular expression matching a time stamp range.")
  4959. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4960. org-ts-regexp "\\)?")
  4961. "Regular expression matching a time stamp or time stamp range.")
  4962. (defconst org-tsr-regexp-both
  4963. (concat org-ts-regexp-both "\\(--?-?"
  4964. org-ts-regexp-both "\\)?")
  4965. "Regular expression matching a time stamp or time stamp range.
  4966. The time stamps may be either active or inactive.")
  4967. (defvar org-emph-face nil)
  4968. (defun org-do-emphasis-faces (limit)
  4969. "Run through the buffer and add overlays to emphasized strings."
  4970. (let (rtn a)
  4971. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4972. (if (not (= (char-after (match-beginning 3))
  4973. (char-after (match-beginning 4))))
  4974. (progn
  4975. (setq rtn t)
  4976. (setq a (assoc (match-string 3) org-emphasis-alist))
  4977. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4978. 'face
  4979. (nth 1 a))
  4980. (and (nth 4 a)
  4981. (org-remove-flyspell-overlays-in
  4982. (match-beginning 0) (match-end 0)))
  4983. (add-text-properties (match-beginning 2) (match-end 2)
  4984. '(font-lock-multiline t org-emphasis t))
  4985. (when org-hide-emphasis-markers
  4986. (add-text-properties (match-end 4) (match-beginning 5)
  4987. '(invisible org-link))
  4988. (add-text-properties (match-beginning 3) (match-end 3)
  4989. '(invisible org-link)))))
  4990. (backward-char 1))
  4991. rtn))
  4992. (defun org-emphasize (&optional char)
  4993. "Insert or change an emphasis, i.e. a font like bold or italic.
  4994. If there is an active region, change that region to a new emphasis.
  4995. If there is no region, just insert the marker characters and position
  4996. the cursor between them.
  4997. CHAR should be either the marker character, or the first character of the
  4998. HTML tag associated with that emphasis. If CHAR is a space, the means
  4999. to remove the emphasis of the selected region.
  5000. If char is not given (for example in an interactive call) it
  5001. will be prompted for."
  5002. (interactive)
  5003. (let ((eal org-emphasis-alist) e det
  5004. (erc org-emphasis-regexp-components)
  5005. (prompt "")
  5006. (string "") beg end move tag c s)
  5007. (if (org-region-active-p)
  5008. (setq beg (region-beginning) end (region-end)
  5009. string (buffer-substring beg end))
  5010. (setq move t))
  5011. (while (setq e (pop eal))
  5012. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  5013. c (aref tag 0))
  5014. (push (cons c (string-to-char (car e))) det)
  5015. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  5016. (substring tag 1)))))
  5017. (setq det (nreverse det))
  5018. (unless char
  5019. (message "%s" (concat "Emphasis marker or tag:" prompt))
  5020. (setq char (read-char-exclusive)))
  5021. (setq char (or (cdr (assoc char det)) char))
  5022. (if (equal char ?\ )
  5023. (setq s "" move nil)
  5024. (unless (assoc (char-to-string char) org-emphasis-alist)
  5025. (error "No such emphasis marker: \"%c\"" char))
  5026. (setq s (char-to-string char)))
  5027. (while (and (> (length string) 1)
  5028. (equal (substring string 0 1) (substring string -1))
  5029. (assoc (substring string 0 1) org-emphasis-alist))
  5030. (setq string (substring string 1 -1)))
  5031. (setq string (concat s string s))
  5032. (if beg (delete-region beg end))
  5033. (unless (or (bolp)
  5034. (string-match (concat "[" (nth 0 erc) "\n]")
  5035. (char-to-string (char-before (point)))))
  5036. (insert " "))
  5037. (unless (or (eobp)
  5038. (string-match (concat "[" (nth 1 erc) "\n]")
  5039. (char-to-string (char-after (point)))))
  5040. (insert " ") (backward-char 1))
  5041. (insert string)
  5042. (and move (backward-char 1))))
  5043. (defconst org-nonsticky-props
  5044. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5045. (defsubst org-rear-nonsticky-at (pos)
  5046. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5047. (defun org-activate-plain-links (limit)
  5048. "Run through the buffer and add overlays to links."
  5049. (let (f hl)
  5050. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5051. (not (org-in-src-block-p)))
  5052. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5053. (setq f (get-text-property (match-beginning 0) 'face))
  5054. (setq hl (org-match-string-no-properties 0))
  5055. (if (or (eq f 'org-tag)
  5056. (and (listp f) (memq 'org-tag f)))
  5057. nil
  5058. (add-text-properties (match-beginning 0) (match-end 0)
  5059. (list 'mouse-face 'highlight
  5060. 'face 'org-link
  5061. 'htmlize-link `(:uri ,hl)
  5062. 'keymap org-mouse-map))
  5063. (org-rear-nonsticky-at (match-end 0)))
  5064. t)))
  5065. (defun org-activate-code (limit)
  5066. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5067. (progn
  5068. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5069. (remove-text-properties (match-beginning 0) (match-end 0)
  5070. '(display t invisible t intangible t))
  5071. t)))
  5072. (defcustom org-src-fontify-natively nil
  5073. "When non-nil, fontify code in code blocks."
  5074. :type 'boolean
  5075. :version "24.1"
  5076. :group 'org-appearance
  5077. :group 'org-babel)
  5078. (defcustom org-allow-promoting-top-level-subtree nil
  5079. "When non-nil, allow promoting a top level subtree.
  5080. The leading star of the top level headline will be replaced
  5081. by a #."
  5082. :type 'boolean
  5083. :version "24.1"
  5084. :group 'org-appearance)
  5085. (defun org-fontify-meta-lines-and-blocks (limit)
  5086. (condition-case nil
  5087. (org-fontify-meta-lines-and-blocks-1 limit)
  5088. (error (message "org-mode fontification error"))))
  5089. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5090. "Fontify #+ lines and blocks."
  5091. (let ((case-fold-search t))
  5092. (if (re-search-forward
  5093. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5094. limit t)
  5095. (let ((beg (match-beginning 0))
  5096. (block-start (match-end 0))
  5097. (block-end nil)
  5098. (lang (match-string 7))
  5099. (beg1 (line-beginning-position 2))
  5100. (dc1 (downcase (match-string 2)))
  5101. (dc3 (downcase (match-string 3)))
  5102. end end1 quoting block-type ovl)
  5103. (cond
  5104. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5105. ;; a single line of backend-specific content
  5106. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5107. (remove-text-properties (match-beginning 0) (match-end 0)
  5108. '(display t invisible t intangible t))
  5109. (add-text-properties (match-beginning 1) (match-end 3)
  5110. '(font-lock-fontified t face org-meta-line))
  5111. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5112. '(font-lock-fontified t face org-block))
  5113. ; for backend-specific code
  5114. t)
  5115. ((and (match-end 4) (equal dc3 "+begin"))
  5116. ;; Truly a block
  5117. (setq block-type (downcase (match-string 5))
  5118. quoting (member block-type org-protecting-blocks))
  5119. (when (re-search-forward
  5120. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5121. nil t) ;; on purpose, we look further than LIMIT
  5122. (setq end (min (point-max) (match-end 0))
  5123. end1 (min (point-max) (1- (match-beginning 0))))
  5124. (setq block-end (match-beginning 0))
  5125. (when quoting
  5126. (remove-text-properties beg end
  5127. '(display t invisible t intangible t)))
  5128. (add-text-properties
  5129. beg end
  5130. '(font-lock-fontified t font-lock-multiline t))
  5131. (add-text-properties beg beg1 '(face org-meta-line))
  5132. (add-text-properties end1 (min (point-max) (1+ end))
  5133. '(face org-meta-line)) ; for end_src
  5134. (cond
  5135. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5136. (org-src-font-lock-fontify-block lang block-start block-end)
  5137. ;; remove old background overlays
  5138. (mapc (lambda (ov)
  5139. (if (eq (overlay-get ov 'face) 'org-block-background)
  5140. (delete-overlay ov)))
  5141. (overlays-at (/ (+ beg1 block-end) 2)))
  5142. ;; add a background overlay
  5143. (setq ovl (make-overlay beg1 block-end))
  5144. (overlay-put ovl 'face 'org-block-background)
  5145. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5146. (quoting
  5147. (add-text-properties beg1 (min (point-max) (1+ end1))
  5148. '(face org-block))) ; end of source block
  5149. ((not org-fontify-quote-and-verse-blocks))
  5150. ((string= block-type "quote")
  5151. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5152. ((string= block-type "verse")
  5153. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5154. (add-text-properties beg beg1 '(face org-block-begin-line))
  5155. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5156. '(face org-block-end-line))
  5157. t))
  5158. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5159. (add-text-properties
  5160. beg (match-end 3)
  5161. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5162. '(font-lock-fontified t invisible t)
  5163. '(font-lock-fontified t face org-document-info-keyword)))
  5164. (add-text-properties
  5165. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5166. (if (string-equal dc1 "+title:")
  5167. '(font-lock-fontified t face org-document-title)
  5168. '(font-lock-fontified t face org-document-info))))
  5169. ((or (equal dc1 "+results")
  5170. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5171. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5172. "+call:" "+header:" "+headers:" "+name:"))
  5173. (and (match-end 4) (equal dc3 "+attr")))
  5174. (add-text-properties
  5175. beg (match-end 0)
  5176. '(font-lock-fontified t face org-meta-line))
  5177. t)
  5178. ((member dc3 '(" " ""))
  5179. (add-text-properties
  5180. beg (match-end 0)
  5181. '(font-lock-fontified t face font-lock-comment-face)))
  5182. ((not (member (char-after beg) '(?\ ?\t)))
  5183. ;; just any other in-buffer setting, but not indented
  5184. (add-text-properties
  5185. beg (match-end 0)
  5186. '(font-lock-fontified t face org-meta-line))
  5187. t)
  5188. (t nil))))))
  5189. (defun org-activate-angle-links (limit)
  5190. "Run through the buffer and add overlays to links."
  5191. (if (and (re-search-forward org-angle-link-re limit t)
  5192. (not (org-in-src-block-p)))
  5193. (progn
  5194. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5195. (add-text-properties (match-beginning 0) (match-end 0)
  5196. (list 'mouse-face 'highlight
  5197. 'keymap org-mouse-map))
  5198. (org-rear-nonsticky-at (match-end 0))
  5199. t)))
  5200. (defun org-activate-footnote-links (limit)
  5201. "Run through the buffer and add overlays to footnotes."
  5202. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5203. (when fn
  5204. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5205. (org-remove-flyspell-overlays-in beg end)
  5206. (add-text-properties beg end
  5207. (list 'mouse-face 'highlight
  5208. 'keymap org-mouse-map
  5209. 'help-echo
  5210. (if (= (point-at-bol) beg)
  5211. "Footnote definition"
  5212. "Footnote reference")
  5213. 'font-lock-fontified t
  5214. 'font-lock-multiline t
  5215. 'face 'org-footnote))))))
  5216. (defun org-activate-bracket-links (limit)
  5217. "Run through the buffer and add overlays to bracketed links."
  5218. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5219. (not (org-in-src-block-p)))
  5220. (let* ((hl (org-match-string-no-properties 1))
  5221. (help (concat "LINK: " hl))
  5222. ;; FIXME: Above we should remove the escapes. But that
  5223. ;; requires another match, protecting match data, a lot
  5224. ;; of overhead for font-lock.
  5225. (ip (org-maybe-intangible
  5226. (list 'invisible 'org-link
  5227. 'keymap org-mouse-map 'mouse-face 'highlight
  5228. 'font-lock-multiline t 'help-echo help
  5229. 'htmlize-link `(:uri ,hl))))
  5230. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5231. 'font-lock-multiline t 'help-echo help
  5232. 'htmlize-link `(:uri ,hl))))
  5233. ;; We need to remove the invisible property here. Table narrowing
  5234. ;; may have made some of this invisible.
  5235. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5236. (remove-text-properties (match-beginning 0) (match-end 0)
  5237. '(invisible nil))
  5238. (if (match-end 3)
  5239. (progn
  5240. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5241. (org-rear-nonsticky-at (match-beginning 3))
  5242. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5243. (org-rear-nonsticky-at (match-end 3))
  5244. (add-text-properties (match-end 3) (match-end 0) ip)
  5245. (org-rear-nonsticky-at (match-end 0)))
  5246. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5247. (org-rear-nonsticky-at (match-beginning 1))
  5248. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5249. (org-rear-nonsticky-at (match-end 1))
  5250. (add-text-properties (match-end 1) (match-end 0) ip)
  5251. (org-rear-nonsticky-at (match-end 0)))
  5252. t)))
  5253. (defun org-activate-dates (limit)
  5254. "Run through the buffer and add overlays to dates."
  5255. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5256. (not (equal (char-before (match-beginning 0)) 91)))
  5257. (progn
  5258. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5259. (add-text-properties (match-beginning 0) (match-end 0)
  5260. (list 'mouse-face 'highlight
  5261. 'keymap org-mouse-map))
  5262. (org-rear-nonsticky-at (match-end 0))
  5263. (when org-display-custom-times
  5264. (if (match-end 3)
  5265. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5266. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5267. t)))
  5268. (defvar org-target-link-regexp nil
  5269. "Regular expression matching radio targets in plain text.")
  5270. (make-variable-buffer-local 'org-target-link-regexp)
  5271. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5272. "Regular expression matching a link target.")
  5273. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5274. "Regular expression matching a radio target.")
  5275. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5276. "Regular expression matching any target.")
  5277. (defun org-activate-target-links (limit)
  5278. "Run through the buffer and add overlays to target matches."
  5279. (when org-target-link-regexp
  5280. (let ((case-fold-search t))
  5281. (if (re-search-forward org-target-link-regexp limit t)
  5282. (progn
  5283. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5284. (add-text-properties (match-beginning 0) (match-end 0)
  5285. (list 'mouse-face 'highlight
  5286. 'keymap org-mouse-map
  5287. 'help-echo "Radio target link"
  5288. 'org-linked-text t))
  5289. (org-rear-nonsticky-at (match-end 0))
  5290. t)))))
  5291. (defun org-update-radio-target-regexp ()
  5292. "Find all radio targets in this file and update the regular expression."
  5293. (interactive)
  5294. (when (memq 'radio org-activate-links)
  5295. (setq org-target-link-regexp
  5296. (org-make-target-link-regexp (org-all-targets 'radio)))
  5297. (org-restart-font-lock)))
  5298. (defun org-hide-wide-columns (limit)
  5299. (let (s e)
  5300. (setq s (text-property-any (point) (or limit (point-max))
  5301. 'org-cwidth t))
  5302. (when s
  5303. (setq e (next-single-property-change s 'org-cwidth))
  5304. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5305. (goto-char e)
  5306. t)))
  5307. (defvar org-latex-and-related-regexp nil
  5308. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5309. (defvar org-match-substring-regexp)
  5310. (defvar org-match-substring-with-braces-regexp)
  5311. (defun org-compute-latex-and-related-regexp ()
  5312. "Compute regular expression for LaTeX, entities and sub/superscript.
  5313. Result depends on variable `org-highlight-latex-and-related'."
  5314. (org-set-local
  5315. 'org-latex-and-related-regexp
  5316. (let* ((re-sub
  5317. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5318. ((eq org-use-sub-superscripts '{})
  5319. (list org-match-substring-with-braces-regexp))
  5320. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5321. (re-latex
  5322. (when (memq 'latex org-highlight-latex-and-related)
  5323. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5324. (delq nil
  5325. (mapcar (lambda (x)
  5326. (and (member (car x) matchers) (nth 1 x)))
  5327. org-latex-regexps)))))
  5328. (re-entities
  5329. (when (memq 'entities org-highlight-latex-and-related)
  5330. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5331. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5332. (defun org-do-latex-and-related (limit)
  5333. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5334. LIMIT bounds the search for syntax to highlight. Stop at first
  5335. highlighted object, if any. Return t if some highlighting was
  5336. done, nil otherwise."
  5337. (when (org-string-nw-p org-latex-and-related-regexp)
  5338. (catch 'found
  5339. (while (re-search-forward org-latex-and-related-regexp limit t)
  5340. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5341. 'face))
  5342. '(org-code org-verbatim underline))
  5343. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5344. '(?_ ?^))
  5345. 1
  5346. 0)))
  5347. (font-lock-prepend-text-property
  5348. (+ offset (match-beginning 0)) (match-end 0)
  5349. 'face 'org-latex-and-related)
  5350. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5351. '(font-lock-multiline t)))
  5352. (throw 'found t)))
  5353. nil)))
  5354. (defun org-restart-font-lock ()
  5355. "Restart `font-lock-mode', to force refontification."
  5356. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5357. (font-lock-mode -1)
  5358. (font-lock-mode 1)))
  5359. (defun org-all-targets (&optional radio)
  5360. "Return a list of all targets in this file.
  5361. When optional argument RADIO is non-nil, only find radio
  5362. targets."
  5363. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5364. (save-excursion
  5365. (goto-char (point-min))
  5366. (while (re-search-forward re nil t)
  5367. ;; Make sure point is really within the object.
  5368. (backward-char)
  5369. (let ((obj (org-element-context)))
  5370. (when (memq (org-element-type obj) '(radio-target target))
  5371. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5372. rtn)))
  5373. (defun org-make-target-link-regexp (targets)
  5374. "Make regular expression matching all strings in TARGETS.
  5375. The regular expression finds the targets also if there is a line break
  5376. between words."
  5377. (and targets
  5378. (concat
  5379. "\\<\\("
  5380. (mapconcat
  5381. (lambda (x)
  5382. (setq x (regexp-quote x))
  5383. (while (string-match " +" x)
  5384. (setq x (replace-match "\\s-+" t t x)))
  5385. x)
  5386. targets
  5387. "\\|")
  5388. "\\)\\>")))
  5389. (defun org-activate-tags (limit)
  5390. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5391. (progn
  5392. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5393. (add-text-properties (match-beginning 1) (match-end 1)
  5394. (list 'mouse-face 'highlight
  5395. 'keymap org-mouse-map))
  5396. (org-rear-nonsticky-at (match-end 1))
  5397. t)))
  5398. (defun org-outline-level ()
  5399. "Compute the outline level of the heading at point.
  5400. If this is called at a normal headline, the level is the number of stars.
  5401. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5402. (save-excursion
  5403. (if (not (condition-case nil
  5404. (org-back-to-heading t)
  5405. (error nil)))
  5406. 0
  5407. (looking-at org-outline-regexp)
  5408. (1- (- (match-end 0) (match-beginning 0))))))
  5409. (defvar org-font-lock-keywords nil)
  5410. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5411. "Regular expression matching a property line.")
  5412. (defvar org-font-lock-hook nil
  5413. "Functions to be called for special font lock stuff.")
  5414. (defvar org-font-lock-set-keywords-hook nil
  5415. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5416. This is called after `org-font-lock-extra-keywords' is defined, but before
  5417. it is installed to be used by font lock. This can be useful if something
  5418. needs to be inserted at a specific position in the font-lock sequence.")
  5419. (defun org-font-lock-hook (limit)
  5420. "Run `org-font-lock-hook' within LIMIT."
  5421. (run-hook-with-args 'org-font-lock-hook limit))
  5422. (defun org-set-font-lock-defaults ()
  5423. "Set font lock defaults for the current buffer."
  5424. (let* ((em org-fontify-emphasized-text)
  5425. (lk org-activate-links)
  5426. (org-font-lock-extra-keywords
  5427. (list
  5428. ;; Call the hook
  5429. '(org-font-lock-hook)
  5430. ;; Headlines
  5431. `(,(if org-fontify-whole-heading-line
  5432. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5433. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5434. (1 (org-get-level-face 1))
  5435. (2 (org-get-level-face 2))
  5436. (3 (org-get-level-face 3)))
  5437. ;; Table lines
  5438. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5439. (1 'org-table t))
  5440. ;; Table internals
  5441. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5442. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5443. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5444. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5445. ;; Drawers
  5446. (list org-drawer-regexp '(0 'org-special-keyword t))
  5447. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5448. ;; Properties
  5449. (list org-property-re
  5450. '(1 'org-special-keyword t)
  5451. '(3 'org-property-value t))
  5452. ;; Links
  5453. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5454. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5455. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5456. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5457. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5458. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5459. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5460. ;; Targets.
  5461. (list org-any-target-regexp '(0 'org-target t))
  5462. ;; Diary sexps.
  5463. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5464. ;; Macro
  5465. '("{{{.+}}}" (0 'org-macro t))
  5466. '(org-hide-wide-columns (0 nil append))
  5467. ;; TODO keyword
  5468. (list (format org-heading-keyword-regexp-format
  5469. org-todo-regexp)
  5470. '(2 (org-get-todo-face 2) t))
  5471. ;; DONE
  5472. (if org-fontify-done-headline
  5473. (list (format org-heading-keyword-regexp-format
  5474. (concat
  5475. "\\(?:"
  5476. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5477. "\\)"))
  5478. '(2 'org-headline-done t))
  5479. nil)
  5480. ;; Priorities
  5481. '(org-font-lock-add-priority-faces)
  5482. ;; Tags
  5483. '(org-font-lock-add-tag-faces)
  5484. ;; Tags groups
  5485. (if (and org-group-tags org-tag-groups-alist)
  5486. (list (concat org-outline-regexp-bol ".+\\(:"
  5487. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5488. ":\\).*$")
  5489. '(1 'org-tag-group prepend)))
  5490. ;; Special keywords
  5491. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5492. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5493. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5494. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5495. ;; Emphasis
  5496. (if em
  5497. (if (featurep 'xemacs)
  5498. '(org-do-emphasis-faces (0 nil append))
  5499. '(org-do-emphasis-faces)))
  5500. ;; Checkboxes
  5501. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5502. 1 'org-checkbox prepend)
  5503. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5504. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5505. (0 (org-get-checkbox-statistics-face) t)))
  5506. ;; Description list items
  5507. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5508. 1 'org-list-dt prepend)
  5509. ;; ARCHIVEd headings
  5510. (list (concat
  5511. org-outline-regexp-bol
  5512. "\\(.*:" org-archive-tag ":.*\\)")
  5513. '(1 'org-archived prepend))
  5514. ;; Specials
  5515. '(org-do-latex-and-related)
  5516. '(org-fontify-entities)
  5517. '(org-raise-scripts)
  5518. ;; Code
  5519. '(org-activate-code (1 'org-code t))
  5520. ;; COMMENT
  5521. (list (format org-heading-keyword-regexp-format
  5522. (concat "\\("
  5523. org-comment-string "\\|" org-quote-string
  5524. "\\)"))
  5525. '(2 'org-special-keyword t))
  5526. ;; Blocks and meta lines
  5527. '(org-fontify-meta-lines-and-blocks)
  5528. )))
  5529. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5530. (run-hooks 'org-font-lock-set-keywords-hook)
  5531. ;; Now set the full font-lock-keywords
  5532. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5533. (org-set-local 'font-lock-defaults
  5534. '(org-font-lock-keywords t nil nil backward-paragraph))
  5535. (kill-local-variable 'font-lock-keywords) nil))
  5536. (defun org-toggle-pretty-entities ()
  5537. "Toggle the composition display of entities as UTF8 characters."
  5538. (interactive)
  5539. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5540. (org-restart-font-lock)
  5541. (if org-pretty-entities
  5542. (message "Entities are now displayed as UTF8 characters")
  5543. (save-restriction
  5544. (widen)
  5545. (org-decompose-region (point-min) (point-max))
  5546. (message "Entities are now displayed as plain text"))))
  5547. (defvar org-custom-properties-overlays nil
  5548. "List of overlays used for custom properties.")
  5549. (make-variable-buffer-local 'org-custom-properties-overlays)
  5550. (defun org-toggle-custom-properties-visibility ()
  5551. "Display or hide properties in `org-custom-properties'."
  5552. (interactive)
  5553. (if org-custom-properties-overlays
  5554. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5555. (setq org-custom-properties-overlays nil))
  5556. (unless (not org-custom-properties)
  5557. (save-excursion
  5558. (save-restriction
  5559. (widen)
  5560. (goto-char (point-min))
  5561. (while (re-search-forward org-property-re nil t)
  5562. (mapc (lambda(p)
  5563. (when (equal p (substring (match-string 1) 1 -1))
  5564. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5565. (overlay-put o 'invisible t)
  5566. (overlay-put o 'org-custom-property t)
  5567. (push o org-custom-properties-overlays))))
  5568. org-custom-properties)))))))
  5569. (defun org-fontify-entities (limit)
  5570. "Find an entity to fontify."
  5571. (let (ee)
  5572. (when org-pretty-entities
  5573. (catch 'match
  5574. (while (re-search-forward
  5575. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5576. limit t)
  5577. (if (and (not (org-in-indented-comment-line))
  5578. (setq ee (org-entity-get (match-string 1)))
  5579. (= (length (nth 6 ee)) 1))
  5580. (let*
  5581. ((end (if (equal (match-string 2) "{}")
  5582. (match-end 2)
  5583. (match-end 1))))
  5584. (add-text-properties
  5585. (match-beginning 0) end
  5586. (list 'font-lock-fontified t))
  5587. (compose-region (match-beginning 0) end
  5588. (nth 6 ee) nil)
  5589. (backward-char 1)
  5590. (throw 'match t))))
  5591. nil))))
  5592. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5593. "Fontify string S like in Org-mode."
  5594. (with-temp-buffer
  5595. (insert s)
  5596. (let ((org-odd-levels-only odd-levels))
  5597. (org-mode)
  5598. (font-lock-fontify-buffer)
  5599. (buffer-string))))
  5600. (defvar org-m nil)
  5601. (defvar org-l nil)
  5602. (defvar org-f nil)
  5603. (defun org-get-level-face (n)
  5604. "Get the right face for match N in font-lock matching of headlines."
  5605. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5606. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5607. (if org-cycle-level-faces
  5608. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5609. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5610. (cond
  5611. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5612. ((eq n 2) org-f)
  5613. (t (if org-level-color-stars-only nil org-f))))
  5614. (defun org-get-todo-face (kwd)
  5615. "Get the right face for a TODO keyword KWD.
  5616. If KWD is a number, get the corresponding match group."
  5617. (if (numberp kwd) (setq kwd (match-string kwd)))
  5618. (or (org-face-from-face-or-color
  5619. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5620. (and (member kwd org-done-keywords) 'org-done)
  5621. 'org-todo))
  5622. (defun org-face-from-face-or-color (context inherit face-or-color)
  5623. "Create a face list that inherits INHERIT, but sets the foreground color.
  5624. When FACE-OR-COLOR is not a string, just return it."
  5625. (if (stringp face-or-color)
  5626. (list :inherit inherit
  5627. (cdr (assoc context org-faces-easy-properties))
  5628. face-or-color)
  5629. face-or-color))
  5630. (defun org-font-lock-add-tag-faces (limit)
  5631. "Add the special tag faces."
  5632. (when (and org-tag-faces org-tags-special-faces-re)
  5633. (while (re-search-forward org-tags-special-faces-re limit t)
  5634. (add-text-properties (match-beginning 1) (match-end 1)
  5635. (list 'face (org-get-tag-face 1)
  5636. 'font-lock-fontified t))
  5637. (backward-char 1))))
  5638. (defun org-font-lock-add-priority-faces (limit)
  5639. "Add the special priority faces."
  5640. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5641. (when (save-match-data (org-at-heading-p))
  5642. (add-text-properties
  5643. (match-beginning 0) (match-end 0)
  5644. (list 'face (or (org-face-from-face-or-color
  5645. 'priority 'org-priority
  5646. (cdr (assoc (char-after (match-beginning 1))
  5647. org-priority-faces)))
  5648. 'org-priority)
  5649. 'font-lock-fontified t)))))
  5650. (defun org-get-tag-face (kwd)
  5651. "Get the right face for a TODO keyword KWD.
  5652. If KWD is a number, get the corresponding match group."
  5653. (if (numberp kwd) (setq kwd (match-string kwd)))
  5654. (or (org-face-from-face-or-color
  5655. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5656. 'org-tag))
  5657. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5658. "Remove fontification and activation overlays from links."
  5659. (font-lock-default-unfontify-region beg end)
  5660. (let* ((buffer-undo-list t)
  5661. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5662. (inhibit-modification-hooks t)
  5663. deactivate-mark buffer-file-name buffer-file-truename)
  5664. (org-decompose-region beg end)
  5665. (remove-text-properties beg end
  5666. '(mouse-face t keymap t org-linked-text t
  5667. invisible t intangible t
  5668. org-no-flyspell t org-emphasis t))
  5669. (org-remove-font-lock-display-properties beg end)))
  5670. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5671. ((raise 0.3) (height 0.7))
  5672. ((raise -0.5))
  5673. ((raise 0.5)))
  5674. "Display properties for showing superscripts and subscripts.")
  5675. (defun org-remove-font-lock-display-properties (beg end)
  5676. "Remove specific display properties that have been added by font lock.
  5677. The will remove the raise properties that are used to show superscripts
  5678. and subscripts."
  5679. (let (next prop)
  5680. (while (< beg end)
  5681. (setq next (next-single-property-change beg 'display nil end)
  5682. prop (get-text-property beg 'display))
  5683. (if (member prop org-script-display)
  5684. (put-text-property beg next 'display nil))
  5685. (setq beg next))))
  5686. (defun org-raise-scripts (limit)
  5687. "Add raise properties to sub/superscripts."
  5688. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5689. (if (re-search-forward
  5690. (if (eq org-use-sub-superscripts t)
  5691. org-match-substring-regexp
  5692. org-match-substring-with-braces-regexp)
  5693. limit t)
  5694. (let* ((pos (point)) table-p comment-p
  5695. (mpos (match-beginning 3))
  5696. (emph-p (get-text-property mpos 'org-emphasis))
  5697. (link-p (get-text-property mpos 'mouse-face))
  5698. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5699. (goto-char (point-at-bol))
  5700. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5701. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5702. (goto-char pos)
  5703. ;; Handle a_b^c
  5704. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5705. (if (or comment-p emph-p link-p keyw-p)
  5706. t
  5707. (put-text-property (match-beginning 3) (match-end 0)
  5708. 'display
  5709. (if (equal (char-after (match-beginning 2)) ?^)
  5710. (nth (if table-p 3 1) org-script-display)
  5711. (nth (if table-p 2 0) org-script-display)))
  5712. (add-text-properties (match-beginning 2) (match-end 2)
  5713. (list 'invisible t
  5714. 'org-dwidth t 'org-dwidth-n 1))
  5715. (if (and (eq (char-after (match-beginning 3)) ?{)
  5716. (eq (char-before (match-end 3)) ?}))
  5717. (progn
  5718. (add-text-properties
  5719. (match-beginning 3) (1+ (match-beginning 3))
  5720. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5721. (add-text-properties
  5722. (1- (match-end 3)) (match-end 3)
  5723. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5724. t)))))
  5725. ;;;; Visibility cycling, including org-goto and indirect buffer
  5726. ;;; Cycling
  5727. (defvar org-cycle-global-status nil)
  5728. (make-variable-buffer-local 'org-cycle-global-status)
  5729. (put 'org-cycle-global-status 'org-state t)
  5730. (defvar org-cycle-subtree-status nil)
  5731. (make-variable-buffer-local 'org-cycle-subtree-status)
  5732. (put 'org-cycle-subtree-status 'org-state t)
  5733. (defvar org-inlinetask-min-level)
  5734. ;;;###autoload
  5735. (defun org-cycle (&optional arg)
  5736. "TAB-action and visibility cycling for Org-mode.
  5737. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5738. is outline visibility cycling, but it also invokes other actions
  5739. in special contexts.
  5740. - When this function is called with a prefix argument, rotate the entire
  5741. buffer through 3 states (global cycling)
  5742. 1. OVERVIEW: Show only top-level headlines.
  5743. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5744. 3. SHOW ALL: Show everything.
  5745. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5746. determined by the variable `org-startup-folded', and by any VISIBILITY
  5747. properties in the buffer.
  5748. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5749. including any drawers.
  5750. - When inside a table, re-align the table and move to the next field.
  5751. - When point is at the beginning of a headline, rotate the subtree started
  5752. by this line through 3 different states (local cycling)
  5753. 1. FOLDED: Only the main headline is shown.
  5754. 2. CHILDREN: The main headline and the direct children are shown.
  5755. From this state, you can move to one of the children
  5756. and zoom in further.
  5757. 3. SUBTREE: Show the entire subtree, including body text.
  5758. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5759. - When point is at the beginning of an empty headline and the variable
  5760. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5761. of the headline by demoting and promoting it to likely levels. This
  5762. speeds up creation document structure by pressing TAB once or several
  5763. times right after creating a new headline.
  5764. - When there is a numeric prefix, go up to a heading with level ARG, do
  5765. a `show-subtree' and return to the previous cursor position. If ARG
  5766. is negative, go up that many levels.
  5767. - When point is not at the beginning of a headline, execute the global
  5768. binding for TAB, which is re-indenting the line. See the option
  5769. `org-cycle-emulate-tab' for details.
  5770. - Special case: if point is at the beginning of the buffer and there is
  5771. no headline in line 1, this function will act as if called with prefix arg
  5772. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5773. But only if also the variable `org-cycle-global-at-bob' is t."
  5774. (interactive "P")
  5775. (org-load-modules-maybe)
  5776. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5777. (and org-cycle-level-after-item/entry-creation
  5778. (or (org-cycle-level)
  5779. (org-cycle-item-indentation))))
  5780. (let* (message-log-max ; Don't populate the *Messages* buffer
  5781. (limit-level
  5782. (or org-cycle-max-level
  5783. (and (boundp 'org-inlinetask-min-level)
  5784. org-inlinetask-min-level
  5785. (1- org-inlinetask-min-level))))
  5786. (nstars (and limit-level
  5787. (if org-odd-levels-only
  5788. (and limit-level (1- (* limit-level 2)))
  5789. limit-level)))
  5790. (org-outline-regexp
  5791. (if (not (derived-mode-p 'org-mode))
  5792. outline-regexp
  5793. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5794. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5795. (not (looking-at org-outline-regexp))))
  5796. (org-cycle-hook
  5797. (if bob-special
  5798. (delq 'org-optimize-window-after-visibility-change
  5799. (copy-sequence org-cycle-hook))
  5800. org-cycle-hook))
  5801. (pos (point)))
  5802. (if (or bob-special (equal arg '(4)))
  5803. ;; special case: use global cycling
  5804. (setq arg t))
  5805. (cond
  5806. ((equal arg '(16))
  5807. (setq last-command 'dummy)
  5808. (org-set-startup-visibility)
  5809. (message "Startup visibility, plus VISIBILITY properties"))
  5810. ((equal arg '(64))
  5811. (show-all)
  5812. (message "Entire buffer visible, including drawers"))
  5813. ;; Table: enter it or move to the next field.
  5814. ((org-at-table-p 'any)
  5815. (if (org-at-table.el-p)
  5816. (message "Use C-c ' to edit table.el tables")
  5817. (if arg (org-table-edit-field t)
  5818. (org-table-justify-field-maybe)
  5819. (call-interactively 'org-table-next-field))))
  5820. ((run-hook-with-args-until-success
  5821. 'org-tab-after-check-for-table-hook))
  5822. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5823. ((eq arg t) (org-cycle-internal-global))
  5824. ;; Drawers: delegate to `org-flag-drawer'.
  5825. ((and org-drawers org-drawer-regexp
  5826. (save-excursion
  5827. (beginning-of-line 1)
  5828. (looking-at org-drawer-regexp)))
  5829. (org-flag-drawer ; toggle block visibility
  5830. (not (get-char-property (match-end 0) 'invisible))))
  5831. ;; Show-subtree, ARG levels up from here.
  5832. ((integerp arg)
  5833. (save-excursion
  5834. (org-back-to-heading)
  5835. (outline-up-heading (if (< arg 0) (- arg)
  5836. (- (funcall outline-level) arg)))
  5837. (org-show-subtree)))
  5838. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5839. ((and (featurep 'org-inlinetask)
  5840. (org-inlinetask-at-task-p)
  5841. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5842. (org-inlinetask-toggle-visibility))
  5843. ((org-try-cdlatex-tab))
  5844. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5845. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5846. (save-excursion (beginning-of-line 1)
  5847. (looking-at org-outline-regexp)))
  5848. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5849. (org-cycle-internal-local))
  5850. ;; From there: TAB emulation and template completion.
  5851. (buffer-read-only (org-back-to-heading))
  5852. ((run-hook-with-args-until-success
  5853. 'org-tab-after-check-for-cycling-hook))
  5854. ((org-try-structure-completion))
  5855. ((run-hook-with-args-until-success
  5856. 'org-tab-before-tab-emulation-hook))
  5857. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5858. (or (not (bolp))
  5859. (not (looking-at org-outline-regexp))))
  5860. (call-interactively (global-key-binding "\t")))
  5861. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5862. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5863. (or (and (eq org-cycle-emulate-tab 'white)
  5864. (= (match-end 0) (point-at-eol)))
  5865. (and (eq org-cycle-emulate-tab 'whitestart)
  5866. (>= (match-end 0) pos))))
  5867. t
  5868. (eq org-cycle-emulate-tab t))
  5869. (call-interactively (global-key-binding "\t")))
  5870. (t (save-excursion
  5871. (org-back-to-heading)
  5872. (org-cycle)))))))
  5873. (defun org-cycle-internal-global ()
  5874. "Do the global cycling action."
  5875. ;; Hack to avoid display of messages for .org attachments in Gnus
  5876. (let (message-log-max ; Don't populate the *Messages* buffer
  5877. (ga (string-match "\\*fontification" (buffer-name))))
  5878. (cond
  5879. ((and (eq last-command this-command)
  5880. (eq org-cycle-global-status 'overview))
  5881. ;; We just created the overview - now do table of contents
  5882. ;; This can be slow in very large buffers, so indicate action
  5883. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5884. (unless ga (message "CONTENTS..."))
  5885. (org-content)
  5886. (unless ga (message "CONTENTS...done"))
  5887. (setq org-cycle-global-status 'contents)
  5888. (run-hook-with-args 'org-cycle-hook 'contents))
  5889. ((and (eq last-command this-command)
  5890. (eq org-cycle-global-status 'contents))
  5891. ;; We just showed the table of contents - now show everything
  5892. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5893. (show-all)
  5894. (unless ga (message "SHOW ALL"))
  5895. (setq org-cycle-global-status 'all)
  5896. (run-hook-with-args 'org-cycle-hook 'all))
  5897. (t
  5898. ;; Default action: go to overview
  5899. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5900. (org-overview)
  5901. (unless ga (message "OVERVIEW"))
  5902. (setq org-cycle-global-status 'overview)
  5903. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5904. (defun org-cycle-internal-local ()
  5905. "Do the local cycling action."
  5906. (let (message-log-max ; Don't populate the *Messages* buffer
  5907. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5908. ;; First, determine end of headline (EOH), end of subtree or item
  5909. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5910. (save-excursion
  5911. (if (org-at-item-p)
  5912. (progn
  5913. (beginning-of-line)
  5914. (setq struct (org-list-struct))
  5915. (setq eoh (point-at-eol))
  5916. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5917. (setq has-children (org-list-has-child-p (point) struct)))
  5918. (org-back-to-heading)
  5919. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5920. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5921. (setq has-children
  5922. (or (save-excursion
  5923. (let ((level (funcall outline-level)))
  5924. (outline-next-heading)
  5925. (and (org-at-heading-p t)
  5926. (> (funcall outline-level) level))))
  5927. (save-excursion
  5928. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5929. ;; Determine end invisible part of buffer (EOL)
  5930. (beginning-of-line 2)
  5931. ;; XEmacs doesn't have `next-single-char-property-change'
  5932. (if (featurep 'xemacs)
  5933. (while (and (not (eobp)) ;; this is like `next-line'
  5934. (get-char-property (1- (point)) 'invisible))
  5935. (beginning-of-line 2))
  5936. (while (and (not (eobp)) ;; this is like `next-line'
  5937. (get-char-property (1- (point)) 'invisible))
  5938. (goto-char (next-single-char-property-change (point) 'invisible))
  5939. (and (eolp) (beginning-of-line 2))))
  5940. (setq eol (point)))
  5941. ;; Find out what to do next and set `this-command'
  5942. (cond
  5943. ((= eos eoh)
  5944. ;; Nothing is hidden behind this heading
  5945. (unless (org-before-first-heading-p)
  5946. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5947. (message "EMPTY ENTRY")
  5948. (setq org-cycle-subtree-status nil)
  5949. (save-excursion
  5950. (goto-char eos)
  5951. (outline-next-heading)
  5952. (if (outline-invisible-p) (org-flag-heading nil))))
  5953. ((and (or (>= eol eos)
  5954. (not (string-match "\\S-" (buffer-substring eol eos))))
  5955. (or has-children
  5956. (not (setq children-skipped
  5957. org-cycle-skip-children-state-if-no-children))))
  5958. ;; Entire subtree is hidden in one line: children view
  5959. (unless (org-before-first-heading-p)
  5960. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5961. (if (org-at-item-p)
  5962. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5963. (org-show-entry)
  5964. (org-with-limited-levels (show-children))
  5965. ;; FIXME: This slows down the func way too much.
  5966. ;; How keep drawers hidden in subtree anyway?
  5967. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5968. ;; (org-cycle-hide-drawers 'subtree))
  5969. ;; Fold every list in subtree to top-level items.
  5970. (when (eq org-cycle-include-plain-lists 'integrate)
  5971. (save-excursion
  5972. (org-back-to-heading)
  5973. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5974. (beginning-of-line 1)
  5975. (let* ((struct (org-list-struct))
  5976. (prevs (org-list-prevs-alist struct))
  5977. (end (org-list-get-bottom-point struct)))
  5978. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5979. (org-list-get-all-items (point) struct prevs))
  5980. (goto-char (if (< end eos) end eos)))))))
  5981. (message "CHILDREN")
  5982. (save-excursion
  5983. (goto-char eos)
  5984. (outline-next-heading)
  5985. (if (outline-invisible-p) (org-flag-heading nil)))
  5986. (setq org-cycle-subtree-status 'children)
  5987. (unless (org-before-first-heading-p)
  5988. (run-hook-with-args 'org-cycle-hook 'children)))
  5989. ((or children-skipped
  5990. (and (eq last-command this-command)
  5991. (eq org-cycle-subtree-status 'children)))
  5992. ;; We just showed the children, or no children are there,
  5993. ;; now show everything.
  5994. (unless (org-before-first-heading-p)
  5995. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5996. (outline-flag-region eoh eos nil)
  5997. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5998. (setq org-cycle-subtree-status 'subtree)
  5999. (unless (org-before-first-heading-p)
  6000. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6001. (t
  6002. ;; Default action: hide the subtree.
  6003. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6004. (outline-flag-region eoh eos t)
  6005. (message "FOLDED")
  6006. (setq org-cycle-subtree-status 'folded)
  6007. (unless (org-before-first-heading-p)
  6008. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6009. ;;;###autoload
  6010. (defun org-global-cycle (&optional arg)
  6011. "Cycle the global visibility. For details see `org-cycle'.
  6012. With \\[universal-argument] prefix arg, switch to startup visibility.
  6013. With a numeric prefix, show all headlines up to that level."
  6014. (interactive "P")
  6015. (let ((org-cycle-include-plain-lists
  6016. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6017. (cond
  6018. ((integerp arg)
  6019. (show-all)
  6020. (hide-sublevels arg)
  6021. (setq org-cycle-global-status 'contents))
  6022. ((equal arg '(4))
  6023. (org-set-startup-visibility)
  6024. (message "Startup visibility, plus VISIBILITY properties."))
  6025. (t
  6026. (org-cycle '(4))))))
  6027. (defun org-set-startup-visibility ()
  6028. "Set the visibility required by startup options and properties."
  6029. (cond
  6030. ((eq org-startup-folded t)
  6031. (org-cycle '(4)))
  6032. ((eq org-startup-folded 'content)
  6033. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6034. (org-cycle '(4)) (org-cycle '(4)))))
  6035. (unless (eq org-startup-folded 'showeverything)
  6036. (if org-hide-block-startup (org-hide-block-all))
  6037. (org-set-visibility-according-to-property 'no-cleanup)
  6038. (org-cycle-hide-archived-subtrees 'all)
  6039. (org-cycle-hide-drawers 'all)
  6040. (org-cycle-show-empty-lines t)))
  6041. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6042. "Switch subtree visibilities according to :VISIBILITY: property."
  6043. (interactive)
  6044. (let (org-show-entry-below state)
  6045. (save-excursion
  6046. (goto-char (point-min))
  6047. (while (re-search-forward
  6048. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6049. nil t)
  6050. (setq state (match-string 1))
  6051. (save-excursion
  6052. (org-back-to-heading t)
  6053. (hide-subtree)
  6054. (org-reveal)
  6055. (cond
  6056. ((equal state '("fold" "folded"))
  6057. (hide-subtree))
  6058. ((equal state "children")
  6059. (org-show-hidden-entry)
  6060. (show-children))
  6061. ((equal state "content")
  6062. (save-excursion
  6063. (save-restriction
  6064. (org-narrow-to-subtree)
  6065. (org-content))))
  6066. ((member state '("all" "showall"))
  6067. (show-subtree)))))
  6068. (unless no-cleanup
  6069. (org-cycle-hide-archived-subtrees 'all)
  6070. (org-cycle-hide-drawers 'all)
  6071. (org-cycle-show-empty-lines 'all)))))
  6072. ;; This function uses outline-regexp instead of the more fundamental
  6073. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6074. ;; buffers, where outline-regexp is needed.
  6075. (defun org-overview ()
  6076. "Switch to overview mode, showing only top-level headlines.
  6077. Really, this shows all headlines with level equal or greater than the level
  6078. of the first headline in the buffer. This is important, because if the
  6079. first headline is not level one, then (hide-sublevels 1) gives confusing
  6080. results."
  6081. (interactive)
  6082. (let ((l (org-current-line))
  6083. (level (save-excursion
  6084. (goto-char (point-min))
  6085. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6086. (progn
  6087. (goto-char (match-beginning 0))
  6088. (funcall outline-level))))))
  6089. (and level (hide-sublevels level))
  6090. (recenter '(4))
  6091. (org-goto-line l)))
  6092. (defun org-content (&optional arg)
  6093. "Show all headlines in the buffer, like a table of contents.
  6094. With numerical argument N, show content up to level N."
  6095. (interactive "P")
  6096. (save-excursion
  6097. ;; Visit all headings and show their offspring
  6098. (and (integerp arg) (org-overview))
  6099. (goto-char (point-max))
  6100. (catch 'exit
  6101. (while (and (progn (condition-case nil
  6102. (outline-previous-visible-heading 1)
  6103. (error (goto-char (point-min))))
  6104. t)
  6105. (looking-at org-outline-regexp))
  6106. (if (integerp arg)
  6107. (show-children (1- arg))
  6108. (show-branches))
  6109. (if (bobp) (throw 'exit nil))))))
  6110. (defun org-optimize-window-after-visibility-change (state)
  6111. "Adjust the window after a change in outline visibility.
  6112. This function is the default value of the hook `org-cycle-hook'."
  6113. (when (get-buffer-window (current-buffer))
  6114. (cond
  6115. ((eq state 'content) nil)
  6116. ((eq state 'all) nil)
  6117. ((eq state 'folded) nil)
  6118. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6119. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6120. (defun org-remove-empty-overlays-at (pos)
  6121. "Remove outline overlays that do not contain non-white stuff."
  6122. (mapc
  6123. (lambda (o)
  6124. (and (eq 'outline (overlay-get o 'invisible))
  6125. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6126. (overlay-end o))))
  6127. (delete-overlay o)))
  6128. (overlays-at pos)))
  6129. (defun org-clean-visibility-after-subtree-move ()
  6130. "Fix visibility issues after moving a subtree."
  6131. ;; First, find a reasonable region to look at:
  6132. ;; Start two siblings above, end three below
  6133. (let* ((beg (save-excursion
  6134. (and (org-get-last-sibling)
  6135. (org-get-last-sibling))
  6136. (point)))
  6137. (end (save-excursion
  6138. (and (org-get-next-sibling)
  6139. (org-get-next-sibling)
  6140. (org-get-next-sibling))
  6141. (if (org-at-heading-p)
  6142. (point-at-eol)
  6143. (point))))
  6144. (level (looking-at "\\*+"))
  6145. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6146. (save-excursion
  6147. (save-restriction
  6148. (narrow-to-region beg end)
  6149. (when re
  6150. ;; Properly fold already folded siblings
  6151. (goto-char (point-min))
  6152. (while (re-search-forward re nil t)
  6153. (if (and (not (outline-invisible-p))
  6154. (save-excursion
  6155. (goto-char (point-at-eol)) (outline-invisible-p)))
  6156. (hide-entry))))
  6157. (org-cycle-show-empty-lines 'overview)
  6158. (org-cycle-hide-drawers 'overview)))))
  6159. (defun org-cycle-show-empty-lines (state)
  6160. "Show empty lines above all visible headlines.
  6161. The region to be covered depends on STATE when called through
  6162. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6163. entire buffer covered. Note that an empty line is only shown if there
  6164. are at least `org-cycle-separator-lines' empty lines before the headline."
  6165. (when (not (= org-cycle-separator-lines 0))
  6166. (save-excursion
  6167. (let* ((n (abs org-cycle-separator-lines))
  6168. (re (cond
  6169. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6170. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6171. (t (let ((ns (number-to-string (- n 2))))
  6172. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6173. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6174. beg end b e)
  6175. (cond
  6176. ((memq state '(overview contents t))
  6177. (setq beg (point-min) end (point-max)))
  6178. ((memq state '(children folded))
  6179. (setq beg (point) end (progn (org-end-of-subtree t t)
  6180. (beginning-of-line 2)
  6181. (point)))))
  6182. (when beg
  6183. (goto-char beg)
  6184. (while (re-search-forward re end t)
  6185. (unless (get-char-property (match-end 1) 'invisible)
  6186. (setq e (match-end 1))
  6187. (if (< org-cycle-separator-lines 0)
  6188. (setq b (save-excursion
  6189. (goto-char (match-beginning 0))
  6190. (org-back-over-empty-lines)
  6191. (if (save-excursion
  6192. (goto-char (max (point-min) (1- (point))))
  6193. (org-at-heading-p))
  6194. (1- (point))
  6195. (point))))
  6196. (setq b (match-beginning 1)))
  6197. (outline-flag-region b e nil)))))))
  6198. ;; Never hide empty lines at the end of the file.
  6199. (save-excursion
  6200. (goto-char (point-max))
  6201. (outline-previous-heading)
  6202. (outline-end-of-heading)
  6203. (if (and (looking-at "[ \t\n]+")
  6204. (= (match-end 0) (point-max)))
  6205. (outline-flag-region (point) (match-end 0) nil))))
  6206. (defun org-show-empty-lines-in-parent ()
  6207. "Move to the parent and re-show empty lines before visible headlines."
  6208. (save-excursion
  6209. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6210. (org-cycle-show-empty-lines context))))
  6211. (defun org-files-list ()
  6212. "Return `org-agenda-files' list, plus all open org-mode files.
  6213. This is useful for operations that need to scan all of a user's
  6214. open and agenda-wise Org files."
  6215. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6216. (dolist (buf (buffer-list))
  6217. (with-current-buffer buf
  6218. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6219. (let ((file (expand-file-name (buffer-file-name))))
  6220. (unless (member file files)
  6221. (push file files))))))
  6222. files))
  6223. (defsubst org-entry-beginning-position ()
  6224. "Return the beginning position of the current entry."
  6225. (save-excursion (outline-back-to-heading t) (point)))
  6226. (defsubst org-entry-end-position ()
  6227. "Return the end position of the current entry."
  6228. (save-excursion (outline-next-heading) (point)))
  6229. (defun org-cycle-hide-drawers (state)
  6230. "Re-hide all drawers after a visibility state change."
  6231. (when (and (derived-mode-p 'org-mode)
  6232. (not (memq state '(overview folded contents))))
  6233. (save-excursion
  6234. (let* ((globalp (memq state '(contents all)))
  6235. (beg (if globalp (point-min) (point)))
  6236. (end (if globalp (point-max)
  6237. (if (eq state 'children)
  6238. (save-excursion (outline-next-heading) (point))
  6239. (org-end-of-subtree t)))))
  6240. (goto-char beg)
  6241. (while (re-search-forward org-drawer-regexp end t)
  6242. (org-flag-drawer t))))))
  6243. (defun org-cycle-hide-inline-tasks (state)
  6244. "Re-hide inline task when switching to 'contents visibility state."
  6245. (when (and (eq state 'contents)
  6246. (boundp 'org-inlinetask-min-level)
  6247. org-inlinetask-min-level)
  6248. (hide-sublevels (1- org-inlinetask-min-level))))
  6249. (defun org-flag-drawer (flag)
  6250. "When FLAG is non-nil, hide the drawer we are within.
  6251. Otherwise make it visible."
  6252. (save-excursion
  6253. (beginning-of-line 1)
  6254. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6255. (let ((b (match-end 0)))
  6256. (if (re-search-forward
  6257. "^[ \t]*:END:"
  6258. (save-excursion (outline-next-heading) (point)) t)
  6259. (outline-flag-region b (point-at-eol) flag)
  6260. (error ":END: line missing at position %s" b))))))
  6261. (defun org-subtree-end-visible-p ()
  6262. "Is the end of the current subtree visible?"
  6263. (pos-visible-in-window-p
  6264. (save-excursion (org-end-of-subtree t) (point))))
  6265. (defun org-first-headline-recenter (&optional N)
  6266. "Move cursor to the first headline and recenter the headline.
  6267. Optional argument N means put the headline into the Nth line of the window."
  6268. (goto-char (point-min))
  6269. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6270. (beginning-of-line)
  6271. (recenter (prefix-numeric-value N))))
  6272. ;;; Saving and restoring visibility
  6273. (defun org-outline-overlay-data (&optional use-markers)
  6274. "Return a list of the locations of all outline overlays.
  6275. These are overlays with the `invisible' property value `outline'.
  6276. The return value is a list of cons cells, with start and stop
  6277. positions for each overlay.
  6278. If USE-MARKERS is set, return the positions as markers."
  6279. (let (beg end)
  6280. (save-excursion
  6281. (save-restriction
  6282. (widen)
  6283. (delq nil
  6284. (mapcar (lambda (o)
  6285. (when (eq (overlay-get o 'invisible) 'outline)
  6286. (setq beg (overlay-start o)
  6287. end (overlay-end o))
  6288. (and beg end (> end beg)
  6289. (if use-markers
  6290. (cons (move-marker (make-marker) beg)
  6291. (move-marker (make-marker) end))
  6292. (cons beg end)))))
  6293. (overlays-in (point-min) (point-max))))))))
  6294. (defun org-set-outline-overlay-data (data)
  6295. "Create visibility overlays for all positions in DATA.
  6296. DATA should have been made by `org-outline-overlay-data'."
  6297. (let (o)
  6298. (save-excursion
  6299. (save-restriction
  6300. (widen)
  6301. (show-all)
  6302. (mapc (lambda (c)
  6303. (outline-flag-region (car c) (cdr c) t))
  6304. data)))))
  6305. ;;; Folding of blocks
  6306. (defvar org-hide-block-overlays nil
  6307. "Overlays hiding blocks.")
  6308. (make-variable-buffer-local 'org-hide-block-overlays)
  6309. (defun org-block-map (function &optional start end)
  6310. "Call FUNCTION at the head of all source blocks in the current buffer.
  6311. Optional arguments START and END can be used to limit the range."
  6312. (let ((start (or start (point-min)))
  6313. (end (or end (point-max))))
  6314. (save-excursion
  6315. (goto-char start)
  6316. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6317. (save-excursion
  6318. (save-match-data
  6319. (goto-char (match-beginning 0))
  6320. (funcall function)))))))
  6321. (defun org-hide-block-toggle-all ()
  6322. "Toggle the visibility of all blocks in the current buffer."
  6323. (org-block-map #'org-hide-block-toggle))
  6324. (defun org-hide-block-all ()
  6325. "Fold all blocks in the current buffer."
  6326. (interactive)
  6327. (org-show-block-all)
  6328. (org-block-map #'org-hide-block-toggle-maybe))
  6329. (defun org-show-block-all ()
  6330. "Unfold all blocks in the current buffer."
  6331. (interactive)
  6332. (mapc 'delete-overlay org-hide-block-overlays)
  6333. (setq org-hide-block-overlays nil))
  6334. (defun org-hide-block-toggle-maybe ()
  6335. "Toggle visibility of block at point."
  6336. (interactive)
  6337. (let ((case-fold-search t))
  6338. (if (save-excursion
  6339. (beginning-of-line 1)
  6340. (looking-at org-block-regexp))
  6341. (progn (org-hide-block-toggle)
  6342. t) ;; to signal that we took action
  6343. nil))) ;; to signal that we did not
  6344. (defun org-hide-block-toggle (&optional force)
  6345. "Toggle the visibility of the current block."
  6346. (interactive)
  6347. (save-excursion
  6348. (beginning-of-line)
  6349. (if (re-search-forward org-block-regexp nil t)
  6350. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6351. (end (match-end 0)) ;; end of entire body
  6352. ov)
  6353. (if (memq t (mapcar (lambda (overlay)
  6354. (eq (overlay-get overlay 'invisible)
  6355. 'org-hide-block))
  6356. (overlays-at start)))
  6357. (if (or (not force) (eq force 'off))
  6358. (mapc (lambda (ov)
  6359. (when (member ov org-hide-block-overlays)
  6360. (setq org-hide-block-overlays
  6361. (delq ov org-hide-block-overlays)))
  6362. (when (eq (overlay-get ov 'invisible)
  6363. 'org-hide-block)
  6364. (delete-overlay ov)))
  6365. (overlays-at start)))
  6366. (setq ov (make-overlay start end))
  6367. (overlay-put ov 'invisible 'org-hide-block)
  6368. ;; make the block accessible to isearch
  6369. (overlay-put
  6370. ov 'isearch-open-invisible
  6371. (lambda (ov)
  6372. (when (member ov org-hide-block-overlays)
  6373. (setq org-hide-block-overlays
  6374. (delq ov org-hide-block-overlays)))
  6375. (when (eq (overlay-get ov 'invisible)
  6376. 'org-hide-block)
  6377. (delete-overlay ov))))
  6378. (push ov org-hide-block-overlays)))
  6379. (error "Not looking at a source block"))))
  6380. ;; org-tab-after-check-for-cycling-hook
  6381. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6382. ;; Remove overlays when changing major mode
  6383. (add-hook 'org-mode-hook
  6384. (lambda () (org-add-hook 'change-major-mode-hook
  6385. 'org-show-block-all 'append 'local)))
  6386. ;;; Org-goto
  6387. (defvar org-goto-window-configuration nil)
  6388. (defvar org-goto-marker nil)
  6389. (defvar org-goto-map)
  6390. (defun org-goto-map ()
  6391. "Set the keymap `org-goto'."
  6392. (setq org-goto-map
  6393. (let ((map (make-sparse-keymap)))
  6394. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6395. mouse-drag-region universal-argument org-occur))
  6396. cmd)
  6397. (while (setq cmd (pop cmds))
  6398. (substitute-key-definition cmd cmd map global-map)))
  6399. (suppress-keymap map)
  6400. (org-defkey map "\C-m" 'org-goto-ret)
  6401. (org-defkey map [(return)] 'org-goto-ret)
  6402. (org-defkey map [(left)] 'org-goto-left)
  6403. (org-defkey map [(right)] 'org-goto-right)
  6404. (org-defkey map [(control ?g)] 'org-goto-quit)
  6405. (org-defkey map "\C-i" 'org-cycle)
  6406. (org-defkey map [(tab)] 'org-cycle)
  6407. (org-defkey map [(down)] 'outline-next-visible-heading)
  6408. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6409. (if org-goto-auto-isearch
  6410. (if (fboundp 'define-key-after)
  6411. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6412. nil)
  6413. (org-defkey map "q" 'org-goto-quit)
  6414. (org-defkey map "n" 'outline-next-visible-heading)
  6415. (org-defkey map "p" 'outline-previous-visible-heading)
  6416. (org-defkey map "f" 'outline-forward-same-level)
  6417. (org-defkey map "b" 'outline-backward-same-level)
  6418. (org-defkey map "u" 'outline-up-heading))
  6419. (org-defkey map "/" 'org-occur)
  6420. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6421. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6422. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6423. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6424. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6425. map)))
  6426. (defconst org-goto-help
  6427. "Browse buffer copy, to find location or copy text.%s
  6428. RET=jump to location C-g=quit and return to previous location
  6429. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6430. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6431. (defun org-goto (&optional alternative-interface)
  6432. "Look up a different location in the current file, keeping current visibility.
  6433. When you want look-up or go to a different location in a
  6434. document, the fastest way is often to fold the entire buffer and
  6435. then dive into the tree. This method has the disadvantage, that
  6436. the previous location will be folded, which may not be what you
  6437. want.
  6438. This command works around this by showing a copy of the current
  6439. buffer in an indirect buffer, in overview mode. You can dive
  6440. into the tree in that copy, use org-occur and incremental search
  6441. to find a location. When pressing RET or `Q', the command
  6442. returns to the original buffer in which the visibility is still
  6443. unchanged. After RET it will also jump to the location selected
  6444. in the indirect buffer and expose the headline hierarchy above.
  6445. With a prefix argument, use the alternative interface: e.g. if
  6446. `org-goto-interface' is 'outline use 'outline-path-completion."
  6447. (interactive "P")
  6448. (org-goto-map)
  6449. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6450. (org-refile-use-outline-path t)
  6451. (org-refile-target-verify-function nil)
  6452. (interface
  6453. (if (not alternative-interface)
  6454. org-goto-interface
  6455. (if (eq org-goto-interface 'outline)
  6456. 'outline-path-completion
  6457. 'outline)))
  6458. (org-goto-start-pos (point))
  6459. (selected-point
  6460. (if (eq interface 'outline)
  6461. (car (org-get-location (current-buffer) org-goto-help))
  6462. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6463. (org-refile-check-position pa)
  6464. (nth 3 pa)))))
  6465. (if selected-point
  6466. (progn
  6467. (org-mark-ring-push org-goto-start-pos)
  6468. (goto-char selected-point)
  6469. (if (or (outline-invisible-p) (org-invisible-p2))
  6470. (org-show-context 'org-goto)))
  6471. (message "Quit"))))
  6472. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6473. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6474. (defvar org-goto-local-auto-isearch-map) ; defined below
  6475. (defun org-get-location (buf help)
  6476. "Let the user select a location in the Org-mode buffer BUF.
  6477. This function uses a recursive edit. It returns the selected position
  6478. or nil."
  6479. (org-no-popups
  6480. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6481. (isearch-hide-immediately nil)
  6482. (isearch-search-fun-function
  6483. (lambda () 'org-goto-local-search-headings))
  6484. (org-goto-selected-point org-goto-exit-command))
  6485. (save-excursion
  6486. (save-window-excursion
  6487. (delete-other-windows)
  6488. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6489. (org-pop-to-buffer-same-window
  6490. (condition-case nil
  6491. (make-indirect-buffer (current-buffer) "*org-goto*")
  6492. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6493. (with-output-to-temp-buffer "*Org Help*"
  6494. (princ (format help (if org-goto-auto-isearch
  6495. " Just type for auto-isearch."
  6496. " n/p/f/b/u to navigate, q to quit."))))
  6497. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6498. (setq buffer-read-only nil)
  6499. (let ((org-startup-truncated t)
  6500. (org-startup-folded nil)
  6501. (org-startup-align-all-tables nil))
  6502. (org-mode)
  6503. (org-overview))
  6504. (setq buffer-read-only t)
  6505. (if (and (boundp 'org-goto-start-pos)
  6506. (integer-or-marker-p org-goto-start-pos))
  6507. (let ((org-show-hierarchy-above t)
  6508. (org-show-siblings t)
  6509. (org-show-following-heading t))
  6510. (goto-char org-goto-start-pos)
  6511. (and (outline-invisible-p) (org-show-context)))
  6512. (goto-char (point-min)))
  6513. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6514. (message "Select location and press RET")
  6515. (use-local-map org-goto-map)
  6516. (recursive-edit)))
  6517. (kill-buffer "*org-goto*")
  6518. (cons org-goto-selected-point org-goto-exit-command))))
  6519. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6520. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6521. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6522. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6523. (defun org-goto-local-search-headings (string bound noerror)
  6524. "Search and make sure that any matches are in headlines."
  6525. (catch 'return
  6526. (while (if isearch-forward
  6527. (search-forward string bound noerror)
  6528. (search-backward string bound noerror))
  6529. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6530. (and (member :headline context)
  6531. (not (member :tags context))))
  6532. (throw 'return (point))))))
  6533. (defun org-goto-local-auto-isearch ()
  6534. "Start isearch."
  6535. (interactive)
  6536. (goto-char (point-min))
  6537. (let ((keys (this-command-keys)))
  6538. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6539. (isearch-mode t)
  6540. (isearch-process-search-char (string-to-char keys)))))
  6541. (defun org-goto-ret (&optional arg)
  6542. "Finish `org-goto' by going to the new location."
  6543. (interactive "P")
  6544. (setq org-goto-selected-point (point)
  6545. org-goto-exit-command 'return)
  6546. (throw 'exit nil))
  6547. (defun org-goto-left ()
  6548. "Finish `org-goto' by going to the new location."
  6549. (interactive)
  6550. (if (org-at-heading-p)
  6551. (progn
  6552. (beginning-of-line 1)
  6553. (setq org-goto-selected-point (point)
  6554. org-goto-exit-command 'left)
  6555. (throw 'exit nil))
  6556. (error "Not on a heading")))
  6557. (defun org-goto-right ()
  6558. "Finish `org-goto' by going to the new location."
  6559. (interactive)
  6560. (if (org-at-heading-p)
  6561. (progn
  6562. (setq org-goto-selected-point (point)
  6563. org-goto-exit-command 'right)
  6564. (throw 'exit nil))
  6565. (error "Not on a heading")))
  6566. (defun org-goto-quit ()
  6567. "Finish `org-goto' without cursor motion."
  6568. (interactive)
  6569. (setq org-goto-selected-point nil)
  6570. (setq org-goto-exit-command 'quit)
  6571. (throw 'exit nil))
  6572. ;;; Indirect buffer display of subtrees
  6573. (defvar org-indirect-dedicated-frame nil
  6574. "This is the frame being used for indirect tree display.")
  6575. (defvar org-last-indirect-buffer nil)
  6576. (defun org-tree-to-indirect-buffer (&optional arg)
  6577. "Create indirect buffer and narrow it to current subtree.
  6578. With a numerical prefix ARG, go up to this level and then take that tree.
  6579. If ARG is negative, go up that many levels.
  6580. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6581. indirect buffer previously made with this command, to avoid proliferation of
  6582. indirect buffers. However, when you call the command with a \
  6583. \\[universal-argument] prefix, or
  6584. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6585. is kept so that you can work with several indirect buffers at the same time.
  6586. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6587. \\[universal-argument] prefix also
  6588. requests that a new frame be made for the new buffer, so that the dedicated
  6589. frame is not changed."
  6590. (interactive "P")
  6591. (let ((cbuf (current-buffer))
  6592. (cwin (selected-window))
  6593. (pos (point))
  6594. beg end level heading ibuf)
  6595. (save-excursion
  6596. (org-back-to-heading t)
  6597. (when (numberp arg)
  6598. (setq level (org-outline-level))
  6599. (if (< arg 0) (setq arg (+ level arg)))
  6600. (while (> (setq level (org-outline-level)) arg)
  6601. (org-up-heading-safe)))
  6602. (setq beg (point)
  6603. heading (org-get-heading))
  6604. (org-end-of-subtree t t)
  6605. (if (org-at-heading-p) (backward-char 1))
  6606. (setq end (point)))
  6607. (if (and (buffer-live-p org-last-indirect-buffer)
  6608. (not (eq org-indirect-buffer-display 'new-frame))
  6609. (not arg))
  6610. (kill-buffer org-last-indirect-buffer))
  6611. (setq ibuf (org-get-indirect-buffer cbuf)
  6612. org-last-indirect-buffer ibuf)
  6613. (cond
  6614. ((or (eq org-indirect-buffer-display 'new-frame)
  6615. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6616. (select-frame (make-frame))
  6617. (delete-other-windows)
  6618. (org-pop-to-buffer-same-window ibuf)
  6619. (org-set-frame-title heading))
  6620. ((eq org-indirect-buffer-display 'dedicated-frame)
  6621. (raise-frame
  6622. (select-frame (or (and org-indirect-dedicated-frame
  6623. (frame-live-p org-indirect-dedicated-frame)
  6624. org-indirect-dedicated-frame)
  6625. (setq org-indirect-dedicated-frame (make-frame)))))
  6626. (delete-other-windows)
  6627. (org-pop-to-buffer-same-window ibuf)
  6628. (org-set-frame-title (concat "Indirect: " heading)))
  6629. ((eq org-indirect-buffer-display 'current-window)
  6630. (org-pop-to-buffer-same-window ibuf))
  6631. ((eq org-indirect-buffer-display 'other-window)
  6632. (pop-to-buffer ibuf))
  6633. (t (error "Invalid value")))
  6634. (if (featurep 'xemacs)
  6635. (save-excursion (org-mode) (turn-on-font-lock)))
  6636. (narrow-to-region beg end)
  6637. (show-all)
  6638. (goto-char pos)
  6639. (run-hook-with-args 'org-cycle-hook 'all)
  6640. (and (window-live-p cwin) (select-window cwin))))
  6641. (defun org-get-indirect-buffer (&optional buffer)
  6642. (setq buffer (or buffer (current-buffer)))
  6643. (let ((n 1) (base (buffer-name buffer)) bname)
  6644. (while (buffer-live-p
  6645. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6646. (setq n (1+ n)))
  6647. (condition-case nil
  6648. (make-indirect-buffer buffer bname 'clone)
  6649. (error (make-indirect-buffer buffer bname)))))
  6650. (defun org-set-frame-title (title)
  6651. "Set the title of the current frame to the string TITLE."
  6652. ;; FIXME: how to name a single frame in XEmacs???
  6653. (unless (featurep 'xemacs)
  6654. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6655. ;;;; Structure editing
  6656. ;;; Inserting headlines
  6657. (defun org-previous-line-empty-p (&optional next)
  6658. "Is the previous line a blank line?
  6659. When NEXT is non-nil, check the next line instead."
  6660. (save-excursion
  6661. (and (not (bobp))
  6662. (or (beginning-of-line (if next 2 0)) t)
  6663. (save-match-data
  6664. (looking-at "[ \t]*$")))))
  6665. (defun org-insert-heading (&optional arg invisible-ok)
  6666. "Insert a new heading or item with same depth at point.
  6667. If point is in a plain list and ARG is nil, create a new list item.
  6668. With one universal prefix argument, insert a heading even in lists.
  6669. With two universal prefix arguments, insert the heading at the end
  6670. of the parent subtree.
  6671. If point is at the beginning of a headline, insert a sibling before
  6672. the current headline. If point is not at the beginning, split the line
  6673. and create a new headline with the text in the current line after point
  6674. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6675. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6676. This is important for non-interactive uses of the command."
  6677. (interactive "P")
  6678. (cond
  6679. ((or (= (buffer-size) 0)
  6680. (and (not (save-excursion
  6681. (and (ignore-errors (org-back-to-heading invisible-ok))
  6682. (org-at-heading-p))))
  6683. (or arg (not (org-in-item-p)))))
  6684. (insert
  6685. (if (org-previous-line-empty-p) "" "\n")
  6686. (if (org-in-src-block-p) ",* " "* "))
  6687. (run-hooks 'org-insert-heading-hook))
  6688. ((or arg (not (org-insert-item
  6689. (save-excursion
  6690. (beginning-of-line)
  6691. (looking-at org-list-full-item-re)
  6692. (match-string 3)))))
  6693. (let (begn endn)
  6694. (when (org-buffer-narrowed-p)
  6695. (setq begn (point-min) endn (point-max))
  6696. (widen))
  6697. (let* ((empty-line-p nil)
  6698. (eops (equal arg '(16))) ; insert at end of parent subtree
  6699. (org-insert-heading-respect-content
  6700. (or (not (null arg)) org-insert-heading-respect-content))
  6701. (level nil)
  6702. (on-heading (org-at-heading-p))
  6703. (head (save-excursion
  6704. (condition-case nil
  6705. (progn
  6706. (org-back-to-heading invisible-ok)
  6707. (when (and (not on-heading)
  6708. (featurep 'org-inlinetask)
  6709. (integerp org-inlinetask-min-level)
  6710. (>= (length (match-string 0))
  6711. org-inlinetask-min-level))
  6712. ;; Find a heading level before the inline task
  6713. (while (and (setq level (org-up-heading-safe))
  6714. (>= level org-inlinetask-min-level)))
  6715. (if (org-at-heading-p)
  6716. (org-back-to-heading invisible-ok)
  6717. (error "This should not happen")))
  6718. (unless (and (save-excursion
  6719. (save-match-data
  6720. (org-backward-heading-same-level 1 invisible-ok))
  6721. (= (point) (match-beginning 0)))
  6722. (not (org-previous-line-empty-p t)))
  6723. (setq empty-line-p (org-previous-line-empty-p)))
  6724. (match-string 0))
  6725. (error "* "))))
  6726. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6727. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6728. pos hide-previous previous-pos)
  6729. (if ;; At the beginning of a heading, open a new line for insertiong
  6730. (and (bolp) (org-at-heading-p)
  6731. (not eops)
  6732. (or (bobp)
  6733. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6734. (open-line (if blank 2 1))
  6735. (save-excursion
  6736. (setq previous-pos (point-at-bol))
  6737. (end-of-line)
  6738. (setq hide-previous (outline-invisible-p)))
  6739. (and org-insert-heading-respect-content
  6740. (save-excursion
  6741. (while (outline-invisible-p)
  6742. (org-show-subtree)
  6743. (org-up-heading-safe))))
  6744. (let ((split
  6745. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6746. (save-excursion
  6747. (let ((p (point)))
  6748. (goto-char (point-at-bol))
  6749. (and (looking-at org-complex-heading-regexp)
  6750. (match-beginning 4)
  6751. (> p (match-beginning 4)))))))
  6752. tags pos)
  6753. (cond
  6754. ;; Insert a new line, possibly at end of parent subtree
  6755. (org-insert-heading-respect-content
  6756. (if (not eops)
  6757. (progn
  6758. (org-end-of-subtree nil t)
  6759. (and (looking-at "^\\*") (backward-char 1))
  6760. (while (and (not (bobp))
  6761. ;; Don't delete spaces in empty headlines
  6762. (not (looking-back org-outline-regexp))
  6763. (member (char-before) '(?\ ?\t ?\n)))
  6764. (backward-delete-char 1)))
  6765. (let ((p (point)))
  6766. (org-up-heading-safe)
  6767. (if (= p (point))
  6768. (goto-char (point-max))
  6769. (org-end-of-subtree nil t))))
  6770. (when (featurep 'org-inlinetask)
  6771. (while (and (not (eobp))
  6772. (looking-at "\\(\\*+\\)[ \t]+")
  6773. (>= (length (match-string 1))
  6774. org-inlinetask-min-level))
  6775. (org-end-of-subtree nil t)))
  6776. (or (bolp) (newline))
  6777. (or (org-previous-line-empty-p)
  6778. (and blank (newline)))
  6779. (if (or empty-line-p eops) (open-line 1)))
  6780. ;; Insert a headling containing text after point
  6781. ((org-at-heading-p)
  6782. (when hide-previous
  6783. (show-children)
  6784. (org-show-entry))
  6785. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6786. (setq tags (and (match-end 2) (match-string 2)))
  6787. (and (match-end 1)
  6788. (delete-region (match-beginning 1) (match-end 1)))
  6789. (setq pos (point-at-bol))
  6790. (or split (end-of-line 1))
  6791. (delete-horizontal-space)
  6792. (if (string-match "\\`\\*+\\'"
  6793. (buffer-substring (point-at-bol) (point)))
  6794. (insert " "))
  6795. (newline (if blank 2 1))
  6796. (when tags
  6797. (save-excursion
  6798. (goto-char pos)
  6799. (end-of-line 1)
  6800. (insert " " tags)
  6801. (org-set-tags nil 'align))))
  6802. (t
  6803. (or split (end-of-line 1))
  6804. (newline (if blank 2 1))))))
  6805. (insert head) (just-one-space)
  6806. (setq pos (point))
  6807. (end-of-line 1)
  6808. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6809. (when (and org-insert-heading-respect-content hide-previous)
  6810. (save-excursion
  6811. (goto-char previous-pos)
  6812. (hide-subtree)))
  6813. (when (and begn endn)
  6814. (narrow-to-region (min (point) begn) (max (point) endn)))
  6815. (run-hooks 'org-insert-heading-hook))))))
  6816. (defun org-get-heading (&optional no-tags no-todo)
  6817. "Return the heading of the current entry, without the stars.
  6818. When NO-TAGS is non-nil, don't include tags.
  6819. When NO-TODO is non-nil, don't include TODO keywords."
  6820. (save-excursion
  6821. (org-back-to-heading t)
  6822. (cond
  6823. ((and no-tags no-todo)
  6824. (looking-at org-complex-heading-regexp)
  6825. (match-string 4))
  6826. (no-tags
  6827. (looking-at (concat org-outline-regexp
  6828. "\\(.*?\\)"
  6829. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6830. (match-string 1))
  6831. (no-todo
  6832. (looking-at org-todo-line-regexp)
  6833. (match-string 3))
  6834. (t (looking-at org-heading-regexp)
  6835. (match-string 2)))))
  6836. (defvar orgstruct-mode) ; defined below
  6837. (defun org-heading-components ()
  6838. "Return the components of the current heading.
  6839. This is a list with the following elements:
  6840. - the level as an integer
  6841. - the reduced level, different if `org-odd-levels-only' is set.
  6842. - the TODO keyword, or nil
  6843. - the priority character, like ?A, or nil if no priority is given
  6844. - the headline text itself, or the tags string if no headline text
  6845. - the tags string, or nil."
  6846. (save-excursion
  6847. (org-back-to-heading t)
  6848. (if (let (case-fold-search)
  6849. (looking-at
  6850. (if orgstruct-mode
  6851. org-heading-regexp
  6852. org-complex-heading-regexp)))
  6853. (if orgstruct-mode
  6854. (list (length (match-string 1))
  6855. (org-reduced-level (length (match-string 1)))
  6856. nil
  6857. nil
  6858. (match-string 2)
  6859. nil)
  6860. (list (length (match-string 1))
  6861. (org-reduced-level (length (match-string 1)))
  6862. (org-match-string-no-properties 2)
  6863. (and (match-end 3) (aref (match-string 3) 2))
  6864. (org-match-string-no-properties 4)
  6865. (org-match-string-no-properties 5))))))
  6866. (defun org-get-entry ()
  6867. "Get the entry text, after heading, entire subtree."
  6868. (save-excursion
  6869. (org-back-to-heading t)
  6870. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6871. (defun org-insert-heading-after-current ()
  6872. "Insert a new heading with same level as current, after current subtree."
  6873. (interactive)
  6874. (org-back-to-heading)
  6875. (org-insert-heading)
  6876. (org-move-subtree-down)
  6877. (end-of-line 1))
  6878. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6879. "Insert heading with `org-insert-heading-respect-content' set to t."
  6880. (interactive "P")
  6881. (let ((org-insert-heading-respect-content t))
  6882. (org-insert-heading arg invisible-ok)))
  6883. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6884. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6885. (interactive "P")
  6886. (let ((org-insert-heading-respect-content t))
  6887. (org-insert-todo-heading force-state t)))
  6888. (defun org-insert-todo-heading (arg &optional force-heading)
  6889. "Insert a new heading with the same level and TODO state as current heading.
  6890. If the heading has no TODO state, or if the state is DONE, use the first
  6891. state (TODO by default). Also one prefix arg, force first state. With two
  6892. prefix args, force inserting at the end of the parent subtree."
  6893. (interactive "P")
  6894. (when (or force-heading (not (org-insert-item 'checkbox)))
  6895. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6896. force-heading))
  6897. (save-excursion
  6898. (org-back-to-heading)
  6899. (outline-previous-heading)
  6900. (looking-at org-todo-line-regexp))
  6901. (let*
  6902. ((new-mark-x
  6903. (if (or arg
  6904. (not (match-beginning 2))
  6905. (member (match-string 2) org-done-keywords))
  6906. (car org-todo-keywords-1)
  6907. (match-string 2)))
  6908. (new-mark
  6909. (or
  6910. (run-hook-with-args-until-success
  6911. 'org-todo-get-default-hook new-mark-x nil)
  6912. new-mark-x)))
  6913. (beginning-of-line 1)
  6914. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6915. (if org-treat-insert-todo-heading-as-state-change
  6916. (org-todo new-mark)
  6917. (insert new-mark " "))))
  6918. (when org-provide-todo-statistics
  6919. (org-update-parent-todo-statistics))))
  6920. (defun org-insert-subheading (arg)
  6921. "Insert a new subheading and demote it.
  6922. Works for outline headings and for plain lists alike."
  6923. (interactive "P")
  6924. (org-insert-heading arg)
  6925. (cond
  6926. ((org-at-heading-p) (org-do-demote))
  6927. ((org-at-item-p) (org-indent-item))))
  6928. (defun org-insert-todo-subheading (arg)
  6929. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6930. Works for outline headings and for plain lists alike."
  6931. (interactive "P")
  6932. (org-insert-todo-heading arg)
  6933. (cond
  6934. ((org-at-heading-p) (org-do-demote))
  6935. ((org-at-item-p) (org-indent-item))))
  6936. ;;; Promotion and Demotion
  6937. (defvar org-after-demote-entry-hook nil
  6938. "Hook run after an entry has been demoted.
  6939. The cursor will be at the beginning of the entry.
  6940. When a subtree is being demoted, the hook will be called for each node.")
  6941. (defvar org-after-promote-entry-hook nil
  6942. "Hook run after an entry has been promoted.
  6943. The cursor will be at the beginning of the entry.
  6944. When a subtree is being promoted, the hook will be called for each node.")
  6945. (defun org-promote-subtree ()
  6946. "Promote the entire subtree.
  6947. See also `org-promote'."
  6948. (interactive)
  6949. (save-excursion
  6950. (org-with-limited-levels (org-map-tree 'org-promote)))
  6951. (org-fix-position-after-promote))
  6952. (defun org-demote-subtree ()
  6953. "Demote the entire subtree. See `org-demote'.
  6954. See also `org-promote'."
  6955. (interactive)
  6956. (save-excursion
  6957. (org-with-limited-levels (org-map-tree 'org-demote)))
  6958. (org-fix-position-after-promote))
  6959. (defun org-do-promote ()
  6960. "Promote the current heading higher up the tree.
  6961. If the region is active in `transient-mark-mode', promote all headings
  6962. in the region."
  6963. (interactive)
  6964. (save-excursion
  6965. (if (org-region-active-p)
  6966. (org-map-region 'org-promote (region-beginning) (region-end))
  6967. (org-promote)))
  6968. (org-fix-position-after-promote))
  6969. (defun org-do-demote ()
  6970. "Demote the current heading lower down the tree.
  6971. If the region is active in `transient-mark-mode', demote all headings
  6972. in the region."
  6973. (interactive)
  6974. (save-excursion
  6975. (if (org-region-active-p)
  6976. (org-map-region 'org-demote (region-beginning) (region-end))
  6977. (org-demote)))
  6978. (org-fix-position-after-promote))
  6979. (defun org-fix-position-after-promote ()
  6980. "Make sure that after pro/demotion cursor position is right."
  6981. (let ((pos (point)))
  6982. (when (save-excursion
  6983. (beginning-of-line 1)
  6984. (looking-at org-todo-line-regexp)
  6985. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6986. (cond ((eobp) (insert " "))
  6987. ((eolp) (insert " "))
  6988. ((equal (char-after) ?\ ) (forward-char 1))))))
  6989. (defun org-current-level ()
  6990. "Return the level of the current entry, or nil if before the first headline.
  6991. The level is the number of stars at the beginning of the headline."
  6992. (save-excursion
  6993. (org-with-limited-levels
  6994. (if (ignore-errors (org-back-to-heading t))
  6995. (funcall outline-level)))))
  6996. (defun org-get-previous-line-level ()
  6997. "Return the outline depth of the last headline before the current line.
  6998. Returns 0 for the first headline in the buffer, and nil if before the
  6999. first headline."
  7000. (let ((current-level (org-current-level))
  7001. (prev-level (when (> (line-number-at-pos) 1)
  7002. (save-excursion
  7003. (beginning-of-line 0)
  7004. (org-current-level)))))
  7005. (cond ((null current-level) nil) ; Before first headline
  7006. ((null prev-level) 0) ; At first headline
  7007. (prev-level))))
  7008. (defun org-reduced-level (l)
  7009. "Compute the effective level of a heading.
  7010. This takes into account the setting of `org-odd-levels-only'."
  7011. (cond
  7012. ((zerop l) 0)
  7013. (org-odd-levels-only (1+ (floor (/ l 2))))
  7014. (t l)))
  7015. (defun org-level-increment ()
  7016. "Return the number of stars that will be added or removed at a
  7017. time to headlines when structure editing, based on the value of
  7018. `org-odd-levels-only'."
  7019. (if org-odd-levels-only 2 1))
  7020. (defun org-get-valid-level (level &optional change)
  7021. "Rectify a level change under the influence of `org-odd-levels-only'
  7022. LEVEL is a current level, CHANGE is by how much the level should be
  7023. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7024. even level numbers will become the next higher odd number."
  7025. (if org-odd-levels-only
  7026. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7027. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7028. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7029. (max 1 (+ level (or change 0)))))
  7030. (if (boundp 'define-obsolete-function-alias)
  7031. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7032. (define-obsolete-function-alias 'org-get-legal-level
  7033. 'org-get-valid-level)
  7034. (define-obsolete-function-alias 'org-get-legal-level
  7035. 'org-get-valid-level "23.1")))
  7036. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7037. ;; ̀org-with-limited-levels'
  7038. (defun org-promote ()
  7039. "Promote the current heading higher up the tree.
  7040. If the region is active in `transient-mark-mode', promote all headings
  7041. in the region."
  7042. (org-back-to-heading t)
  7043. (let* ((level (save-match-data (funcall outline-level)))
  7044. (after-change-functions (remove 'flyspell-after-change-function
  7045. after-change-functions))
  7046. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7047. (diff (abs (- level (length up-head) -1))))
  7048. (cond ((and (= level 1) org-called-with-limited-levels
  7049. org-allow-promoting-top-level-subtree)
  7050. (replace-match "# " nil t))
  7051. ((= level 1)
  7052. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  7053. (t (replace-match up-head nil t)))
  7054. ;; Fixup tag positioning
  7055. (unless (= level 1)
  7056. (and org-auto-align-tags (org-set-tags nil t))
  7057. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7058. (run-hooks 'org-after-promote-entry-hook)))
  7059. (defun org-demote ()
  7060. "Demote the current heading lower down the tree.
  7061. If the region is active in `transient-mark-mode', demote all headings
  7062. in the region."
  7063. (org-back-to-heading t)
  7064. (let* ((level (save-match-data (funcall outline-level)))
  7065. (after-change-functions (remove 'flyspell-after-change-function
  7066. after-change-functions))
  7067. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7068. (diff (abs (- level (length down-head) -1))))
  7069. (replace-match down-head nil t)
  7070. ;; Fixup tag positioning
  7071. (and org-auto-align-tags (org-set-tags nil t))
  7072. (if org-adapt-indentation (org-fixup-indentation diff))
  7073. (run-hooks 'org-after-demote-entry-hook)))
  7074. (defun org-cycle-level ()
  7075. "Cycle the level of an empty headline through possible states.
  7076. This goes first to child, then to parent, level, then up the hierarchy.
  7077. After top level, it switches back to sibling level."
  7078. (interactive)
  7079. (let ((org-adapt-indentation nil))
  7080. (when (org-point-at-end-of-empty-headline)
  7081. (setq this-command 'org-cycle-level) ; Only needed for caching
  7082. (let ((cur-level (org-current-level))
  7083. (prev-level (org-get-previous-line-level)))
  7084. (cond
  7085. ;; If first headline in file, promote to top-level.
  7086. ((= prev-level 0)
  7087. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7088. do (org-do-promote)))
  7089. ;; If same level as prev, demote one.
  7090. ((= prev-level cur-level)
  7091. (org-do-demote))
  7092. ;; If parent is top-level, promote to top level if not already.
  7093. ((= prev-level 1)
  7094. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7095. do (org-do-promote)))
  7096. ;; If top-level, return to prev-level.
  7097. ((= cur-level 1)
  7098. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7099. do (org-do-demote)))
  7100. ;; If less than prev-level, promote one.
  7101. ((< cur-level prev-level)
  7102. (org-do-promote))
  7103. ;; If deeper than prev-level, promote until higher than
  7104. ;; prev-level.
  7105. ((> cur-level prev-level)
  7106. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7107. do (org-do-promote))))
  7108. t))))
  7109. (defun org-map-tree (fun)
  7110. "Call FUN for every heading underneath the current one."
  7111. (org-back-to-heading)
  7112. (let ((level (funcall outline-level)))
  7113. (save-excursion
  7114. (funcall fun)
  7115. (while (and (progn
  7116. (outline-next-heading)
  7117. (> (funcall outline-level) level))
  7118. (not (eobp)))
  7119. (funcall fun)))))
  7120. (defun org-map-region (fun beg end)
  7121. "Call FUN for every heading between BEG and END."
  7122. (let ((org-ignore-region t))
  7123. (save-excursion
  7124. (setq end (copy-marker end))
  7125. (goto-char beg)
  7126. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7127. (< (point) end))
  7128. (funcall fun))
  7129. (while (and (progn
  7130. (outline-next-heading)
  7131. (< (point) end))
  7132. (not (eobp)))
  7133. (funcall fun)))))
  7134. (defvar org-property-end-re) ; silence byte-compiler
  7135. (defun org-fixup-indentation (diff)
  7136. "Change the indentation in the current entry by DIFF.
  7137. However, if any line in the current entry has no indentation, or if it
  7138. would end up with no indentation after the change, nothing at all is done."
  7139. (save-excursion
  7140. (let ((end (save-excursion (outline-next-heading)
  7141. (point-marker)))
  7142. (prohibit (if (> diff 0)
  7143. "^\\S-"
  7144. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7145. col)
  7146. (unless (save-excursion (end-of-line 1)
  7147. (re-search-forward prohibit end t))
  7148. (while (and (< (point) end)
  7149. (re-search-forward "^[ \t]+" end t))
  7150. (goto-char (match-end 0))
  7151. (setq col (current-column))
  7152. (if (< diff 0) (replace-match ""))
  7153. (org-indent-to-column (+ diff col))))
  7154. (move-marker end nil))))
  7155. (defun org-convert-to-odd-levels ()
  7156. "Convert an org-mode file with all levels allowed to one with odd levels.
  7157. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7158. level 5 etc."
  7159. (interactive)
  7160. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7161. (let ((outline-level 'org-outline-level)
  7162. (org-odd-levels-only nil) n)
  7163. (save-excursion
  7164. (goto-char (point-min))
  7165. (while (re-search-forward "^\\*\\*+ " nil t)
  7166. (setq n (- (length (match-string 0)) 2))
  7167. (while (>= (setq n (1- n)) 0)
  7168. (org-demote))
  7169. (end-of-line 1))))))
  7170. (defun org-convert-to-oddeven-levels ()
  7171. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7172. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7173. file contains a section with an even level, conversion would
  7174. destroy the structure of the file. An error is signaled in this
  7175. case."
  7176. (interactive)
  7177. (goto-char (point-min))
  7178. ;; First check if there are no even levels
  7179. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7180. (org-show-context t)
  7181. (error "Not all levels are odd in this file. Conversion not possible"))
  7182. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7183. (let ((outline-regexp org-outline-regexp)
  7184. (outline-level 'org-outline-level)
  7185. (org-odd-levels-only nil) n)
  7186. (save-excursion
  7187. (goto-char (point-min))
  7188. (while (re-search-forward "^\\*\\*+ " nil t)
  7189. (setq n (/ (1- (length (match-string 0))) 2))
  7190. (while (>= (setq n (1- n)) 0)
  7191. (org-promote))
  7192. (end-of-line 1))))))
  7193. (defun org-tr-level (n)
  7194. "Make N odd if required."
  7195. (if org-odd-levels-only (1+ (/ n 2)) n))
  7196. ;;; Vertical tree motion, cutting and pasting of subtrees
  7197. (defun org-move-subtree-up (&optional arg)
  7198. "Move the current subtree up past ARG headlines of the same level."
  7199. (interactive "p")
  7200. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7201. (defun org-move-subtree-down (&optional arg)
  7202. "Move the current subtree down past ARG headlines of the same level."
  7203. (interactive "p")
  7204. (setq arg (prefix-numeric-value arg))
  7205. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7206. 'org-get-last-sibling))
  7207. (ins-point (make-marker))
  7208. (cnt (abs arg))
  7209. (col (current-column))
  7210. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7211. ;; Select the tree
  7212. (org-back-to-heading)
  7213. (setq beg0 (point))
  7214. (save-excursion
  7215. (setq ne-beg (org-back-over-empty-lines))
  7216. (setq beg (point)))
  7217. (save-match-data
  7218. (save-excursion (outline-end-of-heading)
  7219. (setq folded (outline-invisible-p)))
  7220. (outline-end-of-subtree))
  7221. (outline-next-heading)
  7222. (setq ne-end (org-back-over-empty-lines))
  7223. (setq end (point))
  7224. (goto-char beg0)
  7225. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7226. ;; include less whitespace
  7227. (save-excursion
  7228. (goto-char beg)
  7229. (forward-line (- ne-beg ne-end))
  7230. (setq beg (point))))
  7231. ;; Find insertion point, with error handling
  7232. (while (> cnt 0)
  7233. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7234. (progn (goto-char beg0)
  7235. (user-error "Cannot move past superior level or buffer limit")))
  7236. (setq cnt (1- cnt)))
  7237. (if (> arg 0)
  7238. ;; Moving forward - still need to move over subtree
  7239. (progn (org-end-of-subtree t t)
  7240. (save-excursion
  7241. (org-back-over-empty-lines)
  7242. (or (bolp) (newline)))))
  7243. (setq ne-ins (org-back-over-empty-lines))
  7244. (move-marker ins-point (point))
  7245. (setq txt (buffer-substring beg end))
  7246. (org-save-markers-in-region beg end)
  7247. (delete-region beg end)
  7248. (org-remove-empty-overlays-at beg)
  7249. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7250. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7251. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7252. (let ((bbb (point)))
  7253. (insert-before-markers txt)
  7254. (org-reinstall-markers-in-region bbb)
  7255. (move-marker ins-point bbb))
  7256. (or (bolp) (insert "\n"))
  7257. (setq ins-end (point))
  7258. (goto-char ins-point)
  7259. (org-skip-whitespace)
  7260. (when (and (< arg 0)
  7261. (org-first-sibling-p)
  7262. (> ne-ins ne-beg))
  7263. ;; Move whitespace back to beginning
  7264. (save-excursion
  7265. (goto-char ins-end)
  7266. (let ((kill-whole-line t))
  7267. (kill-line (- ne-ins ne-beg)) (point)))
  7268. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7269. (move-marker ins-point nil)
  7270. (if folded
  7271. (hide-subtree)
  7272. (org-show-entry)
  7273. (show-children)
  7274. (org-cycle-hide-drawers 'children))
  7275. (org-clean-visibility-after-subtree-move)
  7276. ;; move back to the initial column we were at
  7277. (move-to-column col)))
  7278. (defvar org-subtree-clip ""
  7279. "Clipboard for cut and paste of subtrees.
  7280. This is actually only a copy of the kill, because we use the normal kill
  7281. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7282. (defvar org-subtree-clip-folded nil
  7283. "Was the last copied subtree folded?
  7284. This is used to fold the tree back after pasting.")
  7285. (defun org-cut-subtree (&optional n)
  7286. "Cut the current subtree into the clipboard.
  7287. With prefix arg N, cut this many sequential subtrees.
  7288. This is a short-hand for marking the subtree and then cutting it."
  7289. (interactive "p")
  7290. (org-copy-subtree n 'cut))
  7291. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7292. "Cut the current subtree into the clipboard.
  7293. With prefix arg N, cut this many sequential subtrees.
  7294. This is a short-hand for marking the subtree and then copying it.
  7295. If CUT is non-nil, actually cut the subtree.
  7296. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7297. of some markers in the region, even if CUT is non-nil. This is
  7298. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7299. (interactive "p")
  7300. (let (beg end folded (beg0 (point)))
  7301. (if (org-called-interactively-p 'any)
  7302. (org-back-to-heading nil) ; take what looks like a subtree
  7303. (org-back-to-heading t)) ; take what is really there
  7304. (setq beg (point))
  7305. (skip-chars-forward " \t\r\n")
  7306. (save-match-data
  7307. (if nosubtrees
  7308. (outline-next-heading)
  7309. (save-excursion (outline-end-of-heading)
  7310. (setq folded (outline-invisible-p)))
  7311. (condition-case nil
  7312. (org-forward-heading-same-level (1- n) t)
  7313. (error nil))
  7314. (org-end-of-subtree t t)))
  7315. (setq end (point))
  7316. (goto-char beg0)
  7317. (when (> end beg)
  7318. (setq org-subtree-clip-folded folded)
  7319. (when (or cut force-store-markers)
  7320. (org-save-markers-in-region beg end))
  7321. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7322. (setq org-subtree-clip (current-kill 0))
  7323. (message "%s: Subtree(s) with %d characters"
  7324. (if cut "Cut" "Copied")
  7325. (length org-subtree-clip)))))
  7326. (defun org-paste-subtree (&optional level tree for-yank)
  7327. "Paste the clipboard as a subtree, with modification of headline level.
  7328. The entire subtree is promoted or demoted in order to match a new headline
  7329. level.
  7330. If the cursor is at the beginning of a headline, the same level as
  7331. that headline is used to paste the tree.
  7332. If not, the new level is derived from the *visible* headings
  7333. before and after the insertion point, and taken to be the inferior headline
  7334. level of the two. So if the previous visible heading is level 3 and the
  7335. next is level 4 (or vice versa), level 4 will be used for insertion.
  7336. This makes sure that the subtree remains an independent subtree and does
  7337. not swallow low level entries.
  7338. You can also force a different level, either by using a numeric prefix
  7339. argument, or by inserting the heading marker by hand. For example, if the
  7340. cursor is after \"*****\", then the tree will be shifted to level 5.
  7341. If optional TREE is given, use this text instead of the kill ring.
  7342. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7343. move back over whitespace before inserting, and move point to the end of
  7344. the inserted text when done."
  7345. (interactive "P")
  7346. (setq tree (or tree (and kill-ring (current-kill 0))))
  7347. (unless (org-kill-is-subtree-p tree)
  7348. (error "%s"
  7349. (substitute-command-keys
  7350. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7351. (org-with-limited-levels
  7352. (let* ((visp (not (outline-invisible-p)))
  7353. (txt tree)
  7354. (^re_ "\\(\\*+\\)[ \t]*")
  7355. (old-level (if (string-match org-outline-regexp-bol txt)
  7356. (- (match-end 0) (match-beginning 0) 1)
  7357. -1))
  7358. (force-level (cond (level (prefix-numeric-value level))
  7359. ((and (looking-at "[ \t]*$")
  7360. (string-match
  7361. "^\\*+$" (buffer-substring
  7362. (point-at-bol) (point))))
  7363. (- (match-end 1) (match-beginning 1)))
  7364. ((and (bolp)
  7365. (looking-at org-outline-regexp))
  7366. (- (match-end 0) (point) 1))))
  7367. (previous-level (save-excursion
  7368. (condition-case nil
  7369. (progn
  7370. (outline-previous-visible-heading 1)
  7371. (if (looking-at ^re_)
  7372. (- (match-end 0) (match-beginning 0) 1)
  7373. 1))
  7374. (error 1))))
  7375. (next-level (save-excursion
  7376. (condition-case nil
  7377. (progn
  7378. (or (looking-at org-outline-regexp)
  7379. (outline-next-visible-heading 1))
  7380. (if (looking-at ^re_)
  7381. (- (match-end 0) (match-beginning 0) 1)
  7382. 1))
  7383. (error 1))))
  7384. (new-level (or force-level (max previous-level next-level)))
  7385. (shift (if (or (= old-level -1)
  7386. (= new-level -1)
  7387. (= old-level new-level))
  7388. 0
  7389. (- new-level old-level)))
  7390. (delta (if (> shift 0) -1 1))
  7391. (func (if (> shift 0) 'org-demote 'org-promote))
  7392. (org-odd-levels-only nil)
  7393. beg end newend)
  7394. ;; Remove the forced level indicator
  7395. (if force-level
  7396. (delete-region (point-at-bol) (point)))
  7397. ;; Paste
  7398. (beginning-of-line (if (bolp) 1 2))
  7399. (setq beg (point))
  7400. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7401. (insert-before-markers txt)
  7402. (unless (string-match "\n\\'" txt) (insert "\n"))
  7403. (setq newend (point))
  7404. (org-reinstall-markers-in-region beg)
  7405. (setq end (point))
  7406. (goto-char beg)
  7407. (skip-chars-forward " \t\n\r")
  7408. (setq beg (point))
  7409. (if (and (outline-invisible-p) visp)
  7410. (save-excursion (outline-show-heading)))
  7411. ;; Shift if necessary
  7412. (unless (= shift 0)
  7413. (save-restriction
  7414. (narrow-to-region beg end)
  7415. (while (not (= shift 0))
  7416. (org-map-region func (point-min) (point-max))
  7417. (setq shift (+ delta shift)))
  7418. (goto-char (point-min))
  7419. (setq newend (point-max))))
  7420. (when (or (org-called-interactively-p 'interactive) for-yank)
  7421. (message "Clipboard pasted as level %d subtree" new-level))
  7422. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7423. kill-ring
  7424. (eq org-subtree-clip (current-kill 0))
  7425. org-subtree-clip-folded)
  7426. ;; The tree was folded before it was killed/copied
  7427. (hide-subtree))
  7428. (and for-yank (goto-char newend)))))
  7429. (defun org-kill-is-subtree-p (&optional txt)
  7430. "Check if the current kill is an outline subtree, or a set of trees.
  7431. Returns nil if kill does not start with a headline, or if the first
  7432. headline level is not the largest headline level in the tree.
  7433. So this will actually accept several entries of equal levels as well,
  7434. which is OK for `org-paste-subtree'.
  7435. If optional TXT is given, check this string instead of the current kill."
  7436. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7437. (re (org-get-limited-outline-regexp))
  7438. (^re (concat "^" re))
  7439. (start-level (and kill
  7440. (string-match
  7441. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7442. kill)
  7443. (- (match-end 2) (match-beginning 2) 1)))
  7444. (start (1+ (or (match-beginning 2) -1))))
  7445. (if (not start-level)
  7446. (progn
  7447. nil) ;; does not even start with a heading
  7448. (catch 'exit
  7449. (while (setq start (string-match ^re kill (1+ start)))
  7450. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7451. (throw 'exit nil)))
  7452. t))))
  7453. (defvar org-markers-to-move nil
  7454. "Markers that should be moved with a cut-and-paste operation.
  7455. Those markers are stored together with their positions relative to
  7456. the start of the region.")
  7457. (defun org-save-markers-in-region (beg end)
  7458. "Check markers in region.
  7459. If these markers are between BEG and END, record their position relative
  7460. to BEG, so that after moving the block of text, we can put the markers back
  7461. into place.
  7462. This function gets called just before an entry or tree gets cut from the
  7463. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7464. called immediately, to move the markers with the entries."
  7465. (setq org-markers-to-move nil)
  7466. (when (featurep 'org-clock)
  7467. (org-clock-save-markers-for-cut-and-paste beg end))
  7468. (when (featurep 'org-agenda)
  7469. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7470. (defun org-check-and-save-marker (marker beg end)
  7471. "Check if MARKER is between BEG and END.
  7472. If yes, remember the marker and the distance to BEG."
  7473. (when (and (marker-buffer marker)
  7474. (equal (marker-buffer marker) (current-buffer)))
  7475. (if (and (>= marker beg) (< marker end))
  7476. (push (cons marker (- marker beg)) org-markers-to-move))))
  7477. (defun org-reinstall-markers-in-region (beg)
  7478. "Move all remembered markers to their position relative to BEG."
  7479. (mapc (lambda (x)
  7480. (move-marker (car x) (+ beg (cdr x))))
  7481. org-markers-to-move)
  7482. (setq org-markers-to-move nil))
  7483. (defun org-narrow-to-subtree ()
  7484. "Narrow buffer to the current subtree."
  7485. (interactive)
  7486. (save-excursion
  7487. (save-match-data
  7488. (org-with-limited-levels
  7489. (narrow-to-region
  7490. (progn (org-back-to-heading t) (point))
  7491. (progn (org-end-of-subtree t t)
  7492. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7493. (point)))))))
  7494. (defun org-narrow-to-block ()
  7495. "Narrow buffer to the current block."
  7496. (interactive)
  7497. (let* ((case-fold-search t)
  7498. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7499. "^[ \t]*#\\+end_.*")))
  7500. (if blockp
  7501. (narrow-to-region (car blockp) (cdr blockp))
  7502. (error "Not in a block"))))
  7503. (eval-when-compile
  7504. (defvar org-property-drawer-re))
  7505. (defvar org-property-start-re) ;; defined below
  7506. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7507. "Clone the task (subtree) at point N times.
  7508. The clones will be inserted as siblings.
  7509. In interactive use, the user will be prompted for the number of
  7510. clones to be produced. When called with a universal prefix argument
  7511. and if the entry has a timestamp, the user will also be prompted for
  7512. a time shift, which may be a repeater as used in time stamps, for
  7513. example `+3d'.
  7514. When a valid repeater is given and the entry contains any time
  7515. stamps, the clones will become a sequence in time, with time
  7516. stamps in the subtree shifted for each clone produced. If SHIFT
  7517. is nil or the empty string, time stamps will be left alone. The
  7518. ID property of the original subtree is removed.
  7519. If the original subtree did contain time stamps with a repeater,
  7520. the following will happen:
  7521. - the repeater will be removed in each clone
  7522. - an additional clone will be produced, with the current, unshifted
  7523. date(s) in the entry.
  7524. - the original entry will be placed *after* all the clones, with
  7525. repeater intact.
  7526. - the start days in the repeater in the original entry will be shifted
  7527. to past the last clone.
  7528. In this way you can spell out a number of instances of a repeating task,
  7529. and still retain the repeater to cover future instances of the task."
  7530. (interactive "nNumber of clones to produce: ")
  7531. (let ((shift
  7532. (or shift
  7533. (if (and (equal current-prefix-arg '(4))
  7534. (save-excursion
  7535. (re-search-forward org-ts-regexp-both
  7536. (save-excursion
  7537. (org-end-of-subtree t)
  7538. (point)) t)))
  7539. (read-from-minibuffer
  7540. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7541. ""))) ;; No time shift
  7542. (n-no-remove -1)
  7543. (drawer-re org-drawer-regexp)
  7544. beg end template task idprop
  7545. shift-n shift-what doshift nmin nmax)
  7546. (if (not (and (integerp n) (> n 0)))
  7547. (error "Invalid number of replications %s" n))
  7548. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7549. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7550. shift)))
  7551. (error "Invalid shift specification %s" shift))
  7552. (when doshift
  7553. (setq shift-n (string-to-number (match-string 1 shift))
  7554. shift-what (cdr (assoc (match-string 2 shift)
  7555. '(("d" . day) ("w" . week)
  7556. ("m" . month) ("y" . year))))))
  7557. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7558. (setq nmin 1 nmax n)
  7559. (org-back-to-heading t)
  7560. (setq beg (point))
  7561. (setq idprop (org-entry-get nil "ID"))
  7562. (org-end-of-subtree t t)
  7563. (or (bolp) (insert "\n"))
  7564. (setq end (point))
  7565. (setq template (buffer-substring beg end))
  7566. (when (and doshift
  7567. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7568. (delete-region beg end)
  7569. (setq end beg)
  7570. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7571. (goto-char end)
  7572. (loop for n from nmin to nmax do
  7573. ;; prepare clone
  7574. (with-temp-buffer
  7575. (insert template)
  7576. (org-mode)
  7577. (goto-char (point-min))
  7578. (org-show-subtree)
  7579. (and idprop (if org-clone-delete-id
  7580. (org-entry-delete nil "ID")
  7581. (org-id-get-create t)))
  7582. (unless (= n 0)
  7583. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7584. (kill-whole-line))
  7585. (goto-char (point-min))
  7586. (while (re-search-forward drawer-re nil t)
  7587. (mapc (lambda (d)
  7588. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7589. (goto-char (point-min))
  7590. (when doshift
  7591. (while (re-search-forward org-ts-regexp-both nil t)
  7592. (org-timestamp-change (* n shift-n) shift-what))
  7593. (unless (= n n-no-remove)
  7594. (goto-char (point-min))
  7595. (while (re-search-forward org-ts-regexp nil t)
  7596. (save-excursion
  7597. (goto-char (match-beginning 0))
  7598. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7599. (delete-region (match-beginning 1) (match-end 1)))))))
  7600. (setq task (buffer-string)))
  7601. (insert task))
  7602. (goto-char beg)))
  7603. ;;; Outline Sorting
  7604. (defun org-sort (with-case)
  7605. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7606. Optional argument WITH-CASE means sort case-sensitively."
  7607. (interactive "P")
  7608. (cond
  7609. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7610. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7611. (t
  7612. (org-call-with-arg 'org-sort-entries with-case))))
  7613. (defun org-sort-remove-invisible (s)
  7614. "Remove invisible links from string S."
  7615. (remove-text-properties 0 (length s) org-rm-props s)
  7616. (while (string-match org-bracket-link-regexp s)
  7617. (setq s (replace-match (if (match-end 2)
  7618. (match-string 3 s)
  7619. (match-string 1 s)) t t s)))
  7620. (let ((st (format " %s " s)))
  7621. (while (string-match org-emph-re st)
  7622. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7623. (setq s (substring st 1 -1)))
  7624. s)
  7625. (defvar org-priority-regexp) ; defined later in the file
  7626. (defvar org-after-sorting-entries-or-items-hook nil
  7627. "Hook that is run after a bunch of entries or items have been sorted.
  7628. When children are sorted, the cursor is in the parent line when this
  7629. hook gets called. When a region or a plain list is sorted, the cursor
  7630. will be in the first entry of the sorted region/list.")
  7631. (defun org-sort-entries
  7632. (&optional with-case sorting-type getkey-func compare-func property)
  7633. "Sort entries on a certain level of an outline tree.
  7634. If there is an active region, the entries in the region are sorted.
  7635. Else, if the cursor is before the first entry, sort the top-level items.
  7636. Else, the children of the entry at point are sorted.
  7637. Sorting can be alphabetically, numerically, by date/time as given by
  7638. a time stamp, by a property, by priority order, or by a custom function.
  7639. The command prompts for the sorting type unless it has been given to the
  7640. function through the SORTING-TYPE argument, which needs to be a character,
  7641. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7642. precise meaning of each character:
  7643. n Numerically, by converting the beginning of the entry/item to a number.
  7644. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7645. o By order of TODO keywords.
  7646. t By date/time, either the first active time stamp in the entry, or, if
  7647. none exist, by the first inactive one.
  7648. s By the scheduled date/time.
  7649. d By deadline date/time.
  7650. c By creation time, which is assumed to be the first inactive time stamp
  7651. at the beginning of a line.
  7652. p By priority according to the cookie.
  7653. r By the value of a property.
  7654. Capital letters will reverse the sort order.
  7655. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7656. called with point at the beginning of the record. It must return either
  7657. a string or a number that should serve as the sorting key for that record.
  7658. Comparing entries ignores case by default. However, with an optional argument
  7659. WITH-CASE, the sorting considers case as well.
  7660. Sorting is done against the visible part of the headlines, it ignores hidden
  7661. links."
  7662. (interactive "P")
  7663. (let ((case-func (if with-case 'identity 'downcase))
  7664. (cmstr
  7665. ;; The clock marker is lost when using `sort-subr', let's
  7666. ;; store the clocking string.
  7667. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7668. (save-excursion
  7669. (goto-char org-clock-marker)
  7670. (looking-back "^.*") (match-string-no-properties 0))))
  7671. start beg end stars re re2
  7672. txt what tmp)
  7673. ;; Find beginning and end of region to sort
  7674. (cond
  7675. ((org-region-active-p)
  7676. ;; we will sort the region
  7677. (setq end (region-end)
  7678. what "region")
  7679. (goto-char (region-beginning))
  7680. (if (not (org-at-heading-p)) (outline-next-heading))
  7681. (setq start (point)))
  7682. ((or (org-at-heading-p)
  7683. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7684. ;; we will sort the children of the current headline
  7685. (org-back-to-heading)
  7686. (setq start (point)
  7687. end (progn (org-end-of-subtree t t)
  7688. (or (bolp) (insert "\n"))
  7689. (org-back-over-empty-lines)
  7690. (point))
  7691. what "children")
  7692. (goto-char start)
  7693. (show-subtree)
  7694. (outline-next-heading))
  7695. (t
  7696. ;; we will sort the top-level entries in this file
  7697. (goto-char (point-min))
  7698. (or (org-at-heading-p) (outline-next-heading))
  7699. (setq start (point))
  7700. (goto-char (point-max))
  7701. (beginning-of-line 1)
  7702. (when (looking-at ".*?\\S-")
  7703. ;; File ends in a non-white line
  7704. (end-of-line 1)
  7705. (insert "\n"))
  7706. (setq end (point-max))
  7707. (setq what "top-level")
  7708. (goto-char start)
  7709. (show-all)))
  7710. (setq beg (point))
  7711. (if (>= beg end) (error "Nothing to sort"))
  7712. (looking-at "\\(\\*+\\)")
  7713. (setq stars (match-string 1)
  7714. re (concat "^" (regexp-quote stars) " +")
  7715. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7716. txt (buffer-substring beg end))
  7717. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7718. (if (and (not (equal stars "*")) (string-match re2 txt))
  7719. (error "Region to sort contains a level above the first entry"))
  7720. (unless sorting-type
  7721. (message
  7722. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7723. [t]ime [s]cheduled [d]eadline [c]reated
  7724. A/N/P/R/O/F/T/S/D/C means reversed:"
  7725. what)
  7726. (setq sorting-type (read-char-exclusive))
  7727. (unless getkey-func
  7728. (and (= (downcase sorting-type) ?f)
  7729. (setq getkey-func
  7730. (org-icompleting-read "Sort using function: "
  7731. obarray 'fboundp t nil nil))
  7732. (setq getkey-func (intern getkey-func))))
  7733. (and (= (downcase sorting-type) ?r)
  7734. (not property)
  7735. (setq property
  7736. (org-icompleting-read "Property: "
  7737. (mapcar 'list (org-buffer-property-keys t))
  7738. nil t))))
  7739. (message "Sorting entries...")
  7740. (save-restriction
  7741. (narrow-to-region start end)
  7742. (let ((dcst (downcase sorting-type))
  7743. (case-fold-search nil)
  7744. (now (current-time)))
  7745. (sort-subr
  7746. (/= dcst sorting-type)
  7747. ;; This function moves to the beginning character of the "record" to
  7748. ;; be sorted.
  7749. (lambda nil
  7750. (if (re-search-forward re nil t)
  7751. (goto-char (match-beginning 0))
  7752. (goto-char (point-max))))
  7753. ;; This function moves to the last character of the "record" being
  7754. ;; sorted.
  7755. (lambda nil
  7756. (save-match-data
  7757. (condition-case nil
  7758. (outline-forward-same-level 1)
  7759. (error
  7760. (goto-char (point-max))))))
  7761. ;; This function returns the value that gets sorted against.
  7762. (lambda nil
  7763. (cond
  7764. ((= dcst ?n)
  7765. (if (looking-at org-complex-heading-regexp)
  7766. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7767. nil))
  7768. ((= dcst ?a)
  7769. (if (looking-at org-complex-heading-regexp)
  7770. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7771. nil))
  7772. ((= dcst ?t)
  7773. (let ((end (save-excursion (outline-next-heading) (point))))
  7774. (if (or (re-search-forward org-ts-regexp end t)
  7775. (re-search-forward org-ts-regexp-both end t))
  7776. (org-time-string-to-seconds (match-string 0))
  7777. (org-float-time now))))
  7778. ((= dcst ?c)
  7779. (let ((end (save-excursion (outline-next-heading) (point))))
  7780. (if (re-search-forward
  7781. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7782. end t)
  7783. (org-time-string-to-seconds (match-string 0))
  7784. (org-float-time now))))
  7785. ((= dcst ?s)
  7786. (let ((end (save-excursion (outline-next-heading) (point))))
  7787. (if (re-search-forward org-scheduled-time-regexp end t)
  7788. (org-time-string-to-seconds (match-string 1))
  7789. (org-float-time now))))
  7790. ((= dcst ?d)
  7791. (let ((end (save-excursion (outline-next-heading) (point))))
  7792. (if (re-search-forward org-deadline-time-regexp end t)
  7793. (org-time-string-to-seconds (match-string 1))
  7794. (org-float-time now))))
  7795. ((= dcst ?p)
  7796. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7797. (string-to-char (match-string 2))
  7798. org-default-priority))
  7799. ((= dcst ?r)
  7800. (or (org-entry-get nil property) ""))
  7801. ((= dcst ?o)
  7802. (if (looking-at org-complex-heading-regexp)
  7803. (- 9999 (length (member (match-string 2)
  7804. org-todo-keywords-1)))))
  7805. ((= dcst ?f)
  7806. (if getkey-func
  7807. (progn
  7808. (setq tmp (funcall getkey-func))
  7809. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7810. tmp)
  7811. (error "Invalid key function `%s'" getkey-func)))
  7812. (t (error "Invalid sorting type `%c'" sorting-type))))
  7813. nil
  7814. (cond
  7815. ((= dcst ?a) 'string<)
  7816. ((= dcst ?f) compare-func)
  7817. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7818. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7819. ;; Reset the clock marker if needed
  7820. (when cmstr
  7821. (save-excursion
  7822. (goto-char start)
  7823. (search-forward cmstr nil t)
  7824. (move-marker org-clock-marker (point))))
  7825. (message "Sorting entries...done")))
  7826. (defun org-do-sort (table what &optional with-case sorting-type)
  7827. "Sort TABLE of WHAT according to SORTING-TYPE.
  7828. The user will be prompted for the SORTING-TYPE if the call to this
  7829. function does not specify it. WHAT is only for the prompt, to indicate
  7830. what is being sorted. The sorting key will be extracted from
  7831. the car of the elements of the table.
  7832. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7833. (unless sorting-type
  7834. (message
  7835. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7836. what)
  7837. (setq sorting-type (read-char-exclusive)))
  7838. (let ((dcst (downcase sorting-type))
  7839. extractfun comparefun)
  7840. ;; Define the appropriate functions
  7841. (cond
  7842. ((= dcst ?n)
  7843. (setq extractfun 'string-to-number
  7844. comparefun (if (= dcst sorting-type) '< '>)))
  7845. ((= dcst ?a)
  7846. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7847. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7848. comparefun (if (= dcst sorting-type)
  7849. 'string<
  7850. (lambda (a b) (and (not (string< a b))
  7851. (not (string= a b)))))))
  7852. ((= dcst ?t)
  7853. (setq extractfun
  7854. (lambda (x)
  7855. (if (or (string-match org-ts-regexp x)
  7856. (string-match org-ts-regexp-both x))
  7857. (org-float-time
  7858. (org-time-string-to-time (match-string 0 x)))
  7859. 0))
  7860. comparefun (if (= dcst sorting-type) '< '>)))
  7861. (t (error "Invalid sorting type `%c'" sorting-type)))
  7862. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7863. table)
  7864. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7865. ;;; The orgstruct minor mode
  7866. ;; Define a minor mode which can be used in other modes in order to
  7867. ;; integrate the org-mode structure editing commands.
  7868. ;; This is really a hack, because the org-mode structure commands use
  7869. ;; keys which normally belong to the major mode. Here is how it
  7870. ;; works: The minor mode defines all the keys necessary to operate the
  7871. ;; structure commands, but wraps the commands into a function which
  7872. ;; tests if the cursor is currently at a headline or a plain list
  7873. ;; item. If that is the case, the structure command is used,
  7874. ;; temporarily setting many Org-mode variables like regular
  7875. ;; expressions for filling etc. However, when any of those keys is
  7876. ;; used at a different location, function uses `key-binding' to look
  7877. ;; up if the key has an associated command in another currently active
  7878. ;; keymap (minor modes, major mode, global), and executes that
  7879. ;; command. There might be problems if any of the keys is otherwise
  7880. ;; used as a prefix key.
  7881. (defcustom orgstruct-heading-prefix-regexp nil
  7882. "Regexp that matches the custom prefix of Org headlines in
  7883. orgstruct(++)-mode."
  7884. :group 'org
  7885. :version "24.4"
  7886. :package-version '(Org . "8.0")
  7887. :type 'string)
  7888. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7889. (defcustom orgstruct-setup-hook nil
  7890. "Hook run after orgstruct-mode-map is filled."
  7891. :group 'org
  7892. :version "24.4"
  7893. :package-version '(Org . "8.0")
  7894. :type 'hook)
  7895. (defvar orgstruct-initialized nil)
  7896. (defvar org-local-vars nil
  7897. "List of local variables, for use by `orgstruct-mode'.")
  7898. ;;;###autoload
  7899. (define-minor-mode orgstruct-mode
  7900. "Toggle the minor mode `orgstruct-mode'.
  7901. This mode is for using Org-mode structure commands in other
  7902. modes. The following keys behave as if Org-mode were active, if
  7903. the cursor is on a headline, or on a plain list item (both as
  7904. defined by Org-mode)."
  7905. nil " OrgStruct" (make-sparse-keymap)
  7906. (funcall (if orgstruct-mode
  7907. 'add-to-invisibility-spec
  7908. 'remove-from-invisibility-spec)
  7909. '(outline . t))
  7910. (when orgstruct-mode
  7911. (org-load-modules-maybe)
  7912. (unless orgstruct-initialized
  7913. (orgstruct-setup)
  7914. (setq orgstruct-initialized t))))
  7915. ;;;###autoload
  7916. (defun turn-on-orgstruct ()
  7917. "Unconditionally turn on `orgstruct-mode'."
  7918. (orgstruct-mode 1))
  7919. (defvar org-fb-vars nil)
  7920. (make-variable-buffer-local 'org-fb-vars)
  7921. (defun orgstruct++-mode (&optional arg)
  7922. "Toggle `orgstruct-mode', the enhanced version of it.
  7923. In addition to setting orgstruct-mode, this also exports all
  7924. indentation and autofilling variables from org-mode into the
  7925. buffer. It will also recognize item context in multiline items."
  7926. (interactive "P")
  7927. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7928. (if (< arg 1)
  7929. (progn (orgstruct-mode -1)
  7930. (mapc (lambda(v)
  7931. (org-set-local (car v)
  7932. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7933. org-fb-vars))
  7934. (orgstruct-mode 1)
  7935. (setq org-fb-vars nil)
  7936. (let (var val)
  7937. (mapc
  7938. (lambda (x)
  7939. (when (string-match
  7940. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7941. (symbol-name (car x)))
  7942. (setq var (car x) val (nth 1 x))
  7943. (push (list var `(quote ,(eval var))) org-fb-vars)
  7944. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7945. org-local-vars)
  7946. (org-set-local 'orgstruct-is-++ t))))
  7947. (defvar orgstruct-is-++ nil
  7948. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7949. (make-variable-buffer-local 'orgstruct-is-++)
  7950. ;;;###autoload
  7951. (defun turn-on-orgstruct++ ()
  7952. "Unconditionally turn on `orgstruct++-mode'."
  7953. (orgstruct++-mode 1))
  7954. (defun orgstruct-error ()
  7955. "Error when there is no default binding for a structure key."
  7956. (interactive)
  7957. (funcall (if (fboundp 'user-error)
  7958. 'user-error
  7959. 'error)
  7960. "This key has no function outside structure elements"))
  7961. (defun orgstruct-setup ()
  7962. "Setup orgstruct keymap."
  7963. (dolist (cell '((org-demote . t)
  7964. (org-metaleft . t)
  7965. (org-metaright . t)
  7966. (org-promote . t)
  7967. (org-shiftmetaleft . t)
  7968. (org-shiftmetaright . t)
  7969. org-backward-element
  7970. org-backward-heading-same-level
  7971. org-ctrl-c-ret
  7972. org-ctrl-c-minus
  7973. org-ctrl-c-star
  7974. org-cycle
  7975. org-forward-heading-same-level
  7976. org-insert-heading
  7977. org-insert-heading-respect-content
  7978. org-kill-note-or-show-branches
  7979. org-mark-subtree
  7980. org-meta-return
  7981. org-metadown
  7982. org-metaup
  7983. org-narrow-to-subtree
  7984. org-promote-subtree
  7985. org-reveal
  7986. org-shiftdown
  7987. org-shiftleft
  7988. org-shiftmetadown
  7989. org-shiftmetaup
  7990. org-shiftright
  7991. org-shifttab
  7992. org-shifttab
  7993. org-shiftup
  7994. org-show-subtree
  7995. org-sort
  7996. org-up-element
  7997. outline-demote
  7998. outline-next-visible-heading
  7999. outline-previous-visible-heading
  8000. outline-promote
  8001. outline-up-heading
  8002. show-children))
  8003. (let ((f (or (car-safe cell) cell))
  8004. (disable-when-heading-prefix (cdr-safe cell)))
  8005. (when (fboundp f)
  8006. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8007. (where-is-internal f outline-mode-map)))
  8008. ;; TODO use local-function-key-map
  8009. (dolist (rep '(("<tab>" . "TAB")
  8010. ("<return>" . "RET")
  8011. ("<escape>" . "ESC")
  8012. ("<delete>" . "DEL")))
  8013. (setq binding (read-kbd-macro
  8014. (let ((case-fold-search))
  8015. (replace-regexp-in-string
  8016. (regexp-quote (cdr rep))
  8017. (car rep)
  8018. (key-description binding))))))
  8019. (let ((key (lookup-key orgstruct-mode-map binding)))
  8020. (when (or (not key) (numberp key))
  8021. (condition-case nil
  8022. (org-defkey orgstruct-mode-map
  8023. binding
  8024. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8025. (error nil))))))))
  8026. (run-hooks 'orgstruct-setup-hook))
  8027. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8028. "Create a function for binding in the structure minor mode.
  8029. FUN is the command to call inside a table. KEY is the key that
  8030. should be checked in for a command to execute outside of tables.
  8031. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8032. if `orgstruct-heading-prefix-regexp' is non-nil."
  8033. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8034. (let ((nname name)
  8035. (i 0))
  8036. (while (fboundp (intern nname))
  8037. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8038. (setq name (intern nname)))
  8039. (eval
  8040. (let ((bindings '((org-heading-regexp
  8041. (concat "^"
  8042. orgstruct-heading-prefix-regexp
  8043. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8044. (org-outline-regexp
  8045. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8046. (org-outline-regexp-bol
  8047. (concat "^" org-outline-regexp))
  8048. (outline-regexp org-outline-regexp)
  8049. (outline-heading-end-regexp "\n")
  8050. (outline-level 'org-outline-level)
  8051. (outline-heading-alist))))
  8052. `(defun ,name (arg)
  8053. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8054. "Outside of structure, run the binding of `"
  8055. (key-description key) "'."
  8056. (when disable-when-heading-prefix
  8057. (concat
  8058. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8059. "back to the default binding due to limitations of Org's implementation of\n"
  8060. "`" (symbol-name fun) "'.")))
  8061. (interactive "p")
  8062. (let* ((disable
  8063. ,(when disable-when-heading-prefix
  8064. '(and orgstruct-heading-prefix-regexp
  8065. (not (string= orgstruct-heading-prefix-regexp "")))))
  8066. (fallback
  8067. (or disable
  8068. (not
  8069. (let* ,bindings
  8070. (org-context-p 'headline 'item
  8071. ,(when (memq fun '(org-insert-heading))
  8072. '(when orgstruct-is-++
  8073. 'item-body))))))))
  8074. (if fallback
  8075. (let* ((orgstruct-mode)
  8076. (binding
  8077. (loop with key = ,key
  8078. for rep in
  8079. '(nil
  8080. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8081. ("<\\([^>]*\\)return>" . "\\1RET")
  8082. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8083. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8084. do
  8085. (when rep
  8086. (setq key (read-kbd-macro
  8087. (let ((case-fold-search))
  8088. (replace-regexp-in-string
  8089. (car rep)
  8090. (cdr rep)
  8091. (key-description key))))))
  8092. thereis (key-binding key))))
  8093. (if (keymapp binding)
  8094. (set-temporary-overlay-map binding)
  8095. (let ((func (or binding
  8096. (unless disable
  8097. 'orgstruct-error))))
  8098. (when func
  8099. (call-interactively func)))))
  8100. (org-run-like-in-org-mode
  8101. (lambda ()
  8102. (interactive)
  8103. (let* ,bindings
  8104. (call-interactively ',fun)))))))))
  8105. name))
  8106. (defun org-contextualize-keys (alist contexts)
  8107. "Return valid elements in ALIST depending on CONTEXTS.
  8108. `org-agenda-custom-commands' or `org-capture-templates' are the
  8109. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8110. or `org-capture-templates-contexts' are the associated contexts
  8111. definitions."
  8112. (let ((contexts
  8113. ;; normalize contexts
  8114. (mapcar
  8115. (lambda(c) (cond ((listp (cadr c))
  8116. (list (car c) (car c) (cadr c)))
  8117. ((string= "" (cadr c))
  8118. (list (car c) (car c) (caddr c)))
  8119. (t c))) contexts))
  8120. (a alist) c r s)
  8121. ;; loop over all commands or templates
  8122. (while (setq c (pop a))
  8123. (let (vrules repl)
  8124. (cond
  8125. ((not (assoc (car c) contexts))
  8126. (push c r))
  8127. ((and (assoc (car c) contexts)
  8128. (setq vrules (org-contextualize-validate-key
  8129. (car c) contexts)))
  8130. (mapc (lambda (vr)
  8131. (when (not (equal (car vr) (cadr vr)))
  8132. (setq repl vr))) vrules)
  8133. (if (not repl) (push c r)
  8134. (push (cadr repl) s)
  8135. (push
  8136. (cons (car c)
  8137. (cdr (or (assoc (cadr repl) alist)
  8138. (error "Undefined key `%s' as contextual replacement for `%s'"
  8139. (cadr repl) (car c)))))
  8140. r))))))
  8141. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8142. (delq
  8143. nil
  8144. (delete-dups
  8145. (mapcar (lambda (x)
  8146. (let ((tpl (car x)))
  8147. (when (not (delq
  8148. nil
  8149. (mapcar (lambda(y)
  8150. (equal y tpl)) s))) x)))
  8151. (reverse r))))))
  8152. (defun org-contextualize-validate-key (key contexts)
  8153. "Check CONTEXTS for agenda or capture KEY."
  8154. (let (r rr res)
  8155. (while (setq r (pop contexts))
  8156. (mapc
  8157. (lambda (rr)
  8158. (when
  8159. (and (equal key (car r))
  8160. (if (functionp rr) (funcall rr)
  8161. (or (and (eq (car rr) 'in-file)
  8162. (buffer-file-name)
  8163. (string-match (cdr rr) (buffer-file-name)))
  8164. (and (eq (car rr) 'in-mode)
  8165. (string-match (cdr rr) (symbol-name major-mode)))
  8166. (and (eq (car rr) 'in-buffer)
  8167. (string-match (cdr rr) (buffer-name)))
  8168. (when (and (eq (car rr) 'not-in-file)
  8169. (buffer-file-name))
  8170. (not (string-match (cdr rr) (buffer-file-name))))
  8171. (when (eq (car rr) 'not-in-mode)
  8172. (not (string-match (cdr rr) (symbol-name major-mode))))
  8173. (when (eq (car rr) 'not-in-buffer)
  8174. (not (string-match (cdr rr) (buffer-name)))))))
  8175. (push r res)))
  8176. (car (last r))))
  8177. (delete-dups (delq nil res))))
  8178. (defun org-context-p (&rest contexts)
  8179. "Check if local context is any of CONTEXTS.
  8180. Possible values in the list of contexts are `table', `headline', and `item'."
  8181. (let ((pos (point)))
  8182. (goto-char (point-at-bol))
  8183. (prog1 (or (and (memq 'table contexts)
  8184. (looking-at "[ \t]*|"))
  8185. (and (memq 'headline contexts)
  8186. (looking-at org-outline-regexp))
  8187. (and (memq 'item contexts)
  8188. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8189. (and (memq 'item-body contexts)
  8190. (org-in-item-p)))
  8191. (goto-char pos))))
  8192. (defun org-get-local-variables ()
  8193. "Return a list of all local variables in an Org mode buffer."
  8194. (let (varlist)
  8195. (with-current-buffer (get-buffer-create "*Org tmp*")
  8196. (erase-buffer)
  8197. (org-mode)
  8198. (setq varlist (buffer-local-variables)))
  8199. (kill-buffer "*Org tmp*")
  8200. (delq nil
  8201. (mapcar
  8202. (lambda (x)
  8203. (setq x
  8204. (if (symbolp x)
  8205. (list x)
  8206. (list (car x) (cdr x))))
  8207. (if (and (not (get (car x) 'org-state))
  8208. (string-match
  8209. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8210. (symbol-name (car x))))
  8211. x nil))
  8212. varlist))))
  8213. (defun org-clone-local-variables (from-buffer &optional regexp)
  8214. "Clone local variables from FROM-BUFFER.
  8215. Optional argument REGEXP selects variables to clone."
  8216. (mapc
  8217. (lambda (pair)
  8218. (and (symbolp (car pair))
  8219. (or (null regexp)
  8220. (string-match regexp (symbol-name (car pair))))
  8221. (set (make-local-variable (car pair))
  8222. (cdr pair))))
  8223. (buffer-local-variables from-buffer)))
  8224. ;;;###autoload
  8225. (defun org-run-like-in-org-mode (cmd)
  8226. "Run a command, pretending that the current buffer is in Org-mode.
  8227. This will temporarily bind local variables that are typically bound in
  8228. Org-mode to the values they have in Org-mode, and then interactively
  8229. call CMD."
  8230. (org-load-modules-maybe)
  8231. (unless org-local-vars
  8232. (setq org-local-vars (org-get-local-variables)))
  8233. (let (binds)
  8234. (dolist (var org-local-vars)
  8235. (when (or (not (boundp (car var)))
  8236. (eq (symbol-value (car var))
  8237. (default-value (car var))))
  8238. (push (list (car var) `(quote ,(cadr var))) binds)))
  8239. (eval `(let ,binds
  8240. (call-interactively (quote ,cmd))))))
  8241. ;;;; Archiving
  8242. (defun org-get-category (&optional pos force-refresh)
  8243. "Get the category applying to position POS."
  8244. (save-match-data
  8245. (if force-refresh (org-refresh-category-properties))
  8246. (let ((pos (or pos (point))))
  8247. (or (get-text-property pos 'org-category)
  8248. (progn (org-refresh-category-properties)
  8249. (get-text-property pos 'org-category))))))
  8250. (defun org-refresh-category-properties ()
  8251. "Refresh category text properties in the buffer."
  8252. (let ((case-fold-search t)
  8253. (inhibit-read-only t)
  8254. (def-cat (cond
  8255. ((null org-category)
  8256. (if buffer-file-name
  8257. (file-name-sans-extension
  8258. (file-name-nondirectory buffer-file-name))
  8259. "???"))
  8260. ((symbolp org-category) (symbol-name org-category))
  8261. (t org-category)))
  8262. beg end cat pos optionp)
  8263. (org-with-silent-modifications
  8264. (save-excursion
  8265. (save-restriction
  8266. (widen)
  8267. (goto-char (point-min))
  8268. (put-text-property (point) (point-max) 'org-category def-cat)
  8269. (while (re-search-forward
  8270. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8271. (setq pos (match-end 0)
  8272. optionp (equal (char-after (match-beginning 0)) ?#)
  8273. cat (org-trim (match-string 2)))
  8274. (if optionp
  8275. (setq beg (point-at-bol) end (point-max))
  8276. (org-back-to-heading t)
  8277. (setq beg (point) end (org-end-of-subtree t t)))
  8278. (put-text-property beg end 'org-category cat)
  8279. (put-text-property beg end 'org-category-position beg)
  8280. (goto-char pos)))))))
  8281. (defun org-refresh-properties (dprop tprop)
  8282. "Refresh buffer text properties.
  8283. DPROP is the drawer property and TPROP is the corresponding text
  8284. property to set."
  8285. (let ((case-fold-search t)
  8286. (inhibit-read-only t) p)
  8287. (org-with-silent-modifications
  8288. (save-excursion
  8289. (save-restriction
  8290. (widen)
  8291. (goto-char (point-min))
  8292. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8293. (setq p (org-match-string-no-properties 1))
  8294. (save-excursion
  8295. (org-back-to-heading t)
  8296. (put-text-property
  8297. (point-at-bol) (point-at-eol) tprop p))))))))
  8298. ;;;; Link Stuff
  8299. ;;; Link abbreviations
  8300. (defun org-link-expand-abbrev (link)
  8301. "Apply replacements as defined in `org-link-abbrev-alist'."
  8302. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8303. (let* ((key (match-string 1 link))
  8304. (as (or (assoc key org-link-abbrev-alist-local)
  8305. (assoc key org-link-abbrev-alist)))
  8306. (tag (and (match-end 2) (match-string 3 link)))
  8307. rpl)
  8308. (if (not as)
  8309. link
  8310. (setq rpl (cdr as))
  8311. (cond
  8312. ((symbolp rpl) (funcall rpl tag))
  8313. ((string-match "%(\\([^)]+\\))" rpl)
  8314. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8315. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8316. ((string-match "%h" rpl)
  8317. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8318. (t (concat rpl tag)))))
  8319. link))
  8320. ;;; Storing and inserting links
  8321. (defvar org-insert-link-history nil
  8322. "Minibuffer history for links inserted with `org-insert-link'.")
  8323. (defvar org-stored-links nil
  8324. "Contains the links stored with `org-store-link'.")
  8325. (defvar org-store-link-plist nil
  8326. "Plist with info about the most recently link created with `org-store-link'.")
  8327. (defvar org-link-protocols nil
  8328. "Link protocols added to Org-mode using `org-add-link-type'.")
  8329. (defvar org-store-link-functions nil
  8330. "List of functions that are called to create and store a link.
  8331. Each function will be called in turn until one returns a non-nil
  8332. value. Each function should check if it is responsible for creating
  8333. this link (for example by looking at the major mode).
  8334. If not, it must exit and return nil.
  8335. If yes, it should return a non-nil value after a calling
  8336. `org-store-link-props' with a list of properties and values.
  8337. Special properties are:
  8338. :type The link prefix, like \"http\". This must be given.
  8339. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8340. This is obligatory as well.
  8341. :description Optional default description for the second pair
  8342. of brackets in an Org-mode link. The user can still change
  8343. this when inserting this link into an Org-mode buffer.
  8344. In addition to these, any additional properties can be specified
  8345. and then used in capture templates.")
  8346. (defun org-add-link-type (type &optional follow export)
  8347. "Add TYPE to the list of `org-link-types'.
  8348. Re-compute all regular expressions depending on `org-link-types'
  8349. FOLLOW and EXPORT are two functions.
  8350. FOLLOW should take the link path as the single argument and do whatever
  8351. is necessary to follow the link, for example find a file or display
  8352. a mail message.
  8353. EXPORT should format the link path for export to one of the export formats.
  8354. It should be a function accepting three arguments:
  8355. path the path of the link, the text after the prefix (like \"http:\")
  8356. desc the description of the link, if any, or a description added by
  8357. org-export-normalize-links if there is none
  8358. format the export format, a symbol like `html' or `latex' or `ascii'..
  8359. The function may use the FORMAT information to return different values
  8360. depending on the format. The return value will be put literally into
  8361. the exported file. If the return value is nil, this means Org should
  8362. do what it normally does with links which do not have EXPORT defined.
  8363. Org-mode has a built-in default for exporting links. If you are happy with
  8364. this default, there is no need to define an export function for the link
  8365. type. For a simple example of an export function, see `org-bbdb.el'."
  8366. (add-to-list 'org-link-types type t)
  8367. (org-make-link-regexps)
  8368. (if (assoc type org-link-protocols)
  8369. (setcdr (assoc type org-link-protocols) (list follow export))
  8370. (push (list type follow export) org-link-protocols)))
  8371. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8372. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8373. ;;;###autoload
  8374. (defun org-store-link (arg)
  8375. "\\<org-mode-map>Store an org-link to the current location.
  8376. This link is added to `org-stored-links' and can later be inserted
  8377. into an org-buffer with \\[org-insert-link].
  8378. For some link types, a prefix arg is interpreted.
  8379. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8380. For file links, arg negates `org-context-in-file-links'.
  8381. A double prefix arg force skipping storing functions that are not
  8382. part of Org's core.
  8383. A triple prefix arg force storing a link for each line in the
  8384. active region."
  8385. (interactive "P")
  8386. (org-load-modules-maybe)
  8387. (if (and (equal arg '(64)) (org-region-active-p))
  8388. (save-excursion
  8389. (let ((end (region-end)))
  8390. (goto-char (region-beginning))
  8391. (set-mark (point))
  8392. (while (< (point-at-eol) end)
  8393. (move-end-of-line 1) (activate-mark)
  8394. (let (current-prefix-arg)
  8395. (call-interactively 'org-store-link))
  8396. (move-beginning-of-line 2)
  8397. (set-mark (point)))))
  8398. (org-with-limited-levels
  8399. (setq org-store-link-plist nil)
  8400. (let (link cpltxt desc description search
  8401. txt custom-id agenda-link sfuns sfunsn)
  8402. (cond
  8403. ;; Store a link using an external link type
  8404. ((and (not (equal arg '(16)))
  8405. (setq sfuns
  8406. (delq
  8407. nil (mapcar (lambda (f)
  8408. (let (fs) (if (funcall f) (push f fs))))
  8409. org-store-link-functions))
  8410. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8411. (or (and (cdr sfuns)
  8412. (funcall (intern
  8413. (completing-read
  8414. "Which function for creating the link? "
  8415. sfunsn t (car sfunsn)))))
  8416. (funcall (caar sfuns)))
  8417. (setq link (plist-get org-store-link-plist :link)
  8418. desc (or (plist-get org-store-link-plist
  8419. :description) link))))
  8420. ;; Store a link from a source code buffer
  8421. ((org-src-edit-buffer-p)
  8422. (let (label gc)
  8423. (while (or (not label)
  8424. (save-excursion
  8425. (save-restriction
  8426. (widen)
  8427. (goto-char (point-min))
  8428. (re-search-forward
  8429. (regexp-quote (format org-coderef-label-format label))
  8430. nil t))))
  8431. (when label (message "Label exists already") (sit-for 2))
  8432. (setq label (read-string "Code line label: " label)))
  8433. (end-of-line 1)
  8434. (setq link (format org-coderef-label-format label))
  8435. (setq gc (- 79 (length link)))
  8436. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8437. (insert link)
  8438. (setq link (concat "(" label ")") desc nil)))
  8439. ;; We are in the agenda, link to referenced location
  8440. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8441. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8442. (get-text-property (point) 'org-marker))))
  8443. (when m
  8444. (org-with-point-at m
  8445. (setq agenda-link
  8446. (if (org-called-interactively-p 'any)
  8447. (call-interactively 'org-store-link)
  8448. (org-store-link nil)))))))
  8449. ((eq major-mode 'calendar-mode)
  8450. (let ((cd (calendar-cursor-to-date)))
  8451. (setq link
  8452. (format-time-string
  8453. (car org-time-stamp-formats)
  8454. (apply 'encode-time
  8455. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8456. nil nil nil))))
  8457. (org-store-link-props :type "calendar" :date cd)))
  8458. ((eq major-mode 'help-mode)
  8459. (setq link (concat "help:" (save-excursion
  8460. (goto-char (point-min))
  8461. (looking-at "^[^ ]+")
  8462. (match-string 0))))
  8463. (org-store-link-props :type "help"))
  8464. ((eq major-mode 'w3-mode)
  8465. (setq cpltxt (if (and (buffer-name)
  8466. (not (string-match "Untitled" (buffer-name))))
  8467. (buffer-name)
  8468. (url-view-url t))
  8469. link (url-view-url t))
  8470. (org-store-link-props :type "w3" :url (url-view-url t)))
  8471. ((eq major-mode 'image-mode)
  8472. (setq cpltxt (concat "file:"
  8473. (abbreviate-file-name buffer-file-name))
  8474. link cpltxt)
  8475. (org-store-link-props :type "image" :file buffer-file-name))
  8476. ;; In dired, store a link to the file of the current line
  8477. ((eq major-mode 'dired-mode)
  8478. (let ((file (dired-get-filename nil t)))
  8479. (setq file (if file
  8480. (abbreviate-file-name
  8481. (expand-file-name (dired-get-filename nil t)))
  8482. ;; otherwise, no file so use current directory.
  8483. default-directory))
  8484. (setq cpltxt (concat "file:" file)
  8485. link cpltxt)))
  8486. ((setq search (run-hook-with-args-until-success
  8487. 'org-create-file-search-functions))
  8488. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8489. "::" search))
  8490. (setq cpltxt (or description link)))
  8491. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8492. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8493. (cond
  8494. ;; Store a link using the radio target at point
  8495. ((org-in-regexp "<<\\(.*?\\)>>")
  8496. (setq cpltxt
  8497. (concat "file:"
  8498. (abbreviate-file-name
  8499. (buffer-file-name (buffer-base-buffer)))
  8500. "::" (match-string 1))
  8501. link cpltxt))
  8502. ((and (featurep 'org-id)
  8503. (or (eq org-id-link-to-org-use-id t)
  8504. (and (org-called-interactively-p 'any)
  8505. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8506. (and (eq org-id-link-to-org-use-id
  8507. 'create-if-interactive-and-no-custom-id)
  8508. (not custom-id))))
  8509. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8510. ;; Store a link using the ID at point
  8511. (setq link (condition-case nil
  8512. (prog1 (org-id-store-link)
  8513. (setq desc (plist-get org-store-link-plist
  8514. :description)))
  8515. (error
  8516. ;; Probably before first headline, link only to file
  8517. (concat "file:"
  8518. (abbreviate-file-name
  8519. (buffer-file-name (buffer-base-buffer))))))))
  8520. (t
  8521. ;; Just link to current headline
  8522. (setq cpltxt (concat "file:"
  8523. (abbreviate-file-name
  8524. (buffer-file-name (buffer-base-buffer)))))
  8525. ;; Add a context search string
  8526. (when (org-xor org-context-in-file-links arg)
  8527. (let* ((ee (org-element-at-point))
  8528. (et (org-element-type ee))
  8529. (ev (plist-get (cadr ee) :value))
  8530. (ek (plist-get (cadr ee) :key))
  8531. (eok (and (stringp ek) (string-match "name\\|target" ek))))
  8532. (setq txt (cond
  8533. ((org-at-heading-p) nil)
  8534. ((and (eq et 'keyword) eok) ev)
  8535. ((org-region-active-p)
  8536. (buffer-substring (region-beginning) (region-end)))))
  8537. (when (or (null txt) (string-match "\\S-" txt))
  8538. (setq cpltxt
  8539. (concat cpltxt "::"
  8540. (condition-case nil
  8541. (org-make-org-heading-search-string txt)
  8542. (error "")))
  8543. desc (or (and (eq et 'keyword) eok ev)
  8544. (nth 4 (ignore-errors (org-heading-components)))
  8545. "NONE")))))
  8546. (if (string-match "::\\'" cpltxt)
  8547. (setq cpltxt (substring cpltxt 0 -2)))
  8548. (setq link cpltxt))))
  8549. ((buffer-file-name (buffer-base-buffer))
  8550. ;; Just link to this file here.
  8551. (setq cpltxt (concat "file:"
  8552. (abbreviate-file-name
  8553. (buffer-file-name (buffer-base-buffer)))))
  8554. ;; Add a context string.
  8555. (when (org-xor org-context-in-file-links arg)
  8556. (setq txt (if (org-region-active-p)
  8557. (buffer-substring (region-beginning) (region-end))
  8558. (buffer-substring (point-at-bol) (point-at-eol))))
  8559. ;; Only use search option if there is some text.
  8560. (when (string-match "\\S-" txt)
  8561. (setq cpltxt
  8562. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8563. desc "NONE")))
  8564. (setq link cpltxt))
  8565. ((org-called-interactively-p 'interactive)
  8566. (user-error "No method for storing a link from this buffer"))
  8567. (t (setq link nil)))
  8568. ;; We're done setting link and desc, clean up
  8569. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8570. (setq link (or link cpltxt)
  8571. desc (or desc cpltxt))
  8572. (cond ((equal desc "NONE") (setq desc nil))
  8573. ((string-match org-bracket-link-regexp desc)
  8574. (setq desc
  8575. (replace-regexp-in-string
  8576. org-bracket-link-regexp
  8577. (concat "\\3" (if (equal (length (match-string 0 desc))
  8578. (length desc)) "*" "")) desc))))
  8579. ;; Return the link
  8580. (if (not (and (or (org-called-interactively-p 'any)
  8581. executing-kbd-macro) link))
  8582. (or agenda-link (and link (org-make-link-string link desc)))
  8583. (push (list link desc) org-stored-links)
  8584. (message "Stored: %s" (or desc link))
  8585. (when custom-id
  8586. (setq link (concat "file:" (abbreviate-file-name
  8587. (buffer-file-name)) "::#" custom-id))
  8588. (push (list link desc) org-stored-links)))))))
  8589. (defun org-store-link-props (&rest plist)
  8590. "Store link properties, extract names and addresses."
  8591. (let (x adr)
  8592. (when (setq x (plist-get plist :from))
  8593. (setq adr (mail-extract-address-components x))
  8594. (setq plist (plist-put plist :fromname (car adr)))
  8595. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8596. (when (setq x (plist-get plist :to))
  8597. (setq adr (mail-extract-address-components x))
  8598. (setq plist (plist-put plist :toname (car adr)))
  8599. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8600. (let ((from (plist-get plist :from))
  8601. (to (plist-get plist :to)))
  8602. (when (and from to org-from-is-user-regexp)
  8603. (setq plist
  8604. (plist-put plist :fromto
  8605. (if (string-match org-from-is-user-regexp from)
  8606. (concat "to %t")
  8607. (concat "from %f"))))))
  8608. (setq org-store-link-plist plist))
  8609. (defun org-add-link-props (&rest plist)
  8610. "Add these properties to the link property list."
  8611. (let (key value)
  8612. (while plist
  8613. (setq key (pop plist) value (pop plist))
  8614. (setq org-store-link-plist
  8615. (plist-put org-store-link-plist key value)))))
  8616. (defun org-email-link-description (&optional fmt)
  8617. "Return the description part of an email link.
  8618. This takes information from `org-store-link-plist' and formats it
  8619. according to FMT (default from `org-email-link-description-format')."
  8620. (setq fmt (or fmt org-email-link-description-format))
  8621. (let* ((p org-store-link-plist)
  8622. (to (plist-get p :toaddress))
  8623. (from (plist-get p :fromaddress))
  8624. (table
  8625. (list
  8626. (cons "%c" (plist-get p :fromto))
  8627. (cons "%F" (plist-get p :from))
  8628. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8629. (cons "%T" (plist-get p :to))
  8630. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8631. (cons "%s" (plist-get p :subject))
  8632. (cons "%d" (plist-get p :date))
  8633. (cons "%m" (plist-get p :message-id)))))
  8634. (when (string-match "%c" fmt)
  8635. ;; Check if the user wrote this message
  8636. (if (and org-from-is-user-regexp from to
  8637. (save-match-data (string-match org-from-is-user-regexp from)))
  8638. (setq fmt (replace-match "to %t" t t fmt))
  8639. (setq fmt (replace-match "from %f" t t fmt))))
  8640. (org-replace-escapes fmt table)))
  8641. (defun org-make-org-heading-search-string (&optional string)
  8642. "Make search string for the current headline or STRING."
  8643. (let ((s (or string
  8644. (and (derived-mode-p 'org-mode)
  8645. (save-excursion
  8646. (org-back-to-heading t)
  8647. (org-element-property :raw-value (org-element-at-point))))))
  8648. (lines org-context-in-file-links))
  8649. (or string (setq s (concat "*" s))) ; Add * for headlines
  8650. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8651. (when (and string (integerp lines) (> lines 0))
  8652. (let ((slines (org-split-string s "\n")))
  8653. (when (< lines (length slines))
  8654. (setq s (mapconcat
  8655. 'identity
  8656. (reverse (nthcdr (- (length slines) lines)
  8657. (reverse slines))) "\n")))))
  8658. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8659. (defun org-make-link-string (link &optional description)
  8660. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8661. (unless (string-match "\\S-" link)
  8662. (error "Empty link"))
  8663. (when (and description
  8664. (stringp description)
  8665. (not (string-match "\\S-" description)))
  8666. (setq description nil))
  8667. (when (stringp description)
  8668. ;; Remove brackets from the description, they are fatal.
  8669. (while (string-match "\\[" description)
  8670. (setq description (replace-match "{" t t description)))
  8671. (while (string-match "\\]" description)
  8672. (setq description (replace-match "}" t t description))))
  8673. (when (equal link description)
  8674. ;; No description needed, it is identical
  8675. (setq description nil))
  8676. (when (and (not description)
  8677. (not (string-match (org-image-file-name-regexp) link))
  8678. (not (equal link (org-link-escape link))))
  8679. (setq description (org-extract-attributes link)))
  8680. (setq link
  8681. (cond ((string-match (org-image-file-name-regexp) link) link)
  8682. ((string-match org-link-types-re link)
  8683. (concat (match-string 1 link)
  8684. (org-link-escape (substring link (match-end 1)))))
  8685. (t (org-link-escape link))))
  8686. (concat "[[" link "]"
  8687. (if description (concat "[" description "]") "")
  8688. "]"))
  8689. (defconst org-link-escape-chars
  8690. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8691. "List of characters that should be escaped in link.
  8692. This is the list that is used for internal purposes.")
  8693. (defconst org-link-escape-chars-browser
  8694. '(?\ )
  8695. "List of escapes for characters that are problematic in links.
  8696. This is the list that is used before handing over to the browser.")
  8697. (defun org-link-escape (text &optional table merge)
  8698. "Return percent escaped representation of TEXT.
  8699. TEXT is a string with the text to escape.
  8700. Optional argument TABLE is a list with characters that should be
  8701. escaped. When nil, `org-link-escape-chars' is used.
  8702. If optional argument MERGE is set, merge TABLE into
  8703. `org-link-escape-chars'."
  8704. (cond
  8705. ((and table merge)
  8706. (mapc (lambda (defchr)
  8707. (unless (member defchr table)
  8708. (setq table (cons defchr table)))) org-link-escape-chars))
  8709. ((null table)
  8710. (setq table org-link-escape-chars)))
  8711. (mapconcat
  8712. (lambda (char)
  8713. (if (or (member char table)
  8714. (and (or (< char 32) (= char 37) (> char 126))
  8715. org-url-hexify-p))
  8716. (mapconcat (lambda (sequence-element)
  8717. (format "%%%.2X" sequence-element))
  8718. (or (encode-coding-char char 'utf-8)
  8719. (error "Unable to percent escape character: %s"
  8720. (char-to-string char))) "")
  8721. (char-to-string char))) text ""))
  8722. (defun org-link-unescape (str)
  8723. "Unhex hexified Unicode strings as returned from the JavaScript function
  8724. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8725. (unless (and (null str) (string= "" str))
  8726. (let ((pos 0) (case-fold-search t) unhexed)
  8727. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8728. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8729. (setq str (replace-match unhexed t t str))
  8730. (setq pos (+ pos (length unhexed))))))
  8731. str)
  8732. (defun org-link-unescape-compound (hex)
  8733. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8734. Note: this function also decodes single byte encodings like
  8735. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8736. (save-match-data
  8737. (let* ((bytes (cdr (split-string hex "%")))
  8738. (ret "")
  8739. (eat 0)
  8740. (sum 0))
  8741. (while bytes
  8742. (let* ((val (string-to-number (pop bytes) 16))
  8743. (shift-xor
  8744. (if (= 0 eat)
  8745. (cond
  8746. ((>= val 252) (cons 6 252))
  8747. ((>= val 248) (cons 5 248))
  8748. ((>= val 240) (cons 4 240))
  8749. ((>= val 224) (cons 3 224))
  8750. ((>= val 192) (cons 2 192))
  8751. (t (cons 0 0)))
  8752. (cons 6 128))))
  8753. (if (>= val 192) (setq eat (car shift-xor)))
  8754. (setq val (logxor val (cdr shift-xor)))
  8755. (setq sum (+ (lsh sum (car shift-xor)) val))
  8756. (if (> eat 0) (setq eat (- eat 1)))
  8757. (cond
  8758. ((= 0 eat) ;multi byte
  8759. (setq ret (concat ret (org-char-to-string sum)))
  8760. (setq sum 0))
  8761. ((not bytes) ; single byte(s)
  8762. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8763. )) ;; end (while bytes
  8764. ret )))
  8765. (defun org-link-unescape-single-byte-sequence (hex)
  8766. "Unhexify hex-encoded single byte character sequences."
  8767. (mapconcat (lambda (byte)
  8768. (char-to-string (string-to-number byte 16)))
  8769. (cdr (split-string hex "%")) ""))
  8770. (defun org-xor (a b)
  8771. "Exclusive or."
  8772. (if a (not b) b))
  8773. (defun org-fixup-message-id-for-http (s)
  8774. "Replace special characters in a message id, so it can be used in an http query."
  8775. (when (string-match "%" s)
  8776. (setq s (mapconcat (lambda (c)
  8777. (if (eq c ?%)
  8778. "%25"
  8779. (char-to-string c)))
  8780. s "")))
  8781. (while (string-match "<" s)
  8782. (setq s (replace-match "%3C" t t s)))
  8783. (while (string-match ">" s)
  8784. (setq s (replace-match "%3E" t t s)))
  8785. (while (string-match "@" s)
  8786. (setq s (replace-match "%40" t t s)))
  8787. s)
  8788. (defun org-link-prettify (link)
  8789. "Return a human-readable representation of LINK.
  8790. The car of LINK must be a raw link the cdr of LINK must be either
  8791. a link description or nil."
  8792. (let ((desc (or (cadr link) "<no description>")))
  8793. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8794. "<" (car link) ">")))
  8795. ;;;###autoload
  8796. (defun org-insert-link-global ()
  8797. "Insert a link like Org-mode does.
  8798. This command can be called in any mode to insert a link in Org-mode syntax."
  8799. (interactive)
  8800. (org-load-modules-maybe)
  8801. (org-run-like-in-org-mode 'org-insert-link))
  8802. (defun org-insert-all-links (&optional keep)
  8803. "Insert all links in `org-stored-links'."
  8804. (interactive "P")
  8805. (let ((links (copy-sequence org-stored-links)) l)
  8806. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8807. (insert "- ")
  8808. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8809. (insert "\n"))))
  8810. (defun org-link-fontify-links-to-this-file ()
  8811. "Fontify links to the current file in `org-stored-links'."
  8812. (let ((f (buffer-file-name)) a b)
  8813. (setq a (mapcar (lambda(l)
  8814. (let ((ll (car l)))
  8815. (when (and (string-match "^file:\\(.+\\)::" ll)
  8816. (equal f (expand-file-name (match-string 1 ll))))
  8817. ll)))
  8818. org-stored-links))
  8819. (when (featurep 'org-id)
  8820. (setq b (mapcar (lambda(l)
  8821. (let ((ll (car l)))
  8822. (when (and (string-match "^id:\\(.+\\)$" ll)
  8823. (equal f (expand-file-name
  8824. (or (org-id-find-id-file
  8825. (match-string 1 ll)) ""))))
  8826. ll)))
  8827. org-stored-links)))
  8828. (mapcar (lambda(l)
  8829. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8830. (delq nil (append a b)))))
  8831. (defvar org-link-links-in-this-file nil)
  8832. (defun org-insert-link (&optional complete-file link-location default-description)
  8833. "Insert a link. At the prompt, enter the link.
  8834. Completion can be used to insert any of the link protocol prefixes like
  8835. http or ftp in use.
  8836. The history can be used to select a link previously stored with
  8837. `org-store-link'. When the empty string is entered (i.e. if you just
  8838. press RET at the prompt), the link defaults to the most recently
  8839. stored link. As SPC triggers completion in the minibuffer, you need to
  8840. use M-SPC or C-q SPC to force the insertion of a space character.
  8841. You will also be prompted for a description, and if one is given, it will
  8842. be displayed in the buffer instead of the link.
  8843. If there is already a link at point, this command will allow you to edit link
  8844. and description parts.
  8845. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8846. be selected using completion. The path to the file will be relative to the
  8847. current directory if the file is in the current directory or a subdirectory.
  8848. Otherwise, the link will be the absolute path as completed in the minibuffer
  8849. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8850. option `org-link-file-path-type'.
  8851. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8852. the current directory or below.
  8853. With three \\[universal-argument] prefixes, negate the meaning of
  8854. `org-keep-stored-link-after-insertion'.
  8855. If `org-make-link-description-function' is non-nil, this function will be
  8856. called with the link target, and the result will be the default
  8857. link description.
  8858. If the LINK-LOCATION parameter is non-nil, this value will be
  8859. used as the link location instead of reading one interactively.
  8860. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8861. be used as the default description."
  8862. (interactive "P")
  8863. (let* ((wcf (current-window-configuration))
  8864. (origbuf (current-buffer))
  8865. (region (if (org-region-active-p)
  8866. (buffer-substring (region-beginning) (region-end))))
  8867. (remove (and region (list (region-beginning) (region-end))))
  8868. (desc region)
  8869. tmphist ; byte-compile incorrectly complains about this
  8870. (link link-location)
  8871. (abbrevs org-link-abbrev-alist-local)
  8872. entry file all-prefixes auto-desc)
  8873. (cond
  8874. (link-location) ; specified by arg, just use it.
  8875. ((org-in-regexp org-bracket-link-regexp 1)
  8876. ;; We do have a link at point, and we are going to edit it.
  8877. (setq remove (list (match-beginning 0) (match-end 0)))
  8878. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8879. (setq link (read-string "Link: "
  8880. (org-link-unescape
  8881. (org-match-string-no-properties 1)))))
  8882. ((or (org-in-regexp org-angle-link-re)
  8883. (org-in-regexp org-plain-link-re))
  8884. ;; Convert to bracket link
  8885. (setq remove (list (match-beginning 0) (match-end 0))
  8886. link (read-string "Link: "
  8887. (org-remove-angle-brackets (match-string 0)))))
  8888. ((member complete-file '((4) (16)))
  8889. ;; Completing read for file names.
  8890. (setq link (org-file-complete-link complete-file)))
  8891. (t
  8892. ;; Read link, with completion for stored links.
  8893. (org-link-fontify-links-to-this-file)
  8894. (org-switch-to-buffer-other-window "*Org Links*")
  8895. (with-current-buffer "*Org Links*"
  8896. (erase-buffer)
  8897. (insert "Insert a link.
  8898. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8899. (when org-stored-links
  8900. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8901. (insert (mapconcat 'org-link-prettify
  8902. (reverse org-stored-links) "\n")))
  8903. (goto-char (point-min)))
  8904. (let ((cw (selected-window)))
  8905. (select-window (get-buffer-window "*Org Links*" 'visible))
  8906. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8907. (unless (pos-visible-in-window-p (point-max))
  8908. (org-fit-window-to-buffer))
  8909. (and (window-live-p cw) (select-window cw)))
  8910. ;; Fake a link history, containing the stored links.
  8911. (setq tmphist (append (mapcar 'car org-stored-links)
  8912. org-insert-link-history))
  8913. (setq all-prefixes (append (mapcar 'car abbrevs)
  8914. (mapcar 'car org-link-abbrev-alist)
  8915. org-link-types))
  8916. (unwind-protect
  8917. (progn
  8918. (setq link
  8919. (org-completing-read
  8920. "Link: "
  8921. (append
  8922. (mapcar (lambda (x) (concat x ":"))
  8923. all-prefixes)
  8924. (mapcar 'car org-stored-links))
  8925. nil nil nil
  8926. 'tmphist
  8927. (caar org-stored-links)))
  8928. (if (not (string-match "\\S-" link))
  8929. (error "No link selected"))
  8930. (mapc (lambda(l)
  8931. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8932. org-stored-links)
  8933. (if (or (member link all-prefixes)
  8934. (and (equal ":" (substring link -1))
  8935. (member (substring link 0 -1) all-prefixes)
  8936. (setq link (substring link 0 -1))))
  8937. (setq link (with-current-buffer origbuf
  8938. (org-link-try-special-completion link)))))
  8939. (set-window-configuration wcf)
  8940. (kill-buffer "*Org Links*"))
  8941. (setq entry (assoc link org-stored-links))
  8942. (or entry (push link org-insert-link-history))
  8943. (setq desc (or desc (nth 1 entry)))))
  8944. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8945. (not org-keep-stored-link-after-insertion))
  8946. (setq org-stored-links (delq (assoc link org-stored-links)
  8947. org-stored-links)))
  8948. (if (string-match org-plain-link-re link)
  8949. ;; URL-like link, normalize the use of angular brackets.
  8950. (setq link (org-remove-angle-brackets link)))
  8951. ;; Check if we are linking to the current file with a search
  8952. ;; option If yes, simplify the link by using only the search
  8953. ;; option.
  8954. (when (and buffer-file-name
  8955. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8956. (let* ((path (match-string 1 link))
  8957. (case-fold-search nil)
  8958. (search (match-string 2 link)))
  8959. (save-match-data
  8960. (if (equal (file-truename buffer-file-name) (file-truename path))
  8961. ;; We are linking to this same file, with a search option
  8962. (setq link search)))))
  8963. ;; Check if we can/should use a relative path. If yes, simplify the link
  8964. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8965. (let* ((type (match-string 1 link))
  8966. (path (match-string 2 link))
  8967. (origpath path)
  8968. (case-fold-search nil))
  8969. (cond
  8970. ((or (eq org-link-file-path-type 'absolute)
  8971. (equal complete-file '(16)))
  8972. (setq path (abbreviate-file-name (expand-file-name path))))
  8973. ((eq org-link-file-path-type 'noabbrev)
  8974. (setq path (expand-file-name path)))
  8975. ((eq org-link-file-path-type 'relative)
  8976. (setq path (file-relative-name path)))
  8977. (t
  8978. (save-match-data
  8979. (if (string-match (concat "^" (regexp-quote
  8980. (expand-file-name
  8981. (file-name-as-directory
  8982. default-directory))))
  8983. (expand-file-name path))
  8984. ;; We are linking a file with relative path name.
  8985. (setq path (substring (expand-file-name path)
  8986. (match-end 0)))
  8987. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8988. (setq link (concat type path))
  8989. (if (equal desc origpath)
  8990. (setq desc path))))
  8991. (if org-make-link-description-function
  8992. (setq desc
  8993. (or (condition-case nil
  8994. (funcall org-make-link-description-function link desc)
  8995. (error (progn (message "Can't get link description from `%s'"
  8996. (symbol-name org-make-link-description-function))
  8997. (sit-for 2) nil)))
  8998. (read-string "Description: " default-description)))
  8999. (if default-description (setq desc default-description)
  9000. (setq desc (or (and auto-desc desc)
  9001. (read-string "Description: " desc)))))
  9002. (unless (string-match "\\S-" desc) (setq desc nil))
  9003. (if remove (apply 'delete-region remove))
  9004. (insert (org-make-link-string link desc))))
  9005. (defun org-link-try-special-completion (type)
  9006. "If there is completion support for link type TYPE, offer it."
  9007. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9008. (if (functionp fun)
  9009. (funcall fun)
  9010. (read-string "Link (no completion support): " (concat type ":")))))
  9011. (defun org-file-complete-link (&optional arg)
  9012. "Create a file link using completion."
  9013. (let (file link)
  9014. (setq file (org-iread-file-name "File: "))
  9015. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9016. (pwd1 (file-name-as-directory (abbreviate-file-name
  9017. (expand-file-name ".")))))
  9018. (cond
  9019. ((equal arg '(16))
  9020. (setq link (concat
  9021. "file:"
  9022. (abbreviate-file-name (expand-file-name file)))))
  9023. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9024. (setq link (concat "file:" (match-string 1 file))))
  9025. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9026. (expand-file-name file))
  9027. (setq link (concat
  9028. "file:" (match-string 1 (expand-file-name file)))))
  9029. (t (setq link (concat "file:" file)))))
  9030. link))
  9031. (defun org-iread-file-name (&rest args)
  9032. "Read-file-name using `ido-mode' speedup if available.
  9033. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9034. See `read-file-name' for a description of parameters."
  9035. (org-without-partial-completion
  9036. (if (and org-completion-use-ido
  9037. (fboundp 'ido-read-file-name)
  9038. (boundp 'ido-mode) ido-mode
  9039. (listp (second args)))
  9040. (let ((ido-enter-matching-directory nil))
  9041. (apply 'ido-read-file-name args))
  9042. (apply 'read-file-name args))))
  9043. (defun org-completing-read (&rest args)
  9044. "Completing-read with SPACE being a normal character."
  9045. (let ((enable-recursive-minibuffers t)
  9046. (minibuffer-local-completion-map
  9047. (copy-keymap minibuffer-local-completion-map)))
  9048. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9049. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9050. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9051. (apply 'org-icompleting-read args)))
  9052. (defun org-completing-read-no-i (&rest args)
  9053. (let (org-completion-use-ido org-completion-use-iswitchb)
  9054. (apply 'org-completing-read args)))
  9055. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9056. "Use iswitch as a completing-read replacement to choose from choices.
  9057. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9058. from."
  9059. (let* ((iswitchb-use-virtual-buffers nil)
  9060. (iswitchb-make-buflist-hook
  9061. (lambda ()
  9062. (setq iswitchb-temp-buflist choices))))
  9063. (iswitchb-read-buffer prompt)))
  9064. (defun org-icompleting-read (&rest args)
  9065. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9066. (org-without-partial-completion
  9067. (if (and org-completion-use-ido
  9068. (fboundp 'ido-completing-read)
  9069. (boundp 'ido-mode) ido-mode
  9070. (listp (second args)))
  9071. (let ((ido-enter-matching-directory nil))
  9072. (apply 'ido-completing-read (concat (car args))
  9073. (if (consp (car (nth 1 args)))
  9074. (mapcar 'car (nth 1 args))
  9075. (nth 1 args))
  9076. (cddr args)))
  9077. (if (and org-completion-use-iswitchb
  9078. (boundp 'iswitchb-mode) iswitchb-mode
  9079. (listp (second args)))
  9080. (apply 'org-iswitchb-completing-read (concat (car args))
  9081. (if (consp (car (nth 1 args)))
  9082. (mapcar 'car (nth 1 args))
  9083. (nth 1 args))
  9084. (cddr args))
  9085. (apply 'completing-read args)))))
  9086. (defun org-extract-attributes (s)
  9087. "Extract the attributes cookie from a string and set as text property."
  9088. (let (a attr (start 0) key value)
  9089. (save-match-data
  9090. (when (string-match "{{\\([^}]+\\)}}$" s)
  9091. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9092. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9093. (setq key (match-string 1 a) value (match-string 2 a)
  9094. start (match-end 0)
  9095. attr (plist-put attr (intern key) value))))
  9096. (org-add-props s nil 'org-attr attr))
  9097. s))
  9098. (defun org-extract-attributes-from-string (tag)
  9099. (let (key value attr)
  9100. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  9101. (setq key (match-string 1 tag) value (match-string 2 tag)
  9102. tag (replace-match "" t t tag)
  9103. attr (plist-put attr (intern key) value)))
  9104. (cons tag attr)))
  9105. (defun org-attributes-to-string (plist)
  9106. "Format a property list into an HTML attribute list."
  9107. (let ((s "") key value)
  9108. (while plist
  9109. (setq key (pop plist) value (pop plist))
  9110. (and value
  9111. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  9112. s))
  9113. ;;; Opening/following a link
  9114. (defvar org-link-search-failed nil)
  9115. (defvar org-open-link-functions nil
  9116. "Hook for functions finding a plain text link.
  9117. These functions must take a single argument, the link content.
  9118. They will be called for links that look like [[link text][description]]
  9119. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9120. like a filename (e.g. \"./blue.png\").
  9121. These functions will be called *before* Org attempts to resolve the
  9122. link by doing text searches in the current buffer - so if you want a
  9123. link \"[[target]]\" to still find \"<<target>>\", your function should
  9124. handle this as a special case.
  9125. When the function does handle the link, it must return a non-nil value.
  9126. If it decides that it is not responsible for this link, it must return
  9127. nil to indicate that that Org-mode can continue with other options
  9128. like exact and fuzzy text search.")
  9129. (defun org-next-link (&optional search-backward)
  9130. "Move forward to the next link.
  9131. If the link is in hidden text, expose it."
  9132. (interactive "P")
  9133. (when (and org-link-search-failed (eq this-command last-command))
  9134. (goto-char (point-min))
  9135. (message "Link search wrapped back to beginning of buffer"))
  9136. (setq org-link-search-failed nil)
  9137. (let* ((pos (point))
  9138. (ct (org-context))
  9139. (a (assoc :link ct))
  9140. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9141. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9142. ((looking-at org-any-link-re)
  9143. ;; Don't stay stuck at link without an org-link face
  9144. (forward-char (if search-backward -1 1))))
  9145. (if (funcall srch-fun org-any-link-re nil t)
  9146. (progn
  9147. (goto-char (match-beginning 0))
  9148. (if (outline-invisible-p) (org-show-context)))
  9149. (goto-char pos)
  9150. (setq org-link-search-failed t)
  9151. (message "No further link found"))))
  9152. (defun org-previous-link ()
  9153. "Move backward to the previous link.
  9154. If the link is in hidden text, expose it."
  9155. (interactive)
  9156. (funcall 'org-next-link t))
  9157. (defun org-translate-link (s)
  9158. "Translate a link string if a translation function has been defined."
  9159. (if (and org-link-translation-function
  9160. (fboundp org-link-translation-function)
  9161. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9162. (progn
  9163. (setq s (funcall org-link-translation-function
  9164. (match-string 1 s) (match-string 2 s)))
  9165. (concat (car s) ":" (cdr s)))
  9166. s))
  9167. (defun org-translate-link-from-planner (type path)
  9168. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9169. This is still an experimental function, your mileage may vary."
  9170. (cond
  9171. ((member type '("http" "https" "news" "ftp"))
  9172. ;; standard Internet links are the same.
  9173. nil)
  9174. ((and (equal type "irc") (string-match "^//" path))
  9175. ;; Planner has two / at the beginning of an irc link, we have 1.
  9176. ;; We should have zero, actually....
  9177. (setq path (substring path 1)))
  9178. ((and (equal type "lisp") (string-match "^/" path))
  9179. ;; Planner has a slash, we do not.
  9180. (setq type "elisp" path (substring path 1)))
  9181. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9182. ;; A typical message link. Planner has the id after the final slash,
  9183. ;; we separate it with a hash mark
  9184. (setq path (concat (match-string 1 path) "#"
  9185. (org-remove-angle-brackets (match-string 2 path)))))
  9186. )
  9187. (cons type path))
  9188. (defun org-find-file-at-mouse (ev)
  9189. "Open file link or URL at mouse."
  9190. (interactive "e")
  9191. (mouse-set-point ev)
  9192. (org-open-at-point 'in-emacs))
  9193. (defun org-open-at-mouse (ev)
  9194. "Open file link or URL at mouse.
  9195. See the docstring of `org-open-file' for details."
  9196. (interactive "e")
  9197. (mouse-set-point ev)
  9198. (if (eq major-mode 'org-agenda-mode)
  9199. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9200. (org-open-at-point))
  9201. (defvar org-window-config-before-follow-link nil
  9202. "The window configuration before following a link.
  9203. This is saved in case the need arises to restore it.")
  9204. (defvar org-open-link-marker (make-marker)
  9205. "Marker pointing to the location where `org-open-at-point; was called.")
  9206. ;;;###autoload
  9207. (defun org-open-at-point-global ()
  9208. "Follow a link like Org-mode does.
  9209. This command can be called in any mode to follow a link that has
  9210. Org-mode syntax."
  9211. (interactive)
  9212. (org-run-like-in-org-mode 'org-open-at-point))
  9213. ;;;###autoload
  9214. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9215. "Open a link in the string S, as if it was in Org-mode."
  9216. (interactive "sLink: \nP")
  9217. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9218. (with-temp-buffer
  9219. (let ((org-inhibit-startup (not reference-buffer)))
  9220. (org-mode)
  9221. (insert s)
  9222. (goto-char (point-min))
  9223. (when reference-buffer
  9224. (setq org-link-abbrev-alist-local
  9225. (with-current-buffer reference-buffer
  9226. org-link-abbrev-alist-local)))
  9227. (org-open-at-point arg reference-buffer)))))
  9228. (defvar org-open-at-point-functions nil
  9229. "Hook that is run when following a link at point.
  9230. Functions in this hook must return t if they identify and follow
  9231. a link at point. If they don't find anything interesting at point,
  9232. they must return nil.")
  9233. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9234. (defun org-open-at-point (&optional arg reference-buffer)
  9235. "Open link at or after point.
  9236. If there is no link at point, this function will search forward up to
  9237. the end of the current line.
  9238. Normally, files will be opened by an appropriate application. If the
  9239. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9240. With a double prefix argument, try to open outside of Emacs, in the
  9241. application the system uses for this file type."
  9242. (interactive "P")
  9243. ;; if in a code block, then open the block's results
  9244. (unless (call-interactively #'org-babel-open-src-block-result)
  9245. (org-load-modules-maybe)
  9246. (move-marker org-open-link-marker (point))
  9247. (setq org-window-config-before-follow-link (current-window-configuration))
  9248. (org-remove-occur-highlights nil nil t)
  9249. (cond
  9250. ((and (org-at-heading-p)
  9251. (not (org-at-timestamp-p t))
  9252. (not (org-in-regexp
  9253. (concat org-plain-link-re "\\|"
  9254. org-bracket-link-regexp "\\|"
  9255. org-angle-link-re "\\|"
  9256. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9257. (not (get-text-property (point) 'org-linked-text)))
  9258. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9259. (lk0 (car lkall))
  9260. (lk (if (stringp lk0) (list lk0) lk0))
  9261. (lkend (cdr lkall)))
  9262. (mapcar (lambda(l)
  9263. (search-forward l nil lkend)
  9264. (goto-char (match-beginning 0))
  9265. (org-open-at-point))
  9266. lk))
  9267. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9268. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9269. ((and (org-at-timestamp-p t)
  9270. (not (org-in-regexp org-bracket-link-regexp)))
  9271. (org-follow-timestamp-link))
  9272. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9273. (not (org-in-regexp org-any-link-re)))
  9274. (org-footnote-action))
  9275. (t
  9276. (let (type path link line search (pos (point)))
  9277. (catch 'match
  9278. (save-excursion
  9279. (skip-chars-forward "^]\n\r")
  9280. (when (org-in-regexp org-bracket-link-regexp 1)
  9281. (setq link (org-extract-attributes
  9282. (org-link-unescape (org-match-string-no-properties 1))))
  9283. (while (string-match " *\n *" link)
  9284. (setq link (replace-match " " t t link)))
  9285. (setq link (org-link-expand-abbrev link))
  9286. (cond
  9287. ((or (file-name-absolute-p link)
  9288. (string-match "^\\.\\.?/" link))
  9289. (setq type "file" path link))
  9290. ((string-match org-link-re-with-space3 link)
  9291. (setq type (match-string 1 link) path (match-string 2 link)))
  9292. ((string-match "^help:+\\(.+\\)" link)
  9293. (setq type "help" path (match-string 1 link)))
  9294. (t (setq type "thisfile" path link)))
  9295. (throw 'match t)))
  9296. (when (get-text-property (point) 'org-linked-text)
  9297. (setq type "thisfile"
  9298. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9299. (1+ (point)) (point))
  9300. path (buffer-substring
  9301. (or (previous-single-property-change pos 'org-linked-text)
  9302. (point-min))
  9303. (or (next-single-property-change pos 'org-linked-text)
  9304. (point-max))))
  9305. (throw 'match t))
  9306. (save-excursion
  9307. (when (or (org-in-regexp org-angle-link-re)
  9308. (let ((match (org-in-regexp org-plain-link-re)))
  9309. ;; Check a plain link is not within a bracket link
  9310. (and match
  9311. (save-excursion
  9312. (progn
  9313. (goto-char (car match))
  9314. (not (org-in-regexp org-bracket-link-regexp))))))
  9315. (let ((line_ending (save-excursion (end-of-line) (point))))
  9316. ;; We are in a line before a plain or bracket link
  9317. (or (re-search-forward org-plain-link-re line_ending t)
  9318. (re-search-forward org-bracket-link-regexp line_ending t))))
  9319. (setq type (match-string 1)
  9320. path (org-link-unescape (match-string 2)))
  9321. (throw 'match t)))
  9322. (save-excursion
  9323. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9324. (setq type "tags"
  9325. path (match-string 1))
  9326. (while (string-match ":" path)
  9327. (setq path (replace-match "+" t t path)))
  9328. (throw 'match t)))
  9329. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9330. (setq type "tree-match"
  9331. path (match-string 1))
  9332. (throw 'match t)))
  9333. (unless path
  9334. (user-error "No link found"))
  9335. ;; switch back to reference buffer
  9336. ;; needed when if called in a temporary buffer through
  9337. ;; org-open-link-from-string
  9338. (with-current-buffer (or reference-buffer (current-buffer))
  9339. ;; Remove any trailing spaces in path
  9340. (if (string-match " +\\'" path)
  9341. (setq path (replace-match "" t t path)))
  9342. (if (and org-link-translation-function
  9343. (fboundp org-link-translation-function))
  9344. ;; Check if we need to translate the link
  9345. (let ((tmp (funcall org-link-translation-function type path)))
  9346. (setq type (car tmp) path (cdr tmp))))
  9347. (cond
  9348. ((assoc type org-link-protocols)
  9349. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9350. ((equal type "help")
  9351. (let ((f-or-v (intern path)))
  9352. (cond ((fboundp f-or-v)
  9353. (describe-function f-or-v))
  9354. ((boundp f-or-v)
  9355. (describe-variable f-or-v))
  9356. (t (error "Not a known function or variable")))))
  9357. ((equal type "mailto")
  9358. (let ((cmd (car org-link-mailto-program))
  9359. (args (cdr org-link-mailto-program)) args1
  9360. (address path) (subject "") a)
  9361. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9362. (setq address (match-string 1 path)
  9363. subject (org-link-escape (match-string 2 path))))
  9364. (while args
  9365. (cond
  9366. ((not (stringp (car args))) (push (pop args) args1))
  9367. (t (setq a (pop args))
  9368. (if (string-match "%a" a)
  9369. (setq a (replace-match address t t a)))
  9370. (if (string-match "%s" a)
  9371. (setq a (replace-match subject t t a)))
  9372. (push a args1))))
  9373. (apply cmd (nreverse args1))))
  9374. ((member type '("http" "https" "ftp" "news"))
  9375. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9376. (org-link-escape
  9377. path org-link-escape-chars-browser)
  9378. path))))
  9379. ((string= type "doi")
  9380. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9381. (org-link-escape
  9382. path org-link-escape-chars-browser)
  9383. path))))
  9384. ((member type '("message"))
  9385. (browse-url (concat type ":" path)))
  9386. ((string= type "tags")
  9387. (org-tags-view arg path))
  9388. ((string= type "tree-match")
  9389. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9390. ((string= type "file")
  9391. (if (string-match "::\\([0-9]+\\)\\'" path)
  9392. (setq line (string-to-number (match-string 1 path))
  9393. path (substring path 0 (match-beginning 0)))
  9394. (if (string-match "::\\(.+\\)\\'" path)
  9395. (setq search (match-string 1 path)
  9396. path (substring path 0 (match-beginning 0)))))
  9397. (if (string-match "[*?{]" (file-name-nondirectory path))
  9398. (dired path)
  9399. (org-open-file path arg line search)))
  9400. ((string= type "shell")
  9401. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9402. (cmd path))
  9403. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9404. (string-match org-confirm-shell-link-not-regexp cmd))
  9405. (not org-confirm-shell-link-function)
  9406. (funcall org-confirm-shell-link-function
  9407. (format "Execute \"%s\" in shell? "
  9408. (org-add-props cmd nil
  9409. 'face 'org-warning))))
  9410. (progn
  9411. (message "Executing %s" cmd)
  9412. (shell-command cmd buf)
  9413. (if (featurep 'midnight)
  9414. (setq clean-buffer-list-kill-buffer-names
  9415. (cons buf clean-buffer-list-kill-buffer-names))))
  9416. (error "Abort"))))
  9417. ((string= type "elisp")
  9418. (let ((cmd path))
  9419. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9420. (string-match org-confirm-elisp-link-not-regexp cmd))
  9421. (not org-confirm-elisp-link-function)
  9422. (funcall org-confirm-elisp-link-function
  9423. (format "Execute \"%s\" as elisp? "
  9424. (org-add-props cmd nil
  9425. 'face 'org-warning))))
  9426. (message "%s => %s" cmd
  9427. (if (equal (string-to-char cmd) ?\()
  9428. (eval (read cmd))
  9429. (call-interactively (read cmd))))
  9430. (error "Abort"))))
  9431. ((and (string= type "thisfile")
  9432. (run-hook-with-args-until-success
  9433. 'org-open-link-functions path)))
  9434. ((string= type "thisfile")
  9435. (if arg
  9436. (switch-to-buffer-other-window
  9437. (org-get-buffer-for-internal-link (current-buffer)))
  9438. (org-mark-ring-push))
  9439. (let ((cmd `(org-link-search
  9440. ,path
  9441. ,(cond ((equal arg '(4)) ''occur)
  9442. ((equal arg '(16)) ''org-occur))
  9443. ,pos)))
  9444. (condition-case nil (let ((org-link-search-inhibit-query t))
  9445. (eval cmd))
  9446. (error (progn (widen) (eval cmd))))))
  9447. (t (browse-url-at-point)))))))
  9448. (move-marker org-open-link-marker nil)
  9449. (run-hook-with-args 'org-follow-link-hook)))
  9450. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9451. "Offer links in the current entry and return the selected link.
  9452. If there is only one link, return it.
  9453. If NTH is an integer, return the NTH link found.
  9454. If ZERO is a string, check also this string for a link, and if
  9455. there is one, return it."
  9456. (with-current-buffer buffer
  9457. (save-excursion
  9458. (save-restriction
  9459. (widen)
  9460. (goto-char marker)
  9461. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9462. "\\(" org-angle-link-re "\\)\\|"
  9463. "\\(" org-plain-link-re "\\)"))
  9464. (cnt ?0)
  9465. (in-emacs (if (integerp nth) nil nth))
  9466. have-zero end links link c)
  9467. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9468. (push (match-string 0 zero) links)
  9469. (setq cnt (1- cnt) have-zero t))
  9470. (save-excursion
  9471. (org-back-to-heading t)
  9472. (setq end (save-excursion (outline-next-heading) (point)))
  9473. (while (re-search-forward re end t)
  9474. (push (match-string 0) links))
  9475. (setq links (org-uniquify (reverse links))))
  9476. (cond
  9477. ((null links)
  9478. (message "No links"))
  9479. ((equal (length links) 1)
  9480. (setq link (car links)))
  9481. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9482. (setq link (nth (if have-zero nth (1- nth)) links)))
  9483. (t ; we have to select a link
  9484. (save-excursion
  9485. (save-window-excursion
  9486. (delete-other-windows)
  9487. (with-output-to-temp-buffer "*Select Link*"
  9488. (mapc (lambda (l)
  9489. (if (not (string-match org-bracket-link-regexp l))
  9490. (princ (format "[%c] %s\n" (incf cnt)
  9491. (org-remove-angle-brackets l)))
  9492. (if (match-end 3)
  9493. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9494. (match-string 3 l) (match-string 1 l)))
  9495. (princ (format "[%c] %s\n" (incf cnt)
  9496. (match-string 1 l))))))
  9497. links))
  9498. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9499. (message "Select link to open, RET to open all:")
  9500. (setq c (read-char-exclusive))
  9501. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9502. (when (equal c ?q) (error "Abort"))
  9503. (if (equal c ?\C-m)
  9504. (setq link links)
  9505. (setq nth (- c ?0))
  9506. (if have-zero (setq nth (1+ nth)))
  9507. (unless (and (integerp nth) (>= (length links) nth))
  9508. (error "Invalid link selection"))
  9509. (setq link (nth (1- nth) links)))))
  9510. (cons link end))))))
  9511. ;; Add special file links that specify the way of opening
  9512. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9513. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9514. (defun org-open-file-with-system (path)
  9515. "Open file at PATH using the system way of opening it."
  9516. (org-open-file path 'system))
  9517. (defun org-open-file-with-emacs (path)
  9518. "Open file at PATH in Emacs."
  9519. (org-open-file path 'emacs))
  9520. ;;; File search
  9521. (defvar org-create-file-search-functions nil
  9522. "List of functions to construct the right search string for a file link.
  9523. These functions are called in turn with point at the location to
  9524. which the link should point.
  9525. A function in the hook should first test if it would like to
  9526. handle this file type, for example by checking the `major-mode'
  9527. or the file extension. If it decides not to handle this file, it
  9528. should just return nil to give other functions a chance. If it
  9529. does handle the file, it must return the search string to be used
  9530. when following the link. The search string will be part of the
  9531. file link, given after a double colon, and `org-open-at-point'
  9532. will automatically search for it. If special measures must be
  9533. taken to make the search successful, another function should be
  9534. added to the companion hook `org-execute-file-search-functions',
  9535. which see.
  9536. A function in this hook may also use `setq' to set the variable
  9537. `description' to provide a suggestion for the descriptive text to
  9538. be used for this link when it gets inserted into an Org-mode
  9539. buffer with \\[org-insert-link].")
  9540. (defvar org-execute-file-search-functions nil
  9541. "List of functions to execute a file search triggered by a link.
  9542. Functions added to this hook must accept a single argument, the
  9543. search string that was part of the file link, the part after the
  9544. double colon. The function must first check if it would like to
  9545. handle this search, for example by checking the `major-mode' or
  9546. the file extension. If it decides not to handle this search, it
  9547. should just return nil to give other functions a chance. If it
  9548. does handle the search, it must return a non-nil value to keep
  9549. other functions from trying.
  9550. Each function can access the current prefix argument through the
  9551. variable `current-prefix-arg'. Note that a single prefix is used
  9552. to force opening a link in Emacs, so it may be good to only use a
  9553. numeric or double prefix to guide the search function.
  9554. In case this is needed, a function in this hook can also restore
  9555. the window configuration before `org-open-at-point' was called using:
  9556. (set-window-configuration org-window-config-before-follow-link)")
  9557. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9558. (defun org-link-search (s &optional type avoid-pos stealth)
  9559. "Search for a link search option.
  9560. If S is surrounded by forward slashes, it is interpreted as a
  9561. regular expression. In org-mode files, this will create an `org-occur'
  9562. sparse tree. In ordinary files, `occur' will be used to list matches.
  9563. If the current buffer is in `dired-mode', grep will be used to search
  9564. in all files. If AVOID-POS is given, ignore matches near that position.
  9565. When optional argument STEALTH is non-nil, do not modify
  9566. visibility around point, thus ignoring
  9567. `org-show-hierarchy-above', `org-show-following-heading' and
  9568. `org-show-siblings' variables."
  9569. (let ((case-fold-search t)
  9570. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9571. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9572. (append '(("") (" ") ("\t") ("\n"))
  9573. org-emphasis-alist)
  9574. "\\|") "\\)"))
  9575. (pos (point))
  9576. (pre nil) (post nil)
  9577. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9578. (cond
  9579. ;; First check if there are any special search functions
  9580. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9581. ;; Now try the builtin stuff
  9582. ((and (equal (string-to-char s0) ?#)
  9583. (> (length s0) 1)
  9584. (save-excursion
  9585. (goto-char (point-min))
  9586. (and
  9587. (re-search-forward
  9588. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9589. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9590. (setq type 'dedicated
  9591. pos (match-beginning 0))))
  9592. ;; There is an exact target for this
  9593. (goto-char pos)
  9594. (org-back-to-heading t)))
  9595. ((save-excursion
  9596. (goto-char (point-min))
  9597. (and
  9598. (re-search-forward
  9599. (concat "<<" (regexp-quote s0) ">>") nil t)
  9600. (setq type 'dedicated
  9601. pos (match-beginning 0))))
  9602. ;; There is an exact target for this
  9603. (goto-char pos))
  9604. ((save-excursion
  9605. (goto-char (point-min))
  9606. (and
  9607. (re-search-forward
  9608. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9609. (setq type 'dedicated pos (match-beginning 0))))
  9610. ;; Found an invisible target.
  9611. (goto-char pos))
  9612. ((save-excursion
  9613. (goto-char (point-min))
  9614. (and
  9615. (re-search-forward
  9616. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9617. (setq type 'dedicated pos (match-beginning 0))))
  9618. ;; Found an element with a matching #+name affiliated keyword.
  9619. (goto-char pos))
  9620. ((and (string-match "^(\\(.*\\))$" s0)
  9621. (save-excursion
  9622. (goto-char (point-min))
  9623. (and
  9624. (re-search-forward
  9625. (concat "[^[]" (regexp-quote
  9626. (format org-coderef-label-format
  9627. (match-string 1 s0))))
  9628. nil t)
  9629. (setq type 'dedicated
  9630. pos (1+ (match-beginning 0))))))
  9631. ;; There is a coderef target for this
  9632. (goto-char pos))
  9633. ((string-match "^/\\(.*\\)/$" s)
  9634. ;; A regular expression
  9635. (cond
  9636. ((derived-mode-p 'org-mode)
  9637. (org-occur (match-string 1 s)))
  9638. (t (org-do-occur (match-string 1 s)))))
  9639. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9640. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9641. (goto-char (point-min))
  9642. (cond
  9643. ((let (case-fold-search)
  9644. (re-search-forward (format org-complex-heading-regexp-format
  9645. (regexp-quote s))
  9646. nil t))
  9647. ;; OK, found a match
  9648. (setq type 'dedicated)
  9649. (goto-char (match-beginning 0)))
  9650. ((and (not org-link-search-inhibit-query)
  9651. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9652. (y-or-n-p "No match - create this as a new heading? "))
  9653. (goto-char (point-max))
  9654. (or (bolp) (newline))
  9655. (insert "* " s "\n")
  9656. (beginning-of-line 0))
  9657. (t
  9658. (goto-char pos)
  9659. (error "No match"))))
  9660. (t
  9661. ;; A normal search string
  9662. (when (equal (string-to-char s) ?*)
  9663. ;; Anchor on headlines, post may include tags.
  9664. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9665. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9666. s (substring s 1)))
  9667. (remove-text-properties
  9668. 0 (length s)
  9669. '(face nil mouse-face nil keymap nil fontified nil) s)
  9670. ;; Make a series of regular expressions to find a match
  9671. (setq words (org-split-string s "[ \n\r\t]+")
  9672. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9673. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9674. "\\)" markers)
  9675. re2a_ (concat "\\(" (mapconcat 'downcase words
  9676. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9677. re2a (concat "[ \t\r\n]" re2a_)
  9678. re4_ (concat "\\(" (mapconcat 'downcase words
  9679. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9680. re4 (concat "[^a-zA-Z_]" re4_)
  9681. re1 (concat pre re2 post)
  9682. re3 (concat pre (if pre re4_ re4) post)
  9683. re5 (concat pre ".*" re4)
  9684. re2 (concat pre re2)
  9685. re2a (concat pre (if pre re2a_ re2a))
  9686. re4 (concat pre (if pre re4_ re4))
  9687. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9688. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9689. re5 "\\)"
  9690. ))
  9691. (cond
  9692. ((eq type 'org-occur) (org-occur reall))
  9693. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9694. (t (goto-char (point-min))
  9695. (setq type 'fuzzy)
  9696. (if (or (and (org-search-not-self 1 re0 nil t)
  9697. (setq type 'dedicated))
  9698. (org-search-not-self 1 re1 nil t)
  9699. (org-search-not-self 1 re2 nil t)
  9700. (org-search-not-self 1 re2a nil t)
  9701. (org-search-not-self 1 re3 nil t)
  9702. (org-search-not-self 1 re4 nil t)
  9703. (org-search-not-self 1 re5 nil t))
  9704. (goto-char (match-beginning 1))
  9705. (goto-char pos)
  9706. (error "No match"))))))
  9707. (and (derived-mode-p 'org-mode)
  9708. (not stealth)
  9709. (org-show-context 'link-search))
  9710. type))
  9711. (defun org-search-not-self (group &rest args)
  9712. "Execute `re-search-forward', but only accept matches that do not
  9713. enclose the position of `org-open-link-marker'."
  9714. (let ((m org-open-link-marker))
  9715. (catch 'exit
  9716. (while (apply 're-search-forward args)
  9717. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9718. (goto-char (match-end group))
  9719. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9720. (> (match-beginning 0) (marker-position m))
  9721. (< (match-end 0) (marker-position m)))
  9722. (save-match-data
  9723. (or (not (org-in-regexp
  9724. org-bracket-link-analytic-regexp 1))
  9725. (not (match-end 4)) ; no description
  9726. (and (<= (match-beginning 4) (point))
  9727. (>= (match-end 4) (point))))))
  9728. (throw 'exit (point))))))))
  9729. (defun org-get-buffer-for-internal-link (buffer)
  9730. "Return a buffer to be used for displaying the link target of internal links."
  9731. (cond
  9732. ((not org-display-internal-link-with-indirect-buffer)
  9733. buffer)
  9734. ((string-match "(Clone)$" (buffer-name buffer))
  9735. (message "Buffer is already a clone, not making another one")
  9736. ;; we also do not modify visibility in this case
  9737. buffer)
  9738. (t ; make a new indirect buffer for displaying the link
  9739. (let* ((bn (buffer-name buffer))
  9740. (ibn (concat bn "(Clone)"))
  9741. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9742. (with-current-buffer ib (org-overview))
  9743. ib))))
  9744. (defun org-do-occur (regexp &optional cleanup)
  9745. "Call the Emacs command `occur'.
  9746. If CLEANUP is non-nil, remove the printout of the regular expression
  9747. in the *Occur* buffer. This is useful if the regex is long and not useful
  9748. to read."
  9749. (occur regexp)
  9750. (when cleanup
  9751. (let ((cwin (selected-window)) win beg end)
  9752. (when (setq win (get-buffer-window "*Occur*"))
  9753. (select-window win))
  9754. (goto-char (point-min))
  9755. (when (re-search-forward "match[a-z]+" nil t)
  9756. (setq beg (match-end 0))
  9757. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9758. (setq end (1- (match-beginning 0)))))
  9759. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9760. (goto-char (point-min))
  9761. (select-window cwin))))
  9762. ;;; The mark ring for links jumps
  9763. (defvar org-mark-ring nil
  9764. "Mark ring for positions before jumps in Org-mode.")
  9765. (defvar org-mark-ring-last-goto nil
  9766. "Last position in the mark ring used to go back.")
  9767. ;; Fill and close the ring
  9768. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9769. (loop for i from 1 to org-mark-ring-length do
  9770. (push (make-marker) org-mark-ring))
  9771. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9772. org-mark-ring)
  9773. (defun org-mark-ring-push (&optional pos buffer)
  9774. "Put the current position or POS into the mark ring and rotate it."
  9775. (interactive)
  9776. (setq pos (or pos (point)))
  9777. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9778. (move-marker (car org-mark-ring)
  9779. (or pos (point))
  9780. (or buffer (current-buffer)))
  9781. (message "%s"
  9782. (substitute-command-keys
  9783. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9784. (defun org-mark-ring-goto (&optional n)
  9785. "Jump to the previous position in the mark ring.
  9786. With prefix arg N, jump back that many stored positions. When
  9787. called several times in succession, walk through the entire ring.
  9788. Org-mode commands jumping to a different position in the current file,
  9789. or to another Org-mode file, automatically push the old position
  9790. onto the ring."
  9791. (interactive "p")
  9792. (let (p m)
  9793. (if (eq last-command this-command)
  9794. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9795. (setq p org-mark-ring))
  9796. (setq org-mark-ring-last-goto p)
  9797. (setq m (car p))
  9798. (org-pop-to-buffer-same-window (marker-buffer m))
  9799. (goto-char m)
  9800. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9801. (defun org-remove-angle-brackets (s)
  9802. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9803. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9804. s)
  9805. (defun org-add-angle-brackets (s)
  9806. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9807. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9808. s)
  9809. (defun org-remove-double-quotes (s)
  9810. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9811. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9812. s)
  9813. ;;; Following specific links
  9814. (defun org-follow-timestamp-link ()
  9815. "Open an agenda view for the time-stamp date/range at point."
  9816. (cond
  9817. ((org-at-date-range-p t)
  9818. (let ((org-agenda-start-on-weekday)
  9819. (t1 (match-string 1))
  9820. (t2 (match-string 2)) tt1 tt2)
  9821. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9822. tt2 (time-to-days (org-time-string-to-time t2)))
  9823. (let ((org-agenda-buffer-tmp-name
  9824. (format "*Org Agenda(a:%s)"
  9825. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9826. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9827. ((org-at-timestamp-p t)
  9828. (let ((org-agenda-buffer-tmp-name
  9829. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9830. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9831. (substring (match-string 1) 0 10)))
  9832. 1)))
  9833. (t (error "This should not happen"))))
  9834. ;;; Following file links
  9835. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9836. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9837. (declare-function mailcap-mime-info
  9838. "mailcap" (string &optional request no-decode))
  9839. (defvar org-wait nil)
  9840. (defun org-open-file (path &optional in-emacs line search)
  9841. "Open the file at PATH.
  9842. First, this expands any special file name abbreviations. Then the
  9843. configuration variable `org-file-apps' is checked if it contains an
  9844. entry for this file type, and if yes, the corresponding command is launched.
  9845. If no application is found, Emacs simply visits the file.
  9846. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9847. With a double \\[universal-argument] \\[universal-argument] \
  9848. prefix arg, Org tries to avoid opening in Emacs
  9849. and to use an external application to visit the file.
  9850. Optional LINE specifies a line to go to, optional SEARCH a string
  9851. to search for. If LINE or SEARCH is given, the file will be
  9852. opened in Emacs, unless an entry from org-file-apps that makes
  9853. use of groups in a regexp matches.
  9854. If you want to change the way frames are used when following a
  9855. link, please customize `org-link-frame-setup'.
  9856. If the file does not exist, an error is thrown."
  9857. (let* ((file (if (equal path "")
  9858. buffer-file-name
  9859. (substitute-in-file-name (expand-file-name path))))
  9860. (file-apps (append org-file-apps (org-default-apps)))
  9861. (apps (org-remove-if
  9862. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9863. (apps-dlink (org-remove-if-not
  9864. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9865. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9866. (dirp (if remp nil (file-directory-p file)))
  9867. (file (if (and dirp org-open-directory-means-index-dot-org)
  9868. (concat (file-name-as-directory file) "index.org")
  9869. file))
  9870. (a-m-a-p (assq 'auto-mode apps))
  9871. (dfile (downcase file))
  9872. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9873. (link (cond ((and (eq line nil)
  9874. (eq search nil))
  9875. file)
  9876. (line
  9877. (concat file "::" (number-to-string line)))
  9878. (search
  9879. (concat file "::" search))))
  9880. (dlink (downcase link))
  9881. (old-buffer (current-buffer))
  9882. (old-pos (point))
  9883. (old-mode major-mode)
  9884. ext cmd link-match-data)
  9885. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9886. (setq ext (match-string 1 dfile))
  9887. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9888. (setq ext (match-string 1 dfile))))
  9889. (cond
  9890. ((member in-emacs '((16) system))
  9891. (setq cmd (cdr (assoc 'system apps))))
  9892. (in-emacs (setq cmd 'emacs))
  9893. (t
  9894. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9895. (and dirp (cdr (assoc 'directory apps)))
  9896. ; first, try matching against apps-dlink
  9897. ; if we get a match here, store the match data for later
  9898. (let ((match (assoc-default dlink apps-dlink
  9899. 'string-match)))
  9900. (if match
  9901. (progn (setq link-match-data (match-data))
  9902. match)
  9903. (progn (setq in-emacs (or in-emacs line search))
  9904. nil))) ; if we have no match in apps-dlink,
  9905. ; always open the file in emacs if line or search
  9906. ; is given (for backwards compatibility)
  9907. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9908. 'string-match)
  9909. (cdr (assoc ext apps))
  9910. (cdr (assoc t apps))))))
  9911. (when (eq cmd 'system)
  9912. (setq cmd (cdr (assoc 'system apps))))
  9913. (when (eq cmd 'default)
  9914. (setq cmd (cdr (assoc t apps))))
  9915. (when (eq cmd 'mailcap)
  9916. (require 'mailcap)
  9917. (mailcap-parse-mailcaps)
  9918. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9919. (command (mailcap-mime-info mime-type)))
  9920. (if (stringp command)
  9921. (setq cmd command)
  9922. (setq cmd 'emacs))))
  9923. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9924. (not (file-exists-p file))
  9925. (not org-open-non-existing-files))
  9926. (error "No such file: %s" file))
  9927. (cond
  9928. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9929. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9930. (while (string-match "['\"]%s['\"]" cmd)
  9931. (setq cmd (replace-match "%s" t t cmd)))
  9932. (while (string-match "%s" cmd)
  9933. (setq cmd (replace-match
  9934. (save-match-data
  9935. (shell-quote-argument
  9936. (convert-standard-filename file)))
  9937. t t cmd)))
  9938. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9939. (save-match-data
  9940. (let ((match-index 1)
  9941. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9942. (set-match-data link-match-data)
  9943. (while (<= match-index number-of-groups)
  9944. (let ((regex (concat "%" (number-to-string match-index)))
  9945. (replace-with (match-string match-index dlink)))
  9946. (while (string-match regex cmd)
  9947. (setq cmd (replace-match replace-with t t cmd))))
  9948. (setq match-index (+ match-index 1)))))
  9949. (save-window-excursion
  9950. (message "Running %s...done" cmd)
  9951. (start-process-shell-command cmd nil cmd)
  9952. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9953. ((or (stringp cmd)
  9954. (eq cmd 'emacs))
  9955. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9956. (widen)
  9957. (if line (org-goto-line line)
  9958. (if search (org-link-search search))))
  9959. ((consp cmd)
  9960. (let ((file (convert-standard-filename file)))
  9961. (save-match-data
  9962. (set-match-data link-match-data)
  9963. (eval cmd))))
  9964. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9965. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9966. (or (not (equal old-buffer (current-buffer)))
  9967. (not (equal old-pos (point))))
  9968. (org-mark-ring-push old-pos old-buffer))))
  9969. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9970. "This function returns non-nil if `entry' uses a regular
  9971. expression which should be matched against the whole link by
  9972. org-open-file.
  9973. It assumes that is the case when the entry uses a regular
  9974. expression which has at least one grouping construct and the
  9975. action is either a lisp form or a command string containing
  9976. '%1', i.e. using at least one subexpression match as a
  9977. parameter."
  9978. (let ((selector (car entry))
  9979. (action (cdr entry)))
  9980. (if (stringp selector)
  9981. (and (> (regexp-opt-depth selector) 0)
  9982. (or (and (stringp action)
  9983. (string-match "%[0-9]" action))
  9984. (consp action)))
  9985. nil)))
  9986. (defun org-default-apps ()
  9987. "Return the default applications for this operating system."
  9988. (cond
  9989. ((eq system-type 'darwin)
  9990. org-file-apps-defaults-macosx)
  9991. ((eq system-type 'windows-nt)
  9992. org-file-apps-defaults-windowsnt)
  9993. (t org-file-apps-defaults-gnu)))
  9994. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9995. "Convert extensions to regular expressions in the cars of LIST.
  9996. Also, weed out any non-string entries, because the return value is used
  9997. only for regexp matching.
  9998. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9999. point to the symbol `emacs', indicating that the file should
  10000. be opened in Emacs."
  10001. (append
  10002. (delq nil
  10003. (mapcar (lambda (x)
  10004. (if (not (stringp (car x)))
  10005. nil
  10006. (if (string-match "\\W" (car x))
  10007. x
  10008. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10009. list))
  10010. (if add-auto-mode
  10011. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10012. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10013. (defun org-file-remote-p (file)
  10014. "Test whether FILE specifies a location on a remote system.
  10015. Return non-nil if the location is indeed remote.
  10016. For example, the filename \"/user@host:/foo\" specifies a location
  10017. on the system \"/user@host:\"."
  10018. (cond ((fboundp 'file-remote-p)
  10019. (file-remote-p file))
  10020. ((fboundp 'tramp-handle-file-remote-p)
  10021. (tramp-handle-file-remote-p file))
  10022. ((and (boundp 'ange-ftp-name-format)
  10023. (string-match (car ange-ftp-name-format) file))
  10024. t)))
  10025. ;;;; Refiling
  10026. (defun org-get-org-file ()
  10027. "Read a filename, with default directory `org-directory'."
  10028. (let ((default (or org-default-notes-file remember-data-file)))
  10029. (read-file-name (format "File name [%s]: " default)
  10030. (file-name-as-directory org-directory)
  10031. default)))
  10032. (defun org-notes-order-reversed-p ()
  10033. "Check if the current file should receive notes in reversed order."
  10034. (cond
  10035. ((not org-reverse-note-order) nil)
  10036. ((eq t org-reverse-note-order) t)
  10037. ((not (listp org-reverse-note-order)) nil)
  10038. (t (catch 'exit
  10039. (let ((all org-reverse-note-order)
  10040. entry)
  10041. (while (setq entry (pop all))
  10042. (if (string-match (car entry) buffer-file-name)
  10043. (throw 'exit (cdr entry))))
  10044. nil)))))
  10045. (defvar org-refile-target-table nil
  10046. "The list of refile targets, created by `org-refile'.")
  10047. (defvar org-agenda-new-buffers nil
  10048. "Buffers created to visit agenda files.")
  10049. (defvar org-refile-cache nil
  10050. "Cache for refile targets.")
  10051. (defvar org-refile-markers nil
  10052. "All the markers used for caching refile locations.")
  10053. (defun org-refile-marker (pos)
  10054. "Get a new refile marker, but only if caching is in use."
  10055. (if (not org-refile-use-cache)
  10056. pos
  10057. (let ((m (make-marker)))
  10058. (move-marker m pos)
  10059. (push m org-refile-markers)
  10060. m)))
  10061. (defun org-refile-cache-clear ()
  10062. "Clear the refile cache and disable all the markers."
  10063. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10064. (setq org-refile-markers nil)
  10065. (setq org-refile-cache nil)
  10066. (message "Refile cache has been cleared"))
  10067. (defun org-refile-cache-check-set (set)
  10068. "Check if all the markers in the cache still have live buffers."
  10069. (let (marker)
  10070. (catch 'exit
  10071. (while (and set (setq marker (nth 3 (pop set))))
  10072. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10073. (when (and marker (null (marker-buffer marker)))
  10074. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10075. (sit-for 3)
  10076. (throw 'exit nil)))
  10077. t)))
  10078. (defun org-refile-cache-put (set &rest identifiers)
  10079. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10080. (let* ((key (sha1 (prin1-to-string identifiers)))
  10081. (entry (assoc key org-refile-cache)))
  10082. (if entry
  10083. (setcdr entry set)
  10084. (push (cons key set) org-refile-cache))))
  10085. (defun org-refile-cache-get (&rest identifiers)
  10086. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10087. (cond
  10088. ((not org-refile-cache) nil)
  10089. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10090. (t
  10091. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10092. org-refile-cache))))
  10093. (and set (org-refile-cache-check-set set) set)))))
  10094. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10095. "Produce a table with refile targets."
  10096. (let ((case-fold-search nil)
  10097. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10098. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10099. targets tgs txt re files f desc descre fast-path-p level pos0)
  10100. (message "Getting targets...")
  10101. (with-current-buffer (or default-buffer (current-buffer))
  10102. (while (setq entry (pop entries))
  10103. (setq files (car entry) desc (cdr entry))
  10104. (setq fast-path-p nil)
  10105. (cond
  10106. ((null files) (setq files (list (current-buffer))))
  10107. ((eq files 'org-agenda-files)
  10108. (setq files (org-agenda-files 'unrestricted)))
  10109. ((and (symbolp files) (fboundp files))
  10110. (setq files (funcall files)))
  10111. ((and (symbolp files) (boundp files))
  10112. (setq files (symbol-value files))))
  10113. (if (stringp files) (setq files (list files)))
  10114. (cond
  10115. ((eq (car desc) :tag)
  10116. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10117. ((eq (car desc) :todo)
  10118. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10119. ((eq (car desc) :regexp)
  10120. (setq descre (cdr desc)))
  10121. ((eq (car desc) :level)
  10122. (setq descre (concat "^\\*\\{" (number-to-string
  10123. (if org-odd-levels-only
  10124. (1- (* 2 (cdr desc)))
  10125. (cdr desc)))
  10126. "\\}[ \t]")))
  10127. ((eq (car desc) :maxlevel)
  10128. (setq fast-path-p t)
  10129. (setq descre (concat "^\\*\\{1," (number-to-string
  10130. (if org-odd-levels-only
  10131. (1- (* 2 (cdr desc)))
  10132. (cdr desc)))
  10133. "\\}[ \t]")))
  10134. (t (error "Bad refiling target description %s" desc)))
  10135. (while (setq f (pop files))
  10136. (with-current-buffer
  10137. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10138. (or
  10139. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10140. (progn
  10141. (if (bufferp f) (setq f (buffer-file-name
  10142. (buffer-base-buffer f))))
  10143. (setq f (and f (expand-file-name f)))
  10144. (if (eq org-refile-use-outline-path 'file)
  10145. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10146. (save-excursion
  10147. (save-restriction
  10148. (widen)
  10149. (goto-char (point-min))
  10150. (while (re-search-forward descre nil t)
  10151. (goto-char (setq pos0 (point-at-bol)))
  10152. (catch 'next
  10153. (when org-refile-target-verify-function
  10154. (save-match-data
  10155. (or (funcall org-refile-target-verify-function)
  10156. (throw 'next t))))
  10157. (when (and (looking-at org-complex-heading-regexp)
  10158. (not (member (match-string 4) excluded-entries))
  10159. (match-string 4))
  10160. (setq level (org-reduced-level
  10161. (- (match-end 1) (match-beginning 1)))
  10162. txt (org-link-display-format (match-string 4))
  10163. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10164. re (format org-complex-heading-regexp-format
  10165. (regexp-quote (match-string 4))))
  10166. (when org-refile-use-outline-path
  10167. (setq txt (mapconcat
  10168. 'org-protect-slash
  10169. (append
  10170. (if (eq org-refile-use-outline-path
  10171. 'file)
  10172. (list (file-name-nondirectory
  10173. (buffer-file-name
  10174. (buffer-base-buffer))))
  10175. (if (eq org-refile-use-outline-path
  10176. 'full-file-path)
  10177. (list (buffer-file-name
  10178. (buffer-base-buffer)))))
  10179. (org-get-outline-path fast-path-p
  10180. level txt)
  10181. (list txt))
  10182. "/")))
  10183. (push (list txt f re (org-refile-marker (point)))
  10184. tgs)))
  10185. (when (= (point) pos0)
  10186. ;; verification function has not moved point
  10187. (goto-char (point-at-eol))))))))
  10188. (when org-refile-use-cache
  10189. (org-refile-cache-put tgs (buffer-file-name) descre))
  10190. (setq targets (append tgs targets))
  10191. ))))
  10192. (message "Getting targets...done")
  10193. (nreverse targets)))
  10194. (defun org-protect-slash (s)
  10195. (while (string-match "/" s)
  10196. (setq s (replace-match "\\" t t s)))
  10197. s)
  10198. (defvar org-olpa (make-vector 20 nil))
  10199. (defun org-get-outline-path (&optional fastp level heading)
  10200. "Return the outline path to the current entry, as a list.
  10201. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10202. routine which makes outline path derivations for an entire file,
  10203. avoiding backtracing. Refile target collection makes use of that."
  10204. (if fastp
  10205. (progn
  10206. (if (> level 19)
  10207. (error "Outline path failure, more than 19 levels"))
  10208. (loop for i from level upto 19 do
  10209. (aset org-olpa i nil))
  10210. (prog1
  10211. (delq nil (append org-olpa nil))
  10212. (aset org-olpa level heading)))
  10213. (let (rtn case-fold-search)
  10214. (save-excursion
  10215. (save-restriction
  10216. (widen)
  10217. (while (org-up-heading-safe)
  10218. (when (looking-at org-complex-heading-regexp)
  10219. (push (org-match-string-no-properties 4) rtn)))
  10220. rtn)))))
  10221. (defun org-format-outline-path (path &optional width prefix separator)
  10222. "Format the outline path PATH for display.
  10223. WIDTH is the maximum number of characters that is available.
  10224. PREFIX is a prefix to be included in the returned string,
  10225. such as the file name.
  10226. SEPARATOR is inserted between the different parts of the path,
  10227. the default is \"/\"."
  10228. (setq width (or width 79))
  10229. (if prefix (setq width (- width (length prefix))))
  10230. (if (not path)
  10231. (or prefix "")
  10232. (let* ((nsteps (length path))
  10233. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10234. (maxwidth (if (<= total-width width)
  10235. 10000 ;; everything fits
  10236. ;; we need to shorten the level headings
  10237. (/ (- width nsteps) nsteps)))
  10238. (org-odd-levels-only nil)
  10239. (n 0)
  10240. (total (1+ (length prefix))))
  10241. (setq maxwidth (max maxwidth 10))
  10242. (concat prefix
  10243. (if prefix (or separator "/"))
  10244. (mapconcat
  10245. (lambda (h)
  10246. (setq n (1+ n))
  10247. (if (and (= n nsteps) (< maxwidth 10000))
  10248. (setq maxwidth (- total-width total)))
  10249. (if (< (length h) maxwidth)
  10250. (progn (setq total (+ total (length h) 1)) h)
  10251. (setq h (substring h 0 (- maxwidth 2))
  10252. total (+ total maxwidth 1))
  10253. (if (string-match "[ \t]+\\'" h)
  10254. (setq h (substring h 0 (match-beginning 0))))
  10255. (setq h (concat h "..")))
  10256. (org-add-props h nil 'face
  10257. (nth (% (1- n) org-n-level-faces)
  10258. org-level-faces))
  10259. h)
  10260. path (or separator "/"))))))
  10261. (defun org-display-outline-path (&optional file current separator just-return-string)
  10262. "Display the current outline path in the echo area.
  10263. If FILE is non-nil, prepend the output with the file name.
  10264. If CURRENT is non-nil, append the current heading to the output.
  10265. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10266. the different parts of the path and defaults to \"/\".
  10267. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10268. (interactive "P")
  10269. (let* (case-fold-search
  10270. message-log-max ; Don't populate the *Messages* buffer
  10271. (bfn (buffer-file-name (buffer-base-buffer)))
  10272. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10273. res)
  10274. (if current (setq path (append path
  10275. (save-excursion
  10276. (org-back-to-heading t)
  10277. (if (looking-at org-complex-heading-regexp)
  10278. (list (match-string 4)))))))
  10279. (setq res
  10280. (org-format-outline-path
  10281. path
  10282. (1- (frame-width))
  10283. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10284. separator))
  10285. (if just-return-string
  10286. (org-no-properties res)
  10287. (message "%s" res))))
  10288. (defvar org-refile-history nil
  10289. "History for refiling operations.")
  10290. (defvar org-after-refile-insert-hook nil
  10291. "Hook run after `org-refile' has inserted its stuff at the new location.
  10292. Note that this is still *before* the stuff will be removed from
  10293. the *old* location.")
  10294. (defvar org-capture-last-stored-marker)
  10295. (defvar org-refile-keep nil
  10296. "Non-nil means `org-refile' will copy instead of refile.")
  10297. (defun org-copy ()
  10298. "Like `org-refile', but copy."
  10299. (interactive)
  10300. (let ((org-refile-keep t))
  10301. (funcall 'org-refile nil nil nil "Copy")))
  10302. (defun org-refile (&optional goto default-buffer rfloc msg)
  10303. "Move the entry or entries at point to another heading.
  10304. The list of target headings is compiled using the information in
  10305. `org-refile-targets', which see.
  10306. At the target location, the entry is filed as a subitem of the target
  10307. heading. Depending on `org-reverse-note-order', the new subitem will
  10308. either be the first or the last subitem.
  10309. If there is an active region, all entries in that region will be moved.
  10310. However, the region must fulfill the requirement that the first heading
  10311. is the first one sets the top-level of the moved text - at most siblings
  10312. below it are allowed.
  10313. With prefix arg GOTO, the command will only visit the target location
  10314. and not actually move anything.
  10315. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10316. go to the location where the last refiling operation has put the subtree.
  10317. With a prefix argument of `2', refile to the running clock.
  10318. RFLOC can be a refile location obtained in a different way.
  10319. MSG is a string to replace \"Refile\" in the default prompt with
  10320. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10321. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10322. If you are using target caching (see `org-refile-use-cache'),
  10323. you have to clear the target cache in order to find new targets.
  10324. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10325. prefix argument (`C-u C-u C-u C-c C-w')."
  10326. (interactive "P")
  10327. (if (member goto '(0 (64)))
  10328. (org-refile-cache-clear)
  10329. (let* ((actionmsg (or msg "Refile"))
  10330. (cbuf (current-buffer))
  10331. (regionp (org-region-active-p))
  10332. (region-start (and regionp (region-beginning)))
  10333. (region-end (and regionp (region-end)))
  10334. (region-length (and regionp (- region-end region-start)))
  10335. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10336. pos it nbuf file re level reversed)
  10337. (setq last-command nil)
  10338. (when regionp
  10339. (goto-char region-start)
  10340. (or (bolp) (goto-char (point-at-bol)))
  10341. (setq region-start (point))
  10342. (unless (or (org-kill-is-subtree-p
  10343. (buffer-substring region-start region-end))
  10344. (prog1 org-refile-active-region-within-subtree
  10345. (org-toggle-heading)))
  10346. (error "The region is not a (sequence of) subtree(s)")))
  10347. (if (equal goto '(16))
  10348. (org-refile-goto-last-stored)
  10349. (when (or
  10350. (and (equal goto 2)
  10351. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10352. (prog1
  10353. (setq it (list (or org-clock-heading "running clock")
  10354. (buffer-file-name
  10355. (marker-buffer org-clock-hd-marker))
  10356. ""
  10357. (marker-position org-clock-hd-marker)))
  10358. (setq goto nil)))
  10359. (setq it (or rfloc
  10360. (let (heading-text)
  10361. (save-excursion
  10362. (unless goto
  10363. (org-back-to-heading t)
  10364. (setq heading-text
  10365. (nth 4 (org-heading-components))))
  10366. (org-refile-get-location
  10367. (cond (goto "Goto")
  10368. (regionp (concat actionmsg " region to"))
  10369. (t (concat actionmsg " subtree \""
  10370. heading-text "\" to")))
  10371. default-buffer
  10372. (and (not (equal '(4) goto))
  10373. org-refile-allow-creating-parent-nodes)
  10374. goto))))))
  10375. (setq file (nth 1 it)
  10376. re (nth 2 it)
  10377. pos (nth 3 it))
  10378. (if (and (not goto)
  10379. pos
  10380. (equal (buffer-file-name) file)
  10381. (if regionp
  10382. (and (>= pos region-start)
  10383. (<= pos region-end))
  10384. (and (>= pos (point))
  10385. (< pos (save-excursion
  10386. (org-end-of-subtree t t))))))
  10387. (error "Cannot refile to position inside the tree or region"))
  10388. (setq nbuf (or (find-buffer-visiting file)
  10389. (find-file-noselect file)))
  10390. (if goto
  10391. (progn
  10392. (org-pop-to-buffer-same-window nbuf)
  10393. (goto-char pos)
  10394. (org-show-context 'org-goto))
  10395. (if regionp
  10396. (progn
  10397. (org-kill-new (buffer-substring region-start region-end))
  10398. (org-save-markers-in-region region-start region-end))
  10399. (org-copy-subtree 1 nil t))
  10400. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10401. (find-file-noselect file)))
  10402. (setq reversed (org-notes-order-reversed-p))
  10403. (save-excursion
  10404. (save-restriction
  10405. (widen)
  10406. (if pos
  10407. (progn
  10408. (goto-char pos)
  10409. (looking-at org-outline-regexp)
  10410. (setq level (org-get-valid-level (funcall outline-level) 1))
  10411. (goto-char
  10412. (if reversed
  10413. (or (outline-next-heading) (point-max))
  10414. (or (save-excursion (org-get-next-sibling))
  10415. (org-end-of-subtree t t)
  10416. (point-max)))))
  10417. (setq level 1)
  10418. (if (not reversed)
  10419. (goto-char (point-max))
  10420. (goto-char (point-min))
  10421. (or (outline-next-heading) (goto-char (point-max)))))
  10422. (if (not (bolp)) (newline))
  10423. (org-paste-subtree level)
  10424. (when org-log-refile
  10425. (org-add-log-setup 'refile nil nil 'findpos
  10426. org-log-refile)
  10427. (unless (eq org-log-refile 'note)
  10428. (save-excursion (org-add-log-note))))
  10429. (and org-auto-align-tags
  10430. (let ((org-loop-over-headlines-in-active-region nil))
  10431. (org-set-tags nil t)))
  10432. (with-demoted-errors
  10433. (bookmark-set "org-refile-last-stored"))
  10434. ;; If we are refiling for capture, make sure that the
  10435. ;; last-capture pointers point here
  10436. (when (org-bound-and-true-p org-refile-for-capture)
  10437. (with-demoted-errors
  10438. (bookmark-set "org-capture-last-stored-marker"))
  10439. (move-marker org-capture-last-stored-marker (point)))
  10440. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10441. (run-hooks 'org-after-refile-insert-hook))))
  10442. (unless org-refile-keep
  10443. (if regionp
  10444. (delete-region (point) (+ (point) region-length))
  10445. (org-cut-subtree)))
  10446. (when (featurep 'org-inlinetask)
  10447. (org-inlinetask-remove-END-maybe))
  10448. (setq org-markers-to-move nil)
  10449. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10450. (defun org-refile-goto-last-stored ()
  10451. "Go to the location where the last refile was stored."
  10452. (interactive)
  10453. (bookmark-jump "org-refile-last-stored")
  10454. (message "This is the location of the last refile"))
  10455. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10456. no-exclude)
  10457. "Prompt the user for a refile location, using PROMPT.
  10458. PROMPT should not be suffixed with a colon and a space, because
  10459. this function appends the default value from
  10460. `org-refile-history' automatically, if that is not empty.
  10461. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10462. this is used for the GOTO interface."
  10463. (let ((org-refile-targets org-refile-targets)
  10464. (org-refile-use-outline-path org-refile-use-outline-path)
  10465. excluded-entries)
  10466. (when (and (derived-mode-p 'org-mode)
  10467. (not org-refile-use-cache)
  10468. (not no-exclude))
  10469. (org-map-tree
  10470. (lambda()
  10471. (setq excluded-entries
  10472. (append excluded-entries (list (org-get-heading t t)))))))
  10473. (setq org-refile-target-table
  10474. (org-refile-get-targets default-buffer excluded-entries)))
  10475. (unless org-refile-target-table
  10476. (error "No refile targets"))
  10477. (let* ((cbuf (current-buffer))
  10478. (partial-completion-mode nil)
  10479. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10480. (cfunc (if (and org-refile-use-outline-path
  10481. org-outline-path-complete-in-steps)
  10482. 'org-olpath-completing-read
  10483. 'org-icompleting-read))
  10484. (extra (if org-refile-use-outline-path "/" ""))
  10485. (cbnex (concat (buffer-name) extra))
  10486. (filename (and cfn (expand-file-name cfn)))
  10487. (tbl (mapcar
  10488. (lambda (x)
  10489. (if (and (not (member org-refile-use-outline-path
  10490. '(file full-file-path)))
  10491. (not (equal filename (nth 1 x))))
  10492. (cons (concat (car x) extra " ("
  10493. (file-name-nondirectory (nth 1 x)) ")")
  10494. (cdr x))
  10495. (cons (concat (car x) extra) (cdr x))))
  10496. org-refile-target-table))
  10497. (completion-ignore-case t)
  10498. cdef
  10499. (prompt (concat prompt
  10500. (or (and (car org-refile-history)
  10501. (concat " (default " (car org-refile-history) ")"))
  10502. (and (assoc cbnex tbl) (setq cdef cbnex)
  10503. (concat " (default " cbnex ")"))) ": "))
  10504. pa answ parent-target child parent old-hist)
  10505. (setq old-hist org-refile-history)
  10506. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10507. nil 'org-refile-history (or cdef (car org-refile-history))))
  10508. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10509. (org-refile-check-position pa)
  10510. (if pa
  10511. (progn
  10512. (when (or (not org-refile-history)
  10513. (not (eq old-hist org-refile-history))
  10514. (not (equal (car pa) (car org-refile-history))))
  10515. (setq org-refile-history
  10516. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10517. org-refile-history
  10518. (cdr org-refile-history))))
  10519. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10520. (pop org-refile-history)))
  10521. pa)
  10522. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10523. (progn
  10524. (setq parent (match-string 1 answ)
  10525. child (match-string 2 answ))
  10526. (setq parent-target (or (assoc parent tbl)
  10527. (assoc (concat parent "/") tbl)))
  10528. (when (and parent-target
  10529. (or (eq new-nodes t)
  10530. (and (eq new-nodes 'confirm)
  10531. (y-or-n-p (format "Create new node \"%s\"? "
  10532. child)))))
  10533. (org-refile-new-child parent-target child)))
  10534. (error "Invalid target location")))))
  10535. (declare-function org-string-nw-p "org-macs" (s))
  10536. (defun org-refile-check-position (refile-pointer)
  10537. "Check if the refile pointer matches the headline to which it points."
  10538. (let* ((file (nth 1 refile-pointer))
  10539. (re (nth 2 refile-pointer))
  10540. (pos (nth 3 refile-pointer))
  10541. buffer)
  10542. (if (and (not (markerp pos)) (not file))
  10543. (error "Please save the buffer to a file before refiling")
  10544. (when (org-string-nw-p re)
  10545. (setq buffer (if (markerp pos)
  10546. (marker-buffer pos)
  10547. (or (find-buffer-visiting file)
  10548. (find-file-noselect file))))
  10549. (with-current-buffer buffer
  10550. (save-excursion
  10551. (save-restriction
  10552. (widen)
  10553. (goto-char pos)
  10554. (beginning-of-line 1)
  10555. (unless (org-looking-at-p re)
  10556. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10557. (defun org-refile-new-child (parent-target child)
  10558. "Use refile target PARENT-TARGET to add new CHILD below it."
  10559. (unless parent-target
  10560. (error "Cannot find parent for new node"))
  10561. (let ((file (nth 1 parent-target))
  10562. (pos (nth 3 parent-target))
  10563. level)
  10564. (with-current-buffer (or (find-buffer-visiting file)
  10565. (find-file-noselect file))
  10566. (save-excursion
  10567. (save-restriction
  10568. (widen)
  10569. (if pos
  10570. (goto-char pos)
  10571. (goto-char (point-max))
  10572. (if (not (bolp)) (newline)))
  10573. (when (looking-at org-outline-regexp)
  10574. (setq level (funcall outline-level))
  10575. (org-end-of-subtree t t))
  10576. (org-back-over-empty-lines)
  10577. (insert "\n" (make-string
  10578. (if pos (org-get-valid-level level 1) 1) ?*)
  10579. " " child "\n")
  10580. (beginning-of-line 0)
  10581. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10582. (defun org-olpath-completing-read (prompt collection &rest args)
  10583. "Read an outline path like a file name."
  10584. (let ((thetable collection)
  10585. (org-completion-use-ido nil) ; does not work with ido.
  10586. (org-completion-use-iswitchb nil)) ; or iswitchb
  10587. (apply
  10588. 'org-icompleting-read prompt
  10589. (lambda (string predicate &optional flag)
  10590. (let (rtn r f (l (length string)))
  10591. (cond
  10592. ((eq flag nil)
  10593. ;; try completion
  10594. (try-completion string thetable))
  10595. ((eq flag t)
  10596. ;; all-completions
  10597. (setq rtn (all-completions string thetable predicate))
  10598. (mapcar
  10599. (lambda (x)
  10600. (setq r (substring x l))
  10601. (if (string-match " ([^)]*)$" x)
  10602. (setq f (match-string 0 x))
  10603. (setq f ""))
  10604. (if (string-match "/" r)
  10605. (concat string (substring r 0 (match-end 0)) f)
  10606. x))
  10607. rtn))
  10608. ((eq flag 'lambda)
  10609. ;; exact match?
  10610. (assoc string thetable)))))
  10611. args)))
  10612. ;;;; Dynamic blocks
  10613. (defun org-find-dblock (name)
  10614. "Find the first dynamic block with name NAME in the buffer.
  10615. If not found, stay at current position and return nil."
  10616. (let ((case-fold-search t) pos)
  10617. (save-excursion
  10618. (goto-char (point-min))
  10619. (setq pos (and (re-search-forward
  10620. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10621. (match-beginning 0))))
  10622. (if pos (goto-char pos))
  10623. pos))
  10624. (defconst org-dblock-start-re
  10625. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10626. "Matches the start line of a dynamic block, with parameters.")
  10627. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10628. "Matches the end of a dynamic block.")
  10629. (defun org-create-dblock (plist)
  10630. "Create a dynamic block section, with parameters taken from PLIST.
  10631. PLIST must contain a :name entry which is used as name of the block."
  10632. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10633. (end-of-line 1)
  10634. (newline))
  10635. (let ((col (current-column))
  10636. (name (plist-get plist :name)))
  10637. (insert "#+BEGIN: " name)
  10638. (while plist
  10639. (if (eq (car plist) :name)
  10640. (setq plist (cddr plist))
  10641. (insert " " (prin1-to-string (pop plist)))))
  10642. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10643. (beginning-of-line -2)))
  10644. (defun org-prepare-dblock ()
  10645. "Prepare dynamic block for refresh.
  10646. This empties the block, puts the cursor at the insert position and returns
  10647. the property list including an extra property :name with the block name."
  10648. (unless (looking-at org-dblock-start-re)
  10649. (error "Not at a dynamic block"))
  10650. (let* ((begdel (1+ (match-end 0)))
  10651. (name (org-no-properties (match-string 1)))
  10652. (params (append (list :name name)
  10653. (read (concat "(" (match-string 3) ")")))))
  10654. (save-excursion
  10655. (beginning-of-line 1)
  10656. (skip-chars-forward " \t")
  10657. (setq params (plist-put params :indentation-column (current-column))))
  10658. (unless (re-search-forward org-dblock-end-re nil t)
  10659. (error "Dynamic block not terminated"))
  10660. (setq params
  10661. (append params
  10662. (list :content (buffer-substring
  10663. begdel (match-beginning 0)))))
  10664. (delete-region begdel (match-beginning 0))
  10665. (goto-char begdel)
  10666. (open-line 1)
  10667. params))
  10668. (defun org-map-dblocks (&optional command)
  10669. "Apply COMMAND to all dynamic blocks in the current buffer.
  10670. If COMMAND is not given, use `org-update-dblock'."
  10671. (let ((cmd (or command 'org-update-dblock)))
  10672. (save-excursion
  10673. (goto-char (point-min))
  10674. (while (re-search-forward org-dblock-start-re nil t)
  10675. (goto-char (match-beginning 0))
  10676. (save-excursion
  10677. (condition-case nil
  10678. (funcall cmd)
  10679. (error (message "Error during update of dynamic block"))))
  10680. (unless (re-search-forward org-dblock-end-re nil t)
  10681. (error "Dynamic block not terminated"))))))
  10682. (defun org-dblock-update (&optional arg)
  10683. "User command for updating dynamic blocks.
  10684. Update the dynamic block at point. With prefix ARG, update all dynamic
  10685. blocks in the buffer."
  10686. (interactive "P")
  10687. (if arg
  10688. (org-update-all-dblocks)
  10689. (or (looking-at org-dblock-start-re)
  10690. (org-beginning-of-dblock))
  10691. (org-update-dblock)))
  10692. (defun org-update-dblock ()
  10693. "Update the dynamic block at point.
  10694. This means to empty the block, parse for parameters and then call
  10695. the correct writing function."
  10696. (interactive)
  10697. (save-window-excursion
  10698. (let* ((pos (point))
  10699. (line (org-current-line))
  10700. (params (org-prepare-dblock))
  10701. (name (plist-get params :name))
  10702. (indent (plist-get params :indentation-column))
  10703. (cmd (intern (concat "org-dblock-write:" name))))
  10704. (message "Updating dynamic block `%s' at line %d..." name line)
  10705. (funcall cmd params)
  10706. (message "Updating dynamic block `%s' at line %d...done" name line)
  10707. (goto-char pos)
  10708. (when (and indent (> indent 0))
  10709. (setq indent (make-string indent ?\ ))
  10710. (save-excursion
  10711. (org-beginning-of-dblock)
  10712. (forward-line 1)
  10713. (while (not (looking-at org-dblock-end-re))
  10714. (insert indent)
  10715. (beginning-of-line 2))
  10716. (when (looking-at org-dblock-end-re)
  10717. (and (looking-at "[ \t]+")
  10718. (replace-match ""))
  10719. (insert indent)))))))
  10720. (defun org-beginning-of-dblock ()
  10721. "Find the beginning of the dynamic block at point.
  10722. Error if there is no such block at point."
  10723. (let ((pos (point))
  10724. beg)
  10725. (end-of-line 1)
  10726. (if (and (re-search-backward org-dblock-start-re nil t)
  10727. (setq beg (match-beginning 0))
  10728. (re-search-forward org-dblock-end-re nil t)
  10729. (> (match-end 0) pos))
  10730. (goto-char beg)
  10731. (goto-char pos)
  10732. (error "Not in a dynamic block"))))
  10733. (defun org-update-all-dblocks ()
  10734. "Update all dynamic blocks in the buffer.
  10735. This function can be used in a hook."
  10736. (interactive)
  10737. (when (derived-mode-p 'org-mode)
  10738. (org-map-dblocks 'org-update-dblock)))
  10739. ;;;; Completion
  10740. (defun org-get-export-keywords ()
  10741. "Return a list of all currently understood export keywords.
  10742. Export keywords include options, block names, attributes and
  10743. keywords relative to each registered export back-end."
  10744. (delq nil
  10745. (let (keywords)
  10746. (mapc
  10747. (lambda (back-end)
  10748. (let ((props (cdr back-end)))
  10749. ;; Back-end name (for keywords, like #+LATEX:)
  10750. (push (upcase (symbol-name (car back-end))) keywords)
  10751. ;; Back-end options.
  10752. (mapc (lambda (option) (push (cadr option) keywords))
  10753. (plist-get (cdr back-end) :options-alist))))
  10754. (org-bound-and-true-p org-export-registered-backends))
  10755. keywords)))
  10756. (defconst org-options-keywords
  10757. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10758. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10759. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10760. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10761. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "TARGET:"))
  10762. (defcustom org-structure-template-alist
  10763. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10764. "<src lang=\"?\">\n\n</src>")
  10765. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10766. "<example>\n?\n</example>")
  10767. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10768. "<quote>\n?\n</quote>")
  10769. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10770. "<verse>\n?\n</verse>")
  10771. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10772. "<verbatim>\n?\n</verbatim>")
  10773. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10774. "<center>\n?\n</center>")
  10775. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10776. "<literal style=\"latex\">\n?\n</literal>")
  10777. ("L" "#+LaTeX: "
  10778. "<literal style=\"latex\">?</literal>")
  10779. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10780. "<literal style=\"html\">\n?\n</literal>")
  10781. ("H" "#+HTML: "
  10782. "<literal style=\"html\">?</literal>")
  10783. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10784. ("A" "#+ASCII: ")
  10785. ("i" "#+INDEX: ?"
  10786. "#+INDEX: ?")
  10787. ("I" "#+INCLUDE: %file ?"
  10788. "<include file=%file markup=\"?\">"))
  10789. "Structure completion elements.
  10790. This is a list of abbreviation keys and values. The value gets inserted
  10791. if you type `<' followed by the key and then press the completion key,
  10792. usually `M-TAB'. %file will be replaced by a file name after prompting
  10793. for the file using completion. The cursor will be placed at the position
  10794. of the `?` in the template.
  10795. There are two templates for each key, the first uses the original Org syntax,
  10796. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10797. the default when the /org-mtags.el/ module has been loaded. See also the
  10798. variable `org-mtags-prefer-muse-templates'."
  10799. :group 'org-completion
  10800. :type '(repeat
  10801. (string :tag "Key")
  10802. (string :tag "Template")
  10803. (string :tag "Muse Template")))
  10804. (defun org-try-structure-completion ()
  10805. "Try to complete a structure template before point.
  10806. This looks for strings like \"<e\" on an otherwise empty line and
  10807. expands them."
  10808. (let ((l (buffer-substring (point-at-bol) (point)))
  10809. a)
  10810. (when (and (looking-at "[ \t]*$")
  10811. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10812. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10813. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10814. (match-beginning 1)) a)
  10815. t)))
  10816. (defun org-complete-expand-structure-template (start cell)
  10817. "Expand a structure template."
  10818. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10819. (rpl (nth (if musep 2 1) cell))
  10820. (ind ""))
  10821. (delete-region start (point))
  10822. (when (string-match "\\`#\\+" rpl)
  10823. (cond
  10824. ((bolp))
  10825. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10826. (setq ind (buffer-substring (point-at-bol) (point))))
  10827. (t (newline))))
  10828. (setq start (point))
  10829. (if (string-match "%file" rpl)
  10830. (setq rpl (replace-match
  10831. (concat
  10832. "\""
  10833. (save-match-data
  10834. (abbreviate-file-name (read-file-name "Include file: ")))
  10835. "\"")
  10836. t t rpl)))
  10837. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10838. (concat "\n" ind)))
  10839. (insert rpl)
  10840. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10841. ;;;; TODO, DEADLINE, Comments
  10842. (defun org-toggle-comment ()
  10843. "Change the COMMENT state of an entry."
  10844. (interactive)
  10845. (save-excursion
  10846. (org-back-to-heading)
  10847. (let (case-fold-search)
  10848. (cond
  10849. ((looking-at (format org-heading-keyword-regexp-format
  10850. org-comment-string))
  10851. (goto-char (match-end 1))
  10852. (looking-at (concat " +" org-comment-string))
  10853. (replace-match "" t t)
  10854. (when (eolp) (insert " ")))
  10855. ((looking-at org-outline-regexp)
  10856. (goto-char (match-end 0))
  10857. (insert org-comment-string " "))))))
  10858. (defvar org-last-todo-state-is-todo nil
  10859. "This is non-nil when the last TODO state change led to a TODO state.
  10860. If the last change removed the TODO tag or switched to DONE, then
  10861. this is nil.")
  10862. (defvar org-setting-tags nil) ; dynamically skipped
  10863. (defvar org-todo-setup-filter-hook nil
  10864. "Hook for functions that pre-filter todo specs.
  10865. Each function takes a todo spec and returns either nil or the spec
  10866. transformed into canonical form." )
  10867. (defvar org-todo-get-default-hook nil
  10868. "Hook for functions that get a default item for todo.
  10869. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10870. nil or a string to be used for the todo mark." )
  10871. (defvar org-agenda-headline-snapshot-before-repeat)
  10872. (defun org-current-effective-time ()
  10873. "Return current time adjusted for `org-extend-today-until' variable."
  10874. (let* ((ct (org-current-time))
  10875. (dct (decode-time ct))
  10876. (ct1
  10877. (cond
  10878. (org-use-last-clock-out-time-as-effective-time
  10879. (or (org-clock-get-last-clock-out-time) ct))
  10880. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10881. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10882. (t ct))))
  10883. ct1))
  10884. (defun org-todo-yesterday (&optional arg)
  10885. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10886. (interactive "P")
  10887. (if (eq major-mode 'org-agenda-mode)
  10888. (apply 'org-agenda-todo-yesterday arg)
  10889. (let* ((hour (third (decode-time
  10890. (org-current-time))))
  10891. (org-extend-today-until (1+ hour)))
  10892. (org-todo arg))))
  10893. (defvar org-block-entry-blocking ""
  10894. "First entry preventing the TODO state change.")
  10895. (defun org-todo (&optional arg)
  10896. "Change the TODO state of an item.
  10897. The state of an item is given by a keyword at the start of the heading,
  10898. like
  10899. *** TODO Write paper
  10900. *** DONE Call mom
  10901. The different keywords are specified in the variable `org-todo-keywords'.
  10902. By default the available states are \"TODO\" and \"DONE\".
  10903. So for this example: when the item starts with TODO, it is changed to DONE.
  10904. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10905. DONE are present, add TODO at the beginning of the heading.
  10906. With \\[universal-argument] prefix arg, use completion to determine the new \
  10907. state.
  10908. With numeric prefix arg, switch to that state.
  10909. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10910. keywords (nextset).
  10911. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10912. With a numeric prefix arg of 0, inhibit note taking for the change.
  10913. For calling through lisp, arg is also interpreted in the following way:
  10914. 'none -> empty state
  10915. \"\"(empty string) -> switch to empty state
  10916. 'done -> switch to DONE
  10917. 'nextset -> switch to the next set of keywords
  10918. 'previousset -> switch to the previous set of keywords
  10919. \"WAITING\" -> switch to the specified keyword, but only if it
  10920. really is a member of `org-todo-keywords'."
  10921. (interactive "P")
  10922. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10923. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10924. 'region-start-level 'region))
  10925. org-loop-over-headlines-in-active-region)
  10926. (org-map-entries
  10927. `(org-todo ,arg)
  10928. org-loop-over-headlines-in-active-region
  10929. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10930. (if (equal arg '(16)) (setq arg 'nextset))
  10931. (let ((org-blocker-hook org-blocker-hook)
  10932. commentp
  10933. case-fold-search)
  10934. (when (equal arg '(64))
  10935. (setq arg nil org-blocker-hook nil))
  10936. (when (and org-blocker-hook
  10937. (or org-inhibit-blocking
  10938. (org-entry-get nil "NOBLOCKING")))
  10939. (setq org-blocker-hook nil))
  10940. (save-excursion
  10941. (catch 'exit
  10942. (org-back-to-heading t)
  10943. (when (looking-at (concat "^\\*+ " org-comment-string))
  10944. (org-toggle-comment)
  10945. (setq commentp t))
  10946. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10947. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10948. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10949. (let* ((match-data (match-data))
  10950. (startpos (point-at-bol))
  10951. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10952. (org-log-done org-log-done)
  10953. (org-log-repeat org-log-repeat)
  10954. (org-todo-log-states org-todo-log-states)
  10955. (org-inhibit-logging
  10956. (if (equal arg 0)
  10957. (progn (setq arg nil) 'note) org-inhibit-logging))
  10958. (this (match-string 1))
  10959. (hl-pos (match-beginning 0))
  10960. (head (org-get-todo-sequence-head this))
  10961. (ass (assoc head org-todo-kwd-alist))
  10962. (interpret (nth 1 ass))
  10963. (done-word (nth 3 ass))
  10964. (final-done-word (nth 4 ass))
  10965. (org-last-state (or this ""))
  10966. (completion-ignore-case t)
  10967. (member (member this org-todo-keywords-1))
  10968. (tail (cdr member))
  10969. (org-state (cond
  10970. ((and org-todo-key-trigger
  10971. (or (and (equal arg '(4))
  10972. (eq org-use-fast-todo-selection 'prefix))
  10973. (and (not arg) org-use-fast-todo-selection
  10974. (not (eq org-use-fast-todo-selection
  10975. 'prefix)))))
  10976. ;; Use fast selection
  10977. (org-fast-todo-selection))
  10978. ((and (equal arg '(4))
  10979. (or (not org-use-fast-todo-selection)
  10980. (not org-todo-key-trigger)))
  10981. ;; Read a state with completion
  10982. (org-icompleting-read
  10983. "State: " (mapcar 'list org-todo-keywords-1)
  10984. nil t))
  10985. ((eq arg 'right)
  10986. (if this
  10987. (if tail (car tail) nil)
  10988. (car org-todo-keywords-1)))
  10989. ((eq arg 'left)
  10990. (if (equal member org-todo-keywords-1)
  10991. nil
  10992. (if this
  10993. (nth (- (length org-todo-keywords-1)
  10994. (length tail) 2)
  10995. org-todo-keywords-1)
  10996. (org-last org-todo-keywords-1))))
  10997. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10998. (setq arg nil))) ; hack to fall back to cycling
  10999. (arg
  11000. ;; user or caller requests a specific state
  11001. (cond
  11002. ((equal arg "") nil)
  11003. ((eq arg 'none) nil)
  11004. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11005. ((eq arg 'nextset)
  11006. (or (car (cdr (member head org-todo-heads)))
  11007. (car org-todo-heads)))
  11008. ((eq arg 'previousset)
  11009. (let ((org-todo-heads (reverse org-todo-heads)))
  11010. (or (car (cdr (member head org-todo-heads)))
  11011. (car org-todo-heads))))
  11012. ((car (member arg org-todo-keywords-1)))
  11013. ((stringp arg)
  11014. (error "State `%s' not valid in this file" arg))
  11015. ((nth (1- (prefix-numeric-value arg))
  11016. org-todo-keywords-1))))
  11017. ((null member) (or head (car org-todo-keywords-1)))
  11018. ((equal this final-done-word) nil) ;; -> make empty
  11019. ((null tail) nil) ;; -> first entry
  11020. ((memq interpret '(type priority))
  11021. (if (eq this-command last-command)
  11022. (car tail)
  11023. (if (> (length tail) 0)
  11024. (or done-word (car org-done-keywords))
  11025. nil)))
  11026. (t
  11027. (car tail))))
  11028. (org-state (or
  11029. (run-hook-with-args-until-success
  11030. 'org-todo-get-default-hook org-state org-last-state)
  11031. org-state))
  11032. (next (if org-state (concat " " org-state " ") " "))
  11033. (change-plist (list :type 'todo-state-change :from this :to org-state
  11034. :position startpos))
  11035. dolog now-done-p)
  11036. (when org-blocker-hook
  11037. (setq org-last-todo-state-is-todo
  11038. (not (member this org-done-keywords)))
  11039. (unless (save-excursion
  11040. (save-match-data
  11041. (org-with-wide-buffer
  11042. (run-hook-with-args-until-failure
  11043. 'org-blocker-hook change-plist))))
  11044. (if (org-called-interactively-p 'interactive)
  11045. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11046. this org-state org-block-entry-blocking)
  11047. ;; fail silently
  11048. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11049. this org-state org-block-entry-blocking)
  11050. (throw 'exit nil))))
  11051. (store-match-data match-data)
  11052. (replace-match next t t)
  11053. (unless (pos-visible-in-window-p hl-pos)
  11054. (message "TODO state changed to %s" (org-trim next)))
  11055. (unless head
  11056. (setq head (org-get-todo-sequence-head org-state)
  11057. ass (assoc head org-todo-kwd-alist)
  11058. interpret (nth 1 ass)
  11059. done-word (nth 3 ass)
  11060. final-done-word (nth 4 ass)))
  11061. (when (memq arg '(nextset previousset))
  11062. (message "Keyword-Set %d/%d: %s"
  11063. (- (length org-todo-sets) -1
  11064. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11065. (length org-todo-sets)
  11066. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11067. (setq org-last-todo-state-is-todo
  11068. (not (member org-state org-done-keywords)))
  11069. (setq now-done-p (and (member org-state org-done-keywords)
  11070. (not (member this org-done-keywords))))
  11071. (and logging (org-local-logging logging))
  11072. (when (and (or org-todo-log-states org-log-done)
  11073. (not (eq org-inhibit-logging t))
  11074. (not (memq arg '(nextset previousset))))
  11075. ;; we need to look at recording a time and note
  11076. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11077. (nth 2 (assoc this org-todo-log-states))))
  11078. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11079. (setq dolog 'time))
  11080. (when (and org-state
  11081. (member org-state org-not-done-keywords)
  11082. (not (member this org-not-done-keywords)))
  11083. ;; This is now a todo state and was not one before
  11084. ;; If there was a CLOSED time stamp, get rid of it.
  11085. (org-add-planning-info nil nil 'closed))
  11086. (when (and now-done-p org-log-done)
  11087. ;; It is now done, and it was not done before
  11088. (org-add-planning-info 'closed (org-current-effective-time))
  11089. (if (and (not dolog) (eq 'note org-log-done))
  11090. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11091. (when (and org-state dolog)
  11092. ;; This is a non-nil state, and we need to log it
  11093. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11094. ;; Fixup tag positioning
  11095. (org-todo-trigger-tag-changes org-state)
  11096. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11097. (when org-provide-todo-statistics
  11098. (org-update-parent-todo-statistics))
  11099. (run-hooks 'org-after-todo-state-change-hook)
  11100. (if (and arg (not (member org-state org-done-keywords)))
  11101. (setq head (org-get-todo-sequence-head org-state)))
  11102. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11103. ;; Do we need to trigger a repeat?
  11104. (when now-done-p
  11105. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11106. ;; This is for the agenda, take a snapshot of the headline.
  11107. (save-match-data
  11108. (setq org-agenda-headline-snapshot-before-repeat
  11109. (org-get-heading))))
  11110. (org-auto-repeat-maybe org-state))
  11111. ;; Fixup cursor location if close to the keyword
  11112. (if (and (outline-on-heading-p)
  11113. (not (bolp))
  11114. (save-excursion (beginning-of-line 1)
  11115. (looking-at org-todo-line-regexp))
  11116. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11117. (progn
  11118. (goto-char (or (match-end 2) (match-end 1)))
  11119. (and (looking-at " ") (just-one-space))))
  11120. (when org-trigger-hook
  11121. (save-excursion
  11122. (run-hook-with-args 'org-trigger-hook change-plist)))
  11123. (when commentp (org-toggle-comment))))))))
  11124. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11125. "Block turning an entry into a TODO, using the hierarchy.
  11126. This checks whether the current task should be blocked from state
  11127. changes. Such blocking occurs when:
  11128. 1. The task has children which are not all in a completed state.
  11129. 2. A task has a parent with the property :ORDERED:, and there
  11130. are siblings prior to the current task with incomplete
  11131. status.
  11132. 3. The parent of the task is blocked because it has siblings that should
  11133. be done first, or is child of a block grandparent TODO entry."
  11134. (if (not org-enforce-todo-dependencies)
  11135. t ; if locally turned off don't block
  11136. (catch 'dont-block
  11137. ;; If this is not a todo state change, or if this entry is already DONE,
  11138. ;; do not block
  11139. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11140. (member (plist-get change-plist :from)
  11141. (cons 'done org-done-keywords))
  11142. (member (plist-get change-plist :to)
  11143. (cons 'todo org-not-done-keywords))
  11144. (not (plist-get change-plist :to)))
  11145. (throw 'dont-block t))
  11146. ;; If this task has children, and any are undone, it's blocked
  11147. (save-excursion
  11148. (org-back-to-heading t)
  11149. (let ((this-level (funcall outline-level)))
  11150. (outline-next-heading)
  11151. (let ((child-level (funcall outline-level)))
  11152. (while (and (not (eobp))
  11153. (> child-level this-level))
  11154. ;; this todo has children, check whether they are all
  11155. ;; completed
  11156. (if (and (not (org-entry-is-done-p))
  11157. (org-entry-is-todo-p))
  11158. (progn (setq org-block-entry-blocking (org-get-heading))
  11159. (throw 'dont-block nil)))
  11160. (outline-next-heading)
  11161. (setq child-level (funcall outline-level))))))
  11162. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11163. ;; any previous siblings are undone, it's blocked
  11164. (save-excursion
  11165. (org-back-to-heading t)
  11166. (let* ((pos (point))
  11167. (parent-pos (and (org-up-heading-safe) (point))))
  11168. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11169. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11170. (forward-line 1)
  11171. (re-search-forward org-not-done-heading-regexp pos t))
  11172. (setq org-block-entry-blocking (match-string 0))
  11173. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11174. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11175. (while t
  11176. (goto-char parent-pos)
  11177. (if (not (looking-at org-not-done-heading-regexp))
  11178. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11179. (setq pos (point))
  11180. (setq parent-pos (and (org-up-heading-safe) (point)))
  11181. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11182. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11183. (forward-line 1)
  11184. (re-search-forward org-not-done-heading-regexp pos t)
  11185. (setq org-block-entry-blocking (org-get-heading)))
  11186. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11187. (defcustom org-track-ordered-property-with-tag nil
  11188. "Should the ORDERED property also be shown as a tag?
  11189. The ORDERED property decides if an entry should require subtasks to be
  11190. completed in sequence. Since a property is not very visible, setting
  11191. this option means that toggling the ORDERED property with the command
  11192. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11193. not relevant for the behavior, but it makes things more visible.
  11194. Note that toggling the tag with tags commands will not change the property
  11195. and therefore not influence behavior!
  11196. This can be t, meaning the tag ORDERED should be used, It can also be a
  11197. string to select a different tag for this task."
  11198. :group 'org-todo
  11199. :type '(choice
  11200. (const :tag "No tracking" nil)
  11201. (const :tag "Track with ORDERED tag" t)
  11202. (string :tag "Use other tag")))
  11203. (defun org-toggle-ordered-property ()
  11204. "Toggle the ORDERED property of the current entry.
  11205. For better visibility, you can track the value of this property with a tag.
  11206. See variable `org-track-ordered-property-with-tag'."
  11207. (interactive)
  11208. (let* ((t1 org-track-ordered-property-with-tag)
  11209. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11210. (save-excursion
  11211. (org-back-to-heading)
  11212. (if (org-entry-get nil "ORDERED")
  11213. (progn
  11214. (org-delete-property "ORDERED" "PROPERTIES")
  11215. (and tag (org-toggle-tag tag 'off))
  11216. (message "Subtasks can be completed in arbitrary order"))
  11217. (org-entry-put nil "ORDERED" "t")
  11218. (and tag (org-toggle-tag tag 'on))
  11219. (message "Subtasks must be completed in sequence")))))
  11220. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11221. (defun org-block-todo-from-checkboxes (change-plist)
  11222. "Block turning an entry into a TODO, using checkboxes.
  11223. This checks whether the current task should be blocked from state
  11224. changes because there are unchecked boxes in this entry."
  11225. (if (not org-enforce-todo-checkbox-dependencies)
  11226. t ; if locally turned off don't block
  11227. (catch 'dont-block
  11228. ;; If this is not a todo state change, or if this entry is already DONE,
  11229. ;; do not block
  11230. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11231. (member (plist-get change-plist :from)
  11232. (cons 'done org-done-keywords))
  11233. (member (plist-get change-plist :to)
  11234. (cons 'todo org-not-done-keywords))
  11235. (not (plist-get change-plist :to)))
  11236. (throw 'dont-block t))
  11237. ;; If this task has checkboxes that are not checked, it's blocked
  11238. (save-excursion
  11239. (org-back-to-heading t)
  11240. (let ((beg (point)) end)
  11241. (outline-next-heading)
  11242. (setq end (point))
  11243. (goto-char beg)
  11244. (if (org-list-search-forward
  11245. (concat (org-item-beginning-re)
  11246. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11247. "\\[[- ]\\]")
  11248. end t)
  11249. (progn
  11250. (if (boundp 'org-blocked-by-checkboxes)
  11251. (setq org-blocked-by-checkboxes t))
  11252. (throw 'dont-block nil)))))
  11253. t))) ; do not block
  11254. (defun org-entry-blocked-p ()
  11255. "Is the current entry blocked?"
  11256. (org-with-silent-modifications
  11257. (if (org-entry-get nil "NOBLOCKING")
  11258. nil ;; Never block this entry
  11259. (not (run-hook-with-args-until-failure
  11260. 'org-blocker-hook
  11261. (list :type 'todo-state-change
  11262. :position (point)
  11263. :from 'todo
  11264. :to 'done))))))
  11265. (defun org-update-statistics-cookies (all)
  11266. "Update the statistics cookie, either from TODO or from checkboxes.
  11267. This should be called with the cursor in a line with a statistics cookie."
  11268. (interactive "P")
  11269. (if all
  11270. (progn
  11271. (org-update-checkbox-count 'all)
  11272. (org-map-entries 'org-update-parent-todo-statistics))
  11273. (if (not (org-at-heading-p))
  11274. (org-update-checkbox-count)
  11275. (let ((pos (point-marker))
  11276. end l1 l2)
  11277. (ignore-errors (org-back-to-heading t))
  11278. (if (not (org-at-heading-p))
  11279. (org-update-checkbox-count)
  11280. (setq l1 (org-outline-level))
  11281. (setq end (save-excursion
  11282. (outline-next-heading)
  11283. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11284. (point)))
  11285. (if (and (save-excursion
  11286. (re-search-forward
  11287. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11288. (not (save-excursion (re-search-forward
  11289. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11290. (org-update-checkbox-count)
  11291. (if (and l2 (> l2 l1))
  11292. (progn
  11293. (goto-char end)
  11294. (org-update-parent-todo-statistics))
  11295. (goto-char pos)
  11296. (beginning-of-line 1)
  11297. (while (re-search-forward
  11298. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11299. (point-at-eol) t)
  11300. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11301. (goto-char pos)
  11302. (move-marker pos nil)))))
  11303. (defvar org-entry-property-inherited-from) ;; defined below
  11304. (defun org-update-parent-todo-statistics ()
  11305. "Update any statistics cookie in the parent of the current headline.
  11306. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11307. the entire subtree and this will travel up the hierarchy and update
  11308. statistics everywhere."
  11309. (let* ((prop (save-excursion (org-up-heading-safe)
  11310. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11311. (recursive (or (not org-hierarchical-todo-statistics)
  11312. (and prop (string-match "\\<recursive\\>" prop))))
  11313. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11314. 0))
  11315. (first t)
  11316. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11317. level ltoggle l1 new ndel
  11318. (cnt-all 0) (cnt-done 0) is-percent kwd
  11319. checkbox-beg ov ovs ove cookie-present)
  11320. (catch 'exit
  11321. (save-excursion
  11322. (beginning-of-line 1)
  11323. (setq ltoggle (funcall outline-level))
  11324. ;; Three situations are to consider:
  11325. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11326. ;; to the top-level ancestor on the headline;
  11327. ;; 2. If parent has "recursive" property, repeat up to the
  11328. ;; headline setting that property, taking inheritance into
  11329. ;; account;
  11330. ;; 3. Else, move up to direct parent and proceed only once.
  11331. (while (and (setq level (org-up-heading-safe))
  11332. (or recursive first)
  11333. (>= (point) lim))
  11334. (setq first nil cookie-present nil)
  11335. (unless (and level
  11336. (not (string-match
  11337. "\\<checkbox\\>"
  11338. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11339. "")))))
  11340. (throw 'exit nil))
  11341. (while (re-search-forward box-re (point-at-eol) t)
  11342. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11343. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11344. (save-match-data
  11345. (unless (outline-next-heading) (throw 'exit nil))
  11346. (while (and (looking-at org-complex-heading-regexp)
  11347. (> (setq l1 (length (match-string 1))) level))
  11348. (setq kwd (and (or recursive (= l1 ltoggle))
  11349. (match-string 2)))
  11350. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11351. (and (listp org-provide-todo-statistics)
  11352. (or (member kwd org-provide-todo-statistics)
  11353. (member kwd org-done-keywords))))
  11354. (setq cnt-all (1+ cnt-all))
  11355. (if (eq org-provide-todo-statistics t)
  11356. (and kwd (setq cnt-all (1+ cnt-all)))))
  11357. (and (member kwd org-done-keywords)
  11358. (setq cnt-done (1+ cnt-done)))
  11359. (outline-next-heading)))
  11360. (setq new
  11361. (if is-percent
  11362. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11363. (format "[%d/%d]" cnt-done cnt-all))
  11364. ndel (- (match-end 0) checkbox-beg))
  11365. ;; handle overlays when updating cookie from column view
  11366. (when (setq ov (car (overlays-at checkbox-beg)))
  11367. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11368. (delete-overlay ov))
  11369. (goto-char checkbox-beg)
  11370. (insert new)
  11371. (delete-region (point) (+ (point) ndel))
  11372. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11373. (when ov (move-overlay ov ovs ove)))
  11374. (when cookie-present
  11375. (run-hook-with-args 'org-after-todo-statistics-hook
  11376. cnt-done (- cnt-all cnt-done))))))
  11377. (run-hooks 'org-todo-statistics-hook)))
  11378. (defvar org-after-todo-statistics-hook nil
  11379. "Hook that is called after a TODO statistics cookie has been updated.
  11380. Each function is called with two arguments: the number of not-done entries
  11381. and the number of done entries.
  11382. For example, the following function, when added to this hook, will switch
  11383. an entry to DONE when all children are done, and back to TODO when new
  11384. entries are set to a TODO status. Note that this hook is only called
  11385. when there is a statistics cookie in the headline!
  11386. (defun org-summary-todo (n-done n-not-done)
  11387. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11388. (let (org-log-done org-log-states) ; turn off logging
  11389. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11390. ")
  11391. (defvar org-todo-statistics-hook nil
  11392. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11393. This hook runs even if there is no statistics cookie present, in which case
  11394. `org-after-todo-statistics-hook' would not run.")
  11395. (defun org-todo-trigger-tag-changes (state)
  11396. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11397. (let ((l org-todo-state-tags-triggers)
  11398. changes)
  11399. (when (or (not state) (equal state ""))
  11400. (setq changes (append changes (cdr (assoc "" l)))))
  11401. (when (and (stringp state) (> (length state) 0))
  11402. (setq changes (append changes (cdr (assoc state l)))))
  11403. (when (member state org-not-done-keywords)
  11404. (setq changes (append changes (cdr (assoc 'todo l)))))
  11405. (when (member state org-done-keywords)
  11406. (setq changes (append changes (cdr (assoc 'done l)))))
  11407. (dolist (c changes)
  11408. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11409. (defun org-local-logging (value)
  11410. "Get logging settings from a property VALUE."
  11411. (let* (words w a)
  11412. ;; directly set the variables, they are already local.
  11413. (setq org-log-done nil
  11414. org-log-repeat nil
  11415. org-todo-log-states nil)
  11416. (setq words (org-split-string value))
  11417. (while (setq w (pop words))
  11418. (cond
  11419. ((setq a (assoc w org-startup-options))
  11420. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11421. (set (nth 1 a) (nth 2 a))))
  11422. ((setq a (org-extract-log-state-settings w))
  11423. (and (member (car a) org-todo-keywords-1)
  11424. (push a org-todo-log-states)))))))
  11425. (defun org-get-todo-sequence-head (kwd)
  11426. "Return the head of the TODO sequence to which KWD belongs.
  11427. If KWD is not set, check if there is a text property remembering the
  11428. right sequence."
  11429. (let (p)
  11430. (cond
  11431. ((not kwd)
  11432. (or (get-text-property (point-at-bol) 'org-todo-head)
  11433. (progn
  11434. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11435. nil (point-at-eol)))
  11436. (get-text-property p 'org-todo-head))))
  11437. ((not (member kwd org-todo-keywords-1))
  11438. (car org-todo-keywords-1))
  11439. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11440. (defun org-fast-todo-selection ()
  11441. "Fast TODO keyword selection with single keys.
  11442. Returns the new TODO keyword, or nil if no state change should occur."
  11443. (let* ((fulltable org-todo-key-alist)
  11444. (done-keywords org-done-keywords) ;; needed for the faces.
  11445. (maxlen (apply 'max (mapcar
  11446. (lambda (x)
  11447. (if (stringp (car x)) (string-width (car x)) 0))
  11448. fulltable)))
  11449. (expert nil)
  11450. (fwidth (+ maxlen 3 1 3))
  11451. (ncol (/ (- (window-width) 4) fwidth))
  11452. tg cnt e c tbl
  11453. groups ingroup)
  11454. (save-excursion
  11455. (save-window-excursion
  11456. (if expert
  11457. (set-buffer (get-buffer-create " *Org todo*"))
  11458. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11459. (erase-buffer)
  11460. (org-set-local 'org-done-keywords done-keywords)
  11461. (setq tbl fulltable cnt 0)
  11462. (while (setq e (pop tbl))
  11463. (cond
  11464. ((equal e '(:startgroup))
  11465. (push '() groups) (setq ingroup t)
  11466. (when (not (= cnt 0))
  11467. (setq cnt 0)
  11468. (insert "\n"))
  11469. (insert "{ "))
  11470. ((equal e '(:endgroup))
  11471. (setq ingroup nil cnt 0)
  11472. (insert "}\n"))
  11473. ((equal e '(:newline))
  11474. (when (not (= cnt 0))
  11475. (setq cnt 0)
  11476. (insert "\n")
  11477. (setq e (car tbl))
  11478. (while (equal (car tbl) '(:newline))
  11479. (insert "\n")
  11480. (setq tbl (cdr tbl)))))
  11481. (t
  11482. (setq tg (car e) c (cdr e))
  11483. (if ingroup (push tg (car groups)))
  11484. (setq tg (org-add-props tg nil 'face
  11485. (org-get-todo-face tg)))
  11486. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11487. (insert "[" c "] " tg (make-string
  11488. (- fwidth 4 (length tg)) ?\ ))
  11489. (when (= (setq cnt (1+ cnt)) ncol)
  11490. (insert "\n")
  11491. (if ingroup (insert " "))
  11492. (setq cnt 0)))))
  11493. (insert "\n")
  11494. (goto-char (point-min))
  11495. (if (not expert) (org-fit-window-to-buffer))
  11496. (message "[a-z..]:Set [SPC]:clear")
  11497. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11498. (cond
  11499. ((or (= c ?\C-g)
  11500. (and (= c ?q) (not (rassoc c fulltable))))
  11501. (setq quit-flag t))
  11502. ((= c ?\ ) nil)
  11503. ((setq e (rassoc c fulltable) tg (car e))
  11504. tg)
  11505. (t (setq quit-flag t)))))))
  11506. (defun org-entry-is-todo-p ()
  11507. (member (org-get-todo-state) org-not-done-keywords))
  11508. (defun org-entry-is-done-p ()
  11509. (member (org-get-todo-state) org-done-keywords))
  11510. (defun org-get-todo-state ()
  11511. (save-excursion
  11512. (org-back-to-heading t)
  11513. (and (looking-at org-todo-line-regexp)
  11514. (match-end 2)
  11515. (match-string 2))))
  11516. (defun org-at-date-range-p (&optional inactive-ok)
  11517. "Is the cursor inside a date range?"
  11518. (interactive)
  11519. (save-excursion
  11520. (catch 'exit
  11521. (let ((pos (point)))
  11522. (skip-chars-backward "^[<\r\n")
  11523. (skip-chars-backward "<[")
  11524. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11525. (>= (match-end 0) pos)
  11526. (throw 'exit t))
  11527. (skip-chars-backward "^<[\r\n")
  11528. (skip-chars-backward "<[")
  11529. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11530. (>= (match-end 0) pos)
  11531. (throw 'exit t)))
  11532. nil)))
  11533. (defun org-get-repeat (&optional tagline)
  11534. "Check if there is a deadline/schedule with repeater in this entry."
  11535. (save-match-data
  11536. (save-excursion
  11537. (org-back-to-heading t)
  11538. (and (re-search-forward (if tagline
  11539. (concat tagline "\\s-*" org-repeat-re)
  11540. org-repeat-re)
  11541. (org-entry-end-position) t)
  11542. (match-string-no-properties 1)))))
  11543. (defvar org-last-changed-timestamp)
  11544. (defvar org-last-inserted-timestamp)
  11545. (defvar org-log-post-message)
  11546. (defvar org-log-note-purpose)
  11547. (defvar org-log-note-how)
  11548. (defvar org-log-note-extra)
  11549. (defun org-auto-repeat-maybe (done-word)
  11550. "Check if the current headline contains a repeated deadline/schedule.
  11551. If yes, set TODO state back to what it was and change the base date
  11552. of repeating deadline/scheduled time stamps to new date.
  11553. This function is run automatically after each state change to a DONE state."
  11554. ;; last-state is dynamically scoped into this function
  11555. (let* ((repeat (org-get-repeat))
  11556. (aa (assoc org-last-state org-todo-kwd-alist))
  11557. (interpret (nth 1 aa))
  11558. (head (nth 2 aa))
  11559. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11560. (msg "Entry repeats: ")
  11561. (org-log-done nil)
  11562. (org-todo-log-states nil)
  11563. re type n what ts time to-state)
  11564. (when repeat
  11565. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11566. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11567. org-todo-repeat-to-state))
  11568. (unless (and to-state (member to-state org-todo-keywords-1))
  11569. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11570. (org-todo to-state)
  11571. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11572. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11573. (org-time-stamp-format t t))))
  11574. (when org-log-repeat
  11575. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11576. (memq 'org-add-log-note post-command-hook))
  11577. ;; OK, we are already setup for some record
  11578. (if (eq org-log-repeat 'note)
  11579. ;; make sure we take a note, not only a time stamp
  11580. (setq org-log-note-how 'note))
  11581. ;; Set up for taking a record
  11582. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11583. org-last-state
  11584. 'findpos org-log-repeat)))
  11585. (org-back-to-heading t)
  11586. (org-add-planning-info nil nil 'closed)
  11587. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11588. org-deadline-time-regexp "\\)\\|\\("
  11589. org-ts-regexp "\\)"))
  11590. (while (re-search-forward
  11591. re (save-excursion (outline-next-heading) (point)) t)
  11592. (setq type (if (match-end 1) org-scheduled-string
  11593. (if (match-end 3) org-deadline-string "Plain:"))
  11594. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11595. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11596. (setq n (string-to-number (match-string 2 ts))
  11597. what (match-string 3 ts))
  11598. (if (equal what "w") (setq n (* n 7) what "d"))
  11599. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11600. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11601. ;; Preparation, see if we need to modify the start date for the change
  11602. (when (match-end 1)
  11603. (setq time (save-match-data (org-time-string-to-time ts)))
  11604. (cond
  11605. ((equal (match-string 1 ts) ".")
  11606. ;; Shift starting date to today
  11607. (org-timestamp-change
  11608. (- (org-today) (time-to-days time))
  11609. 'day))
  11610. ((equal (match-string 1 ts) "+")
  11611. (let ((nshiftmax 10) (nshift 0))
  11612. (while (or (= nshift 0)
  11613. (<= (time-to-days time)
  11614. (time-to-days (current-time))))
  11615. (when (= (incf nshift) nshiftmax)
  11616. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11617. (error "Abort")))
  11618. (org-timestamp-change n (cdr (assoc what whata)))
  11619. (org-at-timestamp-p t)
  11620. (setq ts (match-string 1))
  11621. (setq time (save-match-data (org-time-string-to-time ts)))))
  11622. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11623. ;; rematch, so that we have everything in place for the real shift
  11624. (org-at-timestamp-p t)
  11625. (setq ts (match-string 1))
  11626. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11627. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11628. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11629. (setq org-log-post-message msg)
  11630. (message "%s" msg))))
  11631. (defun org-show-todo-tree (arg)
  11632. "Make a compact tree which shows all headlines marked with TODO.
  11633. The tree will show the lines where the regexp matches, and all higher
  11634. headlines above the match.
  11635. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11636. With a numeric prefix N, construct a sparse tree for the Nth element
  11637. of `org-todo-keywords-1'."
  11638. (interactive "P")
  11639. (let ((case-fold-search nil)
  11640. (kwd-re
  11641. (cond ((null arg) org-not-done-regexp)
  11642. ((equal arg '(4))
  11643. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11644. (mapcar 'list org-todo-keywords-1))))
  11645. (concat "\\("
  11646. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11647. "\\)\\>")))
  11648. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11649. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11650. org-todo-keywords-1)))
  11651. (t (error "Invalid prefix argument: %s" arg)))))
  11652. (message "%d TODO entries found"
  11653. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11654. (defun org-deadline (arg &optional time)
  11655. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11656. With one universal prefix argument, remove any deadline from the item.
  11657. With two universal prefix arguments, prompt for a warning delay.
  11658. With argument TIME, set the deadline at the corresponding date. TIME
  11659. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11660. (interactive "P")
  11661. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11662. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11663. 'region-start-level 'region))
  11664. org-loop-over-headlines-in-active-region)
  11665. (org-map-entries
  11666. `(org-deadline ',arg ,time)
  11667. org-loop-over-headlines-in-active-region
  11668. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11669. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11670. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11671. (repeater (and old-date
  11672. (string-match
  11673. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11674. old-date)
  11675. (match-string 1 old-date))))
  11676. (cond
  11677. ((equal arg '(4))
  11678. (when (and old-date org-log-redeadline)
  11679. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11680. org-log-redeadline))
  11681. (org-remove-timestamp-with-keyword org-deadline-string)
  11682. (message "Item no longer has a deadline."))
  11683. ((equal arg '(16))
  11684. (save-excursion
  11685. (if (re-search-forward
  11686. org-deadline-time-regexp
  11687. (save-excursion (outline-next-heading) (point)) t)
  11688. (let* ((rpl0 (match-string 1))
  11689. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11690. (replace-match
  11691. (concat org-deadline-string
  11692. " <" rpl
  11693. (format " -%dd"
  11694. (abs
  11695. (- (time-to-days
  11696. (save-match-data
  11697. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11698. (time-to-days old-date-time))))
  11699. ">") t t))
  11700. (user-error "No deadline information to update"))))
  11701. (t
  11702. (org-add-planning-info 'deadline time 'closed)
  11703. (when (and old-date org-log-redeadline
  11704. (not (equal old-date
  11705. (substring org-last-inserted-timestamp 1 -1))))
  11706. (org-add-log-setup 'redeadline nil old-date 'findpos
  11707. org-log-redeadline))
  11708. (when repeater
  11709. (save-excursion
  11710. (org-back-to-heading t)
  11711. (when (re-search-forward (concat org-deadline-string " "
  11712. org-last-inserted-timestamp)
  11713. (save-excursion
  11714. (outline-next-heading) (point)) t)
  11715. (goto-char (1- (match-end 0)))
  11716. (insert " " repeater)
  11717. (setq org-last-inserted-timestamp
  11718. (concat (substring org-last-inserted-timestamp 0 -1)
  11719. " " repeater
  11720. (substring org-last-inserted-timestamp -1))))))
  11721. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11722. (defun org-schedule (arg &optional time)
  11723. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11724. With one universal prefix argument, remove any scheduling date from the item.
  11725. With two universal prefix arguments, prompt for a delay cookie.
  11726. With argument TIME, scheduled at the corresponding date. TIME can
  11727. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11728. (interactive "P")
  11729. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11730. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11731. 'region-start-level 'region))
  11732. org-loop-over-headlines-in-active-region)
  11733. (org-map-entries
  11734. `(org-schedule ',arg ,time)
  11735. org-loop-over-headlines-in-active-region
  11736. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11737. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11738. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11739. (repeater (and old-date
  11740. (string-match
  11741. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11742. old-date)
  11743. (match-string 1 old-date))))
  11744. (cond
  11745. ((equal arg '(4))
  11746. (progn
  11747. (when (and old-date org-log-reschedule)
  11748. (org-add-log-setup 'delschedule nil old-date 'findpos
  11749. org-log-reschedule))
  11750. (org-remove-timestamp-with-keyword org-scheduled-string)
  11751. (message "Item is no longer scheduled.")))
  11752. ((equal arg '(16))
  11753. (save-excursion
  11754. (if (re-search-forward
  11755. org-scheduled-time-regexp
  11756. (save-excursion (outline-next-heading) (point)) t)
  11757. (let* ((rpl0 (match-string 1))
  11758. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11759. (replace-match
  11760. (concat org-scheduled-string
  11761. " <" rpl
  11762. (format " -%dd"
  11763. (abs
  11764. (- (time-to-days
  11765. (save-match-data
  11766. (org-read-date nil t nil "Delay until" old-date-time)))
  11767. (time-to-days old-date-time))))
  11768. ">") t t))
  11769. (user-error "No scheduled information to update"))))
  11770. (t
  11771. (org-add-planning-info 'scheduled time 'closed)
  11772. (when (and old-date org-log-reschedule
  11773. (not (equal old-date
  11774. (substring org-last-inserted-timestamp 1 -1))))
  11775. (org-add-log-setup 'reschedule nil old-date 'findpos
  11776. org-log-reschedule))
  11777. (when repeater
  11778. (save-excursion
  11779. (org-back-to-heading t)
  11780. (when (re-search-forward (concat org-scheduled-string " "
  11781. org-last-inserted-timestamp)
  11782. (save-excursion
  11783. (outline-next-heading) (point)) t)
  11784. (goto-char (1- (match-end 0)))
  11785. (insert " " repeater)
  11786. (setq org-last-inserted-timestamp
  11787. (concat (substring org-last-inserted-timestamp 0 -1)
  11788. " " repeater
  11789. (substring org-last-inserted-timestamp -1))))))
  11790. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11791. (defun org-get-scheduled-time (pom &optional inherit)
  11792. "Get the scheduled time as a time tuple, of a format suitable
  11793. for calling org-schedule with, or if there is no scheduling,
  11794. returns nil."
  11795. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11796. (when time
  11797. (apply 'encode-time (org-parse-time-string time)))))
  11798. (defun org-get-deadline-time (pom &optional inherit)
  11799. "Get the deadline as a time tuple, of a format suitable for
  11800. calling org-deadline with, or if there is no scheduling, returns
  11801. nil."
  11802. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11803. (when time
  11804. (apply 'encode-time (org-parse-time-string time)))))
  11805. (defun org-remove-timestamp-with-keyword (keyword)
  11806. "Remove all time stamps with KEYWORD in the current entry."
  11807. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11808. beg)
  11809. (save-excursion
  11810. (org-back-to-heading t)
  11811. (setq beg (point))
  11812. (outline-next-heading)
  11813. (while (re-search-backward re beg t)
  11814. (replace-match "")
  11815. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11816. (equal (char-before) ?\ ))
  11817. (backward-delete-char 1)
  11818. (if (string-match "^[ \t]*$" (buffer-substring
  11819. (point-at-bol) (point-at-eol)))
  11820. (delete-region (point-at-bol)
  11821. (min (point-max) (1+ (point-at-eol))))))))))
  11822. (defun org-add-planning-info (what &optional time &rest remove)
  11823. "Insert new timestamp with keyword in the line directly after the headline.
  11824. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11825. If non is given, the user is prompted for a date.
  11826. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11827. be removed."
  11828. (interactive)
  11829. (let (org-time-was-given org-end-time-was-given ts
  11830. end default-time default-input)
  11831. (catch 'exit
  11832. (when (and (memq what '(scheduled deadline))
  11833. (or (not time)
  11834. (and (stringp time)
  11835. (string-match "^[-+]+[0-9]" time))))
  11836. ;; Try to get a default date/time from existing timestamp
  11837. (save-excursion
  11838. (org-back-to-heading t)
  11839. (setq end (save-excursion (outline-next-heading) (point)))
  11840. (when (re-search-forward (if (eq what 'scheduled)
  11841. org-scheduled-time-regexp
  11842. org-deadline-time-regexp)
  11843. end t)
  11844. (setq ts (match-string 1)
  11845. default-time
  11846. (apply 'encode-time (org-parse-time-string ts))
  11847. default-input (and ts (org-get-compact-tod ts))))))
  11848. (when what
  11849. (setq time
  11850. (if (stringp time)
  11851. ;; This is a string (relative or absolute), set proper date
  11852. (apply 'encode-time
  11853. (org-read-date-analyze
  11854. time default-time (decode-time default-time)))
  11855. ;; If necessary, get the time from the user
  11856. (or time (org-read-date nil 'to-time nil nil
  11857. default-time default-input)))))
  11858. (when (and org-insert-labeled-timestamps-at-point
  11859. (member what '(scheduled deadline)))
  11860. (insert
  11861. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11862. (org-insert-time-stamp time org-time-was-given
  11863. nil nil nil (list org-end-time-was-given))
  11864. (setq what nil))
  11865. (save-excursion
  11866. (save-restriction
  11867. (let (col list elt ts buffer-invisibility-spec)
  11868. (org-back-to-heading t)
  11869. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11870. (goto-char (match-end 1))
  11871. (setq col (current-column))
  11872. (goto-char (match-end 0))
  11873. (if (eobp) (insert "\n") (forward-char 1))
  11874. (when (and (not what)
  11875. (not (looking-at
  11876. (concat "[ \t]*"
  11877. org-keyword-time-not-clock-regexp))))
  11878. ;; Nothing to add, nothing to remove...... :-)
  11879. (throw 'exit nil))
  11880. (if (and (not (looking-at org-outline-regexp))
  11881. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11882. "[^\r\n]*"))
  11883. (not (equal (match-string 1) org-clock-string)))
  11884. (narrow-to-region (match-beginning 0) (match-end 0))
  11885. (insert-before-markers "\n")
  11886. (backward-char 1)
  11887. (narrow-to-region (point) (point))
  11888. (and org-adapt-indentation (org-indent-to-column col)))
  11889. ;; Check if we have to remove something.
  11890. (setq list (cons what remove))
  11891. (while list
  11892. (setq elt (pop list))
  11893. (when (or (and (eq elt 'scheduled)
  11894. (re-search-forward org-scheduled-time-regexp nil t))
  11895. (and (eq elt 'deadline)
  11896. (re-search-forward org-deadline-time-regexp nil t))
  11897. (and (eq elt 'closed)
  11898. (re-search-forward org-closed-time-regexp nil t)))
  11899. (replace-match "")
  11900. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11901. (and (looking-at "[ \t]+") (replace-match ""))
  11902. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11903. (when what
  11904. (insert
  11905. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11906. (cond ((eq what 'scheduled) org-scheduled-string)
  11907. ((eq what 'deadline) org-deadline-string)
  11908. ((eq what 'closed) org-closed-string))
  11909. " ")
  11910. (setq ts (org-insert-time-stamp
  11911. time
  11912. (or org-time-was-given
  11913. (and (eq what 'closed) org-log-done-with-time))
  11914. (eq what 'closed)
  11915. nil nil (list org-end-time-was-given)))
  11916. (insert
  11917. (if (not (or (bolp) (eq (char-before) ?\ )
  11918. (memq (char-after) '(32 10))
  11919. (eobp))) " " ""))
  11920. (end-of-line 1))
  11921. (goto-char (point-min))
  11922. (widen)
  11923. (if (and (looking-at "[ \t]*\n")
  11924. (equal (char-before) ?\n))
  11925. (delete-region (1- (point)) (point-at-eol)))
  11926. ts))))))
  11927. (defvar org-log-note-marker (make-marker))
  11928. (defvar org-log-note-purpose nil)
  11929. (defvar org-log-note-state nil)
  11930. (defvar org-log-note-previous-state nil)
  11931. (defvar org-log-note-how nil)
  11932. (defvar org-log-note-extra nil)
  11933. (defvar org-log-note-window-configuration nil)
  11934. (defvar org-log-note-return-to (make-marker))
  11935. (defvar org-log-note-effective-time nil
  11936. "Remembered current time so that dynamically scoped
  11937. `org-extend-today-until' affects tha timestamps in state change
  11938. log")
  11939. (defvar org-log-post-message nil
  11940. "Message to be displayed after a log note has been stored.
  11941. The auto-repeater uses this.")
  11942. (defun org-add-note ()
  11943. "Add a note to the current entry.
  11944. This is done in the same way as adding a state change note."
  11945. (interactive)
  11946. (org-add-log-setup 'note nil nil 'findpos nil))
  11947. (defvar org-property-end-re)
  11948. (defun org-add-log-setup (&optional purpose state prev-state
  11949. findpos how extra)
  11950. "Set up the post command hook to take a note.
  11951. If this is about to TODO state change, the new state is expected in STATE.
  11952. When FINDPOS is non-nil, find the correct position for the note in
  11953. the current entry. If not, assume that it can be inserted at point.
  11954. HOW is an indicator what kind of note should be created.
  11955. EXTRA is additional text that will be inserted into the notes buffer."
  11956. (let* ((org-log-into-drawer (org-log-into-drawer))
  11957. (drawer (cond ((stringp org-log-into-drawer)
  11958. org-log-into-drawer)
  11959. (org-log-into-drawer "LOGBOOK"))))
  11960. (save-restriction
  11961. (save-excursion
  11962. (when findpos
  11963. (org-back-to-heading t)
  11964. (narrow-to-region (point) (save-excursion
  11965. (outline-next-heading) (point)))
  11966. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11967. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11968. "[^\r\n]*\\)?"))
  11969. (goto-char (match-end 0))
  11970. (cond
  11971. (drawer
  11972. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11973. nil t)
  11974. (progn
  11975. (goto-char (match-end 0))
  11976. (or org-log-states-order-reversed
  11977. (and (re-search-forward org-property-end-re nil t)
  11978. (goto-char (1- (match-beginning 0))))))
  11979. (insert "\n:" drawer ":\n:END:")
  11980. (beginning-of-line 0)
  11981. (org-indent-line)
  11982. (beginning-of-line 2)
  11983. (org-indent-line)
  11984. (end-of-line 0)))
  11985. ((and org-log-state-notes-insert-after-drawers
  11986. (save-excursion
  11987. (forward-line) (looking-at org-drawer-regexp)))
  11988. (forward-line)
  11989. (while (looking-at org-drawer-regexp)
  11990. (goto-char (match-end 0))
  11991. (re-search-forward org-property-end-re (point-max) t)
  11992. (forward-line))
  11993. (forward-line -1)))
  11994. (unless org-log-states-order-reversed
  11995. (and (= (char-after) ?\n) (forward-char 1))
  11996. (org-skip-over-state-notes)
  11997. (skip-chars-backward " \t\n\r")))
  11998. (move-marker org-log-note-marker (point))
  11999. (setq org-log-note-purpose purpose
  12000. org-log-note-state state
  12001. org-log-note-previous-state prev-state
  12002. org-log-note-how how
  12003. org-log-note-extra extra
  12004. org-log-note-effective-time (org-current-effective-time))
  12005. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12006. (defun org-skip-over-state-notes ()
  12007. "Skip past the list of State notes in an entry."
  12008. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12009. (when (ignore-errors (goto-char (org-in-item-p)))
  12010. (let* ((struct (org-list-struct))
  12011. (prevs (org-list-prevs-alist struct)))
  12012. (while (looking-at "[ \t]*- State")
  12013. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12014. (org-list-get-item-end (point) struct)))))))
  12015. (defun org-add-log-note (&optional purpose)
  12016. "Pop up a window for taking a note, and add this note later at point."
  12017. (remove-hook 'post-command-hook 'org-add-log-note)
  12018. (setq org-log-note-window-configuration (current-window-configuration))
  12019. (delete-other-windows)
  12020. (move-marker org-log-note-return-to (point))
  12021. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12022. (goto-char org-log-note-marker)
  12023. (org-switch-to-buffer-other-window "*Org Note*")
  12024. (erase-buffer)
  12025. (if (memq org-log-note-how '(time state))
  12026. (let (current-prefix-arg) (org-store-log-note))
  12027. (let ((org-inhibit-startup t)) (org-mode))
  12028. (insert (format "# Insert note for %s.
  12029. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12030. (cond
  12031. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12032. ((eq org-log-note-purpose 'done) "closed todo item")
  12033. ((eq org-log-note-purpose 'state)
  12034. (format "state change from \"%s\" to \"%s\""
  12035. (or org-log-note-previous-state "")
  12036. (or org-log-note-state "")))
  12037. ((eq org-log-note-purpose 'reschedule)
  12038. "rescheduling")
  12039. ((eq org-log-note-purpose 'delschedule)
  12040. "no longer scheduled")
  12041. ((eq org-log-note-purpose 'redeadline)
  12042. "changing deadline")
  12043. ((eq org-log-note-purpose 'deldeadline)
  12044. "removing deadline")
  12045. ((eq org-log-note-purpose 'refile)
  12046. "refiling")
  12047. ((eq org-log-note-purpose 'note)
  12048. "this entry")
  12049. (t (error "This should not happen")))))
  12050. (if org-log-note-extra (insert org-log-note-extra))
  12051. (org-set-local 'org-finish-function 'org-store-log-note)
  12052. (run-hooks 'org-log-buffer-setup-hook)))
  12053. (defvar org-note-abort nil) ; dynamically scoped
  12054. (defun org-store-log-note ()
  12055. "Finish taking a log note, and insert it to where it belongs."
  12056. (let ((txt (buffer-string)))
  12057. (kill-buffer (current-buffer))
  12058. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12059. lines ind bul)
  12060. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12061. (setq txt (replace-match "" t t txt)))
  12062. (if (string-match "\\s-+\\'" txt)
  12063. (setq txt (replace-match "" t t txt)))
  12064. (setq lines (org-split-string txt "\n"))
  12065. (when (and note (string-match "\\S-" note))
  12066. (setq note
  12067. (org-replace-escapes
  12068. note
  12069. (list (cons "%u" (user-login-name))
  12070. (cons "%U" user-full-name)
  12071. (cons "%t" (format-time-string
  12072. (org-time-stamp-format 'long 'inactive)
  12073. org-log-note-effective-time))
  12074. (cons "%T" (format-time-string
  12075. (org-time-stamp-format 'long nil)
  12076. org-log-note-effective-time))
  12077. (cons "%d" (format-time-string
  12078. (org-time-stamp-format nil 'inactive)
  12079. org-log-note-effective-time))
  12080. (cons "%D" (format-time-string
  12081. (org-time-stamp-format nil nil)
  12082. org-log-note-effective-time))
  12083. (cons "%s" (if org-log-note-state
  12084. (concat "\"" org-log-note-state "\"")
  12085. ""))
  12086. (cons "%S" (if org-log-note-previous-state
  12087. (concat "\"" org-log-note-previous-state "\"")
  12088. "\"\"")))))
  12089. (if lines (setq note (concat note " \\\\")))
  12090. (push note lines))
  12091. (when (or current-prefix-arg org-note-abort)
  12092. (when org-log-into-drawer
  12093. (org-remove-empty-drawer-at
  12094. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12095. org-log-note-marker))
  12096. (setq lines nil))
  12097. (when lines
  12098. (with-current-buffer (marker-buffer org-log-note-marker)
  12099. (save-excursion
  12100. (goto-char org-log-note-marker)
  12101. (move-marker org-log-note-marker nil)
  12102. (end-of-line 1)
  12103. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12104. (setq ind (save-excursion
  12105. (if (ignore-errors (goto-char (org-in-item-p)))
  12106. (let ((struct (org-list-struct)))
  12107. (org-list-get-ind
  12108. (org-list-get-top-point struct) struct))
  12109. (skip-chars-backward " \r\t\n")
  12110. (cond
  12111. ((and (org-at-heading-p)
  12112. org-adapt-indentation)
  12113. (1+ (org-current-level)))
  12114. ((org-at-heading-p) 0)
  12115. (t (org-get-indentation))))))
  12116. (setq bul (org-list-bullet-string "-"))
  12117. (org-indent-line-to ind)
  12118. (insert bul (pop lines))
  12119. (let ((ind-body (+ (length bul) ind)))
  12120. (while lines
  12121. (insert "\n")
  12122. (org-indent-line-to ind-body)
  12123. (insert (pop lines))))
  12124. (message "Note stored")
  12125. (org-back-to-heading t)
  12126. (org-cycle-hide-drawers 'children))))))
  12127. (set-window-configuration org-log-note-window-configuration)
  12128. (with-current-buffer (marker-buffer org-log-note-return-to)
  12129. (goto-char org-log-note-return-to))
  12130. (move-marker org-log-note-return-to nil)
  12131. (and org-log-post-message (message "%s" org-log-post-message)))
  12132. (defun org-remove-empty-drawer-at (drawer pos)
  12133. "Remove an empty drawer DRAWER at position POS.
  12134. POS may also be a marker."
  12135. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12136. (save-excursion
  12137. (save-restriction
  12138. (widen)
  12139. (goto-char pos)
  12140. (if (org-in-regexp
  12141. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12142. (replace-match ""))))))
  12143. (defvar org-ts-type nil)
  12144. (defun org-sparse-tree (&optional arg type)
  12145. "Create a sparse tree, prompt for the details.
  12146. This command can create sparse trees. You first need to select the type
  12147. of match used to create the tree:
  12148. t Show all TODO entries.
  12149. T Show entries with a specific TODO keyword.
  12150. m Show entries selected by a tags/property match.
  12151. p Enter a property name and its value (both with completion on existing
  12152. names/values) and show entries with that property.
  12153. r Show entries matching a regular expression (`/' can be used as well).
  12154. b Show deadlines and scheduled items before a date.
  12155. a Show deadlines and scheduled items after a date.
  12156. d Show deadlines due within `org-deadline-warning-days'.
  12157. D Show deadlines and scheduled items between a date range."
  12158. (interactive "P")
  12159. (let (ans kwd value ts-type)
  12160. (setq type (or type org-sparse-tree-default-date-type))
  12161. (setq org-ts-type type)
  12162. (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"
  12163. (cond ((eq type 'all) "all timestamps")
  12164. ((eq type 'scheduled) "only scheduled")
  12165. ((eq type 'deadline) "only deadline")
  12166. ((eq type 'active) "only active timestamps")
  12167. ((eq type 'inactive) "only inactive timestamps")
  12168. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12169. ((eq type 'closed) "with a closed time-stamp")
  12170. (t "scheduled/deadline")))
  12171. (setq ans (read-char-exclusive))
  12172. (cond
  12173. ((equal ans ?c)
  12174. (org-sparse-tree
  12175. arg (cadr (member type '(scheduled-or-deadline
  12176. all scheduled deadline active inactive closed)))))
  12177. ((equal ans ?d)
  12178. (call-interactively 'org-check-deadlines))
  12179. ((equal ans ?b)
  12180. (call-interactively 'org-check-before-date))
  12181. ((equal ans ?a)
  12182. (call-interactively 'org-check-after-date))
  12183. ((equal ans ?D)
  12184. (call-interactively 'org-check-dates-range))
  12185. ((equal ans ?t)
  12186. (call-interactively 'org-show-todo-tree))
  12187. ((equal ans ?T)
  12188. (org-show-todo-tree '(4)))
  12189. ((member ans '(?T ?m))
  12190. (call-interactively 'org-match-sparse-tree))
  12191. ((member ans '(?p ?P))
  12192. (setq kwd (org-icompleting-read "Property: "
  12193. (mapcar 'list (org-buffer-property-keys))))
  12194. (setq value (org-icompleting-read "Value: "
  12195. (mapcar 'list (org-property-values kwd))))
  12196. (unless (string-match "\\`{.*}\\'" value)
  12197. (setq value (concat "\"" value "\"")))
  12198. (org-match-sparse-tree arg (concat kwd "=" value)))
  12199. ((member ans '(?r ?R ?/))
  12200. (call-interactively 'org-occur))
  12201. (t (error "No such sparse tree command \"%c\"" ans)))))
  12202. (defvar org-occur-highlights nil
  12203. "List of overlays used for occur matches.")
  12204. (make-variable-buffer-local 'org-occur-highlights)
  12205. (defvar org-occur-parameters nil
  12206. "Parameters of the active org-occur calls.
  12207. This is a list, each call to org-occur pushes as cons cell,
  12208. containing the regular expression and the callback, onto the list.
  12209. The list can contain several entries if `org-occur' has been called
  12210. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12211. will only contain one set of parameters. When the highlights are
  12212. removed (for example with `C-c C-c', or with the next edit (depending
  12213. on `org-remove-highlights-with-change'), this variable is emptied
  12214. as well.")
  12215. (make-variable-buffer-local 'org-occur-parameters)
  12216. (defun org-occur (regexp &optional keep-previous callback)
  12217. "Make a compact tree which shows all matches of REGEXP.
  12218. The tree will show the lines where the regexp matches, and all higher
  12219. headlines above the match. It will also show the heading after the match,
  12220. to make sure editing the matching entry is easy.
  12221. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12222. call to `org-occur' will be kept, to allow stacking of calls to this
  12223. command.
  12224. If CALLBACK is non-nil, it is a function which is called to confirm
  12225. that the match should indeed be shown."
  12226. (interactive "sRegexp: \nP")
  12227. (when (equal regexp "")
  12228. (error "Regexp cannot be empty"))
  12229. (unless keep-previous
  12230. (org-remove-occur-highlights nil nil t))
  12231. (push (cons regexp callback) org-occur-parameters)
  12232. (let ((cnt 0))
  12233. (save-excursion
  12234. (goto-char (point-min))
  12235. (if (or (not keep-previous) ; do not want to keep
  12236. (not org-occur-highlights)) ; no previous matches
  12237. ;; hide everything
  12238. (org-overview))
  12239. (while (re-search-forward regexp nil t)
  12240. (when (or (not callback)
  12241. (save-match-data (funcall callback)))
  12242. (setq cnt (1+ cnt))
  12243. (when org-highlight-sparse-tree-matches
  12244. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12245. (org-show-context 'occur-tree))))
  12246. (when org-remove-highlights-with-change
  12247. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12248. nil 'local))
  12249. (unless org-sparse-tree-open-archived-trees
  12250. (org-hide-archived-subtrees (point-min) (point-max)))
  12251. (run-hooks 'org-occur-hook)
  12252. (if (org-called-interactively-p 'interactive)
  12253. (message "%d match(es) for regexp %s" cnt regexp))
  12254. cnt))
  12255. (defun org-occur-next-match (&optional n reset)
  12256. "Function for `next-error-function' to find sparse tree matches.
  12257. N is the number of matches to move, when negative move backwards.
  12258. RESET is entirely ignored - this function always goes back to the
  12259. starting point when no match is found."
  12260. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12261. (search-func (if (< n 0)
  12262. 'previous-single-char-property-change
  12263. 'next-single-char-property-change))
  12264. (n (abs n))
  12265. (pos (point))
  12266. p1)
  12267. (catch 'exit
  12268. (while (setq p1 (funcall search-func (point) 'org-type))
  12269. (when (equal p1 limit)
  12270. (goto-char pos)
  12271. (error "No more matches"))
  12272. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12273. (setq n (1- n))
  12274. (when (= n 0)
  12275. (goto-char p1)
  12276. (throw 'exit (point))))
  12277. (goto-char p1))
  12278. (goto-char p1)
  12279. (error "No more matches"))))
  12280. (defun org-show-context (&optional key)
  12281. "Make sure point and context are visible.
  12282. How much context is shown depends upon the variables
  12283. `org-show-hierarchy-above', `org-show-following-heading',
  12284. `org-show-entry-below' and `org-show-siblings'."
  12285. (let ((heading-p (org-at-heading-p t))
  12286. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12287. (following-p (org-get-alist-option org-show-following-heading key))
  12288. (entry-p (org-get-alist-option org-show-entry-below key))
  12289. (siblings-p (org-get-alist-option org-show-siblings key)))
  12290. ;; Show heading or entry text
  12291. (if (and heading-p (not entry-p))
  12292. (org-flag-heading nil) ; only show the heading
  12293. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12294. (org-show-hidden-entry))) ; show entire entry
  12295. (when following-p
  12296. ;; Show next sibling, or heading below text
  12297. (save-excursion
  12298. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12299. (org-flag-heading nil))))
  12300. (when siblings-p (org-show-siblings))
  12301. (when hierarchy-p
  12302. ;; show all higher headings, possibly with siblings
  12303. (save-excursion
  12304. (while (and (condition-case nil
  12305. (progn (org-up-heading-all 1) t)
  12306. (error nil))
  12307. (not (bobp)))
  12308. (org-flag-heading nil)
  12309. (when siblings-p (org-show-siblings)))))
  12310. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12311. (defvar org-reveal-start-hook nil
  12312. "Hook run before revealing a location.")
  12313. (defun org-reveal (&optional siblings)
  12314. "Show current entry, hierarchy above it, and the following headline.
  12315. This can be used to show a consistent set of context around locations
  12316. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12317. not t for the search context.
  12318. With optional argument SIBLINGS, on each level of the hierarchy all
  12319. siblings are shown. This repairs the tree structure to what it would
  12320. look like when opened with hierarchical calls to `org-cycle'.
  12321. With double optional argument \\[universal-argument] \\[universal-argument], \
  12322. go to the parent and show the
  12323. entire tree."
  12324. (interactive "P")
  12325. (run-hooks 'org-reveal-start-hook)
  12326. (let ((org-show-hierarchy-above t)
  12327. (org-show-following-heading t)
  12328. (org-show-siblings (if siblings t org-show-siblings)))
  12329. (org-show-context nil))
  12330. (when (equal siblings '(16))
  12331. (save-excursion
  12332. (when (org-up-heading-safe)
  12333. (org-show-subtree)
  12334. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12335. (defun org-highlight-new-match (beg end)
  12336. "Highlight from BEG to END and mark the highlight is an occur headline."
  12337. (let ((ov (make-overlay beg end)))
  12338. (overlay-put ov 'face 'secondary-selection)
  12339. (overlay-put ov 'org-type 'org-occur)
  12340. (push ov org-occur-highlights)))
  12341. (defun org-remove-occur-highlights (&optional beg end noremove)
  12342. "Remove the occur highlights from the buffer.
  12343. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12344. from the `before-change-functions' in the current buffer."
  12345. (interactive)
  12346. (unless org-inhibit-highlight-removal
  12347. (mapc 'delete-overlay org-occur-highlights)
  12348. (setq org-occur-highlights nil)
  12349. (setq org-occur-parameters nil)
  12350. (unless noremove
  12351. (remove-hook 'before-change-functions
  12352. 'org-remove-occur-highlights 'local))))
  12353. ;;;; Priorities
  12354. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12355. "Regular expression matching the priority indicator.")
  12356. (defvar org-remove-priority-next-time nil)
  12357. (defun org-priority-up ()
  12358. "Increase the priority of the current item."
  12359. (interactive)
  12360. (org-priority 'up))
  12361. (defun org-priority-down ()
  12362. "Decrease the priority of the current item."
  12363. (interactive)
  12364. (org-priority 'down))
  12365. (defun org-priority (&optional action show)
  12366. "Change the priority of an item.
  12367. ACTION can be `set', `up', `down', or a character."
  12368. (interactive "P")
  12369. (if (equal action '(4))
  12370. (org-show-priority)
  12371. (unless org-enable-priority-commands
  12372. (error "Priority commands are disabled"))
  12373. (setq action (or action 'set))
  12374. (let (current new news have remove)
  12375. (save-excursion
  12376. (org-back-to-heading t)
  12377. (if (looking-at org-priority-regexp)
  12378. (setq current (string-to-char (match-string 2))
  12379. have t))
  12380. (cond
  12381. ((eq action 'remove)
  12382. (setq remove t new ?\ ))
  12383. ((or (eq action 'set)
  12384. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12385. (if (not (eq action 'set))
  12386. (setq new action)
  12387. (message "Priority %c-%c, SPC to remove: "
  12388. org-highest-priority org-lowest-priority)
  12389. (save-match-data
  12390. (setq new (read-char-exclusive))))
  12391. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12392. (= (upcase org-lowest-priority) org-lowest-priority))
  12393. (setq new (upcase new)))
  12394. (cond ((equal new ?\ ) (setq remove t))
  12395. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12396. (error "Priority must be between `%c' and `%c'"
  12397. org-highest-priority org-lowest-priority))))
  12398. ((eq action 'up)
  12399. (setq new (if have
  12400. (1- current) ; normal cycling
  12401. ;; last priority was empty
  12402. (if (eq last-command this-command)
  12403. org-lowest-priority ; wrap around empty to lowest
  12404. ;; default
  12405. (if org-priority-start-cycle-with-default
  12406. org-default-priority
  12407. (1- org-default-priority))))))
  12408. ((eq action 'down)
  12409. (setq new (if have
  12410. (1+ current) ; normal cycling
  12411. ;; last priority was empty
  12412. (if (eq last-command this-command)
  12413. org-highest-priority ; wrap around empty to highest
  12414. ;; default
  12415. (if org-priority-start-cycle-with-default
  12416. org-default-priority
  12417. (1+ org-default-priority))))))
  12418. (t (error "Invalid action")))
  12419. (if (or (< (upcase new) org-highest-priority)
  12420. (> (upcase new) org-lowest-priority))
  12421. (if (and (memq action '(up down))
  12422. (not have) (not (eq last-command this-command)))
  12423. ;; `new' is from default priority
  12424. (error
  12425. "The default can not be set, see `org-default-priority' why")
  12426. ;; normal cycling: `new' is beyond highest/lowest priority
  12427. ;; and is wrapped around to the empty priority
  12428. (setq remove t)))
  12429. (setq news (format "%c" new))
  12430. (if have
  12431. (if remove
  12432. (replace-match "" t t nil 1)
  12433. (replace-match news t t nil 2))
  12434. (if remove
  12435. (error "No priority cookie found in line")
  12436. (let ((case-fold-search nil))
  12437. (looking-at org-todo-line-regexp))
  12438. (if (match-end 2)
  12439. (progn
  12440. (goto-char (match-end 2))
  12441. (insert " [#" news "]"))
  12442. (goto-char (match-beginning 3))
  12443. (insert "[#" news "] "))))
  12444. (org-preserve-lc (org-set-tags nil 'align)))
  12445. (if remove
  12446. (message "Priority removed")
  12447. (message "Priority of current item set to %s" news)))))
  12448. (defun org-show-priority ()
  12449. "Show the priority of the current item.
  12450. This priority is composed of the main priority given with the [#A] cookies,
  12451. and by additional input from the age of a schedules or deadline entry."
  12452. (interactive)
  12453. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12454. (org-get-at-bol 'priority)
  12455. (save-excursion
  12456. (save-match-data
  12457. (beginning-of-line)
  12458. (and (looking-at org-heading-regexp)
  12459. (org-get-priority (match-string 0))))))))
  12460. (message "Priority is %d" (if pri pri -1000))))
  12461. (defun org-get-priority (s)
  12462. "Find priority cookie and return priority."
  12463. (save-match-data
  12464. (if (functionp org-get-priority-function)
  12465. (funcall org-get-priority-function)
  12466. (if (not (string-match org-priority-regexp s))
  12467. (* 1000 (- org-lowest-priority org-default-priority))
  12468. (* 1000 (- org-lowest-priority
  12469. (string-to-char (match-string 2 s))))))))
  12470. ;;;; Tags
  12471. (defvar org-agenda-archives-mode)
  12472. (defvar org-map-continue-from nil
  12473. "Position from where mapping should continue.
  12474. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12475. (defvar org-scanner-tags nil
  12476. "The current tag list while the tags scanner is running.")
  12477. (defvar org-trust-scanner-tags nil
  12478. "Should `org-get-tags-at' use the tags for the scanner.
  12479. This is for internal dynamical scoping only.
  12480. When this is non-nil, the function `org-get-tags-at' will return the value
  12481. of `org-scanner-tags' instead of building the list by itself. This
  12482. can lead to large speed-ups when the tags scanner is used in a file with
  12483. many entries, and when the list of tags is retrieved, for example to
  12484. obtain a list of properties. Building the tags list for each entry in such
  12485. a file becomes an N^2 operation - but with this variable set, it scales
  12486. as N.")
  12487. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12488. "Sca headline tags with inheritance and produce output ACTION.
  12489. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12490. or `agenda' to produce an entry list for an agenda view. It can also be
  12491. a Lisp form or a function that should be called at each matched headline, in
  12492. this case the return value is a list of all return values from these calls.
  12493. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12494. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12495. only lines with a not-done TODO keyword are included in the output.
  12496. This should be the same variable that was scoped into
  12497. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12498. START-LEVEL can be a string with asterisks, reducing the scope to
  12499. headlines matching this string."
  12500. (require 'org-agenda)
  12501. (let* ((re (concat "^"
  12502. (if start-level
  12503. ;; Get the correct level to match
  12504. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12505. org-outline-regexp)
  12506. " *\\(\\<\\("
  12507. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12508. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12509. (props (list 'face 'default
  12510. 'done-face 'org-agenda-done
  12511. 'undone-face 'default
  12512. 'mouse-face 'highlight
  12513. 'org-not-done-regexp org-not-done-regexp
  12514. 'org-todo-regexp org-todo-regexp
  12515. 'org-complex-heading-regexp org-complex-heading-regexp
  12516. 'help-echo
  12517. (format "mouse-2 or RET jump to org file %s"
  12518. (abbreviate-file-name
  12519. (or (buffer-file-name (buffer-base-buffer))
  12520. (buffer-name (buffer-base-buffer)))))))
  12521. (case-fold-search nil)
  12522. (org-map-continue-from nil)
  12523. lspos tags tags-list
  12524. (tags-alist (list (cons 0 org-file-tags)))
  12525. (llast 0) rtn rtn1 level category i txt
  12526. todo marker entry priority)
  12527. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12528. (setq action (list 'lambda nil action)))
  12529. (save-excursion
  12530. (goto-char (point-min))
  12531. (when (eq action 'sparse-tree)
  12532. (org-overview)
  12533. (org-remove-occur-highlights))
  12534. (while (re-search-forward re nil t)
  12535. (setq org-map-continue-from nil)
  12536. (catch :skip
  12537. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12538. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12539. (goto-char (setq lspos (match-beginning 0)))
  12540. (setq level (org-reduced-level (org-outline-level))
  12541. category (org-get-category))
  12542. (setq i llast llast level)
  12543. ;; remove tag lists from same and sublevels
  12544. (while (>= i level)
  12545. (when (setq entry (assoc i tags-alist))
  12546. (setq tags-alist (delete entry tags-alist)))
  12547. (setq i (1- i)))
  12548. ;; add the next tags
  12549. (when tags
  12550. (setq tags (org-split-string tags ":")
  12551. tags-alist
  12552. (cons (cons level tags) tags-alist)))
  12553. ;; compile tags for current headline
  12554. (setq tags-list
  12555. (if org-use-tag-inheritance
  12556. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12557. tags)
  12558. org-scanner-tags tags-list)
  12559. (when org-use-tag-inheritance
  12560. (setcdr (car tags-alist)
  12561. (mapcar (lambda (x)
  12562. (setq x (copy-sequence x))
  12563. (org-add-prop-inherited x))
  12564. (cdar tags-alist))))
  12565. (when (and tags org-use-tag-inheritance
  12566. (or (not (eq t org-use-tag-inheritance))
  12567. org-tags-exclude-from-inheritance))
  12568. ;; selective inheritance, remove uninherited ones
  12569. (setcdr (car tags-alist)
  12570. (org-remove-uninherited-tags (cdar tags-alist))))
  12571. (when (and
  12572. ;; eval matcher only when the todo condition is OK
  12573. (and (or (not todo-only) (member todo org-not-done-keywords))
  12574. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12575. (eval matcher)))
  12576. ;; Call the skipper, but return t if it does not skip,
  12577. ;; so that the `and' form continues evaluating
  12578. (progn
  12579. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12580. t)
  12581. ;; Check if timestamps are deselecting this entry
  12582. (or (not todo-only)
  12583. (and (member todo org-not-done-keywords)
  12584. (or (not org-agenda-tags-todo-honor-ignore-options)
  12585. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12586. ;; select this headline
  12587. (cond
  12588. ((eq action 'sparse-tree)
  12589. (and org-highlight-sparse-tree-matches
  12590. (org-get-heading) (match-end 0)
  12591. (org-highlight-new-match
  12592. (match-beginning 1) (match-end 1)))
  12593. (org-show-context 'tags-tree))
  12594. ((eq action 'agenda)
  12595. (setq txt (org-agenda-format-item
  12596. ""
  12597. (concat
  12598. (if (eq org-tags-match-list-sublevels 'indented)
  12599. (make-string (1- level) ?.) "")
  12600. (org-get-heading))
  12601. level category
  12602. tags-list)
  12603. priority (org-get-priority txt))
  12604. (goto-char lspos)
  12605. (setq marker (org-agenda-new-marker))
  12606. (org-add-props txt props
  12607. 'org-marker marker 'org-hd-marker marker 'org-category category
  12608. 'todo-state todo
  12609. 'priority priority 'type "tagsmatch")
  12610. (push txt rtn))
  12611. ((functionp action)
  12612. (setq org-map-continue-from nil)
  12613. (save-excursion
  12614. (setq rtn1 (funcall action))
  12615. (push rtn1 rtn)))
  12616. (t (error "Invalid action")))
  12617. ;; if we are to skip sublevels, jump to end of subtree
  12618. (unless org-tags-match-list-sublevels
  12619. (org-end-of-subtree t)
  12620. (backward-char 1))))
  12621. ;; Get the correct position from where to continue
  12622. (if org-map-continue-from
  12623. (goto-char org-map-continue-from)
  12624. (and (= (point) lspos) (end-of-line 1)))))
  12625. (when (and (eq action 'sparse-tree)
  12626. (not org-sparse-tree-open-archived-trees))
  12627. (org-hide-archived-subtrees (point-min) (point-max)))
  12628. (nreverse rtn)))
  12629. (defun org-remove-uninherited-tags (tags)
  12630. "Remove all tags that are not inherited from the list TAGS."
  12631. (cond
  12632. ((eq org-use-tag-inheritance t)
  12633. (if org-tags-exclude-from-inheritance
  12634. (org-delete-all org-tags-exclude-from-inheritance tags)
  12635. tags))
  12636. ((not org-use-tag-inheritance) nil)
  12637. ((stringp org-use-tag-inheritance)
  12638. (delq nil (mapcar
  12639. (lambda (x)
  12640. (if (and (string-match org-use-tag-inheritance x)
  12641. (not (member x org-tags-exclude-from-inheritance)))
  12642. x nil))
  12643. tags)))
  12644. ((listp org-use-tag-inheritance)
  12645. (delq nil (mapcar
  12646. (lambda (x)
  12647. (if (member x org-use-tag-inheritance) x nil))
  12648. tags)))))
  12649. (defun org-match-sparse-tree (&optional todo-only match)
  12650. "Create a sparse tree according to tags string MATCH.
  12651. MATCH can contain positive and negative selection of tags, like
  12652. \"+WORK+URGENT-WITHBOSS\".
  12653. If optional argument TODO-ONLY is non-nil, only select lines that are
  12654. also TODO lines."
  12655. (interactive "P")
  12656. (org-agenda-prepare-buffers (list (current-buffer)))
  12657. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12658. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12659. (defvar org-cached-props nil)
  12660. (defun org-cached-entry-get (pom property)
  12661. (if (or (eq t org-use-property-inheritance)
  12662. (and (stringp org-use-property-inheritance)
  12663. (string-match org-use-property-inheritance property))
  12664. (and (listp org-use-property-inheritance)
  12665. (member property org-use-property-inheritance)))
  12666. ;; Caching is not possible, check it directly
  12667. (org-entry-get pom property 'inherit)
  12668. ;; Get all properties, so that we can do complicated checks easily
  12669. (cdr (assoc property (or org-cached-props
  12670. (setq org-cached-props
  12671. (org-entry-properties pom)))))))
  12672. (defun org-global-tags-completion-table (&optional files)
  12673. "Return the list of all tags in all agenda buffer/files.
  12674. Optional FILES argument is a list of files which can be used
  12675. instead of the agenda files."
  12676. (save-excursion
  12677. (org-uniquify
  12678. (delq nil
  12679. (apply 'append
  12680. (mapcar
  12681. (lambda (file)
  12682. (set-buffer (find-file-noselect file))
  12683. (append (org-get-buffer-tags)
  12684. (mapcar (lambda (x) (if (stringp (car-safe x))
  12685. (list (car-safe x)) nil))
  12686. org-tag-alist)))
  12687. (if (and files (car files))
  12688. files
  12689. (org-agenda-files))))))))
  12690. (defun org-make-tags-matcher (match)
  12691. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12692. The variable `todo-only' is scoped dynamically into this function.
  12693. It will be set to t if the matcher restricts matching to TODO entries,
  12694. otherwise will not be touched.
  12695. Returns a cons of the selection string MATCH and the constructed
  12696. lisp form implementing the matcher. The matcher is to be evaluated
  12697. at an Org entry, with point on the headline, and returns t if the
  12698. entry matches the selection string MATCH. The returned lisp form
  12699. references two variables with information about the entry, which
  12700. must be bound around the form's evaluation: todo, the TODO keyword
  12701. at the entry (or nil of none); and tags-list, the list of all tags
  12702. at the entry including inherited ones. Additionally, the category
  12703. of the entry (if any) must be specified as the text property
  12704. 'org-category on the headline.
  12705. See also `org-scan-tags'.
  12706. "
  12707. (declare (special todo-only))
  12708. (unless (boundp 'todo-only)
  12709. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12710. (unless match
  12711. ;; Get a new match request, with completion
  12712. (let ((org-last-tags-completion-table
  12713. (org-global-tags-completion-table)))
  12714. (setq match (org-completing-read-no-i
  12715. "Match: " 'org-tags-completion-function nil nil nil
  12716. 'org-tags-history))))
  12717. ;; Parse the string and create a lisp form
  12718. (let ((match0 match)
  12719. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12720. minus tag mm
  12721. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12722. orterms term orlist re-p str-p level-p level-op time-p
  12723. prop-p pn pv po gv rest)
  12724. ;; Expand group tags
  12725. (setq match (org-tags-expand match))
  12726. (if (string-match "/+" match)
  12727. ;; match contains also a todo-matching request
  12728. (progn
  12729. (setq tagsmatch (substring match 0 (match-beginning 0))
  12730. todomatch (substring match (match-end 0)))
  12731. (if (string-match "^!" todomatch)
  12732. (setq todo-only t todomatch (substring todomatch 1)))
  12733. (if (string-match "^\\s-*$" todomatch)
  12734. (setq todomatch nil)))
  12735. ;; only matching tags
  12736. (setq tagsmatch match todomatch nil))
  12737. ;; Make the tags matcher
  12738. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12739. (setq tagsmatcher t)
  12740. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12741. (while (setq term (pop orterms))
  12742. (while (and (equal (substring term -1) "\\") orterms)
  12743. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12744. (while (string-match re term)
  12745. (setq rest (substring term (match-end 0))
  12746. minus (and (match-end 1)
  12747. (equal (match-string 1 term) "-"))
  12748. tag (save-match-data (replace-regexp-in-string
  12749. "\\\\-" "-"
  12750. (match-string 2 term)))
  12751. re-p (equal (string-to-char tag) ?{)
  12752. level-p (match-end 4)
  12753. prop-p (match-end 5)
  12754. mm (cond
  12755. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12756. (level-p
  12757. (setq level-op (org-op-to-function (match-string 3 term)))
  12758. `(,level-op level ,(string-to-number
  12759. (match-string 4 term))))
  12760. (prop-p
  12761. (setq pn (match-string 5 term)
  12762. po (match-string 6 term)
  12763. pv (match-string 7 term)
  12764. re-p (equal (string-to-char pv) ?{)
  12765. str-p (equal (string-to-char pv) ?\")
  12766. time-p (save-match-data
  12767. (string-match "^\"[[<].*[]>]\"$" pv))
  12768. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12769. (if time-p (setq pv (org-matcher-time pv)))
  12770. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12771. (cond
  12772. ((equal pn "CATEGORY")
  12773. (setq gv '(get-text-property (point) 'org-category)))
  12774. ((equal pn "TODO")
  12775. (setq gv 'todo))
  12776. (t
  12777. (setq gv `(org-cached-entry-get nil ,pn))))
  12778. (if re-p
  12779. (if (eq po 'org<>)
  12780. `(not (string-match ,pv (or ,gv "")))
  12781. `(string-match ,pv (or ,gv "")))
  12782. (if str-p
  12783. `(,po (or ,gv "") ,pv)
  12784. `(,po (string-to-number (or ,gv ""))
  12785. ,(string-to-number pv) ))))
  12786. (t `(member ,tag tags-list)))
  12787. mm (if minus (list 'not mm) mm)
  12788. term rest)
  12789. (push mm tagsmatcher))
  12790. (push (if (> (length tagsmatcher) 1)
  12791. (cons 'and tagsmatcher)
  12792. (car tagsmatcher))
  12793. orlist)
  12794. (setq tagsmatcher nil))
  12795. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12796. (setq tagsmatcher
  12797. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12798. ;; Make the todo matcher
  12799. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12800. (setq todomatcher t)
  12801. (setq orterms (org-split-string todomatch "|") orlist nil)
  12802. (while (setq term (pop orterms))
  12803. (while (string-match re term)
  12804. (setq minus (and (match-end 1)
  12805. (equal (match-string 1 term) "-"))
  12806. kwd (match-string 2 term)
  12807. re-p (equal (string-to-char kwd) ?{)
  12808. term (substring term (match-end 0))
  12809. mm (if re-p
  12810. `(string-match ,(substring kwd 1 -1) todo)
  12811. (list 'equal 'todo kwd))
  12812. mm (if minus (list 'not mm) mm))
  12813. (push mm todomatcher))
  12814. (push (if (> (length todomatcher) 1)
  12815. (cons 'and todomatcher)
  12816. (car todomatcher))
  12817. orlist)
  12818. (setq todomatcher nil))
  12819. (setq todomatcher (if (> (length orlist) 1)
  12820. (cons 'or orlist) (car orlist))))
  12821. ;; Return the string and lisp forms of the matcher
  12822. (setq matcher (if todomatcher
  12823. (list 'and tagsmatcher todomatcher)
  12824. tagsmatcher))
  12825. (when todo-only
  12826. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12827. matcher)))
  12828. (cons match0 matcher)))
  12829. (defun org-tags-expand (match &optional single-as-list downcased)
  12830. "Expand group tags in MATCH.
  12831. This replaces every group tag in MATCH with a regexp tag search.
  12832. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12833. will be replaced like this:
  12834. Work => {\(?:Work\|Lab\|Conf\}
  12835. +Work => +{\(?:Work\|Lab\|Conf\}
  12836. -Work => -{\(?:Work\|Lab\|Conf\}
  12837. Replacing by a regexp preserves the structure of the match.
  12838. E.g., this expansion
  12839. Work|Home => {\(?:Work\|Lab\|Conf\}|Home
  12840. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12841. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12842. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12843. assumed to be a single group tag, and the function will return
  12844. the list of tags in this group.
  12845. When DOWNCASE is non-nil, expand downcased TAGS."
  12846. (if org-group-tags
  12847. (let* ((case-fold-search t)
  12848. (stable org-mode-syntax-table)
  12849. (tal (or org-tag-groups-alist-for-agenda
  12850. org-tag-groups-alist))
  12851. (tal (if downcased (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12852. (tml (mapcar 'car tal))
  12853. (rtnmatch match) rpl)
  12854. ;; @ and _ are allowed as word-components in tags
  12855. (modify-syntax-entry ?@ "w" stable)
  12856. (modify-syntax-entry ?_ "w" stable)
  12857. (while (and tml (string-match
  12858. (concat "\\(?1:[+-]?\\)\\(?2:\\<" (regexp-opt tml) "\\>\\)")
  12859. rtnmatch))
  12860. (let* ((dir (match-string 1 rtnmatch))
  12861. (tag (match-string 2 rtnmatch))
  12862. (tag (if downcased (downcase tag) tag)))
  12863. (setq tml (delete tag tml))
  12864. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12865. (setq rtnmatch
  12866. (replace-match
  12867. (concat dir "{" (regexp-opt rpl) "}") t t rtnmatch))))
  12868. (if single-as-list
  12869. (or (reverse rpl) (list rtnmatch))
  12870. rtnmatch))
  12871. (if single-as-list (list (if downcased (downcase match) match))
  12872. match)))
  12873. (defun org-op-to-function (op &optional stringp)
  12874. "Turn an operator into the appropriate function."
  12875. (setq op
  12876. (cond
  12877. ((equal op "<" ) '(< string< org-time<))
  12878. ((equal op ">" ) '(> org-string> org-time>))
  12879. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12880. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12881. ((member op '("=" "==")) '(= string= org-time=))
  12882. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12883. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12884. (defun org<> (a b) (not (= a b)))
  12885. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12886. (defun org-string>= (a b) (not (string< a b)))
  12887. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12888. (defun org-string<> (a b) (not (string= a b)))
  12889. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= 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) (org<> a b)))
  12895. (defun org-2ft (s)
  12896. "Convert S to a floating point time.
  12897. If S is already a number, just return it. If it is a string, parse
  12898. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12899. (cond
  12900. ((numberp s) s)
  12901. ((stringp s)
  12902. (condition-case nil
  12903. (float-time (apply 'encode-time (org-parse-time-string s)))
  12904. (error 0.)))
  12905. (t 0.)))
  12906. (defun org-time-today ()
  12907. "Time in seconds today at 0:00.
  12908. Returns the float number of seconds since the beginning of the
  12909. epoch to the beginning of today (00:00)."
  12910. (float-time (apply 'encode-time
  12911. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12912. (defun org-matcher-time (s)
  12913. "Interpret a time comparison value."
  12914. (save-match-data
  12915. (cond
  12916. ((string= s "<now>") (float-time))
  12917. ((string= s "<today>") (org-time-today))
  12918. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12919. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12920. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12921. (+ (org-time-today)
  12922. (* (string-to-number (match-string 1 s))
  12923. (cdr (assoc (match-string 2 s)
  12924. '(("d" . 86400.0) ("w" . 604800.0)
  12925. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12926. (t (org-2ft s)))))
  12927. (defun org-match-any-p (re list)
  12928. "Does re match any element of list?"
  12929. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12930. (delq nil list))
  12931. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12932. (defvar org-tags-overlay (make-overlay 1 1))
  12933. (org-detach-overlay org-tags-overlay)
  12934. (defun org-get-local-tags-at (&optional pos)
  12935. "Get a list of tags defined in the current headline."
  12936. (org-get-tags-at pos 'local))
  12937. (defun org-get-local-tags ()
  12938. "Get a list of tags defined in the current headline."
  12939. (org-get-tags-at nil 'local))
  12940. (defun org-get-tags-at (&optional pos local)
  12941. "Get a list of all headline tags applicable at POS.
  12942. POS defaults to point. If tags are inherited, the list contains
  12943. the targets in the same sequence as the headlines appear, i.e.
  12944. the tags of the current headline come last.
  12945. When LOCAL is non-nil, only return tags from the current headline,
  12946. ignore inherited ones."
  12947. (interactive)
  12948. (if (and org-trust-scanner-tags
  12949. (or (not pos) (equal pos (point)))
  12950. (not local))
  12951. org-scanner-tags
  12952. (let (tags ltags lastpos parent)
  12953. (save-excursion
  12954. (save-restriction
  12955. (widen)
  12956. (goto-char (or pos (point)))
  12957. (save-match-data
  12958. (catch 'done
  12959. (condition-case nil
  12960. (progn
  12961. (org-back-to-heading t)
  12962. (while (not (equal lastpos (point)))
  12963. (setq lastpos (point))
  12964. (when (looking-at
  12965. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12966. (setq ltags (org-split-string
  12967. (org-match-string-no-properties 1) ":"))
  12968. (when parent
  12969. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12970. (setq tags (append
  12971. (if parent
  12972. (org-remove-uninherited-tags ltags)
  12973. ltags)
  12974. tags)))
  12975. (or org-use-tag-inheritance (throw 'done t))
  12976. (if local (throw 'done t))
  12977. (or (org-up-heading-safe) (error nil))
  12978. (setq parent t)))
  12979. (error nil)))))
  12980. (if local
  12981. tags
  12982. (reverse (delete-dups
  12983. (reverse (append
  12984. (org-remove-uninherited-tags
  12985. org-file-tags) tags)))))))))
  12986. (defun org-add-prop-inherited (s)
  12987. (add-text-properties 0 (length s) '(inherited t) s)
  12988. s)
  12989. (defun org-toggle-tag (tag &optional onoff)
  12990. "Toggle the tag TAG for the current line.
  12991. If ONOFF is `on' or `off', don't toggle but set to this state."
  12992. (let (res current)
  12993. (save-excursion
  12994. (org-back-to-heading t)
  12995. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12996. (point-at-eol) t)
  12997. (progn
  12998. (setq current (match-string 1))
  12999. (replace-match ""))
  13000. (setq current ""))
  13001. (setq current (nreverse (org-split-string current ":")))
  13002. (cond
  13003. ((eq onoff 'on)
  13004. (setq res t)
  13005. (or (member tag current) (push tag current)))
  13006. ((eq onoff 'off)
  13007. (or (not (member tag current)) (setq current (delete tag current))))
  13008. (t (if (member tag current)
  13009. (setq current (delete tag current))
  13010. (setq res t)
  13011. (push tag current))))
  13012. (end-of-line 1)
  13013. (if current
  13014. (progn
  13015. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13016. (org-set-tags nil t))
  13017. (delete-horizontal-space))
  13018. (run-hooks 'org-after-tags-change-hook))
  13019. res))
  13020. (defun org-align-tags-here (to-col)
  13021. ;; Assumes that this is a headline
  13022. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13023. (beginning-of-line 1)
  13024. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13025. (< pos (match-beginning 2)))
  13026. (progn
  13027. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13028. (goto-char (match-beginning 1))
  13029. (insert " ")
  13030. (delete-region (point) (1+ (match-beginning 2)))
  13031. (setq ncol (max (current-column)
  13032. (1+ col)
  13033. (if (> to-col 0)
  13034. to-col
  13035. (- (abs to-col) tags-l))))
  13036. (setq p (point))
  13037. (insert (make-string (- ncol (current-column)) ?\ ))
  13038. (setq ncol (current-column))
  13039. (when indent-tabs-mode (tabify p (point-at-eol)))
  13040. (org-move-to-column (min ncol col) t))
  13041. (goto-char pos))))
  13042. (defun org-set-tags-command (&optional arg just-align)
  13043. "Call the set-tags command for the current entry."
  13044. (interactive "P")
  13045. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13046. (org-set-tags arg just-align)
  13047. (save-excursion
  13048. (unless (and (org-region-active-p)
  13049. org-loop-over-headlines-in-active-region)
  13050. (org-back-to-heading t))
  13051. (org-set-tags arg just-align))))
  13052. (defun org-set-tags-to (data)
  13053. "Set the tags of the current entry to DATA, replacing the current tags.
  13054. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13055. If DATA is nil or the empty string, any tags will be removed."
  13056. (interactive "sTags: ")
  13057. (setq data
  13058. (cond
  13059. ((eq data nil) "")
  13060. ((equal data "") "")
  13061. ((stringp data)
  13062. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13063. ":"))
  13064. ((listp data)
  13065. (concat ":" (mapconcat 'identity data ":") ":"))))
  13066. (when data
  13067. (save-excursion
  13068. (org-back-to-heading t)
  13069. (when (looking-at org-complex-heading-regexp)
  13070. (if (match-end 5)
  13071. (progn
  13072. (goto-char (match-beginning 5))
  13073. (insert data)
  13074. (delete-region (point) (point-at-eol))
  13075. (org-set-tags nil 'align))
  13076. (goto-char (point-at-eol))
  13077. (insert " " data)
  13078. (org-set-tags nil 'align)))
  13079. (beginning-of-line 1)
  13080. (if (looking-at ".*?\\([ \t]+\\)$")
  13081. (delete-region (match-beginning 1) (match-end 1))))))
  13082. (defun org-align-all-tags ()
  13083. "Align the tags i all headings."
  13084. (interactive)
  13085. (save-excursion
  13086. (or (ignore-errors (org-back-to-heading t))
  13087. (outline-next-heading))
  13088. (if (org-at-heading-p)
  13089. (org-set-tags t)
  13090. (message "No headings"))))
  13091. (defvar org-indent-indentation-per-level)
  13092. (defun org-set-tags (&optional arg just-align)
  13093. "Set the tags for the current headline.
  13094. With prefix ARG, realign all tags in headings in the current buffer."
  13095. (interactive "P")
  13096. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13097. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13098. 'region-start-level 'region))
  13099. org-loop-over-headlines-in-active-region)
  13100. (org-map-entries
  13101. ;; We don't use ARG and JUST-ALIGN here these args are not
  13102. ;; useful when looping over headlines
  13103. `(org-set-tags)
  13104. org-loop-over-headlines-in-active-region
  13105. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13106. (let* ((re org-outline-regexp-bol)
  13107. (current (unless arg (org-get-tags-string)))
  13108. (col (current-column))
  13109. (org-setting-tags t)
  13110. table current-tags inherited-tags ; computed below when needed
  13111. tags p0 c0 c1 rpl di tc level)
  13112. (if arg
  13113. (save-excursion
  13114. (goto-char (point-min))
  13115. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13116. (while (re-search-forward re nil t)
  13117. (org-set-tags nil t)
  13118. (end-of-line 1)))
  13119. (message "All tags realigned to column %d" org-tags-column))
  13120. (if just-align
  13121. (setq tags current)
  13122. ;; Get a new set of tags from the user
  13123. (save-excursion
  13124. (setq table (append org-tag-persistent-alist
  13125. (or org-tag-alist (org-get-buffer-tags))
  13126. (and
  13127. org-complete-tags-always-offer-all-agenda-tags
  13128. (org-global-tags-completion-table
  13129. (org-agenda-files))))
  13130. org-last-tags-completion-table table
  13131. current-tags (org-split-string current ":")
  13132. inherited-tags (nreverse
  13133. (nthcdr (length current-tags)
  13134. (nreverse (org-get-tags-at))))
  13135. tags
  13136. (if (or (eq t org-use-fast-tag-selection)
  13137. (and org-use-fast-tag-selection
  13138. (delq nil (mapcar 'cdr table))))
  13139. (org-fast-tag-selection
  13140. current-tags inherited-tags table
  13141. (if org-fast-tag-selection-include-todo
  13142. org-todo-key-alist))
  13143. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13144. (org-trim
  13145. (org-icompleting-read "Tags: "
  13146. 'org-tags-completion-function
  13147. nil nil current 'org-tags-history))))))
  13148. (while (string-match "[-+&]+" tags)
  13149. ;; No boolean logic, just a list
  13150. (setq tags (replace-match ":" t t tags))))
  13151. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13152. (if org-tags-sort-function
  13153. (setq tags (mapconcat 'identity
  13154. (sort (org-split-string
  13155. tags (org-re "[^[:alnum:]_@#%]+"))
  13156. org-tags-sort-function) ":")))
  13157. (if (string-match "\\`[\t ]*\\'" tags)
  13158. (setq tags "")
  13159. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13160. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13161. ;; Insert new tags at the correct column
  13162. (beginning-of-line 1)
  13163. (setq level (or (and (looking-at org-outline-regexp)
  13164. (- (match-end 0) (point) 1))
  13165. 1))
  13166. (cond
  13167. ((and (equal current "") (equal tags "")))
  13168. ((re-search-forward
  13169. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13170. (point-at-eol) t)
  13171. (if (equal tags "")
  13172. (setq rpl "")
  13173. (goto-char (match-beginning 0))
  13174. (setq c0 (current-column)
  13175. ;; compute offset for the case of org-indent-mode active
  13176. di (if org-indent-mode
  13177. (* (1- org-indent-indentation-per-level) (1- level))
  13178. 0)
  13179. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13180. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13181. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13182. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13183. (replace-match rpl t t)
  13184. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13185. tags)
  13186. (t (error "Tags alignment failed")))
  13187. (org-move-to-column col)
  13188. (unless just-align
  13189. (run-hooks 'org-after-tags-change-hook))))))
  13190. (defun org-change-tag-in-region (beg end tag off)
  13191. "Add or remove TAG for each entry in the region.
  13192. This works in the agenda, and also in an org-mode buffer."
  13193. (interactive
  13194. (list (region-beginning) (region-end)
  13195. (let ((org-last-tags-completion-table
  13196. (if (derived-mode-p 'org-mode)
  13197. (org-get-buffer-tags)
  13198. (org-global-tags-completion-table))))
  13199. (org-icompleting-read
  13200. "Tag: " 'org-tags-completion-function nil nil nil
  13201. 'org-tags-history))
  13202. (progn
  13203. (message "[s]et or [r]emove? ")
  13204. (equal (read-char-exclusive) ?r))))
  13205. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13206. (let ((agendap (equal major-mode 'org-agenda-mode))
  13207. l1 l2 m buf pos newhead (cnt 0))
  13208. (goto-char end)
  13209. (setq l2 (1- (org-current-line)))
  13210. (goto-char beg)
  13211. (setq l1 (org-current-line))
  13212. (loop for l from l1 to l2 do
  13213. (org-goto-line l)
  13214. (setq m (get-text-property (point) 'org-hd-marker))
  13215. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13216. (and agendap m))
  13217. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13218. pos (if agendap m (point)))
  13219. (with-current-buffer buf
  13220. (save-excursion
  13221. (save-restriction
  13222. (goto-char pos)
  13223. (setq cnt (1+ cnt))
  13224. (org-toggle-tag tag (if off 'off 'on))
  13225. (setq newhead (org-get-heading)))))
  13226. (and agendap (org-agenda-change-all-lines newhead m))))
  13227. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13228. (defun org-tags-completion-function (string predicate &optional flag)
  13229. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13230. (confirm (lambda (x) (stringp (car x)))))
  13231. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13232. (setq s1 (match-string 1 string)
  13233. s2 (match-string 2 string))
  13234. (setq s1 "" s2 string))
  13235. (cond
  13236. ((eq flag nil)
  13237. ;; try completion
  13238. (setq rtn (try-completion s2 ctable confirm))
  13239. (if (stringp rtn)
  13240. (setq rtn
  13241. (concat s1 s2 (substring rtn (length s2))
  13242. (if (and org-add-colon-after-tag-completion
  13243. (assoc rtn ctable))
  13244. ":" ""))))
  13245. rtn)
  13246. ((eq flag t)
  13247. ;; all-completions
  13248. (all-completions s2 ctable confirm))
  13249. ((eq flag 'lambda)
  13250. ;; exact match?
  13251. (assoc s2 ctable)))))
  13252. (defun org-fast-tag-insert (kwd tags face &optional end)
  13253. "Insert KDW, and the TAGS, the latter with face FACE.
  13254. Also insert END."
  13255. (insert (format "%-12s" (concat kwd ":"))
  13256. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13257. (or end "")))
  13258. (defun org-fast-tag-show-exit (flag)
  13259. (save-excursion
  13260. (org-goto-line 3)
  13261. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13262. (replace-match ""))
  13263. (when flag
  13264. (end-of-line 1)
  13265. (org-move-to-column (- (window-width) 19) t)
  13266. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13267. (defun org-set-current-tags-overlay (current prefix)
  13268. "Add an overlay to CURRENT tag with PREFIX."
  13269. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13270. (if (featurep 'xemacs)
  13271. (org-overlay-display org-tags-overlay (concat prefix s)
  13272. 'secondary-selection)
  13273. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13274. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13275. (defvar org-last-tag-selection-key nil)
  13276. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13277. "Fast tag selection with single keys.
  13278. CURRENT is the current list of tags in the headline, INHERITED is the
  13279. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13280. possibly with grouping information. TODO-TABLE is a similar table with
  13281. TODO keywords, should these have keys assigned to them.
  13282. If the keys are nil, a-z are automatically assigned.
  13283. Returns the new tags string, or nil to not change the current settings."
  13284. (let* ((fulltable (append table todo-table))
  13285. (maxlen (apply 'max (mapcar
  13286. (lambda (x)
  13287. (if (stringp (car x)) (string-width (car x)) 0))
  13288. fulltable)))
  13289. (buf (current-buffer))
  13290. (expert (eq org-fast-tag-selection-single-key 'expert))
  13291. (buffer-tags nil)
  13292. (fwidth (+ maxlen 3 1 3))
  13293. (ncol (/ (- (window-width) 4) fwidth))
  13294. (i-face 'org-done)
  13295. (c-face 'org-todo)
  13296. tg cnt e c char c1 c2 ntable tbl rtn
  13297. ov-start ov-end ov-prefix
  13298. (exit-after-next org-fast-tag-selection-single-key)
  13299. (done-keywords org-done-keywords)
  13300. groups ingroup)
  13301. (save-excursion
  13302. (beginning-of-line 1)
  13303. (if (looking-at
  13304. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13305. (setq ov-start (match-beginning 1)
  13306. ov-end (match-end 1)
  13307. ov-prefix "")
  13308. (setq ov-start (1- (point-at-eol))
  13309. ov-end (1+ ov-start))
  13310. (skip-chars-forward "^\n\r")
  13311. (setq ov-prefix
  13312. (concat
  13313. (buffer-substring (1- (point)) (point))
  13314. (if (> (current-column) org-tags-column)
  13315. " "
  13316. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13317. (move-overlay org-tags-overlay ov-start ov-end)
  13318. (save-window-excursion
  13319. (if expert
  13320. (set-buffer (get-buffer-create " *Org tags*"))
  13321. (delete-other-windows)
  13322. (split-window-vertically)
  13323. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13324. (erase-buffer)
  13325. (org-set-local 'org-done-keywords done-keywords)
  13326. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13327. (org-fast-tag-insert "Current" current c-face "\n\n")
  13328. (org-fast-tag-show-exit exit-after-next)
  13329. (org-set-current-tags-overlay current ov-prefix)
  13330. (setq tbl fulltable char ?a cnt 0)
  13331. (while (setq e (pop tbl))
  13332. (cond
  13333. ((equal (car e) :startgroup)
  13334. (push '() groups) (setq ingroup t)
  13335. (when (not (= cnt 0))
  13336. (setq cnt 0)
  13337. (insert "\n"))
  13338. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13339. ((equal (car e) :endgroup)
  13340. (setq ingroup nil cnt 0)
  13341. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13342. ((equal e '(:newline))
  13343. (when (not (= cnt 0))
  13344. (setq cnt 0)
  13345. (insert "\n")
  13346. (setq e (car tbl))
  13347. (while (equal (car tbl) '(:newline))
  13348. (insert "\n")
  13349. (setq tbl (cdr tbl)))))
  13350. ((equal e '(:grouptags)) nil)
  13351. (t
  13352. (setq tg (copy-sequence (car e)) c2 nil)
  13353. (if (cdr e)
  13354. (setq c (cdr e))
  13355. ;; automatically assign a character.
  13356. (setq c1 (string-to-char
  13357. (downcase (substring
  13358. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13359. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13360. (while (or (rassoc char ntable) (rassoc char table))
  13361. (setq char (1+ char)))
  13362. (setq c2 c1))
  13363. (setq c (or c2 char)))
  13364. (if ingroup (push tg (car groups)))
  13365. (setq tg (org-add-props tg nil 'face
  13366. (cond
  13367. ((not (assoc tg table))
  13368. (org-get-todo-face tg))
  13369. ((member tg current) c-face)
  13370. ((member tg inherited) i-face))))
  13371. (if (equal (caar tbl) :grouptags)
  13372. (org-add-props tg nil 'face 'org-tag-group))
  13373. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13374. (insert "[" c "] " tg (make-string
  13375. (- fwidth 4 (length tg)) ?\ ))
  13376. (push (cons tg c) ntable)
  13377. (when (= (setq cnt (1+ cnt)) ncol)
  13378. (insert "\n")
  13379. (if ingroup (insert " "))
  13380. (setq cnt 0)))))
  13381. (setq ntable (nreverse ntable))
  13382. (insert "\n")
  13383. (goto-char (point-min))
  13384. (if (not expert) (org-fit-window-to-buffer))
  13385. (setq rtn
  13386. (catch 'exit
  13387. (while t
  13388. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13389. (if (not groups) "no " "")
  13390. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13391. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13392. (setq org-last-tag-selection-key c)
  13393. (cond
  13394. ((= c ?\r) (throw 'exit t))
  13395. ((= c ?!)
  13396. (setq groups (not groups))
  13397. (goto-char (point-min))
  13398. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13399. ((= c ?\C-c)
  13400. (if (not expert)
  13401. (org-fast-tag-show-exit
  13402. (setq exit-after-next (not exit-after-next)))
  13403. (setq expert nil)
  13404. (delete-other-windows)
  13405. (set-window-buffer (split-window-vertically) " *Org tags*")
  13406. (org-switch-to-buffer-other-window " *Org tags*")
  13407. (org-fit-window-to-buffer)))
  13408. ((or (= c ?\C-g)
  13409. (and (= c ?q) (not (rassoc c ntable))))
  13410. (org-detach-overlay org-tags-overlay)
  13411. (setq quit-flag t))
  13412. ((= c ?\ )
  13413. (setq current nil)
  13414. (if exit-after-next (setq exit-after-next 'now)))
  13415. ((= c ?\t)
  13416. (condition-case nil
  13417. (setq tg (org-icompleting-read
  13418. "Tag: "
  13419. (or buffer-tags
  13420. (with-current-buffer buf
  13421. (org-get-buffer-tags)))))
  13422. (quit (setq tg "")))
  13423. (when (string-match "\\S-" tg)
  13424. (add-to-list 'buffer-tags (list tg))
  13425. (if (member tg current)
  13426. (setq current (delete tg current))
  13427. (push tg current)))
  13428. (if exit-after-next (setq exit-after-next 'now)))
  13429. ((setq e (rassoc c todo-table) tg (car e))
  13430. (with-current-buffer buf
  13431. (save-excursion (org-todo tg)))
  13432. (if exit-after-next (setq exit-after-next 'now)))
  13433. ((setq e (rassoc c ntable) tg (car e))
  13434. (if (member tg current)
  13435. (setq current (delete tg current))
  13436. (loop for g in groups do
  13437. (if (member tg g)
  13438. (mapc (lambda (x)
  13439. (setq current (delete x current)))
  13440. g)))
  13441. (push tg current))
  13442. (if exit-after-next (setq exit-after-next 'now))))
  13443. ;; Create a sorted list
  13444. (setq current
  13445. (sort current
  13446. (lambda (a b)
  13447. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13448. (if (eq exit-after-next 'now) (throw 'exit t))
  13449. (goto-char (point-min))
  13450. (beginning-of-line 2)
  13451. (delete-region (point) (point-at-eol))
  13452. (org-fast-tag-insert "Current" current c-face)
  13453. (org-set-current-tags-overlay current ov-prefix)
  13454. (while (re-search-forward
  13455. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13456. (setq tg (match-string 1))
  13457. (add-text-properties
  13458. (match-beginning 1) (match-end 1)
  13459. (list 'face
  13460. (cond
  13461. ((member tg current) c-face)
  13462. ((member tg inherited) i-face)
  13463. (t (get-text-property (match-beginning 1) 'face))))))
  13464. (goto-char (point-min)))))
  13465. (org-detach-overlay org-tags-overlay)
  13466. (if rtn
  13467. (mapconcat 'identity current ":")
  13468. nil))))
  13469. (defun org-get-tags-string ()
  13470. "Get the TAGS string in the current headline."
  13471. (unless (org-at-heading-p t)
  13472. (error "Not on a heading"))
  13473. (save-excursion
  13474. (beginning-of-line 1)
  13475. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13476. (org-match-string-no-properties 1)
  13477. "")))
  13478. (defun org-get-tags ()
  13479. "Get the list of tags specified in the current headline."
  13480. (org-split-string (org-get-tags-string) ":"))
  13481. (defun org-get-buffer-tags ()
  13482. "Get a table of all tags used in the buffer, for completion."
  13483. (let (tags)
  13484. (save-excursion
  13485. (goto-char (point-min))
  13486. (while (re-search-forward
  13487. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13488. (when (equal (char-after (point-at-bol 0)) ?*)
  13489. (mapc (lambda (x) (add-to-list 'tags x))
  13490. (org-split-string (org-match-string-no-properties 1) ":")))))
  13491. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13492. (mapcar 'list tags)))
  13493. ;;;; The mapping API
  13494. (defun org-map-entries (func &optional match scope &rest skip)
  13495. "Call FUNC at each headline selected by MATCH in SCOPE.
  13496. FUNC is a function or a lisp form. The function will be called without
  13497. arguments, with the cursor positioned at the beginning of the headline.
  13498. The return values of all calls to the function will be collected and
  13499. returned as a list.
  13500. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13501. does not need to preserve point. After evaluation, the cursor will be
  13502. moved to the end of the line (presumably of the headline of the
  13503. processed entry) and search continues from there. Under some
  13504. circumstances, this may not produce the wanted results. For example,
  13505. if you have removed (e.g. archived) the current (sub)tree it could
  13506. mean that the next entry will be skipped entirely. In such cases, you
  13507. can specify the position from where search should continue by making
  13508. FUNC set the variable `org-map-continue-from' to the desired buffer
  13509. position.
  13510. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13511. Only headlines that are matched by this query will be considered during
  13512. the iteration. When MATCH is nil or t, all headlines will be
  13513. visited by the iteration.
  13514. SCOPE determines the scope of this command. It can be any of:
  13515. nil The current buffer, respecting the restriction if any
  13516. tree The subtree started with the entry at point
  13517. region The entries within the active region, if any
  13518. region-start-level
  13519. The entries within the active region, but only those at
  13520. the same level than the first one.
  13521. file The current buffer, without restriction
  13522. file-with-archives
  13523. The current buffer, and any archives associated with it
  13524. agenda All agenda files
  13525. agenda-with-archives
  13526. All agenda files with any archive files associated with them
  13527. \(file1 file2 ...)
  13528. If this is a list, all files in the list will be scanned
  13529. The remaining args are treated as settings for the skipping facilities of
  13530. the scanner. The following items can be given here:
  13531. archive skip trees with the archive tag
  13532. comment skip trees with the COMMENT keyword
  13533. function or Emacs Lisp form:
  13534. will be used as value for `org-agenda-skip-function', so
  13535. whenever the function returns a position, FUNC will not be
  13536. called for that entry and search will continue from the
  13537. position returned
  13538. If your function needs to retrieve the tags including inherited tags
  13539. at the *current* entry, you can use the value of the variable
  13540. `org-scanner-tags' which will be much faster than getting the value
  13541. with `org-get-tags-at'. If your function gets properties with
  13542. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13543. to t around the call to `org-entry-properties' to get the same speedup.
  13544. Note that if your function moves around to retrieve tags and properties at
  13545. a *different* entry, you cannot use these techniques."
  13546. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13547. (not (org-region-active-p)))
  13548. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13549. (org-agenda-skip-archived-trees (memq 'archive skip))
  13550. (org-agenda-skip-comment-trees (memq 'comment skip))
  13551. (org-agenda-skip-function
  13552. (car (org-delete-all '(comment archive) skip)))
  13553. (org-tags-match-list-sublevels t)
  13554. (start-level (eq scope 'region-start-level))
  13555. matcher file res
  13556. org-todo-keywords-for-agenda
  13557. org-done-keywords-for-agenda
  13558. org-todo-keyword-alist-for-agenda
  13559. org-drawers-for-agenda
  13560. org-tag-alist-for-agenda
  13561. todo-only)
  13562. (cond
  13563. ((eq match t) (setq matcher t))
  13564. ((eq match nil) (setq matcher t))
  13565. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13566. (save-window-excursion
  13567. (save-restriction
  13568. (cond ((eq scope 'tree)
  13569. (org-back-to-heading t)
  13570. (org-narrow-to-subtree)
  13571. (setq scope nil))
  13572. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13573. (org-region-active-p))
  13574. ;; If needed, set start-level to a string like "2"
  13575. (when start-level
  13576. (save-excursion
  13577. (goto-char (region-beginning))
  13578. (unless (org-at-heading-p) (outline-next-heading))
  13579. (setq start-level (org-current-level))))
  13580. (narrow-to-region (region-beginning)
  13581. (save-excursion
  13582. (goto-char (region-end))
  13583. (unless (and (bolp) (org-at-heading-p))
  13584. (outline-next-heading))
  13585. (point)))
  13586. (setq scope nil)))
  13587. (if (not scope)
  13588. (progn
  13589. (org-agenda-prepare-buffers
  13590. (list (buffer-file-name (current-buffer))))
  13591. (setq res (org-scan-tags func matcher todo-only start-level)))
  13592. ;; Get the right scope
  13593. (cond
  13594. ((and scope (listp scope) (symbolp (car scope)))
  13595. (setq scope (eval scope)))
  13596. ((eq scope 'agenda)
  13597. (setq scope (org-agenda-files t)))
  13598. ((eq scope 'agenda-with-archives)
  13599. (setq scope (org-agenda-files t))
  13600. (setq scope (org-add-archive-files scope)))
  13601. ((eq scope 'file)
  13602. (setq scope (list (buffer-file-name))))
  13603. ((eq scope 'file-with-archives)
  13604. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13605. (org-agenda-prepare-buffers scope)
  13606. (while (setq file (pop scope))
  13607. (with-current-buffer (org-find-base-buffer-visiting file)
  13608. (save-excursion
  13609. (save-restriction
  13610. (widen)
  13611. (goto-char (point-min))
  13612. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13613. res)))
  13614. ;;;; Properties
  13615. ;;; Setting and retrieving properties
  13616. (defconst org-special-properties
  13617. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13618. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13619. "The special properties valid in Org-mode.
  13620. These are properties that are not defined in the property drawer,
  13621. but in some other way.")
  13622. (defconst org-default-properties
  13623. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13624. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13625. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13626. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13627. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13628. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13629. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13630. "Some properties that are used by Org-mode for various purposes.
  13631. Being in this list makes sure that they are offered for completion.")
  13632. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13633. "Regular expression matching the first line of a property drawer.")
  13634. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13635. "Regular expression matching the last line of a property drawer.")
  13636. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13637. "Regular expression matching the first line of a property drawer.")
  13638. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13639. "Regular expression matching the first line of a property drawer.")
  13640. (defconst org-property-drawer-re
  13641. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13642. org-property-end-re "\\)\n?")
  13643. "Matches an entire property drawer.")
  13644. (defconst org-clock-drawer-re
  13645. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13646. org-property-end-re "\\)\n?")
  13647. "Matches an entire clock drawer.")
  13648. (defsubst org-re-property (property)
  13649. "Return a regexp matching a PROPERTY line.
  13650. Match group 1 will be set to the value."
  13651. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13652. (defsubst org-re-property-keyword (property)
  13653. "Return a regexp matching a PROPERTY line, possibly with no
  13654. value for the property."
  13655. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13656. (defun org-property-action ()
  13657. "Do an action on properties."
  13658. (interactive)
  13659. (let (c)
  13660. (org-at-property-p)
  13661. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13662. (setq c (read-char-exclusive))
  13663. (cond
  13664. ((equal c ?s)
  13665. (call-interactively 'org-set-property))
  13666. ((equal c ?d)
  13667. (call-interactively 'org-delete-property))
  13668. ((equal c ?D)
  13669. (call-interactively 'org-delete-property-globally))
  13670. ((equal c ?c)
  13671. (call-interactively 'org-compute-property-at-point))
  13672. (t (error "No such property action %c" c)))))
  13673. (defun org-inc-effort ()
  13674. "Increment the value of the effort property in the current entry."
  13675. (interactive)
  13676. (org-set-effort nil t))
  13677. (defvar org-clock-effort) ;; Defined in org-clock.el
  13678. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13679. (defun org-set-effort (&optional value increment)
  13680. "Set the effort property of the current entry.
  13681. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13682. 10th allowed value.
  13683. When INCREMENT is non-nil, set the property to the next allowed value."
  13684. (interactive "P")
  13685. (if (equal value 0) (setq value 10))
  13686. (let* ((completion-ignore-case t)
  13687. (prop org-effort-property)
  13688. (cur (org-entry-get nil prop))
  13689. (allowed (org-property-get-allowed-values nil prop 'table))
  13690. (existing (mapcar 'list (org-property-values prop)))
  13691. (heading (nth 4 (org-heading-components)))
  13692. rpl
  13693. (val (cond
  13694. ((stringp value) value)
  13695. ((and allowed (integerp value))
  13696. (or (car (nth (1- value) allowed))
  13697. (car (org-last allowed))))
  13698. ((and allowed increment)
  13699. (or (caadr (member (list cur) allowed))
  13700. (error "Allowed effort values are not set")))
  13701. (allowed
  13702. (message "Select 1-9,0, [RET%s]: %s"
  13703. (if cur (concat "=" cur) "")
  13704. (mapconcat 'car allowed " "))
  13705. (setq rpl (read-char-exclusive))
  13706. (if (equal rpl ?\r)
  13707. cur
  13708. (setq rpl (- rpl ?0))
  13709. (if (equal rpl 0) (setq rpl 10))
  13710. (if (and (> rpl 0) (<= rpl (length allowed)))
  13711. (car (nth (1- rpl) allowed))
  13712. (org-completing-read "Effort: " allowed nil))))
  13713. (t
  13714. (let (org-completion-use-ido org-completion-use-iswitchb)
  13715. (org-completing-read
  13716. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13717. (concat "[" cur "]") "")
  13718. ": ")
  13719. existing nil nil "" nil cur))))))
  13720. (unless (equal (org-entry-get nil prop) val)
  13721. (org-entry-put nil prop val))
  13722. (save-excursion
  13723. (org-back-to-heading t)
  13724. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13725. (when (string= heading org-clock-current-task)
  13726. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13727. (org-clock-update-mode-line))
  13728. (message "%s is now %s" prop val)))
  13729. (defun org-at-property-p ()
  13730. "Is cursor inside a property drawer?"
  13731. (save-excursion
  13732. (beginning-of-line 1)
  13733. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13734. (save-match-data ;; Used by calling procedures
  13735. (let ((p (point))
  13736. (range (unless (org-before-first-heading-p)
  13737. (org-get-property-block))))
  13738. (and range (<= (car range) p) (< p (cdr range))))))))
  13739. (defun org-get-property-block (&optional beg end force)
  13740. "Return the (beg . end) range of the body of the property drawer.
  13741. BEG and END are the beginning and end of the current subtree, or of
  13742. the part before the first headline. If they are not given, they will
  13743. be found. If the drawer does not exist and FORCE is non-nil, create
  13744. the drawer."
  13745. (catch 'exit
  13746. (save-excursion
  13747. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13748. (progn (org-back-to-heading t) (point))))
  13749. (end (or end (and (not (outline-next-heading)) (point-max))
  13750. (point))))
  13751. (goto-char beg)
  13752. (if (re-search-forward org-property-start-re end t)
  13753. (setq beg (1+ (match-end 0)))
  13754. (if force
  13755. (save-excursion
  13756. (org-insert-property-drawer)
  13757. (setq end (progn (outline-next-heading) (point))))
  13758. (throw 'exit nil))
  13759. (goto-char beg)
  13760. (if (re-search-forward org-property-start-re end t)
  13761. (setq beg (1+ (match-end 0)))))
  13762. (if (re-search-forward org-property-end-re end t)
  13763. (setq end (match-beginning 0))
  13764. (or force (throw 'exit nil))
  13765. (goto-char beg)
  13766. (setq end beg)
  13767. (org-indent-line)
  13768. (insert ":END:\n"))
  13769. (cons beg end)))))
  13770. (defun org-entry-properties (&optional pom which specific)
  13771. "Get all properties of the entry at point-or-marker POM.
  13772. This includes the TODO keyword, the tags, time strings for deadline,
  13773. scheduled, and clocking, and any additional properties defined in the
  13774. entry. The return value is an alist, keys may occur multiple times
  13775. if the property key was used several times.
  13776. POM may also be nil, in which case the current entry is used.
  13777. If WHICH is nil or `all', get all properties. If WHICH is
  13778. `special' or `standard', only get that subclass. If WHICH
  13779. is a string only get exactly this property. SPECIFIC can be a string, the
  13780. specific property we are interested in. Specifying it can speed
  13781. things up because then unnecessary parsing is avoided."
  13782. (setq which (or which 'all))
  13783. (org-with-point-at pom
  13784. (let ((clockstr (substring org-clock-string 0 -1))
  13785. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13786. (case-fold-search nil)
  13787. beg end range props sum-props key key1 value string clocksum clocksumt)
  13788. (save-excursion
  13789. (when (condition-case nil
  13790. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13791. (error nil))
  13792. (setq beg (point))
  13793. (setq sum-props (get-text-property (point) 'org-summaries))
  13794. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13795. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13796. (outline-next-heading)
  13797. (setq end (point))
  13798. (when (memq which '(all special))
  13799. ;; Get the special properties, like TODO and tags
  13800. (goto-char beg)
  13801. (when (and (or (not specific) (string= specific "TODO"))
  13802. (looking-at org-todo-line-regexp) (match-end 2))
  13803. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13804. (when (and (or (not specific) (string= specific "PRIORITY"))
  13805. (looking-at org-priority-regexp))
  13806. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13807. (when (or (not specific) (string= specific "FILE"))
  13808. (push (cons "FILE" buffer-file-name) props))
  13809. (when (and (or (not specific) (string= specific "TAGS"))
  13810. (setq value (org-get-tags-string))
  13811. (string-match "\\S-" value))
  13812. (push (cons "TAGS" value) props))
  13813. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13814. (setq value (org-get-tags-at)))
  13815. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13816. ":"))
  13817. props))
  13818. (when (or (not specific) (string= specific "BLOCKED"))
  13819. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13820. (when (or (not specific)
  13821. (member specific
  13822. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13823. "TIMESTAMP" "TIMESTAMP_IA")))
  13824. (catch 'match
  13825. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13826. (setq key (if (match-end 1)
  13827. (substring (org-match-string-no-properties 1)
  13828. 0 -1))
  13829. string (if (equal key clockstr)
  13830. (org-trim
  13831. (buffer-substring-no-properties
  13832. (match-beginning 3) (goto-char
  13833. (point-at-eol))))
  13834. (substring (org-match-string-no-properties 3)
  13835. 1 -1)))
  13836. ;; Get the correct property name from the key. This is
  13837. ;; necessary if the user has configured time keywords.
  13838. (setq key1 (concat key ":"))
  13839. (cond
  13840. ((not key)
  13841. (setq key
  13842. (if (= (char-after (match-beginning 3)) ?\[)
  13843. "TIMESTAMP_IA" "TIMESTAMP")))
  13844. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13845. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13846. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13847. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13848. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13849. (progn
  13850. (push (cons key string) props)
  13851. ;; no need to search further if match is found
  13852. (throw 'match t))
  13853. (when (or (equal key "CLOCK") (not (assoc key props)))
  13854. (push (cons key string) props)))))))
  13855. (when (memq which '(all standard))
  13856. ;; Get the standard properties, like :PROP: ...
  13857. (setq range (org-get-property-block beg end))
  13858. (when range
  13859. (goto-char (car range))
  13860. (while (re-search-forward
  13861. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13862. (cdr range) t)
  13863. (setq key (org-match-string-no-properties 1)
  13864. value (org-trim (or (org-match-string-no-properties 2) "")))
  13865. (unless (member key excluded)
  13866. (push (cons key (or value "")) props)))))
  13867. (if clocksum
  13868. (push (cons "CLOCKSUM"
  13869. (org-columns-number-to-string (/ (float clocksum) 60.)
  13870. 'add_times))
  13871. props))
  13872. (if clocksumt
  13873. (push (cons "CLOCKSUM_T"
  13874. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13875. 'add_times))
  13876. props))
  13877. (unless (assoc "CATEGORY" props)
  13878. (push (cons "CATEGORY" (org-get-category)) props))
  13879. (append sum-props (nreverse props)))))))
  13880. (defun org-entry-get (pom property &optional inherit literal-nil)
  13881. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13882. If INHERIT is non-nil and the entry does not have the property,
  13883. then also check higher levels of the hierarchy.
  13884. If INHERIT is the symbol `selective', use inheritance only if the setting
  13885. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13886. If the property is present but empty, the return value is the empty string.
  13887. If the property is not present at all, nil is returned.
  13888. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13889. do not interpret it as the list atom nil. This is used for inheritance
  13890. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13891. (org-with-point-at pom
  13892. (if (and inherit (if (eq inherit 'selective)
  13893. (org-property-inherit-p property)
  13894. t))
  13895. (org-entry-get-with-inheritance property literal-nil)
  13896. (if (member property org-special-properties)
  13897. ;; We need a special property. Use `org-entry-properties' to
  13898. ;; retrieve it, but specify the wanted property
  13899. (cdr (assoc property (org-entry-properties nil 'special property)))
  13900. (let ((range (org-get-property-block)))
  13901. (when (and range (not (eq (car range) (cdr range))))
  13902. (let* ((props (list (or (assoc property org-file-properties)
  13903. (assoc property org-global-properties)
  13904. (assoc property org-global-properties-fixed))))
  13905. (ap (lambda (key)
  13906. (when (re-search-forward
  13907. (org-re-property key) (cdr range) t)
  13908. (setq props
  13909. (org-update-property-plist
  13910. key
  13911. (if (match-end 1)
  13912. (org-match-string-no-properties 1) "")
  13913. props)))))
  13914. val)
  13915. (goto-char (car range))
  13916. (funcall ap property)
  13917. (goto-char (car range))
  13918. (while (funcall ap (concat property "+")))
  13919. (setq val (cdr (assoc property props)))
  13920. (when val (if literal-nil val (org-not-nil val))))))))))
  13921. (defun org-property-or-variable-value (var &optional inherit)
  13922. "Check if there is a property fixing the value of VAR.
  13923. If yes, return this value. If not, return the current value of the variable."
  13924. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13925. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13926. (read prop)
  13927. (symbol-value var))))
  13928. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13929. "Delete the property PROPERTY from entry at point-or-marker POM.
  13930. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13931. an empty drawer to delete."
  13932. (org-with-point-at pom
  13933. (if (member property org-special-properties)
  13934. nil ; cannot delete these properties.
  13935. (let ((range (org-get-property-block)))
  13936. (if (and range
  13937. (goto-char (car range))
  13938. (re-search-forward
  13939. (org-re-property property)
  13940. (cdr range) t))
  13941. (progn
  13942. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13943. (and delete-empty-drawer
  13944. (org-remove-empty-drawer-at
  13945. delete-empty-drawer (car range)))
  13946. t)
  13947. nil)))))
  13948. ;; Multi-values properties are properties that contain multiple values
  13949. ;; These values are assumed to be single words, separated by whitespace.
  13950. (defun org-entry-add-to-multivalued-property (pom property value)
  13951. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13952. (let* ((old (org-entry-get pom property))
  13953. (values (and old (org-split-string old "[ \t]"))))
  13954. (setq value (org-entry-protect-space value))
  13955. (unless (member value values)
  13956. (setq values (append values (list value)))
  13957. (org-entry-put pom property
  13958. (mapconcat 'identity values " ")))))
  13959. (defun org-entry-remove-from-multivalued-property (pom property value)
  13960. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13961. (let* ((old (org-entry-get pom property))
  13962. (values (and old (org-split-string old "[ \t]"))))
  13963. (setq value (org-entry-protect-space value))
  13964. (when (member value values)
  13965. (setq values (delete value values))
  13966. (org-entry-put pom property
  13967. (mapconcat 'identity values " ")))))
  13968. (defun org-entry-member-in-multivalued-property (pom property value)
  13969. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13970. (let* ((old (org-entry-get pom property))
  13971. (values (and old (org-split-string old "[ \t]"))))
  13972. (setq value (org-entry-protect-space value))
  13973. (member value values)))
  13974. (defun org-entry-get-multivalued-property (pom property)
  13975. "Return a list of values in a multivalued property."
  13976. (let* ((value (org-entry-get pom property))
  13977. (values (and value (org-split-string value "[ \t]"))))
  13978. (mapcar 'org-entry-restore-space values)))
  13979. (defun org-entry-put-multivalued-property (pom property &rest values)
  13980. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13981. VALUES should be a list of strings. Spaces will be protected."
  13982. (org-entry-put pom property
  13983. (mapconcat 'org-entry-protect-space values " "))
  13984. (let* ((value (org-entry-get pom property))
  13985. (values (and value (org-split-string value "[ \t]"))))
  13986. (mapcar 'org-entry-restore-space values)))
  13987. (defun org-entry-protect-space (s)
  13988. "Protect spaces and newline in string S."
  13989. (while (string-match " " s)
  13990. (setq s (replace-match "%20" t t s)))
  13991. (while (string-match "\n" s)
  13992. (setq s (replace-match "%0A" t t s)))
  13993. s)
  13994. (defun org-entry-restore-space (s)
  13995. "Restore spaces and newline in string S."
  13996. (while (string-match "%20" s)
  13997. (setq s (replace-match " " t t s)))
  13998. (while (string-match "%0A" s)
  13999. (setq s (replace-match "\n" t t s)))
  14000. s)
  14001. (defvar org-entry-property-inherited-from (make-marker)
  14002. "Marker pointing to the entry from where a property was inherited.
  14003. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14004. location of the entry where the inheritance search matched. If there was
  14005. no match, the marker will point nowhere.
  14006. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14007. is set.")
  14008. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14009. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14010. The search will stop at the first ancestor which has the property defined.
  14011. If the value found is \"nil\", return nil to show that the property
  14012. should be considered as undefined (this is the meaning of nil here).
  14013. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14014. (move-marker org-entry-property-inherited-from nil)
  14015. (let (tmp)
  14016. (save-excursion
  14017. (save-restriction
  14018. (widen)
  14019. (catch 'ex
  14020. (while t
  14021. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14022. (or (ignore-errors (org-back-to-heading t))
  14023. (goto-char (point-min)))
  14024. (move-marker org-entry-property-inherited-from (point))
  14025. (throw 'ex tmp))
  14026. (or (ignore-errors (org-up-heading-safe))
  14027. (throw 'ex nil))))))
  14028. (setq tmp (or tmp
  14029. (cdr (assoc property org-file-properties))
  14030. (cdr (assoc property org-global-properties))
  14031. (cdr (assoc property org-global-properties-fixed))))
  14032. (if literal-nil tmp (org-not-nil tmp))))
  14033. (defvar org-property-changed-functions nil
  14034. "Hook called when the value of a property has changed.
  14035. Each hook function should accept two arguments, the name of the property
  14036. and the new value.")
  14037. (defun org-entry-put (pom property value)
  14038. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14039. (org-with-point-at pom
  14040. (org-back-to-heading t)
  14041. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14042. range)
  14043. (cond
  14044. ((equal property "TODO")
  14045. (when (and (stringp value) (string-match "\\S-" value)
  14046. (not (member value org-todo-keywords-1)))
  14047. (error "\"%s\" is not a valid TODO state" value))
  14048. (if (or (not value)
  14049. (not (string-match "\\S-" value)))
  14050. (setq value 'none))
  14051. (org-todo value)
  14052. (org-set-tags nil 'align))
  14053. ((equal property "PRIORITY")
  14054. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14055. (string-to-char value) ?\ ))
  14056. (org-set-tags nil 'align))
  14057. ((equal property "CLOCKSUM")
  14058. (if (not (re-search-forward
  14059. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14060. (error "Cannot find a clock log")
  14061. (goto-char (- (match-end 1) 2))
  14062. (cond
  14063. ((eq value 'earlier) (org-timestamp-down))
  14064. ((eq value 'later) (org-timestamp-up)))
  14065. (org-clock-sum-current-item)))
  14066. ((equal property "SCHEDULED")
  14067. (if (re-search-forward org-scheduled-time-regexp end t)
  14068. (cond
  14069. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14070. ((eq value 'later) (org-timestamp-change 1 'day))
  14071. (t (call-interactively 'org-schedule)))
  14072. (call-interactively 'org-schedule)))
  14073. ((equal property "DEADLINE")
  14074. (if (re-search-forward org-deadline-time-regexp end t)
  14075. (cond
  14076. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14077. ((eq value 'later) (org-timestamp-change 1 'day))
  14078. (t (call-interactively 'org-deadline)))
  14079. (call-interactively 'org-deadline)))
  14080. ((member property org-special-properties)
  14081. (error "The %s property can not yet be set with `org-entry-put'"
  14082. property))
  14083. (t ; a non-special property
  14084. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14085. (setq range (org-get-property-block beg end 'force))
  14086. (goto-char (car range))
  14087. (if (re-search-forward
  14088. (org-re-property-keyword property) (cdr range) t)
  14089. (progn
  14090. (delete-region (match-beginning 0) (match-end 0))
  14091. (goto-char (match-beginning 0)))
  14092. (goto-char (cdr range))
  14093. (insert "\n")
  14094. (backward-char 1)
  14095. (org-indent-line))
  14096. (insert ":" property ":")
  14097. (and value (insert " " value))
  14098. (org-indent-line)))))
  14099. (run-hook-with-args 'org-property-changed-functions property value)))
  14100. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14101. "Get all property keys in the current buffer.
  14102. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14103. like tags and TODO state.
  14104. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14105. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14106. and others.
  14107. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14108. formats in the current buffer."
  14109. (let (rtn range cfmt s p)
  14110. (save-excursion
  14111. (save-restriction
  14112. (widen)
  14113. (goto-char (point-min))
  14114. (while (re-search-forward org-property-start-re nil t)
  14115. (setq range (org-get-property-block))
  14116. (goto-char (car range))
  14117. (while (re-search-forward
  14118. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14119. (cdr range) t)
  14120. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14121. (outline-next-heading))))
  14122. (when include-specials
  14123. (setq rtn (append org-special-properties rtn)))
  14124. (when include-defaults
  14125. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14126. (add-to-list 'rtn org-effort-property))
  14127. (when include-columns
  14128. (save-excursion
  14129. (save-restriction
  14130. (widen)
  14131. (goto-char (point-min))
  14132. (while (re-search-forward
  14133. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14134. nil t)
  14135. (setq cfmt (match-string 2) s 0)
  14136. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14137. cfmt s)
  14138. (setq s (match-end 0)
  14139. p (match-string 1 cfmt))
  14140. (unless (or (equal p "ITEM")
  14141. (member p org-special-properties))
  14142. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14143. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14144. (defun org-property-values (key)
  14145. "Return a list of all values of property KEY in the current buffer."
  14146. (save-excursion
  14147. (save-restriction
  14148. (widen)
  14149. (goto-char (point-min))
  14150. (let ((re (org-re-property key))
  14151. values)
  14152. (while (re-search-forward re nil t)
  14153. (add-to-list 'values (org-trim (match-string 1))))
  14154. (delete "" values)))))
  14155. (defun org-insert-property-drawer ()
  14156. "Insert a property drawer into the current entry."
  14157. (org-back-to-heading t)
  14158. (looking-at org-outline-regexp)
  14159. (let ((indent (if org-adapt-indentation
  14160. (- (match-end 0) (match-beginning 0))
  14161. 0))
  14162. (beg (point))
  14163. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14164. end hiddenp)
  14165. (outline-next-heading)
  14166. (setq end (point))
  14167. (goto-char beg)
  14168. (while (re-search-forward re end t))
  14169. (setq hiddenp (outline-invisible-p))
  14170. (end-of-line 1)
  14171. (and (equal (char-after) ?\n) (forward-char 1))
  14172. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14173. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14174. ;; just skip this line
  14175. (beginning-of-line 2)
  14176. ;; Drawer start, find the end
  14177. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14178. (beginning-of-line 1)))
  14179. (org-skip-over-state-notes)
  14180. (skip-chars-backward " \t\n\r")
  14181. (if (eq (char-before) ?*) (forward-char 1))
  14182. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14183. (beginning-of-line 0)
  14184. (org-indent-to-column indent)
  14185. (beginning-of-line 2)
  14186. (org-indent-to-column indent)
  14187. (beginning-of-line 0)
  14188. (if hiddenp
  14189. (save-excursion
  14190. (org-back-to-heading t)
  14191. (hide-entry))
  14192. (org-flag-drawer t))))
  14193. (defun org-insert-drawer (&optional arg drawer)
  14194. "Insert a drawer at point.
  14195. Optional argument DRAWER, when non-nil, is a string representing
  14196. drawer's name. Otherwise, the user is prompted for a name.
  14197. If a region is active, insert the drawer around that region
  14198. instead.
  14199. Point is left between drawer's boundaries."
  14200. (interactive "P")
  14201. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14202. "LOGBOOK"))
  14203. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14204. ;; internally by Org. They are meant to be inserted
  14205. ;; automatically.
  14206. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14207. ;; Remove system drawers from list. Note: For some reason,
  14208. ;; `org-completing-read' ignores the predicate while
  14209. ;; `completing-read' handles it fine.
  14210. (drawer (if arg "PROPERTIES"
  14211. (or drawer
  14212. (completing-read
  14213. "Drawer: " org-drawers
  14214. (lambda (d) (not (member d system-drawers))))))))
  14215. (cond
  14216. ;; With C-u, fall back on `org-insert-property-drawer'
  14217. (arg (org-insert-property-drawer))
  14218. ;; With an active region, insert a drawer at point.
  14219. ((not (org-region-active-p))
  14220. (progn
  14221. (unless (bolp) (insert "\n"))
  14222. (insert (format ":%s:\n\n:END:\n" drawer))
  14223. (forward-line -2)))
  14224. ;; Otherwise, insert the drawer at point
  14225. (t
  14226. (let ((rbeg (region-beginning))
  14227. (rend (copy-marker (region-end))))
  14228. (unwind-protect
  14229. (progn
  14230. (goto-char rbeg)
  14231. (beginning-of-line)
  14232. (when (save-excursion
  14233. (re-search-forward org-outline-regexp-bol rend t))
  14234. (error "Drawers cannot contain headlines"))
  14235. ;; Position point at the beginning of the first
  14236. ;; non-blank line in region. Insert drawer's opening
  14237. ;; there, then indent it.
  14238. (org-skip-whitespace)
  14239. (beginning-of-line)
  14240. (insert ":" drawer ":\n")
  14241. (forward-line -1)
  14242. (indent-for-tab-command)
  14243. ;; Move point to the beginning of the first blank line
  14244. ;; after the last non-blank line in region. Insert
  14245. ;; drawer's closing, then indent it.
  14246. (goto-char rend)
  14247. (skip-chars-backward " \r\t\n")
  14248. (insert "\n:END:")
  14249. (deactivate-mark t)
  14250. (indent-for-tab-command)
  14251. (unless (eolp) (insert "\n")))
  14252. ;; Clear marker, whatever the outcome of insertion is.
  14253. (set-marker rend nil)))))))
  14254. (defvar org-property-set-functions-alist nil
  14255. "Property set function alist.
  14256. Each entry should have the following format:
  14257. (PROPERTY . READ-FUNCTION)
  14258. The read function will be called with the same argument as
  14259. `org-completing-read'.")
  14260. (defun org-set-property-function (property)
  14261. "Get the function that should be used to set PROPERTY.
  14262. This is computed according to `org-property-set-functions-alist'."
  14263. (or (cdr (assoc property org-property-set-functions-alist))
  14264. 'org-completing-read))
  14265. (defun org-read-property-value (property)
  14266. "Read PROPERTY value from user."
  14267. (let* ((completion-ignore-case t)
  14268. (allowed (org-property-get-allowed-values nil property 'table))
  14269. (cur (org-entry-get nil property))
  14270. (prompt (concat property " value"
  14271. (if (and cur (string-match "\\S-" cur))
  14272. (concat " [" cur "]") "") ": "))
  14273. (set-function (org-set-property-function property))
  14274. (val (if allowed
  14275. (funcall set-function prompt allowed nil
  14276. (not (get-text-property 0 'org-unrestricted
  14277. (caar allowed))))
  14278. (let (org-completion-use-ido org-completion-use-iswitchb)
  14279. (funcall set-function prompt
  14280. (mapcar 'list (org-property-values property))
  14281. nil nil "" nil cur)))))
  14282. (if (equal val "")
  14283. cur
  14284. val)))
  14285. (defvar org-last-set-property nil)
  14286. (defvar org-last-set-property-value nil)
  14287. (defun org-read-property-name ()
  14288. "Read a property name."
  14289. (let* ((completion-ignore-case t)
  14290. (keys (org-buffer-property-keys nil t t))
  14291. (default-prop (or (save-excursion
  14292. (save-match-data
  14293. (beginning-of-line)
  14294. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14295. (null (string= (match-string 1) "END"))
  14296. (match-string 1))))
  14297. org-last-set-property))
  14298. (property (org-icompleting-read
  14299. (concat "Property"
  14300. (if default-prop (concat " [" default-prop "]") "")
  14301. ": ")
  14302. (mapcar 'list keys)
  14303. nil nil nil nil
  14304. default-prop)))
  14305. (if (member property keys)
  14306. property
  14307. (or (cdr (assoc (downcase property)
  14308. (mapcar (lambda (x) (cons (downcase x) x))
  14309. keys)))
  14310. property))))
  14311. (defun org-set-property-and-value (use-last)
  14312. "Allow to set [PROPERTY]: [value] direction from prompt.
  14313. When use-default, don't even ask, just use the last
  14314. \"[PROPERTY]: [value]\" string from the history."
  14315. (interactive "P")
  14316. (let* ((completion-ignore-case t)
  14317. (pv (or (and use-last org-last-set-property-value)
  14318. (org-completing-read
  14319. "Enter a \"[Property]: [value]\" pair: "
  14320. nil nil nil nil nil
  14321. org-last-set-property-value)))
  14322. prop val)
  14323. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14324. (setq prop (match-string 1 pv)
  14325. val (match-string 2 pv))
  14326. (org-set-property prop val))))
  14327. (defun org-set-property (property value)
  14328. "In the current entry, set PROPERTY to VALUE.
  14329. When called interactively, this will prompt for a property name, offering
  14330. completion on existing and default properties. And then it will prompt
  14331. for a value, offering completion either on allowed values (via an inherited
  14332. xxx_ALL property) or on existing values in other instances of this property
  14333. in the current file."
  14334. (interactive (list nil nil))
  14335. (let* ((property (or property (org-read-property-name)))
  14336. (value (or value (org-read-property-value property)))
  14337. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14338. (setq org-last-set-property property)
  14339. (setq org-last-set-property-value (concat property ": " value))
  14340. ;; Possibly postprocess the inserted value:
  14341. (when fn (setq value (funcall fn value)))
  14342. (unless (equal (org-entry-get nil property) value)
  14343. (org-entry-put nil property value))))
  14344. (defun org-delete-property (property &optional delete-empty-drawer)
  14345. "In the current entry, delete PROPERTY.
  14346. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14347. an empty drawer to delete."
  14348. (interactive
  14349. (let* ((completion-ignore-case t)
  14350. (prop (org-icompleting-read "Property: "
  14351. (org-entry-properties nil 'standard))))
  14352. (list prop)))
  14353. (message "Property %s %s" property
  14354. (if (org-entry-delete nil property delete-empty-drawer)
  14355. "deleted"
  14356. "was not present in the entry")))
  14357. (defun org-delete-property-globally (property)
  14358. "Remove PROPERTY globally, from all entries."
  14359. (interactive
  14360. (let* ((completion-ignore-case t)
  14361. (prop (org-icompleting-read
  14362. "Globally remove property: "
  14363. (mapcar 'list (org-buffer-property-keys)))))
  14364. (list prop)))
  14365. (save-excursion
  14366. (save-restriction
  14367. (widen)
  14368. (goto-char (point-min))
  14369. (let ((cnt 0))
  14370. (while (re-search-forward
  14371. (org-re-property property)
  14372. nil t)
  14373. (setq cnt (1+ cnt))
  14374. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14375. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14376. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14377. (defun org-compute-property-at-point ()
  14378. "Compute the property at point.
  14379. This looks for an enclosing column format, extracts the operator and
  14380. then applies it to the property in the column format's scope."
  14381. (interactive)
  14382. (unless (org-at-property-p)
  14383. (error "Not at a property"))
  14384. (let ((prop (org-match-string-no-properties 2)))
  14385. (org-columns-get-format-and-top-level)
  14386. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14387. (error "No operator defined for property %s" prop))
  14388. (org-columns-compute prop)))
  14389. (defvar org-property-allowed-value-functions nil
  14390. "Hook for functions supplying allowed values for a specific property.
  14391. The functions must take a single argument, the name of the property, and
  14392. return a flat list of allowed values. If \":ETC\" is one of
  14393. the values, this means that these values are intended as defaults for
  14394. completion, but that other values should be allowed too.
  14395. The functions must return nil if they are not responsible for this
  14396. property.")
  14397. (defun org-property-get-allowed-values (pom property &optional table)
  14398. "Get allowed values for the property PROPERTY.
  14399. When TABLE is non-nil, return an alist that can directly be used for
  14400. completion."
  14401. (let (vals)
  14402. (cond
  14403. ((equal property "TODO")
  14404. (setq vals (org-with-point-at pom
  14405. (append org-todo-keywords-1 '("")))))
  14406. ((equal property "PRIORITY")
  14407. (let ((n org-lowest-priority))
  14408. (while (>= n org-highest-priority)
  14409. (push (char-to-string n) vals)
  14410. (setq n (1- n)))))
  14411. ((member property org-special-properties))
  14412. ((setq vals (run-hook-with-args-until-success
  14413. 'org-property-allowed-value-functions property)))
  14414. (t
  14415. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14416. (when (and vals (string-match "\\S-" vals))
  14417. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14418. (setq vals (mapcar (lambda (x)
  14419. (cond ((stringp x) x)
  14420. ((numberp x) (number-to-string x))
  14421. ((symbolp x) (symbol-name x))
  14422. (t "???")))
  14423. vals)))))
  14424. (when (member ":ETC" vals)
  14425. (setq vals (remove ":ETC" vals))
  14426. (org-add-props (car vals) '(org-unrestricted t)))
  14427. (if table (mapcar 'list vals) vals)))
  14428. (defun org-property-previous-allowed-value (&optional previous)
  14429. "Switch to the next allowed value for this property."
  14430. (interactive)
  14431. (org-property-next-allowed-value t))
  14432. (defun org-property-next-allowed-value (&optional previous)
  14433. "Switch to the next allowed value for this property."
  14434. (interactive)
  14435. (unless (org-at-property-p)
  14436. (error "Not at a property"))
  14437. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14438. (key (match-string 2))
  14439. (value (match-string 3))
  14440. (allowed (or (org-property-get-allowed-values (point) key)
  14441. (and (member value '("[ ]" "[-]" "[X]"))
  14442. '("[ ]" "[X]"))))
  14443. (heading (save-match-data (nth 4 (org-heading-components))))
  14444. nval)
  14445. (unless allowed
  14446. (error "Allowed values for this property have not been defined"))
  14447. (if previous (setq allowed (reverse allowed)))
  14448. (if (member value allowed)
  14449. (setq nval (car (cdr (member value allowed)))))
  14450. (setq nval (or nval (car allowed)))
  14451. (if (equal nval value)
  14452. (error "Only one allowed value for this property"))
  14453. (org-at-property-p)
  14454. (replace-match (concat " :" key ": " nval) t t)
  14455. (org-indent-line)
  14456. (beginning-of-line 1)
  14457. (skip-chars-forward " \t")
  14458. (when (equal prop org-effort-property)
  14459. (save-excursion
  14460. (org-back-to-heading t)
  14461. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14462. (when (string= org-clock-current-task heading)
  14463. (setq org-clock-effort nval)
  14464. (org-clock-update-mode-line)))
  14465. (run-hook-with-args 'org-property-changed-functions key nval)))
  14466. (defun org-find-olp (path &optional this-buffer)
  14467. "Return a marker pointing to the entry at outline path OLP.
  14468. If anything goes wrong, throw an error.
  14469. You can wrap this call to catch the error like this:
  14470. (condition-case msg
  14471. (org-mobile-locate-entry (match-string 4))
  14472. (error (nth 1 msg)))
  14473. The return value will then be either a string with the error message,
  14474. or a marker if everything is OK.
  14475. If THIS-BUFFER is set, the outline path does not contain a file,
  14476. only headings."
  14477. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14478. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14479. (level 1)
  14480. (lmin 1)
  14481. (lmax 1)
  14482. limit re end found pos heading cnt flevel)
  14483. (unless buffer (error "File not found :%s" file))
  14484. (with-current-buffer buffer
  14485. (save-excursion
  14486. (save-restriction
  14487. (widen)
  14488. (setq limit (point-max))
  14489. (goto-char (point-min))
  14490. (while (setq heading (pop path))
  14491. (setq re (format org-complex-heading-regexp-format
  14492. (regexp-quote heading)))
  14493. (setq cnt 0 pos (point))
  14494. (while (re-search-forward re end t)
  14495. (setq level (- (match-end 1) (match-beginning 1)))
  14496. (if (and (>= level lmin) (<= level lmax))
  14497. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14498. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14499. lmax heading))
  14500. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14501. lmax heading))
  14502. (goto-char found)
  14503. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14504. (setq end (save-excursion (org-end-of-subtree t t))))
  14505. (when (org-at-heading-p)
  14506. (point-marker)))))))
  14507. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14508. "Find node HEADING in BUFFER.
  14509. Return a marker to the heading if it was found, or nil if not.
  14510. If POS-ONLY is set, return just the position instead of a marker.
  14511. The heading text must match exact, but it may have a TODO keyword,
  14512. a priority cookie and tags in the standard locations."
  14513. (with-current-buffer (or buffer (current-buffer))
  14514. (save-excursion
  14515. (save-restriction
  14516. (widen)
  14517. (goto-char (point-min))
  14518. (let (case-fold-search)
  14519. (if (re-search-forward
  14520. (format org-complex-heading-regexp-format
  14521. (regexp-quote heading)) nil t)
  14522. (if pos-only
  14523. (match-beginning 0)
  14524. (move-marker (make-marker) (match-beginning 0)))))))))
  14525. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14526. "Find Org node headline HEADING in all .org files in directory DIR.
  14527. When the target headline is found, return a marker to this location."
  14528. (let ((files (directory-files (or dir default-directory)
  14529. nil "\\`[^.#].*\\.org\\'"))
  14530. file visiting m buffer)
  14531. (catch 'found
  14532. (while (setq file (pop files))
  14533. (message "trying %s" file)
  14534. (setq visiting (org-find-base-buffer-visiting file))
  14535. (setq buffer (or visiting (find-file-noselect file)))
  14536. (setq m (org-find-exact-headline-in-buffer
  14537. heading buffer))
  14538. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14539. (and m (throw 'found m))))))
  14540. (defun org-find-entry-with-id (ident)
  14541. "Locate the entry that contains the ID property with exact value IDENT.
  14542. IDENT can be a string, a symbol or a number, this function will search for
  14543. the string representation of it.
  14544. Return the position where this entry starts, or nil if there is no such entry."
  14545. (interactive "sID: ")
  14546. (let ((id (cond
  14547. ((stringp ident) ident)
  14548. ((symbol-name ident) (symbol-name ident))
  14549. ((numberp ident) (number-to-string ident))
  14550. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14551. (case-fold-search nil))
  14552. (save-excursion
  14553. (save-restriction
  14554. (widen)
  14555. (goto-char (point-min))
  14556. (when (re-search-forward
  14557. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14558. nil t)
  14559. (org-back-to-heading t)
  14560. (point))))))
  14561. ;;;; Timestamps
  14562. (defvar org-last-changed-timestamp nil)
  14563. (defvar org-last-inserted-timestamp nil
  14564. "The last time stamp inserted with `org-insert-time-stamp'.")
  14565. (defvar org-time-was-given) ; dynamically scoped parameter
  14566. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14567. (defvar org-ts-what) ; dynamically scoped parameter
  14568. (defun org-time-stamp (arg &optional inactive)
  14569. "Prompt for a date/time and insert a time stamp.
  14570. If the user specifies a time like HH:MM or if this command is
  14571. called with at least one prefix argument, the time stamp contains
  14572. the date and the time. Otherwise, only the date is be included.
  14573. All parts of a date not specified by the user is filled in from
  14574. the current date/time. So if you just press return without
  14575. typing anything, the time stamp will represent the current
  14576. date/time.
  14577. If there is already a timestamp at the cursor, it will be
  14578. modified.
  14579. With two universal prefix arguments, insert an active timestamp
  14580. with the current time without prompting the user."
  14581. (interactive "P")
  14582. (let* ((ts nil)
  14583. (default-time
  14584. ;; Default time is either today, or, when entering a range,
  14585. ;; the range start.
  14586. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14587. (save-excursion
  14588. (re-search-backward
  14589. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14590. (- (point) 20) t)))
  14591. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14592. (current-time)))
  14593. (default-input (and ts (org-get-compact-tod ts)))
  14594. (repeater (save-excursion
  14595. (save-match-data
  14596. (beginning-of-line)
  14597. (when (re-search-forward
  14598. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14599. (save-excursion (progn (end-of-line) (point))) t)
  14600. (match-string 0)))))
  14601. org-time-was-given org-end-time-was-given time)
  14602. (cond
  14603. ((and (org-at-timestamp-p t)
  14604. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14605. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14606. (insert "--")
  14607. (setq time (let ((this-command this-command))
  14608. (org-read-date arg 'totime nil nil
  14609. default-time default-input inactive)))
  14610. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14611. ((org-at-timestamp-p t)
  14612. (setq time (let ((this-command this-command))
  14613. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14614. (when (org-at-timestamp-p t) ; just to get the match data
  14615. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14616. (replace-match "")
  14617. (setq org-last-changed-timestamp
  14618. (org-insert-time-stamp
  14619. time (or org-time-was-given arg)
  14620. inactive nil nil (list org-end-time-was-given)))
  14621. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14622. (setq org-last-changed-timestamp
  14623. (concat (substring org-last-inserted-timestamp 0 -1)
  14624. " " repeater ">"))))
  14625. (message "Timestamp updated"))
  14626. ((equal arg '(16))
  14627. (org-insert-time-stamp (current-time) t))
  14628. (t
  14629. (setq time (let ((this-command this-command))
  14630. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14631. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14632. nil nil (list org-end-time-was-given))))))
  14633. ;; FIXME: can we use this for something else, like computing time differences?
  14634. (defun org-get-compact-tod (s)
  14635. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14636. (let* ((t1 (match-string 1 s))
  14637. (h1 (string-to-number (match-string 2 s)))
  14638. (m1 (string-to-number (match-string 3 s)))
  14639. (t2 (and (match-end 4) (match-string 5 s)))
  14640. (h2 (and t2 (string-to-number (match-string 6 s))))
  14641. (m2 (and t2 (string-to-number (match-string 7 s))))
  14642. dh dm)
  14643. (if (not t2)
  14644. t1
  14645. (setq dh (- h2 h1) dm (- m2 m1))
  14646. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14647. (concat t1 "+" (number-to-string dh)
  14648. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14649. (defun org-time-stamp-inactive (&optional arg)
  14650. "Insert an inactive time stamp.
  14651. An inactive time stamp is enclosed in square brackets instead of angle
  14652. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14653. does not link to the calendar and cannot be changed with the S-cursor keys.
  14654. So these are more for recording a certain time/date."
  14655. (interactive "P")
  14656. (org-time-stamp arg 'inactive))
  14657. (defvar org-date-ovl (make-overlay 1 1))
  14658. (overlay-put org-date-ovl 'face 'org-date-selected)
  14659. (org-detach-overlay org-date-ovl)
  14660. (defvar org-ans1) ; dynamically scoped parameter
  14661. (defvar org-ans2) ; dynamically scoped parameter
  14662. (defvar org-plain-time-of-day-regexp) ; defined below
  14663. (defvar org-overriding-default-time nil) ; dynamically scoped
  14664. (defvar org-read-date-overlay nil)
  14665. (defvar org-dcst nil) ; dynamically scoped
  14666. (defvar org-read-date-history nil)
  14667. (defvar org-read-date-final-answer nil)
  14668. (defvar org-read-date-analyze-futurep nil)
  14669. (defvar org-read-date-analyze-forced-year nil)
  14670. (defvar org-read-date-inactive)
  14671. (defvar org-read-date-minibuffer-local-map
  14672. (let ((map (make-sparse-keymap)))
  14673. (set-keymap-parent map minibuffer-local-map)
  14674. (org-defkey map (kbd ".")
  14675. (lambda () (interactive)
  14676. (org-eval-in-calendar '(calendar-goto-today))))
  14677. (org-defkey map [(meta shift left)]
  14678. (lambda () (interactive)
  14679. (org-eval-in-calendar '(calendar-backward-month 1))))
  14680. (org-defkey map [(meta shift right)]
  14681. (lambda () (interactive)
  14682. (org-eval-in-calendar '(calendar-forward-month 1))))
  14683. (org-defkey map [(meta shift up)]
  14684. (lambda () (interactive)
  14685. (org-eval-in-calendar '(calendar-backward-year 1))))
  14686. (org-defkey map [(meta shift down)]
  14687. (lambda () (interactive)
  14688. (org-eval-in-calendar '(calendar-forward-year 1))))
  14689. (org-defkey map [?\e (shift left)]
  14690. (lambda () (interactive)
  14691. (org-eval-in-calendar '(calendar-backward-month 1))))
  14692. (org-defkey map [?\e (shift right)]
  14693. (lambda () (interactive)
  14694. (org-eval-in-calendar '(calendar-forward-month 1))))
  14695. (org-defkey map [?\e (shift up)]
  14696. (lambda () (interactive)
  14697. (org-eval-in-calendar '(calendar-backward-year 1))))
  14698. (org-defkey map [?\e (shift down)]
  14699. (lambda () (interactive)
  14700. (org-eval-in-calendar '(calendar-forward-year 1))))
  14701. (org-defkey map [(shift up)]
  14702. (lambda () (interactive)
  14703. (org-eval-in-calendar '(calendar-backward-week 1))))
  14704. (org-defkey map [(shift down)]
  14705. (lambda () (interactive)
  14706. (org-eval-in-calendar '(calendar-forward-week 1))))
  14707. (org-defkey map [(shift left)]
  14708. (lambda () (interactive)
  14709. (org-eval-in-calendar '(calendar-backward-day 1))))
  14710. (org-defkey map [(shift right)]
  14711. (lambda () (interactive)
  14712. (org-eval-in-calendar '(calendar-forward-day 1))))
  14713. (org-defkey map "!"
  14714. (lambda () (interactive)
  14715. (org-eval-in-calendar '(diary-view-entries))
  14716. (message "")))
  14717. (org-defkey map ">"
  14718. (lambda () (interactive)
  14719. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14720. (org-defkey map "<"
  14721. (lambda () (interactive)
  14722. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14723. (org-defkey map "\C-v"
  14724. (lambda () (interactive)
  14725. (org-eval-in-calendar
  14726. '(calendar-scroll-left-three-months 1))))
  14727. (org-defkey map "\M-v"
  14728. (lambda () (interactive)
  14729. (org-eval-in-calendar
  14730. '(calendar-scroll-right-three-months 1))))
  14731. map)
  14732. "Keymap for minibuffer commands when using `org-read-date'.")
  14733. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14734. default-time default-input inactive)
  14735. "Read a date, possibly a time, and make things smooth for the user.
  14736. The prompt will suggest to enter an ISO date, but you can also enter anything
  14737. which will at least partially be understood by `parse-time-string'.
  14738. Unrecognized parts of the date will default to the current day, month, year,
  14739. hour and minute. If this command is called to replace a timestamp at point,
  14740. or to enter the second timestamp of a range, the default time is taken
  14741. from the existing stamp. Furthermore, the command prefers the future,
  14742. so if you are giving a date where the year is not given, and the day-month
  14743. combination is already past in the current year, it will assume you
  14744. mean next year. For details, see the manual. A few examples:
  14745. 3-2-5 --> 2003-02-05
  14746. feb 15 --> currentyear-02-15
  14747. 2/15 --> currentyear-02-15
  14748. sep 12 9 --> 2009-09-12
  14749. 12:45 --> today 12:45
  14750. 22 sept 0:34 --> currentyear-09-22 0:34
  14751. 12 --> currentyear-currentmonth-12
  14752. Fri --> nearest Friday (today or later)
  14753. etc.
  14754. Furthermore you can specify a relative date by giving, as the *first* thing
  14755. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14756. change in days weeks, months, years.
  14757. With a single plus or minus, the date is relative to today. With a double
  14758. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14759. +4d --> four days from today
  14760. +4 --> same as above
  14761. +2w --> two weeks from today
  14762. ++5 --> five days from default date
  14763. The function understands only English month and weekday abbreviations.
  14764. While prompting, a calendar is popped up - you can also select the
  14765. date with the mouse (button 1). The calendar shows a period of three
  14766. months. To scroll it to other months, use the keys `>' and `<'.
  14767. If you don't like the calendar, turn it off with
  14768. \(setq org-read-date-popup-calendar nil)
  14769. With optional argument TO-TIME, the date will immediately be converted
  14770. to an internal time.
  14771. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14772. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14773. still enter a time, and this function will inform the calling routine
  14774. about this change. The calling routine may then choose to change the
  14775. format used to insert the time stamp into the buffer to include the time.
  14776. With optional argument FROM-STRING, read from this string instead from
  14777. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14778. the time/date that is used for everything that is not specified by the
  14779. user."
  14780. (require 'parse-time)
  14781. (let* ((org-time-stamp-rounding-minutes
  14782. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14783. (org-dcst org-display-custom-times)
  14784. (ct (org-current-time))
  14785. (org-def (or org-overriding-default-time default-time ct))
  14786. (org-defdecode (decode-time org-def))
  14787. (dummy (progn
  14788. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14789. (setcar (nthcdr 2 org-defdecode) -1)
  14790. (setcar (nthcdr 1 org-defdecode) 59)
  14791. (setq org-def (apply 'encode-time org-defdecode)
  14792. org-defdecode (decode-time org-def)))))
  14793. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14794. (calendar-frame-setup nil)
  14795. (calendar-setup nil)
  14796. (calendar-move-hook nil)
  14797. (calendar-view-diary-initially-flag nil)
  14798. (calendar-view-holidays-initially-flag nil)
  14799. (timestr (format-time-string
  14800. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14801. (prompt (concat (if prompt (concat prompt " ") "")
  14802. (format "Date+time [%s]: " timestr)))
  14803. ans (org-ans0 "") org-ans1 org-ans2 final)
  14804. (cond
  14805. (from-string (setq ans from-string))
  14806. (org-read-date-popup-calendar
  14807. (save-excursion
  14808. (save-window-excursion
  14809. (calendar)
  14810. (org-eval-in-calendar '(setq cursor-type nil) t)
  14811. (unwind-protect
  14812. (progn
  14813. (calendar-forward-day (- (time-to-days org-def)
  14814. (calendar-absolute-from-gregorian
  14815. (calendar-current-date))))
  14816. (org-eval-in-calendar nil t)
  14817. (let* ((old-map (current-local-map))
  14818. (map (copy-keymap calendar-mode-map))
  14819. (minibuffer-local-map
  14820. (copy-keymap org-read-date-minibuffer-local-map)))
  14821. (org-defkey map (kbd "RET") 'org-calendar-select)
  14822. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14823. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14824. (unwind-protect
  14825. (progn
  14826. (use-local-map map)
  14827. (setq org-read-date-inactive inactive)
  14828. (add-hook 'post-command-hook 'org-read-date-display)
  14829. (setq org-ans0 (read-string prompt default-input
  14830. 'org-read-date-history nil))
  14831. ;; org-ans0: from prompt
  14832. ;; org-ans1: from mouse click
  14833. ;; org-ans2: from calendar motion
  14834. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14835. (remove-hook 'post-command-hook 'org-read-date-display)
  14836. (use-local-map old-map)
  14837. (when org-read-date-overlay
  14838. (delete-overlay org-read-date-overlay)
  14839. (setq org-read-date-overlay nil)))))
  14840. (bury-buffer "*Calendar*")))))
  14841. (t ; Naked prompt only
  14842. (unwind-protect
  14843. (setq ans (read-string prompt default-input
  14844. 'org-read-date-history timestr))
  14845. (when org-read-date-overlay
  14846. (delete-overlay org-read-date-overlay)
  14847. (setq org-read-date-overlay nil)))))
  14848. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14849. (when org-read-date-analyze-forced-year
  14850. (message "Year was forced into %s"
  14851. (if org-read-date-force-compatible-dates
  14852. "compatible range (1970-2037)"
  14853. "range representable on this machine"))
  14854. (ding))
  14855. ;; One round trip to get rid of 34th of August and stuff like that....
  14856. (setq final (decode-time (apply 'encode-time final)))
  14857. (setq org-read-date-final-answer ans)
  14858. (if to-time
  14859. (apply 'encode-time final)
  14860. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14861. (format "%04d-%02d-%02d %02d:%02d"
  14862. (nth 5 final) (nth 4 final) (nth 3 final)
  14863. (nth 2 final) (nth 1 final))
  14864. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14865. (defvar org-def)
  14866. (defvar org-defdecode)
  14867. (defvar org-with-time)
  14868. (defun org-read-date-display ()
  14869. "Display the current date prompt interpretation in the minibuffer."
  14870. (when org-read-date-display-live
  14871. (when org-read-date-overlay
  14872. (delete-overlay org-read-date-overlay))
  14873. (when (minibufferp (current-buffer))
  14874. (save-excursion
  14875. (end-of-line 1)
  14876. (while (not (equal (buffer-substring
  14877. (max (point-min) (- (point) 4)) (point))
  14878. " "))
  14879. (insert " ")))
  14880. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14881. " " (or org-ans1 org-ans2)))
  14882. (org-end-time-was-given nil)
  14883. (f (org-read-date-analyze ans org-def org-defdecode))
  14884. (fmts (if org-dcst
  14885. org-time-stamp-custom-formats
  14886. org-time-stamp-formats))
  14887. (fmt (if (or org-with-time
  14888. (and (boundp 'org-time-was-given) org-time-was-given))
  14889. (cdr fmts)
  14890. (car fmts)))
  14891. (txt (format-time-string fmt (apply 'encode-time f)))
  14892. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14893. (txt (concat "=> " txt)))
  14894. (when (and org-end-time-was-given
  14895. (string-match org-plain-time-of-day-regexp txt))
  14896. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14897. org-end-time-was-given
  14898. (substring txt (match-end 0)))))
  14899. (when org-read-date-analyze-futurep
  14900. (setq txt (concat txt " (=>F)")))
  14901. (setq org-read-date-overlay
  14902. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14903. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14904. (defun org-read-date-analyze (ans org-def org-defdecode)
  14905. "Analyze the combined answer of the date prompt."
  14906. ;; FIXME: cleanup and comment
  14907. (let ((nowdecode (decode-time (current-time)))
  14908. delta deltan deltaw deltadef year month day
  14909. hour minute second wday pm h2 m2 tl wday1
  14910. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14911. (setq org-read-date-analyze-futurep nil
  14912. org-read-date-analyze-forced-year nil)
  14913. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14914. (setq ans "+0"))
  14915. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14916. (setq ans (replace-match "" t t ans)
  14917. deltan (car delta)
  14918. deltaw (nth 1 delta)
  14919. deltadef (nth 2 delta)))
  14920. ;; Check if there is an iso week date in there. If yes, store the
  14921. ;; info and postpone interpreting it until the rest of the parsing
  14922. ;; is done.
  14923. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14924. (setq iso-year (if (match-end 1)
  14925. (org-small-year-to-year
  14926. (string-to-number (match-string 1 ans))))
  14927. iso-weekday (if (match-end 3)
  14928. (string-to-number (match-string 3 ans)))
  14929. iso-week (string-to-number (match-string 2 ans)))
  14930. (setq ans (replace-match "" t t ans)))
  14931. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14932. (when (string-match
  14933. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14934. (setq year (if (match-end 2)
  14935. (string-to-number (match-string 2 ans))
  14936. (progn (setq kill-year t)
  14937. (string-to-number (format-time-string "%Y"))))
  14938. month (string-to-number (match-string 3 ans))
  14939. day (string-to-number (match-string 4 ans)))
  14940. (if (< year 100) (setq year (+ 2000 year)))
  14941. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14942. t nil ans)))
  14943. ;; Help matching dotted european dates
  14944. (when (string-match
  14945. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14946. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14947. (setq kill-year t)
  14948. (string-to-number (format-time-string "%Y")))
  14949. day (string-to-number (match-string 1 ans))
  14950. month (string-to-number (match-string 2 ans))
  14951. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14952. t nil ans)))
  14953. ;; Help matching american dates, like 5/30 or 5/30/7
  14954. (when (string-match
  14955. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14956. (setq year (if (match-end 4)
  14957. (string-to-number (match-string 4 ans))
  14958. (progn (setq kill-year t)
  14959. (string-to-number (format-time-string "%Y"))))
  14960. month (string-to-number (match-string 1 ans))
  14961. day (string-to-number (match-string 2 ans)))
  14962. (if (< year 100) (setq year (+ 2000 year)))
  14963. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14964. t nil ans)))
  14965. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14966. ;; If there is a time with am/pm, and *no* time without it, we convert
  14967. ;; so that matching will be successful.
  14968. (loop for i from 1 to 2 do ; twice, for end time as well
  14969. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14970. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14971. (setq hour (string-to-number (match-string 1 ans))
  14972. minute (if (match-end 3)
  14973. (string-to-number (match-string 3 ans))
  14974. 0)
  14975. pm (equal ?p
  14976. (string-to-char (downcase (match-string 4 ans)))))
  14977. (if (and (= hour 12) (not pm))
  14978. (setq hour 0)
  14979. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14980. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14981. t t ans))))
  14982. ;; Check if a time range is given as a duration
  14983. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14984. (setq hour (string-to-number (match-string 1 ans))
  14985. h2 (+ hour (string-to-number (match-string 3 ans)))
  14986. minute (string-to-number (match-string 2 ans))
  14987. m2 (+ minute (if (match-end 5) (string-to-number
  14988. (match-string 5 ans))0)))
  14989. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14990. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14991. t t ans)))
  14992. ;; Check if there is a time range
  14993. (when (boundp 'org-end-time-was-given)
  14994. (setq org-time-was-given nil)
  14995. (when (and (string-match org-plain-time-of-day-regexp ans)
  14996. (match-end 8))
  14997. (setq org-end-time-was-given (match-string 8 ans))
  14998. (setq ans (concat (substring ans 0 (match-beginning 7))
  14999. (substring ans (match-end 7))))))
  15000. (setq tl (parse-time-string ans)
  15001. day (or (nth 3 tl) (nth 3 org-defdecode))
  15002. month (or (nth 4 tl)
  15003. (if (and org-read-date-prefer-future
  15004. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15005. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15006. (nth 4 org-defdecode)))
  15007. year (or (and (not kill-year) (nth 5 tl))
  15008. (if (and org-read-date-prefer-future
  15009. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15010. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15011. (nth 5 org-defdecode)))
  15012. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15013. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15014. second (or (nth 0 tl) 0)
  15015. wday (nth 6 tl))
  15016. (when (and (eq org-read-date-prefer-future 'time)
  15017. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15018. (equal day (nth 3 nowdecode))
  15019. (equal month (nth 4 nowdecode))
  15020. (equal year (nth 5 nowdecode))
  15021. (nth 2 tl)
  15022. (or (< (nth 2 tl) (nth 2 nowdecode))
  15023. (and (= (nth 2 tl) (nth 2 nowdecode))
  15024. (nth 1 tl)
  15025. (< (nth 1 tl) (nth 1 nowdecode)))))
  15026. (setq day (1+ day)
  15027. futurep t))
  15028. ;; Special date definitions below
  15029. (cond
  15030. (iso-week
  15031. ;; There was an iso week
  15032. (require 'cal-iso)
  15033. (setq futurep nil)
  15034. (setq year (or iso-year year)
  15035. day (or iso-weekday wday 1)
  15036. wday nil ; to make sure that the trigger below does not match
  15037. iso-date (calendar-gregorian-from-absolute
  15038. (calendar-absolute-from-iso
  15039. (list iso-week day year))))
  15040. ; FIXME: Should we also push ISO weeks into the future?
  15041. ; (when (and org-read-date-prefer-future
  15042. ; (not iso-year)
  15043. ; (< (calendar-absolute-from-gregorian iso-date)
  15044. ; (time-to-days (current-time))))
  15045. ; (setq year (1+ year)
  15046. ; iso-date (calendar-gregorian-from-absolute
  15047. ; (calendar-absolute-from-iso
  15048. ; (list iso-week day year)))))
  15049. (setq month (car iso-date)
  15050. year (nth 2 iso-date)
  15051. day (nth 1 iso-date)))
  15052. (deltan
  15053. (setq futurep nil)
  15054. (unless deltadef
  15055. (let ((now (decode-time (current-time))))
  15056. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15057. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15058. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15059. ((equal deltaw "m") (setq month (+ month deltan)))
  15060. ((equal deltaw "y") (setq year (+ year deltan)))))
  15061. ((and wday (not (nth 3 tl)))
  15062. ;; Weekday was given, but no day, so pick that day in the week
  15063. ;; on or after the derived date.
  15064. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15065. (unless (equal wday wday1)
  15066. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15067. (if (and (boundp 'org-time-was-given)
  15068. (nth 2 tl))
  15069. (setq org-time-was-given t))
  15070. (if (< year 100) (setq year (+ 2000 year)))
  15071. ;; Check of the date is representable
  15072. (if org-read-date-force-compatible-dates
  15073. (progn
  15074. (if (< year 1970)
  15075. (setq year 1970 org-read-date-analyze-forced-year t))
  15076. (if (> year 2037)
  15077. (setq year 2037 org-read-date-analyze-forced-year t)))
  15078. (condition-case nil
  15079. (ignore (encode-time second minute hour day month year))
  15080. (error
  15081. (setq year (nth 5 org-defdecode))
  15082. (setq org-read-date-analyze-forced-year t))))
  15083. (setq org-read-date-analyze-futurep futurep)
  15084. (list second minute hour day month year)))
  15085. (defvar parse-time-weekdays)
  15086. (defun org-read-date-get-relative (s today default)
  15087. "Check string S for special relative date string.
  15088. TODAY and DEFAULT are internal times, for today and for a default.
  15089. Return shift list (N what def-flag)
  15090. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15091. N is the number of WHATs to shift.
  15092. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15093. the DEFAULT date rather than TODAY."
  15094. (require 'parse-time)
  15095. (when (and
  15096. (string-match
  15097. (concat
  15098. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15099. "\\([0-9]+\\)?"
  15100. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15101. "\\([ \t]\\|$\\)") s)
  15102. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15103. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15104. (string-to-char (substring (match-string 1 s) -1))
  15105. ?+))
  15106. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15107. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15108. (what (if (match-end 3) (match-string 3 s) "d"))
  15109. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15110. (date (if rel default today))
  15111. (wday (nth 6 (decode-time date)))
  15112. delta)
  15113. (if wday1
  15114. (progn
  15115. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15116. (if (= dir ?-) (setq delta (- delta 7)))
  15117. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15118. (list delta "d" rel))
  15119. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15120. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15121. "Turn a user-specified date into the internal representation.
  15122. The internal representation needed by the calendar is (month day year).
  15123. This is a wrapper to handle the brain-dead convention in calendar that
  15124. user function argument order change dependent on argument order."
  15125. (if (boundp 'calendar-date-style)
  15126. (cond
  15127. ((eq calendar-date-style 'american)
  15128. (list arg1 arg2 arg3))
  15129. ((eq calendar-date-style 'european)
  15130. (list arg2 arg1 arg3))
  15131. ((eq calendar-date-style 'iso)
  15132. (list arg2 arg3 arg1)))
  15133. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15134. (if (org-bound-and-true-p european-calendar-style)
  15135. (list arg2 arg1 arg3)
  15136. (list arg1 arg2 arg3)))))
  15137. (defun org-eval-in-calendar (form &optional keepdate)
  15138. "Eval FORM in the calendar window and return to current window.
  15139. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15140. otherwise stick to the current value of `org-ans2'."
  15141. (let ((sf (selected-frame))
  15142. (sw (selected-window)))
  15143. (select-window (get-buffer-window "*Calendar*" t))
  15144. (eval form)
  15145. (when (and (not keepdate) (calendar-cursor-to-date))
  15146. (let* ((date (calendar-cursor-to-date))
  15147. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15148. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15149. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15150. (select-window sw)
  15151. (org-select-frame-set-input-focus sf)))
  15152. (defun org-calendar-select ()
  15153. "Return to `org-read-date' with the date currently selected.
  15154. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15155. (interactive)
  15156. (when (calendar-cursor-to-date)
  15157. (let* ((date (calendar-cursor-to-date))
  15158. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15159. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15160. (if (active-minibuffer-window) (exit-minibuffer))))
  15161. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15162. "Insert a date stamp for the date given by the internal TIME.
  15163. WITH-HM means use the stamp format that includes the time of the day.
  15164. INACTIVE means use square brackets instead of angular ones, so that the
  15165. stamp will not contribute to the agenda.
  15166. PRE and POST are optional strings to be inserted before and after the
  15167. stamp.
  15168. The command returns the inserted time stamp."
  15169. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15170. stamp)
  15171. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15172. (insert-before-markers (or pre ""))
  15173. (when (listp extra)
  15174. (setq extra (car extra))
  15175. (if (and (stringp extra)
  15176. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15177. (setq extra (format "-%02d:%02d"
  15178. (string-to-number (match-string 1 extra))
  15179. (string-to-number (match-string 2 extra))))
  15180. (setq extra nil)))
  15181. (when extra
  15182. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15183. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15184. (insert-before-markers (or post ""))
  15185. (setq org-last-inserted-timestamp stamp)))
  15186. (defun org-toggle-time-stamp-overlays ()
  15187. "Toggle the use of custom time stamp formats."
  15188. (interactive)
  15189. (setq org-display-custom-times (not org-display-custom-times))
  15190. (unless org-display-custom-times
  15191. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15192. (while (setq p (next-single-property-change p 'display))
  15193. (if (and (get-text-property p 'display)
  15194. (eq (get-text-property p 'face) 'org-date))
  15195. (remove-text-properties
  15196. p (setq p (next-single-property-change p 'display))
  15197. '(display t))))
  15198. (set-buffer-modified-p bmp)))
  15199. (if (featurep 'xemacs)
  15200. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15201. (org-restart-font-lock)
  15202. (setq org-table-may-need-update t)
  15203. (if org-display-custom-times
  15204. (message "Time stamps are overlaid with custom format")
  15205. (message "Time stamp overlays removed")))
  15206. (defun org-display-custom-time (beg end)
  15207. "Overlay modified time stamp format over timestamp between BEG and END."
  15208. (let* ((ts (buffer-substring beg end))
  15209. t1 w1 with-hm tf time str w2 (off 0))
  15210. (save-match-data
  15211. (setq t1 (org-parse-time-string ts t))
  15212. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15213. (setq off (- (match-end 0) (match-beginning 0)))))
  15214. (setq end (- end off))
  15215. (setq w1 (- end beg)
  15216. with-hm (and (nth 1 t1) (nth 2 t1))
  15217. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15218. time (org-fix-decoded-time t1)
  15219. str (org-add-props
  15220. (format-time-string
  15221. (substring tf 1 -1) (apply 'encode-time time))
  15222. nil 'mouse-face 'highlight)
  15223. w2 (length str))
  15224. (if (not (= w2 w1))
  15225. (add-text-properties (1+ beg) (+ 2 beg)
  15226. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15227. (if (featurep 'xemacs)
  15228. (progn
  15229. (put-text-property beg end 'invisible t)
  15230. (put-text-property beg end 'end-glyph (make-glyph str)))
  15231. (put-text-property beg end 'display str))))
  15232. (defun org-translate-time (string)
  15233. "Translate all timestamps in STRING to custom format.
  15234. But do this only if the variable `org-display-custom-times' is set."
  15235. (when org-display-custom-times
  15236. (save-match-data
  15237. (let* ((start 0)
  15238. (re org-ts-regexp-both)
  15239. t1 with-hm inactive tf time str beg end)
  15240. (while (setq start (string-match re string start))
  15241. (setq beg (match-beginning 0)
  15242. end (match-end 0)
  15243. t1 (save-match-data
  15244. (org-parse-time-string (substring string beg end) t))
  15245. with-hm (and (nth 1 t1) (nth 2 t1))
  15246. inactive (equal (substring string beg (1+ beg)) "[")
  15247. tf (funcall (if with-hm 'cdr 'car)
  15248. org-time-stamp-custom-formats)
  15249. time (org-fix-decoded-time t1)
  15250. str (format-time-string
  15251. (concat
  15252. (if inactive "[" "<") (substring tf 1 -1)
  15253. (if inactive "]" ">"))
  15254. (apply 'encode-time time))
  15255. string (replace-match str t t string)
  15256. start (+ start (length str)))))))
  15257. string)
  15258. (defun org-fix-decoded-time (time)
  15259. "Set 0 instead of nil for the first 6 elements of time.
  15260. Don't touch the rest."
  15261. (let ((n 0))
  15262. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15263. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15264. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15265. "Difference between TIMESTAMP-STRING and now in days.
  15266. If SECONDS is non-nil, return the difference in seconds."
  15267. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15268. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15269. (funcall fdiff (current-time)))))
  15270. (defun org-deadline-close (timestamp-string &optional ndays)
  15271. "Is the time in TIMESTAMP-STRING close to the current date?"
  15272. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15273. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15274. (not (org-entry-is-done-p))))
  15275. (defun org-get-wdays (ts &optional delay zero-delay)
  15276. "Get the deadline lead time appropriate for timestring TS.
  15277. When DELAY is non-nil, get the delay time for scheduled items
  15278. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15279. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15280. don't try to find the delay cookie in the scheduled timestamp."
  15281. (let ((tv (if delay org-scheduled-delay-days
  15282. org-deadline-warning-days)))
  15283. (cond
  15284. ((or (and delay (< tv 0))
  15285. (and delay zero-delay (<= tv 0))
  15286. (and (not delay) (<= tv 0)))
  15287. ;; Enforce this value no matter what
  15288. (- tv))
  15289. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15290. ;; lead time is specified.
  15291. (floor (* (string-to-number (match-string 1 ts))
  15292. (cdr (assoc (match-string 2 ts)
  15293. '(("d" . 1) ("w" . 7)
  15294. ("m" . 30.4) ("y" . 365.25)
  15295. ("h" . 0.041667)))))))
  15296. ;; go for the default.
  15297. (t tv))))
  15298. (defun org-calendar-select-mouse (ev)
  15299. "Return to `org-read-date' with the date currently selected.
  15300. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15301. (interactive "e")
  15302. (mouse-set-point ev)
  15303. (when (calendar-cursor-to-date)
  15304. (let* ((date (calendar-cursor-to-date))
  15305. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15306. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15307. (if (active-minibuffer-window) (exit-minibuffer))))
  15308. (defun org-check-deadlines (ndays)
  15309. "Check if there are any deadlines due or past due.
  15310. A deadline is considered due if it happens within `org-deadline-warning-days'
  15311. days from today's date. If the deadline appears in an entry marked DONE,
  15312. it is not shown. The prefix arg NDAYS can be used to test that many
  15313. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15314. (interactive "P")
  15315. (let* ((org-warn-days
  15316. (cond
  15317. ((equal ndays '(4)) 100000)
  15318. (ndays (prefix-numeric-value ndays))
  15319. (t (abs org-deadline-warning-days))))
  15320. (case-fold-search nil)
  15321. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15322. (callback
  15323. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15324. (message "%d deadlines past-due or due within %d days"
  15325. (org-occur regexp nil callback)
  15326. org-warn-days)))
  15327. (defsubst org-re-timestamp (type)
  15328. "Return a regexp for timestamp TYPE.
  15329. Allowed values for TYPE are:
  15330. all: all timestamps
  15331. active: only active timestamps (<...>)
  15332. inactive: only inactive timestamps ([...])
  15333. scheduled: only scheduled timestamps
  15334. deadline: only deadline timestamps
  15335. closed: only closed time-stamps
  15336. When TYPE is nil, fall back on returning a regexp that matches
  15337. both scheduled and deadline timestamps."
  15338. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15339. ((eq type 'active) org-ts-regexp)
  15340. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15341. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15342. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15343. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15344. ((eq type 'scheduled-or-deadline)
  15345. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15346. (defun org-check-before-date (date)
  15347. "Check if there are deadlines or scheduled entries before DATE."
  15348. (interactive (list (org-read-date)))
  15349. (let ((case-fold-search nil)
  15350. (regexp (org-re-timestamp org-ts-type))
  15351. (callback
  15352. (lambda () (time-less-p
  15353. (org-time-string-to-time (match-string 1))
  15354. (org-time-string-to-time date)))))
  15355. (message "%d entries before %s"
  15356. (org-occur regexp nil callback) date)))
  15357. (defun org-check-after-date (date)
  15358. "Check if there are deadlines or scheduled entries after DATE."
  15359. (interactive (list (org-read-date)))
  15360. (let ((case-fold-search nil)
  15361. (regexp (org-re-timestamp org-ts-type))
  15362. (callback
  15363. (lambda () (not
  15364. (time-less-p
  15365. (org-time-string-to-time (match-string 1))
  15366. (org-time-string-to-time date))))))
  15367. (message "%d entries after %s"
  15368. (org-occur regexp nil callback) date)))
  15369. (defun org-check-dates-range (start-date end-date)
  15370. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15371. (interactive (list (org-read-date nil nil nil "Range starts")
  15372. (org-read-date nil nil nil "Range end")))
  15373. (let ((case-fold-search nil)
  15374. (regexp (org-re-timestamp org-ts-type))
  15375. (callback
  15376. (lambda ()
  15377. (let ((match (match-string 1)))
  15378. (and
  15379. (not (time-less-p
  15380. (org-time-string-to-time match)
  15381. (org-time-string-to-time start-date)))
  15382. (time-less-p
  15383. (org-time-string-to-time match)
  15384. (org-time-string-to-time end-date)))))))
  15385. (message "%d entries between %s and %s"
  15386. (org-occur regexp nil callback) start-date end-date)))
  15387. (defun org-evaluate-time-range (&optional to-buffer)
  15388. "Evaluate a time range by computing the difference between start and end.
  15389. Normally the result is just printed in the echo area, but with prefix arg
  15390. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15391. If the time range is actually in a table, the result is inserted into the
  15392. next column.
  15393. For time difference computation, a year is assumed to be exactly 365
  15394. days in order to avoid rounding problems."
  15395. (interactive "P")
  15396. (or
  15397. (org-clock-update-time-maybe)
  15398. (save-excursion
  15399. (unless (org-at-date-range-p t)
  15400. (goto-char (point-at-bol))
  15401. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15402. (if (not (org-at-date-range-p t))
  15403. (error "Not at a time-stamp range, and none found in current line")))
  15404. (let* ((ts1 (match-string 1))
  15405. (ts2 (match-string 2))
  15406. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15407. (match-end (match-end 0))
  15408. (time1 (org-time-string-to-time ts1))
  15409. (time2 (org-time-string-to-time ts2))
  15410. (t1 (org-float-time time1))
  15411. (t2 (org-float-time time2))
  15412. (diff (abs (- t2 t1)))
  15413. (negative (< (- t2 t1) 0))
  15414. ;; (ys (floor (* 365 24 60 60)))
  15415. (ds (* 24 60 60))
  15416. (hs (* 60 60))
  15417. (fy "%dy %dd %02d:%02d")
  15418. (fy1 "%dy %dd")
  15419. (fd "%dd %02d:%02d")
  15420. (fd1 "%dd")
  15421. (fh "%02d:%02d")
  15422. y d h m align)
  15423. (if havetime
  15424. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15425. y 0
  15426. d (floor (/ diff ds)) diff (mod diff ds)
  15427. h (floor (/ diff hs)) diff (mod diff hs)
  15428. m (floor (/ diff 60)))
  15429. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15430. y 0
  15431. d (floor (+ (/ diff ds) 0.5))
  15432. h 0 m 0))
  15433. (if (not to-buffer)
  15434. (message "%s" (org-make-tdiff-string y d h m))
  15435. (if (org-at-table-p)
  15436. (progn
  15437. (goto-char match-end)
  15438. (setq align t)
  15439. (and (looking-at " *|") (goto-char (match-end 0))))
  15440. (goto-char match-end))
  15441. (if (looking-at
  15442. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15443. (replace-match ""))
  15444. (if negative (insert " -"))
  15445. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15446. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15447. (insert " " (format fh h m))))
  15448. (if align (org-table-align))
  15449. (message "Time difference inserted")))))
  15450. (defun org-make-tdiff-string (y d h m)
  15451. (let ((fmt "")
  15452. (l nil))
  15453. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15454. l (push y l)))
  15455. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15456. l (push d l)))
  15457. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15458. l (push h l)))
  15459. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15460. l (push m l)))
  15461. (apply 'format fmt (nreverse l))))
  15462. (defun org-time-string-to-time (s &optional buffer pos)
  15463. "Convert a timestamp string into internal time."
  15464. (condition-case errdata
  15465. (apply 'encode-time (org-parse-time-string s))
  15466. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15467. s (if (not (and buffer pos))
  15468. ""
  15469. (format " at %d in buffer `%s'" pos buffer))
  15470. (cdr errdata)))))
  15471. (defun org-time-string-to-seconds (s)
  15472. "Convert a timestamp string to a number of seconds."
  15473. (org-float-time (org-time-string-to-time s)))
  15474. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15475. "Convert a time stamp to an absolute day number.
  15476. If there is a specifier for a cyclic time stamp, get the closest
  15477. date to DAYNR.
  15478. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15479. The variable `date' is bound by the calendar when this is called."
  15480. (cond
  15481. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15482. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15483. daynr
  15484. (+ daynr 1000)))
  15485. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15486. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15487. (time-to-days (current-time))) (match-string 0 s)
  15488. prefer show-all))
  15489. (t (time-to-days
  15490. (condition-case errdata
  15491. (apply 'encode-time (org-parse-time-string s))
  15492. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15493. s (if (not (and buffer pos))
  15494. ""
  15495. (format " at %d in buffer `%s'" pos buffer))
  15496. (cdr errdata))))))))
  15497. (defun org-days-to-iso-week (days)
  15498. "Return the iso week number."
  15499. (require 'cal-iso)
  15500. (car (calendar-iso-from-absolute days)))
  15501. (defun org-small-year-to-year (year)
  15502. "Convert 2-digit years into 4-digit years.
  15503. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15504. The year 2000 cannot be abbreviated. Any year larger than 99
  15505. is returned unchanged."
  15506. (if (< year 38)
  15507. (setq year (+ 2000 year))
  15508. (if (< year 100)
  15509. (setq year (+ 1900 year))))
  15510. year)
  15511. (defun org-time-from-absolute (d)
  15512. "Return the time corresponding to date D.
  15513. D may be an absolute day number, or a calendar-type list (month day year)."
  15514. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15515. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15516. (defun org-calendar-holiday ()
  15517. "List of holidays, for Diary display in Org-mode."
  15518. (require 'holidays)
  15519. (let ((hl (funcall
  15520. (if (fboundp 'calendar-check-holidays)
  15521. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15522. (if hl (mapconcat 'identity hl "; "))))
  15523. (defun org-diary-sexp-entry (sexp entry date)
  15524. "Process a SEXP diary ENTRY for DATE."
  15525. (require 'diary-lib)
  15526. (let ((result (if calendar-debug-sexp
  15527. (let ((stack-trace-on-error t))
  15528. (eval (car (read-from-string sexp))))
  15529. (condition-case nil
  15530. (eval (car (read-from-string sexp)))
  15531. (error
  15532. (beep)
  15533. (message "Bad sexp at line %d in %s: %s"
  15534. (org-current-line)
  15535. (buffer-file-name) sexp)
  15536. (sleep-for 2))))))
  15537. (cond ((stringp result) (split-string result "; "))
  15538. ((and (consp result)
  15539. (not (consp (cdr result)))
  15540. (stringp (cdr result))) (cdr result))
  15541. ((and (consp result)
  15542. (stringp (car result))) result)
  15543. (result entry))))
  15544. (defun org-diary-to-ical-string (frombuf)
  15545. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15546. This uses the icalendar.el library."
  15547. (let* ((tmpdir (if (featurep 'xemacs)
  15548. (temp-directory)
  15549. temporary-file-directory))
  15550. (tmpfile (make-temp-name
  15551. (expand-file-name "orgics" tmpdir)))
  15552. buf rtn b e)
  15553. (with-current-buffer frombuf
  15554. (icalendar-export-region (point-min) (point-max) tmpfile)
  15555. (setq buf (find-buffer-visiting tmpfile))
  15556. (set-buffer buf)
  15557. (goto-char (point-min))
  15558. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15559. (setq b (match-beginning 0)))
  15560. (goto-char (point-max))
  15561. (if (re-search-backward "^END:VEVENT" nil t)
  15562. (setq e (match-end 0)))
  15563. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15564. (kill-buffer buf)
  15565. (delete-file tmpfile)
  15566. rtn))
  15567. (defun org-closest-date (start current change prefer show-all)
  15568. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15569. When PREFER is `past', return a date that is either CURRENT or past.
  15570. When PREFER is `future', return a date that is either CURRENT or future.
  15571. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15572. ;; Make the proper lists from the dates
  15573. (catch 'exit
  15574. (let ((a1 '(("h" . hour)
  15575. ("d" . day)
  15576. ("w" . week)
  15577. ("m" . month)
  15578. ("y" . year)))
  15579. (shour (nth 2 (org-parse-time-string start)))
  15580. dn dw sday cday n1 n2 n0
  15581. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15582. (setq start (org-date-to-gregorian start)
  15583. current (org-date-to-gregorian
  15584. (if show-all
  15585. current
  15586. (time-to-days (current-time))))
  15587. sday (calendar-absolute-from-gregorian start)
  15588. cday (calendar-absolute-from-gregorian current))
  15589. (if (<= cday sday) (throw 'exit sday))
  15590. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15591. (setq dn (string-to-number (match-string 1 change))
  15592. dw (cdr (assoc (match-string 2 change) a1)))
  15593. (error "Invalid change specifier: %s" change))
  15594. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15595. (cond
  15596. ((eq dw 'hour)
  15597. (let ((missing-hours
  15598. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15599. dn)))
  15600. (setq n1 (if (zerop missing-hours) cday
  15601. (- cday (1+ (floor (/ missing-hours 24)))))
  15602. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15603. ((eq dw 'day)
  15604. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15605. n2 (+ n1 dn)))
  15606. ((eq dw 'year)
  15607. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15608. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15609. (setq date1 (list m d y1)
  15610. n1 (calendar-absolute-from-gregorian date1)
  15611. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15612. n2 (calendar-absolute-from-gregorian date2)))
  15613. ((eq dw 'month)
  15614. ;; approx number of month between the two dates
  15615. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15616. ;; How often does dn fit in there?
  15617. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15618. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15619. m (+ m nm)
  15620. ny (floor (/ m 12))
  15621. y (+ y ny)
  15622. m (- m (* ny 12)))
  15623. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15624. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15625. (setq m2 (+ m dn) y2 y)
  15626. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15627. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15628. (while (<= n2 cday)
  15629. (setq n1 n2 m m2 y y2)
  15630. (setq m2 (+ m dn) y2 y)
  15631. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15632. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15633. ;; Make sure n1 is the earlier date
  15634. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15635. (if show-all
  15636. (cond
  15637. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15638. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15639. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15640. (cond
  15641. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15642. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15643. (t (if (= cday n1) n1 n2)))))))
  15644. (defun org-date-to-gregorian (date)
  15645. "Turn any specification of DATE into a Gregorian date for the calendar."
  15646. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15647. ((and (listp date) (= (length date) 3)) date)
  15648. ((stringp date)
  15649. (setq date (org-parse-time-string date))
  15650. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15651. ((listp date)
  15652. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15653. (defun org-parse-time-string (s &optional nodefault)
  15654. "Parse the standard Org-mode time string.
  15655. This should be a lot faster than the normal `parse-time-string'.
  15656. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15657. hour and minute fields will be nil if not given."
  15658. (cond ((string-match org-ts-regexp0 s)
  15659. (list 0
  15660. (if (or (match-beginning 8) (not nodefault))
  15661. (string-to-number (or (match-string 8 s) "0")))
  15662. (if (or (match-beginning 7) (not nodefault))
  15663. (string-to-number (or (match-string 7 s) "0")))
  15664. (string-to-number (match-string 4 s))
  15665. (string-to-number (match-string 3 s))
  15666. (string-to-number (match-string 2 s))
  15667. nil nil nil))
  15668. ((string-match "^<[^>]+>$" s)
  15669. (decode-time (seconds-to-time (org-matcher-time s))))
  15670. (t (error "Not a standard Org-mode time string: %s" s))))
  15671. (defun org-timestamp-up (&optional arg)
  15672. "Increase the date item at the cursor by one.
  15673. If the cursor is on the year, change the year. If it is on the month,
  15674. the day or the time, change that.
  15675. With prefix ARG, change by that many units."
  15676. (interactive "p")
  15677. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15678. (defun org-timestamp-down (&optional arg)
  15679. "Decrease the date item at the cursor by one.
  15680. If the cursor is on the year, change the year. If it is on the month,
  15681. the day or the time, change that.
  15682. With prefix ARG, change by that many units."
  15683. (interactive "p")
  15684. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15685. (defun org-timestamp-up-day (&optional arg)
  15686. "Increase the date in the time stamp by one day.
  15687. With prefix ARG, change that many days."
  15688. (interactive "p")
  15689. (if (and (not (org-at-timestamp-p t))
  15690. (org-at-heading-p))
  15691. (org-todo 'up)
  15692. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15693. (defun org-timestamp-down-day (&optional arg)
  15694. "Decrease the date in the time stamp by one day.
  15695. With prefix ARG, change that many days."
  15696. (interactive "p")
  15697. (if (and (not (org-at-timestamp-p t))
  15698. (org-at-heading-p))
  15699. (org-todo 'down)
  15700. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15701. (defun org-at-timestamp-p (&optional inactive-ok)
  15702. "Determine if the cursor is in or at a timestamp."
  15703. (interactive)
  15704. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15705. (pos (point))
  15706. (ans (or (looking-at tsr)
  15707. (save-excursion
  15708. (skip-chars-backward "^[<\n\r\t")
  15709. (if (> (point) (point-min)) (backward-char 1))
  15710. (and (looking-at tsr)
  15711. (> (- (match-end 0) pos) -1))))))
  15712. (and ans
  15713. (boundp 'org-ts-what)
  15714. (setq org-ts-what
  15715. (cond
  15716. ((= pos (match-beginning 0)) 'bracket)
  15717. ;; Point is considered to be "on the bracket" whether
  15718. ;; it's really on it or right after it.
  15719. ((= pos (1- (match-end 0))) 'bracket)
  15720. ((= pos (match-end 0)) 'after)
  15721. ((org-pos-in-match-range pos 2) 'year)
  15722. ((org-pos-in-match-range pos 3) 'month)
  15723. ((org-pos-in-match-range pos 7) 'hour)
  15724. ((org-pos-in-match-range pos 8) 'minute)
  15725. ((or (org-pos-in-match-range pos 4)
  15726. (org-pos-in-match-range pos 5)) 'day)
  15727. ((and (> pos (or (match-end 8) (match-end 5)))
  15728. (< pos (match-end 0)))
  15729. (- pos (or (match-end 8) (match-end 5))))
  15730. (t 'day))))
  15731. ans))
  15732. (defun org-toggle-timestamp-type ()
  15733. "Toggle the type (<active> or [inactive]) of a time stamp."
  15734. (interactive)
  15735. (when (org-at-timestamp-p t)
  15736. (let ((beg (match-beginning 0)) (end (match-end 0))
  15737. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15738. (save-excursion
  15739. (goto-char beg)
  15740. (while (re-search-forward "[][<>]" end t)
  15741. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15742. t t)))
  15743. (message "Timestamp is now %sactive"
  15744. (if (equal (char-after beg) ?<) "" "in")))))
  15745. (defun org-at-clock-log-p nil
  15746. "Is the cursor on the clock log line?"
  15747. (save-excursion
  15748. (move-beginning-of-line 1)
  15749. (looking-at "^[ \t]*CLOCK:")))
  15750. (defvar org-clock-history) ; defined in org-clock.el
  15751. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15752. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15753. "Change the date in the time stamp at point.
  15754. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15755. `year', `minute', `second'. If WHAT is not given, the cursor position
  15756. in the timestamp determines what will be changed.
  15757. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15758. (let ((origin (point)) origin-cat
  15759. with-hm inactive
  15760. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15761. org-ts-what
  15762. extra rem
  15763. ts time time0 fixnext clrgx)
  15764. (if (not (org-at-timestamp-p t))
  15765. (error "Not at a timestamp"))
  15766. (if (and (not what) (eq org-ts-what 'bracket))
  15767. (org-toggle-timestamp-type)
  15768. ;; Point isn't on brackets. Remember the part of the time-stamp
  15769. ;; the point was in. Indeed, size of time-stamps may change,
  15770. ;; but point must be kept in the same category nonetheless.
  15771. (setq origin-cat org-ts-what)
  15772. (if (and (not what) (not (eq org-ts-what 'day))
  15773. org-display-custom-times
  15774. (get-text-property (point) 'display)
  15775. (not (get-text-property (1- (point)) 'display)))
  15776. (setq org-ts-what 'day))
  15777. (setq org-ts-what (or what org-ts-what)
  15778. inactive (= (char-after (match-beginning 0)) ?\[)
  15779. ts (match-string 0))
  15780. (replace-match "")
  15781. (when (string-match
  15782. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15783. ts)
  15784. (setq extra (match-string 1 ts))
  15785. (if suppress-tmp-delay
  15786. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15787. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15788. (setq with-hm t))
  15789. (setq time0 (org-parse-time-string ts))
  15790. (when (and updown
  15791. (eq org-ts-what 'minute)
  15792. (not current-prefix-arg))
  15793. ;; This looks like s-up and s-down. Change by one rounding step.
  15794. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15795. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15796. (setcar (cdr time0) (+ (nth 1 time0)
  15797. (if (> n 0) (- rem) (- dm rem))))))
  15798. (setq time
  15799. (encode-time (or (car time0) 0)
  15800. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15801. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15802. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15803. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15804. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15805. (nthcdr 6 time0)))
  15806. (when (and (member org-ts-what '(hour minute))
  15807. extra
  15808. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15809. (setq extra (org-modify-ts-extra
  15810. extra
  15811. (if (eq org-ts-what 'hour) 2 5)
  15812. n dm)))
  15813. (when (integerp org-ts-what)
  15814. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15815. (if (eq what 'calendar)
  15816. (let ((cal-date (org-get-date-from-calendar)))
  15817. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15818. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15819. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15820. (setcar time0 (or (car time0) 0))
  15821. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15822. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15823. (setq time (apply 'encode-time time0))))
  15824. ;; Insert the new time-stamp, and ensure point stays in the same
  15825. ;; category as before (i.e. not after the last position in that
  15826. ;; category).
  15827. (let ((pos (point)))
  15828. ;; Stay before inserted string. `save-excursion' is of no use.
  15829. (setq org-last-changed-timestamp
  15830. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15831. (goto-char pos))
  15832. (save-match-data
  15833. (looking-at org-ts-regexp3)
  15834. (goto-char (cond
  15835. ;; `day' category ends before `hour' if any, or at
  15836. ;; the end of the day name.
  15837. ((eq origin-cat 'day)
  15838. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15839. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15840. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15841. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15842. ;; `year' and `month' have both fixed size: point
  15843. ;; couldn't have moved into another part.
  15844. (t origin))))
  15845. ;; Update clock if on a CLOCK line.
  15846. (org-clock-update-time-maybe)
  15847. ;; Maybe adjust the closest clock in `org-clock-history'
  15848. (when org-clock-adjust-closest
  15849. (if (not (and (org-at-clock-log-p)
  15850. (< 1 (length (delq nil (mapcar 'marker-position
  15851. org-clock-history))))))
  15852. (message "No clock to adjust")
  15853. (cond ((save-excursion ; fix previous clock?
  15854. (re-search-backward org-ts-regexp0 nil t)
  15855. (org-looking-back (concat org-clock-string " \\[")))
  15856. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15857. ((save-excursion ; fix next clock?
  15858. (re-search-backward org-ts-regexp0 nil t)
  15859. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15860. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15861. (save-window-excursion
  15862. ;; Find closest clock to point, adjust the previous/next one in history
  15863. (let* ((p (save-excursion (org-back-to-heading t)))
  15864. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15865. (clfixnth
  15866. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15867. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15868. (if (not clfixpos)
  15869. (message "No clock to adjust")
  15870. (save-excursion
  15871. (org-goto-marker-or-bmk clfixpos)
  15872. (org-show-subtree)
  15873. (when (re-search-forward clrgx nil t)
  15874. (goto-char (match-beginning 1))
  15875. (let (org-clock-adjust-closest)
  15876. (org-timestamp-change n org-ts-what updown))
  15877. (message "Clock adjusted in %s for heading: %s"
  15878. (file-name-nondirectory (buffer-file-name))
  15879. (org-get-heading t t)))))))))
  15880. ;; Try to recenter the calendar window, if any.
  15881. (if (and org-calendar-follow-timestamp-change
  15882. (get-buffer-window "*Calendar*" t)
  15883. (memq org-ts-what '(day month year)))
  15884. (org-recenter-calendar (time-to-days time))))))
  15885. (defun org-modify-ts-extra (s pos n dm)
  15886. "Change the different parts of the lead-time and repeat fields in timestamp."
  15887. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15888. ng h m new rem)
  15889. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15890. (cond
  15891. ((or (org-pos-in-match-range pos 2)
  15892. (org-pos-in-match-range pos 3))
  15893. (setq m (string-to-number (match-string 3 s))
  15894. h (string-to-number (match-string 2 s)))
  15895. (if (org-pos-in-match-range pos 2)
  15896. (setq h (+ h n))
  15897. (setq n (* dm (org-no-warnings (signum n))))
  15898. (when (not (= 0 (setq rem (% m dm))))
  15899. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15900. (setq m (+ m n)))
  15901. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15902. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15903. (setq h (min 24 (max 0 h)))
  15904. (setq ng 1 new (format "-%02d:%02d" h m)))
  15905. ((org-pos-in-match-range pos 6)
  15906. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15907. ((org-pos-in-match-range pos 5)
  15908. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15909. ((org-pos-in-match-range pos 9)
  15910. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15911. ((org-pos-in-match-range pos 8)
  15912. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15913. (when ng
  15914. (setq s (concat
  15915. (substring s 0 (match-beginning ng))
  15916. new
  15917. (substring s (match-end ng))))))
  15918. s))
  15919. (defun org-recenter-calendar (date)
  15920. "If the calendar is visible, recenter it to DATE."
  15921. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15922. (when cwin
  15923. (let ((calendar-move-hook nil))
  15924. (with-selected-window cwin
  15925. (calendar-goto-date (if (listp date) date
  15926. (calendar-gregorian-from-absolute date))))))))
  15927. (defun org-goto-calendar (&optional arg)
  15928. "Go to the Emacs calendar at the current date.
  15929. If there is a time stamp in the current line, go to that date.
  15930. A prefix ARG can be used to force the current date."
  15931. (interactive "P")
  15932. (let ((tsr org-ts-regexp) diff
  15933. (calendar-move-hook nil)
  15934. (calendar-view-holidays-initially-flag nil)
  15935. (calendar-view-diary-initially-flag nil))
  15936. (if (or (org-at-timestamp-p)
  15937. (save-excursion
  15938. (beginning-of-line 1)
  15939. (looking-at (concat ".*" tsr))))
  15940. (let ((d1 (time-to-days (current-time)))
  15941. (d2 (time-to-days
  15942. (org-time-string-to-time (match-string 1)))))
  15943. (setq diff (- d2 d1))))
  15944. (calendar)
  15945. (calendar-goto-today)
  15946. (if (and diff (not arg)) (calendar-forward-day diff))))
  15947. (defun org-get-date-from-calendar ()
  15948. "Return a list (month day year) of date at point in calendar."
  15949. (with-current-buffer "*Calendar*"
  15950. (save-match-data
  15951. (calendar-cursor-to-date))))
  15952. (defun org-date-from-calendar ()
  15953. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15954. If there is already a time stamp at the cursor position, update it."
  15955. (interactive)
  15956. (if (org-at-timestamp-p t)
  15957. (org-timestamp-change 0 'calendar)
  15958. (let ((cal-date (org-get-date-from-calendar)))
  15959. (org-insert-time-stamp
  15960. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15961. (defcustom org-effort-durations
  15962. `(("h" . 60)
  15963. ("d" . ,(* 60 8))
  15964. ("w" . ,(* 60 8 5))
  15965. ("m" . ,(* 60 8 5 4))
  15966. ("y" . ,(* 60 8 5 40)))
  15967. "Conversion factor to minutes for an effort modifier.
  15968. Each entry has the form (MODIFIER . MINUTES).
  15969. In an effort string, a number followed by MODIFIER is multiplied
  15970. by the specified number of MINUTES to obtain an effort in
  15971. minutes.
  15972. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15973. effort string \"2hours\" is equivalent to 120 minutes."
  15974. :group 'org-agenda
  15975. :version "24.1"
  15976. :type '(alist :key-type (string :tag "Modifier")
  15977. :value-type (number :tag "Minutes")))
  15978. (defun org-minutes-to-clocksum-string (m)
  15979. "Format number of minutes as a clocksum string.
  15980. The format is determined by `org-time-clocksum-format',
  15981. `org-time-clocksum-use-fractional' and
  15982. `org-time-clocksum-fractional-format' and
  15983. `org-time-clocksum-use-effort-durations'."
  15984. (let ((clocksum "") h d w mo y fmt n)
  15985. (setq h (if org-time-clocksum-use-effort-durations
  15986. (cdr (assoc "h" org-effort-durations)) 60)
  15987. d (if org-time-clocksum-use-effort-durations
  15988. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15989. w (if org-time-clocksum-use-effort-durations
  15990. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15991. mo (if org-time-clocksum-use-effort-durations
  15992. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15993. y (if org-time-clocksum-use-effort-durations
  15994. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15995. ;; fractional format
  15996. (if org-time-clocksum-use-fractional
  15997. (cond
  15998. ;; single format string
  15999. ((stringp org-time-clocksum-fractional-format)
  16000. (format org-time-clocksum-fractional-format (/ m (float h))))
  16001. ;; choice of fractional formats for different time units
  16002. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16003. (> (/ (truncate m) (* y d h)) 0))
  16004. (format fmt (/ m (* y d (float h)))))
  16005. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16006. (> (/ (truncate m) (* mo d h)) 0))
  16007. (format fmt (/ m (* mo d (float h)))))
  16008. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16009. (> (/ (truncate m) (* w d h)) 0))
  16010. (format fmt (/ m (* w d (float h)))))
  16011. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16012. (> (/ (truncate m) (* d h)) 0))
  16013. (format fmt (/ m (* d (float h)))))
  16014. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16015. (> (/ (truncate m) h) 0))
  16016. (format fmt (/ m (float h))))
  16017. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16018. (format fmt m))
  16019. ;; fall back to smallest time unit with a format
  16020. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16021. (format fmt (/ m (float h))))
  16022. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16023. (format fmt (/ m (* d (float h)))))
  16024. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16025. (format fmt (/ m (* w d (float h)))))
  16026. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16027. (format fmt (/ m (* mo d (float h)))))
  16028. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16029. (format fmt (/ m (* y d (float h))))))
  16030. ;; standard (non-fractional) format, with single format string
  16031. (if (stringp org-time-clocksum-format)
  16032. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16033. ;; separate formats components
  16034. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16035. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16036. (plist-get org-time-clocksum-format :require-years))
  16037. (setq clocksum (concat clocksum (format fmt n))
  16038. m (- m (* n y d h))))
  16039. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16040. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16041. (plist-get org-time-clocksum-format :require-months))
  16042. (setq clocksum (concat clocksum (format fmt n))
  16043. m (- m (* n mo d h))))
  16044. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16045. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16046. (plist-get org-time-clocksum-format :require-weeks))
  16047. (setq clocksum (concat clocksum (format fmt n))
  16048. m (- m (* n w d h))))
  16049. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16050. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16051. (plist-get org-time-clocksum-format :require-days))
  16052. (setq clocksum (concat clocksum (format fmt n))
  16053. m (- m (* n d h))))
  16054. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16055. (or (> (setq n (/ (truncate m) h)) 0)
  16056. (plist-get org-time-clocksum-format :require-hours))
  16057. (setq clocksum (concat clocksum (format fmt n))
  16058. m (- m (* n h))))
  16059. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16060. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16061. (setq clocksum (concat clocksum (format fmt m))))
  16062. ;; return formatted time duration
  16063. clocksum))))
  16064. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16065. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16066. "Org mode version 8.0")
  16067. (defun org-hours-to-clocksum-string (n)
  16068. (org-minutes-to-clocksum-string (* n 60)))
  16069. (defun org-hh:mm-string-to-minutes (s)
  16070. "Convert a string H:MM to a number of minutes.
  16071. If the string is just a number, interpret it as minutes.
  16072. In fact, the first hh:mm or number in the string will be taken,
  16073. there can be extra stuff in the string.
  16074. If no number is found, the return value is 0."
  16075. (cond
  16076. ((integerp s) s)
  16077. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16078. (+ (* (string-to-number (match-string 1 s)) 60)
  16079. (string-to-number (match-string 2 s))))
  16080. ((string-match "\\([0-9]+\\)" s)
  16081. (string-to-number (match-string 1 s)))
  16082. (t 0)))
  16083. (defcustom org-image-actual-width t
  16084. "Should we use the actual width of images when inlining them?
  16085. When set to `t', always use the image width.
  16086. When set to a number, use imagemagick (when available) to set
  16087. the image's width to this value.
  16088. When set to a number in a list, try to get the width from any
  16089. #+ATTR.* keyword if it matches a width specification like
  16090. #+ATTR_HTML: :width 300px
  16091. and fall back on that number if none is found.
  16092. When set to nil, try to get the width from an #+ATTR.* keyword
  16093. and fall back on the original width if none is found.
  16094. This requires Emacs >= 24.1, build with imagemagick support."
  16095. :group 'org-appearance
  16096. :version "24.4"
  16097. :package-version '(Org . "8.0")
  16098. :type '(choice
  16099. (const :tag "Use the image width" t)
  16100. (integer :tag "Use a number of pixels")
  16101. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16102. (const :tag "Use #+ATTR* or don't resize" nil)))
  16103. (defcustom org-agenda-inhibit-startup nil
  16104. "Inhibit startup when preparing agenda buffers.
  16105. When this variable is `t' (the default), the initialization of
  16106. the Org agenda buffers is inhibited: e.g. the visibility state
  16107. is not set, the tables are not re-aligned, etc."
  16108. :type 'boolean
  16109. :version "24.3"
  16110. :group 'org-agenda)
  16111. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16112. "Convert a duration string S to minutes.
  16113. A bare number is interpreted as minutes, modifiers can be set by
  16114. customizing `org-effort-durations' (which see).
  16115. Entries containing a colon are interpreted as H:MM by
  16116. `org-hh:mm-string-to-minutes'."
  16117. (let ((result 0)
  16118. (re (concat "\\([0-9.]+\\) *\\("
  16119. (regexp-opt (mapcar 'car org-effort-durations))
  16120. "\\)")))
  16121. (while (string-match re s)
  16122. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16123. (string-to-number (match-string 1 s))))
  16124. (setq s (replace-match "" nil t s)))
  16125. (setq result (floor result))
  16126. (incf result (org-hh:mm-string-to-minutes s))
  16127. (if output-to-string (number-to-string result) result)))
  16128. ;;;; Files
  16129. (defun org-save-all-org-buffers ()
  16130. "Save all Org-mode buffers without user confirmation."
  16131. (interactive)
  16132. (message "Saving all Org-mode buffers...")
  16133. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16134. (when (featurep 'org-id) (org-id-locations-save))
  16135. (message "Saving all Org-mode buffers... done"))
  16136. (defun org-revert-all-org-buffers ()
  16137. "Revert all Org-mode buffers.
  16138. Prompt for confirmation when there are unsaved changes.
  16139. Be sure you know what you are doing before letting this function
  16140. overwrite your changes.
  16141. This function is useful in a setup where one tracks org files
  16142. with a version control system, to revert on one machine after pulling
  16143. changes from another. I believe the procedure must be like this:
  16144. 1. M-x org-save-all-org-buffers
  16145. 2. Pull changes from the other machine, resolve conflicts
  16146. 3. M-x org-revert-all-org-buffers"
  16147. (interactive)
  16148. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16149. (error "Abort"))
  16150. (save-excursion
  16151. (save-window-excursion
  16152. (mapc
  16153. (lambda (b)
  16154. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16155. (with-current-buffer b buffer-file-name))
  16156. (org-pop-to-buffer-same-window b)
  16157. (revert-buffer t 'no-confirm)))
  16158. (buffer-list))
  16159. (when (and (featurep 'org-id) org-id-track-globally)
  16160. (org-id-locations-load)))))
  16161. ;;;; Agenda files
  16162. ;;;###autoload
  16163. (defun org-switchb (&optional arg)
  16164. "Switch between Org buffers.
  16165. With one prefix argument, restrict available buffers to files.
  16166. With two prefix arguments, restrict available buffers to agenda files.
  16167. Defaults to `iswitchb' for buffer name completion.
  16168. Set `org-completion-use-ido' to make it use ido instead."
  16169. (interactive "P")
  16170. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16171. ((equal arg '(16)) (org-buffer-list 'agenda))
  16172. (t (org-buffer-list))))
  16173. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16174. (org-completion-use-ido org-completion-use-ido))
  16175. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16176. (setq org-completion-use-iswitchb t))
  16177. (org-pop-to-buffer-same-window
  16178. (org-icompleting-read "Org buffer: "
  16179. (mapcar 'list (mapcar 'buffer-name blist))
  16180. nil t))))
  16181. ;;; Define some older names previously used for this functionality
  16182. ;;;###autoload
  16183. (defalias 'org-ido-switchb 'org-switchb)
  16184. ;;;###autoload
  16185. (defalias 'org-iswitchb 'org-switchb)
  16186. (defun org-buffer-list (&optional predicate exclude-tmp)
  16187. "Return a list of Org buffers.
  16188. PREDICATE can be `export', `files' or `agenda'.
  16189. export restrict the list to Export buffers.
  16190. files restrict the list to buffers visiting Org files.
  16191. agenda restrict the list to buffers visiting agenda files.
  16192. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16193. (let* ((bfn nil)
  16194. (agenda-files (and (eq predicate 'agenda)
  16195. (mapcar 'file-truename (org-agenda-files t))))
  16196. (filter
  16197. (cond
  16198. ((eq predicate 'files)
  16199. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16200. ((eq predicate 'export)
  16201. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16202. ((eq predicate 'agenda)
  16203. (lambda (b)
  16204. (with-current-buffer b
  16205. (and (derived-mode-p 'org-mode)
  16206. (setq bfn (buffer-file-name b))
  16207. (member (file-truename bfn) agenda-files)))))
  16208. (t (lambda (b) (with-current-buffer b
  16209. (or (derived-mode-p 'org-mode)
  16210. (string-match "\*Org .*Export"
  16211. (buffer-name b)))))))))
  16212. (delq nil
  16213. (mapcar
  16214. (lambda(b)
  16215. (if (and (funcall filter b)
  16216. (or (not exclude-tmp)
  16217. (not (string-match "tmp" (buffer-name b)))))
  16218. b
  16219. nil))
  16220. (buffer-list)))))
  16221. (defun org-agenda-files (&optional unrestricted archives)
  16222. "Get the list of agenda files.
  16223. Optional UNRESTRICTED means return the full list even if a restriction
  16224. is currently in place.
  16225. When ARCHIVES is t, include all archive files that are really being
  16226. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16227. `org-agenda-archives-mode' is t."
  16228. (let ((files
  16229. (cond
  16230. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16231. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16232. ((listp org-agenda-files) org-agenda-files)
  16233. (t (error "Invalid value of `org-agenda-files'")))))
  16234. (setq files (apply 'append
  16235. (mapcar (lambda (f)
  16236. (if (file-directory-p f)
  16237. (directory-files
  16238. f t org-agenda-file-regexp)
  16239. (list f)))
  16240. files)))
  16241. (when org-agenda-skip-unavailable-files
  16242. (setq files (delq nil
  16243. (mapcar (function
  16244. (lambda (file)
  16245. (and (file-readable-p file) file)))
  16246. files))))
  16247. (when (or (eq archives t)
  16248. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16249. (setq files (org-add-archive-files files)))
  16250. files))
  16251. (defun org-agenda-file-p (&optional file)
  16252. "Return non-nil, if FILE is an agenda file.
  16253. If FILE is omitted, use the file associated with the current
  16254. buffer."
  16255. (member (or file (buffer-file-name))
  16256. (org-agenda-files t)))
  16257. (defun org-edit-agenda-file-list ()
  16258. "Edit the list of agenda files.
  16259. Depending on setup, this either uses customize to edit the variable
  16260. `org-agenda-files', or it visits the file that is holding the list. In the
  16261. latter case, the buffer is set up in a way that saving it automatically kills
  16262. the buffer and restores the previous window configuration."
  16263. (interactive)
  16264. (if (stringp org-agenda-files)
  16265. (let ((cw (current-window-configuration)))
  16266. (find-file org-agenda-files)
  16267. (org-set-local 'org-window-configuration cw)
  16268. (org-add-hook 'after-save-hook
  16269. (lambda ()
  16270. (set-window-configuration
  16271. (prog1 org-window-configuration
  16272. (kill-buffer (current-buffer))))
  16273. (org-install-agenda-files-menu)
  16274. (message "New agenda file list installed"))
  16275. nil 'local)
  16276. (message "%s" (substitute-command-keys
  16277. "Edit list and finish with \\[save-buffer]")))
  16278. (customize-variable 'org-agenda-files)))
  16279. (defun org-store-new-agenda-file-list (list)
  16280. "Set new value for the agenda file list and save it correctly."
  16281. (if (stringp org-agenda-files)
  16282. (let ((fe (org-read-agenda-file-list t)) b u)
  16283. (while (setq b (find-buffer-visiting org-agenda-files))
  16284. (kill-buffer b))
  16285. (with-temp-file org-agenda-files
  16286. (insert
  16287. (mapconcat
  16288. (lambda (f) ;; Keep un-expanded entries.
  16289. (if (setq u (assoc f fe))
  16290. (cdr u)
  16291. f))
  16292. list "\n")
  16293. "\n")))
  16294. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16295. (org-insert-mode-line-in-empty-file nil))
  16296. (setq org-agenda-files list)
  16297. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16298. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16299. "Read the list of agenda files from a file.
  16300. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16301. filenames, used by `org-store-new-agenda-file-list' to write back
  16302. un-expanded file names."
  16303. (when (file-directory-p org-agenda-files)
  16304. (error "`org-agenda-files' cannot be a single directory"))
  16305. (when (stringp org-agenda-files)
  16306. (with-temp-buffer
  16307. (insert-file-contents org-agenda-files)
  16308. (mapcar
  16309. (lambda (f)
  16310. (let ((e (expand-file-name (substitute-in-file-name f)
  16311. org-directory)))
  16312. (if pair-with-expansion
  16313. (cons e f)
  16314. e)))
  16315. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16316. ;;;###autoload
  16317. (defun org-cycle-agenda-files ()
  16318. "Cycle through the files in `org-agenda-files'.
  16319. If the current buffer visits an agenda file, find the next one in the list.
  16320. If the current buffer does not, find the first agenda file."
  16321. (interactive)
  16322. (let* ((fs (org-agenda-files t))
  16323. (files (append fs (list (car fs))))
  16324. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16325. file)
  16326. (unless files (error "No agenda files"))
  16327. (catch 'exit
  16328. (while (setq file (pop files))
  16329. (if (equal (file-truename file) tcf)
  16330. (when (car files)
  16331. (find-file (car files))
  16332. (throw 'exit t))))
  16333. (find-file (car fs)))
  16334. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16335. (defun org-agenda-file-to-front (&optional to-end)
  16336. "Move/add the current file to the top of the agenda file list.
  16337. If the file is not present in the list, it is added to the front. If it is
  16338. present, it is moved there. With optional argument TO-END, add/move to the
  16339. end of the list."
  16340. (interactive "P")
  16341. (let ((org-agenda-skip-unavailable-files nil)
  16342. (file-alist (mapcar (lambda (x)
  16343. (cons (file-truename x) x))
  16344. (org-agenda-files t)))
  16345. (ctf (file-truename
  16346. (or buffer-file-name
  16347. (error "Please save the current buffer to a file"))))
  16348. x had)
  16349. (setq x (assoc ctf file-alist) had x)
  16350. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16351. (if to-end
  16352. (setq file-alist (append (delq x file-alist) (list x)))
  16353. (setq file-alist (cons x (delq x file-alist))))
  16354. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16355. (org-install-agenda-files-menu)
  16356. (message "File %s to %s of agenda file list"
  16357. (if had "moved" "added") (if to-end "end" "front"))))
  16358. (defun org-remove-file (&optional file)
  16359. "Remove current file from the list of files in variable `org-agenda-files'.
  16360. These are the files which are being checked for agenda entries.
  16361. Optional argument FILE means use this file instead of the current."
  16362. (interactive)
  16363. (let* ((org-agenda-skip-unavailable-files nil)
  16364. (file (or file buffer-file-name
  16365. (error "Current buffer does not visit a file")))
  16366. (true-file (file-truename file))
  16367. (afile (abbreviate-file-name file))
  16368. (files (delq nil (mapcar
  16369. (lambda (x)
  16370. (if (equal true-file
  16371. (file-truename x))
  16372. nil x))
  16373. (org-agenda-files t)))))
  16374. (if (not (= (length files) (length (org-agenda-files t))))
  16375. (progn
  16376. (org-store-new-agenda-file-list files)
  16377. (org-install-agenda-files-menu)
  16378. (message "Removed file: %s" afile))
  16379. (message "File was not in list: %s (not removed)" afile))))
  16380. (defun org-file-menu-entry (file)
  16381. (vector file (list 'find-file file) t))
  16382. (defun org-check-agenda-file (file)
  16383. "Make sure FILE exists. If not, ask user what to do."
  16384. (when (not (file-exists-p file))
  16385. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16386. (abbreviate-file-name file))
  16387. (let ((r (downcase (read-char-exclusive))))
  16388. (cond
  16389. ((equal r ?r)
  16390. (org-remove-file file)
  16391. (throw 'nextfile t))
  16392. (t (error "Abort"))))))
  16393. (defun org-get-agenda-file-buffer (file)
  16394. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16395. it to the list of buffers which might be released later."
  16396. (let ((buf (org-find-base-buffer-visiting file)))
  16397. (if buf
  16398. buf ; just return it
  16399. ;; Make a new buffer and remember it
  16400. (setq buf (find-file-noselect file))
  16401. (if buf (push buf org-agenda-new-buffers))
  16402. buf)))
  16403. (defun org-release-buffers (blist)
  16404. "Release all buffers in list, asking the user for confirmation when needed.
  16405. When a buffer is unmodified, it is just killed. When modified, it is saved
  16406. \(if the user agrees) and then killed."
  16407. (let (buf file)
  16408. (while (setq buf (pop blist))
  16409. (setq file (buffer-file-name buf))
  16410. (when (and (buffer-modified-p buf)
  16411. file
  16412. (y-or-n-p (format "Save file %s? " file)))
  16413. (with-current-buffer buf (save-buffer)))
  16414. (kill-buffer buf))))
  16415. (defun org-agenda-prepare-buffers (files)
  16416. "Create buffers for all agenda files, protect archived trees and comments."
  16417. (interactive)
  16418. (let ((pa '(:org-archived t))
  16419. (pc '(:org-comment t))
  16420. (pall '(:org-archived t :org-comment t))
  16421. (inhibit-read-only t)
  16422. (org-inhibit-startup org-agenda-inhibit-startup)
  16423. (rea (concat ":" org-archive-tag ":"))
  16424. file re)
  16425. (setq org-tag-alist-for-agenda nil
  16426. org-tag-groups-alist-for-agenda nil)
  16427. (save-excursion
  16428. (save-restriction
  16429. (while (setq file (pop files))
  16430. (catch 'nextfile
  16431. (if (bufferp file)
  16432. (set-buffer file)
  16433. (org-check-agenda-file file)
  16434. (set-buffer (org-get-agenda-file-buffer file)))
  16435. (widen)
  16436. (org-set-regexps-and-options-for-tags)
  16437. (org-refresh-category-properties)
  16438. (org-refresh-properties org-effort-property 'org-effort)
  16439. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16440. (setq org-todo-keywords-for-agenda
  16441. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16442. (setq org-done-keywords-for-agenda
  16443. (append org-done-keywords-for-agenda org-done-keywords))
  16444. (setq org-todo-keyword-alist-for-agenda
  16445. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16446. (setq org-drawers-for-agenda
  16447. (append org-drawers-for-agenda org-drawers))
  16448. (unless (equal org-tag-alist-for-agenda org-tag-alist)
  16449. (setq org-tag-alist-for-agenda
  16450. (append org-tag-alist-for-agenda org-tag-alist)))
  16451. (if org-group-tags
  16452. (setq org-tag-groups-alist-for-agenda
  16453. (org-uniquify-alist
  16454. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16455. (org-with-silent-modifications
  16456. (save-excursion
  16457. (remove-text-properties (point-min) (point-max) pall)
  16458. (when org-agenda-skip-archived-trees
  16459. (goto-char (point-min))
  16460. (while (re-search-forward rea nil t)
  16461. (if (org-at-heading-p t)
  16462. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16463. (goto-char (point-min))
  16464. (setq re (format org-heading-keyword-regexp-format
  16465. org-comment-string))
  16466. (while (re-search-forward re nil t)
  16467. (add-text-properties
  16468. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16469. (setq org-todo-keywords-for-agenda
  16470. (org-uniquify org-todo-keywords-for-agenda))
  16471. (setq org-todo-keyword-alist-for-agenda
  16472. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16473. ;;;; CDLaTeX minor mode
  16474. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16475. "Keymap for the minor `org-cdlatex-mode'.")
  16476. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16477. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16478. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16479. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16480. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16481. (defvar org-cdlatex-texmathp-advice-is-done nil
  16482. "Flag remembering if we have applied the advice to texmathp already.")
  16483. (define-minor-mode org-cdlatex-mode
  16484. "Toggle the minor `org-cdlatex-mode'.
  16485. This mode supports entering LaTeX environment and math in LaTeX fragments
  16486. in Org-mode.
  16487. \\{org-cdlatex-mode-map}"
  16488. nil " OCDL" nil
  16489. (when org-cdlatex-mode
  16490. (require 'cdlatex)
  16491. (run-hooks 'cdlatex-mode-hook)
  16492. (cdlatex-compute-tables))
  16493. (unless org-cdlatex-texmathp-advice-is-done
  16494. (setq org-cdlatex-texmathp-advice-is-done t)
  16495. (defadvice texmathp (around org-math-always-on activate)
  16496. "Always return t in org-mode buffers.
  16497. This is because we want to insert math symbols without dollars even outside
  16498. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16499. an embedded LaTeX fragment, let texmathp do its job.
  16500. \\[org-cdlatex-mode-map]"
  16501. (interactive)
  16502. (let (p)
  16503. (cond
  16504. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16505. ((eq this-command 'cdlatex-math-symbol)
  16506. (setq ad-return-value t
  16507. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16508. (t
  16509. (let ((p (org-inside-LaTeX-fragment-p)))
  16510. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16511. (setq ad-return-value t
  16512. texmathp-why '("Org-mode embedded math" . 0))
  16513. (if p ad-do-it)))))))))
  16514. (defun turn-on-org-cdlatex ()
  16515. "Unconditionally turn on `org-cdlatex-mode'."
  16516. (org-cdlatex-mode 1))
  16517. (defun org-try-cdlatex-tab ()
  16518. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16519. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16520. - inside a LaTeX fragment, or
  16521. - after the first word in a line, where an abbreviation expansion could
  16522. insert a LaTeX environment."
  16523. (when org-cdlatex-mode
  16524. (cond
  16525. ;; Before any word on the line: No expansion possible.
  16526. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16527. ;; Just after first word on the line: Expand it. Make sure it
  16528. ;; cannot happen on headlines, though.
  16529. ((save-excursion
  16530. (skip-chars-backward "a-zA-Z0-9*")
  16531. (skip-chars-backward " \t")
  16532. (and (bolp) (not (org-at-heading-p))))
  16533. (cdlatex-tab) t)
  16534. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16535. (defun org-cdlatex-underscore-caret (&optional arg)
  16536. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16537. Revert to the normal definition outside of these fragments."
  16538. (interactive "P")
  16539. (if (org-inside-LaTeX-fragment-p)
  16540. (call-interactively 'cdlatex-sub-superscript)
  16541. (let (org-cdlatex-mode)
  16542. (call-interactively (key-binding (vector last-input-event))))))
  16543. (defun org-cdlatex-math-modify (&optional arg)
  16544. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16545. Revert to the normal definition outside of these fragments."
  16546. (interactive "P")
  16547. (if (org-inside-LaTeX-fragment-p)
  16548. (call-interactively 'cdlatex-math-modify)
  16549. (let (org-cdlatex-mode)
  16550. (call-interactively (key-binding (vector last-input-event))))))
  16551. ;;;; LaTeX fragments
  16552. (defvar org-latex-regexps
  16553. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16554. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16555. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16556. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16557. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16558. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16559. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16560. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16561. "Regular expressions for matching embedded LaTeX.")
  16562. (defun org-inside-LaTeX-fragment-p ()
  16563. "Test if point is inside a LaTeX fragment.
  16564. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16565. sequence appearing also before point.
  16566. Even though the matchers for math are configurable, this function assumes
  16567. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16568. delimiters are skipped when they have been removed by customization.
  16569. The return value is nil, or a cons cell with the delimiter and the
  16570. position of this delimiter.
  16571. This function does a reasonably good job, but can locally be fooled by
  16572. for example currency specifications. For example it will assume being in
  16573. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16574. fragments that are properly closed, but during editing, we have to live
  16575. with the uncertainty caused by missing closing delimiters. This function
  16576. looks only before point, not after."
  16577. (catch 'exit
  16578. (let ((pos (point))
  16579. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16580. (lim (progn
  16581. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16582. (point)))
  16583. dd-on str (start 0) m re)
  16584. (goto-char pos)
  16585. (when dodollar
  16586. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16587. re (nth 1 (assoc "$" org-latex-regexps)))
  16588. (while (string-match re str start)
  16589. (cond
  16590. ((= (match-end 0) (length str))
  16591. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16592. ((= (match-end 0) (- (length str) 5))
  16593. (throw 'exit nil))
  16594. (t (setq start (match-end 0))))))
  16595. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16596. (goto-char pos)
  16597. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16598. (and (match-beginning 2) (throw 'exit nil))
  16599. ;; count $$
  16600. (while (re-search-backward "\\$\\$" lim t)
  16601. (setq dd-on (not dd-on)))
  16602. (goto-char pos)
  16603. (if dd-on (cons "$$" m))))))
  16604. (defun org-inside-latex-macro-p ()
  16605. "Is point inside a LaTeX macro or its arguments?"
  16606. (save-match-data
  16607. (org-in-regexp
  16608. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16609. (defvar org-latex-fragment-image-overlays nil
  16610. "List of overlays carrying the images of latex fragments.")
  16611. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16612. (defun org-remove-latex-fragment-image-overlays ()
  16613. "Remove all overlays with LaTeX fragment images in current buffer."
  16614. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16615. (setq org-latex-fragment-image-overlays nil))
  16616. (defun org-preview-latex-fragment (&optional subtree)
  16617. "Preview the LaTeX fragment at point, or all locally or globally.
  16618. If the cursor is in a LaTeX fragment, create the image and overlay
  16619. it over the source code. If there is no fragment at point, display
  16620. all fragments in the current text, from one headline to the next. With
  16621. prefix SUBTREE, display all fragments in the current subtree. With a
  16622. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16623. the cursor is before the first headline,
  16624. display all fragments in the buffer.
  16625. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16626. (interactive "P")
  16627. (unless buffer-file-name
  16628. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16629. (org-remove-latex-fragment-image-overlays)
  16630. (save-excursion
  16631. (save-restriction
  16632. (let (beg end at msg)
  16633. (cond
  16634. ((or (equal subtree '(16))
  16635. (not (save-excursion
  16636. (re-search-backward org-outline-regexp-bol nil t))))
  16637. (setq beg (point-min) end (point-max)
  16638. msg "Creating images for buffer...%s"))
  16639. ((equal subtree '(4))
  16640. (org-back-to-heading)
  16641. (setq beg (point) end (org-end-of-subtree t)
  16642. msg "Creating images for subtree...%s"))
  16643. (t
  16644. (if (setq at (org-inside-LaTeX-fragment-p))
  16645. (goto-char (max (point-min) (- (cdr at) 2)))
  16646. (org-back-to-heading))
  16647. (setq beg (point) end (progn (outline-next-heading) (point))
  16648. msg (if at "Creating image...%s"
  16649. "Creating images for entry...%s"))))
  16650. (message msg "")
  16651. (narrow-to-region beg end)
  16652. (goto-char beg)
  16653. (org-format-latex
  16654. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16655. (file-name-nondirectory
  16656. buffer-file-name)))
  16657. default-directory 'overlays msg at 'forbuffer
  16658. org-latex-create-formula-image-program)
  16659. (message msg "done. Use `C-c C-c' to remove images.")))))
  16660. (defun org-format-latex (prefix &optional dir overlays msg at
  16661. forbuffer processing-type)
  16662. "Replace LaTeX fragments with links to an image, and produce images.
  16663. Some of the options can be changed using the variable
  16664. `org-format-latex-options'."
  16665. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16666. (let* ((prefixnodir (file-name-nondirectory prefix))
  16667. (absprefix (expand-file-name prefix dir))
  16668. (todir (file-name-directory absprefix))
  16669. (opt org-format-latex-options)
  16670. (optnew org-format-latex-options)
  16671. (matchers (plist-get opt :matchers))
  16672. (re-list org-latex-regexps)
  16673. (cnt 0) txt hash link beg end re e checkdir
  16674. string
  16675. m n block-type block linkfile movefile ov)
  16676. ;; Check the different regular expressions
  16677. (while (setq e (pop re-list))
  16678. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16679. block (if block-type "\n\n" ""))
  16680. (when (member m matchers)
  16681. (goto-char (point-min))
  16682. (while (re-search-forward re nil t)
  16683. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16684. (or (not overlays)
  16685. (not (eq (get-char-property (match-beginning n)
  16686. 'org-overlay-type)
  16687. 'org-latex-overlay))))
  16688. (cond
  16689. ((eq processing-type 'verbatim))
  16690. ((eq processing-type 'mathjax)
  16691. ;; Prepare for MathJax processing.
  16692. (setq string (match-string n))
  16693. (when (member m '("$" "$1"))
  16694. (save-excursion
  16695. (delete-region (match-beginning n) (match-end n))
  16696. (goto-char (match-beginning n))
  16697. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16698. ((or (eq processing-type 'dvipng)
  16699. (eq processing-type 'imagemagick))
  16700. ;; Process to an image.
  16701. (setq txt (match-string n)
  16702. beg (match-beginning n) end (match-end n)
  16703. cnt (1+ cnt))
  16704. (let ((face (face-at-point))
  16705. (fg (plist-get opt :foreground))
  16706. (bg (plist-get opt :background))
  16707. ;; Ensure full list is printed.
  16708. print-length print-level)
  16709. (when forbuffer
  16710. ;; Get the colors from the face at point.
  16711. (goto-char beg)
  16712. (when (eq fg 'auto)
  16713. (setq fg (face-attribute face :foreground nil 'default)))
  16714. (when (eq bg 'auto)
  16715. (setq bg (face-attribute face :background nil 'default)))
  16716. (setq optnew (copy-sequence opt))
  16717. (plist-put optnew :foreground fg)
  16718. (plist-put optnew :background bg))
  16719. (setq hash (sha1 (prin1-to-string
  16720. (list org-format-latex-header
  16721. org-latex-default-packages-alist
  16722. org-latex-packages-alist
  16723. org-format-latex-options
  16724. forbuffer txt fg bg)))
  16725. linkfile (format "%s_%s.png" prefix hash)
  16726. movefile (format "%s_%s.png" absprefix hash)))
  16727. (setq link (concat block "[[file:" linkfile "]]" block))
  16728. (if msg (message msg cnt))
  16729. (goto-char beg)
  16730. (unless checkdir ; Ensure the directory exists.
  16731. (setq checkdir t)
  16732. (or (file-directory-p todir) (make-directory todir t)))
  16733. (unless (file-exists-p movefile)
  16734. (org-create-formula-image
  16735. txt movefile optnew forbuffer processing-type))
  16736. (if overlays
  16737. (progn
  16738. (mapc (lambda (o)
  16739. (if (eq (overlay-get o 'org-overlay-type)
  16740. 'org-latex-overlay)
  16741. (delete-overlay o)))
  16742. (overlays-in beg end))
  16743. (setq ov (make-overlay beg end))
  16744. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16745. (if (featurep 'xemacs)
  16746. (progn
  16747. (overlay-put ov 'invisible t)
  16748. (overlay-put
  16749. ov 'end-glyph
  16750. (make-glyph (vector 'png :file movefile))))
  16751. (overlay-put
  16752. ov 'display
  16753. (list 'image :type 'png :file movefile :ascent 'center)))
  16754. (push ov org-latex-fragment-image-overlays)
  16755. (goto-char end))
  16756. (delete-region beg end)
  16757. (insert (org-add-props link
  16758. (list 'org-latex-src
  16759. (replace-regexp-in-string
  16760. "\"" "" txt)
  16761. 'org-latex-src-embed-type
  16762. (if block-type 'paragraph 'character))))))
  16763. ((eq processing-type 'mathml)
  16764. ;; Process to MathML
  16765. (unless (save-match-data (org-format-latex-mathml-available-p))
  16766. (error "LaTeX to MathML converter not configured"))
  16767. (setq txt (match-string n)
  16768. beg (match-beginning n) end (match-end n)
  16769. cnt (1+ cnt))
  16770. (if msg (message msg cnt))
  16771. (goto-char beg)
  16772. (delete-region beg end)
  16773. (insert (org-format-latex-as-mathml
  16774. txt block-type prefix dir)))
  16775. (t
  16776. (error "Unknown conversion type %s for latex fragments"
  16777. processing-type)))))))))
  16778. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16779. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16780. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16781. sucessful, return the portion between \"<math...> </math>\"
  16782. elements otherwise return nil. When MATHML-FILE is specified,
  16783. write the results in to that file. When invoked as an
  16784. interactive command, prompt for LATEX-FRAG, with initial value
  16785. set to the current active region and echo the results for user
  16786. inspection."
  16787. (interactive (list (let ((frag (when (org-region-active-p)
  16788. (buffer-substring-no-properties
  16789. (region-beginning) (region-end)))))
  16790. (read-string "LaTeX Fragment: " frag nil frag))))
  16791. (unless latex-frag (error "Invalid latex-frag"))
  16792. (let* ((tmp-in-file (file-relative-name
  16793. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16794. (ignore (write-region latex-frag nil tmp-in-file))
  16795. (tmp-out-file (file-relative-name
  16796. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16797. (cmd (format-spec
  16798. org-latex-to-mathml-convert-command
  16799. `((?j . ,(shell-quote-argument
  16800. (expand-file-name org-latex-to-mathml-jar-file)))
  16801. (?I . ,(shell-quote-argument tmp-in-file))
  16802. (?o . ,(shell-quote-argument tmp-out-file)))))
  16803. mathml shell-command-output)
  16804. (when (org-called-interactively-p 'any)
  16805. (unless (org-format-latex-mathml-available-p)
  16806. (error "LaTeX to MathML converter not configured")))
  16807. (message "Running %s" cmd)
  16808. (setq shell-command-output (shell-command-to-string cmd))
  16809. (setq mathml
  16810. (when (file-readable-p tmp-out-file)
  16811. (with-current-buffer (find-file-noselect tmp-out-file t)
  16812. (goto-char (point-min))
  16813. (when (re-search-forward
  16814. (concat
  16815. (regexp-quote
  16816. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16817. "\\(.\\|\n\\)*"
  16818. (regexp-quote "</math>")) nil t)
  16819. (prog1 (match-string 0) (kill-buffer))))))
  16820. (cond
  16821. (mathml
  16822. (setq mathml
  16823. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16824. (when mathml-file
  16825. (write-region mathml nil mathml-file))
  16826. (when (org-called-interactively-p 'any)
  16827. (message mathml)))
  16828. ((message "LaTeX to MathML conversion failed")
  16829. (message shell-command-output)))
  16830. (delete-file tmp-in-file)
  16831. (when (file-exists-p tmp-out-file)
  16832. (delete-file tmp-out-file))
  16833. mathml))
  16834. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16835. prefix &optional dir)
  16836. "Use `org-create-math-formula' but check local cache first."
  16837. (let* ((absprefix (expand-file-name prefix dir))
  16838. (print-length nil) (print-level nil)
  16839. (formula-id (concat
  16840. "formula-"
  16841. (sha1
  16842. (prin1-to-string
  16843. (list latex-frag
  16844. org-latex-to-mathml-convert-command)))))
  16845. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16846. (formula-cache-dir (file-name-directory formula-cache)))
  16847. (unless (file-directory-p formula-cache-dir)
  16848. (make-directory formula-cache-dir t))
  16849. (unless (file-exists-p formula-cache)
  16850. (org-create-math-formula latex-frag formula-cache))
  16851. (if (file-exists-p formula-cache)
  16852. ;; Successful conversion. Return the link to MathML file.
  16853. (org-add-props
  16854. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16855. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16856. 'org-latex-src-embed-type (if latex-frag-type
  16857. 'paragraph 'character)))
  16858. ;; Failed conversion. Return the LaTeX fragment verbatim
  16859. latex-frag)))
  16860. (defun org-create-formula-image (string tofile options buffer &optional type)
  16861. "Create an image from LaTeX source using dvipng or convert.
  16862. This function calls either `org-create-formula-image-with-dvipng'
  16863. or `org-create-formula-image-with-imagemagick' depending on the
  16864. value of `org-latex-create-formula-image-program' or on the value
  16865. of the optional TYPE variable.
  16866. Note: ultimately these two function should be combined as they
  16867. share a good deal of logic."
  16868. (org-check-external-command
  16869. "latex" "needed to convert LaTeX fragments to images")
  16870. (funcall
  16871. (case (or type org-latex-create-formula-image-program)
  16872. ('dvipng
  16873. (org-check-external-command
  16874. "dvipng" "needed to convert LaTeX fragments to images")
  16875. #'org-create-formula-image-with-dvipng)
  16876. ('imagemagick
  16877. (org-check-external-command
  16878. "convert" "you need to install imagemagick")
  16879. #'org-create-formula-image-with-imagemagick)
  16880. (t (error
  16881. "invalid value of `org-latex-create-formula-image-program'")))
  16882. string tofile options buffer))
  16883. (declare-function org-export--get-global-options "ox" (&optional backend))
  16884. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16885. (defun org-create-formula--latex-header ()
  16886. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16887. (org-latex-guess-inputenc
  16888. (org-splice-latex-header
  16889. org-format-latex-header
  16890. org-latex-default-packages-alist
  16891. org-latex-packages-alist t
  16892. (plist-get
  16893. (org-combine-plists
  16894. (org-export--get-global-options 'latex)
  16895. (org-export--get-inbuffer-options 'latex))
  16896. :latex-header))))
  16897. ;; This function borrows from Ganesh Swami's latex2png.el
  16898. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16899. "This calls dvipng."
  16900. (require 'ox-latex)
  16901. (let* ((tmpdir (if (featurep 'xemacs)
  16902. (temp-directory)
  16903. temporary-file-directory))
  16904. (texfilebase (make-temp-name
  16905. (expand-file-name "orgtex" tmpdir)))
  16906. (texfile (concat texfilebase ".tex"))
  16907. (dvifile (concat texfilebase ".dvi"))
  16908. (pngfile (concat texfilebase ".png"))
  16909. (fnh (if (featurep 'xemacs)
  16910. (font-height (face-font 'default))
  16911. (face-attribute 'default :height nil)))
  16912. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16913. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16914. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16915. "Black"))
  16916. (bg (or (plist-get options (if buffer :background :html-background))
  16917. "Transparent")))
  16918. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16919. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16920. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16921. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16922. (let ((latex-header (org-create-formula--latex-header)))
  16923. (with-temp-file texfile
  16924. (insert latex-header)
  16925. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16926. (let ((dir default-directory))
  16927. (condition-case nil
  16928. (progn
  16929. (cd tmpdir)
  16930. (call-process "latex" nil nil nil texfile))
  16931. (error nil))
  16932. (cd dir))
  16933. (if (not (file-exists-p dvifile))
  16934. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16935. (condition-case nil
  16936. (if (featurep 'xemacs)
  16937. (call-process "dvipng" nil nil nil
  16938. "-fg" fg "-bg" bg
  16939. "-T" "tight"
  16940. "-o" pngfile
  16941. dvifile)
  16942. (call-process "dvipng" nil nil nil
  16943. "-fg" fg "-bg" bg
  16944. "-D" dpi
  16945. ;;"-x" scale "-y" scale
  16946. "-T" "tight"
  16947. "-o" pngfile
  16948. dvifile))
  16949. (error nil))
  16950. (if (not (file-exists-p pngfile))
  16951. (if org-format-latex-signal-error
  16952. (error "Failed to create png file from %s" texfile)
  16953. (message "Failed to create png file from %s" texfile)
  16954. nil)
  16955. ;; Use the requested file name and clean up
  16956. (copy-file pngfile tofile 'replace)
  16957. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16958. (if (file-exists-p (concat texfilebase e))
  16959. (delete-file (concat texfilebase e))))
  16960. pngfile))))
  16961. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  16962. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16963. "This calls convert, which is included into imagemagick."
  16964. (require 'ox-latex)
  16965. (let* ((tmpdir (if (featurep 'xemacs)
  16966. (temp-directory)
  16967. temporary-file-directory))
  16968. (texfilebase (make-temp-name
  16969. (expand-file-name "orgtex" tmpdir)))
  16970. (texfile (concat texfilebase ".tex"))
  16971. (pdffile (concat texfilebase ".pdf"))
  16972. (pngfile (concat texfilebase ".png"))
  16973. (fnh (if (featurep 'xemacs)
  16974. (font-height (face-font 'default))
  16975. (face-attribute 'default :height nil)))
  16976. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16977. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16978. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16979. "black"))
  16980. (bg (or (plist-get options (if buffer :background :html-background))
  16981. "white")))
  16982. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16983. (setq fg (org-latex-color-format fg)))
  16984. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16985. (setq bg (org-latex-color-format
  16986. (if (string= bg "Transparent") "white" bg))))
  16987. (let ((latex-header (org-create-formula--latex-header)))
  16988. (with-temp-file texfile
  16989. (insert latex-header)
  16990. (insert "\n\\begin{document}\n"
  16991. "\\definecolor{fg}{rgb}{" fg "}\n"
  16992. "\\definecolor{bg}{rgb}{" bg "}\n"
  16993. "\n\\pagecolor{bg}\n"
  16994. "\n{\\color{fg}\n"
  16995. string
  16996. "\n}\n"
  16997. "\n\\end{document}\n")))
  16998. (org-latex-compile texfile t)
  16999. (if (not (file-exists-p pdffile))
  17000. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17001. (condition-case nil
  17002. (if (featurep 'xemacs)
  17003. (call-process "convert" nil nil nil
  17004. "-density" "96"
  17005. "-trim"
  17006. "-antialias"
  17007. pdffile
  17008. "-quality" "100"
  17009. ;; "-sharpen" "0x1.0"
  17010. pngfile)
  17011. (call-process "convert" nil nil nil
  17012. "-density" dpi
  17013. "-trim"
  17014. "-antialias"
  17015. pdffile
  17016. "-quality" "100"
  17017. ;; "-sharpen" "0x1.0"
  17018. pngfile))
  17019. (error nil))
  17020. (if (not (file-exists-p pngfile))
  17021. (if org-format-latex-signal-error
  17022. (error "Failed to create png file from %s" texfile)
  17023. (message "Failed to create png file from %s" texfile)
  17024. nil)
  17025. ;; Use the requested file name and clean up
  17026. (copy-file pngfile tofile 'replace)
  17027. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17028. (if (file-exists-p (concat texfilebase e))
  17029. (delete-file (concat texfilebase e))))
  17030. pngfile))))
  17031. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17032. "Fill a LaTeX header template TPL.
  17033. In the template, the following place holders will be recognized:
  17034. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17035. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17036. [PACKAGES] \\usepackage statements for PKG
  17037. [NO-PACKAGES] do not include PKG
  17038. [EXTRA] the string EXTRA
  17039. [NO-EXTRA] do not include EXTRA
  17040. For backward compatibility, if both the positive and the negative place
  17041. holder is missing, the positive one (without the \"NO-\") will be
  17042. assumed to be present at the end of the template.
  17043. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17044. EXTRA is a string.
  17045. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17046. (let (rpl (end ""))
  17047. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17048. (setq rpl (if (or (match-end 1) (not def-pkg))
  17049. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17050. tpl (replace-match rpl t t tpl))
  17051. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17052. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17053. (setq rpl (if (or (match-end 1) (not pkg))
  17054. "" (org-latex-packages-to-string pkg snippets-p t))
  17055. tpl (replace-match rpl t t tpl))
  17056. (if pkg (setq end
  17057. (concat end "\n"
  17058. (org-latex-packages-to-string pkg snippets-p)))))
  17059. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17060. (setq rpl (if (or (match-end 1) (not extra))
  17061. "" (concat extra "\n"))
  17062. tpl (replace-match rpl t t tpl))
  17063. (if (and extra (string-match "\\S-" extra))
  17064. (setq end (concat end "\n" extra))))
  17065. (if (string-match "\\S-" end)
  17066. (concat tpl "\n" end)
  17067. tpl)))
  17068. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17069. "Turn an alist of packages into a string with the \\usepackage macros."
  17070. (setq pkg (mapconcat (lambda(p)
  17071. (cond
  17072. ((stringp p) p)
  17073. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17074. (format "%% Package %s omitted" (cadr p)))
  17075. ((equal "" (car p))
  17076. (format "\\usepackage{%s}" (cadr p)))
  17077. (t
  17078. (format "\\usepackage[%s]{%s}"
  17079. (car p) (cadr p)))))
  17080. pkg
  17081. "\n"))
  17082. (if newline (concat pkg "\n") pkg))
  17083. (defun org-dvipng-color (attr)
  17084. "Return a RGB color specification for dvipng."
  17085. (apply 'format "rgb %s %s %s"
  17086. (mapcar 'org-normalize-color
  17087. (if (featurep 'xemacs)
  17088. (color-rgb-components
  17089. (face-property 'default
  17090. (cond ((eq attr :foreground) 'foreground)
  17091. ((eq attr :background) 'background))))
  17092. (color-values (face-attribute 'default attr nil))))))
  17093. (defun org-dvipng-color-format (color-name)
  17094. "Convert COLOR-NAME to a RGB color value for dvipng."
  17095. (apply 'format "rgb %s %s %s"
  17096. (mapcar 'org-normalize-color
  17097. (color-values color-name))))
  17098. (defun org-latex-color (attr)
  17099. "Return a RGB color for the LaTeX color package."
  17100. (apply 'format "%s,%s,%s"
  17101. (mapcar 'org-normalize-color
  17102. (if (featurep 'xemacs)
  17103. (color-rgb-components
  17104. (face-property 'default
  17105. (cond ((eq attr :foreground) 'foreground)
  17106. ((eq attr :background) 'background))))
  17107. (color-values (face-attribute 'default attr nil))))))
  17108. (defun org-latex-color-format (color-name)
  17109. "Convert COLOR-NAME to a RGB color value."
  17110. (apply 'format "%s,%s,%s"
  17111. (mapcar 'org-normalize-color
  17112. (color-values color-name))))
  17113. (defun org-normalize-color (value)
  17114. "Return string to be used as color value for an RGB component."
  17115. (format "%g" (/ value 65535.0)))
  17116. ;; Image display
  17117. (defvar org-inline-image-overlays nil)
  17118. (make-variable-buffer-local 'org-inline-image-overlays)
  17119. (defun org-toggle-inline-images (&optional include-linked)
  17120. "Toggle the display of inline images.
  17121. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17122. (interactive "P")
  17123. (if org-inline-image-overlays
  17124. (progn
  17125. (org-remove-inline-images)
  17126. (message "Inline image display turned off"))
  17127. (org-display-inline-images include-linked)
  17128. (if (and (org-called-interactively-p)
  17129. org-inline-image-overlays)
  17130. (message "%d images displayed inline"
  17131. (length org-inline-image-overlays))
  17132. (message "No images to display inline"))))
  17133. (defun org-redisplay-inline-images ()
  17134. "Refresh the display of inline images."
  17135. (interactive)
  17136. (if (not org-inline-image-overlays)
  17137. (org-toggle-inline-images)
  17138. (org-toggle-inline-images)
  17139. (org-toggle-inline-images)))
  17140. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17141. "Display inline images.
  17142. Normally only links without a description part are inlined, because this
  17143. is how it will work for export. When INCLUDE-LINKED is set, also links
  17144. with a description part will be inlined. This can be nice for a quick
  17145. look at those images, but it does not reflect what exported files will look
  17146. like.
  17147. When REFRESH is set, refresh existing images between BEG and END.
  17148. This will create new image displays only if necessary.
  17149. BEG and END default to the buffer boundaries."
  17150. (interactive "P")
  17151. (unless refresh
  17152. (org-remove-inline-images)
  17153. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17154. (save-excursion
  17155. (save-restriction
  17156. (widen)
  17157. (setq beg (or beg (point-min)) end (or end (point-max)))
  17158. (goto-char beg)
  17159. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17160. (substring (org-image-file-name-regexp) 0 -2)
  17161. "\\)\\]" (if include-linked "" "\\]")))
  17162. (case-fold-search t)
  17163. old file ov img type attrwidth width)
  17164. (while (re-search-forward re end t)
  17165. (setq old (get-char-property-and-overlay (match-beginning 1)
  17166. 'org-image-overlay)
  17167. file (expand-file-name
  17168. (concat (or (match-string 3) "") (match-string 4))))
  17169. (when (image-type-available-p 'imagemagick)
  17170. (setq attrwidth (if (or (listp org-image-actual-width)
  17171. (null org-image-actual-width))
  17172. (save-excursion
  17173. (save-match-data
  17174. (when (re-search-backward
  17175. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17176. (save-excursion
  17177. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17178. (string-to-number (match-string 1))))))
  17179. width (cond ((eq org-image-actual-width t) nil)
  17180. ((null org-image-actual-width) attrwidth)
  17181. ((numberp org-image-actual-width)
  17182. org-image-actual-width)
  17183. ((listp org-image-actual-width)
  17184. (or attrwidth (car org-image-actual-width))))
  17185. type (if width 'imagemagick)))
  17186. (when (file-exists-p file)
  17187. (if (and (car-safe old) refresh)
  17188. (image-refresh (overlay-get (cdr old) 'display))
  17189. (setq img (save-match-data (create-image file type nil :width width)))
  17190. (when img
  17191. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17192. (overlay-put ov 'display img)
  17193. (overlay-put ov 'face 'default)
  17194. (overlay-put ov 'org-image-overlay t)
  17195. (overlay-put ov 'modification-hooks
  17196. (list 'org-display-inline-remove-overlay))
  17197. (push ov org-inline-image-overlays)))))))))
  17198. (define-obsolete-function-alias
  17199. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17200. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17201. "Remove inline-display overlay if a corresponding region is modified."
  17202. (let ((inhibit-modification-hooks t))
  17203. (when (and ov after)
  17204. (delete ov org-inline-image-overlays)
  17205. (delete-overlay ov))))
  17206. (defun org-remove-inline-images ()
  17207. "Remove inline display of images."
  17208. (interactive)
  17209. (mapc 'delete-overlay org-inline-image-overlays)
  17210. (setq org-inline-image-overlays nil))
  17211. ;;;; Key bindings
  17212. ;; Outline functions from `outline-mode-prefix-map'
  17213. ;; that can be remapped in Org:
  17214. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17215. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17216. (define-key org-mode-map [remap outline-forward-same-level]
  17217. 'org-forward-heading-same-level)
  17218. (define-key org-mode-map [remap outline-backward-same-level]
  17219. 'org-backward-heading-same-level)
  17220. (define-key org-mode-map [remap show-branches]
  17221. 'org-kill-note-or-show-branches)
  17222. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17223. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17224. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17225. ;; Outline functions from `outline-mode-prefix-map' that can not
  17226. ;; be remapped in Org:
  17227. ;;
  17228. ;; - the column "key binding" shows whether the Outline function is still
  17229. ;; available in Org mode on the same key that it has been bound to in
  17230. ;; Outline mode:
  17231. ;; - "overridden": key used for a different functionality in Org mode
  17232. ;; - else: key still bound to the same Outline function in Org mode
  17233. ;;
  17234. ;; | Outline function | key binding | Org replacement |
  17235. ;; |------------------------------------+-------------+-----------------------|
  17236. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17237. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17238. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17239. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17240. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17241. ;; | `show-entry' | overridden | no replacement |
  17242. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17243. ;; | `show-branches' | `C-c C-k' | still same function |
  17244. ;; | `show-subtree' | overridden | visibility cycling |
  17245. ;; | `show-all' | overridden | no replacement |
  17246. ;; | `hide-subtree' | overridden | visibility cycling |
  17247. ;; | `hide-body' | overridden | no replacement |
  17248. ;; | `hide-entry' | overridden | visibility cycling |
  17249. ;; | `hide-leaves' | overridden | no replacement |
  17250. ;; | `hide-sublevels' | overridden | no replacement |
  17251. ;; | `hide-other' | overridden | no replacement |
  17252. ;; Make `C-c C-x' a prefix key
  17253. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17254. ;; TAB key with modifiers
  17255. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17256. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17257. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17258. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17259. ;; The following line is necessary under Suse GNU/Linux
  17260. (unless (featurep 'xemacs)
  17261. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17262. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17263. (define-key org-mode-map [backtab] 'org-shifttab)
  17264. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17265. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17266. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17267. ;; Cursor keys with modifiers
  17268. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17269. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17270. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17271. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17272. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17273. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17274. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17275. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17276. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17277. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17278. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17279. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17280. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17281. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17282. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17283. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17284. ;; Babel keys
  17285. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17286. (mapc (lambda (pair)
  17287. (define-key org-babel-map (car pair) (cdr pair)))
  17288. org-babel-key-bindings)
  17289. ;;; Extra keys for tty access.
  17290. ;; We only set them when really needed because otherwise the
  17291. ;; menus don't show the simple keys
  17292. (when (or org-use-extra-keys
  17293. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17294. (not window-system))
  17295. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17296. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17297. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17298. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17299. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17300. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17301. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17302. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17303. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17304. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17305. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17306. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17307. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17308. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17309. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17310. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17311. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17312. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17313. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17314. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17315. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17316. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17317. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17318. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17319. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17320. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17321. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17322. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17323. ;; All the other keys
  17324. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17325. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17326. (if (boundp 'narrow-map)
  17327. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17328. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17329. (if (boundp 'narrow-map)
  17330. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17331. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17332. (if (boundp 'narrow-map)
  17333. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17334. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17335. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17336. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17337. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17338. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17339. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17340. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17341. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17342. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17343. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17344. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17345. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17346. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17347. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17348. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17349. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17350. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17351. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17352. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17353. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17354. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17355. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17356. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17357. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17358. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17359. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17360. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17361. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17362. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17363. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17364. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17365. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17366. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17367. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17368. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17369. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17370. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17371. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17372. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17373. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17374. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17375. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17376. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17377. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17378. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17379. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17380. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17381. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17382. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17383. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17384. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17385. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17386. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17387. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17388. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17389. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17390. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17391. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17392. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17393. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17394. (org-defkey org-mode-map "\C-m" 'org-return)
  17395. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17396. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17397. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17398. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17399. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17400. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17401. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17402. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17403. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17404. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17405. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17406. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17407. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17408. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17409. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17410. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17411. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17412. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17413. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17414. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17415. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17416. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17417. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17418. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17419. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17420. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17421. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17422. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17423. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17424. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17425. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17426. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17427. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17428. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17429. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17430. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17431. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17432. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17433. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17434. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17435. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17436. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17437. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17438. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17439. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17440. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17441. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17442. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17443. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17444. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17445. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17446. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17447. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17448. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17449. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17450. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17451. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17452. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17453. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17454. (when (featurep 'xemacs)
  17455. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17456. (defconst org-speed-commands-default
  17457. '(
  17458. ("Outline Navigation")
  17459. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17460. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17461. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17462. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17463. ("F" . org-next-block)
  17464. ("B" . org-previous-block)
  17465. ("u" . (org-speed-move-safe 'outline-up-heading))
  17466. ("j" . org-goto)
  17467. ("g" . (org-refile t))
  17468. ("Outline Visibility")
  17469. ("c" . org-cycle)
  17470. ("C" . org-shifttab)
  17471. (" " . org-display-outline-path)
  17472. ("s" . org-narrow-to-subtree)
  17473. ("=" . org-columns)
  17474. ("Outline Structure Editing")
  17475. ("U" . org-shiftmetaup)
  17476. ("D" . org-shiftmetadown)
  17477. ("r" . org-metaright)
  17478. ("l" . org-metaleft)
  17479. ("R" . org-shiftmetaright)
  17480. ("L" . org-shiftmetaleft)
  17481. ("i" . (progn (forward-char 1) (call-interactively
  17482. 'org-insert-heading-respect-content)))
  17483. ("^" . org-sort)
  17484. ("w" . org-refile)
  17485. ("a" . org-archive-subtree-default-with-confirmation)
  17486. ("@" . org-mark-subtree)
  17487. ("#" . org-toggle-comment)
  17488. ("Clock Commands")
  17489. ("I" . org-clock-in)
  17490. ("O" . org-clock-out)
  17491. ("Meta Data Editing")
  17492. ("t" . org-todo)
  17493. ("," . (org-priority))
  17494. ("0" . (org-priority ?\ ))
  17495. ("1" . (org-priority ?A))
  17496. ("2" . (org-priority ?B))
  17497. ("3" . (org-priority ?C))
  17498. (":" . org-set-tags-command)
  17499. ("e" . org-set-effort)
  17500. ("E" . org-inc-effort)
  17501. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17502. (org-entry-put (point) "APPT_WARNTIME" m)))
  17503. ("Agenda Views etc")
  17504. ("v" . org-agenda)
  17505. ("/" . org-sparse-tree)
  17506. ("Misc")
  17507. ("o" . org-open-at-point)
  17508. ("?" . org-speed-command-help)
  17509. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17510. (">" . (org-agenda-remove-restriction-lock))
  17511. )
  17512. "The default speed commands.")
  17513. (defun org-print-speed-command (e)
  17514. (if (> (length (car e)) 1)
  17515. (progn
  17516. (princ "\n")
  17517. (princ (car e))
  17518. (princ "\n")
  17519. (princ (make-string (length (car e)) ?-))
  17520. (princ "\n"))
  17521. (princ (car e))
  17522. (princ " ")
  17523. (if (symbolp (cdr e))
  17524. (princ (symbol-name (cdr e)))
  17525. (prin1 (cdr e)))
  17526. (princ "\n")))
  17527. (defun org-speed-command-help ()
  17528. "Show the available speed commands."
  17529. (interactive)
  17530. (if (not org-use-speed-commands)
  17531. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17532. (with-output-to-temp-buffer "*Help*"
  17533. (princ "User-defined Speed commands\n===========================\n")
  17534. (mapc 'org-print-speed-command org-speed-commands-user)
  17535. (princ "\n")
  17536. (princ "Built-in Speed commands\n=======================\n")
  17537. (mapc 'org-print-speed-command org-speed-commands-default))
  17538. (with-current-buffer "*Help*"
  17539. (setq truncate-lines t))))
  17540. (defun org-speed-move-safe (cmd)
  17541. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17542. If not, return to the original position and throw an error."
  17543. (interactive)
  17544. (let ((pos (point)))
  17545. (call-interactively cmd)
  17546. (unless (and (bolp) (org-at-heading-p))
  17547. (goto-char pos)
  17548. (error "Boundary reached while executing %s" cmd))))
  17549. (defvar org-self-insert-command-undo-counter 0)
  17550. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17551. (defvar org-speed-command nil)
  17552. (define-obsolete-function-alias
  17553. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17554. (defun org-speed-command-activate (keys)
  17555. "Hook for activating single-letter speed commands.
  17556. `org-speed-commands-default' specifies a minimal command set.
  17557. Use `org-speed-commands-user' for further customization."
  17558. (when (or (and (bolp) (looking-at org-outline-regexp))
  17559. (and (functionp org-use-speed-commands)
  17560. (funcall org-use-speed-commands)))
  17561. (cdr (assoc keys (append org-speed-commands-user
  17562. org-speed-commands-default)))))
  17563. (define-obsolete-function-alias
  17564. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17565. (defun org-babel-speed-command-activate (keys)
  17566. "Hook for activating single-letter code block commands."
  17567. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17568. (cdr (assoc keys org-babel-key-bindings))))
  17569. (defcustom org-speed-command-hook
  17570. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17571. "Hook for activating speed commands at strategic locations.
  17572. Hook functions are called in sequence until a valid handler is
  17573. found.
  17574. Each hook takes a single argument, a user-pressed command key
  17575. which is also a `self-insert-command' from the global map.
  17576. Within the hook, examine the cursor position and the command key
  17577. and return nil or a valid handler as appropriate. Handler could
  17578. be one of an interactive command, a function, or a form.
  17579. Set `org-use-speed-commands' to non-nil value to enable this
  17580. hook. The default setting is `org-speed-command-activate'."
  17581. :group 'org-structure
  17582. :version "24.1"
  17583. :type 'hook)
  17584. (defun org-self-insert-command (N)
  17585. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17586. If the cursor is in a table looking at whitespace, the whitespace is
  17587. overwritten, and the table is not marked as requiring realignment."
  17588. (interactive "p")
  17589. (org-check-before-invisible-edit 'insert)
  17590. (cond
  17591. ((and org-use-speed-commands
  17592. (setq org-speed-command
  17593. (run-hook-with-args-until-success
  17594. 'org-speed-command-hook (this-command-keys))))
  17595. (cond
  17596. ((commandp org-speed-command)
  17597. (setq this-command org-speed-command)
  17598. (call-interactively org-speed-command))
  17599. ((functionp org-speed-command)
  17600. (funcall org-speed-command))
  17601. ((and org-speed-command (listp org-speed-command))
  17602. (eval org-speed-command))
  17603. (t (let (org-use-speed-commands)
  17604. (call-interactively 'org-self-insert-command)))))
  17605. ((and
  17606. (org-table-p)
  17607. (progn
  17608. ;; check if we blank the field, and if that triggers align
  17609. (and (featurep 'org-table) org-table-auto-blank-field
  17610. (member last-command
  17611. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17612. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17613. ;; got extra space, this field does not determine column width
  17614. (let (org-table-may-need-update) (org-table-blank-field))
  17615. ;; no extra space, this field may determine column width
  17616. (org-table-blank-field)))
  17617. t)
  17618. (eq N 1)
  17619. (looking-at "[^|\n]* |"))
  17620. (let (org-table-may-need-update)
  17621. (goto-char (1- (match-end 0)))
  17622. (backward-delete-char 1)
  17623. (goto-char (match-beginning 0))
  17624. (self-insert-command N)))
  17625. (t
  17626. (setq org-table-may-need-update t)
  17627. (self-insert-command N)
  17628. (org-fix-tags-on-the-fly)
  17629. (if org-self-insert-cluster-for-undo
  17630. (if (not (eq last-command 'org-self-insert-command))
  17631. (setq org-self-insert-command-undo-counter 1)
  17632. (if (>= org-self-insert-command-undo-counter 20)
  17633. (setq org-self-insert-command-undo-counter 1)
  17634. (and (> org-self-insert-command-undo-counter 0)
  17635. buffer-undo-list (listp buffer-undo-list)
  17636. (not (cadr buffer-undo-list)) ; remove nil entry
  17637. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17638. (setq org-self-insert-command-undo-counter
  17639. (1+ org-self-insert-command-undo-counter))))))))
  17640. (defun org-check-before-invisible-edit (kind)
  17641. "Check is editing if kind KIND would be dangerous with invisible text around.
  17642. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17643. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17644. (if (and org-catch-invisible-edits
  17645. (or (not (boundp 'visible-mode)) (not visible-mode))
  17646. (or (get-char-property (point) 'invisible)
  17647. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17648. ;; OK, we need to take a closer look
  17649. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17650. (invisible-before-point (if (bobp) nil (get-char-property
  17651. (1- (point)) 'invisible)))
  17652. (border-and-ok-direction
  17653. (or
  17654. ;; Check if we are acting predictably before invisible text
  17655. (and invisible-at-point (not invisible-before-point)
  17656. (memq kind '(insert delete-backward)))
  17657. ;; Check if we are acting predictably after invisible text
  17658. ;; This works not well, and I have turned it off. It seems
  17659. ;; better to always show and stop after invisible text.
  17660. ;; (and (not invisible-at-point) invisible-before-point
  17661. ;; (memq kind '(insert delete)))
  17662. )))
  17663. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17664. (memq invisible-before-point '(outline org-hide-block t)))
  17665. (if (eq org-catch-invisible-edits 'error)
  17666. (error "Editing in invisible areas is prohibited - make visible first"))
  17667. (if (and org-custom-properties-overlays
  17668. (y-or-n-p "Display invisible properties in this buffer? "))
  17669. (org-toggle-custom-properties-visibility)
  17670. ;; Make the area visible
  17671. (save-excursion
  17672. (if invisible-before-point
  17673. (goto-char (previous-single-char-property-change
  17674. (point) 'invisible)))
  17675. (org-cycle))
  17676. (cond
  17677. ((eq org-catch-invisible-edits 'show)
  17678. ;; That's it, we do the edit after showing
  17679. (message
  17680. "Unfolding invisible region around point before editing")
  17681. (sit-for 1))
  17682. ((and (eq org-catch-invisible-edits 'smart)
  17683. border-and-ok-direction)
  17684. (message "Unfolding invisible region around point before editing"))
  17685. (t
  17686. ;; Don't do the edit, make the user repeat it in full visibility
  17687. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17688. (defun org-fix-tags-on-the-fly ()
  17689. (when (and (equal (char-after (point-at-bol)) ?*)
  17690. (org-at-heading-p))
  17691. (org-align-tags-here org-tags-column)))
  17692. (defun org-delete-backward-char (N)
  17693. "Like `delete-backward-char', insert whitespace at field end in tables.
  17694. When deleting backwards, in tables this function will insert whitespace in
  17695. front of the next \"|\" separator, to keep the table aligned. The table will
  17696. still be marked for re-alignment if the field did fill the entire column,
  17697. because, in this case the deletion might narrow the column."
  17698. (interactive "p")
  17699. (save-match-data
  17700. (org-check-before-invisible-edit 'delete-backward)
  17701. (if (and (org-table-p)
  17702. (eq N 1)
  17703. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17704. (looking-at ".*?|"))
  17705. (let ((pos (point))
  17706. (noalign (looking-at "[^|\n\r]* |"))
  17707. (c org-table-may-need-update))
  17708. (backward-delete-char N)
  17709. (if (not overwrite-mode)
  17710. (progn
  17711. (skip-chars-forward "^|")
  17712. (insert " ")
  17713. (goto-char (1- pos))))
  17714. ;; noalign: if there were two spaces at the end, this field
  17715. ;; does not determine the width of the column.
  17716. (if noalign (setq org-table-may-need-update c)))
  17717. (backward-delete-char N)
  17718. (org-fix-tags-on-the-fly))))
  17719. (defun org-delete-char (N)
  17720. "Like `delete-char', but insert whitespace at field end in tables.
  17721. When deleting characters, in tables this function will insert whitespace in
  17722. front of the next \"|\" separator, to keep the table aligned. The table will
  17723. still be marked for re-alignment if the field did fill the entire column,
  17724. because, in this case the deletion might narrow the column."
  17725. (interactive "p")
  17726. (save-match-data
  17727. (org-check-before-invisible-edit 'delete)
  17728. (if (and (org-table-p)
  17729. (not (bolp))
  17730. (not (= (char-after) ?|))
  17731. (eq N 1))
  17732. (if (looking-at ".*?|")
  17733. (let ((pos (point))
  17734. (noalign (looking-at "[^|\n\r]* |"))
  17735. (c org-table-may-need-update))
  17736. (replace-match
  17737. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17738. (goto-char pos)
  17739. ;; noalign: if there were two spaces at the end, this field
  17740. ;; does not determine the width of the column.
  17741. (if noalign (setq org-table-may-need-update c)))
  17742. (delete-char N))
  17743. (delete-char N)
  17744. (org-fix-tags-on-the-fly))))
  17745. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17746. (put 'org-self-insert-command 'delete-selection t)
  17747. (put 'orgtbl-self-insert-command 'delete-selection t)
  17748. (put 'org-delete-char 'delete-selection 'supersede)
  17749. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17750. (put 'org-yank 'delete-selection 'yank)
  17751. ;; Make `flyspell-mode' delay after some commands
  17752. (put 'org-self-insert-command 'flyspell-delayed t)
  17753. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17754. (put 'org-delete-char 'flyspell-delayed t)
  17755. (put 'org-delete-backward-char 'flyspell-delayed t)
  17756. ;; Make pabbrev-mode expand after org-mode commands
  17757. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17758. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17759. ;; How to do this: Measure non-white length of current string
  17760. ;; If equal to column width, we should realign.
  17761. (defun org-remap (map &rest commands)
  17762. "In MAP, remap the functions given in COMMANDS.
  17763. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17764. (let (new old)
  17765. (while commands
  17766. (setq old (pop commands) new (pop commands))
  17767. (if (fboundp 'command-remapping)
  17768. (org-defkey map (vector 'remap old) new)
  17769. (substitute-key-definition old new map global-map)))))
  17770. (defun org-transpose-words ()
  17771. "Transpose words for Org.
  17772. This uses the `org-mode-transpose-word-syntax-table' syntax
  17773. table, which interprets characters in `org-emphasis-alist' as
  17774. word constituants."
  17775. (interactive)
  17776. (with-syntax-table org-mode-transpose-word-syntax-table
  17777. (call-interactively 'transpose-words)))
  17778. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17779. (when (eq org-enable-table-editor 'optimized)
  17780. ;; If the user wants maximum table support, we need to hijack
  17781. ;; some standard editing functions
  17782. (org-remap org-mode-map
  17783. 'self-insert-command 'org-self-insert-command
  17784. 'delete-char 'org-delete-char
  17785. 'delete-backward-char 'org-delete-backward-char)
  17786. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17787. (defvar org-ctrl-c-ctrl-c-hook nil
  17788. "Hook for functions attaching themselves to `C-c C-c'.
  17789. This can be used to add additional functionality to the C-c C-c
  17790. key which executes context-dependent commands. This hook is run
  17791. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17792. run after the last test.
  17793. Each function will be called with no arguments. The function
  17794. must check if the context is appropriate for it to act. If yes,
  17795. it should do its thing and then return a non-nil value. If the
  17796. context is wrong, just do nothing and return nil.")
  17797. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17798. "Hook for functions attaching themselves to `C-c C-c'.
  17799. This can be used to add additional functionality to the C-c C-c
  17800. key which executes context-dependent commands. This hook is run
  17801. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17802. before the first test.
  17803. Each function will be called with no arguments. The function
  17804. must check if the context is appropriate for it to act. If yes,
  17805. it should do its thing and then return a non-nil value. If the
  17806. context is wrong, just do nothing and return nil.")
  17807. (defvar org-tab-first-hook nil
  17808. "Hook for functions to attach themselves to TAB.
  17809. See `org-ctrl-c-ctrl-c-hook' for more information.
  17810. This hook runs as the first action when TAB is pressed, even before
  17811. `org-cycle' messes around with the `outline-regexp' to cater for
  17812. inline tasks and plain list item folding.
  17813. If any function in this hook returns t, any other actions that
  17814. would have been caused by TAB (such as table field motion or visibility
  17815. cycling) will not occur.")
  17816. (defvar org-tab-after-check-for-table-hook nil
  17817. "Hook for functions to attach themselves to TAB.
  17818. See `org-ctrl-c-ctrl-c-hook' for more information.
  17819. This hook runs after it has been established that the cursor is not in a
  17820. table, but before checking if the cursor is in a headline or if global cycling
  17821. should be done.
  17822. If any function in this hook returns t, not other actions like visibility
  17823. cycling will be done.")
  17824. (defvar org-tab-after-check-for-cycling-hook nil
  17825. "Hook for functions to attach themselves to TAB.
  17826. See `org-ctrl-c-ctrl-c-hook' for more information.
  17827. This hook runs after it has been established that not table field motion and
  17828. not visibility should be done because of current context. This is probably
  17829. the place where a package like yasnippets can hook in.")
  17830. (defvar org-tab-before-tab-emulation-hook nil
  17831. "Hook for functions to attach themselves to TAB.
  17832. See `org-ctrl-c-ctrl-c-hook' for more information.
  17833. This hook runs after every other options for TAB have been exhausted, but
  17834. before indentation and \t insertion takes place.")
  17835. (defvar org-metaleft-hook nil
  17836. "Hook for functions attaching themselves to `M-left'.
  17837. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17838. (defvar org-metaright-hook nil
  17839. "Hook for functions attaching themselves to `M-right'.
  17840. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17841. (defvar org-metaup-hook nil
  17842. "Hook for functions attaching themselves to `M-up'.
  17843. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17844. (defvar org-metadown-hook nil
  17845. "Hook for functions attaching themselves to `M-down'.
  17846. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17847. (defvar org-shiftmetaleft-hook nil
  17848. "Hook for functions attaching themselves to `M-S-left'.
  17849. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17850. (defvar org-shiftmetaright-hook nil
  17851. "Hook for functions attaching themselves to `M-S-right'.
  17852. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17853. (defvar org-shiftmetaup-hook nil
  17854. "Hook for functions attaching themselves to `M-S-up'.
  17855. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17856. (defvar org-shiftmetadown-hook nil
  17857. "Hook for functions attaching themselves to `M-S-down'.
  17858. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17859. (defvar org-metareturn-hook nil
  17860. "Hook for functions attaching themselves to `M-RET'.
  17861. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17862. (defvar org-shiftup-hook nil
  17863. "Hook for functions attaching themselves to `S-up'.
  17864. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17865. (defvar org-shiftup-final-hook nil
  17866. "Hook for functions attaching themselves to `S-up'.
  17867. This one runs after all other options except shift-select have been excluded.
  17868. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17869. (defvar org-shiftdown-hook nil
  17870. "Hook for functions attaching themselves to `S-down'.
  17871. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17872. (defvar org-shiftdown-final-hook nil
  17873. "Hook for functions attaching themselves to `S-down'.
  17874. This one runs after all other options except shift-select have been excluded.
  17875. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17876. (defvar org-shiftleft-hook nil
  17877. "Hook for functions attaching themselves to `S-left'.
  17878. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17879. (defvar org-shiftleft-final-hook nil
  17880. "Hook for functions attaching themselves to `S-left'.
  17881. This one runs after all other options except shift-select have been excluded.
  17882. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17883. (defvar org-shiftright-hook nil
  17884. "Hook for functions attaching themselves to `S-right'.
  17885. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17886. (defvar org-shiftright-final-hook nil
  17887. "Hook for functions attaching themselves to `S-right'.
  17888. This one runs after all other options except shift-select have been excluded.
  17889. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17890. (defun org-modifier-cursor-error ()
  17891. "Throw an error, a modified cursor command was applied in wrong context."
  17892. (error "This command is active in special context like tables, headlines or items"))
  17893. (defun org-shiftselect-error ()
  17894. "Throw an error because Shift-Cursor command was applied in wrong context."
  17895. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17896. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17897. (error "This command works only in special context like headlines or timestamps")))
  17898. (defun org-call-for-shift-select (cmd)
  17899. (let ((this-command-keys-shift-translated t))
  17900. (call-interactively cmd)))
  17901. (defun org-shifttab (&optional arg)
  17902. "Global visibility cycling or move to previous table field.
  17903. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17904. on context.
  17905. See the individual commands for more information."
  17906. (interactive "P")
  17907. (cond
  17908. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17909. ((integerp arg)
  17910. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17911. (message "Content view to level: %d" arg)
  17912. (org-content (prefix-numeric-value arg2))
  17913. (setq org-cycle-global-status 'overview)))
  17914. (t (call-interactively 'org-global-cycle))))
  17915. (defun org-shiftmetaleft ()
  17916. "Promote subtree or delete table column.
  17917. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17918. `org-table-delete-column', depending on context. See the
  17919. individual commands for more information."
  17920. (interactive)
  17921. (cond
  17922. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17923. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17924. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17925. ((if (not (org-region-active-p)) (org-at-item-p)
  17926. (save-excursion (goto-char (region-beginning))
  17927. (org-at-item-p)))
  17928. (call-interactively 'org-outdent-item-tree))
  17929. (t (org-modifier-cursor-error))))
  17930. (defun org-shiftmetaright ()
  17931. "Demote subtree or insert table column.
  17932. Calls `org-demote-subtree', `org-indent-item-tree', or
  17933. `org-table-insert-column', depending on context. See the
  17934. individual commands for more information."
  17935. (interactive)
  17936. (cond
  17937. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17938. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17939. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17940. ((if (not (org-region-active-p)) (org-at-item-p)
  17941. (save-excursion (goto-char (region-beginning))
  17942. (org-at-item-p)))
  17943. (call-interactively 'org-indent-item-tree))
  17944. (t (org-modifier-cursor-error))))
  17945. (defun org-shiftmetaup (&optional arg)
  17946. "Move subtree up or kill table row.
  17947. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17948. `org-move-item-up' or `org-timestamp-up', depending on context.
  17949. See the individual commands for more information."
  17950. (interactive "P")
  17951. (cond
  17952. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17953. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17954. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17955. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17956. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17957. (call-interactively 'org-timestamp-up)))
  17958. (t (call-interactively 'org-drag-line-backward))))
  17959. (defun org-shiftmetadown (&optional arg)
  17960. "Move subtree down or insert table row.
  17961. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17962. `org-move-item-down' or `org-timestamp-up', depending on context.
  17963. See the individual commands for more information."
  17964. (interactive "P")
  17965. (cond
  17966. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17967. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17968. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17969. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17970. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17971. (call-interactively 'org-timestamp-down)))
  17972. (t (call-interactively 'org-drag-line-forward))))
  17973. (defsubst org-hidden-tree-error ()
  17974. (error
  17975. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17976. (defun org-metaleft (&optional arg)
  17977. "Promote heading or move table column to left.
  17978. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17979. With no specific context, calls the Emacs default `backward-word'.
  17980. See the individual commands for more information."
  17981. (interactive "P")
  17982. (cond
  17983. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17984. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17985. ((org-with-limited-levels
  17986. (or (org-at-heading-p)
  17987. (and (org-region-active-p)
  17988. (save-excursion
  17989. (goto-char (region-beginning))
  17990. (org-at-heading-p)))))
  17991. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17992. (call-interactively 'org-do-promote))
  17993. ;; At an inline task.
  17994. ((org-at-heading-p)
  17995. (call-interactively 'org-inlinetask-promote))
  17996. ((or (org-at-item-p)
  17997. (and (org-region-active-p)
  17998. (save-excursion
  17999. (goto-char (region-beginning))
  18000. (org-at-item-p))))
  18001. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18002. (call-interactively 'org-outdent-item))
  18003. (t (call-interactively 'backward-word))))
  18004. (defun org-metaright (&optional arg)
  18005. "Demote a subtree, a list item or move table column to right.
  18006. In front of a drawer or a block keyword, indent it correctly.
  18007. With no specific context, calls the Emacs default `forward-word'.
  18008. See the individual commands for more information."
  18009. (interactive "P")
  18010. (cond
  18011. ((run-hook-with-args-until-success 'org-metaright-hook))
  18012. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18013. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18014. ((org-at-block-p) (call-interactively 'org-indent-block))
  18015. ((org-with-limited-levels
  18016. (or (org-at-heading-p)
  18017. (and (org-region-active-p)
  18018. (save-excursion
  18019. (goto-char (region-beginning))
  18020. (org-at-heading-p)))))
  18021. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18022. (call-interactively 'org-do-demote))
  18023. ;; At an inline task.
  18024. ((org-at-heading-p)
  18025. (call-interactively 'org-inlinetask-demote))
  18026. ((or (org-at-item-p)
  18027. (and (org-region-active-p)
  18028. (save-excursion
  18029. (goto-char (region-beginning))
  18030. (org-at-item-p))))
  18031. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18032. (call-interactively 'org-indent-item))
  18033. (t (call-interactively 'forward-word))))
  18034. (defun org-check-for-hidden (what)
  18035. "Check if there are hidden headlines/items in the current visual line.
  18036. WHAT can be either `headlines' or `items'. If the current line is
  18037. an outline or item heading and it has a folded subtree below it,
  18038. this function returns t, nil otherwise."
  18039. (let ((re (cond
  18040. ((eq what 'headlines) org-outline-regexp-bol)
  18041. ((eq what 'items) (org-item-beginning-re))
  18042. (t (error "This should not happen"))))
  18043. beg end)
  18044. (save-excursion
  18045. (catch 'exit
  18046. (unless (org-region-active-p)
  18047. (setq beg (point-at-bol))
  18048. (beginning-of-line 2)
  18049. (while (and (not (eobp)) ;; this is like `next-line'
  18050. (get-char-property (1- (point)) 'invisible))
  18051. (beginning-of-line 2))
  18052. (setq end (point))
  18053. (goto-char beg)
  18054. (goto-char (point-at-eol))
  18055. (setq end (max end (point)))
  18056. (while (re-search-forward re end t)
  18057. (if (get-char-property (match-beginning 0) 'invisible)
  18058. (throw 'exit t))))
  18059. nil))))
  18060. (defun org-metaup (&optional arg)
  18061. "Move subtree up or move table row up.
  18062. Calls `org-move-subtree-up' or `org-table-move-row' or
  18063. `org-move-item-up', depending on context. See the individual commands
  18064. for more information."
  18065. (interactive "P")
  18066. (cond
  18067. ((run-hook-with-args-until-success 'org-metaup-hook))
  18068. ((org-region-active-p)
  18069. (let* ((a (min (region-beginning) (region-end)))
  18070. (b (1- (max (region-beginning) (region-end))))
  18071. (c (save-excursion (goto-char a)
  18072. (move-beginning-of-line 0)))
  18073. (d (save-excursion (goto-char a)
  18074. (move-end-of-line 0) (point))))
  18075. (transpose-regions a b c d)
  18076. (goto-char c)))
  18077. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18078. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18079. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18080. (t (org-drag-element-backward))))
  18081. (defun org-metadown (&optional arg)
  18082. "Move subtree down or move table row down.
  18083. Calls `org-move-subtree-down' or `org-table-move-row' or
  18084. `org-move-item-down', depending on context. See the individual
  18085. commands for more information."
  18086. (interactive "P")
  18087. (cond
  18088. ((run-hook-with-args-until-success 'org-metadown-hook))
  18089. ((org-region-active-p)
  18090. (let* ((a (min (region-beginning) (region-end)))
  18091. (b (max (region-beginning) (region-end)))
  18092. (c (save-excursion (goto-char b)
  18093. (move-beginning-of-line 1)))
  18094. (d (save-excursion (goto-char b)
  18095. (move-end-of-line 1) (1+ (point)))))
  18096. (transpose-regions a b c d)
  18097. (goto-char d)))
  18098. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18099. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18100. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18101. (t (org-drag-element-forward))))
  18102. (defun org-shiftup (&optional arg)
  18103. "Increase item in timestamp or increase priority of current headline.
  18104. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18105. depending on context. See the individual commands for more information."
  18106. (interactive "P")
  18107. (cond
  18108. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18109. ((and org-support-shift-select (org-region-active-p))
  18110. (org-call-for-shift-select 'previous-line))
  18111. ((org-at-timestamp-p t)
  18112. (call-interactively (if org-edit-timestamp-down-means-later
  18113. 'org-timestamp-down 'org-timestamp-up)))
  18114. ((and (not (eq org-support-shift-select 'always))
  18115. org-enable-priority-commands
  18116. (org-at-heading-p))
  18117. (call-interactively 'org-priority-up))
  18118. ((and (not org-support-shift-select) (org-at-item-p))
  18119. (call-interactively 'org-previous-item))
  18120. ((org-clocktable-try-shift 'up arg))
  18121. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18122. (org-support-shift-select
  18123. (org-call-for-shift-select 'previous-line))
  18124. (t (org-shiftselect-error))))
  18125. (defun org-shiftdown (&optional arg)
  18126. "Decrease item in timestamp or decrease priority of current headline.
  18127. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18128. depending on context. See the individual commands for more information."
  18129. (interactive "P")
  18130. (cond
  18131. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18132. ((and org-support-shift-select (org-region-active-p))
  18133. (org-call-for-shift-select 'next-line))
  18134. ((org-at-timestamp-p t)
  18135. (call-interactively (if org-edit-timestamp-down-means-later
  18136. 'org-timestamp-up 'org-timestamp-down)))
  18137. ((and (not (eq org-support-shift-select 'always))
  18138. org-enable-priority-commands
  18139. (org-at-heading-p))
  18140. (call-interactively 'org-priority-down))
  18141. ((and (not org-support-shift-select) (org-at-item-p))
  18142. (call-interactively 'org-next-item))
  18143. ((org-clocktable-try-shift 'down arg))
  18144. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18145. (org-support-shift-select
  18146. (org-call-for-shift-select 'next-line))
  18147. (t (org-shiftselect-error))))
  18148. (defun org-shiftright (&optional arg)
  18149. "Cycle the thing at point or in the current line, depending on context.
  18150. Depending on context, this does one of the following:
  18151. - switch a timestamp at point one day into the future
  18152. - on a headline, switch to the next TODO keyword.
  18153. - on an item, switch entire list to the next bullet type
  18154. - on a property line, switch to the next allowed value
  18155. - on a clocktable definition line, move time block into the future"
  18156. (interactive "P")
  18157. (cond
  18158. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18159. ((and org-support-shift-select (org-region-active-p))
  18160. (org-call-for-shift-select 'forward-char))
  18161. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18162. ((and (not (eq org-support-shift-select 'always))
  18163. (org-at-heading-p))
  18164. (let ((org-inhibit-logging
  18165. (not org-treat-S-cursor-todo-selection-as-state-change))
  18166. (org-inhibit-blocking
  18167. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18168. (org-call-with-arg 'org-todo 'right)))
  18169. ((or (and org-support-shift-select
  18170. (not (eq org-support-shift-select 'always))
  18171. (org-at-item-bullet-p))
  18172. (and (not org-support-shift-select) (org-at-item-p)))
  18173. (org-call-with-arg 'org-cycle-list-bullet nil))
  18174. ((and (not (eq org-support-shift-select 'always))
  18175. (org-at-property-p))
  18176. (call-interactively 'org-property-next-allowed-value))
  18177. ((org-clocktable-try-shift 'right arg))
  18178. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18179. (org-support-shift-select
  18180. (org-call-for-shift-select 'forward-char))
  18181. (t (org-shiftselect-error))))
  18182. (defun org-shiftleft (&optional arg)
  18183. "Cycle the thing at point or in the current line, depending on context.
  18184. Depending on context, this does one of the following:
  18185. - switch a timestamp at point one day into the past
  18186. - on a headline, switch to the previous TODO keyword.
  18187. - on an item, switch entire list to the previous bullet type
  18188. - on a property line, switch to the previous allowed value
  18189. - on a clocktable definition line, move time block into the past"
  18190. (interactive "P")
  18191. (cond
  18192. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18193. ((and org-support-shift-select (org-region-active-p))
  18194. (org-call-for-shift-select 'backward-char))
  18195. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18196. ((and (not (eq org-support-shift-select 'always))
  18197. (org-at-heading-p))
  18198. (let ((org-inhibit-logging
  18199. (not org-treat-S-cursor-todo-selection-as-state-change))
  18200. (org-inhibit-blocking
  18201. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18202. (org-call-with-arg 'org-todo 'left)))
  18203. ((or (and org-support-shift-select
  18204. (not (eq org-support-shift-select 'always))
  18205. (org-at-item-bullet-p))
  18206. (and (not org-support-shift-select) (org-at-item-p)))
  18207. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18208. ((and (not (eq org-support-shift-select 'always))
  18209. (org-at-property-p))
  18210. (call-interactively 'org-property-previous-allowed-value))
  18211. ((org-clocktable-try-shift 'left arg))
  18212. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18213. (org-support-shift-select
  18214. (org-call-for-shift-select 'backward-char))
  18215. (t (org-shiftselect-error))))
  18216. (defun org-shiftcontrolright ()
  18217. "Switch to next TODO set."
  18218. (interactive)
  18219. (cond
  18220. ((and org-support-shift-select (org-region-active-p))
  18221. (org-call-for-shift-select 'forward-word))
  18222. ((and (not (eq org-support-shift-select 'always))
  18223. (org-at-heading-p))
  18224. (org-call-with-arg 'org-todo 'nextset))
  18225. (org-support-shift-select
  18226. (org-call-for-shift-select 'forward-word))
  18227. (t (org-shiftselect-error))))
  18228. (defun org-shiftcontrolleft ()
  18229. "Switch to previous TODO set."
  18230. (interactive)
  18231. (cond
  18232. ((and org-support-shift-select (org-region-active-p))
  18233. (org-call-for-shift-select 'backward-word))
  18234. ((and (not (eq org-support-shift-select 'always))
  18235. (org-at-heading-p))
  18236. (org-call-with-arg 'org-todo 'previousset))
  18237. (org-support-shift-select
  18238. (org-call-for-shift-select 'backward-word))
  18239. (t (org-shiftselect-error))))
  18240. (defun org-shiftcontrolup (&optional n)
  18241. "Change timestamps synchronously up in CLOCK log lines.
  18242. Optional argument N tells to change by that many units."
  18243. (interactive "P")
  18244. (cond ((and (not org-support-shift-select)
  18245. (org-at-clock-log-p)
  18246. (org-at-timestamp-p t))
  18247. (org-clock-timestamps-up n))
  18248. (t (org-shiftselect-error))))
  18249. (defun org-shiftcontroldown (&optional n)
  18250. "Change timestamps synchronously down in CLOCK log lines.
  18251. Optional argument N tells to change by that many units."
  18252. (interactive "P")
  18253. (cond ((and (not org-support-shift-select)
  18254. (org-at-clock-log-p)
  18255. (org-at-timestamp-p t))
  18256. (org-clock-timestamps-down n))
  18257. (t (org-shiftselect-error))))
  18258. (defun org-ctrl-c-ret ()
  18259. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18260. (interactive)
  18261. (cond
  18262. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18263. (t (call-interactively 'org-insert-heading))))
  18264. (defun org-find-visible ()
  18265. (let ((s (point)))
  18266. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18267. (get-char-property s 'invisible)))
  18268. s))
  18269. (defun org-find-invisible ()
  18270. (let ((s (point)))
  18271. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18272. (not (get-char-property s 'invisible))))
  18273. s))
  18274. (defun org-copy-visible (beg end)
  18275. "Copy the visible parts of the region."
  18276. (interactive "r")
  18277. (let (snippets s)
  18278. (save-excursion
  18279. (save-restriction
  18280. (narrow-to-region beg end)
  18281. (setq s (goto-char (point-min)))
  18282. (while (not (= (point) (point-max)))
  18283. (goto-char (org-find-invisible))
  18284. (push (buffer-substring s (point)) snippets)
  18285. (setq s (goto-char (org-find-visible))))))
  18286. (kill-new (apply 'concat (nreverse snippets)))))
  18287. (defun org-copy-special ()
  18288. "Copy region in table or copy current subtree.
  18289. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18290. See the individual commands for more information."
  18291. (interactive)
  18292. (call-interactively
  18293. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18294. (defun org-cut-special ()
  18295. "Cut region in table or cut current subtree.
  18296. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18297. See the individual commands for more information."
  18298. (interactive)
  18299. (call-interactively
  18300. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18301. (defun org-paste-special (arg)
  18302. "Paste rectangular region into table, or past subtree relative to level.
  18303. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18304. See the individual commands for more information."
  18305. (interactive "P")
  18306. (if (org-at-table-p)
  18307. (org-table-paste-rectangle)
  18308. (org-paste-subtree arg)))
  18309. (defsubst org-in-fixed-width-region-p ()
  18310. "Is point in a fixed-width region?"
  18311. (save-match-data
  18312. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18313. (defun org-edit-special (&optional arg)
  18314. "Call a special editor for the element at point.
  18315. When at a table, call the formula editor with `org-table-edit-formulas'.
  18316. When in a source code block, call `org-edit-src-code'.
  18317. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18318. When at an #+INCLUDE keyword, visit the included file.
  18319. On a link, call `ffap' to visit the link at point.
  18320. Otherwise, return a user error."
  18321. (interactive "P")
  18322. (let ((element (org-element-at-point)))
  18323. (assert (not buffer-read-only) nil
  18324. "Buffer is read-only: %s" (buffer-name))
  18325. (case (org-element-type element)
  18326. (src-block
  18327. (if (not arg) (org-edit-src-code)
  18328. (let* ((info (org-babel-get-src-block-info))
  18329. (lang (nth 0 info))
  18330. (params (nth 2 info))
  18331. (session (cdr (assq :session params))))
  18332. (if (not session) (org-edit-src-code)
  18333. ;; At a src-block with a session and function called with
  18334. ;; an ARG: switch to the buffer related to the inferior
  18335. ;; process.
  18336. (switch-to-buffer
  18337. (funcall (intern (concat "org-babel-prep-session:" lang))
  18338. session params))))))
  18339. (keyword
  18340. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18341. (find-file
  18342. (org-remove-double-quotes
  18343. (car (org-split-string (org-element-property :value element)))))
  18344. (user-error "No special environment to edit here")))
  18345. (table
  18346. (if (eq (org-element-property :type element) 'table.el)
  18347. (org-edit-src-code)
  18348. (call-interactively 'org-table-edit-formulas)))
  18349. ;; Only Org tables contain `table-row' type elements.
  18350. (table-row (call-interactively 'org-table-edit-formulas))
  18351. ((example-block export-block) (org-edit-src-code))
  18352. (fixed-width (org-edit-fixed-width-region))
  18353. (otherwise
  18354. ;; No notable element at point. Though, we may be at a link,
  18355. ;; which is an object. Thus, scan deeper.
  18356. (if (eq (org-element-type (org-element-context element)) 'link)
  18357. (call-interactively 'ffap)
  18358. (user-error "No special environment to edit here"))))))
  18359. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18360. (defun org-ctrl-c-ctrl-c (&optional arg)
  18361. "Set tags in headline, or update according to changed information at point.
  18362. This command does many different things, depending on context:
  18363. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18364. this is what we do.
  18365. - If the cursor is on a statistics cookie, update it.
  18366. - If the cursor is in a headline, prompt for tags and insert them
  18367. into the current line, aligned to `org-tags-column'. When called
  18368. with prefix arg, realign all tags in the current buffer.
  18369. - If the cursor is in one of the special #+KEYWORD lines, this
  18370. triggers scanning the buffer for these lines and updating the
  18371. information.
  18372. - If the cursor is inside a table, realign the table. This command
  18373. works even if the automatic table editor has been turned off.
  18374. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18375. the entire table.
  18376. - If the cursor is at a footnote reference or definition, jump to
  18377. the corresponding definition or references, respectively.
  18378. - If the cursor is a the beginning of a dynamic block, update it.
  18379. - If the current buffer is a capture buffer, close note and file it.
  18380. - If the cursor is on a <<<target>>>, update radio targets and
  18381. corresponding links in this buffer.
  18382. - If the cursor is on a numbered item in a plain list, renumber the
  18383. ordered list.
  18384. - If the cursor is on a checkbox, toggle it.
  18385. - If the cursor is on a code block, evaluate it. The variable
  18386. `org-confirm-babel-evaluate' can be used to control prompting
  18387. before code block evaluation, by default every code block
  18388. evaluation requires confirmation. Code block evaluation can be
  18389. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18390. (interactive "P")
  18391. (cond
  18392. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18393. org-occur-highlights
  18394. org-latex-fragment-image-overlays)
  18395. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18396. (org-remove-occur-highlights)
  18397. (org-remove-latex-fragment-image-overlays)
  18398. (message "Temporary highlights/overlays removed from current buffer"))
  18399. ((and (local-variable-p 'org-finish-function (current-buffer))
  18400. (fboundp org-finish-function))
  18401. (funcall org-finish-function))
  18402. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18403. (t
  18404. (let* ((context (org-element-context)) (type (org-element-type context)))
  18405. ;; Test if point is within a blank line.
  18406. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18407. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18408. (user-error "C-c C-c can do nothing useful at this location"))
  18409. ;; For convenience: at the first line of a paragraph on the
  18410. ;; same line as an item, apply function on that item instead.
  18411. (when (eq type 'paragraph)
  18412. (let ((parent (org-element-property :parent context)))
  18413. (when (and (eq (org-element-type parent) 'item)
  18414. (= (point-at-bol) (org-element-property :begin parent)))
  18415. (setq context parent type 'item))))
  18416. ;; Act according to type of element or object at point.
  18417. (case type
  18418. (clock (org-clock-update-time-maybe))
  18419. (dynamic-block
  18420. (save-excursion
  18421. (goto-char (org-element-property :post-affiliated context))
  18422. (org-update-dblock)))
  18423. (footnote-definition
  18424. (goto-char (org-element-property :post-affiliated context))
  18425. (call-interactively 'org-footnote-action))
  18426. (footnote-reference (call-interactively 'org-footnote-action))
  18427. ((headline inlinetask)
  18428. (save-excursion (goto-char (org-element-property :begin context))
  18429. (call-interactively 'org-set-tags)))
  18430. (item
  18431. ;; At an item: a double C-u set checkbox to "[-]"
  18432. ;; unconditionally, whereas a single one will toggle its
  18433. ;; presence. Without an universal argument, if the item
  18434. ;; has a checkbox, toggle it. Otherwise repair the list.
  18435. (let* ((box (org-element-property :checkbox context))
  18436. (struct (org-element-property :structure context))
  18437. (old-struct (copy-tree struct))
  18438. (parents (org-list-parents-alist struct))
  18439. (prevs (org-list-prevs-alist struct))
  18440. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18441. (org-list-set-checkbox
  18442. (org-element-property :begin context) struct
  18443. (cond ((equal arg '(16)) "[-]")
  18444. ((and (not box) (equal arg '(4))) "[ ]")
  18445. ((or (not box) (equal arg '(4))) nil)
  18446. ((eq box 'on) "[ ]")
  18447. (t "[X]")))
  18448. ;; Mimic `org-list-write-struct' but with grabbing
  18449. ;; a return value from `org-list-struct-fix-box'.
  18450. (org-list-struct-fix-ind struct parents 2)
  18451. (org-list-struct-fix-item-end struct)
  18452. (org-list-struct-fix-bul struct prevs)
  18453. (org-list-struct-fix-ind struct parents)
  18454. (let ((block-item
  18455. (org-list-struct-fix-box struct parents prevs orderedp)))
  18456. (if (and box (equal struct old-struct))
  18457. (if (equal arg '(16))
  18458. (message "Checkboxes already reset")
  18459. (user-error "Cannot toggle this checkbox: %s"
  18460. (if (eq box 'on)
  18461. "all subitems checked"
  18462. "unchecked subitems")))
  18463. (org-list-struct-apply-struct struct old-struct)
  18464. (org-update-checkbox-count-maybe))
  18465. (when block-item
  18466. (message "Checkboxes were removed due to empty box at line %d"
  18467. (org-current-line block-item))))))
  18468. (keyword
  18469. (let ((org-inhibit-startup-visibility-stuff t)
  18470. (org-startup-align-all-tables nil))
  18471. (when (boundp 'org-table-coordinate-overlays)
  18472. (mapc 'delete-overlay org-table-coordinate-overlays)
  18473. (setq org-table-coordinate-overlays nil))
  18474. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18475. (message "Local setup has been refreshed"))
  18476. (plain-list
  18477. ;; At a plain list, with a double C-u argument, set
  18478. ;; checkboxes of each item to "[-]", whereas a single one
  18479. ;; will toggle their presence according to the state of the
  18480. ;; first item in the list. Without an argument, repair the
  18481. ;; list.
  18482. (let* ((begin (org-element-property :contents-begin context))
  18483. (struct (org-element-property :structure context))
  18484. (old-struct (copy-tree struct))
  18485. (first-box (save-excursion
  18486. (goto-char begin)
  18487. (looking-at org-list-full-item-re)
  18488. (match-string-no-properties 3)))
  18489. (new-box (cond ((equal arg '(16)) "[-]")
  18490. ((equal arg '(4)) (unless first-box "[ ]"))
  18491. ((equal first-box "[X]") "[ ]")
  18492. (t "[X]"))))
  18493. (cond
  18494. (arg
  18495. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18496. (org-list-get-all-items
  18497. begin struct (org-list-prevs-alist struct))))
  18498. ((and first-box (eq (point) begin))
  18499. ;; For convenience, when point is at bol on the first
  18500. ;; item of the list and no argument is provided, simply
  18501. ;; toggle checkbox of that item, if any.
  18502. (org-list-set-checkbox begin struct new-box)))
  18503. (org-list-write-struct
  18504. struct (org-list-parents-alist struct) old-struct)
  18505. (org-update-checkbox-count-maybe)
  18506. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18507. ((property-drawer node-property)
  18508. (call-interactively 'org-property-action))
  18509. ((radio-target target)
  18510. (call-interactively 'org-update-radio-target-regexp))
  18511. (statistics-cookie
  18512. (call-interactively 'org-update-statistics-cookies))
  18513. ((table table-cell table-row)
  18514. ;; At a table, recalculate every field and align it. Also
  18515. ;; send the table if necessary. If the table has
  18516. ;; a `table.el' type, just give up. At a table row or
  18517. ;; cell, maybe recalculate line but always align table.
  18518. (if (eq (org-element-property :type context) 'table.el)
  18519. (message "Use C-c ' to edit table.el tables")
  18520. (let ((org-enable-table-editor t))
  18521. (if (or (eq type 'table)
  18522. ;; Check if point is at a TBLFM line.
  18523. (and (eq type 'table-row)
  18524. (= (point) (org-element-property :end context))))
  18525. (save-excursion
  18526. (goto-char (org-element-property :contents-begin context))
  18527. (org-call-with-arg 'org-table-recalculate (or arg t))
  18528. (orgtbl-send-table 'maybe))
  18529. (org-table-maybe-eval-formula)
  18530. (cond (arg (call-interactively 'org-table-recalculate))
  18531. ((org-table-maybe-recalculate-line))
  18532. (t (org-table-align)))))))
  18533. (timestamp (org-timestamp-change 0 'day))
  18534. (otherwise
  18535. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18536. (user-error
  18537. "C-c C-c can do nothing useful at this location")))))))))
  18538. (defun org-mode-restart ()
  18539. "Restart Org-mode, to scan again for special lines.
  18540. Also updates the keyword regular expressions."
  18541. (interactive)
  18542. (org-mode)
  18543. (message "Org-mode restarted"))
  18544. (defun org-kill-note-or-show-branches ()
  18545. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18546. (interactive)
  18547. (if (not org-finish-function)
  18548. (progn
  18549. (hide-subtree)
  18550. (call-interactively 'show-branches))
  18551. (let ((org-note-abort t))
  18552. (funcall org-finish-function))))
  18553. (defun org-return (&optional indent)
  18554. "Goto next table row or insert a newline.
  18555. Calls `org-table-next-row' or `newline', depending on context.
  18556. See the individual commands for more information."
  18557. (interactive)
  18558. (let (org-ts-what)
  18559. (cond
  18560. ((or (bobp) (org-in-src-block-p))
  18561. (if indent (newline-and-indent) (newline)))
  18562. ((org-at-table-p)
  18563. (org-table-justify-field-maybe)
  18564. (call-interactively 'org-table-next-row))
  18565. ;; when `newline-and-indent' is called within a list, make sure
  18566. ;; text moved stays inside the item.
  18567. ((and (org-in-item-p) indent)
  18568. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18569. (progn
  18570. (save-match-data (newline))
  18571. (org-indent-line-to (length (match-string 0))))
  18572. (let ((ind (org-get-indentation)))
  18573. (newline)
  18574. (if (org-looking-back org-list-end-re)
  18575. (org-indent-line)
  18576. (org-indent-line-to ind)))))
  18577. ((and org-return-follows-link
  18578. (org-at-timestamp-p t)
  18579. (not (eq org-ts-what 'after)))
  18580. (org-follow-timestamp-link))
  18581. ((and org-return-follows-link
  18582. (let ((tprop (get-text-property (point) 'face)))
  18583. (or (eq tprop 'org-link)
  18584. (and (listp tprop) (memq 'org-link tprop)))))
  18585. (call-interactively 'org-open-at-point))
  18586. ((and (org-at-heading-p)
  18587. (looking-at
  18588. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18589. (org-show-entry)
  18590. (end-of-line 1)
  18591. (newline))
  18592. (t (if indent (newline-and-indent) (newline))))))
  18593. (defun org-return-indent ()
  18594. "Goto next table row or insert a newline and indent.
  18595. Calls `org-table-next-row' or `newline-and-indent', depending on
  18596. context. See the individual commands for more information."
  18597. (interactive)
  18598. (org-return t))
  18599. (defun org-ctrl-c-star ()
  18600. "Compute table, or change heading status of lines.
  18601. Calls `org-table-recalculate' or `org-toggle-heading',
  18602. depending on context."
  18603. (interactive)
  18604. (cond
  18605. ((org-at-table-p)
  18606. (call-interactively 'org-table-recalculate))
  18607. (t
  18608. ;; Convert all lines in region to list items
  18609. (call-interactively 'org-toggle-heading))))
  18610. (defun org-ctrl-c-minus ()
  18611. "Insert separator line in table or modify bullet status of line.
  18612. Also turns a plain line or a region of lines into list items.
  18613. Calls `org-table-insert-hline', `org-toggle-item', or
  18614. `org-cycle-list-bullet', depending on context."
  18615. (interactive)
  18616. (cond
  18617. ((org-at-table-p)
  18618. (call-interactively 'org-table-insert-hline))
  18619. ((org-region-active-p)
  18620. (call-interactively 'org-toggle-item))
  18621. ((org-in-item-p)
  18622. (call-interactively 'org-cycle-list-bullet))
  18623. (t
  18624. (call-interactively 'org-toggle-item))))
  18625. (defun org-toggle-item (arg)
  18626. "Convert headings or normal lines to items, items to normal lines.
  18627. If there is no active region, only the current line is considered.
  18628. If the first non blank line in the region is a headline, convert
  18629. all headlines to items, shifting text accordingly.
  18630. If it is an item, convert all items to normal lines.
  18631. If it is normal text, change region into a list of items.
  18632. With a prefix argument ARG, change the region in a single item."
  18633. (interactive "P")
  18634. (let ((shift-text
  18635. (function
  18636. ;; Shift text in current section to IND, from point to END.
  18637. ;; The function leaves point to END line.
  18638. (lambda (ind end)
  18639. (let ((min-i 1000) (end (copy-marker end)))
  18640. ;; First determine the minimum indentation (MIN-I) of
  18641. ;; the text.
  18642. (save-excursion
  18643. (catch 'exit
  18644. (while (< (point) end)
  18645. (let ((i (org-get-indentation)))
  18646. (cond
  18647. ;; Skip blank lines and inline tasks.
  18648. ((looking-at "^[ \t]*$"))
  18649. ((looking-at org-outline-regexp-bol))
  18650. ;; We can't find less than 0 indentation.
  18651. ((zerop i) (throw 'exit (setq min-i 0)))
  18652. ((< i min-i) (setq min-i i))))
  18653. (forward-line))))
  18654. ;; Then indent each line so that a line indented to
  18655. ;; MIN-I becomes indented to IND. Ignore blank lines
  18656. ;; and inline tasks in the process.
  18657. (let ((delta (- ind min-i)))
  18658. (while (< (point) end)
  18659. (unless (or (looking-at "^[ \t]*$")
  18660. (looking-at org-outline-regexp-bol))
  18661. (org-indent-line-to (+ (org-get-indentation) delta)))
  18662. (forward-line)))))))
  18663. (skip-blanks
  18664. (function
  18665. ;; Return beginning of first non-blank line, starting from
  18666. ;; line at POS.
  18667. (lambda (pos)
  18668. (save-excursion
  18669. (goto-char pos)
  18670. (skip-chars-forward " \r\t\n")
  18671. (point-at-bol)))))
  18672. beg end)
  18673. ;; Determine boundaries of changes.
  18674. (if (org-region-active-p)
  18675. (setq beg (funcall skip-blanks (region-beginning))
  18676. end (copy-marker (region-end)))
  18677. (setq beg (funcall skip-blanks (point-at-bol))
  18678. end (copy-marker (point-at-eol))))
  18679. ;; Depending on the starting line, choose an action on the text
  18680. ;; between BEG and END.
  18681. (org-with-limited-levels
  18682. (save-excursion
  18683. (goto-char beg)
  18684. (cond
  18685. ;; Case 1. Start at an item: de-itemize. Note that it only
  18686. ;; happens when a region is active: `org-ctrl-c-minus'
  18687. ;; would call `org-cycle-list-bullet' otherwise.
  18688. ((org-at-item-p)
  18689. (while (< (point) end)
  18690. (when (org-at-item-p)
  18691. (skip-chars-forward " \t")
  18692. (delete-region (point) (match-end 0)))
  18693. (forward-line)))
  18694. ;; Case 2. Start at an heading: convert to items.
  18695. ((org-at-heading-p)
  18696. (let* ((bul (org-list-bullet-string "-"))
  18697. (bul-len (length bul))
  18698. ;; Indentation of the first heading. It should be
  18699. ;; relative to the indentation of its parent, if any.
  18700. (start-ind (save-excursion
  18701. (cond
  18702. ((not org-adapt-indentation) 0)
  18703. ((not (outline-previous-heading)) 0)
  18704. (t (length (match-string 0))))))
  18705. ;; Level of first heading. Further headings will be
  18706. ;; compared to it to determine hierarchy in the list.
  18707. (ref-level (org-reduced-level (org-outline-level))))
  18708. (while (< (point) end)
  18709. (let* ((level (org-reduced-level (org-outline-level)))
  18710. (delta (max 0 (- level ref-level))))
  18711. ;; If current headline is less indented than the first
  18712. ;; one, set it as reference, in order to preserve
  18713. ;; subtrees.
  18714. (when (< level ref-level) (setq ref-level level))
  18715. (replace-match bul t t)
  18716. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18717. ;; Ensure all text down to END (or SECTION-END) belongs
  18718. ;; to the newly created item.
  18719. (let ((section-end (save-excursion
  18720. (or (outline-next-heading) (point)))))
  18721. (forward-line)
  18722. (funcall shift-text
  18723. (+ start-ind (* (1+ delta) bul-len))
  18724. (min end section-end)))))))
  18725. ;; Case 3. Normal line with ARG: make the first line of region
  18726. ;; an item, and shift indentation of others lines to
  18727. ;; set them as item's body.
  18728. (arg (let* ((bul (org-list-bullet-string "-"))
  18729. (bul-len (length bul))
  18730. (ref-ind (org-get-indentation)))
  18731. (skip-chars-forward " \t")
  18732. (insert bul)
  18733. (forward-line)
  18734. (while (< (point) end)
  18735. ;; Ensure that lines less indented than first one
  18736. ;; still get included in item body.
  18737. (funcall shift-text
  18738. (+ ref-ind bul-len)
  18739. (min end (save-excursion (or (outline-next-heading)
  18740. (point)))))
  18741. (forward-line))))
  18742. ;; Case 4. Normal line without ARG: turn each non-item line
  18743. ;; into an item.
  18744. (t
  18745. (while (< (point) end)
  18746. (unless (or (org-at-heading-p) (org-at-item-p))
  18747. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18748. (replace-match
  18749. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18750. (forward-line))))))))
  18751. (defun org-toggle-heading (&optional nstars)
  18752. "Convert headings to normal text, or items or text to headings.
  18753. If there is no active region, only convert the current line.
  18754. With a \\[universal-argument] prefix, convert the whole list at
  18755. point into heading.
  18756. In a region:
  18757. - If the first non blank line is a headline, remove the stars
  18758. from all headlines in the region.
  18759. - If it is a normal line, turn each and every normal line (i.e.,
  18760. not an heading or an item) in the region into headings. If you
  18761. want to convert only the first line of this region, use one
  18762. universal prefix argument.
  18763. - If it is a plain list item, turn all plain list items into headings.
  18764. When converting a line into a heading, the number of stars is chosen
  18765. such that the lines become children of the current entry. However,
  18766. when a numeric prefix argument is given, its value determines the
  18767. number of stars to add."
  18768. (interactive "P")
  18769. (let ((skip-blanks
  18770. (function
  18771. ;; Return beginning of first non-blank line, starting from
  18772. ;; line at POS.
  18773. (lambda (pos)
  18774. (save-excursion
  18775. (goto-char pos)
  18776. (while (org-at-comment-p) (forward-line))
  18777. (skip-chars-forward " \r\t\n")
  18778. (point-at-bol)))))
  18779. beg end toggled)
  18780. ;; Determine boundaries of changes. If a universal prefix has
  18781. ;; been given, put the list in a region. If region ends at a bol,
  18782. ;; do not consider the last line to be in the region.
  18783. (when (and current-prefix-arg (org-at-item-p))
  18784. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18785. (org-mark-element))
  18786. (if (org-region-active-p)
  18787. (setq beg (funcall skip-blanks (region-beginning))
  18788. end (copy-marker (save-excursion
  18789. (goto-char (region-end))
  18790. (if (bolp) (point) (point-at-eol)))))
  18791. (setq beg (funcall skip-blanks (point-at-bol))
  18792. end (copy-marker (point-at-eol))))
  18793. ;; Ensure inline tasks don't count as headings.
  18794. (org-with-limited-levels
  18795. (save-excursion
  18796. (goto-char beg)
  18797. (cond
  18798. ;; Case 1. Started at an heading: de-star headings.
  18799. ((org-at-heading-p)
  18800. (while (< (point) end)
  18801. (when (org-at-heading-p t)
  18802. (looking-at org-outline-regexp) (replace-match "")
  18803. (setq toggled t))
  18804. (forward-line)))
  18805. ;; Case 2. Started at an item: change items into headlines.
  18806. ;; One star will be added by `org-list-to-subtree'.
  18807. ((org-at-item-p)
  18808. (let* ((stars (make-string
  18809. ;; subtract the star that will be added again by
  18810. ;; `org-list-to-subtree'
  18811. (if (numberp nstars) (1- nstars)
  18812. (or (org-current-level) 0))
  18813. ?*))
  18814. (add-stars
  18815. (cond (nstars "") ; stars from prefix only
  18816. ((equal stars "") "") ; before first heading
  18817. (org-odd-levels-only "*") ; inside heading, odd
  18818. (t "")))) ; inside heading, oddeven
  18819. (while (< (point) end)
  18820. (when (org-at-item-p)
  18821. ;; Pay attention to cases when region ends before list.
  18822. (let* ((struct (org-list-struct))
  18823. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18824. (save-restriction
  18825. (narrow-to-region (point) list-end)
  18826. (insert
  18827. (org-list-to-subtree
  18828. (org-list-parse-list t)
  18829. '(:istart (concat stars add-stars (funcall get-stars depth))
  18830. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18831. (setq toggled t))
  18832. (forward-line))))
  18833. ;; Case 3. Started at normal text: make every line an heading,
  18834. ;; skipping headlines and items.
  18835. (t (let* ((stars
  18836. (make-string
  18837. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18838. (add-stars
  18839. (cond (nstars "") ; stars from prefix only
  18840. ((equal stars "") "*") ; before first heading
  18841. (org-odd-levels-only "**") ; inside heading, odd
  18842. (t "*"))) ; inside heading, oddeven
  18843. (rpl (concat stars add-stars " "))
  18844. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18845. (while (< (point) (if (equal nstars '(4)) lend end))
  18846. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18847. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18848. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18849. (forward-line)))))))
  18850. (unless toggled (message "Cannot toggle heading from here"))))
  18851. (defun org-meta-return (&optional arg)
  18852. "Insert a new heading or wrap a region in a table.
  18853. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18854. See the individual commands for more information."
  18855. (interactive "P")
  18856. (cond
  18857. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18858. ((or (org-at-drawer-p) (org-at-property-p))
  18859. (newline-and-indent))
  18860. ((org-at-table-p)
  18861. (call-interactively 'org-table-wrap-region))
  18862. (t (call-interactively 'org-insert-heading))))
  18863. ;;; Menu entries
  18864. (defsubst org-in-subtree-not-table-p ()
  18865. "Are we in a subtree and not in a table?"
  18866. (and (not (org-before-first-heading-p))
  18867. (not (org-at-table-p))))
  18868. ;; Define the Org-mode menus
  18869. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18870. '("Tbl"
  18871. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18872. ["Next Field" org-cycle (org-at-table-p)]
  18873. ["Previous Field" org-shifttab (org-at-table-p)]
  18874. ["Next Row" org-return (org-at-table-p)]
  18875. "--"
  18876. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18877. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18878. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18879. "--"
  18880. ("Column"
  18881. ["Move Column Left" org-metaleft (org-at-table-p)]
  18882. ["Move Column Right" org-metaright (org-at-table-p)]
  18883. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18884. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18885. ("Row"
  18886. ["Move Row Up" org-metaup (org-at-table-p)]
  18887. ["Move Row Down" org-metadown (org-at-table-p)]
  18888. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18889. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18890. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18891. "--"
  18892. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18893. ("Rectangle"
  18894. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18895. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18896. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18897. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18898. "--"
  18899. ("Calculate"
  18900. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18901. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18902. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18903. "--"
  18904. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18905. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18906. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18907. "--"
  18908. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18909. "--"
  18910. ["Sum Column/Rectangle" org-table-sum
  18911. (or (org-at-table-p) (org-region-active-p))]
  18912. ["Which Column?" org-table-current-column (org-at-table-p)])
  18913. ["Debug Formulas"
  18914. org-table-toggle-formula-debugger
  18915. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18916. ["Show Col/Row Numbers"
  18917. org-table-toggle-coordinate-overlays
  18918. :style toggle
  18919. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18920. "--"
  18921. ["Create" org-table-create (and (not (org-at-table-p))
  18922. org-enable-table-editor)]
  18923. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18924. ["Import from File" org-table-import (not (org-at-table-p))]
  18925. ["Export to File" org-table-export (org-at-table-p)]
  18926. "--"
  18927. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18928. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18929. '("Org"
  18930. ("Show/Hide"
  18931. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18932. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18933. ["Sparse Tree..." org-sparse-tree t]
  18934. ["Reveal Context" org-reveal t]
  18935. ["Show All" show-all t]
  18936. "--"
  18937. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18938. "--"
  18939. ["New Heading" org-insert-heading t]
  18940. ("Navigate Headings"
  18941. ["Up" outline-up-heading t]
  18942. ["Next" outline-next-visible-heading t]
  18943. ["Previous" outline-previous-visible-heading t]
  18944. ["Next Same Level" outline-forward-same-level t]
  18945. ["Previous Same Level" outline-backward-same-level t]
  18946. "--"
  18947. ["Jump" org-goto t])
  18948. ("Edit Structure"
  18949. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18950. "--"
  18951. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18952. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18953. "--"
  18954. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18955. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18956. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18957. "--"
  18958. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18959. "--"
  18960. ["Copy visible text" org-copy-visible t]
  18961. "--"
  18962. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18963. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18964. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18965. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18966. "--"
  18967. ["Sort Region/Children" org-sort t]
  18968. "--"
  18969. ["Convert to odd levels" org-convert-to-odd-levels t]
  18970. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18971. ("Editing"
  18972. ["Emphasis..." org-emphasize t]
  18973. ["Edit Source Example" org-edit-special t]
  18974. "--"
  18975. ["Footnote new/jump" org-footnote-action t]
  18976. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18977. ("Archive"
  18978. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18979. "--"
  18980. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18981. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18982. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18983. )
  18984. "--"
  18985. ("Hyperlinks"
  18986. ["Store Link (Global)" org-store-link t]
  18987. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18988. ["Insert Link" org-insert-link t]
  18989. ["Follow Link" org-open-at-point t]
  18990. "--"
  18991. ["Next link" org-next-link t]
  18992. ["Previous link" org-previous-link t]
  18993. "--"
  18994. ["Descriptive Links"
  18995. org-toggle-link-display
  18996. :style radio
  18997. :selected org-descriptive-links
  18998. ]
  18999. ["Literal Links"
  19000. org-toggle-link-display
  19001. :style radio
  19002. :selected (not org-descriptive-links)])
  19003. "--"
  19004. ("TODO Lists"
  19005. ["TODO/DONE/-" org-todo t]
  19006. ("Select keyword"
  19007. ["Next keyword" org-shiftright (org-at-heading-p)]
  19008. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19009. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19010. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19011. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19012. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19013. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19014. "--"
  19015. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19016. :selected org-enforce-todo-dependencies :style toggle :active t]
  19017. "Settings for tree at point"
  19018. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19019. :selected (org-entry-get nil "ORDERED")
  19020. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19021. ["Do Children parallel" org-toggle-ordered-property :style radio
  19022. :selected (not (org-entry-get nil "ORDERED"))
  19023. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19024. "--"
  19025. ["Set Priority" org-priority t]
  19026. ["Priority Up" org-shiftup t]
  19027. ["Priority Down" org-shiftdown t]
  19028. "--"
  19029. ["Get news from all feeds" org-feed-update-all t]
  19030. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19031. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19032. ("TAGS and Properties"
  19033. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19034. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19035. "--"
  19036. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19037. ["Column view of properties" org-columns t]
  19038. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19039. ("Dates and Scheduling"
  19040. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19041. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19042. ("Change Date"
  19043. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19044. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19045. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19046. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19047. ["Compute Time Range" org-evaluate-time-range t]
  19048. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19049. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19050. "--"
  19051. ["Custom time format" org-toggle-time-stamp-overlays
  19052. :style radio :selected org-display-custom-times]
  19053. "--"
  19054. ["Goto Calendar" org-goto-calendar t]
  19055. ["Date from Calendar" org-date-from-calendar t]
  19056. "--"
  19057. ["Start/Restart Timer" org-timer-start t]
  19058. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19059. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19060. ["Insert Timer String" org-timer t]
  19061. ["Insert Timer Item" org-timer-item t])
  19062. ("Logging work"
  19063. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19064. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19065. ["Clock out" org-clock-out t]
  19066. ["Clock cancel" org-clock-cancel t]
  19067. "--"
  19068. ["Mark as default task" org-clock-mark-default-task t]
  19069. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19070. ["Goto running clock" org-clock-goto t]
  19071. "--"
  19072. ["Display times" org-clock-display t]
  19073. ["Create clock table" org-clock-report t]
  19074. "--"
  19075. ["Record DONE time"
  19076. (progn (setq org-log-done (not org-log-done))
  19077. (message "Switching to %s will %s record a timestamp"
  19078. (car org-done-keywords)
  19079. (if org-log-done "automatically" "not")))
  19080. :style toggle :selected org-log-done])
  19081. "--"
  19082. ["Agenda Command..." org-agenda t]
  19083. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19084. ("File List for Agenda")
  19085. ("Special views current file"
  19086. ["TODO Tree" org-show-todo-tree t]
  19087. ["Check Deadlines" org-check-deadlines t]
  19088. ["Timeline" org-timeline t]
  19089. ["Tags/Property tree" org-match-sparse-tree t])
  19090. "--"
  19091. ["Export/Publish..." org-export-dispatch t]
  19092. ("LaTeX"
  19093. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19094. :selected org-cdlatex-mode]
  19095. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19096. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19097. ["Modify math symbol" org-cdlatex-math-modify
  19098. (org-inside-LaTeX-fragment-p)]
  19099. ["Insert citation" org-reftex-citation t]
  19100. "--"
  19101. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19102. "--"
  19103. ("MobileOrg"
  19104. ["Push Files and Views" org-mobile-push t]
  19105. ["Get Captured and Flagged" org-mobile-pull t]
  19106. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19107. "--"
  19108. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19109. "--"
  19110. ("Documentation"
  19111. ["Show Version" org-version t]
  19112. ["Info Documentation" org-info t])
  19113. ("Customize"
  19114. ["Browse Org Group" org-customize t]
  19115. "--"
  19116. ["Expand This Menu" org-create-customize-menu
  19117. (fboundp 'customize-menu-create)])
  19118. ["Send bug report" org-submit-bug-report t]
  19119. "--"
  19120. ("Refresh/Reload"
  19121. ["Refresh setup current buffer" org-mode-restart t]
  19122. ["Reload Org (after update)" org-reload t]
  19123. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19124. ))
  19125. (defun org-info (&optional node)
  19126. "Read documentation for Org-mode in the info system.
  19127. With optional NODE, go directly to that node."
  19128. (interactive)
  19129. (info (format "(org)%s" (or node ""))))
  19130. ;;;###autoload
  19131. (defun org-submit-bug-report ()
  19132. "Submit a bug report on Org-mode via mail.
  19133. Don't hesitate to report any problems or inaccurate documentation.
  19134. If you don't have setup sending mail from (X)Emacs, please copy the
  19135. output buffer into your mail program, as it gives us important
  19136. information about your Org-mode version and configuration."
  19137. (interactive)
  19138. (require 'reporter)
  19139. (org-load-modules-maybe)
  19140. (org-require-autoloaded-modules)
  19141. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19142. (reporter-submit-bug-report
  19143. "emacs-orgmode@gnu.org"
  19144. (org-version nil 'full)
  19145. (let (list)
  19146. (save-window-excursion
  19147. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19148. (delete-other-windows)
  19149. (erase-buffer)
  19150. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19151. We would like to add your full Org-mode and Outline configuration to the
  19152. bug report. This greatly simplifies the work of the maintainer and
  19153. other experts on the mailing list.
  19154. HOWEVER, some variables you have customized may contain private
  19155. information. The names of customers, colleagues, or friends, might
  19156. appear in the form of file names, tags, todo states, or search strings.
  19157. If you answer yes to the prompt, you might want to check and remove
  19158. such private information before sending the email.")
  19159. (add-text-properties (point-min) (point-max) '(face org-warning))
  19160. (when (yes-or-no-p "Include your Org-mode configuration ")
  19161. (mapatoms
  19162. (lambda (v)
  19163. (and (boundp v)
  19164. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19165. (or (and (symbol-value v)
  19166. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19167. (and
  19168. (get v 'custom-type) (get v 'standard-value)
  19169. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19170. (push v list)))))
  19171. (kill-buffer (get-buffer "*Warn about privacy*"))
  19172. list))
  19173. nil nil
  19174. "Remember to cover the basics, that is, what you expected to happen and
  19175. what in fact did happen. You don't know how to make a good report? See
  19176. http://orgmode.org/manual/Feedback.html#Feedback
  19177. Your bug report will be posted to the Org-mode mailing list.
  19178. ------------------------------------------------------------------------")
  19179. (save-excursion
  19180. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19181. (replace-match "\\1Bug: \\3 [\\2]")))))
  19182. (defun org-install-agenda-files-menu ()
  19183. (let ((bl (buffer-list)))
  19184. (save-excursion
  19185. (while bl
  19186. (set-buffer (pop bl))
  19187. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19188. (when (derived-mode-p 'org-mode)
  19189. (easy-menu-change
  19190. '("Org") "File List for Agenda"
  19191. (append
  19192. (list
  19193. ["Edit File List" (org-edit-agenda-file-list) t]
  19194. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19195. ["Remove Current File from List" org-remove-file t]
  19196. ["Cycle through agenda files" org-cycle-agenda-files t]
  19197. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19198. "--")
  19199. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19200. ;;;; Documentation
  19201. (defun org-require-autoloaded-modules ()
  19202. (interactive)
  19203. (mapc 'require
  19204. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19205. org-remember org-table org-timer)))
  19206. ;;;###autoload
  19207. (defun org-reload (&optional uncompiled)
  19208. "Reload all org lisp files.
  19209. With prefix arg UNCOMPILED, load the uncompiled versions."
  19210. (interactive "P")
  19211. (require 'loadhist)
  19212. (let* ((org-dir (org-find-library-dir "org"))
  19213. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19214. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19215. (remove-re (mapconcat 'identity
  19216. (mapcar (lambda (f) (concat "^" f "$"))
  19217. (list (if (featurep 'xemacs)
  19218. "org-colview"
  19219. "org-colview-xemacs")
  19220. "org" "org-loaddefs" "org-version"))
  19221. "\\|"))
  19222. (feats (delete-dups
  19223. (mapcar 'file-name-sans-extension
  19224. (mapcar 'file-name-nondirectory
  19225. (delq nil
  19226. (mapcar 'feature-file
  19227. features))))))
  19228. (lfeat (append
  19229. (sort
  19230. (setq feats
  19231. (delq nil (mapcar
  19232. (lambda (f)
  19233. (if (and (string-match feature-re f)
  19234. (not (string-match remove-re f)))
  19235. f nil))
  19236. feats)))
  19237. 'string-lessp)
  19238. (list "org-version" "org")))
  19239. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19240. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19241. load-uncore load-misses)
  19242. (setq load-misses
  19243. (delq 't
  19244. (mapcar (lambda (f)
  19245. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19246. (and (string= org-dir contrib-dir)
  19247. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19248. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19249. (add-to-list 'load-uncore f 'append)
  19250. 't)
  19251. f))
  19252. lfeat)))
  19253. (if load-uncore
  19254. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19255. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19256. (if load-misses
  19257. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19258. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19259. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19260. ;;;###autoload
  19261. (defun org-customize ()
  19262. "Call the customize function with org as argument."
  19263. (interactive)
  19264. (org-load-modules-maybe)
  19265. (org-require-autoloaded-modules)
  19266. (customize-browse 'org))
  19267. (defun org-create-customize-menu ()
  19268. "Create a full customization menu for Org-mode, insert it into the menu."
  19269. (interactive)
  19270. (org-load-modules-maybe)
  19271. (org-require-autoloaded-modules)
  19272. (if (fboundp 'customize-menu-create)
  19273. (progn
  19274. (easy-menu-change
  19275. '("Org") "Customize"
  19276. `(["Browse Org group" org-customize t]
  19277. "--"
  19278. ,(customize-menu-create 'org)
  19279. ["Set" Custom-set t]
  19280. ["Save" Custom-save t]
  19281. ["Reset to Current" Custom-reset-current t]
  19282. ["Reset to Saved" Custom-reset-saved t]
  19283. ["Reset to Standard Settings" Custom-reset-standard t]))
  19284. (message "\"Org\"-menu now contains full customization menu"))
  19285. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19286. ;;;; Miscellaneous stuff
  19287. ;;; Generally useful functions
  19288. (defun org-get-at-bol (property)
  19289. "Get text property PROPERTY at beginning of line."
  19290. (get-text-property (point-at-bol) property))
  19291. (defun org-find-text-property-in-string (prop s)
  19292. "Return the first non-nil value of property PROP in string S."
  19293. (or (get-text-property 0 prop s)
  19294. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19295. prop s)))
  19296. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19297. "Display the given MESSAGE as a warning."
  19298. (if (fboundp 'display-warning)
  19299. (display-warning 'org message
  19300. (if (featurep 'xemacs) 'warning :warning))
  19301. (let ((buf (get-buffer-create "*Org warnings*")))
  19302. (with-current-buffer buf
  19303. (goto-char (point-max))
  19304. (insert "Warning (Org): " message)
  19305. (unless (bolp)
  19306. (newline)))
  19307. (display-buffer buf)
  19308. (sit-for 0))))
  19309. (defun org-eval (form)
  19310. "Eval FORM and return result."
  19311. (condition-case error
  19312. (eval form)
  19313. (error (format "%%![Error: %s]" error))))
  19314. (defun org-in-clocktable-p ()
  19315. "Check if the cursor is in a clocktable."
  19316. (let ((pos (point)) start)
  19317. (save-excursion
  19318. (end-of-line 1)
  19319. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19320. (setq start (match-beginning 0))
  19321. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19322. (>= (match-end 0) pos)
  19323. start))))
  19324. (defun org-in-commented-line ()
  19325. "Is point in a line starting with `#'?"
  19326. (equal (char-after (point-at-bol)) ?#))
  19327. (defun org-in-indented-comment-line ()
  19328. "Is point in a line starting with `#' after some white space?"
  19329. (save-excursion
  19330. (save-match-data
  19331. (goto-char (point-at-bol))
  19332. (looking-at "[ \t]*#"))))
  19333. (defun org-in-verbatim-emphasis ()
  19334. (save-match-data
  19335. (and (org-in-regexp org-emph-re 2)
  19336. (>= (point) (match-beginning 3))
  19337. (<= (point) (match-end 4))
  19338. (member (match-string 3) '("=" "~")))))
  19339. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19340. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19341. (if (and marker (marker-buffer marker)
  19342. (buffer-live-p (marker-buffer marker)))
  19343. (progn
  19344. (org-pop-to-buffer-same-window (marker-buffer marker))
  19345. (if (or (> marker (point-max)) (< marker (point-min)))
  19346. (widen))
  19347. (goto-char marker)
  19348. (org-show-context 'org-goto))
  19349. (if bookmark
  19350. (bookmark-jump bookmark)
  19351. (error "Cannot find location"))))
  19352. (defun org-quote-csv-field (s)
  19353. "Quote field for inclusion in CSV material."
  19354. (if (string-match "[\",]" s)
  19355. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19356. s))
  19357. (defun org-force-self-insert (N)
  19358. "Needed to enforce self-insert under remapping."
  19359. (interactive "p")
  19360. (self-insert-command N))
  19361. (defun org-string-width (s)
  19362. "Compute width of string, ignoring invisible characters.
  19363. This ignores character with invisibility property `org-link', and also
  19364. characters with property `org-cwidth', because these will become invisible
  19365. upon the next fontification round."
  19366. (let (b l)
  19367. (when (or (eq t buffer-invisibility-spec)
  19368. (assq 'org-link buffer-invisibility-spec))
  19369. (while (setq b (text-property-any 0 (length s)
  19370. 'invisible 'org-link s))
  19371. (setq s (concat (substring s 0 b)
  19372. (substring s (or (next-single-property-change
  19373. b 'invisible s) (length s)))))))
  19374. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19375. (setq s (concat (substring s 0 b)
  19376. (substring s (or (next-single-property-change
  19377. b 'org-cwidth s) (length s))))))
  19378. (setq l (string-width s) b -1)
  19379. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19380. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19381. l))
  19382. (defun org-shorten-string (s maxlength)
  19383. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19384. If the string is shorter or has length MAXLENGTH, just return the
  19385. original string. If it is longer, the functions finds a space in the
  19386. string, breaks this string off at that locations and adds three dots
  19387. as ellipsis. Including the ellipsis, the string will not be longer
  19388. than MAXLENGTH. If finding a good breaking point in the string does
  19389. not work, the string is just chopped off in the middle of a word
  19390. if necessary."
  19391. (if (<= (length s) maxlength)
  19392. s
  19393. (let* ((n (max (- maxlength 4) 1))
  19394. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19395. (if (string-match re s)
  19396. (concat (match-string 1 s) "...")
  19397. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19398. (defun org-get-indentation (&optional line)
  19399. "Get the indentation of the current line, interpreting tabs.
  19400. When LINE is given, assume it represents a line and compute its indentation."
  19401. (if line
  19402. (if (string-match "^ *" (org-remove-tabs line))
  19403. (match-end 0))
  19404. (save-excursion
  19405. (beginning-of-line 1)
  19406. (skip-chars-forward " \t")
  19407. (current-column))))
  19408. (defun org-get-string-indentation (s)
  19409. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19410. (let ((n -1) (i 0) (w tab-width) c)
  19411. (catch 'exit
  19412. (while (< (setq n (1+ n)) (length s))
  19413. (setq c (aref s n))
  19414. (cond ((= c ?\ ) (setq i (1+ i)))
  19415. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19416. (t (throw 'exit t)))))
  19417. i))
  19418. (defun org-remove-tabs (s &optional width)
  19419. "Replace tabulators in S with spaces.
  19420. Assumes that s is a single line, starting in column 0."
  19421. (setq width (or width tab-width))
  19422. (while (string-match "\t" s)
  19423. (setq s (replace-match
  19424. (make-string
  19425. (- (* width (/ (+ (match-beginning 0) width) width))
  19426. (match-beginning 0)) ?\ )
  19427. t t s)))
  19428. s)
  19429. (defun org-fix-indentation (line ind)
  19430. "Fix indentation in LINE.
  19431. IND is a cons cell with target and minimum indentation.
  19432. If the current indentation in LINE is smaller than the minimum,
  19433. leave it alone. If it is larger than ind, set it to the target."
  19434. (let* ((l (org-remove-tabs line))
  19435. (i (org-get-indentation l))
  19436. (i1 (car ind)) (i2 (cdr ind)))
  19437. (if (>= i i2) (setq l (substring line i2)))
  19438. (if (> i1 0)
  19439. (concat (make-string i1 ?\ ) l)
  19440. l)))
  19441. (defun org-remove-indentation (code &optional n)
  19442. "Remove the maximum common indentation from the lines in CODE.
  19443. N may optionally be the number of spaces to remove."
  19444. (with-temp-buffer
  19445. (insert code)
  19446. (org-do-remove-indentation n)
  19447. (buffer-string)))
  19448. (defun org-do-remove-indentation (&optional n)
  19449. "Remove the maximum common indentation from the buffer."
  19450. (untabify (point-min) (point-max))
  19451. (let ((min 10000) re)
  19452. (if n
  19453. (setq min n)
  19454. (goto-char (point-min))
  19455. (while (re-search-forward "^ *[^ \n]" nil t)
  19456. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19457. (unless (or (= min 0) (= min 10000))
  19458. (setq re (format "^ \\{%d\\}" min))
  19459. (goto-char (point-min))
  19460. (while (re-search-forward re nil t)
  19461. (replace-match "")
  19462. (end-of-line 1))
  19463. min)))
  19464. (defun org-fill-template (template alist)
  19465. "Find each %key of ALIST in TEMPLATE and replace it."
  19466. (let ((case-fold-search nil)
  19467. entry key value)
  19468. (setq alist (sort (copy-sequence alist)
  19469. (lambda (a b) (< (length (car a)) (length (car b))))))
  19470. (while (setq entry (pop alist))
  19471. (setq template
  19472. (replace-regexp-in-string
  19473. (concat "%" (regexp-quote (car entry)))
  19474. (or (cdr entry) "") template t t)))
  19475. template))
  19476. (defun org-base-buffer (buffer)
  19477. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19478. (if (not buffer)
  19479. buffer
  19480. (or (buffer-base-buffer buffer)
  19481. buffer)))
  19482. (defun org-trim (s)
  19483. "Remove whitespace at beginning and end of string."
  19484. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19485. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19486. s)
  19487. (defun org-wrap (string &optional width lines)
  19488. "Wrap string to either a number of lines, or a width in characters.
  19489. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19490. that costs. If there is a word longer than WIDTH, the text is actually
  19491. wrapped to the length of that word.
  19492. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19493. many lines, whatever width that takes.
  19494. The return value is a list of lines, without newlines at the end."
  19495. (let* ((words (org-split-string string "[ \t\n]+"))
  19496. (maxword (apply 'max (mapcar 'org-string-width words)))
  19497. w ll)
  19498. (cond (width
  19499. (org-do-wrap words (max maxword width)))
  19500. (lines
  19501. (setq w maxword)
  19502. (setq ll (org-do-wrap words maxword))
  19503. (if (<= (length ll) lines)
  19504. ll
  19505. (setq ll words)
  19506. (while (> (length ll) lines)
  19507. (setq w (1+ w))
  19508. (setq ll (org-do-wrap words w)))
  19509. ll))
  19510. (t (error "Cannot wrap this")))))
  19511. (defun org-do-wrap (words width)
  19512. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19513. (let (lines line)
  19514. (while words
  19515. (setq line (pop words))
  19516. (while (and words (< (+ (length line) (length (car words))) width))
  19517. (setq line (concat line " " (pop words))))
  19518. (setq lines (push line lines)))
  19519. (nreverse lines)))
  19520. (defun org-split-string (string &optional separators)
  19521. "Splits STRING into substrings at SEPARATORS.
  19522. No empty strings are returned if there are matches at the beginning
  19523. and end of string."
  19524. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19525. (start 0)
  19526. notfirst
  19527. (list nil))
  19528. (while (and (string-match rexp string
  19529. (if (and notfirst
  19530. (= start (match-beginning 0))
  19531. (< start (length string)))
  19532. (1+ start) start))
  19533. (< (match-beginning 0) (length string)))
  19534. (setq notfirst t)
  19535. (or (eq (match-beginning 0) 0)
  19536. (and (eq (match-beginning 0) (match-end 0))
  19537. (eq (match-beginning 0) start))
  19538. (setq list
  19539. (cons (substring string start (match-beginning 0))
  19540. list)))
  19541. (setq start (match-end 0)))
  19542. (or (eq start (length string))
  19543. (setq list
  19544. (cons (substring string start)
  19545. list)))
  19546. (nreverse list)))
  19547. (defun org-quote-vert (s)
  19548. "Replace \"|\" with \"\\vert\"."
  19549. (while (string-match "|" s)
  19550. (setq s (replace-match "\\vert" t t s)))
  19551. s)
  19552. (defun org-uuidgen-p (s)
  19553. "Is S an ID created by UUIDGEN?"
  19554. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19555. (defun org-in-src-block-p (&optional inside)
  19556. "Whether point is in a code source block.
  19557. When INSIDE is non-nil, don't consider we are within a src block
  19558. when point is at #+BEGIN_SRC or #+END_SRC."
  19559. (let ((case-fold-search t) ov)
  19560. (or (and (setq ov (overlays-at (point)))
  19561. (memq 'org-block-background
  19562. (overlay-properties (car ov))))
  19563. (and (not inside)
  19564. (save-match-data
  19565. (save-excursion
  19566. (beginning-of-line)
  19567. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19568. (defun org-context ()
  19569. "Return a list of contexts of the current cursor position.
  19570. If several contexts apply, all are returned.
  19571. Each context entry is a list with a symbol naming the context, and
  19572. two positions indicating start and end of the context. Possible
  19573. contexts are:
  19574. :headline anywhere in a headline
  19575. :headline-stars on the leading stars in a headline
  19576. :todo-keyword on a TODO keyword (including DONE) in a headline
  19577. :tags on the TAGS in a headline
  19578. :priority on the priority cookie in a headline
  19579. :item on the first line of a plain list item
  19580. :item-bullet on the bullet/number of a plain list item
  19581. :checkbox on the checkbox in a plain list item
  19582. :table in an org-mode table
  19583. :table-special on a special filed in a table
  19584. :table-table in a table.el table
  19585. :clocktable in a clocktable
  19586. :src-block in a source block
  19587. :link on a hyperlink
  19588. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19589. :target on a <<target>>
  19590. :radio-target on a <<<radio-target>>>
  19591. :latex-fragment on a LaTeX fragment
  19592. :latex-preview on a LaTeX fragment with overlaid preview image
  19593. This function expects the position to be visible because it uses font-lock
  19594. faces as a help to recognize the following contexts: :table-special, :link,
  19595. and :keyword."
  19596. (let* ((f (get-text-property (point) 'face))
  19597. (faces (if (listp f) f (list f)))
  19598. (case-fold-search t)
  19599. (p (point)) clist o)
  19600. ;; First the large context
  19601. (cond
  19602. ((org-at-heading-p t)
  19603. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19604. (when (progn
  19605. (beginning-of-line 1)
  19606. (looking-at org-todo-line-tags-regexp))
  19607. (push (org-point-in-group p 1 :headline-stars) clist)
  19608. (push (org-point-in-group p 2 :todo-keyword) clist)
  19609. (push (org-point-in-group p 4 :tags) clist))
  19610. (goto-char p)
  19611. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19612. (if (looking-at "\\[#[A-Z0-9]\\]")
  19613. (push (org-point-in-group p 0 :priority) clist)))
  19614. ((org-at-item-p)
  19615. (push (org-point-in-group p 2 :item-bullet) clist)
  19616. (push (list :item (point-at-bol)
  19617. (save-excursion (org-end-of-item) (point)))
  19618. clist)
  19619. (and (org-at-item-checkbox-p)
  19620. (push (org-point-in-group p 0 :checkbox) clist)))
  19621. ((org-at-table-p)
  19622. (push (list :table (org-table-begin) (org-table-end)) clist)
  19623. (if (memq 'org-formula faces)
  19624. (push (list :table-special
  19625. (previous-single-property-change p 'face)
  19626. (next-single-property-change p 'face)) clist)))
  19627. ((org-at-table-p 'any)
  19628. (push (list :table-table) clist)))
  19629. (goto-char p)
  19630. (let ((case-fold-search t))
  19631. ;; New the "medium" contexts: clocktables, source blocks
  19632. (cond ((org-in-clocktable-p)
  19633. (push (list :clocktable
  19634. (and (or (looking-at "#\\+BEGIN: clocktable")
  19635. (search-backward "#+BEGIN: clocktable" nil t))
  19636. (match-beginning 0))
  19637. (and (re-search-forward "#\\+END:?" nil t)
  19638. (match-end 0))) clist))
  19639. ((org-in-src-block-p)
  19640. (push (list :src-block
  19641. (and (or (looking-at "#\\+BEGIN_SRC")
  19642. (search-backward "#+BEGIN_SRC" nil t))
  19643. (match-beginning 0))
  19644. (and (search-forward "#+END_SRC" nil t)
  19645. (match-beginning 0))) clist))))
  19646. (goto-char p)
  19647. ;; Now the small context
  19648. (cond
  19649. ((org-at-timestamp-p)
  19650. (push (org-point-in-group p 0 :timestamp) clist))
  19651. ((memq 'org-link faces)
  19652. (push (list :link
  19653. (previous-single-property-change p 'face)
  19654. (next-single-property-change p 'face)) clist))
  19655. ((memq 'org-special-keyword faces)
  19656. (push (list :keyword
  19657. (previous-single-property-change p 'face)
  19658. (next-single-property-change p 'face)) clist))
  19659. ((org-at-target-p)
  19660. (push (org-point-in-group p 0 :target) clist)
  19661. (goto-char (1- (match-beginning 0)))
  19662. (if (looking-at org-radio-target-regexp)
  19663. (push (org-point-in-group p 0 :radio-target) clist))
  19664. (goto-char p))
  19665. ((setq o (car (delq nil
  19666. (mapcar
  19667. (lambda (x)
  19668. (if (memq x org-latex-fragment-image-overlays) x))
  19669. (overlays-at (point))))))
  19670. (push (list :latex-fragment
  19671. (overlay-start o) (overlay-end o)) clist)
  19672. (push (list :latex-preview
  19673. (overlay-start o) (overlay-end o)) clist))
  19674. ((org-inside-LaTeX-fragment-p)
  19675. ;; FIXME: positions wrong.
  19676. (push (list :latex-fragment (point) (point)) clist)))
  19677. (setq clist (nreverse (delq nil clist)))
  19678. clist))
  19679. ;; FIXME: Compare with at-regexp-p Do we need both?
  19680. (defun org-in-regexp (re &optional nlines visually)
  19681. "Check if point is inside a match of regexp.
  19682. Normally only the current line is checked, but you can include NLINES extra
  19683. lines both before and after point into the search.
  19684. If VISUALLY is set, require that the cursor is not after the match but
  19685. really on, so that the block visually is on the match."
  19686. (catch 'exit
  19687. (let ((pos (point))
  19688. (eol (point-at-eol (+ 1 (or nlines 0))))
  19689. (inc (if visually 1 0)))
  19690. (save-excursion
  19691. (beginning-of-line (- 1 (or nlines 0)))
  19692. (while (re-search-forward re eol t)
  19693. (if (and (<= (match-beginning 0) pos)
  19694. (>= (+ inc (match-end 0)) pos))
  19695. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19696. (defun org-at-regexp-p (regexp)
  19697. "Is point inside a match of REGEXP in the current line?"
  19698. (catch 'exit
  19699. (save-excursion
  19700. (let ((pos (point)) (end (point-at-eol)))
  19701. (beginning-of-line 1)
  19702. (while (re-search-forward regexp end t)
  19703. (if (and (<= (match-beginning 0) pos)
  19704. (>= (match-end 0) pos))
  19705. (throw 'exit t)))
  19706. nil))))
  19707. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19708. "Non-nil when point is between matches of START-RE and END-RE.
  19709. Also return a non-nil value when point is on one of the matches.
  19710. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19711. buffer positions. Default values are the positions of headlines
  19712. surrounding the point.
  19713. The functions returns a cons cell whose car (resp. cdr) is the
  19714. position before START-RE (resp. after END-RE)."
  19715. (save-match-data
  19716. (let ((pos (point))
  19717. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19718. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19719. beg end)
  19720. (save-excursion
  19721. ;; Point is on a block when on START-RE or if START-RE can be
  19722. ;; found before it...
  19723. (and (or (org-at-regexp-p start-re)
  19724. (re-search-backward start-re limit-up t))
  19725. (setq beg (match-beginning 0))
  19726. ;; ... and END-RE after it...
  19727. (goto-char (match-end 0))
  19728. (re-search-forward end-re limit-down t)
  19729. (> (setq end (match-end 0)) pos)
  19730. ;; ... without another START-RE in-between.
  19731. (goto-char (match-beginning 0))
  19732. (not (re-search-backward start-re (1+ beg) t))
  19733. ;; Return value.
  19734. (cons beg end))))))
  19735. (defun org-in-block-p (names)
  19736. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19737. NAMES is a list of strings containing names of blocks.
  19738. Return first block name matched, or nil. Beware that in case of
  19739. nested blocks, the returned name may not belong to the closest
  19740. block from point."
  19741. (save-match-data
  19742. (catch 'exit
  19743. (let ((case-fold-search t)
  19744. (lim-up (save-excursion (outline-previous-heading)))
  19745. (lim-down (save-excursion (outline-next-heading))))
  19746. (mapc (lambda (name)
  19747. (let ((n (regexp-quote name)))
  19748. (when (org-between-regexps-p
  19749. (concat "^[ \t]*#\\+begin_" n)
  19750. (concat "^[ \t]*#\\+end_" n)
  19751. lim-up lim-down)
  19752. (throw 'exit n))))
  19753. names))
  19754. nil)))
  19755. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19756. "Call `multi-occur' with buffers for all agenda files."
  19757. (interactive "sOrg-files matching: \np")
  19758. (let* ((files (org-agenda-files))
  19759. (tnames (mapcar 'file-truename files))
  19760. (extra org-agenda-text-search-extra-files)
  19761. f)
  19762. (when (eq (car extra) 'agenda-archives)
  19763. (setq extra (cdr extra))
  19764. (setq files (org-add-archive-files files)))
  19765. (while (setq f (pop extra))
  19766. (unless (member (file-truename f) tnames)
  19767. (add-to-list 'files f 'append)
  19768. (add-to-list 'tnames (file-truename f) 'append)))
  19769. (multi-occur
  19770. (mapcar (lambda (x)
  19771. (with-current-buffer
  19772. (or (get-file-buffer x) (find-file-noselect x))
  19773. (widen)
  19774. (current-buffer)))
  19775. files)
  19776. regexp)))
  19777. (if (boundp 'occur-mode-find-occurrence-hook)
  19778. ;; Emacs 23
  19779. (add-hook 'occur-mode-find-occurrence-hook
  19780. (lambda ()
  19781. (when (derived-mode-p 'org-mode)
  19782. (org-reveal))))
  19783. ;; Emacs 22
  19784. (defadvice occur-mode-goto-occurrence
  19785. (after org-occur-reveal activate)
  19786. (and (derived-mode-p 'org-mode) (org-reveal)))
  19787. (defadvice occur-mode-goto-occurrence-other-window
  19788. (after org-occur-reveal activate)
  19789. (and (derived-mode-p 'org-mode) (org-reveal)))
  19790. (defadvice occur-mode-display-occurrence
  19791. (after org-occur-reveal activate)
  19792. (when (derived-mode-p 'org-mode)
  19793. (let ((pos (occur-mode-find-occurrence)))
  19794. (with-current-buffer (marker-buffer pos)
  19795. (save-excursion
  19796. (goto-char pos)
  19797. (org-reveal)))))))
  19798. (defun org-occur-link-in-agenda-files ()
  19799. "Create a link and search for it in the agendas.
  19800. The link is not stored in `org-stored-links', it is just created
  19801. for the search purpose."
  19802. (interactive)
  19803. (let ((link (condition-case nil
  19804. (org-store-link nil)
  19805. (error "Unable to create a link to here"))))
  19806. (org-occur-in-agenda-files (regexp-quote link))))
  19807. (defun org-reverse-string (string)
  19808. "Return the reverse of STRING."
  19809. (apply 'string (reverse (string-to-list string))))
  19810. (defun org-uniquify (list)
  19811. "Remove duplicate elements from LIST."
  19812. (let (res)
  19813. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19814. res))
  19815. (defun org-uniquify-alist (alist)
  19816. "Merge duplicate elements of an alist.
  19817. For example, in this alist:
  19818. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19819. => '((a 1 3) (b 2))
  19820. merge (a 1) and (a 3) into (a 1 3) and return the new alist."
  19821. (let (rtn)
  19822. (mapc
  19823. (lambda (e)
  19824. (let (n)
  19825. (if (not (assoc (car e) rtn))
  19826. (push e rtn)
  19827. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19828. (setq rtn (assq-delete-all (car e) rtn))
  19829. (push n rtn))))
  19830. alist)
  19831. rtn))
  19832. (defun org-delete-all (elts list)
  19833. "Remove all elements in ELTS from LIST."
  19834. (while elts
  19835. (setq list (delete (pop elts) list)))
  19836. list)
  19837. (defun org-count (cl-item cl-seq)
  19838. "Count the number of occurrences of ITEM in SEQ.
  19839. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19840. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19841. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19842. (while (< cl-start cl-end)
  19843. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19844. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19845. (setq cl-start (1+ cl-start)))
  19846. cl-count))
  19847. (defun org-remove-if (predicate seq)
  19848. "Remove everything from SEQ that fulfills PREDICATE."
  19849. (let (res e)
  19850. (while seq
  19851. (setq e (pop seq))
  19852. (if (not (funcall predicate e)) (push e res)))
  19853. (nreverse res)))
  19854. (defun org-remove-if-not (predicate seq)
  19855. "Remove everything from SEQ that does not fulfill PREDICATE."
  19856. (let (res e)
  19857. (while seq
  19858. (setq e (pop seq))
  19859. (if (funcall predicate e) (push e res)))
  19860. (nreverse res)))
  19861. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19862. "Reduce two-argument FUNCTION across SEQ.
  19863. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19864. \":initial-value\" removed."
  19865. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19866. (cadr (memq :initial-value cl-keys)))
  19867. (cl-seq (pop cl-seq))
  19868. (t (funcall cl-func)))))
  19869. (while cl-seq
  19870. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19871. cl-accum))
  19872. (defun org-back-over-empty-lines ()
  19873. "Move backwards over whitespace, to the beginning of the first empty line.
  19874. Returns the number of empty lines passed."
  19875. (let ((pos (point)))
  19876. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19877. (skip-chars-backward " \t\n\r")
  19878. (unless (eobp)
  19879. (forward-line -1)))
  19880. (beginning-of-line 2)
  19881. (goto-char (min (point) pos))
  19882. (count-lines (point) pos)))
  19883. (defun org-skip-whitespace ()
  19884. (skip-chars-forward " \t\n\r"))
  19885. (defun org-point-in-group (point group &optional context)
  19886. "Check if POINT is in match-group GROUP.
  19887. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19888. match. If the match group does not exist or point is not inside it,
  19889. return nil."
  19890. (and (match-beginning group)
  19891. (>= point (match-beginning group))
  19892. (<= point (match-end group))
  19893. (if context
  19894. (list context (match-beginning group) (match-end group))
  19895. t)))
  19896. (defun org-switch-to-buffer-other-window (&rest args)
  19897. "Switch to buffer in a second window on the current frame.
  19898. In particular, do not allow pop-up frames.
  19899. Returns the newly created buffer."
  19900. (org-no-popups
  19901. (apply 'switch-to-buffer-other-window args)))
  19902. (defun org-combine-plists (&rest plists)
  19903. "Create a single property list from all plists in PLISTS.
  19904. The process starts by copying the first list, and then setting properties
  19905. from the other lists. Settings in the last list are the most significant
  19906. ones and overrule settings in the other lists."
  19907. (let ((rtn (copy-sequence (pop plists)))
  19908. p v ls)
  19909. (while plists
  19910. (setq ls (pop plists))
  19911. (while ls
  19912. (setq p (pop ls) v (pop ls))
  19913. (setq rtn (plist-put rtn p v))))
  19914. rtn))
  19915. (defun org-replace-escapes (string table)
  19916. "Replace %-escapes in STRING with values in TABLE.
  19917. TABLE is an association list with keys like \"%a\" and string values.
  19918. The sequences in STRING may contain normal field width and padding information,
  19919. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19920. so values can contain further %-escapes if they are define later in TABLE."
  19921. (let ((tbl (copy-alist table))
  19922. (case-fold-search nil)
  19923. (pchg 0)
  19924. e re rpl)
  19925. (while (setq e (pop tbl))
  19926. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19927. (when (and (cdr e) (string-match re (cdr e)))
  19928. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19929. (safe "SREF"))
  19930. (add-text-properties 0 3 (list 'sref sref) safe)
  19931. (setcdr e (replace-match safe t t (cdr e)))))
  19932. (while (string-match re string)
  19933. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19934. (cdr e)))
  19935. (setq string (replace-match rpl t t string))))
  19936. (while (setq pchg (next-property-change pchg string))
  19937. (let ((sref (get-text-property pchg 'sref string)))
  19938. (when (and sref (string-match "SREF" string pchg))
  19939. (setq string (replace-match sref t t string)))))
  19940. string))
  19941. (defun org-sublist (list start end)
  19942. "Return a section of LIST, from START to END.
  19943. Counting starts at 1."
  19944. (let (rtn (c start))
  19945. (setq list (nthcdr (1- start) list))
  19946. (while (and list (<= c end))
  19947. (push (pop list) rtn)
  19948. (setq c (1+ c)))
  19949. (nreverse rtn)))
  19950. (defun org-find-base-buffer-visiting (file)
  19951. "Like `find-buffer-visiting' but always return the base buffer and
  19952. not an indirect buffer."
  19953. (let ((buf (or (get-file-buffer file)
  19954. (find-buffer-visiting file))))
  19955. (if buf
  19956. (or (buffer-base-buffer buf) buf)
  19957. nil)))
  19958. (defun org-image-file-name-regexp (&optional extensions)
  19959. "Return regexp matching the file names of images.
  19960. If EXTENSIONS is given, only match these."
  19961. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19962. (image-file-name-regexp)
  19963. (let ((image-file-name-extensions
  19964. (or extensions
  19965. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19966. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19967. (concat "\\."
  19968. (regexp-opt (nconc (mapcar 'upcase
  19969. image-file-name-extensions)
  19970. image-file-name-extensions)
  19971. t)
  19972. "\\'"))))
  19973. (defun org-file-image-p (file &optional extensions)
  19974. "Return non-nil if FILE is an image."
  19975. (save-match-data
  19976. (string-match (org-image-file-name-regexp extensions) file)))
  19977. (defun org-get-cursor-date (&optional with-time)
  19978. "Return the date at cursor in as a time.
  19979. This works in the calendar and in the agenda, anywhere else it just
  19980. returns the current time.
  19981. If WITH-TIME is non-nil, returns the time of the event at point (in
  19982. the agenda) or the current time of the day."
  19983. (let (date day defd tp tm hod mod)
  19984. (when with-time
  19985. (setq tp (get-text-property (point) 'time))
  19986. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19987. (setq hod (string-to-number (match-string 1 tp))
  19988. mod (string-to-number (match-string 2 tp))))
  19989. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19990. mod (nth 1 (decode-time (current-time))))))
  19991. (cond
  19992. ((eq major-mode 'calendar-mode)
  19993. (setq date (calendar-cursor-to-date)
  19994. defd (encode-time 0 (or mod 0) (or hod 0)
  19995. (nth 1 date) (nth 0 date) (nth 2 date))))
  19996. ((eq major-mode 'org-agenda-mode)
  19997. (setq day (get-text-property (point) 'day))
  19998. (if day
  19999. (setq date (calendar-gregorian-from-absolute day)
  20000. defd (encode-time 0 (or mod 0) (or hod 0)
  20001. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20002. (or defd (current-time))))
  20003. (defun org-mark-subtree (&optional up)
  20004. "Mark the current subtree.
  20005. This puts point at the start of the current subtree, and mark at
  20006. the end. If a numeric prefix UP is given, move up into the
  20007. hierarchy of headlines by UP levels before marking the subtree."
  20008. (interactive "P")
  20009. (org-with-limited-levels
  20010. (cond ((org-at-heading-p) (beginning-of-line))
  20011. ((org-before-first-heading-p) (error "Not in a subtree"))
  20012. (t (outline-previous-visible-heading 1))))
  20013. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20014. (if (org-called-interactively-p 'any)
  20015. (call-interactively 'org-mark-element)
  20016. (org-mark-element)))
  20017. ;;; Indentation
  20018. (defun org-indent-line ()
  20019. "Indent line depending on context."
  20020. (interactive)
  20021. (let* ((pos (point))
  20022. (itemp (org-at-item-p))
  20023. (case-fold-search t)
  20024. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20025. (inline-task-p (and (featurep 'org-inlinetask)
  20026. (org-inlinetask-in-task-p)))
  20027. (inline-re (and inline-task-p
  20028. (org-inlinetask-outline-regexp)))
  20029. column)
  20030. (if (and orgstruct-is-++ (eq pos (point)))
  20031. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20032. (indent-according-to-mode))
  20033. (beginning-of-line 1)
  20034. (cond
  20035. ;; Headings
  20036. ((looking-at org-outline-regexp) (setq column 0))
  20037. ;; Footnote definition
  20038. ((looking-at org-footnote-definition-re) (setq column 0))
  20039. ;; Literal examples
  20040. ((looking-at "[ \t]*:\\( \\|$\\)")
  20041. (setq column (org-get-indentation))) ; do nothing
  20042. ;; Lists
  20043. ((ignore-errors (goto-char (org-in-item-p)))
  20044. (setq column (if itemp
  20045. (org-get-indentation)
  20046. (org-list-item-body-column (point))))
  20047. (goto-char pos))
  20048. ;; Drawers
  20049. ((and (looking-at "[ \t]*:END:")
  20050. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20051. (save-excursion
  20052. (goto-char (1- (match-beginning 1)))
  20053. (setq column (current-column))))
  20054. ;; Special blocks
  20055. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20056. (save-excursion
  20057. (re-search-backward
  20058. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20059. (setq column (org-get-indentation (match-string 0))))
  20060. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20061. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20062. (save-excursion
  20063. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20064. (setq column
  20065. (cond ((equal (downcase (match-string 1)) "src")
  20066. ;; src blocks: let `org-edit-src-exit' handle them
  20067. (org-get-indentation))
  20068. ((equal (downcase (match-string 1)) "example")
  20069. (max (org-get-indentation)
  20070. (org-get-indentation (match-string 0))))
  20071. (t
  20072. (org-get-indentation (match-string 0))))))
  20073. ;; This line has nothing special, look at the previous relevant
  20074. ;; line to compute indentation
  20075. (t
  20076. (beginning-of-line 0)
  20077. (while (and (not (bobp))
  20078. (not (looking-at org-table-line-regexp))
  20079. (not (looking-at org-drawer-regexp))
  20080. ;; When point started in an inline task, do not move
  20081. ;; above task starting line.
  20082. (not (and inline-task-p (looking-at inline-re)))
  20083. ;; Skip drawers, blocks, empty lines, verbatim,
  20084. ;; comments, tables, footnotes definitions, lists,
  20085. ;; inline tasks.
  20086. (or (and (looking-at "[ \t]*:END:")
  20087. (re-search-backward org-drawer-regexp nil t))
  20088. (and (looking-at "[ \t]*#\\+end_")
  20089. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20090. (looking-at "[ \t]*[\n:#|]")
  20091. (looking-at org-footnote-definition-re)
  20092. (and (not inline-task-p)
  20093. (featurep 'org-inlinetask)
  20094. (org-inlinetask-in-task-p)
  20095. (or (org-inlinetask-goto-beginning) t))))
  20096. (beginning-of-line 0))
  20097. (cond
  20098. ;; There was a list item above.
  20099. ((save-excursion
  20100. (and (ignore-errors (goto-char (org-in-item-p)))
  20101. (goto-char
  20102. (org-list-get-top-point (org-list-struct)))))
  20103. (looking-at org-list-full-item-re)
  20104. (setq column (length (match-string 0))))
  20105. ;; There was an heading above.
  20106. ((looking-at "\\*+[ \t]+")
  20107. (if (not org-adapt-indentation)
  20108. (setq column 0)
  20109. (goto-char (match-end 0))
  20110. (setq column (current-column))))
  20111. ;; A drawer had started and is unfinished
  20112. ((looking-at org-drawer-regexp)
  20113. (goto-char (1- (match-beginning 1)))
  20114. (setq column (current-column)))
  20115. ;; Else, nothing noticeable found: get indentation and go on.
  20116. (t (setq column (org-get-indentation))))))
  20117. ;; Now apply indentation and move cursor accordingly
  20118. (goto-char pos)
  20119. (if (<= (current-column) (current-indentation))
  20120. (org-indent-line-to column)
  20121. (save-excursion (org-indent-line-to column)))
  20122. ;; Special polishing for properties, see `org-property-format'
  20123. (setq column (current-column))
  20124. (beginning-of-line 1)
  20125. (if (looking-at
  20126. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20127. (replace-match (concat (match-string 1)
  20128. (format org-property-format
  20129. (match-string 2) (match-string 3)))
  20130. t t))
  20131. (org-move-to-column column))))
  20132. (defun org-indent-drawer ()
  20133. "Indent the drawer at point."
  20134. (interactive)
  20135. (let ((p (point))
  20136. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20137. (match-end 0)))
  20138. (folded
  20139. (save-excursion
  20140. (end-of-line)
  20141. (when (overlays-at (point))
  20142. (member 'invisible (overlay-properties
  20143. (car (overlays-at (point)))))))))
  20144. (when folded (org-cycle))
  20145. (indent-for-tab-command)
  20146. (while (and (move-beginning-of-line 2) (< (point) e))
  20147. (indent-for-tab-command))
  20148. (goto-char p)
  20149. (when folded (org-cycle)))
  20150. (message "Drawer at point indented"))
  20151. (defun org-indent-block ()
  20152. "Indent the block at point."
  20153. (interactive)
  20154. (let ((p (point))
  20155. (case-fold-search t)
  20156. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20157. (match-end 0)))
  20158. (folded
  20159. (save-excursion
  20160. (end-of-line)
  20161. (when (overlays-at (point))
  20162. (member 'invisible (overlay-properties
  20163. (car (overlays-at (point)))))))))
  20164. (when folded (org-cycle))
  20165. (indent-for-tab-command)
  20166. (while (and (move-beginning-of-line 2) (< (point) e))
  20167. (indent-for-tab-command))
  20168. (goto-char p)
  20169. (when folded (org-cycle)))
  20170. (message "Block at point indented"))
  20171. (defun org-indent-region (start end)
  20172. "Indent region."
  20173. (interactive "r")
  20174. (save-excursion
  20175. (let ((line-end (org-current-line end)))
  20176. (goto-char start)
  20177. (while (< (org-current-line) line-end)
  20178. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20179. (t (call-interactively 'org-indent-line)))
  20180. (move-beginning-of-line 2)))))
  20181. ;;; Filling
  20182. ;; We use our own fill-paragraph and auto-fill functions.
  20183. ;; `org-fill-paragraph' relies on adaptive filling and context
  20184. ;; checking. Appropriate `fill-prefix' is computed with
  20185. ;; `org-adaptive-fill-function'.
  20186. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20187. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20188. ;; `org-adaptive-fill-function' value. Internally,
  20189. ;; `org-comment-line-break-function' breaks the line.
  20190. ;; `org-setup-filling' installs filling and auto-filling related
  20191. ;; variables during `org-mode' initialization.
  20192. (defun org-setup-filling ()
  20193. (interactive)
  20194. ;; Prevent auto-fill from inserting unwanted new items.
  20195. (when (boundp 'fill-nobreak-predicate)
  20196. (org-set-local
  20197. 'fill-nobreak-predicate
  20198. (org-uniquify
  20199. (append fill-nobreak-predicate
  20200. '(org-fill-paragraph-separate-nobreak-p
  20201. org-fill-line-break-nobreak-p
  20202. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20203. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20204. (org-set-local 'auto-fill-inhibit-regexp nil)
  20205. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20206. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20207. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20208. (defvar org-element-paragraph-separate) ; org-element.el
  20209. (defun org-fill-paragraph-separate-nobreak-p ()
  20210. "Non-nil when a line break at point would insert a new item."
  20211. (looking-at (substring org-element-paragraph-separate 1)))
  20212. (defun org-fill-line-break-nobreak-p ()
  20213. "Non-nil when a line break at point would create an Org line break."
  20214. (save-excursion
  20215. (skip-chars-backward "[ \t]")
  20216. (skip-chars-backward "\\\\")
  20217. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20218. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20219. "Non-nil when a line break at point would insert a new item."
  20220. (and (org-at-timestamp-p t)
  20221. (not (looking-at org-ts-regexp-both))))
  20222. (declare-function message-in-body-p "message" ())
  20223. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20224. (defun org-adaptive-fill-function ()
  20225. "Compute a fill prefix for the current line.
  20226. Return fill prefix, as a string, or nil if current line isn't
  20227. meant to be filled."
  20228. (let (prefix)
  20229. (catch 'exit
  20230. (when (derived-mode-p 'message-mode)
  20231. (save-excursion
  20232. (beginning-of-line)
  20233. (cond ((or (not (message-in-body-p))
  20234. (looking-at orgtbl-line-start-regexp))
  20235. (throw 'exit nil))
  20236. ((looking-at message-cite-prefix-regexp)
  20237. (throw 'exit (match-string-no-properties 0)))
  20238. ((looking-at org-outline-regexp)
  20239. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20240. (org-with-wide-buffer
  20241. (let* ((p (line-beginning-position))
  20242. (element (save-excursion (beginning-of-line)
  20243. (org-element-at-point)))
  20244. (type (org-element-type element))
  20245. (post-affiliated (org-element-property :post-affiliated element)))
  20246. (unless (and post-affiliated (< p post-affiliated))
  20247. (case type
  20248. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20249. (footnote-definition "")
  20250. ((item plain-list)
  20251. (make-string (org-list-item-body-column
  20252. (or post-affiliated
  20253. (org-element-property :begin element)))
  20254. ? ))
  20255. (paragraph
  20256. ;; Fill prefix is usually the same as the current line,
  20257. ;; except if the paragraph is at the beginning of an item.
  20258. (let ((parent (org-element-property :parent element)))
  20259. (cond ((eq (org-element-type parent) 'item)
  20260. (make-string (org-list-item-body-column
  20261. (org-element-property :begin parent))
  20262. ? ))
  20263. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20264. (match-string 0))
  20265. (t ""))))
  20266. (comment-block
  20267. ;; Only fill contents if P is within block boundaries.
  20268. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20269. (forward-line)
  20270. (point)))
  20271. (cend (save-excursion
  20272. (goto-char (org-element-property :end element))
  20273. (skip-chars-backward " \r\t\n")
  20274. (line-beginning-position))))
  20275. (when (and (>= p cbeg) (< p cend))
  20276. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20277. (match-string 0)
  20278. "")))))))))))
  20279. (declare-function message-goto-body "message" ())
  20280. (defvar message-cite-prefix-regexp) ; From message.el
  20281. (defun org-fill-paragraph (&optional justify)
  20282. "Fill element at point, when applicable.
  20283. This function only applies to comment blocks, comments, example
  20284. blocks and paragraphs. Also, as a special case, re-align table
  20285. when point is at one.
  20286. If JUSTIFY is non-nil (interactively, with prefix argument),
  20287. justify as well. If `sentence-end-double-space' is non-nil, then
  20288. period followed by one space does not end a sentence, so don't
  20289. break a line there. The variable `fill-column' controls the
  20290. width for filling.
  20291. For convenience, when point is at a plain list, an item or
  20292. a footnote definition, try to fill the first paragraph within."
  20293. (interactive)
  20294. (if (and (derived-mode-p 'message-mode)
  20295. (or (not (message-in-body-p))
  20296. (save-excursion (move-beginning-of-line 1)
  20297. (looking-at message-cite-prefix-regexp))))
  20298. ;; First ensure filling is correct in message-mode.
  20299. (let ((fill-paragraph-function
  20300. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20301. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20302. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20303. (paragraph-separate
  20304. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20305. (fill-paragraph nil))
  20306. (with-syntax-table org-mode-transpose-word-syntax-table
  20307. ;; Move to end of line in order to get the first paragraph
  20308. ;; within a plain list or a footnote definition.
  20309. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20310. ;; First check if point is in a blank line at the beginning of
  20311. ;; the buffer. In that case, ignore filling.
  20312. (case (org-element-type element)
  20313. ;; Use major mode filling function is src blocks.
  20314. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20315. ;; Align Org tables, leave table.el tables as-is.
  20316. (table-row (org-table-align) t)
  20317. (table
  20318. (when (eq (org-element-property :type element) 'org)
  20319. (org-table-align))
  20320. t)
  20321. (paragraph
  20322. ;; Paragraphs may contain `line-break' type objects.
  20323. (let ((beg (max (point-min)
  20324. (org-element-property :contents-begin element)))
  20325. (end (min (point-max)
  20326. (org-element-property :contents-end element))))
  20327. ;; Do nothing if point is at an affiliated keyword.
  20328. (if (< (line-end-position) beg) t
  20329. (when (derived-mode-p 'message-mode)
  20330. ;; In `message-mode', do not fill following citation
  20331. ;; in current paragraph nor text before message body.
  20332. (let ((body-start (save-excursion (message-goto-body))))
  20333. (when body-start (setq beg (max body-start beg))))
  20334. (when (save-excursion
  20335. (re-search-forward
  20336. (concat "^" message-cite-prefix-regexp) end t))
  20337. (setq end (match-beginning 0))))
  20338. ;; Fill paragraph, taking line breaks into account.
  20339. ;; For that, slice the paragraph using line breaks as
  20340. ;; separators, and fill the parts in reverse order to
  20341. ;; avoid messing with markers.
  20342. (save-excursion
  20343. (goto-char end)
  20344. (mapc
  20345. (lambda (pos)
  20346. (fill-region-as-paragraph pos (point) justify)
  20347. (goto-char pos))
  20348. ;; Find the list of ending positions for line breaks
  20349. ;; in the current paragraph. Add paragraph
  20350. ;; beginning to include first slice.
  20351. (nreverse
  20352. (cons beg
  20353. (org-element-map
  20354. (org-element--parse-objects
  20355. beg end nil (org-element-restriction 'paragraph))
  20356. 'line-break
  20357. (lambda (lb) (org-element-property :end lb)))))))
  20358. t)))
  20359. ;; Contents of `comment-block' type elements should be
  20360. ;; filled as plain text, but only if point is within block
  20361. ;; markers.
  20362. (comment-block
  20363. (let* ((case-fold-search t)
  20364. (beg (save-excursion
  20365. (goto-char (org-element-property :begin element))
  20366. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20367. (forward-line)
  20368. (point)))
  20369. (end (save-excursion
  20370. (goto-char (org-element-property :end element))
  20371. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20372. (line-beginning-position))))
  20373. (when (and (>= (point) beg) (< (point) end))
  20374. (fill-region-as-paragraph
  20375. (save-excursion
  20376. (end-of-line)
  20377. (re-search-backward "^[ \t]*$" beg 'move)
  20378. (line-beginning-position))
  20379. (save-excursion
  20380. (beginning-of-line)
  20381. (re-search-forward "^[ \t]*$" end 'move)
  20382. (line-beginning-position))
  20383. justify)))
  20384. t)
  20385. ;; Fill comments.
  20386. (comment (fill-comment-paragraph justify))
  20387. ;; Ignore every other element.
  20388. (otherwise t))))))
  20389. (defun org-auto-fill-function ()
  20390. "Auto-fill function."
  20391. ;; Check if auto-filling is meaningful.
  20392. (let ((fc (current-fill-column)))
  20393. (when (and fc (> (current-column) fc))
  20394. (let* ((fill-prefix (org-adaptive-fill-function))
  20395. ;; Enforce empty fill prefix, if required. Otherwise, it
  20396. ;; will be computed again.
  20397. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20398. (when fill-prefix (do-auto-fill))))))
  20399. (defun org-comment-line-break-function (&optional soft)
  20400. "Break line at point and indent, continuing comment if within one.
  20401. The inserted newline is marked hard if variable
  20402. `use-hard-newlines' is true, unless optional argument SOFT is
  20403. non-nil."
  20404. (if soft (insert-and-inherit ?\n) (newline 1))
  20405. (save-excursion (forward-char -1) (delete-horizontal-space))
  20406. (delete-horizontal-space)
  20407. (indent-to-left-margin)
  20408. (insert-before-markers-and-inherit fill-prefix))
  20409. ;;; Comments
  20410. ;; Org comments syntax is quite complex. It requires the entire line
  20411. ;; to be just a comment. Also, even with the right syntax at the
  20412. ;; beginning of line, some some elements (i.e. verse-block or
  20413. ;; example-block) don't accept comments. Usual Emacs comment commands
  20414. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20415. ;; Org still relies on `comment-dwim', but cannot trust
  20416. ;; `comment-only-p'. So, `comment-region-function' and
  20417. ;; `uncomment-region-function' both point
  20418. ;; to`org-comment-or-uncomment-region'. Eventually,
  20419. ;; `org-insert-comment' takes care of insertion of comments at the
  20420. ;; beginning of line.
  20421. ;; `org-setup-comments-handling' install comments related variables
  20422. ;; during `org-mode' initialization.
  20423. (defun org-setup-comments-handling ()
  20424. (interactive)
  20425. (org-set-local 'comment-use-syntax nil)
  20426. (org-set-local 'comment-start "# ")
  20427. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20428. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20429. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20430. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20431. (defun org-insert-comment ()
  20432. "Insert an empty comment above current line.
  20433. If the line is empty, insert comment at its beginning."
  20434. (beginning-of-line)
  20435. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20436. (org-indent-line)
  20437. (insert "# "))
  20438. (defvar comment-empty-lines) ; From newcomment.el.
  20439. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20440. "Comment or uncomment each non-blank line in the region.
  20441. Uncomment each non-blank line between BEG and END if it only
  20442. contains commented lines. Otherwise, comment them."
  20443. (save-restriction
  20444. ;; Restrict region
  20445. (narrow-to-region (save-excursion (goto-char beg)
  20446. (skip-chars-forward " \r\t\n" end)
  20447. (line-beginning-position))
  20448. (save-excursion (goto-char end)
  20449. (skip-chars-backward " \r\t\n" beg)
  20450. (line-end-position)))
  20451. (let ((uncommentp
  20452. ;; UNCOMMENTP is non-nil when every non blank line between
  20453. ;; BEG and END is a comment.
  20454. (save-excursion
  20455. (goto-char (point-min))
  20456. (while (and (not (eobp))
  20457. (let ((element (org-element-at-point)))
  20458. (and (eq (org-element-type element) 'comment)
  20459. (goto-char (min (point-max)
  20460. (org-element-property
  20461. :end element)))))))
  20462. (eobp))))
  20463. (if uncommentp
  20464. ;; Only blank lines and comments in region: uncomment it.
  20465. (save-excursion
  20466. (goto-char (point-min))
  20467. (while (not (eobp))
  20468. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20469. (replace-match "" nil nil nil 1))
  20470. (forward-line)))
  20471. ;; Comment each line in region.
  20472. (let ((min-indent (point-max)))
  20473. ;; First find the minimum indentation across all lines.
  20474. (save-excursion
  20475. (goto-char (point-min))
  20476. (while (and (not (eobp)) (not (zerop min-indent)))
  20477. (unless (looking-at "[ \t]*$")
  20478. (setq min-indent (min min-indent (current-indentation))))
  20479. (forward-line)))
  20480. ;; Then loop over all lines.
  20481. (save-excursion
  20482. (goto-char (point-min))
  20483. (while (not (eobp))
  20484. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20485. ;; Don't get fooled by invisible text (e.g. link path)
  20486. ;; when moving to column MIN-INDENT.
  20487. (let ((buffer-invisibility-spec nil))
  20488. (org-move-to-column min-indent t))
  20489. (insert comment-start))
  20490. (forward-line))))))))
  20491. ;;; Planning
  20492. ;; This section contains tools to operate on timestamp objects, as
  20493. ;; returned by, e.g. `org-element-context'.
  20494. (defun org-timestamp-has-time-p (timestamp)
  20495. "Non-nil when TIMESTAMP has a time specified."
  20496. (org-element-property :hour-start timestamp))
  20497. (defun org-timestamp-format (timestamp format &optional end utc)
  20498. "Format a TIMESTAMP element into a string.
  20499. FORMAT is a format specifier to be passed to
  20500. `format-time-string'.
  20501. When optional argument END is non-nil, use end of date-range or
  20502. time-range, if possible.
  20503. When optional argument UTC is non-nil, time will be expressed as
  20504. Universal Time."
  20505. (format-time-string
  20506. format
  20507. (apply 'encode-time
  20508. (cons 0
  20509. (mapcar
  20510. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20511. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20512. '(:minute-start :hour-start :day-start :month-start
  20513. :year-start)))))
  20514. utc))
  20515. (defun org-timestamp-split-range (timestamp &optional end)
  20516. "Extract a timestamp object from a date or time range.
  20517. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20518. the end of the range. Otherwise, extract its start.
  20519. Return a new timestamp object sharing the same parent as
  20520. TIMESTAMP."
  20521. (let ((type (org-element-property :type timestamp)))
  20522. (if (memq type '(active inactive diary)) timestamp
  20523. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20524. ;; Set new type.
  20525. (org-element-put-property
  20526. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20527. ;; Copy start properties over end properties if END is
  20528. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20529. (let ((p-alist '((:minute-start . :minute-end)
  20530. (:hour-start . :hour-end)
  20531. (:day-start . :day-end)
  20532. (:month-start . :month-end)
  20533. (:year-start . :year-end))))
  20534. (dolist (p-cell p-alist)
  20535. (org-element-put-property
  20536. split-ts
  20537. (funcall (if end 'car 'cdr) p-cell)
  20538. (org-element-property
  20539. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20540. ;; Eventually refresh `:raw-value'.
  20541. (org-element-put-property split-ts :raw-value nil)
  20542. (org-element-put-property
  20543. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20544. (defun org-timestamp-translate (timestamp &optional boundary)
  20545. "Apply `org-translate-time' on a TIMESTAMP object.
  20546. When optional argument BOUNDARY is non-nil, it is either the
  20547. symbol `start' or `end'. In this case, only translate the
  20548. starting or ending part of TIMESTAMP if it is a date or time
  20549. range. Otherwise, translate both parts."
  20550. (if (and (not boundary)
  20551. (memq (org-element-property :type timestamp)
  20552. '(active-range inactive-range)))
  20553. (concat
  20554. (org-translate-time
  20555. (org-element-property :raw-value
  20556. (org-timestamp-split-range timestamp)))
  20557. "--"
  20558. (org-translate-time
  20559. (org-element-property :raw-value
  20560. (org-timestamp-split-range timestamp t))))
  20561. (org-translate-time
  20562. (org-element-property
  20563. :raw-value
  20564. (if (not boundary) timestamp
  20565. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20566. ;;; Other stuff.
  20567. (defun org-toggle-fixed-width-section (arg)
  20568. "Toggle the fixed-width export.
  20569. If there is no active region, the QUOTE keyword at the current headline is
  20570. inserted or removed. When present, it causes the text between this headline
  20571. and the next to be exported as fixed-width text, and unmodified.
  20572. If there is an active region, this command adds or removes a colon as the
  20573. first character of this line. If the first character of a line is a colon,
  20574. this line is also exported in fixed-width font."
  20575. (interactive "P")
  20576. (let* ((cc 0)
  20577. (regionp (org-region-active-p))
  20578. (beg (if regionp (region-beginning) (point)))
  20579. (end (if regionp (region-end)))
  20580. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20581. (case-fold-search nil)
  20582. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20583. off)
  20584. (if regionp
  20585. (save-excursion
  20586. (goto-char beg)
  20587. (setq cc (current-column))
  20588. (beginning-of-line 1)
  20589. (setq off (looking-at re))
  20590. (while (> nlines 0)
  20591. (setq nlines (1- nlines))
  20592. (beginning-of-line 1)
  20593. (cond
  20594. (arg
  20595. (org-move-to-column cc t)
  20596. (insert ": \n")
  20597. (forward-line -1))
  20598. ((and off (looking-at re))
  20599. (replace-match "" t t nil 1))
  20600. ((not off) (org-move-to-column cc t) (insert ": ")))
  20601. (forward-line 1)))
  20602. (save-excursion
  20603. (org-back-to-heading)
  20604. (cond
  20605. ((looking-at (format org-heading-keyword-regexp-format
  20606. org-quote-string))
  20607. (goto-char (match-end 1))
  20608. (looking-at (concat " +" org-quote-string))
  20609. (replace-match "" t t)
  20610. (when (eolp) (insert " ")))
  20611. ((looking-at org-outline-regexp)
  20612. (goto-char (match-end 0))
  20613. (insert org-quote-string " ")))))))
  20614. (defun org-reftex-citation ()
  20615. "Use reftex-citation to insert a citation into the buffer.
  20616. This looks for a line like
  20617. #+BIBLIOGRAPHY: foo plain option:-d
  20618. and derives from it that foo.bib is the bibliography file relevant
  20619. for this document. It then installs the necessary environment for RefTeX
  20620. to work in this buffer and calls `reftex-citation' to insert a citation
  20621. into the buffer.
  20622. Export of such citations to both LaTeX and HTML is handled by the contributed
  20623. package org-exp-bibtex by Taru Karttunen."
  20624. (interactive)
  20625. (let ((reftex-docstruct-symbol 'rds)
  20626. (reftex-cite-format "\\cite{%l}")
  20627. rds bib)
  20628. (save-excursion
  20629. (save-restriction
  20630. (widen)
  20631. (let ((case-fold-search t)
  20632. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20633. (if (not (save-excursion
  20634. (or (re-search-forward re nil t)
  20635. (re-search-backward re nil t))))
  20636. (error "No bibliography defined in file")
  20637. (setq bib (concat (match-string 1) ".bib")
  20638. rds (list (list 'bib bib)))))))
  20639. (call-interactively 'reftex-citation)))
  20640. ;;;; Functions extending outline functionality
  20641. (defun org-beginning-of-line (&optional arg)
  20642. "Go to the beginning of the current line. If that is invisible, continue
  20643. to a visible line beginning. This makes the function of C-a more intuitive.
  20644. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20645. first attempt, and only move to after the tags when the cursor is already
  20646. beyond the end of the headline."
  20647. (interactive "P")
  20648. (let ((pos (point))
  20649. (special (if (consp org-special-ctrl-a/e)
  20650. (car org-special-ctrl-a/e)
  20651. org-special-ctrl-a/e))
  20652. refpos)
  20653. (if (org-bound-and-true-p visual-line-mode)
  20654. (beginning-of-visual-line 1)
  20655. (beginning-of-line 1))
  20656. (if (and arg (fboundp 'move-beginning-of-line))
  20657. (call-interactively 'move-beginning-of-line)
  20658. (if (bobp)
  20659. nil
  20660. (backward-char 1)
  20661. (if (org-truely-invisible-p)
  20662. (while (and (not (bobp)) (org-truely-invisible-p))
  20663. (backward-char 1)
  20664. (beginning-of-line 1))
  20665. (forward-char 1))))
  20666. (when special
  20667. (cond
  20668. ((and (looking-at org-complex-heading-regexp)
  20669. (= (char-after (match-end 1)) ?\ ))
  20670. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20671. (point-at-eol)))
  20672. (goto-char
  20673. (if (eq special t)
  20674. (cond ((> pos refpos) refpos)
  20675. ((= pos (point)) refpos)
  20676. (t (point)))
  20677. (cond ((> pos (point)) (point))
  20678. ((not (eq last-command this-command)) (point))
  20679. (t refpos)))))
  20680. ((org-at-item-p)
  20681. ;; Being at an item and not looking at an the item means point
  20682. ;; was previously moved to beginning of a visual line, which
  20683. ;; doesn't contain the item. Therefore, do nothing special,
  20684. ;; just stay here.
  20685. (when (looking-at org-list-full-item-re)
  20686. ;; Set special position at first white space character after
  20687. ;; bullet, and check-box, if any.
  20688. (let ((after-bullet
  20689. (let ((box (match-end 3)))
  20690. (if (not box) (match-end 1)
  20691. (let ((after (char-after box)))
  20692. (if (and after (= after ? )) (1+ box) box))))))
  20693. ;; Special case: Move point to special position when
  20694. ;; currently after it or at beginning of line.
  20695. (if (eq special t)
  20696. (when (or (> pos after-bullet) (= (point) pos))
  20697. (goto-char after-bullet))
  20698. ;; Reversed case: Move point to special position when
  20699. ;; point was already at beginning of line and command is
  20700. ;; repeated.
  20701. (when (and (= (point) pos) (eq last-command this-command))
  20702. (goto-char after-bullet))))))))
  20703. (org-no-warnings
  20704. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20705. (defun org-end-of-line (&optional arg)
  20706. "Go to the end of the line.
  20707. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20708. tags on the first attempt, and only move to after the tags when
  20709. the cursor is already beyond the end of the headline."
  20710. (interactive "P")
  20711. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20712. org-special-ctrl-a/e))
  20713. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20714. 'end-of-visual-line)
  20715. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20716. (t 'end-of-line))))
  20717. (if (or (not special) arg) (call-interactively move-fun)
  20718. (let* ((element (save-excursion (beginning-of-line)
  20719. (org-element-at-point)))
  20720. (type (org-element-type element)))
  20721. (cond
  20722. ((memq type '(headline inlinetask))
  20723. (let ((pos (point)))
  20724. (beginning-of-line 1)
  20725. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20726. (if (eq special t)
  20727. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20728. (goto-char (match-beginning 1))
  20729. (goto-char (match-end 0)))
  20730. (if (or (< pos (match-end 0))
  20731. (not (eq this-command last-command)))
  20732. (goto-char (match-end 0))
  20733. (goto-char (match-beginning 1))))
  20734. (call-interactively move-fun))))
  20735. ((org-element-property :hiddenp element)
  20736. ;; If element is hidden, `move-end-of-line' would put point
  20737. ;; after it. Use `end-of-line' to stay on current line.
  20738. (call-interactively 'end-of-line))
  20739. (t (call-interactively move-fun)))))
  20740. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20741. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20742. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20743. (defun org-backward-sentence (&optional arg)
  20744. "Go to beginning of sentence, or beginning of table field.
  20745. This will call `backward-sentence' or `org-table-beginning-of-field',
  20746. depending on context."
  20747. (interactive "P")
  20748. (cond
  20749. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20750. (t (call-interactively 'backward-sentence))))
  20751. (defun org-forward-sentence (&optional arg)
  20752. "Go to end of sentence, or end of table field.
  20753. This will call `forward-sentence' or `org-table-end-of-field',
  20754. depending on context."
  20755. (interactive "P")
  20756. (cond
  20757. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20758. (t (call-interactively 'forward-sentence))))
  20759. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20760. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20761. (defun org-kill-line (&optional arg)
  20762. "Kill line, to tags or end of line."
  20763. (interactive "P")
  20764. (cond
  20765. ((or (not org-special-ctrl-k)
  20766. (bolp)
  20767. (not (org-at-heading-p)))
  20768. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20769. org-ctrl-k-protect-subtree)
  20770. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20771. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20772. (error "C-k aborted - would kill hidden subtree")))
  20773. (call-interactively
  20774. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20775. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20776. (kill-region (point) (match-beginning 1))
  20777. (org-set-tags nil t))
  20778. (t (kill-region (point) (point-at-eol)))))
  20779. (define-key org-mode-map "\C-k" 'org-kill-line)
  20780. (defun org-yank (&optional arg)
  20781. "Yank. If the kill is a subtree, treat it specially.
  20782. This command will look at the current kill and check if is a single
  20783. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20784. cursor is at the beginning of a line or after the stars of a currently
  20785. empty headline, then the yank is handled specially. How exactly depends
  20786. on the value of the following variables, both set by default.
  20787. org-yank-folded-subtrees
  20788. When set, the subtree(s) will be folded after insertion, but only
  20789. if doing so would now swallow text after the yanked text.
  20790. org-yank-adjusted-subtrees
  20791. When set, the subtree will be promoted or demoted in order to
  20792. fit into the local outline tree structure, which means that the level
  20793. will be adjusted so that it becomes the smaller one of the two
  20794. *visible* surrounding headings.
  20795. Any prefix to this command will cause `yank' to be called directly with
  20796. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20797. will just
  20798. plainly yank the text as it is.
  20799. \[1] The test checks if the first non-white line is a heading
  20800. and if there are no other headings with fewer stars."
  20801. (interactive "P")
  20802. (org-yank-generic 'yank arg))
  20803. (defun org-yank-generic (command arg)
  20804. "Perform some yank-like command.
  20805. This function implements the behavior described in the `org-yank'
  20806. documentation. However, it has been generalized to work for any
  20807. interactive command with similar behavior."
  20808. ;; pretend to be command COMMAND
  20809. (setq this-command command)
  20810. (if arg
  20811. (call-interactively command)
  20812. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20813. (and (org-kill-is-subtree-p)
  20814. (or (bolp)
  20815. (and (looking-at "[ \t]*$")
  20816. (string-match
  20817. "\\`\\*+\\'"
  20818. (buffer-substring (point-at-bol) (point)))))))
  20819. swallowp)
  20820. (cond
  20821. ((and subtreep org-yank-folded-subtrees)
  20822. (let ((beg (point))
  20823. end)
  20824. (if (and subtreep org-yank-adjusted-subtrees)
  20825. (org-paste-subtree nil nil 'for-yank)
  20826. (call-interactively command))
  20827. (setq end (point))
  20828. (goto-char beg)
  20829. (when (and (bolp) subtreep
  20830. (not (setq swallowp
  20831. (org-yank-folding-would-swallow-text beg end))))
  20832. (org-with-limited-levels
  20833. (or (looking-at org-outline-regexp)
  20834. (re-search-forward org-outline-regexp-bol end t))
  20835. (while (and (< (point) end) (looking-at org-outline-regexp))
  20836. (hide-subtree)
  20837. (org-cycle-show-empty-lines 'folded)
  20838. (condition-case nil
  20839. (outline-forward-same-level 1)
  20840. (error (goto-char end))))))
  20841. (when swallowp
  20842. (message
  20843. "Inserted text not folded because that would swallow text"))
  20844. (goto-char end)
  20845. (skip-chars-forward " \t\n\r")
  20846. (beginning-of-line 1)
  20847. (push-mark beg 'nomsg)))
  20848. ((and subtreep org-yank-adjusted-subtrees)
  20849. (let ((beg (point-at-bol)))
  20850. (org-paste-subtree nil nil 'for-yank)
  20851. (push-mark beg 'nomsg)))
  20852. (t
  20853. (call-interactively command))))))
  20854. (defun org-yank-folding-would-swallow-text (beg end)
  20855. "Would hide-subtree at BEG swallow any text after END?"
  20856. (let (level)
  20857. (org-with-limited-levels
  20858. (save-excursion
  20859. (goto-char beg)
  20860. (when (or (looking-at org-outline-regexp)
  20861. (re-search-forward org-outline-regexp-bol end t))
  20862. (setq level (org-outline-level)))
  20863. (goto-char end)
  20864. (skip-chars-forward " \t\r\n\v\f")
  20865. (if (or (eobp)
  20866. (and (bolp) (looking-at org-outline-regexp)
  20867. (<= (org-outline-level) level)))
  20868. nil ; Nothing would be swallowed
  20869. t))))) ; something would swallow
  20870. (define-key org-mode-map "\C-y" 'org-yank)
  20871. (defun org-truely-invisible-p ()
  20872. "Check if point is at a character currently not visible.
  20873. This version does not only check the character property, but also
  20874. `visible-mode'."
  20875. ;; Early versions of noutline don't have `outline-invisible-p'.
  20876. (if (org-bound-and-true-p visible-mode)
  20877. nil
  20878. (outline-invisible-p)))
  20879. (defun org-invisible-p2 ()
  20880. "Check if point is at a character currently not visible."
  20881. (save-excursion
  20882. (if (and (eolp) (not (bobp))) (backward-char 1))
  20883. ;; Early versions of noutline don't have `outline-invisible-p'.
  20884. (outline-invisible-p)))
  20885. (defun org-back-to-heading (&optional invisible-ok)
  20886. "Call `outline-back-to-heading', but provide a better error message."
  20887. (condition-case nil
  20888. (outline-back-to-heading invisible-ok)
  20889. (error (error "Before first headline at position %d in buffer %s"
  20890. (point) (current-buffer)))))
  20891. (defun org-before-first-heading-p ()
  20892. "Before first heading?"
  20893. (save-excursion
  20894. (end-of-line)
  20895. (null (re-search-backward org-outline-regexp-bol nil t))))
  20896. (defun org-at-heading-p (&optional ignored)
  20897. (outline-on-heading-p t))
  20898. ;; Compatibility alias with Org versions < 7.8.03
  20899. (defalias 'org-on-heading-p 'org-at-heading-p)
  20900. (defun org-at-comment-p nil
  20901. "Is cursor in a line starting with a # character?"
  20902. (save-excursion
  20903. (beginning-of-line)
  20904. (looking-at "^#")))
  20905. (defun org-at-drawer-p nil
  20906. "Is cursor at a drawer keyword?"
  20907. (save-excursion
  20908. (move-beginning-of-line 1)
  20909. (looking-at org-drawer-regexp)))
  20910. (defun org-at-block-p nil
  20911. "Is cursor at a block keyword?"
  20912. (save-excursion
  20913. (move-beginning-of-line 1)
  20914. (looking-at org-block-regexp)))
  20915. (defun org-point-at-end-of-empty-headline ()
  20916. "If point is at the end of an empty headline, return t, else nil.
  20917. If the heading only contains a TODO keyword, it is still still considered
  20918. empty."
  20919. (and (looking-at "[ \t]*$")
  20920. (when org-todo-line-regexp
  20921. (save-excursion
  20922. (beginning-of-line 1)
  20923. (let ((case-fold-search nil))
  20924. (looking-at org-todo-line-regexp)
  20925. (string= (match-string 3) ""))))))
  20926. (defun org-at-heading-or-item-p ()
  20927. (or (org-at-heading-p) (org-at-item-p)))
  20928. (defun org-at-target-p ()
  20929. (or (org-in-regexp org-radio-target-regexp)
  20930. (org-in-regexp org-target-regexp)))
  20931. ;; Compatibility alias with Org versions < 7.8.03
  20932. (defalias 'org-on-target-p 'org-at-target-p)
  20933. (defun org-up-heading-all (arg)
  20934. "Move to the heading line of which the present line is a subheading.
  20935. This function considers both visible and invisible heading lines.
  20936. With argument, move up ARG levels."
  20937. (if (fboundp 'outline-up-heading-all)
  20938. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20939. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20940. (defun org-up-heading-safe ()
  20941. "Move to the heading line of which the present line is a subheading.
  20942. This version will not throw an error. It will return the level of the
  20943. headline found, or nil if no higher level is found.
  20944. Also, this function will be a lot faster than `outline-up-heading',
  20945. because it relies on stars being the outline starters. This can really
  20946. make a significant difference in outlines with very many siblings."
  20947. (let (start-level re)
  20948. (org-back-to-heading t)
  20949. (setq start-level (funcall outline-level))
  20950. (if (equal start-level 1)
  20951. nil
  20952. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20953. (if (re-search-backward re nil t)
  20954. (funcall outline-level)))))
  20955. (defun org-first-sibling-p ()
  20956. "Is this heading the first child of its parents?"
  20957. (interactive)
  20958. (let ((re org-outline-regexp-bol)
  20959. level l)
  20960. (unless (org-at-heading-p t)
  20961. (error "Not at a heading"))
  20962. (setq level (funcall outline-level))
  20963. (save-excursion
  20964. (if (not (re-search-backward re nil t))
  20965. t
  20966. (setq l (funcall outline-level))
  20967. (< l level)))))
  20968. (defun org-goto-sibling (&optional previous)
  20969. "Goto the next sibling, even if it is invisible.
  20970. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20971. when a sibling was found. When none is found, return nil and don't
  20972. move point."
  20973. (let ((fun (if previous 're-search-backward 're-search-forward))
  20974. (pos (point))
  20975. (re org-outline-regexp-bol)
  20976. level l)
  20977. (when (condition-case nil (org-back-to-heading t) (error nil))
  20978. (setq level (funcall outline-level))
  20979. (catch 'exit
  20980. (or previous (forward-char 1))
  20981. (while (funcall fun re nil t)
  20982. (setq l (funcall outline-level))
  20983. (when (< l level) (goto-char pos) (throw 'exit nil))
  20984. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20985. (goto-char pos)
  20986. nil))))
  20987. (defun org-show-siblings ()
  20988. "Show all siblings of the current headline."
  20989. (save-excursion
  20990. (while (org-goto-sibling) (org-flag-heading nil)))
  20991. (save-excursion
  20992. (while (org-goto-sibling 'previous)
  20993. (org-flag-heading nil))))
  20994. (defun org-goto-first-child ()
  20995. "Goto the first child, even if it is invisible.
  20996. Return t when a child was found. Otherwise don't move point and
  20997. return nil."
  20998. (let (level (pos (point)) (re org-outline-regexp-bol))
  20999. (when (condition-case nil (org-back-to-heading t) (error nil))
  21000. (setq level (outline-level))
  21001. (forward-char 1)
  21002. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21003. (progn (goto-char (match-beginning 0)) t)
  21004. (goto-char pos) nil))))
  21005. (defun org-show-hidden-entry ()
  21006. "Show an entry where even the heading is hidden."
  21007. (save-excursion
  21008. (org-show-entry)))
  21009. (defun org-flag-heading (flag &optional entry)
  21010. "Flag the current heading. FLAG non-nil means make invisible.
  21011. When ENTRY is non-nil, show the entire entry."
  21012. (save-excursion
  21013. (org-back-to-heading t)
  21014. ;; Check if we should show the entire entry
  21015. (if entry
  21016. (progn
  21017. (org-show-entry)
  21018. (save-excursion
  21019. (and (outline-next-heading)
  21020. (org-flag-heading nil))))
  21021. (outline-flag-region (max (point-min) (1- (point)))
  21022. (save-excursion (outline-end-of-heading) (point))
  21023. flag))))
  21024. (defun org-get-next-sibling ()
  21025. "Move to next heading of the same level, and return point.
  21026. If there is no such heading, return nil.
  21027. This is like outline-next-sibling, but invisible headings are ok."
  21028. (let ((level (funcall outline-level)))
  21029. (outline-next-heading)
  21030. (while (and (not (eobp)) (> (funcall outline-level) level))
  21031. (outline-next-heading))
  21032. (if (or (eobp) (< (funcall outline-level) level))
  21033. nil
  21034. (point))))
  21035. (defun org-get-last-sibling ()
  21036. "Move to previous heading of the same level, and return point.
  21037. If there is no such heading, return nil."
  21038. (let ((opoint (point))
  21039. (level (funcall outline-level)))
  21040. (outline-previous-heading)
  21041. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21042. (while (and (> (funcall outline-level) level)
  21043. (not (bobp)))
  21044. (outline-previous-heading))
  21045. (if (< (funcall outline-level) level)
  21046. nil
  21047. (point)))))
  21048. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21049. "Goto to the end of a subtree."
  21050. ;; This contains an exact copy of the original function, but it uses
  21051. ;; `org-back-to-heading', to make it work also in invisible
  21052. ;; trees. And is uses an invisible-ok argument.
  21053. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21054. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21055. ;; with many children and grandchildren etc, this can be much faster
  21056. ;; than the outline version.
  21057. (org-back-to-heading invisible-ok)
  21058. (let ((first t)
  21059. (level (funcall outline-level)))
  21060. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21061. ;; A true heading (not a plain list item), in Org-mode
  21062. ;; This means we can easily find the end by looking
  21063. ;; only for the right number of stars. Using a regexp to do
  21064. ;; this is so much faster than using a Lisp loop.
  21065. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21066. (forward-char 1)
  21067. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21068. ;; something else, do it the slow way
  21069. (while (and (not (eobp))
  21070. (or first (> (funcall outline-level) level)))
  21071. (setq first nil)
  21072. (outline-next-heading)))
  21073. (unless to-heading
  21074. (if (memq (preceding-char) '(?\n ?\^M))
  21075. (progn
  21076. ;; Go to end of line before heading
  21077. (forward-char -1)
  21078. (if (memq (preceding-char) '(?\n ?\^M))
  21079. ;; leave blank line before heading
  21080. (forward-char -1))))))
  21081. (point))
  21082. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21083. "Use Org version in org-mode, for dramatic speed-up."
  21084. (if (derived-mode-p 'org-mode)
  21085. (progn
  21086. (org-end-of-subtree nil t)
  21087. (unless (eobp) (backward-char 1)))
  21088. ad-do-it))
  21089. (defun org-end-of-meta-data-and-drawers ()
  21090. "Jump to the first text after meta data and drawers in the current entry.
  21091. This will move over empty lines, lines with planning time stamps,
  21092. clocking lines, and drawers."
  21093. (org-back-to-heading t)
  21094. (let ((end (save-excursion (outline-next-heading) (point)))
  21095. (re (concat "\\(" org-drawer-regexp "\\)"
  21096. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21097. (forward-line 1)
  21098. (while (re-search-forward re end t)
  21099. (if (not (match-end 1))
  21100. ;; empty or planning line
  21101. (forward-line 1)
  21102. ;; a drawer, find the end
  21103. (re-search-forward "^[ \t]*:END:" end 'move)
  21104. (forward-line 1)))
  21105. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21106. (point)))
  21107. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21108. "Move forward to the ARG'th subheading at same level as this one.
  21109. Stop at the first and last subheadings of a superior heading.
  21110. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21111. non-nil it will also look at invisible ones."
  21112. (interactive "p")
  21113. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21114. (if (and arg (< arg 0))
  21115. (goto-char (point-min))
  21116. (outline-next-heading))
  21117. (org-at-heading-p)
  21118. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21119. (f (if (and arg (< arg 0))
  21120. 're-search-backward
  21121. 're-search-forward))
  21122. (count (if arg (abs arg) 1))
  21123. (result (point)))
  21124. (forward-char (if (and arg (< arg 0)) -1 1))
  21125. (while (and (> count 0)
  21126. (funcall f org-outline-regexp-bol nil 'move))
  21127. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21128. (cond ((< l level) (setq count 0))
  21129. ((and (= l level)
  21130. (or invisible-ok
  21131. (progn
  21132. (goto-char (line-beginning-position))
  21133. (not (outline-invisible-p)))))
  21134. (setq count (1- count))
  21135. (when (eq l level)
  21136. (setq result (point)))))))
  21137. (goto-char result))
  21138. (beginning-of-line 1)))
  21139. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21140. "Move backward to the ARG'th subheading at same level as this one.
  21141. Stop at the first and last subheadings of a superior heading."
  21142. (interactive "p")
  21143. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21144. (defun org-next-block (arg &optional backward block-regexp)
  21145. "Jump to the next block.
  21146. With a prefix argument ARG, jump forward ARG many source blocks.
  21147. When BACKWARD is non-nil, jump to the previous block.
  21148. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21149. (interactive "p")
  21150. (let ((re (or block-regexp org-block-regexp))
  21151. (re-search-fn (or (and backward 're-search-backward)
  21152. 're-search-forward)))
  21153. (if (looking-at re) (forward-char 1))
  21154. (condition-case nil
  21155. (funcall re-search-fn re nil nil arg)
  21156. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21157. (goto-char (match-beginning 0)) (org-show-context)))
  21158. (defun org-previous-block (arg &optional block-regexp)
  21159. "Jump to the previous block.
  21160. With a prefix argument ARG, jump backward ARG many source blocks.
  21161. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21162. (interactive "p")
  21163. (org-next-block arg t block-regexp))
  21164. (defun org-forward-element ()
  21165. "Move forward by one element.
  21166. Move to the next element at the same level, when possible."
  21167. (interactive)
  21168. (cond ((eobp) (user-error "Cannot move further down"))
  21169. ((org-with-limited-levels (org-at-heading-p))
  21170. (let ((origin (point)))
  21171. (goto-char (org-end-of-subtree nil t))
  21172. (unless (org-with-limited-levels (org-at-heading-p))
  21173. (goto-char origin)
  21174. (user-error "Cannot move further down"))))
  21175. (t
  21176. (let* ((elem (org-element-at-point))
  21177. (end (org-element-property :end elem))
  21178. (parent (org-element-property :parent elem)))
  21179. (if (and parent (= (org-element-property :contents-end parent) end))
  21180. (goto-char (org-element-property :end parent))
  21181. (goto-char end))))))
  21182. (defun org-backward-element ()
  21183. "Move backward by one element.
  21184. Move to the previous element at the same level, when possible."
  21185. (interactive)
  21186. (cond ((bobp) (user-error "Cannot move further up"))
  21187. ((org-with-limited-levels (org-at-heading-p))
  21188. ;; At a headline, move to the previous one, if any, or stay
  21189. ;; here.
  21190. (let ((origin (point)))
  21191. (org-with-limited-levels (org-backward-heading-same-level 1))
  21192. ;; When current headline has no sibling above, move to its
  21193. ;; parent.
  21194. (when (= (point) origin)
  21195. (or (org-with-limited-levels (org-up-heading-safe))
  21196. (progn (goto-char origin)
  21197. (user-error "Cannot move further up"))))))
  21198. (t
  21199. (let* ((trail (org-element-at-point 'keep-trail))
  21200. (elem (car trail))
  21201. (prev-elem (nth 1 trail))
  21202. (beg (org-element-property :begin elem)))
  21203. (cond
  21204. ;; Move to beginning of current element if point isn't
  21205. ;; there already.
  21206. ((/= (point) beg) (goto-char beg))
  21207. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21208. ((org-before-first-heading-p) (goto-char (point-min)))
  21209. (t (org-back-to-heading)))))))
  21210. (defun org-up-element ()
  21211. "Move to upper element."
  21212. (interactive)
  21213. (if (org-with-limited-levels (org-at-heading-p))
  21214. (unless (org-up-heading-safe) (error "No surrounding element"))
  21215. (let* ((elem (org-element-at-point))
  21216. (parent (org-element-property :parent elem)))
  21217. (if parent (goto-char (org-element-property :begin parent))
  21218. (if (org-with-limited-levels (org-before-first-heading-p))
  21219. (error "No surrounding element")
  21220. (org-with-limited-levels (org-back-to-heading)))))))
  21221. (defvar org-element-greater-elements)
  21222. (defun org-down-element ()
  21223. "Move to inner element."
  21224. (interactive)
  21225. (let ((element (org-element-at-point)))
  21226. (cond
  21227. ((memq (org-element-type element) '(plain-list table))
  21228. (goto-char (org-element-property :contents-begin element))
  21229. (forward-char))
  21230. ((memq (org-element-type element) org-element-greater-elements)
  21231. ;; If contents are hidden, first disclose them.
  21232. (when (org-element-property :hiddenp element) (org-cycle))
  21233. (goto-char (or (org-element-property :contents-begin element)
  21234. (error "No content for this element"))))
  21235. (t (error "No inner element")))))
  21236. (defun org-drag-element-backward ()
  21237. "Move backward element at point."
  21238. (interactive)
  21239. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21240. (let* ((trail (org-element-at-point 'keep-trail))
  21241. (elem (car trail))
  21242. (prev-elem (nth 1 trail)))
  21243. ;; Error out if no previous element or previous element is
  21244. ;; a parent of the current one.
  21245. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21246. (error "Cannot drag element backward")
  21247. (let ((pos (point)))
  21248. (org-element-swap-A-B prev-elem elem)
  21249. (goto-char (+ (org-element-property :begin prev-elem)
  21250. (- pos (org-element-property :begin elem)))))))))
  21251. (defun org-drag-element-forward ()
  21252. "Move forward element at point."
  21253. (interactive)
  21254. (let* ((pos (point))
  21255. (elem (org-element-at-point)))
  21256. (when (= (point-max) (org-element-property :end elem))
  21257. (error "Cannot drag element forward"))
  21258. (goto-char (org-element-property :end elem))
  21259. (let ((next-elem (org-element-at-point)))
  21260. (when (or (org-element-nested-p elem next-elem)
  21261. (and (eq (org-element-type next-elem) 'headline)
  21262. (not (eq (org-element-type elem) 'headline))))
  21263. (goto-char pos)
  21264. (error "Cannot drag element forward"))
  21265. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21266. ;; body's length (without final blanks) and by the length of
  21267. ;; blanks between ELEM and NEXT-ELEM.
  21268. (let ((size-next (- (save-excursion
  21269. (goto-char (org-element-property :end next-elem))
  21270. (skip-chars-backward " \r\t\n")
  21271. (forward-line)
  21272. ;; Small correction if buffer doesn't end
  21273. ;; with a newline character.
  21274. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21275. (org-element-property :begin next-elem)))
  21276. (size-blank (- (org-element-property :end elem)
  21277. (save-excursion
  21278. (goto-char (org-element-property :end elem))
  21279. (skip-chars-backward " \r\t\n")
  21280. (forward-line)
  21281. (point)))))
  21282. (org-element-swap-A-B elem next-elem)
  21283. (goto-char (+ pos size-next size-blank))))))
  21284. (defun org-drag-line-forward (arg)
  21285. "Drag the line at point ARG lines forward."
  21286. (interactive "p")
  21287. (dotimes (n (abs arg))
  21288. (let ((c (current-column)))
  21289. (if (< 0 arg)
  21290. (progn
  21291. (beginning-of-line 2)
  21292. (transpose-lines 1)
  21293. (beginning-of-line 0))
  21294. (transpose-lines 1)
  21295. (beginning-of-line -1))
  21296. (org-move-to-column c))))
  21297. (defun org-drag-line-backward (arg)
  21298. "Drag the line at point ARG lines backward."
  21299. (interactive "p")
  21300. (org-drag-line-forward (- arg)))
  21301. (defun org-mark-element ()
  21302. "Put point at beginning of this element, mark at end.
  21303. Interactively, if this command is repeated or (in Transient Mark
  21304. mode) if the mark is active, it marks the next element after the
  21305. ones already marked."
  21306. (interactive)
  21307. (let (deactivate-mark)
  21308. (if (and (org-called-interactively-p 'any)
  21309. (or (and (eq last-command this-command) (mark t))
  21310. (and transient-mark-mode mark-active)))
  21311. (set-mark
  21312. (save-excursion
  21313. (goto-char (mark))
  21314. (goto-char (org-element-property :end (org-element-at-point)))))
  21315. (let ((element (org-element-at-point)))
  21316. (end-of-line)
  21317. (push-mark (org-element-property :end element) t t)
  21318. (goto-char (org-element-property :begin element))))))
  21319. (defun org-narrow-to-element ()
  21320. "Narrow buffer to current element."
  21321. (interactive)
  21322. (let ((elem (org-element-at-point)))
  21323. (cond
  21324. ((eq (car elem) 'headline)
  21325. (narrow-to-region
  21326. (org-element-property :begin elem)
  21327. (org-element-property :end elem)))
  21328. ((memq (car elem) org-element-greater-elements)
  21329. (narrow-to-region
  21330. (org-element-property :contents-begin elem)
  21331. (org-element-property :contents-end elem)))
  21332. (t
  21333. (narrow-to-region
  21334. (org-element-property :begin elem)
  21335. (org-element-property :end elem))))))
  21336. (defun org-transpose-element ()
  21337. "Transpose current and previous elements, keeping blank lines between.
  21338. Point is moved after both elements."
  21339. (interactive)
  21340. (org-skip-whitespace)
  21341. (let ((end (org-element-property :end (org-element-at-point))))
  21342. (org-drag-element-backward)
  21343. (goto-char end)))
  21344. (defun org-unindent-buffer ()
  21345. "Un-indent the visible part of the buffer.
  21346. Relative indentation (between items, inside blocks, etc.) isn't
  21347. modified."
  21348. (interactive)
  21349. (unless (eq major-mode 'org-mode)
  21350. (error "Cannot un-indent a buffer not in Org mode"))
  21351. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21352. unindent-tree ; For byte-compiler.
  21353. (unindent-tree
  21354. (function
  21355. (lambda (contents)
  21356. (mapc
  21357. (lambda (element)
  21358. (if (memq (org-element-type element) '(headline section))
  21359. (funcall unindent-tree (org-element-contents element))
  21360. (save-excursion
  21361. (save-restriction
  21362. (narrow-to-region
  21363. (org-element-property :begin element)
  21364. (org-element-property :end element))
  21365. (org-do-remove-indentation)))))
  21366. (reverse contents))))))
  21367. (funcall unindent-tree (org-element-contents parse-tree))))
  21368. (defun org-show-subtree ()
  21369. "Show everything after this heading at deeper levels."
  21370. (interactive)
  21371. (outline-flag-region
  21372. (point)
  21373. (save-excursion
  21374. (org-end-of-subtree t t))
  21375. nil))
  21376. (defun org-show-entry ()
  21377. "Show the body directly following this heading.
  21378. Show the heading too, if it is currently invisible."
  21379. (interactive)
  21380. (save-excursion
  21381. (condition-case nil
  21382. (progn
  21383. (org-back-to-heading t)
  21384. (outline-flag-region
  21385. (max (point-min) (1- (point)))
  21386. (save-excursion
  21387. (if (re-search-forward
  21388. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21389. (match-beginning 1)
  21390. (point-max)))
  21391. nil)
  21392. (org-cycle-hide-drawers 'children))
  21393. (error nil))))
  21394. (defun org-make-options-regexp (kwds &optional extra)
  21395. "Make a regular expression for keyword lines."
  21396. (concat
  21397. "^#\\+\\("
  21398. (mapconcat 'regexp-quote kwds "\\|")
  21399. (if extra (concat "\\|" extra))
  21400. "\\):[ \t]*\\(.*\\)"))
  21401. ;; Make isearch reveal the necessary context
  21402. (defun org-isearch-end ()
  21403. "Reveal context after isearch exits."
  21404. (when isearch-success ; only if search was successful
  21405. (if (featurep 'xemacs)
  21406. ;; Under XEmacs, the hook is run in the correct place,
  21407. ;; we directly show the context.
  21408. (org-show-context 'isearch)
  21409. ;; In Emacs the hook runs *before* restoring the overlays.
  21410. ;; So we have to use a one-time post-command-hook to do this.
  21411. ;; (Emacs 22 has a special variable, see function `org-mode')
  21412. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21413. isearch-mode-end-hook-quit)
  21414. ;; Only when the isearch was not quitted.
  21415. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21416. 'append 'local)))
  21417. (org-fix-ellipsis-at-bol)))
  21418. (defun org-isearch-post-command ()
  21419. "Remove self from hook, and show context."
  21420. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21421. (org-show-context 'isearch))
  21422. ;;;; Integration with and fixes for other packages
  21423. ;;; Imenu support
  21424. (defvar org-imenu-markers nil
  21425. "All markers currently used by Imenu.")
  21426. (make-variable-buffer-local 'org-imenu-markers)
  21427. (defun org-imenu-new-marker (&optional pos)
  21428. "Return a new marker for use by Imenu, and remember the marker."
  21429. (let ((m (make-marker)))
  21430. (move-marker m (or pos (point)))
  21431. (push m org-imenu-markers)
  21432. m))
  21433. (defun org-imenu-get-tree ()
  21434. "Produce the index for Imenu."
  21435. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21436. (setq org-imenu-markers nil)
  21437. (let* ((n org-imenu-depth)
  21438. (re (concat "^" (org-get-limited-outline-regexp)))
  21439. (subs (make-vector (1+ n) nil))
  21440. (last-level 0)
  21441. m level head0 head)
  21442. (save-excursion
  21443. (save-restriction
  21444. (widen)
  21445. (goto-char (point-max))
  21446. (while (re-search-backward re nil t)
  21447. (setq level (org-reduced-level (funcall outline-level)))
  21448. (when (and (<= level n)
  21449. (looking-at org-complex-heading-regexp)
  21450. (setq head0 (org-match-string-no-properties 4)))
  21451. (setq head (org-link-display-format head0)
  21452. m (org-imenu-new-marker))
  21453. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21454. (if (>= level last-level)
  21455. (push (cons head m) (aref subs level))
  21456. (push (cons head (aref subs (1+ level))) (aref subs level))
  21457. (loop for i from (1+ level) to n do (aset subs i nil)))
  21458. (setq last-level level)))))
  21459. (aref subs 1)))
  21460. (eval-after-load "imenu"
  21461. '(progn
  21462. (add-hook 'imenu-after-jump-hook
  21463. (lambda ()
  21464. (if (derived-mode-p 'org-mode)
  21465. (org-show-context 'org-goto))))))
  21466. (defun org-link-display-format (link)
  21467. "Replace a link with its the description.
  21468. If there is no description, use the link target."
  21469. (save-match-data
  21470. (if (string-match org-bracket-link-analytic-regexp link)
  21471. (replace-match (if (match-end 5)
  21472. (match-string 5 link)
  21473. (concat (match-string 1 link)
  21474. (match-string 3 link)))
  21475. nil t link)
  21476. link)))
  21477. (defun org-toggle-link-display ()
  21478. "Toggle the literal or descriptive display of links."
  21479. (interactive)
  21480. (if org-descriptive-links
  21481. (progn (org-remove-from-invisibility-spec '(org-link))
  21482. (org-restart-font-lock)
  21483. (setq org-descriptive-links nil))
  21484. (progn (add-to-invisibility-spec '(org-link))
  21485. (org-restart-font-lock)
  21486. (setq org-descriptive-links t))))
  21487. ;; Speedbar support
  21488. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21489. "Overlay marking the agenda restriction line in speedbar.")
  21490. (overlay-put org-speedbar-restriction-lock-overlay
  21491. 'face 'org-agenda-restriction-lock)
  21492. (overlay-put org-speedbar-restriction-lock-overlay
  21493. 'help-echo "Agendas are currently limited to this item.")
  21494. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21495. (defun org-speedbar-set-agenda-restriction ()
  21496. "Restrict future agenda commands to the location at point in speedbar.
  21497. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21498. (interactive)
  21499. (require 'org-agenda)
  21500. (let (p m tp np dir txt)
  21501. (cond
  21502. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21503. 'org-imenu t))
  21504. (setq m (get-text-property p 'org-imenu-marker))
  21505. (with-current-buffer (marker-buffer m)
  21506. (goto-char m)
  21507. (org-agenda-set-restriction-lock 'subtree)))
  21508. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21509. 'speedbar-function 'speedbar-find-file))
  21510. (setq tp (previous-single-property-change
  21511. (1+ p) 'speedbar-function)
  21512. np (next-single-property-change
  21513. tp 'speedbar-function)
  21514. dir (speedbar-line-directory)
  21515. txt (buffer-substring-no-properties (or tp (point-min))
  21516. (or np (point-max))))
  21517. (with-current-buffer (find-file-noselect
  21518. (let ((default-directory dir))
  21519. (expand-file-name txt)))
  21520. (unless (derived-mode-p 'org-mode)
  21521. (error "Cannot restrict to non-Org-mode file"))
  21522. (org-agenda-set-restriction-lock 'file)))
  21523. (t (error "Don't know how to restrict Org-mode's agenda")))
  21524. (move-overlay org-speedbar-restriction-lock-overlay
  21525. (point-at-bol) (point-at-eol))
  21526. (setq current-prefix-arg nil)
  21527. (org-agenda-maybe-redo)))
  21528. (eval-after-load "speedbar"
  21529. '(progn
  21530. (speedbar-add-supported-extension ".org")
  21531. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21532. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21533. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21534. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21535. (add-hook 'speedbar-visiting-tag-hook
  21536. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21537. ;;; Fixes and Hacks for problems with other packages
  21538. ;; Make flyspell not check words in links, to not mess up our keymap
  21539. (defvar org-element-affiliated-keywords) ; From org-element.el
  21540. (defvar org-element-block-name-alist) ; From org-element.el
  21541. (defun org-mode-flyspell-verify ()
  21542. "Don't let flyspell put overlays at active buttons, or on
  21543. {todo,all-time,additional-option-like}-keywords."
  21544. (require 'org-element) ; For `org-element-affiliated-keywords'
  21545. (let ((pos (max (1- (point)) (point-min)))
  21546. (word (thing-at-point 'word)))
  21547. (and (not (get-text-property pos 'keymap))
  21548. (not (get-text-property pos 'org-no-flyspell))
  21549. (not (member word org-todo-keywords-1))
  21550. (not (member word org-all-time-keywords))
  21551. (not (member word org-options-keywords))
  21552. (not (member word (mapcar 'car org-startup-options)))
  21553. (not (member-ignore-case word org-element-affiliated-keywords))
  21554. (not (member-ignore-case word (org-get-export-keywords)))
  21555. (not (member-ignore-case
  21556. word (mapcar 'car org-element-block-name-alist)))
  21557. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21558. (defun org-remove-flyspell-overlays-in (beg end)
  21559. "Remove flyspell overlays in region."
  21560. (and (org-bound-and-true-p flyspell-mode)
  21561. (fboundp 'flyspell-delete-region-overlays)
  21562. (flyspell-delete-region-overlays beg end))
  21563. (add-text-properties beg end '(org-no-flyspell t)))
  21564. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21565. (eval-after-load "bookmark"
  21566. '(if (boundp 'bookmark-after-jump-hook)
  21567. ;; We can use the hook
  21568. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21569. ;; Hook not available, use advice
  21570. (defadvice bookmark-jump (after org-make-visible activate)
  21571. "Make the position visible."
  21572. (org-bookmark-jump-unhide))))
  21573. ;; Make sure saveplace shows the location if it was hidden
  21574. (eval-after-load "saveplace"
  21575. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21576. "Make the position visible."
  21577. (org-bookmark-jump-unhide)))
  21578. ;; Make sure ecb shows the location if it was hidden
  21579. (eval-after-load "ecb"
  21580. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21581. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21582. (if (derived-mode-p 'org-mode)
  21583. (org-show-context))))
  21584. (defun org-bookmark-jump-unhide ()
  21585. "Unhide the current position, to show the bookmark location."
  21586. (and (derived-mode-p 'org-mode)
  21587. (or (outline-invisible-p)
  21588. (save-excursion (goto-char (max (point-min) (1- (point))))
  21589. (outline-invisible-p)))
  21590. (org-show-context 'bookmark-jump)))
  21591. ;; Make session.el ignore our circular variable
  21592. (eval-after-load "session"
  21593. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21594. ;;;; Finish up
  21595. (provide 'org)
  21596. (run-hooks 'org-load-hook)
  21597. ;;; org.el ends here