org.el 893 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625
  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: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (load "org-loaddefs.el" t t t)
  73. (require 'org-macs)
  74. (require 'org-compat)
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches a headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (org-defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (org-defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  106. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "ox-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-table-set-constants "org-table" ())
  114. (declare-function org-id-get-create "org-id" (&optional force))
  115. (declare-function org-id-find-id-file "org-id" (id))
  116. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  117. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  118. (declare-function org-agenda-redo "org-agenda" (&optional all))
  119. (declare-function org-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. (declare-function org-element--parse-objects "org-element"
  124. (beg end acc restriction))
  125. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  126. (declare-function org-element-contents "org-element" (element))
  127. (declare-function org-element-context "org-element" (&optional element))
  128. (declare-function org-element-interpret-data "org-element"
  129. (data &optional parent))
  130. (declare-function org-element-map "org-element"
  131. (data types fun &optional info first-match no-recursion))
  132. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  133. (declare-function org-element-parse-buffer "org-element"
  134. (&optional granularity visible-only))
  135. (declare-function org-element-property "org-element" (property element))
  136. (declare-function org-element-put-property "org-element"
  137. (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element--parse-objects "org-element"
  140. (beg end acc restriction))
  141. (declare-function org-element-parse-buffer "org-element"
  142. (&optional granularity visible-only))
  143. (declare-function org-element-restriction "org-element" (element))
  144. (declare-function org-element-type "org-element" (element))
  145. ;; load languages based on value of `org-babel-load-languages'
  146. (defvar org-babel-load-languages)
  147. ;;;###autoload
  148. (defun org-babel-do-load-languages (sym value)
  149. "Load the languages defined in `org-babel-load-languages'."
  150. (set-default sym value)
  151. (mapc (lambda (pair)
  152. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  153. (if active
  154. (progn
  155. (require (intern (concat "ob-" lang))))
  156. (progn
  157. (funcall 'fmakunbound
  158. (intern (concat "org-babel-execute:" lang)))
  159. (funcall 'fmakunbound
  160. (intern (concat "org-babel-expand-body:" lang)))))))
  161. org-babel-load-languages))
  162. ;;;###autoload
  163. (defun org-babel-load-file (file &optional compile)
  164. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  165. This function exports the source code using `org-babel-tangle'
  166. and then loads the resulting file using `load-file'. With prefix
  167. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  168. file to byte-code before it is loaded."
  169. (interactive "fFile to load: \nP")
  170. (require 'ob-core)
  171. (let* ((age (lambda (file)
  172. (float-time
  173. (time-subtract (current-time)
  174. (nth 5 (or (file-attributes (file-truename file))
  175. (file-attributes file)))))))
  176. (base-name (file-name-sans-extension file))
  177. (exported-file (concat base-name ".el")))
  178. ;; tangle if the org-mode file is newer than the elisp file
  179. (unless (and (file-exists-p exported-file)
  180. (> (funcall age file) (funcall age exported-file)))
  181. (org-babel-tangle-file file exported-file "emacs-lisp"))
  182. (message "%s %s"
  183. (if compile
  184. (progn (byte-compile-file exported-file 'load)
  185. "Compiled and loaded")
  186. (progn (load-file exported-file) "Loaded"))
  187. exported-file)))
  188. (defcustom org-babel-load-languages '((emacs-lisp . t))
  189. "Languages which can be evaluated in Org-mode buffers.
  190. This list can be used to load support for any of the languages
  191. below, note that each language will depend on a different set of
  192. system executables and/or Emacs modes. When a language is
  193. \"loaded\", then code blocks in that language can be evaluated
  194. with `org-babel-execute-src-block' bound by default to C-c
  195. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  196. be set to remove code block evaluation from the C-c C-c
  197. keybinding. By default only Emacs Lisp (which has no
  198. requirements) is loaded."
  199. :group 'org-babel
  200. :set 'org-babel-do-load-languages
  201. :version "24.1"
  202. :type '(alist :tag "Babel Languages"
  203. :key-type
  204. (choice
  205. (const :tag "Awk" awk)
  206. (const :tag "C" C)
  207. (const :tag "R" R)
  208. (const :tag "Asymptote" asymptote)
  209. (const :tag "Calc" calc)
  210. (const :tag "Clojure" clojure)
  211. (const :tag "CSS" css)
  212. (const :tag "Ditaa" ditaa)
  213. (const :tag "Dot" dot)
  214. (const :tag "Emacs Lisp" emacs-lisp)
  215. (const :tag "Fortran" fortran)
  216. (const :tag "Gnuplot" gnuplot)
  217. (const :tag "Haskell" haskell)
  218. (const :tag "IO" io)
  219. (const :tag "Java" java)
  220. (const :tag "Javascript" js)
  221. (const :tag "LaTeX" latex)
  222. (const :tag "Ledger" ledger)
  223. (const :tag "Lilypond" lilypond)
  224. (const :tag "Lisp" lisp)
  225. (const :tag "Makefile" makefile)
  226. (const :tag "Maxima" maxima)
  227. (const :tag "Matlab" matlab)
  228. (const :tag "Mscgen" mscgen)
  229. (const :tag "Ocaml" ocaml)
  230. (const :tag "Octave" octave)
  231. (const :tag "Org" org)
  232. (const :tag "Perl" perl)
  233. (const :tag "Pico Lisp" picolisp)
  234. (const :tag "PlantUML" plantuml)
  235. (const :tag "Python" python)
  236. (const :tag "Ruby" ruby)
  237. (const :tag "Sass" sass)
  238. (const :tag "Scala" scala)
  239. (const :tag "Scheme" scheme)
  240. (const :tag "Screen" screen)
  241. (const :tag "Shell Script" sh)
  242. (const :tag "Shen" shen)
  243. (const :tag "Sql" sql)
  244. (const :tag "Sqlite" sqlite))
  245. :value-type (boolean :tag "Activate" :value t)))
  246. ;;;; Customization variables
  247. (defcustom org-clone-delete-id nil
  248. "Remove ID property of clones of a subtree.
  249. When non-nil, clones of a subtree don't inherit the ID property.
  250. Otherwise they inherit the ID property with a new unique
  251. identifier."
  252. :type 'boolean
  253. :version "24.1"
  254. :group 'org-id)
  255. ;;; Version
  256. (org-check-version)
  257. ;;;###autoload
  258. (defun org-version (&optional here full message)
  259. "Show the org-mode version in the echo area.
  260. With prefix argument HERE, insert it at point.
  261. When FULL is non-nil, use a verbose version string.
  262. When MESSAGE is non-nil, display a message with the version."
  263. (interactive "P")
  264. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  265. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  266. (load-suffixes (list ".el"))
  267. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  268. (org-trash (or
  269. (and (fboundp 'org-release) (fboundp 'org-git-version))
  270. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  271. (load-suffixes save-load-suffixes)
  272. (org-version (org-release))
  273. (git-version (org-git-version))
  274. (version (format "Org-mode version %s (%s @ %s)"
  275. org-version
  276. git-version
  277. (if org-install-dir
  278. (if (string= org-dir org-install-dir)
  279. org-install-dir
  280. (concat "mixed installation! " org-install-dir " and " org-dir))
  281. "org-loaddefs.el can not be found!")))
  282. (_version (if full version org-version)))
  283. (if (org-called-interactively-p 'interactive)
  284. (if here
  285. (insert version)
  286. (message version))
  287. (if message (message _version))
  288. _version)))
  289. (defconst org-version (org-version))
  290. ;;; Compatibility constants
  291. ;;; The custom variables
  292. (defgroup org nil
  293. "Outline-based notes management and organizer."
  294. :tag "Org"
  295. :group 'outlines
  296. :group 'calendar)
  297. (defcustom org-mode-hook nil
  298. "Mode hook for Org-mode, run after the mode was turned on."
  299. :group 'org
  300. :type 'hook)
  301. (defcustom org-load-hook nil
  302. "Hook that is run after org.el has been loaded."
  303. :group 'org
  304. :type 'hook)
  305. (defcustom org-log-buffer-setup-hook nil
  306. "Hook that is run after an Org log buffer is created."
  307. :group 'org
  308. :version "24.1"
  309. :type 'hook)
  310. (defvar org-modules) ; defined below
  311. (defvar org-modules-loaded nil
  312. "Have the modules been loaded already?")
  313. (defun org-load-modules-maybe (&optional force)
  314. "Load all extensions listed in `org-modules'."
  315. (when (or force (not org-modules-loaded))
  316. (mapc (lambda (ext)
  317. (condition-case nil (require ext)
  318. (error (message "Problems while trying to load feature `%s'" ext))))
  319. org-modules)
  320. (setq org-modules-loaded t)))
  321. (defun org-set-modules (var value)
  322. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  323. (set var value)
  324. (when (featurep 'org)
  325. (org-load-modules-maybe 'force)))
  326. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  327. "Modules that should always be loaded together with org.el.
  328. If a description starts with <C>, the file is not part of Emacs
  329. and loading it will require that you have downloaded and properly
  330. installed the Org mode distribution.
  331. You can also use this system to load external packages (i.e. neither Org
  332. core modules, nor modules from the CONTRIB directory). Just add symbols
  333. to the end of the list. If the package is called org-xyz.el, then you need
  334. to add the symbol `xyz', and the package must have a call to:
  335. \(provide 'org-xyz)
  336. For export specific modules, see also `org-export-backends'."
  337. :group 'org
  338. :set 'org-set-modules
  339. :version "24.4"
  340. :package-version '(Org . "8.0")
  341. :type
  342. '(set :greedy t
  343. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  344. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  345. (const :tag " crypt: Encryption of subtrees" org-crypt)
  346. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  347. (const :tag " docview: Links to doc-view buffers" org-docview)
  348. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  349. (const :tag " habit: Track your consistency with habits" org-habit)
  350. (const :tag " id: Global IDs for identifying entries" org-id)
  351. (const :tag " info: Links to Info nodes" org-info)
  352. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  353. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  354. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  355. (const :tag " mouse: Additional mouse support" org-mouse)
  356. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  357. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  358. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  359. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  360. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  361. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  362. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  363. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  364. (const :tag "C collector: Collect properties into tables" org-collector)
  365. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  366. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  367. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  368. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  369. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  370. (const :tag "C eval: Include command output as text" org-eval)
  371. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  372. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  373. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  374. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  375. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  376. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  377. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  378. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  379. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  380. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  381. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  382. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  383. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  384. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  385. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  386. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  387. (const :tag "C registry: A registry for Org-mode links" org-registry)
  388. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  389. (const :tag "C secretary: Team management with org-mode" org-secretary)
  390. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  391. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  392. (const :tag "C track: Keep up with Org-mode development" org-track)
  393. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  394. (const :tag "C vm: Links to VM folders/messages" org-vm)
  395. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  396. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  397. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  398. (defvar org-export-registered-backends) ; From ox.el
  399. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  400. (defcustom org-export-backends '(ascii html icalendar latex)
  401. "List of export back-ends that should be always available.
  402. If a description starts with <C>, the file is not part of Emacs
  403. and loading it will require that you have downloaded and properly
  404. installed the Org mode distribution.
  405. Unlike to `org-modules', libraries in this list will not be
  406. loaded along with Org, but only once the export framework is
  407. needed.
  408. This variable needs to be set before org.el is loaded. If you
  409. need to make a change while Emacs is running, use the customize
  410. interface or run the following code, where VALUE stands for the
  411. new value of the variable, after updating it:
  412. \(progn
  413. \(setq org-export-registered-backends
  414. \(org-remove-if-not
  415. \(lambda (backend)
  416. \(or (memq backend val)
  417. \(catch 'parentp
  418. \(mapc
  419. \(lambda (b)
  420. \(and (org-export-derived-backend-p b (car backend))
  421. \(throw 'parentp t)))
  422. val)
  423. nil)))
  424. org-export-registered-backends))
  425. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  426. \(dolist (backend val)
  427. \(cond
  428. \((not (load (format \"ox-%s\" backend) t t))
  429. \(message \"Problems while trying to load export back-end `%s'\"
  430. backend))
  431. \((not (memq backend new-list)) (push backend new-list))))
  432. \(set-default var new-list)))
  433. Adding a back-end to this list will also pull the back-end it
  434. depends on, if any."
  435. :group 'org
  436. :group 'org-export
  437. :version "24.4"
  438. :package-version '(Org . "8.0")
  439. :initialize 'custom-initialize-set
  440. :set (lambda (var val)
  441. (if (not (featurep 'ox)) (set-default var val)
  442. ;; Any back-end not required anymore (not present in VAL and not
  443. ;; a parent of any back-end in the new value) is removed from the
  444. ;; list of registered back-ends.
  445. (setq org-export-registered-backends
  446. (org-remove-if-not
  447. (lambda (backend)
  448. (or (memq backend val)
  449. (catch 'parentp
  450. (mapc
  451. (lambda (b)
  452. (and (org-export-derived-backend-p b (car backend))
  453. (throw 'parentp t)))
  454. val)
  455. nil)))
  456. org-export-registered-backends))
  457. ;; Now build NEW-LIST of both new back-ends and required
  458. ;; parents.
  459. (let ((new-list (mapcar 'car org-export-registered-backends)))
  460. (dolist (backend val)
  461. (cond
  462. ((not (load (format "ox-%s" backend) t t))
  463. (message "Problems while trying to load export back-end `%s'"
  464. backend))
  465. ((not (memq backend new-list)) (push backend new-list))))
  466. ;; Set VAR to that list with fixed dependencies.
  467. (set-default var new-list))))
  468. :type '(set :greedy t
  469. (const :tag " ascii Export buffer to ASCII format" ascii)
  470. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  471. (const :tag " html Export buffer to HTML format" html)
  472. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  473. (const :tag " latex Export buffer to LaTeX format" latex)
  474. (const :tag " man Export buffer to MAN format" man)
  475. (const :tag " md Export buffer to Markdown format" md)
  476. (const :tag " odt Export buffer to ODT format" odt)
  477. (const :tag " org Export buffer to Org format" org)
  478. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  479. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  480. (const :tag "C deck Export buffer to deck.js presentations" deck)
  481. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  482. (const :tag "C groff Export buffer to Groff format" groff)
  483. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  484. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  485. (const :tag "C s5 Export buffer to s5 presentations" s5)
  486. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  487. (eval-after-load 'ox
  488. '(mapc
  489. (lambda (backend)
  490. (condition-case nil (require (intern (format "ox-%s" backend)))
  491. (error (message "Problems while trying to load export back-end `%s'"
  492. backend))))
  493. org-export-backends))
  494. (defcustom org-support-shift-select nil
  495. "Non-nil means make shift-cursor commands select text when possible.
  496. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  497. start selecting a region, or enlarge regions started in this way.
  498. In Org-mode, in special contexts, these same keys are used for
  499. other purposes, important enough to compete with shift selection.
  500. Org tries to balance these needs by supporting `shift-select-mode'
  501. outside these special contexts, under control of this variable.
  502. The default of this variable is nil, to avoid confusing behavior. Shifted
  503. cursor keys will then execute Org commands in the following contexts:
  504. - on a headline, changing TODO state (left/right) and priority (up/down)
  505. - on a time stamp, changing the time
  506. - in a plain list item, changing the bullet type
  507. - in a property definition line, switching between allowed values
  508. - in the BEGIN line of a clock table (changing the time block).
  509. Outside these contexts, the commands will throw an error.
  510. When this variable is t and the cursor is not in a special
  511. context, Org-mode will support shift-selection for making and
  512. enlarging regions. To make this more effective, the bullet
  513. cycling will no longer happen anywhere in an item line, but only
  514. if the cursor is exactly on the bullet.
  515. If you set this variable to the symbol `always', then the keys
  516. will not be special in headlines, property lines, and item lines,
  517. to make shift selection work there as well. If this is what you
  518. want, you can use the following alternative commands: `C-c C-t'
  519. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  520. can be used to switch TODO sets, `C-c -' to cycle item bullet
  521. types, and properties can be edited by hand or in column view.
  522. However, when the cursor is on a timestamp, shift-cursor commands
  523. will still edit the time stamp - this is just too good to give up.
  524. XEmacs user should have this variable set to nil, because
  525. `shift-select-mode' is in Emacs 23 or later only."
  526. :group 'org
  527. :type '(choice
  528. (const :tag "Never" nil)
  529. (const :tag "When outside special context" t)
  530. (const :tag "Everywhere except timestamps" always)))
  531. (defcustom org-loop-over-headlines-in-active-region nil
  532. "Shall some commands act upon headlines in the active region?
  533. When set to `t', some commands will be performed in all headlines
  534. within the active region.
  535. When set to `start-level', some commands will be performed in all
  536. headlines within the active region, provided that these headlines
  537. are of the same level than the first one.
  538. When set to a string, those commands will be performed on the
  539. matching headlines within the active region. Such string must be
  540. a tags/property/todo match as it is used in the agenda tags view.
  541. The list of commands is: `org-schedule', `org-deadline',
  542. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  543. `org-archive-to-archive-sibling'. The archiving commands skip
  544. already archived entries."
  545. :type '(choice (const :tag "Don't loop" nil)
  546. (const :tag "All headlines in active region" t)
  547. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  548. (string :tag "Tags/Property/Todo matcher"))
  549. :version "24.1"
  550. :group 'org-todo
  551. :group 'org-archive)
  552. (defgroup org-startup nil
  553. "Options concerning startup of Org-mode."
  554. :tag "Org Startup"
  555. :group 'org)
  556. (defcustom org-startup-folded t
  557. "Non-nil means entering Org-mode will switch to OVERVIEW.
  558. This can also be configured on a per-file basis by adding one of
  559. the following lines anywhere in the buffer:
  560. #+STARTUP: fold (or `overview', this is equivalent)
  561. #+STARTUP: nofold (or `showall', this is equivalent)
  562. #+STARTUP: content
  563. #+STARTUP: showeverything
  564. By default, this option is ignored when Org opens agenda files
  565. for the first time. If you want the agenda to honor the startup
  566. option, set `org-agenda-inhibit-startup' to nil."
  567. :group 'org-startup
  568. :type '(choice
  569. (const :tag "nofold: show all" nil)
  570. (const :tag "fold: overview" t)
  571. (const :tag "content: all headlines" content)
  572. (const :tag "show everything, even drawers" showeverything)))
  573. (defcustom org-startup-truncated t
  574. "Non-nil means entering Org-mode will set `truncate-lines'.
  575. This is useful since some lines containing links can be very long and
  576. uninteresting. Also tables look terrible when wrapped."
  577. :group 'org-startup
  578. :type 'boolean)
  579. (defcustom org-startup-indented nil
  580. "Non-nil means turn on `org-indent-mode' on startup.
  581. This can also be configured on a per-file basis by adding one of
  582. the following lines anywhere in the buffer:
  583. #+STARTUP: indent
  584. #+STARTUP: noindent"
  585. :group 'org-structure
  586. :type '(choice
  587. (const :tag "Not" nil)
  588. (const :tag "Globally (slow on startup in large files)" t)))
  589. (defcustom org-use-sub-superscripts t
  590. "Non-nil means interpret \"_\" and \"^\" for display.
  591. When this option is turned on, you can use TeX-like syntax for sub- and
  592. superscripts. Several characters after \"_\" or \"^\" will be
  593. considered as a single item - so grouping with {} is normally not
  594. needed. For example, the following things will be parsed as single
  595. sub- or superscripts.
  596. 10^24 or 10^tau several digits will be considered 1 item.
  597. 10^-12 or 10^-tau a leading sign with digits or a word
  598. x^2-y^3 will be read as x^2 - y^3, because items are
  599. terminated by almost any nonword/nondigit char.
  600. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  601. Still, ambiguity is possible - so when in doubt use {} to enclose
  602. the sub/superscript. If you set this variable to the symbol
  603. `{}', the braces are *required* in order to trigger
  604. interpretations as sub/superscript. This can be helpful in
  605. documents that need \"_\" frequently in plain text."
  606. :group 'org-startup
  607. :version "24.1"
  608. :type '(choice
  609. (const :tag "Always interpret" t)
  610. (const :tag "Only with braces" {})
  611. (const :tag "Never interpret" nil)))
  612. (defcustom org-startup-with-beamer-mode nil
  613. "Non-nil means turn on `org-beamer-mode' on startup.
  614. This can also be configured on a per-file basis by adding one of
  615. the following lines anywhere in the buffer:
  616. #+STARTUP: beamer"
  617. :group 'org-startup
  618. :version "24.1"
  619. :type 'boolean)
  620. (defcustom org-startup-align-all-tables nil
  621. "Non-nil means align all tables when visiting a file.
  622. This is useful when the column width in tables is forced with <N> cookies
  623. in table fields. Such tables will look correct only after the first re-align.
  624. This can also be configured on a per-file basis by adding one of
  625. the following lines anywhere in the buffer:
  626. #+STARTUP: align
  627. #+STARTUP: noalign"
  628. :group 'org-startup
  629. :type 'boolean)
  630. (defcustom org-startup-with-inline-images nil
  631. "Non-nil means show inline images when loading a new Org file.
  632. This can also be configured on a per-file basis by adding one of
  633. the following lines anywhere in the buffer:
  634. #+STARTUP: inlineimages
  635. #+STARTUP: noinlineimages"
  636. :group 'org-startup
  637. :version "24.1"
  638. :type 'boolean)
  639. (defcustom org-startup-with-latex-preview nil
  640. "Non-nil means preview LaTeX fragments when loading a new Org file.
  641. This can also be configured on a per-file basis by adding one of
  642. the followinglines anywhere in the buffer:
  643. #+STARTUP: latexpreview
  644. #+STARTUP: nolatexpreview"
  645. :group 'org-startup
  646. :version "24.4"
  647. :package-version '(Org . "8.0")
  648. :type 'boolean)
  649. (defcustom org-insert-mode-line-in-empty-file nil
  650. "Non-nil means insert the first line setting Org-mode in empty files.
  651. When the function `org-mode' is called interactively in an empty file, this
  652. normally means that the file name does not automatically trigger Org-mode.
  653. To ensure that the file will always be in Org-mode in the future, a
  654. line enforcing Org-mode will be inserted into the buffer, if this option
  655. has been set."
  656. :group 'org-startup
  657. :type 'boolean)
  658. (defcustom org-replace-disputed-keys nil
  659. "Non-nil means use alternative key bindings for some keys.
  660. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  661. These keys are also used by other packages like shift-selection-mode'
  662. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  663. If you want to use Org-mode together with one of these other modes,
  664. or more generally if you would like to move some Org-mode commands to
  665. other keys, set this variable and configure the keys with the variable
  666. `org-disputed-keys'.
  667. This option is only relevant at load-time of Org-mode, and must be set
  668. *before* org.el is loaded. Changing it requires a restart of Emacs to
  669. become effective."
  670. :group 'org-startup
  671. :type 'boolean)
  672. (defcustom org-use-extra-keys nil
  673. "Non-nil means use extra key sequence definitions for certain commands.
  674. This happens automatically if you run XEmacs or if `window-system'
  675. is nil. This variable lets you do the same manually. You must
  676. set it before loading org.
  677. Example: on Carbon Emacs 22 running graphically, with an external
  678. keyboard on a Powerbook, the default way of setting M-left might
  679. not work for either Alt or ESC. Setting this variable will make
  680. it work for ESC."
  681. :group 'org-startup
  682. :type 'boolean)
  683. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  684. (defcustom org-disputed-keys
  685. '(([(shift up)] . [(meta p)])
  686. ([(shift down)] . [(meta n)])
  687. ([(shift left)] . [(meta -)])
  688. ([(shift right)] . [(meta +)])
  689. ([(control shift right)] . [(meta shift +)])
  690. ([(control shift left)] . [(meta shift -)]))
  691. "Keys for which Org-mode and other modes compete.
  692. This is an alist, cars are the default keys, second element specifies
  693. the alternative to use when `org-replace-disputed-keys' is t.
  694. Keys can be specified in any syntax supported by `define-key'.
  695. The value of this option takes effect only at Org-mode's startup,
  696. therefore you'll have to restart Emacs to apply it after changing."
  697. :group 'org-startup
  698. :type 'alist)
  699. (defun org-key (key)
  700. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  701. Or return the original if not disputed.
  702. Also apply the translations defined in `org-xemacs-key-equivalents'."
  703. (when org-replace-disputed-keys
  704. (let* ((nkey (key-description key))
  705. (x (org-find-if (lambda (x)
  706. (equal (key-description (car x)) nkey))
  707. org-disputed-keys)))
  708. (setq key (if x (cdr x) key))))
  709. (when (featurep 'xemacs)
  710. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  711. key)
  712. (defun org-find-if (predicate seq)
  713. (catch 'exit
  714. (while seq
  715. (if (funcall predicate (car seq))
  716. (throw 'exit (car seq))
  717. (pop seq)))))
  718. (defun org-defkey (keymap key def)
  719. "Define a key, possibly translated, as returned by `org-key'."
  720. (define-key keymap (org-key key) def))
  721. (defcustom org-ellipsis nil
  722. "The ellipsis to use in the Org-mode outline.
  723. When nil, just use the standard three dots. When a string, use that instead,
  724. When a face, use the standard 3 dots, but with the specified face.
  725. The change affects only Org-mode (which will then use its own display table).
  726. Changing this requires executing `M-x org-mode' in a buffer to become
  727. effective."
  728. :group 'org-startup
  729. :type '(choice (const :tag "Default" nil)
  730. (face :tag "Face" :value org-warning)
  731. (string :tag "String" :value "...#")))
  732. (defvar org-display-table nil
  733. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  734. (defgroup org-keywords nil
  735. "Keywords in Org-mode."
  736. :tag "Org Keywords"
  737. :group 'org)
  738. (defcustom org-deadline-string "DEADLINE:"
  739. "String to mark deadline entries.
  740. A deadline is this string, followed by a time stamp. Should be a word,
  741. terminated by a colon. You can insert a schedule keyword and
  742. a timestamp with \\[org-deadline].
  743. Changes become only effective after restarting Emacs."
  744. :group 'org-keywords
  745. :type 'string)
  746. (defcustom org-scheduled-string "SCHEDULED:"
  747. "String to mark scheduled TODO entries.
  748. A schedule is this string, followed by a time stamp. Should be a word,
  749. terminated by a colon. You can insert a schedule keyword and
  750. a timestamp with \\[org-schedule].
  751. Changes become only effective after restarting Emacs."
  752. :group 'org-keywords
  753. :type 'string)
  754. (defcustom org-closed-string "CLOSED:"
  755. "String used as the prefix for timestamps logging closing a TODO entry."
  756. :group 'org-keywords
  757. :type 'string)
  758. (defcustom org-clock-string "CLOCK:"
  759. "String used as prefix for timestamps clocking work hours on an item."
  760. :group 'org-keywords
  761. :type 'string)
  762. (defcustom org-closed-keep-when-no-todo nil
  763. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  764. :group 'org-todo
  765. :group 'org-keywords
  766. :version "24.4"
  767. :package-version '(Org . "8.0")
  768. :type 'boolean)
  769. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  770. org-scheduled-string "\\|"
  771. org-deadline-string "\\|"
  772. org-closed-string "\\|"
  773. org-clock-string "\\)")
  774. "Matches a line with planning or clock info.")
  775. (defcustom org-comment-string "COMMENT"
  776. "Entries starting with this keyword will never be exported.
  777. An entry can be toggled between COMMENT and normal with
  778. \\[org-toggle-comment].
  779. Changes become only effective after restarting Emacs."
  780. :group 'org-keywords
  781. :type 'string)
  782. (defcustom org-quote-string "QUOTE"
  783. "Entries starting with this keyword will be exported in fixed-width font.
  784. Quoting applies only to the text in the entry following the headline, and does
  785. not extend beyond the next headline, even if that is lower level.
  786. An entry can be toggled between QUOTE and normal with
  787. \\[org-toggle-fixed-width-section]."
  788. :group 'org-keywords
  789. :type 'string)
  790. (defconst org-repeat-re
  791. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  792. "Regular expression for specifying repeated events.
  793. After a match, group 1 contains the repeat expression.")
  794. (defgroup org-structure nil
  795. "Options concerning the general structure of Org-mode files."
  796. :tag "Org Structure"
  797. :group 'org)
  798. (defgroup org-reveal-location nil
  799. "Options about how to make context of a location visible."
  800. :tag "Org Reveal Location"
  801. :group 'org-structure)
  802. (defconst org-context-choice
  803. '(choice
  804. (const :tag "Always" t)
  805. (const :tag "Never" nil)
  806. (repeat :greedy t :tag "Individual contexts"
  807. (cons
  808. (choice :tag "Context"
  809. (const agenda)
  810. (const org-goto)
  811. (const occur-tree)
  812. (const tags-tree)
  813. (const link-search)
  814. (const mark-goto)
  815. (const bookmark-jump)
  816. (const isearch)
  817. (const default))
  818. (boolean))))
  819. "Contexts for the reveal options.")
  820. (defcustom org-show-hierarchy-above '((default . t))
  821. "Non-nil means show full hierarchy when revealing a location.
  822. Org-mode often shows locations in an org-mode file which might have
  823. been invisible before. When this is set, the hierarchy of headings
  824. above the exposed location is shown.
  825. Turning this off for example for sparse trees makes them very compact.
  826. Instead of t, this can also be an alist specifying this option for different
  827. contexts. Valid contexts are
  828. agenda when exposing an entry from the agenda
  829. org-goto when using the command `org-goto' on key C-c C-j
  830. occur-tree when using the command `org-occur' on key C-c /
  831. tags-tree when constructing a sparse tree based on tags matches
  832. link-search when exposing search matches associated with a link
  833. mark-goto when exposing the jump goal of a mark
  834. bookmark-jump when exposing a bookmark location
  835. isearch when exiting from an incremental search
  836. default default for all contexts not set explicitly"
  837. :group 'org-reveal-location
  838. :type org-context-choice)
  839. (defcustom org-show-following-heading '((default . nil))
  840. "Non-nil means show following heading when revealing a location.
  841. Org-mode often shows locations in an org-mode file which might have
  842. been invisible before. When this is set, the heading following the
  843. match is shown.
  844. Turning this off for example for sparse trees makes them very compact,
  845. but makes it harder to edit the location of the match. In such a case,
  846. use the command \\[org-reveal] to show more context.
  847. Instead of t, this can also be an alist specifying this option for different
  848. contexts. See `org-show-hierarchy-above' for valid contexts."
  849. :group 'org-reveal-location
  850. :type org-context-choice)
  851. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  852. "Non-nil means show all sibling heading when revealing a location.
  853. Org-mode often shows locations in an org-mode file which might have
  854. been invisible before. When this is set, the sibling of the current entry
  855. heading are all made visible. If `org-show-hierarchy-above' is t,
  856. the same happens on each level of the hierarchy above the current entry.
  857. By default this is on for the isearch context, off for all other contexts.
  858. Turning this off for example for sparse trees makes them very compact,
  859. but makes it harder to edit the location of the match. In such a case,
  860. use the command \\[org-reveal] to show more context.
  861. Instead of t, this can also be an alist specifying this option for different
  862. contexts. See `org-show-hierarchy-above' for valid contexts."
  863. :group 'org-reveal-location
  864. :type org-context-choice
  865. :version "24.4"
  866. :package-version '(Org . "8.0"))
  867. (defcustom org-show-entry-below '((default . nil))
  868. "Non-nil means show the entry below a headline when revealing a location.
  869. Org-mode often shows locations in an org-mode file which might have
  870. been invisible before. When this is set, the text below the headline that is
  871. exposed is also shown.
  872. By default this is off for all contexts.
  873. Instead of t, this can also be an alist specifying this option for different
  874. contexts. See `org-show-hierarchy-above' for valid contexts."
  875. :group 'org-reveal-location
  876. :type org-context-choice)
  877. (defcustom org-indirect-buffer-display 'other-window
  878. "How should indirect tree buffers be displayed?
  879. This applies to indirect buffers created with the commands
  880. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  881. Valid values are:
  882. current-window Display in the current window
  883. other-window Just display in another window.
  884. dedicated-frame Create one new frame, and re-use it each time.
  885. new-frame Make a new frame each time. Note that in this case
  886. previously-made indirect buffers are kept, and you need to
  887. kill these buffers yourself."
  888. :group 'org-structure
  889. :group 'org-agenda-windows
  890. :type '(choice
  891. (const :tag "In current window" current-window)
  892. (const :tag "In current frame, other window" other-window)
  893. (const :tag "Each time a new frame" new-frame)
  894. (const :tag "One dedicated frame" dedicated-frame)))
  895. (defcustom org-use-speed-commands nil
  896. "Non-nil means activate single letter commands at beginning of a headline.
  897. This may also be a function to test for appropriate locations where speed
  898. commands should be active."
  899. :group 'org-structure
  900. :type '(choice
  901. (const :tag "Never" nil)
  902. (const :tag "At beginning of headline stars" t)
  903. (function)))
  904. (defcustom org-speed-commands-user nil
  905. "Alist of additional speed commands.
  906. This list will be checked before `org-speed-commands-default'
  907. when the variable `org-use-speed-commands' is non-nil
  908. and when the cursor is at the beginning of a headline.
  909. The car if each entry is a string with a single letter, which must
  910. be assigned to `self-insert-command' in the global map.
  911. The cdr is either a command to be called interactively, a function
  912. to be called, or a form to be evaluated.
  913. An entry that is just a list with a single string will be interpreted
  914. as a descriptive headline that will be added when listing the speed
  915. commands in the Help buffer using the `?' speed command."
  916. :group 'org-structure
  917. :type '(repeat :value ("k" . ignore)
  918. (choice :value ("k" . ignore)
  919. (list :tag "Descriptive Headline" (string :tag "Headline"))
  920. (cons :tag "Letter and Command"
  921. (string :tag "Command letter")
  922. (choice
  923. (function)
  924. (sexp))))))
  925. (defgroup org-cycle nil
  926. "Options concerning visibility cycling in Org-mode."
  927. :tag "Org Cycle"
  928. :group 'org-structure)
  929. (defcustom org-cycle-skip-children-state-if-no-children t
  930. "Non-nil means skip CHILDREN state in entries that don't have any."
  931. :group 'org-cycle
  932. :type 'boolean)
  933. (defcustom org-cycle-max-level nil
  934. "Maximum level which should still be subject to visibility cycling.
  935. Levels higher than this will, for cycling, be treated as text, not a headline.
  936. When `org-odd-levels-only' is set, a value of N in this variable actually
  937. means 2N-1 stars as the limiting headline.
  938. When nil, cycle all levels.
  939. Note that the limiting level of cycling is also influenced by
  940. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  941. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  942. than its value."
  943. :group 'org-cycle
  944. :type '(choice
  945. (const :tag "No limit" nil)
  946. (integer :tag "Maximum level")))
  947. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  948. "Names of drawers. Drawers are not opened by cycling on the headline above.
  949. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  950. this:
  951. :DRAWERNAME:
  952. .....
  953. :END:
  954. The drawer \"PROPERTIES\" is special for capturing properties through
  955. the property API.
  956. Drawers can be defined on the per-file basis with a line like:
  957. #+DRAWERS: HIDDEN STATE PROPERTIES"
  958. :group 'org-structure
  959. :group 'org-cycle
  960. :type '(repeat (string :tag "Drawer Name")))
  961. (defcustom org-hide-block-startup nil
  962. "Non-nil means entering Org-mode will fold all blocks.
  963. This can also be set in on a per-file basis with
  964. #+STARTUP: hideblocks
  965. #+STARTUP: showblocks"
  966. :group 'org-startup
  967. :group 'org-cycle
  968. :type 'boolean)
  969. (defcustom org-cycle-global-at-bob nil
  970. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  971. This makes it possible to do global cycling without having to use S-TAB or
  972. \\[universal-argument] TAB. For this special case to work, the first line
  973. of the buffer must not be a headline -- it may be empty or some other text.
  974. When used in this way, `org-cycle-hook' is disabled temporarily to make
  975. sure the cursor stays at the beginning of the buffer. When this option is
  976. nil, don't do anything special at the beginning of the buffer."
  977. :group 'org-cycle
  978. :type 'boolean)
  979. (defcustom org-cycle-level-after-item/entry-creation t
  980. "Non-nil means cycle entry level or item indentation in new empty entries.
  981. When the cursor is at the end of an empty headline, i.e., with only stars
  982. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  983. and then all possible ancestor states, before returning to the original state.
  984. This makes data entry extremely fast: M-RET to create a new headline,
  985. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  986. When the cursor is at the end of an empty plain list item, one TAB will
  987. make it a subitem, two or more tabs will back up to make this an item
  988. higher up in the item hierarchy."
  989. :group 'org-cycle
  990. :type 'boolean)
  991. (defcustom org-cycle-emulate-tab t
  992. "Where should `org-cycle' emulate TAB.
  993. nil Never
  994. white Only in completely white lines
  995. whitestart Only at the beginning of lines, before the first non-white char
  996. t Everywhere except in headlines
  997. exc-hl-bol Everywhere except at the start of a headline
  998. If TAB is used in a place where it does not emulate TAB, the current subtree
  999. visibility is cycled."
  1000. :group 'org-cycle
  1001. :type '(choice (const :tag "Never" nil)
  1002. (const :tag "Only in completely white lines" white)
  1003. (const :tag "Before first char in a line" whitestart)
  1004. (const :tag "Everywhere except in headlines" t)
  1005. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1006. (defcustom org-cycle-separator-lines 2
  1007. "Number of empty lines needed to keep an empty line between collapsed trees.
  1008. If you leave an empty line between the end of a subtree and the following
  1009. headline, this empty line is hidden when the subtree is folded.
  1010. Org-mode will leave (exactly) one empty line visible if the number of
  1011. empty lines is equal or larger to the number given in this variable.
  1012. So the default 2 means at least 2 empty lines after the end of a subtree
  1013. are needed to produce free space between a collapsed subtree and the
  1014. following headline.
  1015. If the number is negative, and the number of empty lines is at least -N,
  1016. all empty lines are shown.
  1017. Special case: when 0, never leave empty lines in collapsed view."
  1018. :group 'org-cycle
  1019. :type 'integer)
  1020. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1021. (defcustom org-pre-cycle-hook nil
  1022. "Hook that is run before visibility cycling is happening.
  1023. The function(s) in this hook must accept a single argument which indicates
  1024. the new state that will be set right after running this hook. The
  1025. argument is a symbol. Before a global state change, it can have the values
  1026. `overview', `content', or `all'. Before a local state change, it can have
  1027. the values `folded', `children', or `subtree'."
  1028. :group 'org-cycle
  1029. :type 'hook)
  1030. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1031. org-cycle-hide-drawers
  1032. org-cycle-hide-inline-tasks
  1033. org-cycle-show-empty-lines
  1034. org-optimize-window-after-visibility-change)
  1035. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1036. The function(s) in this hook must accept a single argument which indicates
  1037. the new state that was set by the most recent `org-cycle' command. The
  1038. argument is a symbol. After a global state change, it can have the values
  1039. `overview', `contents', or `all'. After a local state change, it can have
  1040. the values `folded', `children', or `subtree'."
  1041. :group 'org-cycle
  1042. :type 'hook)
  1043. (defgroup org-edit-structure nil
  1044. "Options concerning structure editing in Org-mode."
  1045. :tag "Org Edit Structure"
  1046. :group 'org-structure)
  1047. (defcustom org-odd-levels-only nil
  1048. "Non-nil means skip even levels and only use odd levels for the outline.
  1049. This has the effect that two stars are being added/taken away in
  1050. promotion/demotion commands. It also influences how levels are
  1051. handled by the exporters.
  1052. Changing it requires restart of `font-lock-mode' to become effective
  1053. for fontification also in regions already fontified.
  1054. You may also set this on a per-file basis by adding one of the following
  1055. lines to the buffer:
  1056. #+STARTUP: odd
  1057. #+STARTUP: oddeven"
  1058. :group 'org-edit-structure
  1059. :group 'org-appearance
  1060. :type 'boolean)
  1061. (defcustom org-adapt-indentation t
  1062. "Non-nil means adapt indentation to outline node level.
  1063. When this variable is set, Org assumes that you write outlines by
  1064. indenting text in each node to align with the headline (after the stars).
  1065. The following issues are influenced by this variable:
  1066. - When this is set and the *entire* text in an entry is indented, the
  1067. indentation is increased by one space in a demotion command, and
  1068. decreased by one in a promotion command. If any line in the entry
  1069. body starts with text at column 0, indentation is not changed at all.
  1070. - Property drawers and planning information is inserted indented when
  1071. this variable s set. When nil, they will not be indented.
  1072. - TAB indents a line relative to context. The lines below a headline
  1073. will be indented when this variable is set.
  1074. Note that this is all about true indentation, by adding and removing
  1075. space characters. See also `org-indent.el' which does level-dependent
  1076. indentation in a virtual way, i.e. at display time in Emacs."
  1077. :group 'org-edit-structure
  1078. :type 'boolean)
  1079. (defcustom org-special-ctrl-a/e nil
  1080. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1081. When t, `C-a' will bring back the cursor to the beginning of the
  1082. headline text, i.e. after the stars and after a possible TODO
  1083. keyword. In an item, this will be the position after bullet and
  1084. check-box, if any. When the cursor is already at that position,
  1085. another `C-a' will bring it to the beginning of the line.
  1086. `C-e' will jump to the end of the headline, ignoring the presence
  1087. of tags in the headline. A second `C-e' will then jump to the
  1088. true end of the line, after any tags. This also means that, when
  1089. this variable is non-nil, `C-e' also will never jump beyond the
  1090. end of the heading of a folded section, i.e. not after the
  1091. ellipses.
  1092. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1093. normally, going to the true line boundary first. Only a directly
  1094. following, identical keypress will bring the cursor to the
  1095. special positions.
  1096. This may also be a cons cell where the behavior for `C-a' and
  1097. `C-e' is set separately."
  1098. :group 'org-edit-structure
  1099. :type '(choice
  1100. (const :tag "off" nil)
  1101. (const :tag "on: after stars/bullet and before tags first" t)
  1102. (const :tag "reversed: true line boundary first" reversed)
  1103. (cons :tag "Set C-a and C-e separately"
  1104. (choice :tag "Special C-a"
  1105. (const :tag "off" nil)
  1106. (const :tag "on: after stars/bullet first" t)
  1107. (const :tag "reversed: before stars/bullet first" reversed))
  1108. (choice :tag "Special C-e"
  1109. (const :tag "off" nil)
  1110. (const :tag "on: before tags first" t)
  1111. (const :tag "reversed: after tags first" reversed)))))
  1112. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1113. (defcustom org-special-ctrl-k nil
  1114. "Non-nil means `C-k' will behave specially in headlines.
  1115. When nil, `C-k' will call the default `kill-line' command.
  1116. When t, the following will happen while the cursor is in the headline:
  1117. - When the cursor is at the beginning of a headline, kill the entire
  1118. line and possible the folded subtree below the line.
  1119. - When in the middle of the headline text, kill the headline up to the tags.
  1120. - When after the headline text, kill the tags."
  1121. :group 'org-edit-structure
  1122. :type 'boolean)
  1123. (defcustom org-ctrl-k-protect-subtree nil
  1124. "Non-nil means, do not delete a hidden subtree with C-k.
  1125. When set to the symbol `error', simply throw an error when C-k is
  1126. used to kill (part-of) a headline that has hidden text behind it.
  1127. Any other non-nil value will result in a query to the user, if it is
  1128. OK to kill that hidden subtree. When nil, kill without remorse."
  1129. :group 'org-edit-structure
  1130. :version "24.1"
  1131. :type '(choice
  1132. (const :tag "Do not protect hidden subtrees" nil)
  1133. (const :tag "Protect hidden subtrees with a security query" t)
  1134. (const :tag "Never kill a hidden subtree with C-k" error)))
  1135. (defcustom org-catch-invisible-edits nil
  1136. "Check if in invisible region before inserting or deleting a character.
  1137. Valid values are:
  1138. nil Do not check, so just do invisible edits.
  1139. error Throw an error and do nothing.
  1140. show Make point visible, and do the requested edit.
  1141. show-and-error Make point visible, then throw an error and abort the edit.
  1142. smart Make point visible, and do insertion/deletion if it is
  1143. adjacent to visible text and the change feels predictable.
  1144. Never delete a previously invisible character or add in the
  1145. middle or right after an invisible region. Basically, this
  1146. allows insertion and backward-delete right before ellipses.
  1147. FIXME: maybe in this case we should not even show?"
  1148. :group 'org-edit-structure
  1149. :version "24.1"
  1150. :type '(choice
  1151. (const :tag "Do not check" nil)
  1152. (const :tag "Throw error when trying to edit" error)
  1153. (const :tag "Unhide, but do not do the edit" show-and-error)
  1154. (const :tag "Show invisible part and do the edit" show)
  1155. (const :tag "Be smart and do the right thing" smart)))
  1156. (defcustom org-yank-folded-subtrees t
  1157. "Non-nil means when yanking subtrees, fold them.
  1158. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1159. it starts with a heading and all other headings in it are either children
  1160. or siblings, then fold all the subtrees. However, do this only if no
  1161. text after the yank would be swallowed into a folded tree by this action."
  1162. :group 'org-edit-structure
  1163. :type 'boolean)
  1164. (defcustom org-yank-adjusted-subtrees nil
  1165. "Non-nil means when yanking subtrees, adjust the level.
  1166. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1167. this function for details."
  1168. :group 'org-edit-structure
  1169. :type 'boolean)
  1170. (defcustom org-M-RET-may-split-line '((default . t))
  1171. "Non-nil means M-RET will split the line at the cursor position.
  1172. When nil, it will go to the end of the line before making a
  1173. new line.
  1174. You may also set this option in a different way for different
  1175. contexts. Valid contexts are:
  1176. headline when creating a new headline
  1177. item when creating a new item
  1178. table in a table field
  1179. default the value to be used for all contexts not explicitly
  1180. customized"
  1181. :group 'org-structure
  1182. :group 'org-table
  1183. :type '(choice
  1184. (const :tag "Always" t)
  1185. (const :tag "Never" nil)
  1186. (repeat :greedy t :tag "Individual contexts"
  1187. (cons
  1188. (choice :tag "Context"
  1189. (const headline)
  1190. (const item)
  1191. (const table)
  1192. (const default))
  1193. (boolean)))))
  1194. (defcustom org-insert-heading-respect-content nil
  1195. "Non-nil means insert new headings after the current subtree.
  1196. When nil, the new heading is created directly after the current line.
  1197. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1198. this variable on for the duration of the command."
  1199. :group 'org-structure
  1200. :type 'boolean)
  1201. (defcustom org-blank-before-new-entry '((heading . auto)
  1202. (plain-list-item . auto))
  1203. "Should `org-insert-heading' leave a blank line before new heading/item?
  1204. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1205. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1206. which case Org will look at the surrounding headings/items and try to
  1207. make an intelligent decision whether to insert a blank line or not.
  1208. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1209. the setting here is ignored and no empty line is inserted to avoid breaking
  1210. the list structure."
  1211. :group 'org-edit-structure
  1212. :type '(list
  1213. (cons (const heading)
  1214. (choice (const :tag "Never" nil)
  1215. (const :tag "Always" t)
  1216. (const :tag "Auto" auto)))
  1217. (cons (const plain-list-item)
  1218. (choice (const :tag "Never" nil)
  1219. (const :tag "Always" t)
  1220. (const :tag "Auto" auto)))))
  1221. (defcustom org-insert-heading-hook nil
  1222. "Hook being run after inserting a new heading."
  1223. :group 'org-edit-structure
  1224. :type 'hook)
  1225. (defcustom org-enable-fixed-width-editor t
  1226. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1227. This currently only means they are never auto-wrapped.
  1228. When nil, such lines will be treated like ordinary lines.
  1229. See also the QUOTE keyword."
  1230. :group 'org-edit-structure
  1231. :type 'boolean)
  1232. (defcustom org-goto-auto-isearch t
  1233. "Non-nil means typing characters in `org-goto' starts incremental search.
  1234. When nil, you can use these keybindings to navigate the buffer:
  1235. q Quit the org-goto interface
  1236. n Go to the next visible heading
  1237. p Go to the previous visible heading
  1238. f Go one heading forward on same level
  1239. b Go one heading backward on same level
  1240. u Go one heading up"
  1241. :group 'org-edit-structure
  1242. :type 'boolean)
  1243. (defgroup org-sparse-trees nil
  1244. "Options concerning sparse trees in Org-mode."
  1245. :tag "Org Sparse Trees"
  1246. :group 'org-structure)
  1247. (defcustom org-highlight-sparse-tree-matches t
  1248. "Non-nil means highlight all matches that define a sparse tree.
  1249. The highlights will automatically disappear the next time the buffer is
  1250. changed by an edit command."
  1251. :group 'org-sparse-trees
  1252. :type 'boolean)
  1253. (defcustom org-remove-highlights-with-change t
  1254. "Non-nil means any change to the buffer will remove temporary highlights.
  1255. Such highlights are created by `org-occur' and `org-clock-display'.
  1256. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1257. The highlights created by `org-preview-latex-fragment' always need
  1258. `C-c C-c' to be removed."
  1259. :group 'org-sparse-trees
  1260. :group 'org-time
  1261. :type 'boolean)
  1262. (defcustom org-occur-hook '(org-first-headline-recenter)
  1263. "Hook that is run after `org-occur' has constructed a sparse tree.
  1264. This can be used to recenter the window to show as much of the structure
  1265. as possible."
  1266. :group 'org-sparse-trees
  1267. :type 'hook)
  1268. (defgroup org-imenu-and-speedbar nil
  1269. "Options concerning imenu and speedbar in Org-mode."
  1270. :tag "Org Imenu and Speedbar"
  1271. :group 'org-structure)
  1272. (defcustom org-imenu-depth 2
  1273. "The maximum level for Imenu access to Org-mode headlines.
  1274. This also applied for speedbar access."
  1275. :group 'org-imenu-and-speedbar
  1276. :type 'integer)
  1277. (defgroup org-table nil
  1278. "Options concerning tables in Org-mode."
  1279. :tag "Org Table"
  1280. :group 'org)
  1281. (defcustom org-enable-table-editor 'optimized
  1282. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1283. When nil, such lines will be treated like ordinary lines.
  1284. When equal to the symbol `optimized', the table editor will be optimized to
  1285. do the following:
  1286. - Automatic overwrite mode in front of whitespace in table fields.
  1287. This makes the structure of the table stay in tact as long as the edited
  1288. field does not exceed the column width.
  1289. - Minimize the number of realigns. Normally, the table is aligned each time
  1290. TAB or RET are pressed to move to another field. With optimization this
  1291. happens only if changes to a field might have changed the column width.
  1292. Optimization requires replacing the functions `self-insert-command',
  1293. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1294. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1295. very good at guessing when a re-align will be necessary, but you can always
  1296. force one with \\[org-ctrl-c-ctrl-c].
  1297. If you would like to use the optimized version in Org-mode, but the
  1298. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1299. This variable can be used to turn on and off the table editor during a session,
  1300. but in order to toggle optimization, a restart is required.
  1301. See also the variable `org-table-auto-blank-field'."
  1302. :group 'org-table
  1303. :type '(choice
  1304. (const :tag "off" nil)
  1305. (const :tag "on" t)
  1306. (const :tag "on, optimized" optimized)))
  1307. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1308. (version<= emacs-version "24.1"))
  1309. "Non-nil means cluster self-insert commands for undo when possible.
  1310. If this is set, then, like in the Emacs command loop, 20 consecutive
  1311. characters will be undone together.
  1312. This is configurable, because there is some impact on typing performance."
  1313. :group 'org-table
  1314. :type 'boolean)
  1315. (defcustom org-table-tab-recognizes-table.el t
  1316. "Non-nil means TAB will automatically notice a table.el table.
  1317. When it sees such a table, it moves point into it and - if necessary -
  1318. calls `table-recognize-table'."
  1319. :group 'org-table-editing
  1320. :type 'boolean)
  1321. (defgroup org-link nil
  1322. "Options concerning links in Org-mode."
  1323. :tag "Org Link"
  1324. :group 'org)
  1325. (defvar org-link-abbrev-alist-local nil
  1326. "Buffer-local version of `org-link-abbrev-alist', which see.
  1327. The value of this is taken from the #+LINK lines.")
  1328. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1329. (defcustom org-link-abbrev-alist nil
  1330. "Alist of link abbreviations.
  1331. The car of each element is a string, to be replaced at the start of a link.
  1332. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1333. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1334. [[linkkey:tag][description]]
  1335. The 'linkkey' must be a word word, starting with a letter, followed
  1336. by letters, numbers, '-' or '_'.
  1337. If REPLACE is a string, the tag will simply be appended to create the link.
  1338. If the string contains \"%s\", the tag will be inserted there. If the string
  1339. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1340. at that point (see the function `url-hexify-string'). If the string contains
  1341. the specifier \"%(my-function)\", then the custom function `my-function' will
  1342. be invoked: this function takes the tag as its only argument and must return
  1343. a string.
  1344. REPLACE may also be a function that will be called with the tag as the
  1345. only argument to create the link, which should be returned as a string.
  1346. See the manual for examples."
  1347. :group 'org-link
  1348. :type '(repeat
  1349. (cons
  1350. (string :tag "Protocol")
  1351. (choice
  1352. (string :tag "Format")
  1353. (function)))))
  1354. (defcustom org-descriptive-links t
  1355. "Non-nil means Org will display descriptive links.
  1356. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1357. \"Org Website\", hiding the link itself and just displaying its
  1358. description. When set to `nil', Org will display the full links
  1359. literally.
  1360. You can interactively set the value of this variable by calling
  1361. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1362. :group 'org-link
  1363. :type 'boolean)
  1364. (defcustom org-link-file-path-type 'adaptive
  1365. "How the path name in file links should be stored.
  1366. Valid values are:
  1367. relative Relative to the current directory, i.e. the directory of the file
  1368. into which the link is being inserted.
  1369. absolute Absolute path, if possible with ~ for home directory.
  1370. noabbrev Absolute path, no abbreviation of home directory.
  1371. adaptive Use relative path for files in the current directory and sub-
  1372. directories of it. For other files, use an absolute path."
  1373. :group 'org-link
  1374. :type '(choice
  1375. (const relative)
  1376. (const absolute)
  1377. (const noabbrev)
  1378. (const adaptive)))
  1379. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1380. "Types of links that should be activated in Org-mode files.
  1381. This is a list of symbols, each leading to the activation of a certain link
  1382. type. In principle, it does not hurt to turn on most link types - there may
  1383. be a small gain when turning off unused link types. The types are:
  1384. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1385. angle Links in angular brackets that may contain whitespace like
  1386. <bbdb:Carsten Dominik>.
  1387. plain Plain links in normal text, no whitespace, like http://google.com.
  1388. radio Text that is matched by a radio target, see manual for details.
  1389. tag Tag settings in a headline (link to tag search).
  1390. date Time stamps (link to calendar).
  1391. footnote Footnote labels.
  1392. Changing this variable requires a restart of Emacs to become effective."
  1393. :group 'org-link
  1394. :type '(set :greedy t
  1395. (const :tag "Double bracket links" bracket)
  1396. (const :tag "Angular bracket links" angle)
  1397. (const :tag "Plain text links" plain)
  1398. (const :tag "Radio target matches" radio)
  1399. (const :tag "Tags" tag)
  1400. (const :tag "Timestamps" date)
  1401. (const :tag "Footnotes" footnote)))
  1402. (defcustom org-make-link-description-function nil
  1403. "Function to use for generating link descriptions from links.
  1404. When nil, the link location will be used. This function must take
  1405. two parameters: the first one is the link, the second one is the
  1406. description generated by `org-insert-link'. The function should
  1407. return the description to use."
  1408. :group 'org-link
  1409. :type '(choice (const nil) (function)))
  1410. (defgroup org-link-store nil
  1411. "Options concerning storing links in Org-mode."
  1412. :tag "Org Store Link"
  1413. :group 'org-link)
  1414. (defcustom org-url-hexify-p t
  1415. "When non-nil, hexify URL when creating a link."
  1416. :type 'boolean
  1417. :version "24.3"
  1418. :group 'org-link-store)
  1419. (defcustom org-email-link-description-format "Email %c: %.30s"
  1420. "Format of the description part of a link to an email or usenet message.
  1421. The following %-escapes will be replaced by corresponding information:
  1422. %F full \"From\" field
  1423. %f name, taken from \"From\" field, address if no name
  1424. %T full \"To\" field
  1425. %t first name in \"To\" field, address if no name
  1426. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1427. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1428. %s subject
  1429. %d date
  1430. %m message-id.
  1431. You may use normal field width specification between the % and the letter.
  1432. This is for example useful to limit the length of the subject.
  1433. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1434. :group 'org-link-store
  1435. :type 'string)
  1436. (defcustom org-from-is-user-regexp
  1437. (let (r1 r2)
  1438. (when (and user-mail-address (not (string= user-mail-address "")))
  1439. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1440. (when (and user-full-name (not (string= user-full-name "")))
  1441. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1442. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1443. "Regexp matched against the \"From:\" header of an email or usenet message.
  1444. It should match if the message is from the user him/herself."
  1445. :group 'org-link-store
  1446. :type 'regexp)
  1447. (defcustom org-context-in-file-links t
  1448. "Non-nil means file links from `org-store-link' contain context.
  1449. A search string will be added to the file name with :: as separator and
  1450. used to find the context when the link is activated by the command
  1451. `org-open-at-point'. When this option is t, the entire active region
  1452. will be placed in the search string of the file link. If set to a
  1453. positive integer, only the first n lines of context will be stored.
  1454. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1455. negates this setting for the duration of the command."
  1456. :group 'org-link-store
  1457. :type '(choice boolean integer))
  1458. (defcustom org-keep-stored-link-after-insertion nil
  1459. "Non-nil means keep link in list for entire session.
  1460. The command `org-store-link' adds a link pointing to the current
  1461. location to an internal list. These links accumulate during a session.
  1462. The command `org-insert-link' can be used to insert links into any
  1463. Org-mode file (offering completion for all stored links). When this
  1464. option is nil, every link which has been inserted once using \\[org-insert-link]
  1465. will be removed from the list, to make completing the unused links
  1466. more efficient."
  1467. :group 'org-link-store
  1468. :type 'boolean)
  1469. (defgroup org-link-follow nil
  1470. "Options concerning following links in Org-mode."
  1471. :tag "Org Follow Link"
  1472. :group 'org-link)
  1473. (defcustom org-link-translation-function nil
  1474. "Function to translate links with different syntax to Org syntax.
  1475. This can be used to translate links created for example by the Planner
  1476. or emacs-wiki packages to Org syntax.
  1477. The function must accept two parameters, a TYPE containing the link
  1478. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1479. which is everything after the link protocol. It should return a cons
  1480. with possibly modified values of type and path.
  1481. Org contains a function for this, so if you set this variable to
  1482. `org-translate-link-from-planner', you should be able follow many
  1483. links created by planner."
  1484. :group 'org-link-follow
  1485. :type '(choice (const nil) (function)))
  1486. (defcustom org-follow-link-hook nil
  1487. "Hook that is run after a link has been followed."
  1488. :group 'org-link-follow
  1489. :type 'hook)
  1490. (defcustom org-tab-follows-link nil
  1491. "Non-nil means on links TAB will follow the link.
  1492. Needs to be set before org.el is loaded.
  1493. This really should not be used, it does not make sense, and the
  1494. implementation is bad."
  1495. :group 'org-link-follow
  1496. :type 'boolean)
  1497. (defcustom org-return-follows-link nil
  1498. "Non-nil means on links RET will follow the link.
  1499. In tables, the special behavior of RET has precedence."
  1500. :group 'org-link-follow
  1501. :type 'boolean)
  1502. (defcustom org-mouse-1-follows-link
  1503. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1504. "Non-nil means mouse-1 on a link will follow the link.
  1505. A longer mouse click will still set point. Does not work on XEmacs.
  1506. Needs to be set before org.el is loaded."
  1507. :group 'org-link-follow
  1508. :type 'boolean)
  1509. (defcustom org-mark-ring-length 4
  1510. "Number of different positions to be recorded in the ring.
  1511. Changing this requires a restart of Emacs to work correctly."
  1512. :group 'org-link-follow
  1513. :type 'integer)
  1514. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1515. "Non-nil means internal links in Org files must exactly match a headline.
  1516. When nil, the link search tries to match a phrase with all words
  1517. in the search text."
  1518. :group 'org-link-follow
  1519. :version "24.1"
  1520. :type '(choice
  1521. (const :tag "Use fuzzy text search" nil)
  1522. (const :tag "Match only exact headline" t)
  1523. (const :tag "Match exact headline or query to create it"
  1524. query-to-create)))
  1525. (defcustom org-link-frame-setup
  1526. '((vm . vm-visit-folder-other-frame)
  1527. (vm-imap . vm-visit-imap-folder-other-frame)
  1528. (gnus . org-gnus-no-new-news)
  1529. (file . find-file-other-window)
  1530. (wl . wl-other-frame))
  1531. "Setup the frame configuration for following links.
  1532. When following a link with Emacs, it may often be useful to display
  1533. this link in another window or frame. This variable can be used to
  1534. set this up for the different types of links.
  1535. For VM, use any of
  1536. `vm-visit-folder'
  1537. `vm-visit-folder-other-window'
  1538. `vm-visit-folder-other-frame'
  1539. For Gnus, use any of
  1540. `gnus'
  1541. `gnus-other-frame'
  1542. `org-gnus-no-new-news'
  1543. For FILE, use any of
  1544. `find-file'
  1545. `find-file-other-window'
  1546. `find-file-other-frame'
  1547. For Wanderlust use any of
  1548. `wl'
  1549. `wl-other-frame'
  1550. For the calendar, use the variable `calendar-setup'.
  1551. For BBDB, it is currently only possible to display the matches in
  1552. another window."
  1553. :group 'org-link-follow
  1554. :type '(list
  1555. (cons (const vm)
  1556. (choice
  1557. (const vm-visit-folder)
  1558. (const vm-visit-folder-other-window)
  1559. (const vm-visit-folder-other-frame)))
  1560. (cons (const vm-imap)
  1561. (choice
  1562. (const vm-visit-imap-folder)
  1563. (const vm-visit-imap-folder-other-window)
  1564. (const vm-visit-imap-folder-other-frame)))
  1565. (cons (const gnus)
  1566. (choice
  1567. (const gnus)
  1568. (const gnus-other-frame)
  1569. (const org-gnus-no-new-news)))
  1570. (cons (const file)
  1571. (choice
  1572. (const find-file)
  1573. (const find-file-other-window)
  1574. (const find-file-other-frame)))
  1575. (cons (const wl)
  1576. (choice
  1577. (const wl)
  1578. (const wl-other-frame)))))
  1579. (defcustom org-display-internal-link-with-indirect-buffer nil
  1580. "Non-nil means use indirect buffer to display infile links.
  1581. Activating internal links (from one location in a file to another location
  1582. in the same file) normally just jumps to the location. When the link is
  1583. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1584. is displayed in
  1585. another window. When this option is set, the other window actually displays
  1586. an indirect buffer clone of the current buffer, to avoid any visibility
  1587. changes to the current buffer."
  1588. :group 'org-link-follow
  1589. :type 'boolean)
  1590. (defcustom org-open-non-existing-files nil
  1591. "Non-nil means `org-open-file' will open non-existing files.
  1592. When nil, an error will be generated.
  1593. This variable applies only to external applications because they
  1594. might choke on non-existing files. If the link is to a file that
  1595. will be opened in Emacs, the variable is ignored."
  1596. :group 'org-link-follow
  1597. :type 'boolean)
  1598. (defcustom org-open-directory-means-index-dot-org nil
  1599. "Non-nil means a link to a directory really means to index.org.
  1600. When nil, following a directory link will run dired or open a finder/explorer
  1601. window on that directory."
  1602. :group 'org-link-follow
  1603. :type 'boolean)
  1604. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1605. "Function and arguments to call for following mailto links.
  1606. This is a list with the first element being a Lisp function, and the
  1607. remaining elements being arguments to the function. In string arguments,
  1608. %a will be replaced by the address, and %s will be replaced by the subject
  1609. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1610. :group 'org-link-follow
  1611. :type '(choice
  1612. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1613. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1614. (const :tag "message-mail" (message-mail "%a" "%s"))
  1615. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1616. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1617. "Non-nil means ask for confirmation before executing shell links.
  1618. Shell links can be dangerous: just think about a link
  1619. [[shell:rm -rf ~/*][Google Search]]
  1620. This link would show up in your Org-mode document as \"Google Search\",
  1621. but really it would remove your entire home directory.
  1622. Therefore we advise against setting this variable to nil.
  1623. Just change it to `y-or-n-p' if you want to confirm with a
  1624. single keystroke rather than having to type \"yes\"."
  1625. :group 'org-link-follow
  1626. :type '(choice
  1627. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1628. (const :tag "with y-or-n (faster)" y-or-n-p)
  1629. (const :tag "no confirmation (dangerous)" nil)))
  1630. (put 'org-confirm-shell-link-function
  1631. 'safe-local-variable
  1632. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1633. (defcustom org-confirm-shell-link-not-regexp ""
  1634. "A regexp to skip confirmation for shell links."
  1635. :group 'org-link-follow
  1636. :version "24.1"
  1637. :type 'regexp)
  1638. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1639. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1640. Elisp links can be dangerous: just think about a link
  1641. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1642. This link would show up in your Org-mode document as \"Google Search\",
  1643. but really it would remove your entire home directory.
  1644. Therefore we advise against setting this variable to nil.
  1645. Just change it to `y-or-n-p' if you want to confirm with a
  1646. single keystroke rather than having to type \"yes\"."
  1647. :group 'org-link-follow
  1648. :type '(choice
  1649. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1650. (const :tag "with y-or-n (faster)" y-or-n-p)
  1651. (const :tag "no confirmation (dangerous)" nil)))
  1652. (put 'org-confirm-shell-link-function
  1653. 'safe-local-variable
  1654. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1655. (defcustom org-confirm-elisp-link-not-regexp ""
  1656. "A regexp to skip confirmation for Elisp links."
  1657. :group 'org-link-follow
  1658. :version "24.1"
  1659. :type 'regexp)
  1660. (defconst org-file-apps-defaults-gnu
  1661. '((remote . emacs)
  1662. (system . mailcap)
  1663. (t . mailcap))
  1664. "Default file applications on a UNIX or GNU/Linux system.
  1665. See `org-file-apps'.")
  1666. (defconst org-file-apps-defaults-macosx
  1667. '((remote . emacs)
  1668. (t . "open %s")
  1669. (system . "open %s")
  1670. ("ps.gz" . "gv %s")
  1671. ("eps.gz" . "gv %s")
  1672. ("dvi" . "xdvi %s")
  1673. ("fig" . "xfig %s"))
  1674. "Default file applications on a MacOS X system.
  1675. The system \"open\" is known as a default, but we use X11 applications
  1676. for some files for which the OS does not have a good default.
  1677. See `org-file-apps'.")
  1678. (defconst org-file-apps-defaults-windowsnt
  1679. (list
  1680. '(remote . emacs)
  1681. (cons t
  1682. (list (if (featurep 'xemacs)
  1683. 'mswindows-shell-execute
  1684. 'w32-shell-execute)
  1685. "open" 'file))
  1686. (cons 'system
  1687. (list (if (featurep 'xemacs)
  1688. 'mswindows-shell-execute
  1689. 'w32-shell-execute)
  1690. "open" 'file)))
  1691. "Default file applications on a Windows NT system.
  1692. The system \"open\" is used for most files.
  1693. See `org-file-apps'.")
  1694. (defcustom org-file-apps
  1695. '((auto-mode . emacs)
  1696. ("\\.mm\\'" . default)
  1697. ("\\.x?html?\\'" . default)
  1698. ("\\.pdf\\'" . default))
  1699. "External applications for opening `file:path' items in a document.
  1700. Org-mode uses system defaults for different file types, but
  1701. you can use this variable to set the application for a given file
  1702. extension. The entries in this list are cons cells where the car identifies
  1703. files and the cdr the corresponding command. Possible values for the
  1704. file identifier are
  1705. \"string\" A string as a file identifier can be interpreted in different
  1706. ways, depending on its contents:
  1707. - Alphanumeric characters only:
  1708. Match links with this file extension.
  1709. Example: (\"pdf\" . \"evince %s\")
  1710. to open PDFs with evince.
  1711. - Regular expression: Match links where the
  1712. filename matches the regexp. If you want to
  1713. use groups here, use shy groups.
  1714. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1715. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1716. to open *.html and *.xhtml with firefox.
  1717. - Regular expression which contains (non-shy) groups:
  1718. Match links where the whole link, including \"::\", and
  1719. anything after that, matches the regexp.
  1720. In a custom command string, %1, %2, etc. are replaced with
  1721. the parts of the link that were matched by the groups.
  1722. For backwards compatibility, if a command string is given
  1723. that does not use any of the group matches, this case is
  1724. handled identically to the second one (i.e. match against
  1725. file name only).
  1726. In a custom lisp form, you can access the group matches with
  1727. (match-string n link).
  1728. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1729. to open [[file:document.pdf::5]] with evince at page 5.
  1730. `directory' Matches a directory
  1731. `remote' Matches a remote file, accessible through tramp or efs.
  1732. Remote files most likely should be visited through Emacs
  1733. because external applications cannot handle such paths.
  1734. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1735. so all files Emacs knows how to handle. Using this with
  1736. command `emacs' will open most files in Emacs. Beware that this
  1737. will also open html files inside Emacs, unless you add
  1738. (\"html\" . default) to the list as well.
  1739. t Default for files not matched by any of the other options.
  1740. `system' The system command to open files, like `open' on Windows
  1741. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1742. that will be selected if you call `C-c C-o' with a double
  1743. \\[universal-argument] \\[universal-argument] prefix.
  1744. Possible values for the command are:
  1745. `emacs' The file will be visited by the current Emacs process.
  1746. `default' Use the default application for this file type, which is the
  1747. association for t in the list, most likely in the system-specific
  1748. part.
  1749. This can be used to overrule an unwanted setting in the
  1750. system-specific variable.
  1751. `system' Use the system command for opening files, like \"open\".
  1752. This command is specified by the entry whose car is `system'.
  1753. Most likely, the system-specific version of this variable
  1754. does define this command, but you can overrule/replace it
  1755. here.
  1756. string A command to be executed by a shell; %s will be replaced
  1757. by the path to the file.
  1758. sexp A Lisp form which will be evaluated. The file path will
  1759. be available in the Lisp variable `file'.
  1760. For more examples, see the system specific constants
  1761. `org-file-apps-defaults-macosx'
  1762. `org-file-apps-defaults-windowsnt'
  1763. `org-file-apps-defaults-gnu'."
  1764. :group 'org-link-follow
  1765. :type '(repeat
  1766. (cons (choice :value ""
  1767. (string :tag "Extension")
  1768. (const :tag "System command to open files" system)
  1769. (const :tag "Default for unrecognized files" t)
  1770. (const :tag "Remote file" remote)
  1771. (const :tag "Links to a directory" directory)
  1772. (const :tag "Any files that have Emacs modes"
  1773. auto-mode))
  1774. (choice :value ""
  1775. (const :tag "Visit with Emacs" emacs)
  1776. (const :tag "Use default" default)
  1777. (const :tag "Use the system command" system)
  1778. (string :tag "Command")
  1779. (sexp :tag "Lisp form")))))
  1780. (defcustom org-doi-server-url "http://dx.doi.org/"
  1781. "The URL of the DOI server."
  1782. :type 'string
  1783. :version "24.3"
  1784. :group 'org-link-follow)
  1785. (defgroup org-refile nil
  1786. "Options concerning refiling entries in Org-mode."
  1787. :tag "Org Refile"
  1788. :group 'org)
  1789. (defcustom org-directory "~/org"
  1790. "Directory with org files.
  1791. This is just a default location to look for Org files. There is no need
  1792. at all to put your files into this directory. It is only used in the
  1793. following situations:
  1794. 1. When a capture template specifies a target file that is not an
  1795. absolute path. The path will then be interpreted relative to
  1796. `org-directory'
  1797. 2. When a capture note is filed away in an interactive way (when exiting the
  1798. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1799. with `org-directory' as the default path."
  1800. :group 'org-refile
  1801. :group 'org-remember
  1802. :group 'org-capture
  1803. :type 'directory)
  1804. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1805. "Default target for storing notes.
  1806. Used as a fall back file for org-remember.el and org-capture.el, for
  1807. templates that do not specify a target file."
  1808. :group 'org-refile
  1809. :group 'org-remember
  1810. :group 'org-capture
  1811. :type '(choice
  1812. (const :tag "Default from remember-data-file" nil)
  1813. file))
  1814. (defcustom org-goto-interface 'outline
  1815. "The default interface to be used for `org-goto'.
  1816. Allowed values are:
  1817. outline The interface shows an outline of the relevant file
  1818. and the correct heading is found by moving through
  1819. the outline or by searching with incremental search.
  1820. outline-path-completion Headlines in the current buffer are offered via
  1821. completion. This is the interface also used by
  1822. the refile command."
  1823. :group 'org-refile
  1824. :type '(choice
  1825. (const :tag "Outline" outline)
  1826. (const :tag "Outline-path-completion" outline-path-completion)))
  1827. (defcustom org-goto-max-level 5
  1828. "Maximum target level when running `org-goto' with refile interface."
  1829. :group 'org-refile
  1830. :type 'integer)
  1831. (defcustom org-reverse-note-order nil
  1832. "Non-nil means store new notes at the beginning of a file or entry.
  1833. When nil, new notes will be filed to the end of a file or entry.
  1834. This can also be a list with cons cells of regular expressions that
  1835. are matched against file names, and values."
  1836. :group 'org-remember
  1837. :group 'org-capture
  1838. :group 'org-refile
  1839. :type '(choice
  1840. (const :tag "Reverse always" t)
  1841. (const :tag "Reverse never" nil)
  1842. (repeat :tag "By file name regexp"
  1843. (cons regexp boolean))))
  1844. (defcustom org-log-refile nil
  1845. "Information to record when a task is refiled.
  1846. Possible values are:
  1847. nil Don't add anything
  1848. time Add a time stamp to the task
  1849. note Prompt for a note and add it with template `org-log-note-headings'
  1850. This option can also be set with on a per-file-basis with
  1851. #+STARTUP: nologrefile
  1852. #+STARTUP: logrefile
  1853. #+STARTUP: lognoterefile
  1854. You can have local logging settings for a subtree by setting the LOGGING
  1855. property to one or more of these keywords.
  1856. When bulk-refiling from the agenda, the value `note' is forbidden and
  1857. will temporarily be changed to `time'."
  1858. :group 'org-refile
  1859. :group 'org-progress
  1860. :version "24.1"
  1861. :type '(choice
  1862. (const :tag "No logging" nil)
  1863. (const :tag "Record timestamp" time)
  1864. (const :tag "Record timestamp with note." note)))
  1865. (defcustom org-refile-targets nil
  1866. "Targets for refiling entries with \\[org-refile].
  1867. This is a list of cons cells. Each cell contains:
  1868. - a specification of the files to be considered, either a list of files,
  1869. or a symbol whose function or variable value will be used to retrieve
  1870. a file name or a list of file names. If you use `org-agenda-files' for
  1871. that, all agenda files will be scanned for targets. Nil means consider
  1872. headings in the current buffer.
  1873. - A specification of how to find candidate refile targets. This may be
  1874. any of:
  1875. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1876. This tag has to be present in all target headlines, inheritance will
  1877. not be considered.
  1878. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1879. todo keyword.
  1880. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1881. headlines that are refiling targets.
  1882. - a cons cell (:level . N). Any headline of level N is considered a target.
  1883. Note that, when `org-odd-levels-only' is set, level corresponds to
  1884. order in hierarchy, not to the number of stars.
  1885. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1886. Note that, when `org-odd-levels-only' is set, level corresponds to
  1887. order in hierarchy, not to the number of stars.
  1888. Each element of this list generates a set of possible targets.
  1889. The union of these sets is presented (with completion) to
  1890. the user by `org-refile'.
  1891. You can set the variable `org-refile-target-verify-function' to a function
  1892. to verify each headline found by the simple criteria above.
  1893. When this variable is nil, all top-level headlines in the current buffer
  1894. are used, equivalent to the value `((nil . (:level . 1))'."
  1895. :group 'org-refile
  1896. :type '(repeat
  1897. (cons
  1898. (choice :value org-agenda-files
  1899. (const :tag "All agenda files" org-agenda-files)
  1900. (const :tag "Current buffer" nil)
  1901. (function) (variable) (file))
  1902. (choice :tag "Identify target headline by"
  1903. (cons :tag "Specific tag" (const :value :tag) (string))
  1904. (cons :tag "TODO keyword" (const :value :todo) (string))
  1905. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1906. (cons :tag "Level number" (const :value :level) (integer))
  1907. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1908. (defcustom org-refile-target-verify-function nil
  1909. "Function to verify if the headline at point should be a refile target.
  1910. The function will be called without arguments, with point at the
  1911. beginning of the headline. It should return t and leave point
  1912. where it is if the headline is a valid target for refiling.
  1913. If the target should not be selected, the function must return nil.
  1914. In addition to this, it may move point to a place from where the search
  1915. should be continued. For example, the function may decide that the entire
  1916. subtree of the current entry should be excluded and move point to the end
  1917. of the subtree."
  1918. :group 'org-refile
  1919. :type '(choice
  1920. (const nil)
  1921. (function)))
  1922. (defcustom org-refile-use-cache nil
  1923. "Non-nil means cache refile targets to speed up the process.
  1924. The cache for a particular file will be updated automatically when
  1925. the buffer has been killed, or when any of the marker used for flagging
  1926. refile targets no longer points at a live buffer.
  1927. If you have added new entries to a buffer that might themselves be targets,
  1928. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1929. find that easier, `C-u C-u C-u C-c C-w'."
  1930. :group 'org-refile
  1931. :version "24.1"
  1932. :type 'boolean)
  1933. (defcustom org-refile-use-outline-path nil
  1934. "Non-nil means provide refile targets as paths.
  1935. So a level 3 headline will be available as level1/level2/level3.
  1936. When the value is `file', also include the file name (without directory)
  1937. into the path. In this case, you can also stop the completion after
  1938. the file name, to get entries inserted as top level in the file.
  1939. When `full-file-path', include the full file path."
  1940. :group 'org-refile
  1941. :type '(choice
  1942. (const :tag "Not" nil)
  1943. (const :tag "Yes" t)
  1944. (const :tag "Start with file name" file)
  1945. (const :tag "Start with full file path" full-file-path)))
  1946. (defcustom org-outline-path-complete-in-steps t
  1947. "Non-nil means complete the outline path in hierarchical steps.
  1948. When Org-mode uses the refile interface to select an outline path
  1949. \(see variable `org-refile-use-outline-path'), the completion of
  1950. the path can be done is a single go, or if can be done in steps down
  1951. the headline hierarchy. Going in steps is probably the best if you
  1952. do not use a special completion package like `ido' or `icicles'.
  1953. However, when using these packages, going in one step can be very
  1954. fast, while still showing the whole path to the entry."
  1955. :group 'org-refile
  1956. :type 'boolean)
  1957. (defcustom org-refile-allow-creating-parent-nodes nil
  1958. "Non-nil means allow to create new nodes as refile targets.
  1959. New nodes are then created by adding \"/new node name\" to the completion
  1960. of an existing node. When the value of this variable is `confirm',
  1961. new node creation must be confirmed by the user (recommended)
  1962. When nil, the completion must match an existing entry.
  1963. Note that, if the new heading is not seen by the criteria
  1964. listed in `org-refile-targets', multiple instances of the same
  1965. heading would be created by trying again to file under the new
  1966. heading."
  1967. :group 'org-refile
  1968. :type '(choice
  1969. (const :tag "Never" nil)
  1970. (const :tag "Always" t)
  1971. (const :tag "Prompt for confirmation" confirm)))
  1972. (defcustom org-refile-active-region-within-subtree nil
  1973. "Non-nil means also refile active region within a subtree.
  1974. By default `org-refile' doesn't allow refiling regions if they
  1975. don't contain a set of subtrees, but it might be convenient to
  1976. do so sometimes: in that case, the first line of the region is
  1977. converted to a headline before refiling."
  1978. :group 'org-refile
  1979. :version "24.1"
  1980. :type 'boolean)
  1981. (defgroup org-todo nil
  1982. "Options concerning TODO items in Org-mode."
  1983. :tag "Org TODO"
  1984. :group 'org)
  1985. (defgroup org-progress nil
  1986. "Options concerning Progress logging in Org-mode."
  1987. :tag "Org Progress"
  1988. :group 'org-time)
  1989. (defvar org-todo-interpretation-widgets
  1990. '((:tag "Sequence (cycling hits every state)" sequence)
  1991. (:tag "Type (cycling directly to DONE)" type))
  1992. "The available interpretation symbols for customizing `org-todo-keywords'.
  1993. Interested libraries should add to this list.")
  1994. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1995. "List of TODO entry keyword sequences and their interpretation.
  1996. \\<org-mode-map>This is a list of sequences.
  1997. Each sequence starts with a symbol, either `sequence' or `type',
  1998. indicating if the keywords should be interpreted as a sequence of
  1999. action steps, or as different types of TODO items. The first
  2000. keywords are states requiring action - these states will select a headline
  2001. for inclusion into the global TODO list Org-mode produces. If one of
  2002. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2003. signify that no further action is necessary. If \"|\" is not found,
  2004. the last keyword is treated as the only DONE state of the sequence.
  2005. The command \\[org-todo] cycles an entry through these states, and one
  2006. additional state where no keyword is present. For details about this
  2007. cycling, see the manual.
  2008. TODO keywords and interpretation can also be set on a per-file basis with
  2009. the special #+SEQ_TODO and #+TYP_TODO lines.
  2010. Each keyword can optionally specify a character for fast state selection
  2011. \(in combination with the variable `org-use-fast-todo-selection')
  2012. and specifiers for state change logging, using the same syntax that
  2013. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2014. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2015. indicates to record a time stamp each time this state is selected.
  2016. Each keyword may also specify if a timestamp or a note should be
  2017. recorded when entering or leaving the state, by adding additional
  2018. characters in the parenthesis after the keyword. This looks like this:
  2019. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2020. record only the time of the state change. With X and Y being either
  2021. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2022. Y when leaving the state if and only if the *target* state does not
  2023. define X. You may omit any of the fast-selection key or X or /Y,
  2024. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2025. For backward compatibility, this variable may also be just a list
  2026. of keywords. In this case the interpretation (sequence or type) will be
  2027. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2028. :group 'org-todo
  2029. :group 'org-keywords
  2030. :type '(choice
  2031. (repeat :tag "Old syntax, just keywords"
  2032. (string :tag "Keyword"))
  2033. (repeat :tag "New syntax"
  2034. (cons
  2035. (choice
  2036. :tag "Interpretation"
  2037. ;;Quick and dirty way to see
  2038. ;;`org-todo-interpretations'. This takes the
  2039. ;;place of item arguments
  2040. :convert-widget
  2041. (lambda (widget)
  2042. (widget-put widget
  2043. :args (mapcar
  2044. #'(lambda (x)
  2045. (widget-convert
  2046. (cons 'const x)))
  2047. org-todo-interpretation-widgets))
  2048. widget))
  2049. (repeat
  2050. (string :tag "Keyword"))))))
  2051. (defvar org-todo-keywords-1 nil
  2052. "All TODO and DONE keywords active in a buffer.")
  2053. (make-variable-buffer-local 'org-todo-keywords-1)
  2054. (defvar org-todo-keywords-for-agenda nil)
  2055. (defvar org-done-keywords-for-agenda nil)
  2056. (defvar org-drawers-for-agenda nil)
  2057. (defvar org-todo-keyword-alist-for-agenda nil)
  2058. (defvar org-tag-alist-for-agenda nil
  2059. "Alist of all tags from all agenda files.")
  2060. (defvar org-tag-groups-alist-for-agenda nil
  2061. "Alist of all groups tags from all current agenda files.")
  2062. (defvar org-tag-groups-alist nil)
  2063. (make-variable-buffer-local 'org-tag-groups-alist)
  2064. (defvar org-agenda-contributing-files nil)
  2065. (defvar org-not-done-keywords nil)
  2066. (make-variable-buffer-local 'org-not-done-keywords)
  2067. (defvar org-done-keywords nil)
  2068. (make-variable-buffer-local 'org-done-keywords)
  2069. (defvar org-todo-heads nil)
  2070. (make-variable-buffer-local 'org-todo-heads)
  2071. (defvar org-todo-sets nil)
  2072. (make-variable-buffer-local 'org-todo-sets)
  2073. (defvar org-todo-log-states nil)
  2074. (make-variable-buffer-local 'org-todo-log-states)
  2075. (defvar org-todo-kwd-alist nil)
  2076. (make-variable-buffer-local 'org-todo-kwd-alist)
  2077. (defvar org-todo-key-alist nil)
  2078. (make-variable-buffer-local 'org-todo-key-alist)
  2079. (defvar org-todo-key-trigger nil)
  2080. (make-variable-buffer-local 'org-todo-key-trigger)
  2081. (defcustom org-todo-interpretation 'sequence
  2082. "Controls how TODO keywords are interpreted.
  2083. This variable is in principle obsolete and is only used for
  2084. backward compatibility, if the interpretation of todo keywords is
  2085. not given already in `org-todo-keywords'. See that variable for
  2086. more information."
  2087. :group 'org-todo
  2088. :group 'org-keywords
  2089. :type '(choice (const sequence)
  2090. (const type)))
  2091. (defcustom org-use-fast-todo-selection t
  2092. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2093. This variable describes if and under what circumstances the cycling
  2094. mechanism for TODO keywords will be replaced by a single-key, direct
  2095. selection scheme.
  2096. When nil, fast selection is never used.
  2097. When the symbol `prefix', it will be used when `org-todo' is called
  2098. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2099. `C-u t' in an agenda buffer.
  2100. When t, fast selection is used by default. In this case, the prefix
  2101. argument forces cycling instead.
  2102. In all cases, the special interface is only used if access keys have
  2103. actually been assigned by the user, i.e. if keywords in the configuration
  2104. are followed by a letter in parenthesis, like TODO(t)."
  2105. :group 'org-todo
  2106. :type '(choice
  2107. (const :tag "Never" nil)
  2108. (const :tag "By default" t)
  2109. (const :tag "Only with C-u C-c C-t" prefix)))
  2110. (defcustom org-provide-todo-statistics t
  2111. "Non-nil means update todo statistics after insert and toggle.
  2112. ALL-HEADLINES means update todo statistics by including headlines
  2113. with no TODO keyword as well, counting them as not done.
  2114. A list of TODO keywords means the same, but skip keywords that are
  2115. not in this list.
  2116. When this is set, todo statistics is updated in the parent of the
  2117. current entry each time a todo state is changed."
  2118. :group 'org-todo
  2119. :type '(choice
  2120. (const :tag "Yes, only for TODO entries" t)
  2121. (const :tag "Yes, including all entries" 'all-headlines)
  2122. (repeat :tag "Yes, for TODOs in this list"
  2123. (string :tag "TODO keyword"))
  2124. (other :tag "No TODO statistics" nil)))
  2125. (defcustom org-hierarchical-todo-statistics t
  2126. "Non-nil means TODO statistics covers just direct children.
  2127. When nil, all entries in the subtree are considered.
  2128. This has only an effect if `org-provide-todo-statistics' is set.
  2129. To set this to nil for only a single subtree, use a COOKIE_DATA
  2130. property and include the word \"recursive\" into the value."
  2131. :group 'org-todo
  2132. :type 'boolean)
  2133. (defcustom org-after-todo-state-change-hook nil
  2134. "Hook which is run after the state of a TODO item was changed.
  2135. The new state (a string with a TODO keyword, or nil) is available in the
  2136. Lisp variable `org-state'."
  2137. :group 'org-todo
  2138. :type 'hook)
  2139. (defvar org-blocker-hook nil
  2140. "Hook for functions that are allowed to block a state change.
  2141. Functions in this hook should not modify the buffer.
  2142. Each function gets as its single argument a property list,
  2143. see `org-trigger-hook' for more information about this list.
  2144. If any of the functions in this hook returns nil, the state change
  2145. is blocked.")
  2146. (defvar org-trigger-hook nil
  2147. "Hook for functions that are triggered by a state change.
  2148. Each function gets as its single argument a property list with at
  2149. least the following elements:
  2150. (:type type-of-change :position pos-at-entry-start
  2151. :from old-state :to new-state)
  2152. Depending on the type, more properties may be present.
  2153. This mechanism is currently implemented for:
  2154. TODO state changes
  2155. ------------------
  2156. :type todo-state-change
  2157. :from previous state (keyword as a string), or nil, or a symbol
  2158. 'todo' or 'done', to indicate the general type of state.
  2159. :to new state, like in :from")
  2160. (defcustom org-enforce-todo-dependencies nil
  2161. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2162. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2163. be blocked if any prior sibling is not yet done.
  2164. Finally, if the parent is blocked because of ordered siblings of its own,
  2165. the child will also be blocked."
  2166. :set (lambda (var val)
  2167. (set var val)
  2168. (if val
  2169. (add-hook 'org-blocker-hook
  2170. 'org-block-todo-from-children-or-siblings-or-parent)
  2171. (remove-hook 'org-blocker-hook
  2172. 'org-block-todo-from-children-or-siblings-or-parent)))
  2173. :group 'org-todo
  2174. :type 'boolean)
  2175. (defcustom org-enforce-todo-checkbox-dependencies nil
  2176. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2177. When this is nil, checkboxes have no influence on switching TODO states.
  2178. When non-nil, you first need to check off all check boxes before the TODO
  2179. entry can be switched to DONE.
  2180. This variable needs to be set before org.el is loaded, and you need to
  2181. restart Emacs after a change to make the change effective. The only way
  2182. to change is while Emacs is running is through the customize interface."
  2183. :set (lambda (var val)
  2184. (set var val)
  2185. (if val
  2186. (add-hook 'org-blocker-hook
  2187. 'org-block-todo-from-checkboxes)
  2188. (remove-hook 'org-blocker-hook
  2189. 'org-block-todo-from-checkboxes)))
  2190. :group 'org-todo
  2191. :type 'boolean)
  2192. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2193. "Non-nil means inserting a TODO heading is treated as state change.
  2194. So when the command \\[org-insert-todo-heading] is used, state change
  2195. logging will apply if appropriate. When nil, the new TODO item will
  2196. be inserted directly, and no logging will take place."
  2197. :group 'org-todo
  2198. :type 'boolean)
  2199. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2200. "Non-nil means switching TODO states with S-cursor counts as state change.
  2201. This is the default behavior. However, setting this to nil allows a
  2202. convenient way to select a TODO state and bypass any logging associated
  2203. with that."
  2204. :group 'org-todo
  2205. :type 'boolean)
  2206. (defcustom org-todo-state-tags-triggers nil
  2207. "Tag changes that should be triggered by TODO state changes.
  2208. This is a list. Each entry is
  2209. (state-change (tag . flag) .......)
  2210. State-change can be a string with a state, and empty string to indicate the
  2211. state that has no TODO keyword, or it can be one of the symbols `todo'
  2212. or `done', meaning any not-done or done state, respectively."
  2213. :group 'org-todo
  2214. :group 'org-tags
  2215. :type '(repeat
  2216. (cons (choice :tag "When changing to"
  2217. (const :tag "Not-done state" todo)
  2218. (const :tag "Done state" done)
  2219. (string :tag "State"))
  2220. (repeat
  2221. (cons :tag "Tag action"
  2222. (string :tag "Tag")
  2223. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2224. (defcustom org-log-done nil
  2225. "Information to record when a task moves to the DONE state.
  2226. Possible values are:
  2227. nil Don't add anything, just change the keyword
  2228. time Add a time stamp to the task
  2229. note Prompt for a note and add it with template `org-log-note-headings'
  2230. This option can also be set with on a per-file-basis with
  2231. #+STARTUP: nologdone
  2232. #+STARTUP: logdone
  2233. #+STARTUP: lognotedone
  2234. You can have local logging settings for a subtree by setting the LOGGING
  2235. property to one or more of these keywords."
  2236. :group 'org-todo
  2237. :group 'org-progress
  2238. :type '(choice
  2239. (const :tag "No logging" nil)
  2240. (const :tag "Record CLOSED timestamp" time)
  2241. (const :tag "Record CLOSED timestamp with note." note)))
  2242. ;; Normalize old uses of org-log-done.
  2243. (cond
  2244. ((eq org-log-done t) (setq org-log-done 'time))
  2245. ((and (listp org-log-done) (memq 'done org-log-done))
  2246. (setq org-log-done 'note)))
  2247. (defcustom org-log-reschedule nil
  2248. "Information to record when the scheduling date of a tasks is modified.
  2249. Possible values are:
  2250. nil Don't add anything, just change the date
  2251. time Add a time stamp to the task
  2252. note Prompt for a note and add it with template `org-log-note-headings'
  2253. This option can also be set with on a per-file-basis with
  2254. #+STARTUP: nologreschedule
  2255. #+STARTUP: logreschedule
  2256. #+STARTUP: lognotereschedule"
  2257. :group 'org-todo
  2258. :group 'org-progress
  2259. :type '(choice
  2260. (const :tag "No logging" nil)
  2261. (const :tag "Record timestamp" time)
  2262. (const :tag "Record timestamp with note." note)))
  2263. (defcustom org-log-redeadline nil
  2264. "Information to record when the deadline date of a tasks is modified.
  2265. Possible values are:
  2266. nil Don't add anything, just change the date
  2267. time Add a time stamp to the task
  2268. note Prompt for a note and add it with template `org-log-note-headings'
  2269. This option can also be set with on a per-file-basis with
  2270. #+STARTUP: nologredeadline
  2271. #+STARTUP: logredeadline
  2272. #+STARTUP: lognoteredeadline
  2273. You can have local logging settings for a subtree by setting the LOGGING
  2274. property to one or more of these keywords."
  2275. :group 'org-todo
  2276. :group 'org-progress
  2277. :type '(choice
  2278. (const :tag "No logging" nil)
  2279. (const :tag "Record timestamp" time)
  2280. (const :tag "Record timestamp with note." note)))
  2281. (defcustom org-log-note-clock-out nil
  2282. "Non-nil means record a note when clocking out of an item.
  2283. This can also be configured on a per-file basis by adding one of
  2284. the following lines anywhere in the buffer:
  2285. #+STARTUP: lognoteclock-out
  2286. #+STARTUP: nolognoteclock-out"
  2287. :group 'org-todo
  2288. :group 'org-progress
  2289. :type 'boolean)
  2290. (defcustom org-log-done-with-time t
  2291. "Non-nil means the CLOSED time stamp will contain date and time.
  2292. When nil, only the date will be recorded."
  2293. :group 'org-progress
  2294. :type 'boolean)
  2295. (defcustom org-log-note-headings
  2296. '((done . "CLOSING NOTE %t")
  2297. (state . "State %-12s from %-12S %t")
  2298. (note . "Note taken on %t")
  2299. (reschedule . "Rescheduled from %S on %t")
  2300. (delschedule . "Not scheduled, was %S on %t")
  2301. (redeadline . "New deadline from %S on %t")
  2302. (deldeadline . "Removed deadline, was %S on %t")
  2303. (refile . "Refiled on %t")
  2304. (clock-out . ""))
  2305. "Headings for notes added to entries.
  2306. The value is an alist, with the car being a symbol indicating the note
  2307. context, and the cdr is the heading to be used. The heading may also be the
  2308. empty string.
  2309. %t in the heading will be replaced by a time stamp.
  2310. %T will be an active time stamp instead the default inactive one
  2311. %d will be replaced by a short-format time stamp.
  2312. %D will be replaced by an active short-format time stamp.
  2313. %s will be replaced by the new TODO state, in double quotes.
  2314. %S will be replaced by the old TODO state, in double quotes.
  2315. %u will be replaced by the user name.
  2316. %U will be replaced by the full user name.
  2317. In fact, it is not a good idea to change the `state' entry, because
  2318. agenda log mode depends on the format of these entries."
  2319. :group 'org-todo
  2320. :group 'org-progress
  2321. :type '(list :greedy t
  2322. (cons (const :tag "Heading when closing an item" done) string)
  2323. (cons (const :tag
  2324. "Heading when changing todo state (todo sequence only)"
  2325. state) string)
  2326. (cons (const :tag "Heading when just taking a note" note) string)
  2327. (cons (const :tag "Heading when clocking out" clock-out) string)
  2328. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2329. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2330. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2331. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2332. (cons (const :tag "Heading when refiling" refile) string)))
  2333. (unless (assq 'note org-log-note-headings)
  2334. (push '(note . "%t") org-log-note-headings))
  2335. (defcustom org-log-into-drawer nil
  2336. "Non-nil means insert state change notes and time stamps into a drawer.
  2337. When nil, state changes notes will be inserted after the headline and
  2338. any scheduling and clock lines, but not inside a drawer.
  2339. The value of this variable should be the name of the drawer to use.
  2340. LOGBOOK is proposed as the default drawer for this purpose, you can
  2341. also set this to a string to define the drawer of your choice.
  2342. A value of t is also allowed, representing \"LOGBOOK\".
  2343. A value of t or nil can also be set with on a per-file-basis with
  2344. #+STARTUP: logdrawer
  2345. #+STARTUP: nologdrawer
  2346. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2347. will be ignored.
  2348. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2349. a subtree."
  2350. :group 'org-todo
  2351. :group 'org-progress
  2352. :type '(choice
  2353. (const :tag "Not into a drawer" nil)
  2354. (const :tag "LOGBOOK" t)
  2355. (string :tag "Other")))
  2356. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2357. (defun org-log-into-drawer ()
  2358. "Return the value of `org-log-into-drawer', but let properties overrule.
  2359. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2360. used instead of the default value."
  2361. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2362. (cond
  2363. ((not p) org-log-into-drawer)
  2364. ((equal p "nil") nil)
  2365. ((equal p "t") "LOGBOOK")
  2366. (t p))))
  2367. (defcustom org-log-state-notes-insert-after-drawers nil
  2368. "Non-nil means insert state change notes after any drawers in entry.
  2369. Only the drawers that *immediately* follow the headline and the
  2370. deadline/scheduled line are skipped.
  2371. When nil, insert notes right after the heading and perhaps the line
  2372. with deadline/scheduling if present.
  2373. This variable will have no effect if `org-log-into-drawer' is
  2374. set."
  2375. :group 'org-todo
  2376. :group 'org-progress
  2377. :type 'boolean)
  2378. (defcustom org-log-states-order-reversed t
  2379. "Non-nil means the latest state note will be directly after heading.
  2380. When nil, the state change notes will be ordered according to time.
  2381. This option can also be set with on a per-file-basis with
  2382. #+STARTUP: logstatesreversed
  2383. #+STARTUP: nologstatesreversed"
  2384. :group 'org-todo
  2385. :group 'org-progress
  2386. :type 'boolean)
  2387. (defcustom org-todo-repeat-to-state nil
  2388. "The TODO state to which a repeater should return the repeating task.
  2389. By default this is the first task in a TODO sequence, or the previous state
  2390. in a TODO_TYP set. But you can specify another task here.
  2391. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2392. :group 'org-todo
  2393. :version "24.1"
  2394. :type '(choice (const :tag "Head of sequence" nil)
  2395. (string :tag "Specific state")))
  2396. (defcustom org-log-repeat 'time
  2397. "Non-nil means record moving through the DONE state when triggering repeat.
  2398. An auto-repeating task is immediately switched back to TODO when
  2399. marked DONE. If you are not logging state changes (by adding \"@\"
  2400. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2401. record a closing note, there will be no record of the task moving
  2402. through DONE. This variable forces taking a note anyway.
  2403. nil Don't force a record
  2404. time Record a time stamp
  2405. note Prompt for a note and add it with template `org-log-note-headings'
  2406. This option can also be set with on a per-file-basis with
  2407. #+STARTUP: nologrepeat
  2408. #+STARTUP: logrepeat
  2409. #+STARTUP: lognoterepeat
  2410. You can have local logging settings for a subtree by setting the LOGGING
  2411. property to one or more of these keywords."
  2412. :group 'org-todo
  2413. :group 'org-progress
  2414. :type '(choice
  2415. (const :tag "Don't force a record" nil)
  2416. (const :tag "Force recording the DONE state" time)
  2417. (const :tag "Force recording a note with the DONE state" note)))
  2418. (defgroup org-priorities nil
  2419. "Priorities in Org-mode."
  2420. :tag "Org Priorities"
  2421. :group 'org-todo)
  2422. (defcustom org-enable-priority-commands t
  2423. "Non-nil means priority commands are active.
  2424. When nil, these commands will be disabled, so that you never accidentally
  2425. set a priority."
  2426. :group 'org-priorities
  2427. :type 'boolean)
  2428. (defcustom org-highest-priority ?A
  2429. "The highest priority of TODO items. A character like ?A, ?B etc.
  2430. Must have a smaller ASCII number than `org-lowest-priority'."
  2431. :group 'org-priorities
  2432. :type 'character)
  2433. (defcustom org-lowest-priority ?C
  2434. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2435. Must have a larger ASCII number than `org-highest-priority'."
  2436. :group 'org-priorities
  2437. :type 'character)
  2438. (defcustom org-default-priority ?B
  2439. "The default priority of TODO items.
  2440. This is the priority an item gets if no explicit priority is given.
  2441. When starting to cycle on an empty priority the first step in the cycle
  2442. depends on `org-priority-start-cycle-with-default'. The resulting first
  2443. step priority must not exceed the range from `org-highest-priority' to
  2444. `org-lowest-priority' which means that `org-default-priority' has to be
  2445. in this range exclusive or inclusive the range boundaries. Else the
  2446. first step refuses to set the default and the second will fall back
  2447. to (depending on the command used) the highest or lowest priority."
  2448. :group 'org-priorities
  2449. :type 'character)
  2450. (defcustom org-priority-start-cycle-with-default t
  2451. "Non-nil means start with default priority when starting to cycle.
  2452. When this is nil, the first step in the cycle will be (depending on the
  2453. command used) one higher or lower than the default priority.
  2454. See also `org-default-priority'."
  2455. :group 'org-priorities
  2456. :type 'boolean)
  2457. (defcustom org-get-priority-function nil
  2458. "Function to extract the priority from a string.
  2459. The string is normally the headline. If this is nil Org computes the
  2460. priority from the priority cookie like [#A] in the headline. It returns
  2461. an integer, increasing by 1000 for each priority level.
  2462. The user can set a different function here, which should take a string
  2463. as an argument and return the numeric priority."
  2464. :group 'org-priorities
  2465. :version "24.1"
  2466. :type '(choice
  2467. (const nil)
  2468. (function)))
  2469. (defgroup org-time nil
  2470. "Options concerning time stamps and deadlines in Org-mode."
  2471. :tag "Org Time"
  2472. :group 'org)
  2473. (defcustom org-insert-labeled-timestamps-at-point nil
  2474. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2475. When nil, these labeled time stamps are forces into the second line of an
  2476. entry, just after the headline. When scheduling from the global TODO list,
  2477. the time stamp will always be forced into the second line."
  2478. :group 'org-time
  2479. :type 'boolean)
  2480. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2481. "Formats for `format-time-string' which are used for time stamps.
  2482. It is not recommended to change this constant.")
  2483. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2484. "Number of minutes to round time stamps to.
  2485. These are two values, the first applies when first creating a time stamp.
  2486. The second applies when changing it with the commands `S-up' and `S-down'.
  2487. When changing the time stamp, this means that it will change in steps
  2488. of N minutes, as given by the second value.
  2489. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2490. numbers should be factors of 60, so for example 5, 10, 15.
  2491. When this is larger than 1, you can still force an exact time stamp by using
  2492. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2493. and by using a prefix arg to `S-up/down' to specify the exact number
  2494. of minutes to shift."
  2495. :group 'org-time
  2496. :get #'(lambda (var) ; Make sure both elements are there
  2497. (if (integerp (default-value var))
  2498. (list (default-value var) 5)
  2499. (default-value var)))
  2500. :type '(list
  2501. (integer :tag "when inserting times")
  2502. (integer :tag "when modifying times")))
  2503. ;; Normalize old customizations of this variable.
  2504. (when (integerp org-time-stamp-rounding-minutes)
  2505. (setq org-time-stamp-rounding-minutes
  2506. (list org-time-stamp-rounding-minutes
  2507. org-time-stamp-rounding-minutes)))
  2508. (defcustom org-display-custom-times nil
  2509. "Non-nil means overlay custom formats over all time stamps.
  2510. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2511. To turn this on on a per-file basis, insert anywhere in the file:
  2512. #+STARTUP: customtime"
  2513. :group 'org-time
  2514. :set 'set-default
  2515. :type 'sexp)
  2516. (make-variable-buffer-local 'org-display-custom-times)
  2517. (defcustom org-time-stamp-custom-formats
  2518. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2519. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2520. These are overlaid over the default ISO format if the variable
  2521. `org-display-custom-times' is set. Time like %H:%M should be at the
  2522. end of the second format. The custom formats are also honored by export
  2523. commands, if custom time display is turned on at the time of export."
  2524. :group 'org-time
  2525. :type 'sexp)
  2526. (defun org-time-stamp-format (&optional long inactive)
  2527. "Get the right format for a time string."
  2528. (let ((f (if long (cdr org-time-stamp-formats)
  2529. (car org-time-stamp-formats))))
  2530. (if inactive
  2531. (concat "[" (substring f 1 -1) "]")
  2532. f)))
  2533. (defcustom org-time-clocksum-format
  2534. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2535. "The format string used when creating CLOCKSUM lines.
  2536. This is also used when Org mode generates a time duration.
  2537. The value can be a single format string containing two
  2538. %-sequences, which will be filled with the number of hours and
  2539. minutes in that order.
  2540. Alternatively, the value can be a plist associating any of the
  2541. keys :years, :months, :weeks, :days, :hours or :minutes with
  2542. format strings. The time duration is formatted using only the
  2543. time components that are needed and concatenating the results.
  2544. If a time unit in absent, it falls back to the next smallest
  2545. unit.
  2546. The keys :require-years, :require-months, :require-days,
  2547. :require-weeks, :require-hours, :require-minutes are also
  2548. meaningful. A non-nil value for these keys indicates that the
  2549. corresponding time component should always be included, even if
  2550. its value is 0.
  2551. For example,
  2552. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2553. :require-minutes t)
  2554. means durations longer than a day will be expressed in days,
  2555. hours and minutes, and durations less than a day will always be
  2556. expressed in hours and minutes (even for durations less than an
  2557. hour).
  2558. The value
  2559. \(:days \"%dd\" :minutes \"%dm\")
  2560. means durations longer than a day will be expressed in days and
  2561. minutes, and durations less than a day will be expressed entirely
  2562. in minutes (even for durations longer than an hour)."
  2563. :group 'org-time
  2564. :group 'org-clock
  2565. :version "24.4"
  2566. :package-version '(Org . "8.0")
  2567. :type '(choice (string :tag "Format string")
  2568. (set :tag "Plist"
  2569. (group :inline t (const :tag "Years" :years)
  2570. (string :tag "Format string"))
  2571. (group :inline t
  2572. (const :tag "Always show years" :require-years)
  2573. (const t))
  2574. (group :inline t (const :tag "Months" :months)
  2575. (string :tag "Format string"))
  2576. (group :inline t
  2577. (const :tag "Always show months" :require-months)
  2578. (const t))
  2579. (group :inline t (const :tag "Weeks" :weeks)
  2580. (string :tag "Format string"))
  2581. (group :inline t
  2582. (const :tag "Always show weeks" :require-weeks)
  2583. (const t))
  2584. (group :inline t (const :tag "Days" :days)
  2585. (string :tag "Format string"))
  2586. (group :inline t
  2587. (const :tag "Always show days" :require-days)
  2588. (const t))
  2589. (group :inline t (const :tag "Hours" :hours)
  2590. (string :tag "Format string"))
  2591. (group :inline t
  2592. (const :tag "Always show hours" :require-hours)
  2593. (const t))
  2594. (group :inline t (const :tag "Minutes" :minutes)
  2595. (string :tag "Format string"))
  2596. (group :inline t
  2597. (const :tag "Always show minutes" :require-minutes)
  2598. (const t)))))
  2599. (defcustom org-time-clocksum-use-fractional nil
  2600. "When non-nil, \\[org-clock-display] uses fractional times.
  2601. See `org-time-clocksum-format' for more on time clock formats."
  2602. :group 'org-time
  2603. :group 'org-clock
  2604. :version "24.3"
  2605. :type 'boolean)
  2606. (defcustom org-time-clocksum-use-effort-durations nil
  2607. "When non-nil, \\[org-clock-display] uses effort durations.
  2608. E.g. by default, one day is considered to be a 8 hours effort,
  2609. so a task that has been clocked for 16 hours will be displayed
  2610. as during 2 days in the clock display or in the clocktable.
  2611. See `org-effort-durations' on how to set effort durations
  2612. and `org-time-clocksum-format' for more on time clock formats."
  2613. :group 'org-time
  2614. :group 'org-clock
  2615. :version "24.4"
  2616. :package-version '(Org . "8.0")
  2617. :type 'boolean)
  2618. (defcustom org-time-clocksum-fractional-format "%.2f"
  2619. "The format string used when creating CLOCKSUM lines,
  2620. or when Org mode generates a time duration, if
  2621. `org-time-clocksum-use-fractional' is enabled.
  2622. The value can be a single format string containing one
  2623. %-sequence, which will be filled with the number of hours as
  2624. a float.
  2625. Alternatively, the value can be a plist associating any of the
  2626. keys :years, :months, :weeks, :days, :hours or :minutes with
  2627. a format string. The time duration is formatted using the
  2628. largest time unit which gives a non-zero integer part. If all
  2629. specified formats have zero integer part, the smallest time unit
  2630. is used."
  2631. :group 'org-time
  2632. :type '(choice (string :tag "Format string")
  2633. (set (group :inline t (const :tag "Years" :years)
  2634. (string :tag "Format string"))
  2635. (group :inline t (const :tag "Months" :months)
  2636. (string :tag "Format string"))
  2637. (group :inline t (const :tag "Weeks" :weeks)
  2638. (string :tag "Format string"))
  2639. (group :inline t (const :tag "Days" :days)
  2640. (string :tag "Format string"))
  2641. (group :inline t (const :tag "Hours" :hours)
  2642. (string :tag "Format string"))
  2643. (group :inline t (const :tag "Minutes" :minutes)
  2644. (string :tag "Format string")))))
  2645. (defcustom org-deadline-warning-days 14
  2646. "Number of days before expiration during which a deadline becomes active.
  2647. This variable governs the display in sparse trees and in the agenda.
  2648. When 0 or negative, it means use this number (the absolute value of it)
  2649. even if a deadline has a different individual lead time specified.
  2650. Custom commands can set this variable in the options section."
  2651. :group 'org-time
  2652. :group 'org-agenda-daily/weekly
  2653. :type 'integer)
  2654. (defcustom org-scheduled-delay-days 0
  2655. "Number of days before a scheduled item becomes active.
  2656. This variable governs the display in sparse trees and in the agenda.
  2657. The default value (i.e. 0) means: don't delay scheduled item.
  2658. When negative, it means use this number (the absolute value of it)
  2659. even if a scheduled item has a different individual delay time
  2660. specified.
  2661. Custom commands can set this variable in the options section."
  2662. :group 'org-time
  2663. :group 'org-agenda-daily/weekly
  2664. :version "24.4"
  2665. :package-version '(Org . "8.0")
  2666. :type 'integer)
  2667. (defcustom org-read-date-prefer-future t
  2668. "Non-nil means assume future for incomplete date input from user.
  2669. This affects the following situations:
  2670. 1. The user gives a month but not a year.
  2671. For example, if it is April and you enter \"feb 2\", this will be read
  2672. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2673. 2. The user gives a day, but no month.
  2674. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2675. read this as the third of *next* month. However, if you enter \"17\",
  2676. it will be considered as *this* month.
  2677. If you set this variable to the symbol `time', then also the following
  2678. will work:
  2679. 3. If the user gives a time.
  2680. If the time is before now, it will be interpreted as tomorrow.
  2681. Currently none of this works for ISO week specifications.
  2682. When this option is nil, the current day, month and year will always be
  2683. used as defaults.
  2684. See also `org-agenda-jump-prefer-future'."
  2685. :group 'org-time
  2686. :type '(choice
  2687. (const :tag "Never" nil)
  2688. (const :tag "Check month and day" t)
  2689. (const :tag "Check month, day, and time" time)))
  2690. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2691. "Should the agenda jump command prefer the future for incomplete dates?
  2692. The default is to do the same as configured in `org-read-date-prefer-future'.
  2693. But you can also set a deviating value here.
  2694. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2695. :group 'org-agenda
  2696. :group 'org-time
  2697. :version "24.1"
  2698. :type '(choice
  2699. (const :tag "Use org-read-date-prefer-future"
  2700. org-read-date-prefer-future)
  2701. (const :tag "Never" nil)
  2702. (const :tag "Always" t)))
  2703. (defcustom org-read-date-force-compatible-dates t
  2704. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2705. Depending on the system Emacs is running on, certain dates cannot
  2706. be represented with the type used internally to represent time.
  2707. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2708. correctly. Some systems allow for earlier dates, some for later,
  2709. some for both. One way to find out it to insert any date into an
  2710. Org buffer, putting the cursor on the year and hitting S-up and
  2711. S-down to test the range.
  2712. When this variable is set to t, the date/time prompt will not let
  2713. you specify dates outside the 1970-2037 range, so it is certain that
  2714. these dates will work in whatever version of Emacs you are
  2715. running, and also that you can move a file from one Emacs implementation
  2716. to another. WHenever Org is forcing the year for you, it will display
  2717. a message and beep.
  2718. When this variable is nil, Org will check if the date is
  2719. representable in the specific Emacs implementation you are using.
  2720. If not, it will force a year, usually the current year, and beep
  2721. to remind you. Currently this setting is not recommended because
  2722. the likelihood that you will open your Org files in an Emacs that
  2723. has limited date range is not negligible.
  2724. A workaround for this problem is to use diary sexp dates for time
  2725. stamps outside of this range."
  2726. :group 'org-time
  2727. :version "24.1"
  2728. :type 'boolean)
  2729. (defcustom org-read-date-display-live t
  2730. "Non-nil means display current interpretation of date prompt live.
  2731. This display will be in an overlay, in the minibuffer."
  2732. :group 'org-time
  2733. :type 'boolean)
  2734. (defcustom org-read-date-popup-calendar t
  2735. "Non-nil means pop up a calendar when prompting for a date.
  2736. In the calendar, the date can be selected with mouse-1. However, the
  2737. minibuffer will also be active, and you can simply enter the date as well.
  2738. When nil, only the minibuffer will be available."
  2739. :group 'org-time
  2740. :type 'boolean)
  2741. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2742. 'org-read-date-popup-calendar)
  2743. (make-obsolete-variable
  2744. 'org-read-date-minibuffer-setup-hook
  2745. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2746. (defcustom org-read-date-minibuffer-setup-hook nil
  2747. "Hook to be used to set up keys for the date/time interface.
  2748. Add key definitions to `minibuffer-local-map', which will be a
  2749. temporary copy.
  2750. WARNING: This option is obsolete, you should use
  2751. `org-read-date-minibuffer-local-map' to set up keys."
  2752. :group 'org-time
  2753. :type 'hook)
  2754. (defcustom org-extend-today-until 0
  2755. "The hour when your day really ends. Must be an integer.
  2756. This has influence for the following applications:
  2757. - When switching the agenda to \"today\". It it is still earlier than
  2758. the time given here, the day recognized as TODAY is actually yesterday.
  2759. - When a date is read from the user and it is still before the time given
  2760. here, the current date and time will be assumed to be yesterday, 23:59.
  2761. Also, timestamps inserted in capture templates follow this rule.
  2762. IMPORTANT: This is a feature whose implementation is and likely will
  2763. remain incomplete. Really, it is only here because past midnight seems to
  2764. be the favorite working time of John Wiegley :-)"
  2765. :group 'org-time
  2766. :type 'integer)
  2767. (defcustom org-use-effective-time nil
  2768. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2769. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2770. \"effective time\" of any timestamps between midnight and 8am will be
  2771. 23:59 of the previous day."
  2772. :group 'org-time
  2773. :version "24.1"
  2774. :type 'boolean)
  2775. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2776. "When non-nil, use the last clock out time for `org-todo'.
  2777. Note that this option has precedence over the combined use of
  2778. `org-use-effective-time' and `org-extend-today-until'."
  2779. :group 'org-time
  2780. :version "24.4"
  2781. :package-version '(Org . "8.0")
  2782. :type 'boolean)
  2783. (defcustom org-edit-timestamp-down-means-later nil
  2784. "Non-nil means S-down will increase the time in a time stamp.
  2785. When nil, S-up will increase."
  2786. :group 'org-time
  2787. :type 'boolean)
  2788. (defcustom org-calendar-follow-timestamp-change t
  2789. "Non-nil means make the calendar window follow timestamp changes.
  2790. When a timestamp is modified and the calendar window is visible, it will be
  2791. moved to the new date."
  2792. :group 'org-time
  2793. :type 'boolean)
  2794. (defgroup org-tags nil
  2795. "Options concerning tags in Org-mode."
  2796. :tag "Org Tags"
  2797. :group 'org)
  2798. (defcustom org-tag-alist nil
  2799. "List of tags allowed in Org-mode files.
  2800. When this list is nil, Org-mode will base TAG input on what is already in the
  2801. buffer.
  2802. The value of this variable is an alist, the car of each entry must be a
  2803. keyword as a string, the cdr may be a character that is used to select
  2804. that tag through the fast-tag-selection interface.
  2805. See the manual for details."
  2806. :group 'org-tags
  2807. :type '(repeat
  2808. (choice
  2809. (cons (string :tag "Tag name")
  2810. (character :tag "Access char"))
  2811. (list :tag "Start radio group"
  2812. (const :startgroup)
  2813. (option (string :tag "Group description")))
  2814. (list :tag "Group tags delimiter"
  2815. (const :grouptags))
  2816. (list :tag "End radio group"
  2817. (const :endgroup)
  2818. (option (string :tag "Group description")))
  2819. (const :tag "New line" (:newline)))))
  2820. (defcustom org-tag-persistent-alist nil
  2821. "List of tags that will always appear in all Org-mode files.
  2822. This is in addition to any in buffer settings or customizations
  2823. of `org-tag-alist'.
  2824. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2825. The value of this variable is an alist, the car of each entry must be a
  2826. keyword as a string, the cdr may be a character that is used to select
  2827. that tag through the fast-tag-selection interface.
  2828. See the manual for details.
  2829. To disable these tags on a per-file basis, insert anywhere in the file:
  2830. #+STARTUP: noptag"
  2831. :group 'org-tags
  2832. :type '(repeat
  2833. (choice
  2834. (cons (string :tag "Tag name")
  2835. (character :tag "Access char"))
  2836. (const :tag "Start radio group" (:startgroup))
  2837. (const :tag "Group tags delimiter" (:grouptags))
  2838. (const :tag "End radio group" (:endgroup))
  2839. (const :tag "New line" (:newline)))))
  2840. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2841. "If non-nil, always offer completion for all tags of all agenda files.
  2842. Instead of customizing this variable directly, you might want to
  2843. set it locally for capture buffers, because there no list of
  2844. tags in that file can be created dynamically (there are none).
  2845. (add-hook 'org-capture-mode-hook
  2846. (lambda ()
  2847. (set (make-local-variable
  2848. 'org-complete-tags-always-offer-all-agenda-tags)
  2849. t)))"
  2850. :group 'org-tags
  2851. :version "24.1"
  2852. :type 'boolean)
  2853. (defvar org-file-tags nil
  2854. "List of tags that can be inherited by all entries in the file.
  2855. The tags will be inherited if the variable `org-use-tag-inheritance'
  2856. says they should be.
  2857. This variable is populated from #+FILETAGS lines.")
  2858. (defcustom org-use-fast-tag-selection 'auto
  2859. "Non-nil means use fast tag selection scheme.
  2860. This is a special interface to select and deselect tags with single keys.
  2861. When nil, fast selection is never used.
  2862. When the symbol `auto', fast selection is used if and only if selection
  2863. characters for tags have been configured, either through the variable
  2864. `org-tag-alist' or through a #+TAGS line in the buffer.
  2865. When t, fast selection is always used and selection keys are assigned
  2866. automatically if necessary."
  2867. :group 'org-tags
  2868. :type '(choice
  2869. (const :tag "Always" t)
  2870. (const :tag "Never" nil)
  2871. (const :tag "When selection characters are configured" auto)))
  2872. (defcustom org-fast-tag-selection-single-key nil
  2873. "Non-nil means fast tag selection exits after first change.
  2874. When nil, you have to press RET to exit it.
  2875. During fast tag selection, you can toggle this flag with `C-c'.
  2876. This variable can also have the value `expert'. In this case, the window
  2877. displaying the tags menu is not even shown, until you press C-c again."
  2878. :group 'org-tags
  2879. :type '(choice
  2880. (const :tag "No" nil)
  2881. (const :tag "Yes" t)
  2882. (const :tag "Expert" expert)))
  2883. (defvar org-fast-tag-selection-include-todo nil
  2884. "Non-nil means fast tags selection interface will also offer TODO states.
  2885. This is an undocumented feature, you should not rely on it.")
  2886. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2887. "The column to which tags should be indented in a headline.
  2888. If this number is positive, it specifies the column. If it is negative,
  2889. it means that the tags should be flushright to that column. For example,
  2890. -80 works well for a normal 80 character screen.
  2891. When 0, place tags directly after headline text, with only one space in
  2892. between."
  2893. :group 'org-tags
  2894. :type 'integer)
  2895. (defcustom org-auto-align-tags t
  2896. "Non-nil keeps tags aligned when modifying headlines.
  2897. Some operations (i.e. demoting) change the length of a headline and
  2898. therefore shift the tags around. With this option turned on, after
  2899. each such operation the tags are again aligned to `org-tags-column'."
  2900. :group 'org-tags
  2901. :type 'boolean)
  2902. (defcustom org-use-tag-inheritance t
  2903. "Non-nil means tags in levels apply also for sublevels.
  2904. When nil, only the tags directly given in a specific line apply there.
  2905. This may also be a list of tags that should be inherited, or a regexp that
  2906. matches tags that should be inherited. Additional control is possible
  2907. with the variable `org-tags-exclude-from-inheritance' which gives an
  2908. explicit list of tags to be excluded from inheritance, even if the value of
  2909. `org-use-tag-inheritance' would select it for inheritance.
  2910. If this option is t, a match early-on in a tree can lead to a large
  2911. number of matches in the subtree when constructing the agenda or creating
  2912. a sparse tree. If you only want to see the first match in a tree during
  2913. a search, check out the variable `org-tags-match-list-sublevels'."
  2914. :group 'org-tags
  2915. :type '(choice
  2916. (const :tag "Not" nil)
  2917. (const :tag "Always" t)
  2918. (repeat :tag "Specific tags" (string :tag "Tag"))
  2919. (regexp :tag "Tags matched by regexp")))
  2920. (defcustom org-tags-exclude-from-inheritance nil
  2921. "List of tags that should never be inherited.
  2922. This is a way to exclude a few tags from inheritance. For way to do
  2923. the opposite, to actively allow inheritance for selected tags,
  2924. see the variable `org-use-tag-inheritance'."
  2925. :group 'org-tags
  2926. :type '(repeat (string :tag "Tag")))
  2927. (defun org-tag-inherit-p (tag)
  2928. "Check if TAG is one that should be inherited."
  2929. (cond
  2930. ((member tag org-tags-exclude-from-inheritance) nil)
  2931. ((eq org-use-tag-inheritance t) t)
  2932. ((not org-use-tag-inheritance) nil)
  2933. ((stringp org-use-tag-inheritance)
  2934. (string-match org-use-tag-inheritance tag))
  2935. ((listp org-use-tag-inheritance)
  2936. (member tag org-use-tag-inheritance))
  2937. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2938. (defcustom org-tags-match-list-sublevels t
  2939. "Non-nil means list also sublevels of headlines matching a search.
  2940. This variable applies to tags/property searches, and also to stuck
  2941. projects because this search is based on a tags match as well.
  2942. When set to the symbol `indented', sublevels are indented with
  2943. leading dots.
  2944. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2945. the sublevels of a headline matching a tag search often also match
  2946. the same search. Listing all of them can create very long lists.
  2947. Setting this variable to nil causes subtrees of a match to be skipped.
  2948. This variable is semi-obsolete and probably should always be true. It
  2949. is better to limit inheritance to certain tags using the variables
  2950. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2951. :group 'org-tags
  2952. :type '(choice
  2953. (const :tag "No, don't list them" nil)
  2954. (const :tag "Yes, do list them" t)
  2955. (const :tag "List them, indented with leading dots" indented)))
  2956. (defcustom org-tags-sort-function nil
  2957. "When set, tags are sorted using this function as a comparator."
  2958. :group 'org-tags
  2959. :type '(choice
  2960. (const :tag "No sorting" nil)
  2961. (const :tag "Alphabetical" string<)
  2962. (const :tag "Reverse alphabetical" string>)
  2963. (function :tag "Custom function" nil)))
  2964. (defvar org-tags-history nil
  2965. "History of minibuffer reads for tags.")
  2966. (defvar org-last-tags-completion-table nil
  2967. "The last used completion table for tags.")
  2968. (defvar org-after-tags-change-hook nil
  2969. "Hook that is run after the tags in a line have changed.")
  2970. (defgroup org-properties nil
  2971. "Options concerning properties in Org-mode."
  2972. :tag "Org Properties"
  2973. :group 'org)
  2974. (defcustom org-property-format "%-10s %s"
  2975. "How property key/value pairs should be formatted by `indent-line'.
  2976. When `indent-line' hits a property definition, it will format the line
  2977. according to this format, mainly to make sure that the values are
  2978. lined-up with respect to each other."
  2979. :group 'org-properties
  2980. :type 'string)
  2981. (defcustom org-properties-postprocess-alist nil
  2982. "Alist of properties and functions to adjust inserted values.
  2983. Elements of this alist must be of the form
  2984. ([string] [function])
  2985. where [string] must be a property name and [function] must be a
  2986. lambda expression: this lambda expression must take one argument,
  2987. the value to adjust, and return the new value as a string.
  2988. For example, this element will allow the property \"Remaining\"
  2989. to be updated wrt the relation between the \"Effort\" property
  2990. and the clock summary:
  2991. ((\"Remaining\" (lambda(value)
  2992. (let ((clocksum (org-clock-sum-current-item))
  2993. (effort (org-duration-string-to-minutes
  2994. (org-entry-get (point) \"Effort\"))))
  2995. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2996. :group 'org-properties
  2997. :version "24.1"
  2998. :type '(alist :key-type (string :tag "Property")
  2999. :value-type (function :tag "Function")))
  3000. (defcustom org-use-property-inheritance nil
  3001. "Non-nil means properties apply also for sublevels.
  3002. This setting is chiefly used during property searches. Turning it on can
  3003. cause significant overhead when doing a search, which is why it is not
  3004. on by default.
  3005. When nil, only the properties directly given in the current entry count.
  3006. When t, every property is inherited. The value may also be a list of
  3007. properties that should have inheritance, or a regular expression matching
  3008. properties that should be inherited.
  3009. However, note that some special properties use inheritance under special
  3010. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3011. and the properties ending in \"_ALL\" when they are used as descriptor
  3012. for valid values of a property.
  3013. Note for programmers:
  3014. When querying an entry with `org-entry-get', you can control if inheritance
  3015. should be used. By default, `org-entry-get' looks only at the local
  3016. properties. You can request inheritance by setting the inherit argument
  3017. to t (to force inheritance) or to `selective' (to respect the setting
  3018. in this variable)."
  3019. :group 'org-properties
  3020. :type '(choice
  3021. (const :tag "Not" nil)
  3022. (const :tag "Always" t)
  3023. (repeat :tag "Specific properties" (string :tag "Property"))
  3024. (regexp :tag "Properties matched by regexp")))
  3025. (defun org-property-inherit-p (property)
  3026. "Check if PROPERTY is one that should be inherited."
  3027. (cond
  3028. ((eq org-use-property-inheritance t) t)
  3029. ((not org-use-property-inheritance) nil)
  3030. ((stringp org-use-property-inheritance)
  3031. (string-match org-use-property-inheritance property))
  3032. ((listp org-use-property-inheritance)
  3033. (member property org-use-property-inheritance))
  3034. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3035. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3036. "The default column format, if no other format has been defined.
  3037. This variable can be set on the per-file basis by inserting a line
  3038. #+COLUMNS: %25ITEM ....."
  3039. :group 'org-properties
  3040. :type 'string)
  3041. (defcustom org-columns-ellipses ".."
  3042. "The ellipses to be used when a field in column view is truncated.
  3043. When this is the empty string, as many characters as possible are shown,
  3044. but then there will be no visual indication that the field has been truncated.
  3045. When this is a string of length N, the last N characters of a truncated
  3046. field are replaced by this string. If the column is narrower than the
  3047. ellipses string, only part of the ellipses string will be shown."
  3048. :group 'org-properties
  3049. :type 'string)
  3050. (defcustom org-columns-modify-value-for-display-function nil
  3051. "Function that modifies values for display in column view.
  3052. For example, it can be used to cut out a certain part from a time stamp.
  3053. The function must take 2 arguments:
  3054. column-title The title of the column (*not* the property name)
  3055. value The value that should be modified.
  3056. The function should return the value that should be displayed,
  3057. or nil if the normal value should be used."
  3058. :group 'org-properties
  3059. :type '(choice (const nil) (function)))
  3060. (defcustom org-effort-property "Effort"
  3061. "The property that is being used to keep track of effort estimates.
  3062. Effort estimates given in this property need to have the format H:MM."
  3063. :group 'org-properties
  3064. :group 'org-progress
  3065. :type '(string :tag "Property"))
  3066. (defconst org-global-properties-fixed
  3067. '(("VISIBILITY_ALL" . "folded children content all")
  3068. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3069. "List of property/value pairs that can be inherited by any entry.
  3070. These are fixed values, for the preset properties. The user variable
  3071. that can be used to add to this list is `org-global-properties'.
  3072. The entries in this list are cons cells where the car is a property
  3073. name and cdr is a string with the value. If the value represents
  3074. multiple items like an \"_ALL\" property, separate the items by
  3075. spaces.")
  3076. (defcustom org-global-properties nil
  3077. "List of property/value pairs that can be inherited by any entry.
  3078. This list will be combined with the constant `org-global-properties-fixed'.
  3079. The entries in this list are cons cells where the car is a property
  3080. name and cdr is a string with the value.
  3081. You can set buffer-local values for the same purpose in the variable
  3082. `org-file-properties' this by adding lines like
  3083. #+PROPERTY: NAME VALUE"
  3084. :group 'org-properties
  3085. :type '(repeat
  3086. (cons (string :tag "Property")
  3087. (string :tag "Value"))))
  3088. (defvar org-file-properties nil
  3089. "List of property/value pairs that can be inherited by any entry.
  3090. Valid for the current buffer.
  3091. This variable is populated from #+PROPERTY lines.")
  3092. (make-variable-buffer-local 'org-file-properties)
  3093. (defgroup org-agenda nil
  3094. "Options concerning agenda views in Org-mode."
  3095. :tag "Org Agenda"
  3096. :group 'org)
  3097. (defvar org-category nil
  3098. "Variable used by org files to set a category for agenda display.
  3099. Such files should use a file variable to set it, for example
  3100. # -*- mode: org; org-category: \"ELisp\"
  3101. or contain a special line
  3102. #+CATEGORY: ELisp
  3103. If the file does not specify a category, then file's base name
  3104. is used instead.")
  3105. (make-variable-buffer-local 'org-category)
  3106. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3107. (defcustom org-agenda-files nil
  3108. "The files to be used for agenda display.
  3109. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3110. \\[org-remove-file]. You can also use customize to edit the list.
  3111. If an entry is a directory, all files in that directory that are matched by
  3112. `org-agenda-file-regexp' will be part of the file list.
  3113. If the value of the variable is not a list but a single file name, then
  3114. the list of agenda files is actually stored and maintained in that file, one
  3115. agenda file per line. In this file paths can be given relative to
  3116. `org-directory'. Tilde expansion and environment variable substitution
  3117. are also made."
  3118. :group 'org-agenda
  3119. :type '(choice
  3120. (repeat :tag "List of files and directories" file)
  3121. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3122. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3123. "Regular expression to match files for `org-agenda-files'.
  3124. If any element in the list in that variable contains a directory instead
  3125. of a normal file, all files in that directory that are matched by this
  3126. regular expression will be included."
  3127. :group 'org-agenda
  3128. :type 'regexp)
  3129. (defcustom org-agenda-text-search-extra-files nil
  3130. "List of extra files to be searched by text search commands.
  3131. These files will be searched in addition to the agenda files by the
  3132. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3133. Note that these files will only be searched for text search commands,
  3134. not for the other agenda views like todo lists, tag searches or the weekly
  3135. agenda. This variable is intended to list notes and possibly archive files
  3136. that should also be searched by these two commands.
  3137. In fact, if the first element in the list is the symbol `agenda-archives',
  3138. then all archive files of all agenda files will be added to the search
  3139. scope."
  3140. :group 'org-agenda
  3141. :type '(set :greedy t
  3142. (const :tag "Agenda Archives" agenda-archives)
  3143. (repeat :inline t (file))))
  3144. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3145. 'org-agenda-text-search-extra-files)
  3146. (defcustom org-agenda-skip-unavailable-files nil
  3147. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3148. A nil value means to remove them, after a query, from the list."
  3149. :group 'org-agenda
  3150. :type 'boolean)
  3151. (defcustom org-calendar-to-agenda-key [?c]
  3152. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3153. The command `org-calendar-goto-agenda' will be bound to this key. The
  3154. default is the character `c' because then `c' can be used to switch back and
  3155. forth between agenda and calendar."
  3156. :group 'org-agenda
  3157. :type 'sexp)
  3158. (defcustom org-calendar-insert-diary-entry-key [?i]
  3159. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3160. This option is irrelevant until `org-agenda-diary-file' has been configured
  3161. to point to an Org-mode file. When that is the case, the command
  3162. `org-agenda-diary-entry' will be bound to the key given here, by default
  3163. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3164. if you want to continue doing this, you need to change this to a different
  3165. key."
  3166. :group 'org-agenda
  3167. :type 'sexp)
  3168. (defcustom org-agenda-diary-file 'diary-file
  3169. "File to which to add new entries with the `i' key in agenda and calendar.
  3170. When this is the symbol `diary-file', the functionality in the Emacs
  3171. calendar will be used to add entries to the `diary-file'. But when this
  3172. points to a file, `org-agenda-diary-entry' will be used instead."
  3173. :group 'org-agenda
  3174. :type '(choice
  3175. (const :tag "The standard Emacs diary file" diary-file)
  3176. (file :tag "Special Org file diary entries")))
  3177. (eval-after-load "calendar"
  3178. '(progn
  3179. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3180. 'org-calendar-goto-agenda)
  3181. (add-hook 'calendar-mode-hook
  3182. (lambda ()
  3183. (unless (eq org-agenda-diary-file 'diary-file)
  3184. (define-key calendar-mode-map
  3185. org-calendar-insert-diary-entry-key
  3186. 'org-agenda-diary-entry))))))
  3187. (defgroup org-latex nil
  3188. "Options for embedding LaTeX code into Org-mode."
  3189. :tag "Org LaTeX"
  3190. :group 'org)
  3191. (defcustom org-format-latex-options
  3192. '(:foreground default :background default :scale 1.0
  3193. :html-foreground "Black" :html-background "Transparent"
  3194. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3195. "Options for creating images from LaTeX fragments.
  3196. This is a property list with the following properties:
  3197. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3198. `default' means use the foreground of the default face.
  3199. `auto' means use the foreground from the text face.
  3200. :background the background color, or \"Transparent\".
  3201. `default' means use the background of the default face.
  3202. `auto' means use the background from the text face.
  3203. :scale a scaling factor for the size of the images, to get more pixels
  3204. :html-foreground, :html-background, :html-scale
  3205. the same numbers for HTML export.
  3206. :matchers a list indicating which matchers should be used to
  3207. find LaTeX fragments. Valid members of this list are:
  3208. \"begin\" find environments
  3209. \"$1\" find single characters surrounded by $.$
  3210. \"$\" find math expressions surrounded by $...$
  3211. \"$$\" find math expressions surrounded by $$....$$
  3212. \"\\(\" find math expressions surrounded by \\(...\\)
  3213. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3214. :group 'org-latex
  3215. :type 'plist)
  3216. (defcustom org-format-latex-signal-error t
  3217. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3218. When nil, just push out a message."
  3219. :group 'org-latex
  3220. :version "24.1"
  3221. :type 'boolean)
  3222. (defcustom org-latex-to-mathml-jar-file nil
  3223. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3224. Use this to specify additional executable file say a jar file.
  3225. When using MathToWeb as the converter, specify the full-path to
  3226. your mathtoweb.jar file."
  3227. :group 'org-latex
  3228. :version "24.1"
  3229. :type '(choice
  3230. (const :tag "None" nil)
  3231. (file :tag "JAR file" :must-match t)))
  3232. (defcustom org-latex-to-mathml-convert-command nil
  3233. "Command to convert LaTeX fragments to MathML.
  3234. Replace format-specifiers in the command as noted below and use
  3235. `shell-command' to convert LaTeX to MathML.
  3236. %j: Executable file in fully expanded form as specified by
  3237. `org-latex-to-mathml-jar-file'.
  3238. %I: Input LaTeX file in fully expanded form
  3239. %o: Output MathML file
  3240. This command is used by `org-create-math-formula'.
  3241. When using MathToWeb as the converter, set this to
  3242. \"java -jar %j -unicode -force -df %o %I\"."
  3243. :group 'org-latex
  3244. :version "24.1"
  3245. :type '(choice
  3246. (const :tag "None" nil)
  3247. (string :tag "\nShell command")))
  3248. (defcustom org-latex-create-formula-image-program 'dvipng
  3249. "Program to convert LaTeX fragments with.
  3250. dvipng Process the LaTeX fragments to dvi file, then convert
  3251. dvi files to png files using dvipng.
  3252. This will also include processing of non-math environments.
  3253. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3254. to convert pdf files to png files"
  3255. :group 'org-latex
  3256. :version "24.1"
  3257. :type '(choice
  3258. (const :tag "dvipng" dvipng)
  3259. (const :tag "imagemagick" imagemagick)))
  3260. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3261. "Path to store latex preview images.
  3262. A relative path here creates many directories relative to the
  3263. processed org files paths. An absolute path puts all preview
  3264. images at the same place."
  3265. :group 'org-latex
  3266. :version "24.3"
  3267. :type 'string)
  3268. (defun org-format-latex-mathml-available-p ()
  3269. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3270. (save-match-data
  3271. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3272. org-latex-to-mathml-convert-command)
  3273. (let ((executable (car (split-string
  3274. org-latex-to-mathml-convert-command))))
  3275. (when (executable-find executable)
  3276. (if (string-match
  3277. "%j" org-latex-to-mathml-convert-command)
  3278. (file-readable-p org-latex-to-mathml-jar-file)
  3279. t))))))
  3280. (defcustom org-format-latex-header "\\documentclass{article}
  3281. \\usepackage[usenames]{color}
  3282. \\usepackage{amsmath}
  3283. \\usepackage[mathscr]{eucal}
  3284. \\pagestyle{empty} % do not remove
  3285. \[PACKAGES]
  3286. \[DEFAULT-PACKAGES]
  3287. % The settings below are copied from fullpage.sty
  3288. \\setlength{\\textwidth}{\\paperwidth}
  3289. \\addtolength{\\textwidth}{-3cm}
  3290. \\setlength{\\oddsidemargin}{1.5cm}
  3291. \\addtolength{\\oddsidemargin}{-2.54cm}
  3292. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3293. \\setlength{\\textheight}{\\paperheight}
  3294. \\addtolength{\\textheight}{-\\headheight}
  3295. \\addtolength{\\textheight}{-\\headsep}
  3296. \\addtolength{\\textheight}{-\\footskip}
  3297. \\addtolength{\\textheight}{-3cm}
  3298. \\setlength{\\topmargin}{1.5cm}
  3299. \\addtolength{\\topmargin}{-2.54cm}"
  3300. "The document header used for processing LaTeX fragments.
  3301. It is imperative that this header make sure that no page number
  3302. appears on the page. The package defined in the variables
  3303. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3304. will either replace the placeholder \"[PACKAGES]\" in this
  3305. header, or they will be appended."
  3306. :group 'org-latex
  3307. :type 'string)
  3308. (defun org-set-packages-alist (var val)
  3309. "Set the packages alist and make sure it has 3 elements per entry."
  3310. (set var (mapcar (lambda (x)
  3311. (if (and (consp x) (= (length x) 2))
  3312. (list (car x) (nth 1 x) t)
  3313. x))
  3314. val)))
  3315. (defun org-get-packages-alist (var)
  3316. "Get the packages alist and make sure it has 3 elements per entry."
  3317. (mapcar (lambda (x)
  3318. (if (and (consp x) (= (length x) 2))
  3319. (list (car x) (nth 1 x) t)
  3320. x))
  3321. (default-value var)))
  3322. (defcustom org-latex-default-packages-alist
  3323. '(("AUTO" "inputenc" t)
  3324. ("T1" "fontenc" t)
  3325. ("" "fixltx2e" nil)
  3326. ("" "graphicx" t)
  3327. ("" "longtable" nil)
  3328. ("" "float" nil)
  3329. ("" "wrapfig" nil)
  3330. ("" "soul" t)
  3331. ("" "textcomp" t)
  3332. ("" "marvosym" t)
  3333. ("" "wasysym" t)
  3334. ("" "latexsym" t)
  3335. ("" "amssymb" t)
  3336. ("" "amstext" nil)
  3337. ("" "hyperref" nil)
  3338. "\\tolerance=1000")
  3339. "Alist of default packages to be inserted in the header.
  3340. Change this only if one of the packages here causes an
  3341. incompatibility with another package you are using.
  3342. The packages in this list are needed by one part or another of
  3343. Org mode to function properly:
  3344. - inputenc, fontenc: for basic font and character selection
  3345. - amstext: for subscript and superscript
  3346. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3347. symbols used for interpreting the entities in `org-entities'.
  3348. You can skip some of these packages if you don't use any of the
  3349. symbols in it.
  3350. - graphicx: for including images
  3351. - float, wrapfig: for figure placement
  3352. - longtable: for long tables
  3353. - hyperref: for cross references
  3354. Therefore you should not modify this variable unless you know
  3355. what you are doing. The one reason to change it anyway is that
  3356. you might be loading some other package that conflicts with one
  3357. of the default packages. Each cell is of the format
  3358. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3359. the package also needs to be included when compiling LaTeX
  3360. snippets into images for inclusion into non-LaTeX output."
  3361. :group 'org-latex
  3362. :group 'org-export-latex
  3363. :set 'org-set-packages-alist
  3364. :get 'org-get-packages-alist
  3365. :version "24.1"
  3366. :type '(repeat
  3367. (choice
  3368. (list :tag "options/package pair"
  3369. (string :tag "options")
  3370. (string :tag "package")
  3371. (boolean :tag "Snippet"))
  3372. (string :tag "A line of LaTeX"))))
  3373. (defcustom org-latex-packages-alist nil
  3374. "Alist of packages to be inserted in every LaTeX header.
  3375. These will be inserted after `org-latex-default-packages-alist'.
  3376. Each cell is of the format:
  3377. \(\"options\" \"package\" snippet-flag)
  3378. SNIPPET-FLAG, when t, indicates that this package is also needed
  3379. when turning LaTeX snippets into images for inclusion into
  3380. non-LaTeX output.
  3381. Make sure that you only list packages here which:
  3382. - you want in every file
  3383. - do not conflict with the setup in `org-format-latex-header'.
  3384. - do not conflict with the default packages in
  3385. `org-latex-default-packages-alist'."
  3386. :group 'org-latex
  3387. :group 'org-export-latex
  3388. :set 'org-set-packages-alist
  3389. :get 'org-get-packages-alist
  3390. :type '(repeat
  3391. (choice
  3392. (list :tag "options/package pair"
  3393. (string :tag "options")
  3394. (string :tag "package")
  3395. (boolean :tag "Snippet"))
  3396. (string :tag "A line of LaTeX"))))
  3397. (defgroup org-appearance nil
  3398. "Settings for Org-mode appearance."
  3399. :tag "Org Appearance"
  3400. :group 'org)
  3401. (defcustom org-level-color-stars-only nil
  3402. "Non-nil means fontify only the stars in each headline.
  3403. When nil, the entire headline is fontified.
  3404. Changing it requires restart of `font-lock-mode' to become effective
  3405. also in regions already fontified."
  3406. :group 'org-appearance
  3407. :type 'boolean)
  3408. (defcustom org-hide-leading-stars nil
  3409. "Non-nil means hide the first N-1 stars in a headline.
  3410. This works by using the face `org-hide' for these stars. This
  3411. face is white for a light background, and black for a dark
  3412. background. You may have to customize the face `org-hide' to
  3413. make this work.
  3414. Changing it requires restart of `font-lock-mode' to become effective
  3415. also in regions already fontified.
  3416. You may also set this on a per-file basis by adding one of the following
  3417. lines to the buffer:
  3418. #+STARTUP: hidestars
  3419. #+STARTUP: showstars"
  3420. :group 'org-appearance
  3421. :type 'boolean)
  3422. (defcustom org-hidden-keywords nil
  3423. "List of symbols corresponding to keywords to be hidden the org buffer.
  3424. For example, a value '(title) for this list will make the document's title
  3425. appear in the buffer without the initial #+TITLE: keyword."
  3426. :group 'org-appearance
  3427. :version "24.1"
  3428. :type '(set (const :tag "#+AUTHOR" author)
  3429. (const :tag "#+DATE" date)
  3430. (const :tag "#+EMAIL" email)
  3431. (const :tag "#+TITLE" title)))
  3432. (defcustom org-custom-properties nil
  3433. "List of properties (as strings) with a special meaning.
  3434. The default use of these custom properties is to let the user
  3435. hide them with `org-toggle-custom-properties-visibility'."
  3436. :group 'org-properties
  3437. :group 'org-appearance
  3438. :version "24.3"
  3439. :type '(repeat (string :tag "Property Name")))
  3440. (defcustom org-fontify-done-headline nil
  3441. "Non-nil means change the face of a headline if it is marked DONE.
  3442. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3443. When this is non-nil, the headline after the keyword is set to the
  3444. `org-headline-done' as an additional indication."
  3445. :group 'org-appearance
  3446. :type 'boolean)
  3447. (defcustom org-fontify-emphasized-text t
  3448. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3449. Changing this variable requires a restart of Emacs to take effect."
  3450. :group 'org-appearance
  3451. :type 'boolean)
  3452. (defcustom org-fontify-whole-heading-line nil
  3453. "Non-nil means fontify the whole line for headings.
  3454. This is useful when setting a background color for the
  3455. org-level-* faces."
  3456. :group 'org-appearance
  3457. :type 'boolean)
  3458. (defcustom org-highlight-latex-and-related nil
  3459. "Non-nil means highlight LaTeX related syntax in the buffer.
  3460. When non nil, the value should be a list containing any of the
  3461. following symbols:
  3462. `latex' Highlight LaTeX snippets and environments.
  3463. `script' Highlight subscript and superscript.
  3464. `entities' Highlight entities."
  3465. :group 'org-appearance
  3466. :version "24.4"
  3467. :package-version '(Org . "8.0")
  3468. :type '(choice
  3469. (const :tag "No highlighting" nil)
  3470. (set :greedy t :tag "Highlight"
  3471. (const :tag "LaTeX snippets and environments" latex)
  3472. (const :tag "Subscript and superscript" script)
  3473. (const :tag "Entities" entities))))
  3474. (defcustom org-hide-emphasis-markers nil
  3475. "Non-nil mean font-lock should hide the emphasis marker characters."
  3476. :group 'org-appearance
  3477. :type 'boolean)
  3478. (defcustom org-pretty-entities nil
  3479. "Non-nil means show entities as UTF8 characters.
  3480. When nil, the \\name form remains in the buffer."
  3481. :group 'org-appearance
  3482. :version "24.1"
  3483. :type 'boolean)
  3484. (defcustom org-pretty-entities-include-sub-superscripts t
  3485. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3486. :group 'org-appearance
  3487. :version "24.1"
  3488. :type 'boolean)
  3489. (defvar org-emph-re nil
  3490. "Regular expression for matching emphasis.
  3491. After a match, the match groups contain these elements:
  3492. 0 The match of the full regular expression, including the characters
  3493. before and after the proper match
  3494. 1 The character before the proper match, or empty at beginning of line
  3495. 2 The proper match, including the leading and trailing markers
  3496. 3 The leading marker like * or /, indicating the type of highlighting
  3497. 4 The text between the emphasis markers, not including the markers
  3498. 5 The character after the match, empty at the end of a line")
  3499. (defvar org-verbatim-re nil
  3500. "Regular expression for matching verbatim text.")
  3501. (defvar org-emphasis-regexp-components) ; defined just below
  3502. (defvar org-emphasis-alist) ; defined just below
  3503. (defun org-set-emph-re (var val)
  3504. "Set variable and compute the emphasis regular expression."
  3505. (set var val)
  3506. (when (and (boundp 'org-emphasis-alist)
  3507. (boundp 'org-emphasis-regexp-components)
  3508. org-emphasis-alist org-emphasis-regexp-components)
  3509. (let* ((e org-emphasis-regexp-components)
  3510. (pre (car e))
  3511. (post (nth 1 e))
  3512. (border (nth 2 e))
  3513. (body (nth 3 e))
  3514. (nl (nth 4 e))
  3515. (body1 (concat body "*?"))
  3516. (markers (mapconcat 'car org-emphasis-alist ""))
  3517. (vmarkers (mapconcat
  3518. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3519. org-emphasis-alist "")))
  3520. ;; make sure special characters appear at the right position in the class
  3521. (if (string-match "\\^" markers)
  3522. (setq markers (concat (replace-match "" t t markers) "^")))
  3523. (if (string-match "-" markers)
  3524. (setq markers (concat (replace-match "" t t markers) "-")))
  3525. (if (string-match "\\^" vmarkers)
  3526. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3527. (if (string-match "-" vmarkers)
  3528. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3529. (if (> nl 0)
  3530. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3531. (int-to-string nl) "\\}")))
  3532. ;; Make the regexp
  3533. (setq org-emph-re
  3534. (concat "\\([" pre "]\\|^\\)"
  3535. "\\("
  3536. "\\([" markers "]\\)"
  3537. "\\("
  3538. "[^" border "]\\|"
  3539. "[^" border "]"
  3540. body1
  3541. "[^" border "]"
  3542. "\\)"
  3543. "\\3\\)"
  3544. "\\([" post "]\\|$\\)"))
  3545. (setq org-verbatim-re
  3546. (concat "\\([" pre "]\\|^\\)"
  3547. "\\("
  3548. "\\([" vmarkers "]\\)"
  3549. "\\("
  3550. "[^" border "]\\|"
  3551. "[^" border "]"
  3552. body1
  3553. "[^" border "]"
  3554. "\\)"
  3555. "\\3\\)"
  3556. "\\([" post "]\\|$\\)")))))
  3557. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3558. ;; set this option proved cumbersome. See this message/thread:
  3559. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3560. (defvar org-emphasis-regexp-components
  3561. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3562. "Components used to build the regular expression for emphasis.
  3563. This is a list with five entries. Terminology: In an emphasis string
  3564. like \" *strong word* \", we call the initial space PREMATCH, the final
  3565. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3566. and \"trong wor\" is the body. The different components in this variable
  3567. specify what is allowed/forbidden in each part:
  3568. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3569. post Chars allowed as postmatch. End of line will be allowed too.
  3570. border The chars *forbidden* as border characters.
  3571. body-regexp A regexp like \".\" to match a body character. Don't use
  3572. non-shy groups here, and don't allow newline here.
  3573. newline The maximum number of newlines allowed in an emphasis exp.
  3574. You need to reload Org or to restart Emacs after customizing this.")
  3575. (defcustom org-emphasis-alist
  3576. `(("*" bold)
  3577. ("/" italic)
  3578. ("_" underline)
  3579. ("=" org-code verbatim)
  3580. ("~" org-verbatim verbatim)
  3581. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3582. "Alist of characters and faces to emphasize text.
  3583. Text starting and ending with a special character will be emphasized,
  3584. for example *bold*, _underlined_ and /italic/. This variable sets the
  3585. marker characters and the face to be used by font-lock for highlighting
  3586. in Org-mode Emacs buffers.
  3587. You need to reload Org or to restart Emacs after customizing this."
  3588. :group 'org-appearance
  3589. :set 'org-set-emph-re
  3590. :version "24.4"
  3591. :package-version '(Org . "8.0")
  3592. :type '(repeat
  3593. (list
  3594. (string :tag "Marker character")
  3595. (choice
  3596. (face :tag "Font-lock-face")
  3597. (plist :tag "Face property list"))
  3598. (option (const verbatim)))))
  3599. (defvar org-protecting-blocks
  3600. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3601. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3602. This is needed for font-lock setup.")
  3603. ;;; Miscellaneous options
  3604. (defgroup org-completion nil
  3605. "Completion in Org-mode."
  3606. :tag "Org Completion"
  3607. :group 'org)
  3608. (defcustom org-completion-use-ido nil
  3609. "Non-nil means use ido completion wherever possible.
  3610. Note that `ido-mode' must be active for this variable to be relevant.
  3611. If you decide to turn this variable on, you might well want to turn off
  3612. `org-outline-path-complete-in-steps'.
  3613. See also `org-completion-use-iswitchb'."
  3614. :group 'org-completion
  3615. :type 'boolean)
  3616. (defcustom org-completion-use-iswitchb nil
  3617. "Non-nil means use iswitchb completion wherever possible.
  3618. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3619. If you decide to turn this variable on, you might well want to turn off
  3620. `org-outline-path-complete-in-steps'.
  3621. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3622. :group 'org-completion
  3623. :type 'boolean)
  3624. (defcustom org-completion-fallback-command 'hippie-expand
  3625. "The expansion command called by \\[pcomplete] in normal context.
  3626. Normal means, no org-mode-specific context."
  3627. :group 'org-completion
  3628. :type 'function)
  3629. ;;; Functions and variables from their packages
  3630. ;; Declared here to avoid compiler warnings
  3631. ;; XEmacs only
  3632. (defvar outline-mode-menu-heading)
  3633. (defvar outline-mode-menu-show)
  3634. (defvar outline-mode-menu-hide)
  3635. (defvar zmacs-regions) ; XEmacs regions
  3636. ;; Emacs only
  3637. (defvar mark-active)
  3638. ;; Various packages
  3639. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3640. (declare-function calendar-forward-day "cal-move" (arg))
  3641. (declare-function calendar-goto-date "cal-move" (date))
  3642. (declare-function calendar-goto-today "cal-move" ())
  3643. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3644. (defvar calc-embedded-close-formula)
  3645. (defvar calc-embedded-open-formula)
  3646. (declare-function cdlatex-tab "ext:cdlatex" ())
  3647. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3648. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3649. (defvar font-lock-unfontify-region-function)
  3650. (declare-function iswitchb-read-buffer "iswitchb"
  3651. (prompt &optional default require-match start matches-set))
  3652. (defvar iswitchb-temp-buflist)
  3653. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3654. (defvar org-agenda-tags-todo-honor-ignore-options)
  3655. (declare-function org-agenda-skip "org-agenda" ())
  3656. (declare-function
  3657. org-agenda-format-item "org-agenda"
  3658. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3659. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3660. (declare-function org-agenda-change-all-lines "org-agenda"
  3661. (newhead hdmarker &optional fixface just-this))
  3662. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3663. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3664. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3665. (beg end))
  3666. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3667. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3668. "org-agenda" (&optional end))
  3669. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3670. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3671. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3672. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3673. (declare-function org-indent-mode "org-indent" (&optional arg))
  3674. (declare-function parse-time-string "parse-time" (string))
  3675. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3676. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3677. (defvar remember-data-file)
  3678. (defvar texmathp-why)
  3679. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3680. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3681. (defvar org-latex-regexps)
  3682. ;;; Autoload and prepare some org modules
  3683. ;; Some table stuff that needs to be defined here, because it is used
  3684. ;; by the functions setting up org-mode or checking for table context.
  3685. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3686. "Detect an org-type or table-type table.")
  3687. (defconst org-table-line-regexp "^[ \t]*|"
  3688. "Detect an org-type table line.")
  3689. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3690. "Detect an org-type table line.")
  3691. (defconst org-table-hline-regexp "^[ \t]*|-"
  3692. "Detect an org-type table hline.")
  3693. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3694. "Detect a table-type table hline.")
  3695. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3696. "Detect the first line outside a table when searching from within it.
  3697. This works for both table types.")
  3698. ;; Autoload the functions in org-table.el that are needed by functions here.
  3699. (eval-and-compile
  3700. (org-autoload "org-table"
  3701. '(org-table-begin org-table-blank-field org-table-end)))
  3702. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3703. "Detect a #+TBLFM line.")
  3704. ;;;###autoload
  3705. (defun turn-on-orgtbl ()
  3706. "Unconditionally turn on `orgtbl-mode'."
  3707. (require 'org-table)
  3708. (orgtbl-mode 1))
  3709. (defun org-at-table-p (&optional table-type)
  3710. "Return t if the cursor is inside an org-type table.
  3711. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3712. (if org-enable-table-editor
  3713. (save-excursion
  3714. (beginning-of-line 1)
  3715. (looking-at (if table-type org-table-any-line-regexp
  3716. org-table-line-regexp)))
  3717. nil))
  3718. (defsubst org-table-p () (org-at-table-p))
  3719. (defun org-at-table.el-p ()
  3720. "Return t if and only if we are at a table.el table."
  3721. (and (org-at-table-p 'any)
  3722. (save-excursion
  3723. (goto-char (org-table-begin 'any))
  3724. (looking-at org-table1-hline-regexp))))
  3725. (defun org-table-recognize-table.el ()
  3726. "If there is a table.el table nearby, recognize it and move into it."
  3727. (if org-table-tab-recognizes-table.el
  3728. (if (org-at-table.el-p)
  3729. (progn
  3730. (beginning-of-line 1)
  3731. (if (looking-at org-table-dataline-regexp)
  3732. nil
  3733. (if (looking-at org-table1-hline-regexp)
  3734. (progn
  3735. (beginning-of-line 2)
  3736. (if (looking-at org-table-any-border-regexp)
  3737. (beginning-of-line -1)))))
  3738. (if (re-search-forward "|" (org-table-end t) t)
  3739. (progn
  3740. (require 'table)
  3741. (if (table--at-cell-p (point))
  3742. t
  3743. (message "recognizing table.el table...")
  3744. (table-recognize-table)
  3745. (message "recognizing table.el table...done")))
  3746. (error "This should not happen"))
  3747. t)
  3748. nil)
  3749. nil))
  3750. (defun org-at-table-hline-p ()
  3751. "Return t if the cursor is inside a hline in a table."
  3752. (if org-enable-table-editor
  3753. (save-excursion
  3754. (beginning-of-line 1)
  3755. (looking-at org-table-hline-regexp))
  3756. nil))
  3757. (defvar org-table-clean-did-remove-column nil)
  3758. (defun org-table-map-tables (function &optional quietly)
  3759. "Apply FUNCTION to the start of all tables in the buffer."
  3760. (save-excursion
  3761. (save-restriction
  3762. (widen)
  3763. (goto-char (point-min))
  3764. (while (re-search-forward org-table-any-line-regexp nil t)
  3765. (unless quietly
  3766. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3767. (beginning-of-line 1)
  3768. (when (and (looking-at org-table-line-regexp)
  3769. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3770. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3771. (save-excursion (funcall function))
  3772. (or (looking-at org-table-line-regexp)
  3773. (forward-char 1)))
  3774. (re-search-forward org-table-any-border-regexp nil 1))))
  3775. (unless quietly (message "Mapping tables: done")))
  3776. ;; Declare and autoload functions from ox.el and al.
  3777. (declare-function org-export-get-environment "ox"
  3778. (&optional backend subtreep ext-plist))
  3779. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3780. ;; Declare and autoload functions from org-agenda.el
  3781. (eval-and-compile
  3782. (org-autoload "org-agenda"
  3783. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3784. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3785. (declare-function org-clock-update-mode-line "org-clock" ())
  3786. (declare-function org-resolve-clocks "org-clock"
  3787. (&optional also-non-dangling-p prompt last-valid))
  3788. (defun org-at-TBLFM-p (&optional pos)
  3789. "Return t when point (or POS) is in #+TBLFM line."
  3790. (save-excursion
  3791. (let ((pos pos)))
  3792. (goto-char (or pos (point)))
  3793. (beginning-of-line 1)
  3794. (looking-at org-TBLFM-regexp)))
  3795. (defvar org-clock-start-time)
  3796. (defvar org-clock-marker (make-marker)
  3797. "Marker recording the last clock-in.")
  3798. (defvar org-clock-hd-marker (make-marker)
  3799. "Marker recording the last clock-in, but the headline position.")
  3800. (defvar org-clock-heading ""
  3801. "The heading of the current clock entry.")
  3802. (defun org-clock-is-active ()
  3803. "Return non-nil if clock is currently running.
  3804. The return value is actually the clock marker."
  3805. (marker-buffer org-clock-marker))
  3806. (eval-and-compile
  3807. (org-autoload "org-clock" '(org-clock-remove-overlays
  3808. org-clock-update-time-maybe
  3809. org-clocktable-shift)))
  3810. (defun org-check-running-clock ()
  3811. "Check if the current buffer contains the running clock.
  3812. If yes, offer to stop it and to save the buffer with the changes."
  3813. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3814. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3815. (buffer-name))))
  3816. (org-clock-out)
  3817. (when (y-or-n-p "Save changed buffer?")
  3818. (save-buffer))))
  3819. (defun org-clocktable-try-shift (dir n)
  3820. "Check if this line starts a clock table, if yes, shift the time block."
  3821. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3822. (org-clocktable-shift dir n)))
  3823. ;;;###autoload
  3824. (defun org-clock-persistence-insinuate ()
  3825. "Set up hooks for clock persistence."
  3826. (require 'org-clock)
  3827. (add-hook 'org-mode-hook 'org-clock-load)
  3828. (add-hook 'kill-emacs-hook 'org-clock-save))
  3829. ;; Define the variable already here, to make sure we have it.
  3830. (defvar org-indent-mode nil
  3831. "Non-nil if Org-Indent mode is enabled.
  3832. Use the command `org-indent-mode' to change this variable.")
  3833. ;; Autoload archiving code
  3834. ;; The stuff that is needed for cycling and tags has to be defined here.
  3835. (defgroup org-archive nil
  3836. "Options concerning archiving in Org-mode."
  3837. :tag "Org Archive"
  3838. :group 'org-structure)
  3839. (defcustom org-archive-location "%s_archive::"
  3840. "The location where subtrees should be archived.
  3841. The value of this variable is a string, consisting of two parts,
  3842. separated by a double-colon. The first part is a filename and
  3843. the second part is a headline.
  3844. When the filename is omitted, archiving happens in the same file.
  3845. %s in the filename will be replaced by the current file
  3846. name (without the directory part). Archiving to a different file
  3847. is useful to keep archived entries from contributing to the
  3848. Org-mode Agenda.
  3849. The archived entries will be filed as subtrees of the specified
  3850. headline. When the headline is omitted, the subtrees are simply
  3851. filed away at the end of the file, as top-level entries. Also in
  3852. the heading you can use %s to represent the file name, this can be
  3853. useful when using the same archive for a number of different files.
  3854. Here are a few examples:
  3855. \"%s_archive::\"
  3856. If the current file is Projects.org, archive in file
  3857. Projects.org_archive, as top-level trees. This is the default.
  3858. \"::* Archived Tasks\"
  3859. Archive in the current file, under the top-level headline
  3860. \"* Archived Tasks\".
  3861. \"~/org/archive.org::\"
  3862. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3863. \"~/org/archive.org::* From %s\"
  3864. Archive in file ~/org/archive.org (absolute path), under headlines
  3865. \"From FILENAME\" where file name is the current file name.
  3866. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3867. The \"datetree/\" string is special, signifying to archive
  3868. items to the datetree. Items are placed in either the CLOSED
  3869. date of the item, or the current date if there is no CLOSED date.
  3870. The heading will be a subentry to the current date. There doesn't
  3871. need to be a heading, but there always needs to be a slash after
  3872. datetree. For example, to store archived items directly in the
  3873. datetree, use \"~/org/datetree.org::datetree/\".
  3874. \"basement::** Finished Tasks\"
  3875. Archive in file ./basement (relative path), as level 3 trees
  3876. below the level 2 heading \"** Finished Tasks\".
  3877. You may set this option on a per-file basis by adding to the buffer a
  3878. line like
  3879. #+ARCHIVE: basement::** Finished Tasks
  3880. You may also define it locally for a subtree by setting an ARCHIVE property
  3881. in the entry. If such a property is found in an entry, or anywhere up
  3882. the hierarchy, it will be used."
  3883. :group 'org-archive
  3884. :type 'string)
  3885. (defcustom org-archive-tag "ARCHIVE"
  3886. "The tag that marks a subtree as archived.
  3887. An archived subtree does not open during visibility cycling, and does
  3888. not contribute to the agenda listings.
  3889. After changing this, font-lock must be restarted in the relevant buffers to
  3890. get the proper fontification."
  3891. :group 'org-archive
  3892. :group 'org-keywords
  3893. :type 'string)
  3894. (defcustom org-agenda-skip-archived-trees t
  3895. "Non-nil means the agenda will skip any items located in archived trees.
  3896. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3897. variable is no longer recommended, you should leave it at the value t.
  3898. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3899. :group 'org-archive
  3900. :group 'org-agenda-skip
  3901. :type 'boolean)
  3902. (defcustom org-columns-skip-archived-trees t
  3903. "Non-nil means ignore archived trees when creating column view."
  3904. :group 'org-archive
  3905. :group 'org-properties
  3906. :type 'boolean)
  3907. (defcustom org-cycle-open-archived-trees nil
  3908. "Non-nil means `org-cycle' will open archived trees.
  3909. An archived tree is a tree marked with the tag ARCHIVE.
  3910. When nil, archived trees will stay folded. You can still open them with
  3911. normal outline commands like `show-all', but not with the cycling commands."
  3912. :group 'org-archive
  3913. :group 'org-cycle
  3914. :type 'boolean)
  3915. (defcustom org-sparse-tree-open-archived-trees nil
  3916. "Non-nil means sparse tree construction shows matches in archived trees.
  3917. When nil, matches in these trees are highlighted, but the trees are kept in
  3918. collapsed state."
  3919. :group 'org-archive
  3920. :group 'org-sparse-trees
  3921. :type 'boolean)
  3922. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3923. "The default date type when building a sparse tree.
  3924. When this is nil, a date is a scheduled or a deadline timestamp.
  3925. Otherwise, these types are allowed:
  3926. all: all timestamps
  3927. active: only active timestamps (<...>)
  3928. inactive: only inactive timestamps (<...)
  3929. scheduled: only scheduled timestamps
  3930. deadline: only deadline timestamps"
  3931. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  3932. (const :tag "All timestamps" all)
  3933. (const :tag "Only active timestamps" active)
  3934. (const :tag "Only inactive timestamps" inactive)
  3935. (const :tag "Only scheduled timestamps" scheduled)
  3936. (const :tag "Only deadline timestamps" deadline)
  3937. (const :tag "Only closed timestamps" closed))
  3938. :version "24.3"
  3939. :group 'org-sparse-trees)
  3940. (defun org-cycle-hide-archived-subtrees (state)
  3941. "Re-hide all archived subtrees after a visibility state change."
  3942. (when (and (not org-cycle-open-archived-trees)
  3943. (not (memq state '(overview folded))))
  3944. (save-excursion
  3945. (let* ((globalp (memq state '(contents all)))
  3946. (beg (if globalp (point-min) (point)))
  3947. (end (if globalp (point-max) (org-end-of-subtree t))))
  3948. (org-hide-archived-subtrees beg end)
  3949. (goto-char beg)
  3950. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3951. (message "%s" (substitute-command-keys
  3952. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3953. (defun org-force-cycle-archived ()
  3954. "Cycle subtree even if it is archived."
  3955. (interactive)
  3956. (setq this-command 'org-cycle)
  3957. (let ((org-cycle-open-archived-trees t))
  3958. (call-interactively 'org-cycle)))
  3959. (defun org-hide-archived-subtrees (beg end)
  3960. "Re-hide all archived subtrees after a visibility state change."
  3961. (save-excursion
  3962. (let* ((re (concat ":" org-archive-tag ":")))
  3963. (goto-char beg)
  3964. (while (re-search-forward re end t)
  3965. (when (org-at-heading-p)
  3966. (org-flag-subtree t)
  3967. (org-end-of-subtree t))))))
  3968. (declare-function outline-end-of-heading "outline" ())
  3969. (declare-function outline-flag-region "outline" (from to flag))
  3970. (defun org-flag-subtree (flag)
  3971. (save-excursion
  3972. (org-back-to-heading t)
  3973. (outline-end-of-heading)
  3974. (outline-flag-region (point)
  3975. (progn (org-end-of-subtree t) (point))
  3976. flag)))
  3977. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3978. (eval-and-compile
  3979. (org-autoload "org-archive"
  3980. '(org-add-archive-files)))
  3981. ;; Autoload Column View Code
  3982. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3983. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3984. (declare-function org-columns-compute "org-colview" (property))
  3985. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3986. '(org-columns-number-to-string
  3987. org-columns-get-format-and-top-level
  3988. org-columns-compute
  3989. org-columns-remove-overlays))
  3990. ;; Autoload ID code
  3991. (declare-function org-id-store-link "org-id")
  3992. (declare-function org-id-locations-load "org-id")
  3993. (declare-function org-id-locations-save "org-id")
  3994. (defvar org-id-track-globally)
  3995. (org-autoload "org-id"
  3996. '(org-id-new
  3997. org-id-copy
  3998. org-id-get-with-outline-path-completion
  3999. org-id-get-with-outline-drilling))
  4000. ;;; Variables for pre-computed regular expressions, all buffer local
  4001. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  4002. "Matches first line of a hidden block.")
  4003. (make-variable-buffer-local 'org-drawer-regexp)
  4004. (defvar org-todo-regexp nil
  4005. "Matches any of the TODO state keywords.")
  4006. (make-variable-buffer-local 'org-todo-regexp)
  4007. (defvar org-not-done-regexp nil
  4008. "Matches any of the TODO state keywords except the last one.")
  4009. (make-variable-buffer-local 'org-not-done-regexp)
  4010. (defvar org-not-done-heading-regexp nil
  4011. "Matches a TODO headline that is not done.")
  4012. (make-variable-buffer-local 'org-not-done-regexp)
  4013. (defvar org-todo-line-regexp nil
  4014. "Matches a headline and puts TODO state into group 2 if present.")
  4015. (make-variable-buffer-local 'org-todo-line-regexp)
  4016. (defvar org-complex-heading-regexp nil
  4017. "Matches a headline and puts everything into groups:
  4018. group 1: the stars
  4019. group 2: The todo keyword, maybe
  4020. group 3: Priority cookie
  4021. group 4: True headline
  4022. group 5: Tags")
  4023. (make-variable-buffer-local 'org-complex-heading-regexp)
  4024. (defvar org-complex-heading-regexp-format nil
  4025. "Printf format to make regexp to match an exact headline.
  4026. This regexp will match the headline of any node which has the
  4027. exact headline text that is put into the format, but may have any
  4028. TODO state, priority and tags.")
  4029. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4030. (defvar org-todo-line-tags-regexp nil
  4031. "Matches a headline and puts TODO state into group 2 if present.
  4032. Also put tags into group 4 if tags are present.")
  4033. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4034. (defvar org-ds-keyword-length 12
  4035. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4036. (make-variable-buffer-local 'org-ds-keyword-length)
  4037. (defvar org-deadline-regexp nil
  4038. "Matches the DEADLINE keyword.")
  4039. (make-variable-buffer-local 'org-deadline-regexp)
  4040. (defvar org-deadline-time-regexp nil
  4041. "Matches the DEADLINE keyword together with a time stamp.")
  4042. (make-variable-buffer-local 'org-deadline-time-regexp)
  4043. (defvar org-deadline-time-hour-regexp nil
  4044. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4045. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4046. (defvar org-deadline-line-regexp nil
  4047. "Matches the DEADLINE keyword and the rest of the line.")
  4048. (make-variable-buffer-local 'org-deadline-line-regexp)
  4049. (defvar org-scheduled-regexp nil
  4050. "Matches the SCHEDULED keyword.")
  4051. (make-variable-buffer-local 'org-scheduled-regexp)
  4052. (defvar org-scheduled-time-regexp nil
  4053. "Matches the SCHEDULED keyword together with a time stamp.")
  4054. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4055. (defvar org-scheduled-time-hour-regexp nil
  4056. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4057. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4058. (defvar org-closed-time-regexp nil
  4059. "Matches the CLOSED keyword together with a time stamp.")
  4060. (make-variable-buffer-local 'org-closed-time-regexp)
  4061. (defvar org-keyword-time-regexp nil
  4062. "Matches any of the 4 keywords, together with the time stamp.")
  4063. (make-variable-buffer-local 'org-keyword-time-regexp)
  4064. (defvar org-keyword-time-not-clock-regexp nil
  4065. "Matches any of the 3 keywords, together with the time stamp.")
  4066. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4067. (defvar org-maybe-keyword-time-regexp nil
  4068. "Matches a timestamp, possibly preceded by a keyword.")
  4069. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4070. (defvar org-all-time-keywords nil
  4071. "List of time keywords.")
  4072. (make-variable-buffer-local 'org-all-time-keywords)
  4073. (defconst org-plain-time-of-day-regexp
  4074. (concat
  4075. "\\(\\<[012]?[0-9]"
  4076. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4077. "\\(--?"
  4078. "\\(\\<[012]?[0-9]"
  4079. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4080. "\\)?")
  4081. "Regular expression to match a plain time or time range.
  4082. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4083. groups carry important information:
  4084. 0 the full match
  4085. 1 the first time, range or not
  4086. 8 the second time, if it is a range.")
  4087. (defconst org-plain-time-extension-regexp
  4088. (concat
  4089. "\\(\\<[012]?[0-9]"
  4090. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4091. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4092. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4093. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4094. groups carry important information:
  4095. 0 the full match
  4096. 7 hours of duration
  4097. 9 minutes of duration")
  4098. (defconst org-stamp-time-of-day-regexp
  4099. (concat
  4100. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4101. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4102. "\\(--?"
  4103. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4104. "Regular expression to match a timestamp time or time range.
  4105. After a match, the following groups carry important information:
  4106. 0 the full match
  4107. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4108. 2 the first time, range or not
  4109. 4 the second time, if it is a range.")
  4110. (defconst org-startup-options
  4111. '(("fold" org-startup-folded t)
  4112. ("overview" org-startup-folded t)
  4113. ("nofold" org-startup-folded nil)
  4114. ("showall" org-startup-folded nil)
  4115. ("showeverything" org-startup-folded showeverything)
  4116. ("content" org-startup-folded content)
  4117. ("indent" org-startup-indented t)
  4118. ("noindent" org-startup-indented nil)
  4119. ("hidestars" org-hide-leading-stars t)
  4120. ("showstars" org-hide-leading-stars nil)
  4121. ("odd" org-odd-levels-only t)
  4122. ("oddeven" org-odd-levels-only nil)
  4123. ("align" org-startup-align-all-tables t)
  4124. ("noalign" org-startup-align-all-tables nil)
  4125. ("inlineimages" org-startup-with-inline-images t)
  4126. ("noinlineimages" org-startup-with-inline-images nil)
  4127. ("latexpreview" org-startup-with-latex-preview t)
  4128. ("nolatexpreview" org-startup-with-latex-preview nil)
  4129. ("customtime" org-display-custom-times t)
  4130. ("logdone" org-log-done time)
  4131. ("lognotedone" org-log-done note)
  4132. ("nologdone" org-log-done nil)
  4133. ("lognoteclock-out" org-log-note-clock-out t)
  4134. ("nolognoteclock-out" org-log-note-clock-out nil)
  4135. ("logrepeat" org-log-repeat state)
  4136. ("lognoterepeat" org-log-repeat note)
  4137. ("logdrawer" org-log-into-drawer t)
  4138. ("nologdrawer" org-log-into-drawer nil)
  4139. ("logstatesreversed" org-log-states-order-reversed t)
  4140. ("nologstatesreversed" org-log-states-order-reversed nil)
  4141. ("nologrepeat" org-log-repeat nil)
  4142. ("logreschedule" org-log-reschedule time)
  4143. ("lognotereschedule" org-log-reschedule note)
  4144. ("nologreschedule" org-log-reschedule nil)
  4145. ("logredeadline" org-log-redeadline time)
  4146. ("lognoteredeadline" org-log-redeadline note)
  4147. ("nologredeadline" org-log-redeadline nil)
  4148. ("logrefile" org-log-refile time)
  4149. ("lognoterefile" org-log-refile note)
  4150. ("nologrefile" org-log-refile nil)
  4151. ("fninline" org-footnote-define-inline t)
  4152. ("nofninline" org-footnote-define-inline nil)
  4153. ("fnlocal" org-footnote-section nil)
  4154. ("fnauto" org-footnote-auto-label t)
  4155. ("fnprompt" org-footnote-auto-label nil)
  4156. ("fnconfirm" org-footnote-auto-label confirm)
  4157. ("fnplain" org-footnote-auto-label plain)
  4158. ("fnadjust" org-footnote-auto-adjust t)
  4159. ("nofnadjust" org-footnote-auto-adjust nil)
  4160. ("constcgs" constants-unit-system cgs)
  4161. ("constSI" constants-unit-system SI)
  4162. ("noptag" org-tag-persistent-alist nil)
  4163. ("hideblocks" org-hide-block-startup t)
  4164. ("nohideblocks" org-hide-block-startup nil)
  4165. ("beamer" org-startup-with-beamer-mode t)
  4166. ("entitiespretty" org-pretty-entities t)
  4167. ("entitiesplain" org-pretty-entities nil))
  4168. "Variable associated with STARTUP options for org-mode.
  4169. Each element is a list of three items: the startup options (as written
  4170. in the #+STARTUP line), the corresponding variable, and the value to set
  4171. this variable to if the option is found. An optional forth element PUSH
  4172. means to push this value onto the list in the variable.")
  4173. (defun org-update-property-plist (key val props)
  4174. "Update PROPS with KEY and VAL."
  4175. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4176. (key (if appending (substring key 0 (- (length key) 1)) key))
  4177. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4178. (previous (cdr (assoc key props))))
  4179. (if appending
  4180. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4181. (cons (cons key val) remainder))))
  4182. (defconst org-block-regexp
  4183. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4184. "Regular expression for hiding blocks.")
  4185. (defconst org-heading-keyword-regexp-format
  4186. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4187. "Printf format for a regexp matching a headline with some keyword.
  4188. This regexp will match the headline of any node which has the
  4189. exact keyword that is put into the format. The keyword isn't in
  4190. any group by default, but the stars and the body are.")
  4191. (defconst org-heading-keyword-maybe-regexp-format
  4192. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4193. "Printf format for a regexp matching a headline, possibly with some keyword.
  4194. This regexp can match any headline with the specified keyword, or
  4195. without a keyword. The keyword isn't in any group by default,
  4196. but the stars and the body are.")
  4197. (defcustom org-group-tags t
  4198. "When non-nil (the default), use group tags.
  4199. This can be turned on/off through `org-toggle-tags-groups'."
  4200. :group 'org-tags
  4201. :group 'org-startup
  4202. :type 'boolean)
  4203. (defun org-toggle-tags-groups ()
  4204. "Toggle support for group tags.
  4205. Support for group tags is controlled by the option
  4206. `org-group-tags', which is non-nil by default."
  4207. (interactive)
  4208. (setq org-group-tags (not org-group-tags))
  4209. (cond ((and (derived-mode-p 'org-agenda-mode)
  4210. org-group-tags)
  4211. (org-agenda-redo))
  4212. ((derived-mode-p 'org-mode)
  4213. (let ((org-inhibit-startup t)) (org-mode))))
  4214. (message "Groups tags support has been turned %s"
  4215. (if org-group-tags "on" "off")))
  4216. (defun org-set-regexps-and-options-for-tags ()
  4217. "Precompute regular expressions used for tags in the current buffer."
  4218. (when (derived-mode-p 'org-mode)
  4219. (org-set-local 'org-file-tags nil)
  4220. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4221. (splitre "[ \t]+")
  4222. (start 0)
  4223. tags ftags key value)
  4224. (save-excursion
  4225. (save-restriction
  4226. (widen)
  4227. (goto-char (point-min))
  4228. (while (re-search-forward re nil t)
  4229. (setq key (upcase (org-match-string-no-properties 1))
  4230. value (org-match-string-no-properties 2))
  4231. (if (stringp value) (setq value (org-trim value)))
  4232. (cond
  4233. ((equal key "TAGS")
  4234. (setq tags (append tags (if tags '("\\n") nil)
  4235. (org-split-string value splitre))))
  4236. ((equal key "FILETAGS")
  4237. (when (string-match "\\S-" value)
  4238. (setq ftags
  4239. (append
  4240. ftags
  4241. (apply 'append
  4242. (mapcar (lambda (x) (org-split-string x ":"))
  4243. (org-split-string value)))))))))))
  4244. ;; Process the file tags.
  4245. (and ftags (org-set-local 'org-file-tags
  4246. (mapcar 'org-add-prop-inherited ftags)))
  4247. (org-set-local 'org-tag-groups-alist nil)
  4248. ;; Process the tags.
  4249. ;; FIXME
  4250. (when tags
  4251. (let (e tgs g)
  4252. (while (setq e (pop tags))
  4253. (cond
  4254. ((equal e "{")
  4255. (progn (push '(:startgroup) tgs)
  4256. (when (equal (nth 1 tags) ":")
  4257. (push (list (replace-regexp-in-string
  4258. "(.+)$" "" (nth 0 tags)))
  4259. org-tag-groups-alist)
  4260. (setq g 0))))
  4261. ((equal e ":") (push '(:grouptags) tgs))
  4262. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4263. ((equal e "\\n") (push '(:newline) tgs))
  4264. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4265. (push (cons (match-string 1 e)
  4266. (string-to-char (match-string 2 e))) tgs)
  4267. (if (and g (> g 0))
  4268. (setcar org-tag-groups-alist
  4269. (append (car org-tag-groups-alist)
  4270. (list (match-string 1 e)))))
  4271. (if g (setq g (1+ g))))
  4272. (t (push (list e) tgs)
  4273. (if (and g (> g 0))
  4274. (setcar org-tag-groups-alist
  4275. (append (car org-tag-groups-alist) (list e))))
  4276. (if g (setq g (1+ g))))))
  4277. (org-set-local 'org-tag-alist nil)
  4278. (while (setq e (pop tgs))
  4279. (or (and (stringp (car e))
  4280. (assoc (car e) org-tag-alist))
  4281. (push e org-tag-alist))))))))
  4282. (defun org-set-regexps-and-options ()
  4283. "Precompute regular expressions used in the current buffer."
  4284. (when (derived-mode-p 'org-mode)
  4285. (org-set-local 'org-todo-kwd-alist nil)
  4286. (org-set-local 'org-todo-key-alist nil)
  4287. (org-set-local 'org-todo-key-trigger nil)
  4288. (org-set-local 'org-todo-keywords-1 nil)
  4289. (org-set-local 'org-done-keywords nil)
  4290. (org-set-local 'org-todo-heads nil)
  4291. (org-set-local 'org-todo-sets nil)
  4292. (org-set-local 'org-todo-log-states nil)
  4293. (org-set-local 'org-file-properties nil)
  4294. (let ((re (org-make-options-regexp
  4295. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4296. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4297. "SETUPFILE" "OPTIONS")
  4298. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4299. (splitre "[ \t]+")
  4300. (scripts org-use-sub-superscripts)
  4301. kwds kws0 kwsa key log value cat arch const links hw dws
  4302. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4303. (start 0))
  4304. (save-excursion
  4305. (save-restriction
  4306. (widen)
  4307. (goto-char (point-min))
  4308. (while (or (and ext-setup-or-nil
  4309. (let (ret)
  4310. (with-temp-buffer
  4311. (insert ext-setup-or-nil)
  4312. (let ((major-mode 'org-mode))
  4313. (org-set-regexps-and-options-for-tags)
  4314. (setq ret (list org-file-tags org-tag-alist
  4315. org-tag-groups-alist))))
  4316. (setq org-file-tags (nth 0 ret)
  4317. org-tag-alist (nth 1 ret)
  4318. org-tag-groups-alist (nth 2 ret))))
  4319. (and ext-setup-or-nil
  4320. (string-match re ext-setup-or-nil start)
  4321. (setq start (match-end 0)))
  4322. (and (setq ext-setup-or-nil nil start 0)
  4323. (re-search-forward re nil t)))
  4324. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4325. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4326. (if (stringp value) (setq value (org-trim value)))
  4327. (cond
  4328. ((equal key "CATEGORY")
  4329. (setq cat value))
  4330. ((member key '("SEQ_TODO" "TODO"))
  4331. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4332. ((equal key "TYP_TODO")
  4333. (push (cons 'type (org-split-string value splitre)) kwds))
  4334. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4335. ;; general TODO-like setup
  4336. (push (cons (intern (downcase (match-string 1 key)))
  4337. (org-split-string value splitre)) kwds))
  4338. ((equal key "COLUMNS")
  4339. (org-set-local 'org-columns-default-format value))
  4340. ((equal key "LINK")
  4341. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4342. (push (cons (match-string 1 value)
  4343. (org-trim (match-string 2 value)))
  4344. links)))
  4345. ((equal key "PRIORITIES")
  4346. (setq prio (org-split-string value " +")))
  4347. ((equal key "PROPERTY")
  4348. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4349. (setq props (org-update-property-plist (match-string 1 value)
  4350. (match-string 2 value)
  4351. props))))
  4352. ((equal key "DRAWERS")
  4353. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4354. ((equal key "CONSTANTS")
  4355. (org-table-set-constants))
  4356. ((equal key "STARTUP")
  4357. (let ((opts (org-split-string value splitre))
  4358. l var val)
  4359. (while (setq l (pop opts))
  4360. (when (setq l (assoc l org-startup-options))
  4361. (setq var (nth 1 l) val (nth 2 l))
  4362. (if (not (nth 3 l))
  4363. (set (make-local-variable var) val)
  4364. (if (not (listp (symbol-value var)))
  4365. (set (make-local-variable var) nil))
  4366. (set (make-local-variable var) (symbol-value var))
  4367. (add-to-list var val))))))
  4368. ((equal key "ARCHIVE")
  4369. (setq arch value)
  4370. (remove-text-properties 0 (length arch)
  4371. '(face t fontified t) arch))
  4372. ((equal key "OPTIONS")
  4373. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4374. (setq scripts (read (match-string 2 value)))))
  4375. ((and (equal key "SETUPFILE")
  4376. ;; Prevent checking in Gnus messages
  4377. (not buffer-read-only))
  4378. (setq setup-contents (org-file-contents
  4379. (expand-file-name
  4380. (org-remove-double-quotes value))
  4381. 'noerror))
  4382. (if (not ext-setup-or-nil)
  4383. (setq ext-setup-or-nil setup-contents start 0)
  4384. (setq ext-setup-or-nil
  4385. (concat (substring ext-setup-or-nil 0 start)
  4386. "\n" setup-contents "\n"
  4387. (substring ext-setup-or-nil start)))))))
  4388. ;; search for property blocks
  4389. (goto-char (point-min))
  4390. (while (re-search-forward org-block-regexp nil t)
  4391. (when (equal "PROPERTY" (upcase (match-string 1)))
  4392. (setq value (replace-regexp-in-string
  4393. "[\n\r]" " " (match-string 4)))
  4394. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4395. (setq props (org-update-property-plist (match-string 1 value)
  4396. (match-string 2 value)
  4397. props)))))))
  4398. (org-set-local 'org-use-sub-superscripts scripts)
  4399. (when cat
  4400. (org-set-local 'org-category (intern cat))
  4401. (push (cons "CATEGORY" cat) props))
  4402. (when prio
  4403. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4404. (setq prio (mapcar 'string-to-char prio))
  4405. (org-set-local 'org-highest-priority (nth 0 prio))
  4406. (org-set-local 'org-lowest-priority (nth 1 prio))
  4407. (org-set-local 'org-default-priority (nth 2 prio)))
  4408. (and props (org-set-local 'org-file-properties (nreverse props)))
  4409. (and drawers (org-set-local 'org-drawers drawers))
  4410. (and arch (org-set-local 'org-archive-location arch))
  4411. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4412. ;; Process the TODO keywords
  4413. (unless kwds
  4414. ;; Use the global values as if they had been given locally.
  4415. (setq kwds (default-value 'org-todo-keywords))
  4416. (if (stringp (car kwds))
  4417. (setq kwds (list (cons org-todo-interpretation
  4418. (default-value 'org-todo-keywords)))))
  4419. (setq kwds (reverse kwds)))
  4420. (setq kwds (nreverse kwds))
  4421. (let (inter kws kw)
  4422. (while (setq kws (pop kwds))
  4423. (let ((kws (or
  4424. (run-hook-with-args-until-success
  4425. 'org-todo-setup-filter-hook kws)
  4426. kws)))
  4427. (setq inter (pop kws) sep (member "|" kws)
  4428. kws0 (delete "|" (copy-sequence kws))
  4429. kwsa nil
  4430. kws1 (mapcar
  4431. (lambda (x)
  4432. ;; 1 2
  4433. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4434. (progn
  4435. (setq kw (match-string 1 x)
  4436. key (and (match-end 2) (match-string 2 x))
  4437. log (org-extract-log-state-settings x))
  4438. (push (cons kw (and key (string-to-char key))) kwsa)
  4439. (and log (push log org-todo-log-states))
  4440. kw)
  4441. (error "Invalid TODO keyword %s" x)))
  4442. kws0)
  4443. kwsa (if kwsa (append '((:startgroup))
  4444. (nreverse kwsa)
  4445. '((:endgroup))))
  4446. hw (car kws1)
  4447. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4448. tail (list inter hw (car dws) (org-last dws))))
  4449. (add-to-list 'org-todo-heads hw 'append)
  4450. (push kws1 org-todo-sets)
  4451. (setq org-done-keywords (append org-done-keywords dws nil))
  4452. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4453. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4454. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4455. (setq org-todo-sets (nreverse org-todo-sets)
  4456. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4457. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4458. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4459. ;; Compute the regular expressions and other local variables.
  4460. ;; Using `org-outline-regexp-bol' would complicate them much,
  4461. ;; because of the fixed white space at the end of that string.
  4462. (if (not org-done-keywords)
  4463. (setq org-done-keywords (and org-todo-keywords-1
  4464. (list (org-last org-todo-keywords-1)))))
  4465. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4466. (length org-scheduled-string)
  4467. (length org-clock-string)
  4468. (length org-closed-string)))
  4469. org-drawer-regexp
  4470. (concat "^[ \t]*:\\("
  4471. (mapconcat 'regexp-quote org-drawers "\\|")
  4472. "\\):[ \t]*$")
  4473. org-not-done-keywords
  4474. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4475. org-todo-regexp
  4476. (concat "\\("
  4477. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4478. "\\)")
  4479. org-not-done-regexp
  4480. (concat "\\("
  4481. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4482. "\\)")
  4483. org-not-done-heading-regexp
  4484. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4485. org-todo-line-regexp
  4486. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4487. org-complex-heading-regexp
  4488. (concat "^\\(\\*+\\)"
  4489. "\\(?: +" org-todo-regexp "\\)?"
  4490. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4491. "\\(?: +\\(.*?\\)\\)??"
  4492. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4493. "[ \t]*$")
  4494. org-complex-heading-regexp-format
  4495. (concat "^\\(\\*+\\)"
  4496. "\\(?: +" org-todo-regexp "\\)?"
  4497. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4498. "\\(?: +"
  4499. ;; Stats cookies can be stuck to body.
  4500. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4501. "\\(%s\\)"
  4502. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4503. "\\)"
  4504. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4505. "[ \t]*$")
  4506. org-todo-line-tags-regexp
  4507. (concat "^\\(\\*+\\)"
  4508. "\\(?: +" org-todo-regexp "\\)?"
  4509. "\\(?: +\\(.*?\\)\\)??"
  4510. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4511. "[ \t]*$")
  4512. org-deadline-regexp (concat "\\<" org-deadline-string)
  4513. org-deadline-time-regexp
  4514. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4515. org-deadline-time-hour-regexp
  4516. (concat "\\<" org-deadline-string
  4517. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4518. org-deadline-line-regexp
  4519. (concat "\\<\\(" org-deadline-string "\\).*")
  4520. org-scheduled-regexp
  4521. (concat "\\<" org-scheduled-string)
  4522. org-scheduled-time-regexp
  4523. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4524. org-scheduled-time-hour-regexp
  4525. (concat "\\<" org-scheduled-string
  4526. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4527. org-closed-time-regexp
  4528. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4529. org-keyword-time-regexp
  4530. (concat "\\<\\(" org-scheduled-string
  4531. "\\|" org-deadline-string
  4532. "\\|" org-closed-string
  4533. "\\|" org-clock-string "\\)"
  4534. " *[[<]\\([^]>]+\\)[]>]")
  4535. org-keyword-time-not-clock-regexp
  4536. (concat "\\<\\(" org-scheduled-string
  4537. "\\|" org-deadline-string
  4538. "\\|" org-closed-string
  4539. "\\)"
  4540. " *[[<]\\([^]>]+\\)[]>]")
  4541. org-maybe-keyword-time-regexp
  4542. (concat "\\(\\<\\(" org-scheduled-string
  4543. "\\|" org-deadline-string
  4544. "\\|" org-closed-string
  4545. "\\|" org-clock-string "\\)\\)?"
  4546. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4547. org-all-time-keywords
  4548. (mapcar (lambda (w) (substring w 0 -1))
  4549. (list org-scheduled-string org-deadline-string
  4550. org-clock-string org-closed-string)))
  4551. (org-compute-latex-and-related-regexp)
  4552. (org-set-font-lock-defaults))))
  4553. (defun org-file-contents (file &optional noerror)
  4554. "Return the contents of FILE, as a string."
  4555. (if (or (not file)
  4556. (not (file-readable-p file)))
  4557. (if noerror
  4558. (message "Cannot read file \"%s\"" file)
  4559. (error "Cannot read file \"%s\"" file))
  4560. (with-temp-buffer
  4561. (insert-file-contents file)
  4562. (buffer-string))))
  4563. (defun org-extract-log-state-settings (x)
  4564. "Extract the log state setting from a TODO keyword string.
  4565. This will extract info from a string like \"WAIT(w@/!)\"."
  4566. (let (kw key log1 log2)
  4567. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4568. (setq kw (match-string 1 x)
  4569. key (and (match-end 2) (match-string 2 x))
  4570. log1 (and (match-end 3) (match-string 3 x))
  4571. log2 (and (match-end 4) (match-string 4 x)))
  4572. (and (or log1 log2)
  4573. (list kw
  4574. (and log1 (if (equal log1 "!") 'time 'note))
  4575. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4576. (defun org-remove-keyword-keys (list)
  4577. "Remove a pair of parenthesis at the end of each string in LIST."
  4578. (mapcar (lambda (x)
  4579. (if (string-match "(.*)$" x)
  4580. (substring x 0 (match-beginning 0))
  4581. x))
  4582. list))
  4583. (defun org-assign-fast-keys (alist)
  4584. "Assign fast keys to a keyword-key alist.
  4585. Respect keys that are already there."
  4586. (let (new e (alt ?0))
  4587. (while (setq e (pop alist))
  4588. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4589. (cdr e)) ;; Key already assigned.
  4590. (push e new)
  4591. (let ((clist (string-to-list (downcase (car e))))
  4592. (used (append new alist)))
  4593. (when (= (car clist) ?@)
  4594. (pop clist))
  4595. (while (and clist (rassoc (car clist) used))
  4596. (pop clist))
  4597. (unless clist
  4598. (while (rassoc alt used)
  4599. (incf alt)))
  4600. (push (cons (car e) (or (car clist) alt)) new))))
  4601. (nreverse new)))
  4602. ;;; Some variables used in various places
  4603. (defvar org-window-configuration nil
  4604. "Used in various places to store a window configuration.")
  4605. (defvar org-selected-window nil
  4606. "Used in various places to store a window configuration.")
  4607. (defvar org-finish-function nil
  4608. "Function to be called when `C-c C-c' is used.
  4609. This is for getting out of special buffers like capture.")
  4610. ;; FIXME: Occasionally check by commenting these, to make sure
  4611. ;; no other functions uses these, forgetting to let-bind them.
  4612. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4613. (defvar org-last-state)
  4614. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4615. ;; Defined somewhere in this file, but used before definition.
  4616. (defvar org-entities) ;; defined in org-entities.el
  4617. (defvar org-struct-menu)
  4618. (defvar org-org-menu)
  4619. (defvar org-tbl-menu)
  4620. ;;;; Define the Org-mode
  4621. ;; We use a before-change function to check if a table might need
  4622. ;; an update.
  4623. (defvar org-table-may-need-update t
  4624. "Indicates that a table might need an update.
  4625. This variable is set by `org-before-change-function'.
  4626. `org-table-align' sets it back to nil.")
  4627. (defun org-before-change-function (beg end)
  4628. "Every change indicates that a table might need an update."
  4629. (setq org-table-may-need-update t))
  4630. (defvar org-mode-map)
  4631. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4632. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4633. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4634. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4635. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4636. (defvar org-table-buffer-is-an nil)
  4637. (defvar bidi-paragraph-direction)
  4638. (defvar buffer-face-mode-face)
  4639. (require 'outline)
  4640. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4641. (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"))
  4642. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4643. ;; Other stuff we need.
  4644. (require 'time-date)
  4645. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4646. (require 'easymenu)
  4647. (require 'overlay)
  4648. ;; (require 'org-macs) moved higher up in the file before it is first used
  4649. (require 'org-entities)
  4650. ;; (require 'org-compat) moved higher up in the file before it is first used
  4651. (require 'org-faces)
  4652. (require 'org-list)
  4653. (require 'org-pcomplete)
  4654. (require 'org-src)
  4655. (require 'org-footnote)
  4656. (require 'org-macro)
  4657. ;; babel
  4658. (require 'ob)
  4659. ;;;###autoload
  4660. (define-derived-mode org-mode outline-mode "Org"
  4661. "Outline-based notes management and organizer, alias
  4662. \"Carsten's outline-mode for keeping track of everything.\"
  4663. Org-mode develops organizational tasks around a NOTES file which
  4664. contains information about projects as plain text. Org-mode is
  4665. implemented on top of outline-mode, which is ideal to keep the content
  4666. of large files well structured. It supports ToDo items, deadlines and
  4667. time stamps, which magically appear in the diary listing of the Emacs
  4668. calendar. Tables are easily created with a built-in table editor.
  4669. Plain text URL-like links connect to websites, emails (VM), Usenet
  4670. messages (Gnus), BBDB entries, and any files related to the project.
  4671. For printing and sharing of notes, an Org-mode file (or a part of it)
  4672. can be exported as a structured ASCII or HTML file.
  4673. The following commands are available:
  4674. \\{org-mode-map}"
  4675. ;; Get rid of Outline menus, they are not needed
  4676. ;; Need to do this here because define-derived-mode sets up
  4677. ;; the keymap so late. Still, it is a waste to call this each time
  4678. ;; we switch another buffer into org-mode.
  4679. (if (featurep 'xemacs)
  4680. (when (boundp 'outline-mode-menu-heading)
  4681. ;; Assume this is Greg's port, it uses easymenu
  4682. (easy-menu-remove outline-mode-menu-heading)
  4683. (easy-menu-remove outline-mode-menu-show)
  4684. (easy-menu-remove outline-mode-menu-hide))
  4685. (define-key org-mode-map [menu-bar headings] 'undefined)
  4686. (define-key org-mode-map [menu-bar hide] 'undefined)
  4687. (define-key org-mode-map [menu-bar show] 'undefined))
  4688. (org-load-modules-maybe)
  4689. (easy-menu-add org-org-menu)
  4690. (easy-menu-add org-tbl-menu)
  4691. (org-install-agenda-files-menu)
  4692. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4693. (add-to-invisibility-spec '(org-cwidth))
  4694. (add-to-invisibility-spec '(org-hide-block . t))
  4695. (when (featurep 'xemacs)
  4696. (org-set-local 'line-move-ignore-invisible t))
  4697. (org-set-local 'outline-regexp org-outline-regexp)
  4698. (org-set-local 'outline-level 'org-outline-level)
  4699. (setq bidi-paragraph-direction 'left-to-right)
  4700. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4701. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4702. (when (and org-ellipsis
  4703. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4704. (fboundp 'make-glyph-code))
  4705. (unless org-display-table
  4706. (setq org-display-table (make-display-table)))
  4707. (set-display-table-slot
  4708. org-display-table 4
  4709. (vconcat (mapcar
  4710. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4711. org-ellipsis)))
  4712. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4713. (setq buffer-display-table org-display-table))
  4714. (org-set-regexps-and-options-for-tags)
  4715. (org-set-regexps-and-options)
  4716. (when (and org-tag-faces (not org-tags-special-faces-re))
  4717. ;; tag faces set outside customize.... force initialization.
  4718. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4719. ;; Calc embedded
  4720. (org-set-local 'calc-embedded-open-mode "# ")
  4721. ;; Modify a few syntax entries
  4722. (modify-syntax-entry ?@ "w")
  4723. (modify-syntax-entry ?\" "\"")
  4724. (if org-startup-truncated (setq truncate-lines t))
  4725. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4726. (org-set-local 'font-lock-unfontify-region-function
  4727. 'org-unfontify-region)
  4728. ;; Activate before-change-function
  4729. (org-set-local 'org-table-may-need-update t)
  4730. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4731. 'local)
  4732. ;; Check for running clock before killing a buffer
  4733. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4734. ;; Initialize macros templates.
  4735. (org-macro-initialize-templates)
  4736. ;; Initialize radio targets.
  4737. (org-update-radio-target-regexp)
  4738. ;; Indentation.
  4739. (org-set-local 'indent-line-function 'org-indent-line)
  4740. (org-set-local 'indent-region-function 'org-indent-region)
  4741. ;; Filling and auto-filling.
  4742. (org-setup-filling)
  4743. ;; Comments.
  4744. (org-setup-comments-handling)
  4745. ;; Beginning/end of defun
  4746. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4747. (org-set-local 'end-of-defun-function 'org-forward-element)
  4748. ;; Next error for sparse trees
  4749. (org-set-local 'next-error-function 'org-occur-next-match)
  4750. ;; Make sure dependence stuff works reliably, even for users who set it
  4751. ;; too late :-(
  4752. (if org-enforce-todo-dependencies
  4753. (add-hook 'org-blocker-hook
  4754. 'org-block-todo-from-children-or-siblings-or-parent)
  4755. (remove-hook 'org-blocker-hook
  4756. 'org-block-todo-from-children-or-siblings-or-parent))
  4757. (if org-enforce-todo-checkbox-dependencies
  4758. (add-hook 'org-blocker-hook
  4759. 'org-block-todo-from-checkboxes)
  4760. (remove-hook 'org-blocker-hook
  4761. 'org-block-todo-from-checkboxes))
  4762. ;; Align options lines
  4763. (org-set-local
  4764. 'align-mode-rules-list
  4765. '((org-in-buffer-settings
  4766. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4767. (modes . '(org-mode)))))
  4768. ;; Imenu
  4769. (org-set-local 'imenu-create-index-function
  4770. 'org-imenu-get-tree)
  4771. ;; Make isearch reveal context
  4772. (if (or (featurep 'xemacs)
  4773. (not (boundp 'outline-isearch-open-invisible-function)))
  4774. ;; Emacs 21 and XEmacs make use of the hook
  4775. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4776. ;; Emacs 22 deals with this through a special variable
  4777. (org-set-local 'outline-isearch-open-invisible-function
  4778. (lambda (&rest ignore) (org-show-context 'isearch)))
  4779. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4780. ;; Setup the pcomplete hooks
  4781. (set (make-local-variable 'pcomplete-command-completion-function)
  4782. 'org-pcomplete-initial)
  4783. (set (make-local-variable 'pcomplete-command-name-function)
  4784. 'org-command-at-point)
  4785. (set (make-local-variable 'pcomplete-default-completion-function)
  4786. 'ignore)
  4787. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4788. 'org-parse-arguments)
  4789. (set (make-local-variable 'pcomplete-termination-string) "")
  4790. (when (>= emacs-major-version 23)
  4791. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4792. ;; If empty file that did not turn on org-mode automatically, make it to.
  4793. (if (and org-insert-mode-line-in-empty-file
  4794. (org-called-interactively-p 'any)
  4795. (= (point-min) (point-max)))
  4796. (insert "# -*- mode: org -*-\n\n"))
  4797. (unless org-inhibit-startup
  4798. (org-unmodified
  4799. (and org-startup-with-beamer-mode (org-beamer-mode))
  4800. (when org-startup-align-all-tables
  4801. (org-table-map-tables 'org-table-align 'quietly))
  4802. (when org-startup-with-inline-images
  4803. (org-display-inline-images))
  4804. (when org-startup-with-latex-preview
  4805. (org-preview-latex-fragment))
  4806. (unless org-inhibit-startup-visibility-stuff
  4807. (org-set-startup-visibility))))
  4808. ;; Try to set org-hide correctly
  4809. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4810. ;; Update `customize-package-emacs-version-alist'
  4811. (add-to-list 'customize-package-emacs-version-alist
  4812. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4813. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4814. ("8.0" . "24.4")))
  4815. (defvar org-mode-transpose-word-syntax-table
  4816. (let ((st (make-syntax-table)))
  4817. (mapc (lambda(c) (modify-syntax-entry
  4818. (string-to-char (car c)) "w p" st))
  4819. org-emphasis-alist)
  4820. st))
  4821. (when (fboundp 'abbrev-table-put)
  4822. (abbrev-table-put org-mode-abbrev-table
  4823. :parents (list text-mode-abbrev-table)))
  4824. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4825. (defsubst org-fix-ellipsis-at-bol ()
  4826. (save-excursion (goto-char (window-start)) (recenter 0)))
  4827. (defun org-find-invisible-foreground ()
  4828. (let ((candidates (remove
  4829. "unspecified-bg"
  4830. (nconc
  4831. (list (face-background 'default)
  4832. (face-background 'org-default))
  4833. (mapcar
  4834. (lambda (alist)
  4835. (when (boundp alist)
  4836. (cdr (assoc 'background-color (symbol-value alist)))))
  4837. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4838. (list (face-foreground 'org-hide))))))
  4839. (car (remove nil candidates))))
  4840. (defun org-current-time (&optional rounding-minutes past)
  4841. "Current time, possibly rounded to ROUNDING-MINUTES.
  4842. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4843. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4844. the rounding returns a past time."
  4845. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4846. (car org-time-stamp-rounding-minutes)))
  4847. (time (decode-time)) res)
  4848. (if (< r 1)
  4849. (current-time)
  4850. (setq res
  4851. (apply 'encode-time
  4852. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4853. (nthcdr 2 time))))
  4854. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4855. (seconds-to-time (- (org-float-time res) (* r 60)))
  4856. res))))
  4857. (defun org-today ()
  4858. "Return today date, considering `org-extend-today-until'."
  4859. (time-to-days
  4860. (time-subtract (current-time)
  4861. (list 0 (* 3600 org-extend-today-until) 0))))
  4862. ;;;; Font-Lock stuff, including the activators
  4863. (defvar org-mouse-map (make-sparse-keymap))
  4864. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4865. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4866. (when org-mouse-1-follows-link
  4867. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4868. (when org-tab-follows-link
  4869. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4870. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4871. (require 'font-lock)
  4872. (defconst org-non-link-chars "]\t\n\r<>")
  4873. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4874. "shell" "elisp" "doi" "message"))
  4875. (defvar org-link-types-re nil
  4876. "Matches a link that has a url-like prefix like \"http:\"")
  4877. (defvar org-link-re-with-space nil
  4878. "Matches a link with spaces, optional angular brackets around it.")
  4879. (defvar org-link-re-with-space2 nil
  4880. "Matches a link with spaces, optional angular brackets around it.")
  4881. (defvar org-link-re-with-space3 nil
  4882. "Matches a link with spaces, only for internal part in bracket links.")
  4883. (defvar org-angle-link-re nil
  4884. "Matches link with angular brackets, spaces are allowed.")
  4885. (defvar org-plain-link-re nil
  4886. "Matches plain link, without spaces.")
  4887. (defvar org-bracket-link-regexp nil
  4888. "Matches a link in double brackets.")
  4889. (defvar org-bracket-link-analytic-regexp nil
  4890. "Regular expression used to analyze links.
  4891. Here is what the match groups contain after a match:
  4892. 1: http:
  4893. 2: http
  4894. 3: path
  4895. 4: [desc]
  4896. 5: desc")
  4897. (defvar org-bracket-link-analytic-regexp++ nil
  4898. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4899. (defvar org-any-link-re nil
  4900. "Regular expression matching any link.")
  4901. (defconst org-match-sexp-depth 3
  4902. "Number of stacked braces for sub/superscript matching.")
  4903. (defun org-create-multibrace-regexp (left right n)
  4904. "Create a regular expression which will match a balanced sexp.
  4905. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4906. as single character strings.
  4907. The regexp returned will match the entire expression including the
  4908. delimiters. It will also define a single group which contains the
  4909. match except for the outermost delimiters. The maximum depth of
  4910. stacked delimiters is N. Escaping delimiters is not possible."
  4911. (let* ((nothing (concat "[^" left right "]*?"))
  4912. (or "\\|")
  4913. (re nothing)
  4914. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4915. (while (> n 1)
  4916. (setq n (1- n)
  4917. re (concat re or next)
  4918. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4919. (concat left "\\(" re "\\)" right)))
  4920. (defvar org-match-substring-regexp
  4921. (concat
  4922. "\\(\\S-\\)\\([_^]\\)\\("
  4923. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4924. "\\|"
  4925. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4926. "\\|"
  4927. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4928. "The regular expression matching a sub- or superscript.")
  4929. (defvar org-match-substring-with-braces-regexp
  4930. (concat
  4931. "\\(\\S-\\)\\([_^]\\)\\("
  4932. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4933. "\\)")
  4934. "The regular expression matching a sub- or superscript, forcing braces.")
  4935. (defun org-make-link-regexps ()
  4936. "Update the link regular expressions.
  4937. This should be called after the variable `org-link-types' has changed."
  4938. (setq org-link-types-re
  4939. (concat
  4940. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4941. org-link-re-with-space
  4942. (concat
  4943. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4944. "\\([^" org-non-link-chars " ]"
  4945. "[^" org-non-link-chars "]*"
  4946. "[^" org-non-link-chars " ]\\)>?")
  4947. org-link-re-with-space2
  4948. (concat
  4949. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4950. "\\([^" org-non-link-chars " ]"
  4951. "[^\t\n\r]*"
  4952. "[^" org-non-link-chars " ]\\)>?")
  4953. org-link-re-with-space3
  4954. (concat
  4955. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4956. "\\([^" org-non-link-chars " ]"
  4957. "[^\t\n\r]*\\)")
  4958. org-angle-link-re
  4959. (concat
  4960. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4961. "\\([^" org-non-link-chars " ]"
  4962. "[^" org-non-link-chars "]*"
  4963. "\\)>")
  4964. org-plain-link-re
  4965. (concat
  4966. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4967. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4968. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4969. org-bracket-link-regexp
  4970. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4971. org-bracket-link-analytic-regexp
  4972. (concat
  4973. "\\[\\["
  4974. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4975. "\\([^]]+\\)"
  4976. "\\]"
  4977. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4978. "\\]")
  4979. org-bracket-link-analytic-regexp++
  4980. (concat
  4981. "\\[\\["
  4982. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4983. "\\([^]]+\\)"
  4984. "\\]"
  4985. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4986. "\\]")
  4987. org-any-link-re
  4988. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4989. org-angle-link-re "\\)\\|\\("
  4990. org-plain-link-re "\\)")))
  4991. (org-make-link-regexps)
  4992. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4993. "Regular expression for fast time stamp matching.")
  4994. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4995. "Regular expression for fast time stamp matching.")
  4996. (defconst org-ts-regexp0
  4997. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4998. "Regular expression matching time strings for analysis.
  4999. This one does not require the space after the date, so it can be used
  5000. on a string that terminates immediately after the date.")
  5001. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5002. "Regular expression matching time strings for analysis.")
  5003. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5004. "Regular expression matching time stamps, with groups.")
  5005. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5006. "Regular expression matching time stamps (also [..]), with groups.")
  5007. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5008. "Regular expression matching a time stamp range.")
  5009. (defconst org-tr-regexp-both
  5010. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5011. "Regular expression matching a time stamp range.")
  5012. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5013. org-ts-regexp "\\)?")
  5014. "Regular expression matching a time stamp or time stamp range.")
  5015. (defconst org-tsr-regexp-both
  5016. (concat org-ts-regexp-both "\\(--?-?"
  5017. org-ts-regexp-both "\\)?")
  5018. "Regular expression matching a time stamp or time stamp range.
  5019. The time stamps may be either active or inactive.")
  5020. (defvar org-emph-face nil)
  5021. (defun org-do-emphasis-faces (limit)
  5022. "Run through the buffer and add overlays to emphasized strings."
  5023. (let (rtn a)
  5024. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5025. (if (not (= (char-after (match-beginning 3))
  5026. (char-after (match-beginning 4))))
  5027. (progn
  5028. (setq rtn t)
  5029. (setq a (assoc (match-string 3) org-emphasis-alist))
  5030. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5031. 'face
  5032. (nth 1 a))
  5033. (and (nth 4 a)
  5034. (org-remove-flyspell-overlays-in
  5035. (match-beginning 0) (match-end 0)))
  5036. (add-text-properties (match-beginning 2) (match-end 2)
  5037. '(font-lock-multiline t org-emphasis t))
  5038. (when org-hide-emphasis-markers
  5039. (add-text-properties (match-end 4) (match-beginning 5)
  5040. '(invisible org-link))
  5041. (add-text-properties (match-beginning 3) (match-end 3)
  5042. '(invisible org-link)))))
  5043. (backward-char 1))
  5044. rtn))
  5045. (defun org-emphasize (&optional char)
  5046. "Insert or change an emphasis, i.e. a font like bold or italic.
  5047. If there is an active region, change that region to a new emphasis.
  5048. If there is no region, just insert the marker characters and position
  5049. the cursor between them.
  5050. CHAR should be the marker character. If it is a space, it means to
  5051. remove the emphasis of the selected region.
  5052. If CHAR is not given (for example in an interactive call) it will be
  5053. prompted for."
  5054. (interactive)
  5055. (let ((erc org-emphasis-regexp-components)
  5056. (prompt "")
  5057. (string "") beg end move c s)
  5058. (if (org-region-active-p)
  5059. (setq beg (region-beginning) end (region-end)
  5060. string (buffer-substring beg end))
  5061. (setq move t))
  5062. (unless char
  5063. (message "Emphasis marker or tag: [%s]"
  5064. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5065. (setq char (read-char-exclusive)))
  5066. (if (equal char ?\ )
  5067. (setq s "" move nil)
  5068. (unless (assoc (char-to-string char) org-emphasis-alist)
  5069. (user-error "No such emphasis marker: \"%c\"" char))
  5070. (setq s (char-to-string char)))
  5071. (while (and (> (length string) 1)
  5072. (equal (substring string 0 1) (substring string -1))
  5073. (assoc (substring string 0 1) org-emphasis-alist))
  5074. (setq string (substring string 1 -1)))
  5075. (setq string (concat s string s))
  5076. (if beg (delete-region beg end))
  5077. (unless (or (bolp)
  5078. (string-match (concat "[" (nth 0 erc) "\n]")
  5079. (char-to-string (char-before (point)))))
  5080. (insert " "))
  5081. (unless (or (eobp)
  5082. (string-match (concat "[" (nth 1 erc) "\n]")
  5083. (char-to-string (char-after (point)))))
  5084. (insert " ") (backward-char 1))
  5085. (insert string)
  5086. (and move (backward-char 1))))
  5087. (defconst org-nonsticky-props
  5088. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5089. (defsubst org-rear-nonsticky-at (pos)
  5090. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5091. (defun org-activate-plain-links (limit)
  5092. "Run through the buffer and add overlays to links."
  5093. (let (f hl)
  5094. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5095. (not (org-in-src-block-p)))
  5096. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5097. (setq f (get-text-property (match-beginning 0) 'face))
  5098. (setq hl (org-match-string-no-properties 0))
  5099. (if (or (eq f 'org-tag)
  5100. (and (listp f) (memq 'org-tag f)))
  5101. nil
  5102. (add-text-properties (match-beginning 0) (match-end 0)
  5103. (list 'mouse-face 'highlight
  5104. 'face 'org-link
  5105. 'htmlize-link `(:uri ,hl)
  5106. 'keymap org-mouse-map))
  5107. (org-rear-nonsticky-at (match-end 0)))
  5108. t)))
  5109. (defun org-activate-code (limit)
  5110. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5111. (progn
  5112. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5113. (remove-text-properties (match-beginning 0) (match-end 0)
  5114. '(display t invisible t intangible t))
  5115. t)))
  5116. (defcustom org-src-fontify-natively nil
  5117. "When non-nil, fontify code in code blocks."
  5118. :type 'boolean
  5119. :version "24.1"
  5120. :group 'org-appearance
  5121. :group 'org-babel)
  5122. (defcustom org-allow-promoting-top-level-subtree nil
  5123. "When non-nil, allow promoting a top level subtree.
  5124. The leading star of the top level headline will be replaced
  5125. by a #."
  5126. :type 'boolean
  5127. :version "24.1"
  5128. :group 'org-appearance)
  5129. (defun org-fontify-meta-lines-and-blocks (limit)
  5130. (condition-case nil
  5131. (org-fontify-meta-lines-and-blocks-1 limit)
  5132. (error (message "org-mode fontification error"))))
  5133. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5134. "Fontify #+ lines and blocks."
  5135. (let ((case-fold-search t))
  5136. (if (re-search-forward
  5137. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5138. limit t)
  5139. (let ((beg (match-beginning 0))
  5140. (block-start (match-end 0))
  5141. (block-end nil)
  5142. (lang (match-string 7))
  5143. (beg1 (line-beginning-position 2))
  5144. (dc1 (downcase (match-string 2)))
  5145. (dc3 (downcase (match-string 3)))
  5146. end end1 quoting block-type ovl)
  5147. (cond
  5148. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5149. ;; a single line of backend-specific content
  5150. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5151. (remove-text-properties (match-beginning 0) (match-end 0)
  5152. '(display t invisible t intangible t))
  5153. (add-text-properties (match-beginning 1) (match-end 3)
  5154. '(font-lock-fontified t face org-meta-line))
  5155. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5156. '(font-lock-fontified t face org-block))
  5157. ; for backend-specific code
  5158. t)
  5159. ((and (match-end 4) (equal dc3 "+begin"))
  5160. ;; Truly a block
  5161. (setq block-type (downcase (match-string 5))
  5162. quoting (member block-type org-protecting-blocks))
  5163. (when (re-search-forward
  5164. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5165. nil t) ;; on purpose, we look further than LIMIT
  5166. (setq end (min (point-max) (match-end 0))
  5167. end1 (min (point-max) (1- (match-beginning 0))))
  5168. (setq block-end (match-beginning 0))
  5169. (when quoting
  5170. (remove-text-properties beg end
  5171. '(display t invisible t intangible t)))
  5172. (add-text-properties
  5173. beg end
  5174. '(font-lock-fontified t font-lock-multiline t))
  5175. (add-text-properties beg beg1 '(face org-meta-line))
  5176. (add-text-properties end1 (min (point-max) (1+ end))
  5177. '(face org-meta-line)) ; for end_src
  5178. (cond
  5179. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5180. (org-src-font-lock-fontify-block lang block-start block-end)
  5181. ;; remove old background overlays
  5182. (mapc (lambda (ov)
  5183. (if (eq (overlay-get ov 'face) 'org-block-background)
  5184. (delete-overlay ov)))
  5185. (overlays-at (/ (+ beg1 block-end) 2)))
  5186. ;; add a background overlay
  5187. (setq ovl (make-overlay beg1 block-end))
  5188. (overlay-put ovl 'face 'org-block-background)
  5189. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5190. (quoting
  5191. (add-text-properties beg1 (min (point-max) (1+ end1))
  5192. '(face org-block))) ; end of source block
  5193. ((not org-fontify-quote-and-verse-blocks))
  5194. ((string= block-type "quote")
  5195. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5196. ((string= block-type "verse")
  5197. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5198. (add-text-properties beg beg1 '(face org-block-begin-line))
  5199. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5200. '(face org-block-end-line))
  5201. t))
  5202. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5203. (add-text-properties
  5204. beg (match-end 3)
  5205. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5206. '(font-lock-fontified t invisible t)
  5207. '(font-lock-fontified t face org-document-info-keyword)))
  5208. (add-text-properties
  5209. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5210. (if (string-equal dc1 "+title:")
  5211. '(font-lock-fontified t face org-document-title)
  5212. '(font-lock-fontified t face org-document-info))))
  5213. ((or (equal dc1 "+results")
  5214. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5215. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5216. "+call:" "+header:" "+headers:" "+name:"))
  5217. (and (match-end 4) (equal dc3 "+attr")))
  5218. (add-text-properties
  5219. beg (match-end 0)
  5220. '(font-lock-fontified t face org-meta-line))
  5221. t)
  5222. ((member dc3 '(" " ""))
  5223. (add-text-properties
  5224. beg (match-end 0)
  5225. '(font-lock-fontified t face font-lock-comment-face)))
  5226. ((not (member (char-after beg) '(?\ ?\t)))
  5227. ;; just any other in-buffer setting, but not indented
  5228. (add-text-properties
  5229. beg (match-end 0)
  5230. '(font-lock-fontified t face org-meta-line))
  5231. t)
  5232. (t nil))))))
  5233. (defun org-activate-angle-links (limit)
  5234. "Run through the buffer and add overlays to links."
  5235. (if (and (re-search-forward org-angle-link-re limit t)
  5236. (not (org-in-src-block-p)))
  5237. (progn
  5238. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5239. (add-text-properties (match-beginning 0) (match-end 0)
  5240. (list 'mouse-face 'highlight
  5241. 'keymap org-mouse-map))
  5242. (org-rear-nonsticky-at (match-end 0))
  5243. t)))
  5244. (defun org-activate-footnote-links (limit)
  5245. "Run through the buffer and add overlays to footnotes."
  5246. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5247. (when fn
  5248. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5249. (org-remove-flyspell-overlays-in beg end)
  5250. (add-text-properties beg end
  5251. (list 'mouse-face 'highlight
  5252. 'keymap org-mouse-map
  5253. 'help-echo
  5254. (if (= (point-at-bol) beg)
  5255. "Footnote definition"
  5256. "Footnote reference")
  5257. 'font-lock-fontified t
  5258. 'font-lock-multiline t
  5259. 'face 'org-footnote))))))
  5260. (defun org-activate-bracket-links (limit)
  5261. "Run through the buffer and add overlays to bracketed links."
  5262. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5263. (not (org-in-src-block-p)))
  5264. (let* ((hl (org-match-string-no-properties 1))
  5265. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5266. (ip (org-maybe-intangible
  5267. (list 'invisible 'org-link
  5268. 'keymap org-mouse-map 'mouse-face 'highlight
  5269. 'font-lock-multiline t 'help-echo help
  5270. 'htmlize-link `(:uri ,hl))))
  5271. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5272. 'font-lock-multiline t 'help-echo help
  5273. 'htmlize-link `(:uri ,hl))))
  5274. ;; We need to remove the invisible property here. Table narrowing
  5275. ;; may have made some of this invisible.
  5276. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5277. (remove-text-properties (match-beginning 0) (match-end 0)
  5278. '(invisible nil))
  5279. (if (match-end 3)
  5280. (progn
  5281. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5282. (org-rear-nonsticky-at (match-beginning 3))
  5283. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5284. (org-rear-nonsticky-at (match-end 3))
  5285. (add-text-properties (match-end 3) (match-end 0) ip)
  5286. (org-rear-nonsticky-at (match-end 0)))
  5287. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5288. (org-rear-nonsticky-at (match-beginning 1))
  5289. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5290. (org-rear-nonsticky-at (match-end 1))
  5291. (add-text-properties (match-end 1) (match-end 0) ip)
  5292. (org-rear-nonsticky-at (match-end 0)))
  5293. t)))
  5294. (defun org-activate-dates (limit)
  5295. "Run through the buffer and add overlays to dates."
  5296. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5297. (not (equal (char-before (match-beginning 0)) 91)))
  5298. (progn
  5299. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5300. (add-text-properties (match-beginning 0) (match-end 0)
  5301. (list 'mouse-face 'highlight
  5302. 'keymap org-mouse-map))
  5303. (org-rear-nonsticky-at (match-end 0))
  5304. (when org-display-custom-times
  5305. (if (match-end 3)
  5306. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5307. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5308. t)))
  5309. (defvar org-target-link-regexp nil
  5310. "Regular expression matching radio targets in plain text.")
  5311. (make-variable-buffer-local 'org-target-link-regexp)
  5312. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5313. "Regular expression matching a link target.")
  5314. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5315. "Regular expression matching a radio target.")
  5316. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5317. "Regular expression matching any target.")
  5318. (defun org-activate-target-links (limit)
  5319. "Run through the buffer and add overlays to target matches."
  5320. (when org-target-link-regexp
  5321. (let ((case-fold-search t))
  5322. (if (re-search-forward org-target-link-regexp limit t)
  5323. (progn
  5324. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5325. (add-text-properties (match-beginning 0) (match-end 0)
  5326. (list 'mouse-face 'highlight
  5327. 'keymap org-mouse-map
  5328. 'help-echo "Radio target link"
  5329. 'org-linked-text t))
  5330. (org-rear-nonsticky-at (match-end 0))
  5331. t)))))
  5332. (defun org-update-radio-target-regexp ()
  5333. "Find all radio targets in this file and update the regular expression."
  5334. (interactive)
  5335. (when (memq 'radio org-activate-links)
  5336. (setq org-target-link-regexp
  5337. (org-make-target-link-regexp (org-all-targets 'radio)))
  5338. (org-restart-font-lock)))
  5339. (defun org-hide-wide-columns (limit)
  5340. (let (s e)
  5341. (setq s (text-property-any (point) (or limit (point-max))
  5342. 'org-cwidth t))
  5343. (when s
  5344. (setq e (next-single-property-change s 'org-cwidth))
  5345. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5346. (goto-char e)
  5347. t)))
  5348. (defvar org-latex-and-related-regexp nil
  5349. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5350. (defvar org-match-substring-regexp)
  5351. (defvar org-match-substring-with-braces-regexp)
  5352. (defun org-compute-latex-and-related-regexp ()
  5353. "Compute regular expression for LaTeX, entities and sub/superscript.
  5354. Result depends on variable `org-highlight-latex-and-related'."
  5355. (org-set-local
  5356. 'org-latex-and-related-regexp
  5357. (let* ((re-sub
  5358. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5359. ((eq org-use-sub-superscripts '{})
  5360. (list org-match-substring-with-braces-regexp))
  5361. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5362. (re-latex
  5363. (when (memq 'latex org-highlight-latex-and-related)
  5364. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5365. (delq nil
  5366. (mapcar (lambda (x)
  5367. (and (member (car x) matchers) (nth 1 x)))
  5368. org-latex-regexps)))))
  5369. (re-entities
  5370. (when (memq 'entities org-highlight-latex-and-related)
  5371. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5372. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5373. (defun org-do-latex-and-related (limit)
  5374. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5375. LIMIT bounds the search for syntax to highlight. Stop at first
  5376. highlighted object, if any. Return t if some highlighting was
  5377. done, nil otherwise."
  5378. (when (org-string-nw-p org-latex-and-related-regexp)
  5379. (catch 'found
  5380. (while (re-search-forward org-latex-and-related-regexp limit t)
  5381. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5382. 'face))
  5383. '(org-code org-verbatim underline))
  5384. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5385. '(?_ ?^))
  5386. 1
  5387. 0)))
  5388. (font-lock-prepend-text-property
  5389. (+ offset (match-beginning 0)) (match-end 0)
  5390. 'face 'org-latex-and-related)
  5391. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5392. '(font-lock-multiline t)))
  5393. (throw 'found t)))
  5394. nil)))
  5395. (defun org-restart-font-lock ()
  5396. "Restart `font-lock-mode', to force refontification."
  5397. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5398. (font-lock-mode -1)
  5399. (font-lock-mode 1)))
  5400. (defun org-all-targets (&optional radio)
  5401. "Return a list of all targets in this file.
  5402. When optional argument RADIO is non-nil, only find radio
  5403. targets."
  5404. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5405. (save-excursion
  5406. (goto-char (point-min))
  5407. (while (re-search-forward re nil t)
  5408. ;; Make sure point is really within the object.
  5409. (backward-char)
  5410. (let ((obj (org-element-context)))
  5411. (when (memq (org-element-type obj) '(radio-target target))
  5412. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5413. rtn)))
  5414. (defun org-make-target-link-regexp (targets)
  5415. "Make regular expression matching all strings in TARGETS.
  5416. The regular expression finds the targets also if there is a line break
  5417. between words."
  5418. (and targets
  5419. (concat
  5420. "\\<\\("
  5421. (mapconcat
  5422. (lambda (x)
  5423. (setq x (regexp-quote x))
  5424. (while (string-match " +" x)
  5425. (setq x (replace-match "\\s-+" t t x)))
  5426. x)
  5427. targets
  5428. "\\|")
  5429. "\\)\\>")))
  5430. (defun org-activate-tags (limit)
  5431. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5432. (progn
  5433. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5434. (add-text-properties (match-beginning 1) (match-end 1)
  5435. (list 'mouse-face 'highlight
  5436. 'keymap org-mouse-map))
  5437. (org-rear-nonsticky-at (match-end 1))
  5438. t)))
  5439. (defun org-outline-level ()
  5440. "Compute the outline level of the heading at point.
  5441. If this is called at a normal headline, the level is the number of stars.
  5442. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5443. (save-excursion
  5444. (if (not (condition-case nil
  5445. (org-back-to-heading t)
  5446. (error nil)))
  5447. 0
  5448. (looking-at org-outline-regexp)
  5449. (1- (- (match-end 0) (match-beginning 0))))))
  5450. (defvar org-font-lock-keywords nil)
  5451. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5452. "Regular expression matching a property line.")
  5453. (defvar org-font-lock-hook nil
  5454. "Functions to be called for special font lock stuff.")
  5455. (defvar org-font-lock-set-keywords-hook nil
  5456. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5457. This is called after `org-font-lock-extra-keywords' is defined, but before
  5458. it is installed to be used by font lock. This can be useful if something
  5459. needs to be inserted at a specific position in the font-lock sequence.")
  5460. (defun org-font-lock-hook (limit)
  5461. "Run `org-font-lock-hook' within LIMIT."
  5462. (run-hook-with-args 'org-font-lock-hook limit))
  5463. (defun org-set-font-lock-defaults ()
  5464. "Set font lock defaults for the current buffer."
  5465. (let* ((em org-fontify-emphasized-text)
  5466. (lk org-activate-links)
  5467. (org-font-lock-extra-keywords
  5468. (list
  5469. ;; Call the hook
  5470. '(org-font-lock-hook)
  5471. ;; Headlines
  5472. `(,(if org-fontify-whole-heading-line
  5473. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5474. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5475. (1 (org-get-level-face 1))
  5476. (2 (org-get-level-face 2))
  5477. (3 (org-get-level-face 3)))
  5478. ;; Table lines
  5479. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5480. (1 'org-table t))
  5481. ;; Table internals
  5482. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5483. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5484. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5485. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5486. ;; Drawers
  5487. (list org-drawer-regexp '(0 'org-special-keyword t))
  5488. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5489. ;; Properties
  5490. (list org-property-re
  5491. '(1 'org-special-keyword t)
  5492. '(3 'org-property-value t))
  5493. ;; Links
  5494. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5495. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5496. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5497. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5498. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5499. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5500. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5501. ;; Targets.
  5502. (list org-any-target-regexp '(0 'org-target t))
  5503. ;; Diary sexps.
  5504. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5505. ;; Macro
  5506. '("{{{.+}}}" (0 'org-macro t))
  5507. '(org-hide-wide-columns (0 nil append))
  5508. ;; TODO keyword
  5509. (list (format org-heading-keyword-regexp-format
  5510. org-todo-regexp)
  5511. '(2 (org-get-todo-face 2) t))
  5512. ;; DONE
  5513. (if org-fontify-done-headline
  5514. (list (format org-heading-keyword-regexp-format
  5515. (concat
  5516. "\\(?:"
  5517. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5518. "\\)"))
  5519. '(2 'org-headline-done t))
  5520. nil)
  5521. ;; Priorities
  5522. '(org-font-lock-add-priority-faces)
  5523. ;; Tags
  5524. '(org-font-lock-add-tag-faces)
  5525. ;; Tags groups
  5526. (if (and org-group-tags org-tag-groups-alist)
  5527. (list (concat org-outline-regexp-bol ".+\\(:"
  5528. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5529. ":\\).*$")
  5530. '(1 'org-tag-group prepend)))
  5531. ;; Special keywords
  5532. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5533. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5534. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5535. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5536. ;; Emphasis
  5537. (if em
  5538. (if (featurep 'xemacs)
  5539. '(org-do-emphasis-faces (0 nil append))
  5540. '(org-do-emphasis-faces)))
  5541. ;; Checkboxes
  5542. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5543. 1 'org-checkbox prepend)
  5544. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5545. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5546. (0 (org-get-checkbox-statistics-face) t)))
  5547. ;; Description list items
  5548. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5549. 1 'org-list-dt prepend)
  5550. ;; ARCHIVEd headings
  5551. (list (concat
  5552. org-outline-regexp-bol
  5553. "\\(.*:" org-archive-tag ":.*\\)")
  5554. '(1 'org-archived prepend))
  5555. ;; Specials
  5556. '(org-do-latex-and-related)
  5557. '(org-fontify-entities)
  5558. '(org-raise-scripts)
  5559. ;; Code
  5560. '(org-activate-code (1 'org-code t))
  5561. ;; COMMENT
  5562. (list (format org-heading-keyword-regexp-format
  5563. (concat "\\("
  5564. org-comment-string "\\|" org-quote-string
  5565. "\\)"))
  5566. '(2 'org-special-keyword t))
  5567. ;; Blocks and meta lines
  5568. '(org-fontify-meta-lines-and-blocks))))
  5569. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5570. (run-hooks 'org-font-lock-set-keywords-hook)
  5571. ;; Now set the full font-lock-keywords
  5572. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5573. (org-set-local 'font-lock-defaults
  5574. '(org-font-lock-keywords t nil nil backward-paragraph))
  5575. (kill-local-variable 'font-lock-keywords) nil))
  5576. (defun org-toggle-pretty-entities ()
  5577. "Toggle the composition display of entities as UTF8 characters."
  5578. (interactive)
  5579. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5580. (org-restart-font-lock)
  5581. (if org-pretty-entities
  5582. (message "Entities are now displayed as UTF8 characters")
  5583. (save-restriction
  5584. (widen)
  5585. (org-decompose-region (point-min) (point-max))
  5586. (message "Entities are now displayed as plain text"))))
  5587. (defvar org-custom-properties-overlays nil
  5588. "List of overlays used for custom properties.")
  5589. (make-variable-buffer-local 'org-custom-properties-overlays)
  5590. (defun org-toggle-custom-properties-visibility ()
  5591. "Display or hide properties in `org-custom-properties'."
  5592. (interactive)
  5593. (if org-custom-properties-overlays
  5594. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5595. (setq org-custom-properties-overlays nil))
  5596. (unless (not org-custom-properties)
  5597. (save-excursion
  5598. (save-restriction
  5599. (widen)
  5600. (goto-char (point-min))
  5601. (while (re-search-forward org-property-re nil t)
  5602. (mapc (lambda(p)
  5603. (when (equal p (substring (match-string 1) 1 -1))
  5604. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5605. (overlay-put o 'invisible t)
  5606. (overlay-put o 'org-custom-property t)
  5607. (push o org-custom-properties-overlays))))
  5608. org-custom-properties)))))))
  5609. (defun org-fontify-entities (limit)
  5610. "Find an entity to fontify."
  5611. (let (ee)
  5612. (when org-pretty-entities
  5613. (catch 'match
  5614. (while (re-search-forward
  5615. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5616. limit t)
  5617. (if (and (not (org-in-indented-comment-line))
  5618. (setq ee (org-entity-get (match-string 1)))
  5619. (= (length (nth 6 ee)) 1))
  5620. (let*
  5621. ((end (if (equal (match-string 2) "{}")
  5622. (match-end 2)
  5623. (match-end 1))))
  5624. (add-text-properties
  5625. (match-beginning 0) end
  5626. (list 'font-lock-fontified t))
  5627. (compose-region (match-beginning 0) end
  5628. (nth 6 ee) nil)
  5629. (backward-char 1)
  5630. (throw 'match t))))
  5631. nil))))
  5632. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5633. "Fontify string S like in Org-mode."
  5634. (with-temp-buffer
  5635. (insert s)
  5636. (let ((org-odd-levels-only odd-levels))
  5637. (org-mode)
  5638. (font-lock-fontify-buffer)
  5639. (buffer-string))))
  5640. (defvar org-m nil)
  5641. (defvar org-l nil)
  5642. (defvar org-f nil)
  5643. (defun org-get-level-face (n)
  5644. "Get the right face for match N in font-lock matching of headlines."
  5645. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5646. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5647. (if org-cycle-level-faces
  5648. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5649. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5650. (cond
  5651. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5652. ((eq n 2) org-f)
  5653. (t (if org-level-color-stars-only nil org-f))))
  5654. (defun org-get-todo-face (kwd)
  5655. "Get the right face for a TODO keyword KWD.
  5656. If KWD is a number, get the corresponding match group."
  5657. (if (numberp kwd) (setq kwd (match-string kwd)))
  5658. (or (org-face-from-face-or-color
  5659. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5660. (and (member kwd org-done-keywords) 'org-done)
  5661. 'org-todo))
  5662. (defun org-face-from-face-or-color (context inherit face-or-color)
  5663. "Create a face list that inherits INHERIT, but sets the foreground color.
  5664. When FACE-OR-COLOR is not a string, just return it."
  5665. (if (stringp face-or-color)
  5666. (list :inherit inherit
  5667. (cdr (assoc context org-faces-easy-properties))
  5668. face-or-color)
  5669. face-or-color))
  5670. (defun org-font-lock-add-tag-faces (limit)
  5671. "Add the special tag faces."
  5672. (when (and org-tag-faces org-tags-special-faces-re)
  5673. (while (re-search-forward org-tags-special-faces-re limit t)
  5674. (add-text-properties (match-beginning 1) (match-end 1)
  5675. (list 'face (org-get-tag-face 1)
  5676. 'font-lock-fontified t))
  5677. (backward-char 1))))
  5678. (defun org-font-lock-add-priority-faces (limit)
  5679. "Add the special priority faces."
  5680. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5681. (when (save-match-data (org-at-heading-p))
  5682. (add-text-properties
  5683. (match-beginning 0) (match-end 0)
  5684. (list 'face (or (org-face-from-face-or-color
  5685. 'priority 'org-priority
  5686. (cdr (assoc (char-after (match-beginning 1))
  5687. org-priority-faces)))
  5688. 'org-priority)
  5689. 'font-lock-fontified t)))))
  5690. (defun org-get-tag-face (kwd)
  5691. "Get the right face for a TODO keyword KWD.
  5692. If KWD is a number, get the corresponding match group."
  5693. (if (numberp kwd) (setq kwd (match-string kwd)))
  5694. (or (org-face-from-face-or-color
  5695. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5696. 'org-tag))
  5697. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5698. "Remove fontification and activation overlays from links."
  5699. (font-lock-default-unfontify-region beg end)
  5700. (let* ((buffer-undo-list t)
  5701. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5702. (inhibit-modification-hooks t)
  5703. deactivate-mark buffer-file-name buffer-file-truename)
  5704. (org-decompose-region beg end)
  5705. (remove-text-properties beg end
  5706. '(mouse-face t keymap t org-linked-text t
  5707. invisible t intangible t
  5708. org-no-flyspell t org-emphasis t))
  5709. (org-remove-font-lock-display-properties beg end)))
  5710. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5711. ((raise 0.3) (height 0.7))
  5712. ((raise -0.5))
  5713. ((raise 0.5)))
  5714. "Display properties for showing superscripts and subscripts.")
  5715. (defun org-remove-font-lock-display-properties (beg end)
  5716. "Remove specific display properties that have been added by font lock.
  5717. The will remove the raise properties that are used to show superscripts
  5718. and subscripts."
  5719. (let (next prop)
  5720. (while (< beg end)
  5721. (setq next (next-single-property-change beg 'display nil end)
  5722. prop (get-text-property beg 'display))
  5723. (if (member prop org-script-display)
  5724. (put-text-property beg next 'display nil))
  5725. (setq beg next))))
  5726. (defun org-raise-scripts (limit)
  5727. "Add raise properties to sub/superscripts."
  5728. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5729. (if (re-search-forward
  5730. (if (eq org-use-sub-superscripts t)
  5731. org-match-substring-regexp
  5732. org-match-substring-with-braces-regexp)
  5733. limit t)
  5734. (let* ((pos (point)) table-p comment-p
  5735. (mpos (match-beginning 3))
  5736. (emph-p (get-text-property mpos 'org-emphasis))
  5737. (link-p (get-text-property mpos 'mouse-face))
  5738. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5739. (goto-char (point-at-bol))
  5740. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5741. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5742. (goto-char pos)
  5743. ;; Handle a_b^c
  5744. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5745. (if (or comment-p emph-p link-p keyw-p)
  5746. t
  5747. (put-text-property (match-beginning 3) (match-end 0)
  5748. 'display
  5749. (if (equal (char-after (match-beginning 2)) ?^)
  5750. (nth (if table-p 3 1) org-script-display)
  5751. (nth (if table-p 2 0) org-script-display)))
  5752. (add-text-properties (match-beginning 2) (match-end 2)
  5753. (list 'invisible t
  5754. 'org-dwidth t 'org-dwidth-n 1))
  5755. (if (and (eq (char-after (match-beginning 3)) ?{)
  5756. (eq (char-before (match-end 3)) ?}))
  5757. (progn
  5758. (add-text-properties
  5759. (match-beginning 3) (1+ (match-beginning 3))
  5760. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5761. (add-text-properties
  5762. (1- (match-end 3)) (match-end 3)
  5763. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5764. t)))))
  5765. ;;;; Visibility cycling, including org-goto and indirect buffer
  5766. ;;; Cycling
  5767. (defvar org-cycle-global-status nil)
  5768. (make-variable-buffer-local 'org-cycle-global-status)
  5769. (put 'org-cycle-global-status 'org-state t)
  5770. (defvar org-cycle-subtree-status nil)
  5771. (make-variable-buffer-local 'org-cycle-subtree-status)
  5772. (put 'org-cycle-subtree-status 'org-state t)
  5773. (defvar org-inlinetask-min-level)
  5774. ;;;###autoload
  5775. (defun org-cycle (&optional arg)
  5776. "TAB-action and visibility cycling for Org-mode.
  5777. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5778. is outline visibility cycling, but it also invokes other actions
  5779. in special contexts.
  5780. - When this function is called with a prefix argument, rotate the entire
  5781. buffer through 3 states (global cycling)
  5782. 1. OVERVIEW: Show only top-level headlines.
  5783. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5784. 3. SHOW ALL: Show everything.
  5785. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5786. determined by the variable `org-startup-folded', and by any VISIBILITY
  5787. properties in the buffer.
  5788. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5789. including any drawers.
  5790. - When inside a table, re-align the table and move to the next field.
  5791. - When point is at the beginning of a headline, rotate the subtree started
  5792. by this line through 3 different states (local cycling)
  5793. 1. FOLDED: Only the main headline is shown.
  5794. 2. CHILDREN: The main headline and the direct children are shown.
  5795. From this state, you can move to one of the children
  5796. and zoom in further.
  5797. 3. SUBTREE: Show the entire subtree, including body text.
  5798. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5799. - When point is at the beginning of an empty headline and the variable
  5800. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5801. of the headline by demoting and promoting it to likely levels. This
  5802. speeds up creation document structure by pressing TAB once or several
  5803. times right after creating a new headline.
  5804. - When there is a numeric prefix, go up to a heading with level ARG, do
  5805. a `show-subtree' and return to the previous cursor position. If ARG
  5806. is negative, go up that many levels.
  5807. - When point is not at the beginning of a headline, execute the global
  5808. binding for TAB, which is re-indenting the line. See the option
  5809. `org-cycle-emulate-tab' for details.
  5810. - Special case: if point is at the beginning of the buffer and there is
  5811. no headline in line 1, this function will act as if called with prefix arg
  5812. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5813. But only if also the variable `org-cycle-global-at-bob' is t."
  5814. (interactive "P")
  5815. (org-load-modules-maybe)
  5816. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5817. (and org-cycle-level-after-item/entry-creation
  5818. (or (org-cycle-level)
  5819. (org-cycle-item-indentation))))
  5820. (let* (message-log-max ; Don't populate the *Messages* buffer
  5821. (limit-level
  5822. (or org-cycle-max-level
  5823. (and (boundp 'org-inlinetask-min-level)
  5824. org-inlinetask-min-level
  5825. (1- org-inlinetask-min-level))))
  5826. (nstars (and limit-level
  5827. (if org-odd-levels-only
  5828. (and limit-level (1- (* limit-level 2)))
  5829. limit-level)))
  5830. (org-outline-regexp
  5831. (if (not (derived-mode-p 'org-mode))
  5832. outline-regexp
  5833. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5834. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5835. (not (looking-at org-outline-regexp))))
  5836. (org-cycle-hook
  5837. (if bob-special
  5838. (delq 'org-optimize-window-after-visibility-change
  5839. (copy-sequence org-cycle-hook))
  5840. org-cycle-hook))
  5841. (pos (point)))
  5842. (if (or bob-special (equal arg '(4)))
  5843. ;; special case: use global cycling
  5844. (setq arg t))
  5845. (cond
  5846. ((equal arg '(16))
  5847. (setq last-command 'dummy)
  5848. (org-set-startup-visibility)
  5849. (message "Startup visibility, plus VISIBILITY properties"))
  5850. ((equal arg '(64))
  5851. (show-all)
  5852. (message "Entire buffer visible, including drawers"))
  5853. ;; Table: enter it or move to the next field.
  5854. ((org-at-table-p 'any)
  5855. (if (org-at-table.el-p)
  5856. (message "Use C-c ' to edit table.el tables")
  5857. (if arg (org-table-edit-field t)
  5858. (org-table-justify-field-maybe)
  5859. (call-interactively 'org-table-next-field))))
  5860. ((run-hook-with-args-until-success
  5861. 'org-tab-after-check-for-table-hook))
  5862. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5863. ((eq arg t) (org-cycle-internal-global))
  5864. ;; Drawers: delegate to `org-flag-drawer'.
  5865. ((and org-drawers org-drawer-regexp
  5866. (save-excursion
  5867. (beginning-of-line 1)
  5868. (looking-at org-drawer-regexp)))
  5869. (org-flag-drawer ; toggle block visibility
  5870. (not (get-char-property (match-end 0) 'invisible))))
  5871. ;; Show-subtree, ARG levels up from here.
  5872. ((integerp arg)
  5873. (save-excursion
  5874. (org-back-to-heading)
  5875. (outline-up-heading (if (< arg 0) (- arg)
  5876. (- (funcall outline-level) arg)))
  5877. (org-show-subtree)))
  5878. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5879. ((and (featurep 'org-inlinetask)
  5880. (org-inlinetask-at-task-p)
  5881. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5882. (org-inlinetask-toggle-visibility))
  5883. ((org-try-cdlatex-tab))
  5884. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5885. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5886. (save-excursion (beginning-of-line 1)
  5887. (looking-at org-outline-regexp)))
  5888. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5889. (org-cycle-internal-local))
  5890. ;; From there: TAB emulation and template completion.
  5891. (buffer-read-only (org-back-to-heading))
  5892. ((run-hook-with-args-until-success
  5893. 'org-tab-after-check-for-cycling-hook))
  5894. ((org-try-structure-completion))
  5895. ((run-hook-with-args-until-success
  5896. 'org-tab-before-tab-emulation-hook))
  5897. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5898. (or (not (bolp))
  5899. (not (looking-at org-outline-regexp))))
  5900. (call-interactively (global-key-binding "\t")))
  5901. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5902. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5903. (or (and (eq org-cycle-emulate-tab 'white)
  5904. (= (match-end 0) (point-at-eol)))
  5905. (and (eq org-cycle-emulate-tab 'whitestart)
  5906. (>= (match-end 0) pos))))
  5907. t
  5908. (eq org-cycle-emulate-tab t))
  5909. (call-interactively (global-key-binding "\t")))
  5910. (t (save-excursion
  5911. (org-back-to-heading)
  5912. (org-cycle)))))))
  5913. (defun org-cycle-internal-global ()
  5914. "Do the global cycling action."
  5915. ;; Hack to avoid display of messages for .org attachments in Gnus
  5916. (let (message-log-max ; Don't populate the *Messages* buffer
  5917. (ga (string-match "\\*fontification" (buffer-name))))
  5918. (cond
  5919. ((and (eq last-command this-command)
  5920. (eq org-cycle-global-status 'overview))
  5921. ;; We just created the overview - now do table of contents
  5922. ;; This can be slow in very large buffers, so indicate action
  5923. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5924. (unless ga (message "CONTENTS..."))
  5925. (org-content)
  5926. (unless ga (message "CONTENTS...done"))
  5927. (setq org-cycle-global-status 'contents)
  5928. (run-hook-with-args 'org-cycle-hook 'contents))
  5929. ((and (eq last-command this-command)
  5930. (eq org-cycle-global-status 'contents))
  5931. ;; We just showed the table of contents - now show everything
  5932. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5933. (show-all)
  5934. (unless ga (message "SHOW ALL"))
  5935. (setq org-cycle-global-status 'all)
  5936. (run-hook-with-args 'org-cycle-hook 'all))
  5937. (t
  5938. ;; Default action: go to overview
  5939. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5940. (org-overview)
  5941. (unless ga (message "OVERVIEW"))
  5942. (setq org-cycle-global-status 'overview)
  5943. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5944. (defun org-cycle-internal-local ()
  5945. "Do the local cycling action."
  5946. (let (message-log-max ; Don't populate the *Messages* buffer
  5947. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5948. ;; First, determine end of headline (EOH), end of subtree or item
  5949. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5950. (save-excursion
  5951. (if (org-at-item-p)
  5952. (progn
  5953. (beginning-of-line)
  5954. (setq struct (org-list-struct))
  5955. (setq eoh (point-at-eol))
  5956. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5957. (setq has-children (org-list-has-child-p (point) struct)))
  5958. (org-back-to-heading)
  5959. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5960. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5961. (setq has-children
  5962. (or (save-excursion
  5963. (let ((level (funcall outline-level)))
  5964. (outline-next-heading)
  5965. (and (org-at-heading-p t)
  5966. (> (funcall outline-level) level))))
  5967. (save-excursion
  5968. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5969. ;; Determine end invisible part of buffer (EOL)
  5970. (beginning-of-line 2)
  5971. ;; XEmacs doesn't have `next-single-char-property-change'
  5972. (if (featurep 'xemacs)
  5973. (while (and (not (eobp)) ;; this is like `next-line'
  5974. (get-char-property (1- (point)) 'invisible))
  5975. (beginning-of-line 2))
  5976. (while (and (not (eobp)) ;; this is like `next-line'
  5977. (get-char-property (1- (point)) 'invisible))
  5978. (goto-char (next-single-char-property-change (point) 'invisible))
  5979. (and (eolp) (beginning-of-line 2))))
  5980. (setq eol (point)))
  5981. ;; Find out what to do next and set `this-command'
  5982. (cond
  5983. ((= eos eoh)
  5984. ;; Nothing is hidden behind this heading
  5985. (unless (org-before-first-heading-p)
  5986. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5987. (message "EMPTY ENTRY")
  5988. (setq org-cycle-subtree-status nil)
  5989. (save-excursion
  5990. (goto-char eos)
  5991. (outline-next-heading)
  5992. (if (outline-invisible-p) (org-flag-heading nil))))
  5993. ((and (or (>= eol eos)
  5994. (not (string-match "\\S-" (buffer-substring eol eos))))
  5995. (or has-children
  5996. (not (setq children-skipped
  5997. org-cycle-skip-children-state-if-no-children))))
  5998. ;; Entire subtree is hidden in one line: children view
  5999. (unless (org-before-first-heading-p)
  6000. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6001. (if (org-at-item-p)
  6002. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6003. (org-show-entry)
  6004. (org-with-limited-levels (show-children))
  6005. ;; FIXME: This slows down the func way too much.
  6006. ;; How keep drawers hidden in subtree anyway?
  6007. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6008. ;; (org-cycle-hide-drawers 'subtree))
  6009. ;; Fold every list in subtree to top-level items.
  6010. (when (eq org-cycle-include-plain-lists 'integrate)
  6011. (save-excursion
  6012. (org-back-to-heading)
  6013. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6014. (beginning-of-line 1)
  6015. (let* ((struct (org-list-struct))
  6016. (prevs (org-list-prevs-alist struct))
  6017. (end (org-list-get-bottom-point struct)))
  6018. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6019. (org-list-get-all-items (point) struct prevs))
  6020. (goto-char (if (< end eos) end eos)))))))
  6021. (message "CHILDREN")
  6022. (save-excursion
  6023. (goto-char eos)
  6024. (outline-next-heading)
  6025. (if (outline-invisible-p) (org-flag-heading nil)))
  6026. (setq org-cycle-subtree-status 'children)
  6027. (unless (org-before-first-heading-p)
  6028. (run-hook-with-args 'org-cycle-hook 'children)))
  6029. ((or children-skipped
  6030. (and (eq last-command this-command)
  6031. (eq org-cycle-subtree-status 'children)))
  6032. ;; We just showed the children, or no children are there,
  6033. ;; now show everything.
  6034. (unless (org-before-first-heading-p)
  6035. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6036. (outline-flag-region eoh eos nil)
  6037. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6038. (setq org-cycle-subtree-status 'subtree)
  6039. (unless (org-before-first-heading-p)
  6040. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6041. (t
  6042. ;; Default action: hide the subtree.
  6043. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6044. (outline-flag-region eoh eos t)
  6045. (message "FOLDED")
  6046. (setq org-cycle-subtree-status 'folded)
  6047. (unless (org-before-first-heading-p)
  6048. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6049. ;;;###autoload
  6050. (defun org-global-cycle (&optional arg)
  6051. "Cycle the global visibility. For details see `org-cycle'.
  6052. With \\[universal-argument] prefix arg, switch to startup visibility.
  6053. With a numeric prefix, show all headlines up to that level."
  6054. (interactive "P")
  6055. (let ((org-cycle-include-plain-lists
  6056. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6057. (cond
  6058. ((integerp arg)
  6059. (show-all)
  6060. (hide-sublevels arg)
  6061. (setq org-cycle-global-status 'contents))
  6062. ((equal arg '(4))
  6063. (org-set-startup-visibility)
  6064. (message "Startup visibility, plus VISIBILITY properties."))
  6065. (t
  6066. (org-cycle '(4))))))
  6067. (defun org-set-startup-visibility ()
  6068. "Set the visibility required by startup options and properties."
  6069. (cond
  6070. ((eq org-startup-folded t)
  6071. (org-cycle '(4)))
  6072. ((eq org-startup-folded 'content)
  6073. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6074. (org-cycle '(4)) (org-cycle '(4)))))
  6075. (unless (eq org-startup-folded 'showeverything)
  6076. (if org-hide-block-startup (org-hide-block-all))
  6077. (org-set-visibility-according-to-property 'no-cleanup)
  6078. (org-cycle-hide-archived-subtrees 'all)
  6079. (org-cycle-hide-drawers 'all)
  6080. (org-cycle-show-empty-lines t)))
  6081. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6082. "Switch subtree visibilities according to :VISIBILITY: property."
  6083. (interactive)
  6084. (let (org-show-entry-below state)
  6085. (save-excursion
  6086. (goto-char (point-min))
  6087. (while (re-search-forward
  6088. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6089. nil t)
  6090. (setq state (match-string 1))
  6091. (save-excursion
  6092. (org-back-to-heading t)
  6093. (hide-subtree)
  6094. (org-reveal)
  6095. (cond
  6096. ((equal state '("fold" "folded"))
  6097. (hide-subtree))
  6098. ((equal state "children")
  6099. (org-show-hidden-entry)
  6100. (show-children))
  6101. ((equal state "content")
  6102. (save-excursion
  6103. (save-restriction
  6104. (org-narrow-to-subtree)
  6105. (org-content))))
  6106. ((member state '("all" "showall"))
  6107. (show-subtree)))))
  6108. (unless no-cleanup
  6109. (org-cycle-hide-archived-subtrees 'all)
  6110. (org-cycle-hide-drawers 'all)
  6111. (org-cycle-show-empty-lines 'all)))))
  6112. ;; This function uses outline-regexp instead of the more fundamental
  6113. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6114. ;; buffers, where outline-regexp is needed.
  6115. (defun org-overview ()
  6116. "Switch to overview mode, showing only top-level headlines.
  6117. Really, this shows all headlines with level equal or greater than the level
  6118. of the first headline in the buffer. This is important, because if the
  6119. first headline is not level one, then (hide-sublevels 1) gives confusing
  6120. results."
  6121. (interactive)
  6122. (let ((l (org-current-line))
  6123. (level (save-excursion
  6124. (goto-char (point-min))
  6125. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6126. (progn
  6127. (goto-char (match-beginning 0))
  6128. (funcall outline-level))))))
  6129. (and level (hide-sublevels level))
  6130. (recenter '(4))
  6131. (org-goto-line l)))
  6132. (defun org-content (&optional arg)
  6133. "Show all headlines in the buffer, like a table of contents.
  6134. With numerical argument N, show content up to level N."
  6135. (interactive "P")
  6136. (save-excursion
  6137. ;; Visit all headings and show their offspring
  6138. (and (integerp arg) (org-overview))
  6139. (goto-char (point-max))
  6140. (catch 'exit
  6141. (while (and (progn (condition-case nil
  6142. (outline-previous-visible-heading 1)
  6143. (error (goto-char (point-min))))
  6144. t)
  6145. (looking-at org-outline-regexp))
  6146. (if (integerp arg)
  6147. (show-children (1- arg))
  6148. (show-branches))
  6149. (if (bobp) (throw 'exit nil))))))
  6150. (defun org-optimize-window-after-visibility-change (state)
  6151. "Adjust the window after a change in outline visibility.
  6152. This function is the default value of the hook `org-cycle-hook'."
  6153. (when (get-buffer-window (current-buffer))
  6154. (cond
  6155. ((eq state 'content) nil)
  6156. ((eq state 'all) nil)
  6157. ((eq state 'folded) nil)
  6158. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6159. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6160. (defun org-remove-empty-overlays-at (pos)
  6161. "Remove outline overlays that do not contain non-white stuff."
  6162. (mapc
  6163. (lambda (o)
  6164. (and (eq 'outline (overlay-get o 'invisible))
  6165. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6166. (overlay-end o))))
  6167. (delete-overlay o)))
  6168. (overlays-at pos)))
  6169. (defun org-clean-visibility-after-subtree-move ()
  6170. "Fix visibility issues after moving a subtree."
  6171. ;; First, find a reasonable region to look at:
  6172. ;; Start two siblings above, end three below
  6173. (let* ((beg (save-excursion
  6174. (and (org-get-last-sibling)
  6175. (org-get-last-sibling))
  6176. (point)))
  6177. (end (save-excursion
  6178. (and (org-get-next-sibling)
  6179. (org-get-next-sibling)
  6180. (org-get-next-sibling))
  6181. (if (org-at-heading-p)
  6182. (point-at-eol)
  6183. (point))))
  6184. (level (looking-at "\\*+"))
  6185. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6186. (save-excursion
  6187. (save-restriction
  6188. (narrow-to-region beg end)
  6189. (when re
  6190. ;; Properly fold already folded siblings
  6191. (goto-char (point-min))
  6192. (while (re-search-forward re nil t)
  6193. (if (and (not (outline-invisible-p))
  6194. (save-excursion
  6195. (goto-char (point-at-eol)) (outline-invisible-p)))
  6196. (hide-entry))))
  6197. (org-cycle-show-empty-lines 'overview)
  6198. (org-cycle-hide-drawers 'overview)))))
  6199. (defun org-cycle-show-empty-lines (state)
  6200. "Show empty lines above all visible headlines.
  6201. The region to be covered depends on STATE when called through
  6202. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6203. entire buffer covered. Note that an empty line is only shown if there
  6204. are at least `org-cycle-separator-lines' empty lines before the headline."
  6205. (when (not (= org-cycle-separator-lines 0))
  6206. (save-excursion
  6207. (let* ((n (abs org-cycle-separator-lines))
  6208. (re (cond
  6209. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6210. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6211. (t (let ((ns (number-to-string (- n 2))))
  6212. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6213. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6214. beg end b e)
  6215. (cond
  6216. ((memq state '(overview contents t))
  6217. (setq beg (point-min) end (point-max)))
  6218. ((memq state '(children folded))
  6219. (setq beg (point) end (progn (org-end-of-subtree t t)
  6220. (beginning-of-line 2)
  6221. (point)))))
  6222. (when beg
  6223. (goto-char beg)
  6224. (while (re-search-forward re end t)
  6225. (unless (get-char-property (match-end 1) 'invisible)
  6226. (setq e (match-end 1))
  6227. (if (< org-cycle-separator-lines 0)
  6228. (setq b (save-excursion
  6229. (goto-char (match-beginning 0))
  6230. (org-back-over-empty-lines)
  6231. (if (save-excursion
  6232. (goto-char (max (point-min) (1- (point))))
  6233. (org-at-heading-p))
  6234. (1- (point))
  6235. (point))))
  6236. (setq b (match-beginning 1)))
  6237. (outline-flag-region b e nil)))))))
  6238. ;; Never hide empty lines at the end of the file.
  6239. (save-excursion
  6240. (goto-char (point-max))
  6241. (outline-previous-heading)
  6242. (outline-end-of-heading)
  6243. (if (and (looking-at "[ \t\n]+")
  6244. (= (match-end 0) (point-max)))
  6245. (outline-flag-region (point) (match-end 0) nil))))
  6246. (defun org-show-empty-lines-in-parent ()
  6247. "Move to the parent and re-show empty lines before visible headlines."
  6248. (save-excursion
  6249. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6250. (org-cycle-show-empty-lines context))))
  6251. (defun org-files-list ()
  6252. "Return `org-agenda-files' list, plus all open org-mode files.
  6253. This is useful for operations that need to scan all of a user's
  6254. open and agenda-wise Org files."
  6255. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6256. (dolist (buf (buffer-list))
  6257. (with-current-buffer buf
  6258. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6259. (let ((file (expand-file-name (buffer-file-name))))
  6260. (unless (member file files)
  6261. (push file files))))))
  6262. files))
  6263. (defsubst org-entry-beginning-position ()
  6264. "Return the beginning position of the current entry."
  6265. (save-excursion (outline-back-to-heading t) (point)))
  6266. (defsubst org-entry-end-position ()
  6267. "Return the end position of the current entry."
  6268. (save-excursion (outline-next-heading) (point)))
  6269. (defun org-cycle-hide-drawers (state)
  6270. "Re-hide all drawers after a visibility state change."
  6271. (when (and (derived-mode-p 'org-mode)
  6272. (not (memq state '(overview folded contents))))
  6273. (save-excursion
  6274. (let* ((globalp (memq state '(contents all)))
  6275. (beg (if globalp (point-min) (point)))
  6276. (end (if globalp (point-max)
  6277. (if (eq state 'children)
  6278. (save-excursion (outline-next-heading) (point))
  6279. (org-end-of-subtree t)))))
  6280. (goto-char beg)
  6281. (while (re-search-forward org-drawer-regexp end t)
  6282. (org-flag-drawer t))))))
  6283. (defun org-cycle-hide-inline-tasks (state)
  6284. "Re-hide inline task when switching to 'contents visibility state."
  6285. (when (and (eq state 'contents)
  6286. (boundp 'org-inlinetask-min-level)
  6287. org-inlinetask-min-level)
  6288. (hide-sublevels (1- org-inlinetask-min-level))))
  6289. (defun org-flag-drawer (flag)
  6290. "When FLAG is non-nil, hide the drawer we are within.
  6291. Otherwise make it visible."
  6292. (save-excursion
  6293. (beginning-of-line 1)
  6294. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6295. (let ((b (match-end 0)))
  6296. (if (re-search-forward
  6297. "^[ \t]*:END:"
  6298. (save-excursion (outline-next-heading) (point)) t)
  6299. (outline-flag-region b (point-at-eol) flag)
  6300. (user-error ":END: line missing at position %s" b))))))
  6301. (defun org-subtree-end-visible-p ()
  6302. "Is the end of the current subtree visible?"
  6303. (pos-visible-in-window-p
  6304. (save-excursion (org-end-of-subtree t) (point))))
  6305. (defun org-first-headline-recenter (&optional N)
  6306. "Move cursor to the first headline and recenter the headline.
  6307. Optional argument N means put the headline into the Nth line of the window."
  6308. (goto-char (point-min))
  6309. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6310. (beginning-of-line)
  6311. (recenter (prefix-numeric-value N))))
  6312. ;;; Saving and restoring visibility
  6313. (defun org-outline-overlay-data (&optional use-markers)
  6314. "Return a list of the locations of all outline overlays.
  6315. These are overlays with the `invisible' property value `outline'.
  6316. The return value is a list of cons cells, with start and stop
  6317. positions for each overlay.
  6318. If USE-MARKERS is set, return the positions as markers."
  6319. (let (beg end)
  6320. (save-excursion
  6321. (save-restriction
  6322. (widen)
  6323. (delq nil
  6324. (mapcar (lambda (o)
  6325. (when (eq (overlay-get o 'invisible) 'outline)
  6326. (setq beg (overlay-start o)
  6327. end (overlay-end o))
  6328. (and beg end (> end beg)
  6329. (if use-markers
  6330. (cons (move-marker (make-marker) beg)
  6331. (move-marker (make-marker) end))
  6332. (cons beg end)))))
  6333. (overlays-in (point-min) (point-max))))))))
  6334. (defun org-set-outline-overlay-data (data)
  6335. "Create visibility overlays for all positions in DATA.
  6336. DATA should have been made by `org-outline-overlay-data'."
  6337. (let (o)
  6338. (save-excursion
  6339. (save-restriction
  6340. (widen)
  6341. (show-all)
  6342. (mapc (lambda (c)
  6343. (outline-flag-region (car c) (cdr c) t))
  6344. data)))))
  6345. ;;; Folding of blocks
  6346. (defvar org-hide-block-overlays nil
  6347. "Overlays hiding blocks.")
  6348. (make-variable-buffer-local 'org-hide-block-overlays)
  6349. (defun org-block-map (function &optional start end)
  6350. "Call FUNCTION at the head of all source blocks in the current buffer.
  6351. Optional arguments START and END can be used to limit the range."
  6352. (let ((start (or start (point-min)))
  6353. (end (or end (point-max))))
  6354. (save-excursion
  6355. (goto-char start)
  6356. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6357. (save-excursion
  6358. (save-match-data
  6359. (goto-char (match-beginning 0))
  6360. (funcall function)))))))
  6361. (defun org-hide-block-toggle-all ()
  6362. "Toggle the visibility of all blocks in the current buffer."
  6363. (org-block-map #'org-hide-block-toggle))
  6364. (defun org-hide-block-all ()
  6365. "Fold all blocks in the current buffer."
  6366. (interactive)
  6367. (org-show-block-all)
  6368. (org-block-map #'org-hide-block-toggle-maybe))
  6369. (defun org-show-block-all ()
  6370. "Unfold all blocks in the current buffer."
  6371. (interactive)
  6372. (mapc 'delete-overlay org-hide-block-overlays)
  6373. (setq org-hide-block-overlays nil))
  6374. (defun org-hide-block-toggle-maybe ()
  6375. "Toggle visibility of block at point."
  6376. (interactive)
  6377. (let ((case-fold-search t))
  6378. (if (save-excursion
  6379. (beginning-of-line 1)
  6380. (looking-at org-block-regexp))
  6381. (progn (org-hide-block-toggle)
  6382. t) ;; to signal that we took action
  6383. nil))) ;; to signal that we did not
  6384. (defun org-hide-block-toggle (&optional force)
  6385. "Toggle the visibility of the current block."
  6386. (interactive)
  6387. (save-excursion
  6388. (beginning-of-line)
  6389. (if (re-search-forward org-block-regexp nil t)
  6390. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6391. (end (match-end 0)) ;; end of entire body
  6392. ov)
  6393. (if (memq t (mapcar (lambda (overlay)
  6394. (eq (overlay-get overlay 'invisible)
  6395. 'org-hide-block))
  6396. (overlays-at start)))
  6397. (if (or (not force) (eq force 'off))
  6398. (mapc (lambda (ov)
  6399. (when (member ov org-hide-block-overlays)
  6400. (setq org-hide-block-overlays
  6401. (delq ov org-hide-block-overlays)))
  6402. (when (eq (overlay-get ov 'invisible)
  6403. 'org-hide-block)
  6404. (delete-overlay ov)))
  6405. (overlays-at start)))
  6406. (setq ov (make-overlay start end))
  6407. (overlay-put ov 'invisible 'org-hide-block)
  6408. ;; make the block accessible to isearch
  6409. (overlay-put
  6410. ov 'isearch-open-invisible
  6411. (lambda (ov)
  6412. (when (member ov org-hide-block-overlays)
  6413. (setq org-hide-block-overlays
  6414. (delq ov org-hide-block-overlays)))
  6415. (when (eq (overlay-get ov 'invisible)
  6416. 'org-hide-block)
  6417. (delete-overlay ov))))
  6418. (push ov org-hide-block-overlays)))
  6419. (user-error "Not looking at a source block"))))
  6420. ;; org-tab-after-check-for-cycling-hook
  6421. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6422. ;; Remove overlays when changing major mode
  6423. (add-hook 'org-mode-hook
  6424. (lambda () (org-add-hook 'change-major-mode-hook
  6425. 'org-show-block-all 'append 'local)))
  6426. ;;; Org-goto
  6427. (defvar org-goto-window-configuration nil)
  6428. (defvar org-goto-marker nil)
  6429. (defvar org-goto-map)
  6430. (defun org-goto-map ()
  6431. "Set the keymap `org-goto'."
  6432. (setq org-goto-map
  6433. (let ((map (make-sparse-keymap)))
  6434. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6435. mouse-drag-region universal-argument org-occur))
  6436. cmd)
  6437. (while (setq cmd (pop cmds))
  6438. (substitute-key-definition cmd cmd map global-map)))
  6439. (suppress-keymap map)
  6440. (org-defkey map "\C-m" 'org-goto-ret)
  6441. (org-defkey map [(return)] 'org-goto-ret)
  6442. (org-defkey map [(left)] 'org-goto-left)
  6443. (org-defkey map [(right)] 'org-goto-right)
  6444. (org-defkey map [(control ?g)] 'org-goto-quit)
  6445. (org-defkey map "\C-i" 'org-cycle)
  6446. (org-defkey map [(tab)] 'org-cycle)
  6447. (org-defkey map [(down)] 'outline-next-visible-heading)
  6448. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6449. (if org-goto-auto-isearch
  6450. (if (fboundp 'define-key-after)
  6451. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6452. nil)
  6453. (org-defkey map "q" 'org-goto-quit)
  6454. (org-defkey map "n" 'outline-next-visible-heading)
  6455. (org-defkey map "p" 'outline-previous-visible-heading)
  6456. (org-defkey map "f" 'outline-forward-same-level)
  6457. (org-defkey map "b" 'outline-backward-same-level)
  6458. (org-defkey map "u" 'outline-up-heading))
  6459. (org-defkey map "/" 'org-occur)
  6460. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6461. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6462. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6463. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6464. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6465. map)))
  6466. (defconst org-goto-help
  6467. "Browse buffer copy, to find location or copy text.%s
  6468. RET=jump to location C-g=quit and return to previous location
  6469. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6470. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6471. (defun org-goto (&optional alternative-interface)
  6472. "Look up a different location in the current file, keeping current visibility.
  6473. When you want look-up or go to a different location in a
  6474. document, the fastest way is often to fold the entire buffer and
  6475. then dive into the tree. This method has the disadvantage, that
  6476. the previous location will be folded, which may not be what you
  6477. want.
  6478. This command works around this by showing a copy of the current
  6479. buffer in an indirect buffer, in overview mode. You can dive
  6480. into the tree in that copy, use org-occur and incremental search
  6481. to find a location. When pressing RET or `Q', the command
  6482. returns to the original buffer in which the visibility is still
  6483. unchanged. After RET it will also jump to the location selected
  6484. in the indirect buffer and expose the headline hierarchy above.
  6485. With a prefix argument, use the alternative interface: e.g. if
  6486. `org-goto-interface' is 'outline use 'outline-path-completion."
  6487. (interactive "P")
  6488. (org-goto-map)
  6489. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6490. (org-refile-use-outline-path t)
  6491. (org-refile-target-verify-function nil)
  6492. (interface
  6493. (if (not alternative-interface)
  6494. org-goto-interface
  6495. (if (eq org-goto-interface 'outline)
  6496. 'outline-path-completion
  6497. 'outline)))
  6498. (org-goto-start-pos (point))
  6499. (selected-point
  6500. (if (eq interface 'outline)
  6501. (car (org-get-location (current-buffer) org-goto-help))
  6502. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6503. (org-refile-check-position pa)
  6504. (nth 3 pa)))))
  6505. (if selected-point
  6506. (progn
  6507. (org-mark-ring-push org-goto-start-pos)
  6508. (goto-char selected-point)
  6509. (if (or (outline-invisible-p) (org-invisible-p2))
  6510. (org-show-context 'org-goto)))
  6511. (message "Quit"))))
  6512. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6513. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6514. (defvar org-goto-local-auto-isearch-map) ; defined below
  6515. (defun org-get-location (buf help)
  6516. "Let the user select a location in the Org-mode buffer BUF.
  6517. This function uses a recursive edit. It returns the selected position
  6518. or nil."
  6519. (org-no-popups
  6520. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6521. (isearch-hide-immediately nil)
  6522. (isearch-search-fun-function
  6523. (lambda () 'org-goto-local-search-headings))
  6524. (org-goto-selected-point org-goto-exit-command))
  6525. (save-excursion
  6526. (save-window-excursion
  6527. (delete-other-windows)
  6528. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6529. (org-pop-to-buffer-same-window
  6530. (condition-case nil
  6531. (make-indirect-buffer (current-buffer) "*org-goto*")
  6532. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6533. (with-output-to-temp-buffer "*Org Help*"
  6534. (princ (format help (if org-goto-auto-isearch
  6535. " Just type for auto-isearch."
  6536. " n/p/f/b/u to navigate, q to quit."))))
  6537. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6538. (setq buffer-read-only nil)
  6539. (let ((org-startup-truncated t)
  6540. (org-startup-folded nil)
  6541. (org-startup-align-all-tables nil))
  6542. (org-mode)
  6543. (org-overview))
  6544. (setq buffer-read-only t)
  6545. (if (and (boundp 'org-goto-start-pos)
  6546. (integer-or-marker-p org-goto-start-pos))
  6547. (let ((org-show-hierarchy-above t)
  6548. (org-show-siblings t)
  6549. (org-show-following-heading t))
  6550. (goto-char org-goto-start-pos)
  6551. (and (outline-invisible-p) (org-show-context)))
  6552. (goto-char (point-min)))
  6553. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6554. (message "Select location and press RET")
  6555. (use-local-map org-goto-map)
  6556. (recursive-edit)))
  6557. (kill-buffer "*org-goto*")
  6558. (cons org-goto-selected-point org-goto-exit-command))))
  6559. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6560. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6561. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6562. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6563. (defun org-goto-local-search-headings (string bound noerror)
  6564. "Search and make sure that any matches are in headlines."
  6565. (catch 'return
  6566. (while (if isearch-forward
  6567. (search-forward string bound noerror)
  6568. (search-backward string bound noerror))
  6569. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6570. (and (member :headline context)
  6571. (not (member :tags context))))
  6572. (throw 'return (point))))))
  6573. (defun org-goto-local-auto-isearch ()
  6574. "Start isearch."
  6575. (interactive)
  6576. (goto-char (point-min))
  6577. (let ((keys (this-command-keys)))
  6578. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6579. (isearch-mode t)
  6580. (isearch-process-search-char (string-to-char keys)))))
  6581. (defun org-goto-ret (&optional arg)
  6582. "Finish `org-goto' by going to the new location."
  6583. (interactive "P")
  6584. (setq org-goto-selected-point (point)
  6585. org-goto-exit-command 'return)
  6586. (throw 'exit nil))
  6587. (defun org-goto-left ()
  6588. "Finish `org-goto' by going to the new location."
  6589. (interactive)
  6590. (if (org-at-heading-p)
  6591. (progn
  6592. (beginning-of-line 1)
  6593. (setq org-goto-selected-point (point)
  6594. org-goto-exit-command 'left)
  6595. (throw 'exit nil))
  6596. (user-error "Not on a heading")))
  6597. (defun org-goto-right ()
  6598. "Finish `org-goto' by going to the new location."
  6599. (interactive)
  6600. (if (org-at-heading-p)
  6601. (progn
  6602. (setq org-goto-selected-point (point)
  6603. org-goto-exit-command 'right)
  6604. (throw 'exit nil))
  6605. (user-error "Not on a heading")))
  6606. (defun org-goto-quit ()
  6607. "Finish `org-goto' without cursor motion."
  6608. (interactive)
  6609. (setq org-goto-selected-point nil)
  6610. (setq org-goto-exit-command 'quit)
  6611. (throw 'exit nil))
  6612. ;;; Indirect buffer display of subtrees
  6613. (defvar org-indirect-dedicated-frame nil
  6614. "This is the frame being used for indirect tree display.")
  6615. (defvar org-last-indirect-buffer nil)
  6616. (defun org-tree-to-indirect-buffer (&optional arg)
  6617. "Create indirect buffer and narrow it to current subtree.
  6618. With a numerical prefix ARG, go up to this level and then take that tree.
  6619. If ARG is negative, go up that many levels.
  6620. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6621. indirect buffer previously made with this command, to avoid proliferation of
  6622. indirect buffers. However, when you call the command with a \
  6623. \\[universal-argument] prefix, or
  6624. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6625. is kept so that you can work with several indirect buffers at the same time.
  6626. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6627. \\[universal-argument] prefix also
  6628. requests that a new frame be made for the new buffer, so that the dedicated
  6629. frame is not changed."
  6630. (interactive "P")
  6631. (let ((cbuf (current-buffer))
  6632. (cwin (selected-window))
  6633. (pos (point))
  6634. beg end level heading ibuf)
  6635. (save-excursion
  6636. (org-back-to-heading t)
  6637. (when (numberp arg)
  6638. (setq level (org-outline-level))
  6639. (if (< arg 0) (setq arg (+ level arg)))
  6640. (while (> (setq level (org-outline-level)) arg)
  6641. (org-up-heading-safe)))
  6642. (setq beg (point)
  6643. heading (org-get-heading))
  6644. (org-end-of-subtree t t)
  6645. (if (org-at-heading-p) (backward-char 1))
  6646. (setq end (point)))
  6647. (if (and (buffer-live-p org-last-indirect-buffer)
  6648. (not (eq org-indirect-buffer-display 'new-frame))
  6649. (not arg))
  6650. (kill-buffer org-last-indirect-buffer))
  6651. (setq ibuf (org-get-indirect-buffer cbuf)
  6652. org-last-indirect-buffer ibuf)
  6653. (cond
  6654. ((or (eq org-indirect-buffer-display 'new-frame)
  6655. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6656. (select-frame (make-frame))
  6657. (delete-other-windows)
  6658. (org-pop-to-buffer-same-window ibuf)
  6659. (org-set-frame-title heading))
  6660. ((eq org-indirect-buffer-display 'dedicated-frame)
  6661. (raise-frame
  6662. (select-frame (or (and org-indirect-dedicated-frame
  6663. (frame-live-p org-indirect-dedicated-frame)
  6664. org-indirect-dedicated-frame)
  6665. (setq org-indirect-dedicated-frame (make-frame)))))
  6666. (delete-other-windows)
  6667. (org-pop-to-buffer-same-window ibuf)
  6668. (org-set-frame-title (concat "Indirect: " heading)))
  6669. ((eq org-indirect-buffer-display 'current-window)
  6670. (org-pop-to-buffer-same-window ibuf))
  6671. ((eq org-indirect-buffer-display 'other-window)
  6672. (pop-to-buffer ibuf))
  6673. (t (error "Invalid value")))
  6674. (if (featurep 'xemacs)
  6675. (save-excursion (org-mode) (turn-on-font-lock)))
  6676. (narrow-to-region beg end)
  6677. (show-all)
  6678. (goto-char pos)
  6679. (run-hook-with-args 'org-cycle-hook 'all)
  6680. (and (window-live-p cwin) (select-window cwin))))
  6681. (defun org-get-indirect-buffer (&optional buffer)
  6682. (setq buffer (or buffer (current-buffer)))
  6683. (let ((n 1) (base (buffer-name buffer)) bname)
  6684. (while (buffer-live-p
  6685. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6686. (setq n (1+ n)))
  6687. (condition-case nil
  6688. (make-indirect-buffer buffer bname 'clone)
  6689. (error (make-indirect-buffer buffer bname)))))
  6690. (defun org-set-frame-title (title)
  6691. "Set the title of the current frame to the string TITLE."
  6692. ;; FIXME: how to name a single frame in XEmacs???
  6693. (unless (featurep 'xemacs)
  6694. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6695. ;;;; Structure editing
  6696. ;;; Inserting headlines
  6697. (defun org-previous-line-empty-p (&optional next)
  6698. "Is the previous line a blank line?
  6699. When NEXT is non-nil, check the next line instead."
  6700. (save-excursion
  6701. (and (not (bobp))
  6702. (or (beginning-of-line (if next 2 0)) t)
  6703. (save-match-data
  6704. (looking-at "[ \t]*$")))))
  6705. (defun org-insert-heading (&optional arg invisible-ok)
  6706. "Insert a new heading or item with same depth at point.
  6707. If point is in a plain list and ARG is nil, create a new list item.
  6708. With one universal prefix argument, insert a heading even in lists.
  6709. With two universal prefix arguments, insert the heading at the end
  6710. of the parent subtree.
  6711. If point is at the beginning of a headline, insert a sibling before
  6712. the current headline. If point is not at the beginning, split the line
  6713. and create a new headline with the text in the current line after point
  6714. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6715. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6716. This is important for non-interactive uses of the command."
  6717. (interactive "P")
  6718. (if (org-called-interactively-p 'any) (org-reveal))
  6719. (let ((itemp (org-in-item-p)))
  6720. (cond
  6721. ((or (= (buffer-size) 0)
  6722. (and (not (save-excursion
  6723. (and (ignore-errors (org-back-to-heading invisible-ok))
  6724. (org-at-heading-p))))
  6725. (or arg (not itemp))))
  6726. (insert
  6727. (if (org-previous-line-empty-p) "" "\n")
  6728. (if (org-in-src-block-p) ",* " "* "))
  6729. (run-hooks 'org-insert-heading-hook))
  6730. ((or arg
  6731. (and (not itemp) org-insert-heading-respect-content)
  6732. (not (org-insert-item
  6733. (save-excursion
  6734. (and itemp
  6735. (goto-char itemp)
  6736. (looking-at org-list-full-item-re)
  6737. (match-string 3))))))
  6738. (let (begn endn)
  6739. (when (org-buffer-narrowed-p)
  6740. (setq begn (point-min) endn (point-max))
  6741. (widen))
  6742. (let* ((empty-line-p nil)
  6743. (eops (equal arg '(16))) ; insert at end of parent subtree
  6744. (org-insert-heading-respect-content
  6745. (or (not (null arg)) org-insert-heading-respect-content))
  6746. (level nil)
  6747. (on-heading (org-at-heading-p))
  6748. ;; Get a level to fall back on
  6749. (fix-level
  6750. (save-excursion
  6751. (org-back-to-heading t)
  6752. (looking-at org-outline-regexp)
  6753. (make-string (1- (length (match-string 0))) ?*)))
  6754. (on-empty-line
  6755. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6756. (head (save-excursion
  6757. (condition-case nil
  6758. (progn
  6759. (org-back-to-heading invisible-ok)
  6760. (when (and (not on-heading)
  6761. (featurep 'org-inlinetask)
  6762. (integerp org-inlinetask-min-level)
  6763. (>= (length (match-string 0))
  6764. org-inlinetask-min-level))
  6765. ;; Find a heading level before the inline task
  6766. (while (and (setq level (org-up-heading-safe))
  6767. (>= level org-inlinetask-min-level)))
  6768. (if (org-at-heading-p)
  6769. (org-back-to-heading invisible-ok)
  6770. (error "This should not happen")))
  6771. (unless (and (save-excursion
  6772. (save-match-data
  6773. (org-backward-heading-same-level 1 invisible-ok))
  6774. (= (point) (match-beginning 0)))
  6775. (not (org-previous-line-empty-p t)))
  6776. (setq empty-line-p (org-previous-line-empty-p)))
  6777. (match-string 0))
  6778. (error (or fix-level "* ")))))
  6779. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6780. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6781. pos hide-previous previous-pos)
  6782. (if ;; At the beginning of a heading, open a new line for insertion
  6783. (and (bolp) (org-at-heading-p)
  6784. (not eops)
  6785. (or (bobp)
  6786. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6787. (open-line (if blank 2 1))
  6788. (save-excursion
  6789. (setq previous-pos (point-at-bol))
  6790. (end-of-line)
  6791. (setq hide-previous (outline-invisible-p)))
  6792. (and org-insert-heading-respect-content
  6793. (save-excursion
  6794. (while (outline-invisible-p)
  6795. (org-show-subtree)
  6796. (org-up-heading-safe))))
  6797. (let ((split
  6798. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6799. (save-excursion
  6800. (let ((p (point)))
  6801. (goto-char (point-at-bol))
  6802. (and (looking-at org-complex-heading-regexp)
  6803. (match-beginning 4)
  6804. (> p (match-beginning 4)))))))
  6805. tags pos)
  6806. (cond
  6807. ;; Insert a new line, possibly at end of parent subtree
  6808. ((and (not arg) (not on-heading) (not on-empty-line)
  6809. (not (save-excursion
  6810. (beginning-of-line 1)
  6811. (or (looking-at org-list-full-item-re)
  6812. ;; Don't convert :end: lines to headline
  6813. (looking-at "^\\s-*:end:")
  6814. (looking-at "^\\s-*#\\+end_?")))))
  6815. (beginning-of-line 1))
  6816. (org-insert-heading-respect-content
  6817. (if (not eops)
  6818. (progn
  6819. (org-end-of-subtree nil t)
  6820. (and (looking-at "^\\*") (backward-char 1))
  6821. (while (and (not (bobp))
  6822. ;; Don't delete spaces in empty headlines
  6823. (not (looking-back org-outline-regexp))
  6824. (member (char-before) '(?\ ?\t ?\n)))
  6825. (backward-delete-char 1)))
  6826. (let ((p (point)))
  6827. (org-up-heading-safe)
  6828. (if (= p (point))
  6829. (goto-char (point-max))
  6830. (org-end-of-subtree nil t))))
  6831. (when (featurep 'org-inlinetask)
  6832. (while (and (not (eobp))
  6833. (looking-at "\\(\\*+\\)[ \t]+")
  6834. (>= (length (match-string 1))
  6835. org-inlinetask-min-level))
  6836. (org-end-of-subtree nil t)))
  6837. (or (bolp) (newline))
  6838. (or (org-previous-line-empty-p)
  6839. (and blank (newline)))
  6840. (if (or empty-line-p eops) (open-line 1)))
  6841. ;; Insert a headling containing text after point
  6842. ((org-at-heading-p)
  6843. (when hide-previous
  6844. (show-children)
  6845. (org-show-entry))
  6846. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6847. (setq tags (and (match-end 2) (match-string 2)))
  6848. (and (match-end 1)
  6849. (delete-region (match-beginning 1) (match-end 1)))
  6850. (setq pos (point-at-bol))
  6851. (or split (end-of-line 1))
  6852. (delete-horizontal-space)
  6853. (if (string-match "\\`\\*+\\'"
  6854. (buffer-substring (point-at-bol) (point)))
  6855. (insert " "))
  6856. (newline (if blank 2 1))
  6857. (when tags
  6858. (save-excursion
  6859. (goto-char pos)
  6860. (end-of-line 1)
  6861. (insert " " tags)
  6862. (org-set-tags nil 'align))))
  6863. (t
  6864. (or split (end-of-line 1))
  6865. (newline (cond ((and blank (not on-empty-line)) 2)
  6866. (blank 1)
  6867. (on-empty-line 0) (t 1)))))))
  6868. (insert head) (just-one-space)
  6869. (setq pos (point))
  6870. (end-of-line 1)
  6871. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6872. (when (and org-insert-heading-respect-content hide-previous)
  6873. (save-excursion
  6874. (goto-char previous-pos)
  6875. (hide-subtree)))
  6876. (when (and begn endn)
  6877. (narrow-to-region (min (point) begn) (max (point) endn)))
  6878. (run-hooks 'org-insert-heading-hook)))))))
  6879. (defun org-get-heading (&optional no-tags no-todo)
  6880. "Return the heading of the current entry, without the stars.
  6881. When NO-TAGS is non-nil, don't include tags.
  6882. When NO-TODO is non-nil, don't include TODO keywords."
  6883. (save-excursion
  6884. (org-back-to-heading t)
  6885. (cond
  6886. ((and no-tags no-todo)
  6887. (looking-at org-complex-heading-regexp)
  6888. (match-string 4))
  6889. (no-tags
  6890. (looking-at (concat org-outline-regexp
  6891. "\\(.*?\\)"
  6892. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6893. (match-string 1))
  6894. (no-todo
  6895. (looking-at org-todo-line-regexp)
  6896. (match-string 3))
  6897. (t (looking-at org-heading-regexp)
  6898. (match-string 2)))))
  6899. (defvar orgstruct-mode) ; defined below
  6900. (defun org-heading-components ()
  6901. "Return the components of the current heading.
  6902. This is a list with the following elements:
  6903. - the level as an integer
  6904. - the reduced level, different if `org-odd-levels-only' is set.
  6905. - the TODO keyword, or nil
  6906. - the priority character, like ?A, or nil if no priority is given
  6907. - the headline text itself, or the tags string if no headline text
  6908. - the tags string, or nil."
  6909. (save-excursion
  6910. (org-back-to-heading t)
  6911. (if (let (case-fold-search)
  6912. (looking-at
  6913. (if orgstruct-mode
  6914. org-heading-regexp
  6915. org-complex-heading-regexp)))
  6916. (if orgstruct-mode
  6917. (list (length (match-string 1))
  6918. (org-reduced-level (length (match-string 1)))
  6919. nil
  6920. nil
  6921. (match-string 2)
  6922. nil)
  6923. (list (length (match-string 1))
  6924. (org-reduced-level (length (match-string 1)))
  6925. (org-match-string-no-properties 2)
  6926. (and (match-end 3) (aref (match-string 3) 2))
  6927. (org-match-string-no-properties 4)
  6928. (org-match-string-no-properties 5))))))
  6929. (defun org-get-entry ()
  6930. "Get the entry text, after heading, entire subtree."
  6931. (save-excursion
  6932. (org-back-to-heading t)
  6933. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6934. (defun org-insert-heading-after-current ()
  6935. "Insert a new heading with same level as current, after current subtree."
  6936. (interactive)
  6937. (org-back-to-heading)
  6938. (org-insert-heading)
  6939. (org-move-subtree-down)
  6940. (end-of-line 1))
  6941. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6942. "Insert heading with `org-insert-heading-respect-content' set to t."
  6943. (interactive "P")
  6944. (let ((org-insert-heading-respect-content t))
  6945. (org-insert-heading arg invisible-ok)))
  6946. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6947. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6948. (interactive "P")
  6949. (let ((org-insert-heading-respect-content t))
  6950. (org-insert-todo-heading force-state t)))
  6951. (defun org-insert-todo-heading (arg &optional force-heading)
  6952. "Insert a new heading with the same level and TODO state as current heading.
  6953. If the heading has no TODO state, or if the state is DONE, use the first
  6954. state (TODO by default). Also one prefix arg, force first state. With two
  6955. prefix args, force inserting at the end of the parent subtree."
  6956. (interactive "P")
  6957. (when (or force-heading (not (org-insert-item 'checkbox)))
  6958. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6959. force-heading))
  6960. (save-excursion
  6961. (org-back-to-heading)
  6962. (outline-previous-heading)
  6963. (looking-at org-todo-line-regexp))
  6964. (let*
  6965. ((new-mark-x
  6966. (if (or arg
  6967. (not (match-beginning 2))
  6968. (member (match-string 2) org-done-keywords))
  6969. (car org-todo-keywords-1)
  6970. (match-string 2)))
  6971. (new-mark
  6972. (or
  6973. (run-hook-with-args-until-success
  6974. 'org-todo-get-default-hook new-mark-x nil)
  6975. new-mark-x)))
  6976. (beginning-of-line 1)
  6977. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6978. (if org-treat-insert-todo-heading-as-state-change
  6979. (org-todo new-mark)
  6980. (insert new-mark " "))))
  6981. (when org-provide-todo-statistics
  6982. (org-update-parent-todo-statistics))))
  6983. (defun org-insert-subheading (arg)
  6984. "Insert a new subheading and demote it.
  6985. Works for outline headings and for plain lists alike."
  6986. (interactive "P")
  6987. (org-insert-heading arg)
  6988. (cond
  6989. ((org-at-heading-p) (org-do-demote))
  6990. ((org-at-item-p) (org-indent-item))))
  6991. (defun org-insert-todo-subheading (arg)
  6992. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6993. Works for outline headings and for plain lists alike."
  6994. (interactive "P")
  6995. (org-insert-todo-heading arg)
  6996. (cond
  6997. ((org-at-heading-p) (org-do-demote))
  6998. ((org-at-item-p) (org-indent-item))))
  6999. ;;; Promotion and Demotion
  7000. (defvar org-after-demote-entry-hook nil
  7001. "Hook run after an entry has been demoted.
  7002. The cursor will be at the beginning of the entry.
  7003. When a subtree is being demoted, the hook will be called for each node.")
  7004. (defvar org-after-promote-entry-hook nil
  7005. "Hook run after an entry has been promoted.
  7006. The cursor will be at the beginning of the entry.
  7007. When a subtree is being promoted, the hook will be called for each node.")
  7008. (defun org-promote-subtree ()
  7009. "Promote the entire subtree.
  7010. See also `org-promote'."
  7011. (interactive)
  7012. (save-excursion
  7013. (org-with-limited-levels (org-map-tree 'org-promote)))
  7014. (org-fix-position-after-promote))
  7015. (defun org-demote-subtree ()
  7016. "Demote the entire subtree. See `org-demote'.
  7017. See also `org-promote'."
  7018. (interactive)
  7019. (save-excursion
  7020. (org-with-limited-levels (org-map-tree 'org-demote)))
  7021. (org-fix-position-after-promote))
  7022. (defun org-do-promote ()
  7023. "Promote the current heading higher up the tree.
  7024. If the region is active in `transient-mark-mode', promote all headings
  7025. in the region."
  7026. (interactive)
  7027. (save-excursion
  7028. (if (org-region-active-p)
  7029. (org-map-region 'org-promote (region-beginning) (region-end))
  7030. (org-promote)))
  7031. (org-fix-position-after-promote))
  7032. (defun org-do-demote ()
  7033. "Demote the current heading lower down the tree.
  7034. If the region is active in `transient-mark-mode', demote all headings
  7035. in the region."
  7036. (interactive)
  7037. (save-excursion
  7038. (if (org-region-active-p)
  7039. (org-map-region 'org-demote (region-beginning) (region-end))
  7040. (org-demote)))
  7041. (org-fix-position-after-promote))
  7042. (defun org-fix-position-after-promote ()
  7043. "Make sure that after pro/demotion cursor position is right."
  7044. (let ((pos (point)))
  7045. (when (save-excursion
  7046. (beginning-of-line 1)
  7047. (looking-at org-todo-line-regexp)
  7048. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7049. (cond ((eobp) (insert " "))
  7050. ((eolp) (insert " "))
  7051. ((equal (char-after) ?\ ) (forward-char 1))))))
  7052. (defun org-current-level ()
  7053. "Return the level of the current entry, or nil if before the first headline.
  7054. The level is the number of stars at the beginning of the headline."
  7055. (save-excursion
  7056. (org-with-limited-levels
  7057. (if (ignore-errors (org-back-to-heading t))
  7058. (funcall outline-level)))))
  7059. (defun org-get-previous-line-level ()
  7060. "Return the outline depth of the last headline before the current line.
  7061. Returns 0 for the first headline in the buffer, and nil if before the
  7062. first headline."
  7063. (let ((current-level (org-current-level))
  7064. (prev-level (when (> (line-number-at-pos) 1)
  7065. (save-excursion
  7066. (beginning-of-line 0)
  7067. (org-current-level)))))
  7068. (cond ((null current-level) nil) ; Before first headline
  7069. ((null prev-level) 0) ; At first headline
  7070. (prev-level))))
  7071. (defun org-reduced-level (l)
  7072. "Compute the effective level of a heading.
  7073. This takes into account the setting of `org-odd-levels-only'."
  7074. (cond
  7075. ((zerop l) 0)
  7076. (org-odd-levels-only (1+ (floor (/ l 2))))
  7077. (t l)))
  7078. (defun org-level-increment ()
  7079. "Return the number of stars that will be added or removed at a
  7080. time to headlines when structure editing, based on the value of
  7081. `org-odd-levels-only'."
  7082. (if org-odd-levels-only 2 1))
  7083. (defun org-get-valid-level (level &optional change)
  7084. "Rectify a level change under the influence of `org-odd-levels-only'
  7085. LEVEL is a current level, CHANGE is by how much the level should be
  7086. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7087. even level numbers will become the next higher odd number."
  7088. (if org-odd-levels-only
  7089. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7090. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7091. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7092. (max 1 (+ level (or change 0)))))
  7093. (if (boundp 'define-obsolete-function-alias)
  7094. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7095. (define-obsolete-function-alias 'org-get-legal-level
  7096. 'org-get-valid-level)
  7097. (define-obsolete-function-alias 'org-get-legal-level
  7098. 'org-get-valid-level "23.1")))
  7099. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7100. ;; ̀org-with-limited-levels'
  7101. (defun org-promote ()
  7102. "Promote the current heading higher up the tree.
  7103. If the region is active in `transient-mark-mode', promote all headings
  7104. in the region."
  7105. (org-back-to-heading t)
  7106. (let* ((level (save-match-data (funcall outline-level)))
  7107. (after-change-functions (remove 'flyspell-after-change-function
  7108. after-change-functions))
  7109. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7110. (diff (abs (- level (length up-head) -1))))
  7111. (cond ((and (= level 1) org-called-with-limited-levels
  7112. org-allow-promoting-top-level-subtree)
  7113. (replace-match "# " nil t))
  7114. ((= level 1)
  7115. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7116. (t (replace-match up-head nil t)))
  7117. ;; Fixup tag positioning
  7118. (unless (= level 1)
  7119. (and org-auto-align-tags (org-set-tags nil t))
  7120. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7121. (run-hooks 'org-after-promote-entry-hook)))
  7122. (defun org-demote ()
  7123. "Demote the current heading lower down the tree.
  7124. If the region is active in `transient-mark-mode', demote all headings
  7125. in the region."
  7126. (org-back-to-heading t)
  7127. (let* ((level (save-match-data (funcall outline-level)))
  7128. (after-change-functions (remove 'flyspell-after-change-function
  7129. after-change-functions))
  7130. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7131. (diff (abs (- level (length down-head) -1))))
  7132. (replace-match down-head nil t)
  7133. ;; Fixup tag positioning
  7134. (and org-auto-align-tags (org-set-tags nil t))
  7135. (if org-adapt-indentation (org-fixup-indentation diff))
  7136. (run-hooks 'org-after-demote-entry-hook)))
  7137. (defun org-cycle-level ()
  7138. "Cycle the level of an empty headline through possible states.
  7139. This goes first to child, then to parent, level, then up the hierarchy.
  7140. After top level, it switches back to sibling level."
  7141. (interactive)
  7142. (let ((org-adapt-indentation nil))
  7143. (when (org-point-at-end-of-empty-headline)
  7144. (setq this-command 'org-cycle-level) ; Only needed for caching
  7145. (let ((cur-level (org-current-level))
  7146. (prev-level (org-get-previous-line-level)))
  7147. (cond
  7148. ;; If first headline in file, promote to top-level.
  7149. ((= prev-level 0)
  7150. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7151. do (org-do-promote)))
  7152. ;; If same level as prev, demote one.
  7153. ((= prev-level cur-level)
  7154. (org-do-demote))
  7155. ;; If parent is top-level, promote to top level if not already.
  7156. ((= prev-level 1)
  7157. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7158. do (org-do-promote)))
  7159. ;; If top-level, return to prev-level.
  7160. ((= cur-level 1)
  7161. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7162. do (org-do-demote)))
  7163. ;; If less than prev-level, promote one.
  7164. ((< cur-level prev-level)
  7165. (org-do-promote))
  7166. ;; If deeper than prev-level, promote until higher than
  7167. ;; prev-level.
  7168. ((> cur-level prev-level)
  7169. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7170. do (org-do-promote))))
  7171. t))))
  7172. (defun org-map-tree (fun)
  7173. "Call FUN for every heading underneath the current one."
  7174. (org-back-to-heading)
  7175. (let ((level (funcall outline-level)))
  7176. (save-excursion
  7177. (funcall fun)
  7178. (while (and (progn
  7179. (outline-next-heading)
  7180. (> (funcall outline-level) level))
  7181. (not (eobp)))
  7182. (funcall fun)))))
  7183. (defun org-map-region (fun beg end)
  7184. "Call FUN for every heading between BEG and END."
  7185. (let ((org-ignore-region t))
  7186. (save-excursion
  7187. (setq end (copy-marker end))
  7188. (goto-char beg)
  7189. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7190. (< (point) end))
  7191. (funcall fun))
  7192. (while (and (progn
  7193. (outline-next-heading)
  7194. (< (point) end))
  7195. (not (eobp)))
  7196. (funcall fun)))))
  7197. (defvar org-property-end-re) ; silence byte-compiler
  7198. (defun org-fixup-indentation (diff)
  7199. "Change the indentation in the current entry by DIFF.
  7200. However, if any line in the current entry has no indentation, or if it
  7201. would end up with no indentation after the change, nothing at all is done."
  7202. (save-excursion
  7203. (let ((end (save-excursion (outline-next-heading)
  7204. (point-marker)))
  7205. (prohibit (if (> diff 0)
  7206. "^\\S-"
  7207. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7208. col)
  7209. (unless (save-excursion (end-of-line 1)
  7210. (re-search-forward prohibit end t))
  7211. (while (and (< (point) end)
  7212. (re-search-forward "^[ \t]+" end t))
  7213. (goto-char (match-end 0))
  7214. (setq col (current-column))
  7215. (if (< diff 0) (replace-match ""))
  7216. (org-indent-to-column (+ diff col))))
  7217. (move-marker end nil))))
  7218. (defun org-convert-to-odd-levels ()
  7219. "Convert an org-mode file with all levels allowed to one with odd levels.
  7220. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7221. level 5 etc."
  7222. (interactive)
  7223. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7224. (let ((outline-level 'org-outline-level)
  7225. (org-odd-levels-only nil) n)
  7226. (save-excursion
  7227. (goto-char (point-min))
  7228. (while (re-search-forward "^\\*\\*+ " nil t)
  7229. (setq n (- (length (match-string 0)) 2))
  7230. (while (>= (setq n (1- n)) 0)
  7231. (org-demote))
  7232. (end-of-line 1))))))
  7233. (defun org-convert-to-oddeven-levels ()
  7234. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7235. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7236. file contains a section with an even level, conversion would
  7237. destroy the structure of the file. An error is signaled in this
  7238. case."
  7239. (interactive)
  7240. (goto-char (point-min))
  7241. ;; First check if there are no even levels
  7242. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7243. (org-show-context t)
  7244. (error "Not all levels are odd in this file. Conversion not possible"))
  7245. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7246. (let ((outline-regexp org-outline-regexp)
  7247. (outline-level 'org-outline-level)
  7248. (org-odd-levels-only nil) n)
  7249. (save-excursion
  7250. (goto-char (point-min))
  7251. (while (re-search-forward "^\\*\\*+ " nil t)
  7252. (setq n (/ (1- (length (match-string 0))) 2))
  7253. (while (>= (setq n (1- n)) 0)
  7254. (org-promote))
  7255. (end-of-line 1))))))
  7256. (defun org-tr-level (n)
  7257. "Make N odd if required."
  7258. (if org-odd-levels-only (1+ (/ n 2)) n))
  7259. ;;; Vertical tree motion, cutting and pasting of subtrees
  7260. (defun org-move-subtree-up (&optional arg)
  7261. "Move the current subtree up past ARG headlines of the same level."
  7262. (interactive "p")
  7263. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7264. (defun org-move-subtree-down (&optional arg)
  7265. "Move the current subtree down past ARG headlines of the same level."
  7266. (interactive "p")
  7267. (setq arg (prefix-numeric-value arg))
  7268. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7269. 'org-get-last-sibling))
  7270. (ins-point (make-marker))
  7271. (cnt (abs arg))
  7272. (col (current-column))
  7273. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7274. ;; Select the tree
  7275. (org-back-to-heading)
  7276. (setq beg0 (point))
  7277. (save-excursion
  7278. (setq ne-beg (org-back-over-empty-lines))
  7279. (setq beg (point)))
  7280. (save-match-data
  7281. (save-excursion (outline-end-of-heading)
  7282. (setq folded (outline-invisible-p)))
  7283. (outline-end-of-subtree))
  7284. (outline-next-heading)
  7285. (setq ne-end (org-back-over-empty-lines))
  7286. (setq end (point))
  7287. (goto-char beg0)
  7288. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7289. ;; include less whitespace
  7290. (save-excursion
  7291. (goto-char beg)
  7292. (forward-line (- ne-beg ne-end))
  7293. (setq beg (point))))
  7294. ;; Find insertion point, with error handling
  7295. (while (> cnt 0)
  7296. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7297. (progn (goto-char beg0)
  7298. (user-error "Cannot move past superior level or buffer limit")))
  7299. (setq cnt (1- cnt)))
  7300. (if (> arg 0)
  7301. ;; Moving forward - still need to move over subtree
  7302. (progn (org-end-of-subtree t t)
  7303. (save-excursion
  7304. (org-back-over-empty-lines)
  7305. (or (bolp) (newline)))))
  7306. (setq ne-ins (org-back-over-empty-lines))
  7307. (move-marker ins-point (point))
  7308. (setq txt (buffer-substring beg end))
  7309. (org-save-markers-in-region beg end)
  7310. (delete-region beg end)
  7311. (org-remove-empty-overlays-at beg)
  7312. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7313. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7314. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7315. (let ((bbb (point)))
  7316. (insert-before-markers txt)
  7317. (org-reinstall-markers-in-region bbb)
  7318. (move-marker ins-point bbb))
  7319. (or (bolp) (insert "\n"))
  7320. (setq ins-end (point))
  7321. (goto-char ins-point)
  7322. (org-skip-whitespace)
  7323. (when (and (< arg 0)
  7324. (org-first-sibling-p)
  7325. (> ne-ins ne-beg))
  7326. ;; Move whitespace back to beginning
  7327. (save-excursion
  7328. (goto-char ins-end)
  7329. (let ((kill-whole-line t))
  7330. (kill-line (- ne-ins ne-beg)) (point)))
  7331. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7332. (move-marker ins-point nil)
  7333. (if folded
  7334. (hide-subtree)
  7335. (org-show-entry)
  7336. (show-children)
  7337. (org-cycle-hide-drawers 'children))
  7338. (org-clean-visibility-after-subtree-move)
  7339. ;; move back to the initial column we were at
  7340. (move-to-column col)))
  7341. (defvar org-subtree-clip ""
  7342. "Clipboard for cut and paste of subtrees.
  7343. This is actually only a copy of the kill, because we use the normal kill
  7344. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7345. (defvar org-subtree-clip-folded nil
  7346. "Was the last copied subtree folded?
  7347. This is used to fold the tree back after pasting.")
  7348. (defun org-cut-subtree (&optional n)
  7349. "Cut the current subtree into the clipboard.
  7350. With prefix arg N, cut this many sequential subtrees.
  7351. This is a short-hand for marking the subtree and then cutting it."
  7352. (interactive "p")
  7353. (org-copy-subtree n 'cut))
  7354. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7355. "Cut the current subtree into the clipboard.
  7356. With prefix arg N, cut this many sequential subtrees.
  7357. This is a short-hand for marking the subtree and then copying it.
  7358. If CUT is non-nil, actually cut the subtree.
  7359. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7360. of some markers in the region, even if CUT is non-nil. This is
  7361. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7362. (interactive "p")
  7363. (let (beg end folded (beg0 (point)))
  7364. (if (org-called-interactively-p 'any)
  7365. (org-back-to-heading nil) ; take what looks like a subtree
  7366. (org-back-to-heading t)) ; take what is really there
  7367. (setq beg (point))
  7368. (skip-chars-forward " \t\r\n")
  7369. (save-match-data
  7370. (if nosubtrees
  7371. (outline-next-heading)
  7372. (save-excursion (outline-end-of-heading)
  7373. (setq folded (outline-invisible-p)))
  7374. (condition-case nil
  7375. (org-forward-heading-same-level (1- n) t)
  7376. (error nil))
  7377. (org-end-of-subtree t t)))
  7378. (setq end (point))
  7379. (goto-char beg0)
  7380. (when (> end beg)
  7381. (setq org-subtree-clip-folded folded)
  7382. (when (or cut force-store-markers)
  7383. (org-save-markers-in-region beg end))
  7384. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7385. (setq org-subtree-clip (current-kill 0))
  7386. (message "%s: Subtree(s) with %d characters"
  7387. (if cut "Cut" "Copied")
  7388. (length org-subtree-clip)))))
  7389. (defun org-paste-subtree (&optional level tree for-yank)
  7390. "Paste the clipboard as a subtree, with modification of headline level.
  7391. The entire subtree is promoted or demoted in order to match a new headline
  7392. level.
  7393. If the cursor is at the beginning of a headline, the same level as
  7394. that headline is used to paste the tree.
  7395. If not, the new level is derived from the *visible* headings
  7396. before and after the insertion point, and taken to be the inferior headline
  7397. level of the two. So if the previous visible heading is level 3 and the
  7398. next is level 4 (or vice versa), level 4 will be used for insertion.
  7399. This makes sure that the subtree remains an independent subtree and does
  7400. not swallow low level entries.
  7401. You can also force a different level, either by using a numeric prefix
  7402. argument, or by inserting the heading marker by hand. For example, if the
  7403. cursor is after \"*****\", then the tree will be shifted to level 5.
  7404. If optional TREE is given, use this text instead of the kill ring.
  7405. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7406. move back over whitespace before inserting, and move point to the end of
  7407. the inserted text when done."
  7408. (interactive "P")
  7409. (setq tree (or tree (and kill-ring (current-kill 0))))
  7410. (unless (org-kill-is-subtree-p tree)
  7411. (user-error "%s"
  7412. (substitute-command-keys
  7413. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7414. (org-with-limited-levels
  7415. (let* ((visp (not (outline-invisible-p)))
  7416. (txt tree)
  7417. (^re_ "\\(\\*+\\)[ \t]*")
  7418. (old-level (if (string-match org-outline-regexp-bol txt)
  7419. (- (match-end 0) (match-beginning 0) 1)
  7420. -1))
  7421. (force-level (cond (level (prefix-numeric-value level))
  7422. ((and (looking-at "[ \t]*$")
  7423. (string-match
  7424. "^\\*+$" (buffer-substring
  7425. (point-at-bol) (point))))
  7426. (- (match-end 1) (match-beginning 1)))
  7427. ((and (bolp)
  7428. (looking-at org-outline-regexp))
  7429. (- (match-end 0) (point) 1))))
  7430. (previous-level (save-excursion
  7431. (condition-case nil
  7432. (progn
  7433. (outline-previous-visible-heading 1)
  7434. (if (looking-at ^re_)
  7435. (- (match-end 0) (match-beginning 0) 1)
  7436. 1))
  7437. (error 1))))
  7438. (next-level (save-excursion
  7439. (condition-case nil
  7440. (progn
  7441. (or (looking-at org-outline-regexp)
  7442. (outline-next-visible-heading 1))
  7443. (if (looking-at ^re_)
  7444. (- (match-end 0) (match-beginning 0) 1)
  7445. 1))
  7446. (error 1))))
  7447. (new-level (or force-level (max previous-level next-level)))
  7448. (shift (if (or (= old-level -1)
  7449. (= new-level -1)
  7450. (= old-level new-level))
  7451. 0
  7452. (- new-level old-level)))
  7453. (delta (if (> shift 0) -1 1))
  7454. (func (if (> shift 0) 'org-demote 'org-promote))
  7455. (org-odd-levels-only nil)
  7456. beg end newend)
  7457. ;; Remove the forced level indicator
  7458. (if force-level
  7459. (delete-region (point-at-bol) (point)))
  7460. ;; Paste
  7461. (beginning-of-line (if (bolp) 1 2))
  7462. (setq beg (point))
  7463. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7464. (insert-before-markers txt)
  7465. (unless (string-match "\n\\'" txt) (insert "\n"))
  7466. (setq newend (point))
  7467. (org-reinstall-markers-in-region beg)
  7468. (setq end (point))
  7469. (goto-char beg)
  7470. (skip-chars-forward " \t\n\r")
  7471. (setq beg (point))
  7472. (if (and (outline-invisible-p) visp)
  7473. (save-excursion (outline-show-heading)))
  7474. ;; Shift if necessary
  7475. (unless (= shift 0)
  7476. (save-restriction
  7477. (narrow-to-region beg end)
  7478. (while (not (= shift 0))
  7479. (org-map-region func (point-min) (point-max))
  7480. (setq shift (+ delta shift)))
  7481. (goto-char (point-min))
  7482. (setq newend (point-max))))
  7483. (when (or (org-called-interactively-p 'interactive) for-yank)
  7484. (message "Clipboard pasted as level %d subtree" new-level))
  7485. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7486. kill-ring
  7487. (eq org-subtree-clip (current-kill 0))
  7488. org-subtree-clip-folded)
  7489. ;; The tree was folded before it was killed/copied
  7490. (hide-subtree))
  7491. (and for-yank (goto-char newend)))))
  7492. (defun org-kill-is-subtree-p (&optional txt)
  7493. "Check if the current kill is an outline subtree, or a set of trees.
  7494. Returns nil if kill does not start with a headline, or if the first
  7495. headline level is not the largest headline level in the tree.
  7496. So this will actually accept several entries of equal levels as well,
  7497. which is OK for `org-paste-subtree'.
  7498. If optional TXT is given, check this string instead of the current kill."
  7499. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7500. (re (org-get-limited-outline-regexp))
  7501. (^re (concat "^" re))
  7502. (start-level (and kill
  7503. (string-match
  7504. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7505. kill)
  7506. (- (match-end 2) (match-beginning 2) 1)))
  7507. (start (1+ (or (match-beginning 2) -1))))
  7508. (if (not start-level)
  7509. (progn
  7510. nil) ;; does not even start with a heading
  7511. (catch 'exit
  7512. (while (setq start (string-match ^re kill (1+ start)))
  7513. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7514. (throw 'exit nil)))
  7515. t))))
  7516. (defvar org-markers-to-move nil
  7517. "Markers that should be moved with a cut-and-paste operation.
  7518. Those markers are stored together with their positions relative to
  7519. the start of the region.")
  7520. (defun org-save-markers-in-region (beg end)
  7521. "Check markers in region.
  7522. If these markers are between BEG and END, record their position relative
  7523. to BEG, so that after moving the block of text, we can put the markers back
  7524. into place.
  7525. This function gets called just before an entry or tree gets cut from the
  7526. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7527. called immediately, to move the markers with the entries."
  7528. (setq org-markers-to-move nil)
  7529. (when (featurep 'org-clock)
  7530. (org-clock-save-markers-for-cut-and-paste beg end))
  7531. (when (featurep 'org-agenda)
  7532. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7533. (defun org-check-and-save-marker (marker beg end)
  7534. "Check if MARKER is between BEG and END.
  7535. If yes, remember the marker and the distance to BEG."
  7536. (when (and (marker-buffer marker)
  7537. (equal (marker-buffer marker) (current-buffer)))
  7538. (if (and (>= marker beg) (< marker end))
  7539. (push (cons marker (- marker beg)) org-markers-to-move))))
  7540. (defun org-reinstall-markers-in-region (beg)
  7541. "Move all remembered markers to their position relative to BEG."
  7542. (mapc (lambda (x)
  7543. (move-marker (car x) (+ beg (cdr x))))
  7544. org-markers-to-move)
  7545. (setq org-markers-to-move nil))
  7546. (defun org-narrow-to-subtree ()
  7547. "Narrow buffer to the current subtree."
  7548. (interactive)
  7549. (save-excursion
  7550. (save-match-data
  7551. (org-with-limited-levels
  7552. (narrow-to-region
  7553. (progn (org-back-to-heading t) (point))
  7554. (progn (org-end-of-subtree t t)
  7555. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7556. (point)))))))
  7557. (defun org-narrow-to-block ()
  7558. "Narrow buffer to the current block."
  7559. (interactive)
  7560. (let* ((case-fold-search t)
  7561. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7562. "^[ \t]*#\\+end_.*")))
  7563. (if blockp
  7564. (narrow-to-region (car blockp) (cdr blockp))
  7565. (user-error "Not in a block"))))
  7566. (eval-when-compile
  7567. (defvar org-property-drawer-re))
  7568. (defvar org-property-start-re) ;; defined below
  7569. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7570. "Clone the task (subtree) at point N times.
  7571. The clones will be inserted as siblings.
  7572. In interactive use, the user will be prompted for the number of
  7573. clones to be produced. If the entry has a timestamp, the user
  7574. will also be prompted for a time shift, which may be a repeater
  7575. as used in time stamps, for example `+3d'. To disable this,
  7576. you can call the function with a universal prefix argument.
  7577. When a valid repeater is given and the entry contains any time
  7578. stamps, the clones will become a sequence in time, with time
  7579. stamps in the subtree shifted for each clone produced. If SHIFT
  7580. is nil or the empty string, time stamps will be left alone. The
  7581. ID property of the original subtree is removed.
  7582. If the original subtree did contain time stamps with a repeater,
  7583. the following will happen:
  7584. - the repeater will be removed in each clone
  7585. - an additional clone will be produced, with the current, unshifted
  7586. date(s) in the entry.
  7587. - the original entry will be placed *after* all the clones, with
  7588. repeater intact.
  7589. - the start days in the repeater in the original entry will be shifted
  7590. to past the last clone.
  7591. In this way you can spell out a number of instances of a repeating task,
  7592. and still retain the repeater to cover future instances of the task."
  7593. (interactive "nNumber of clones to produce: ")
  7594. (let ((shift
  7595. (or shift
  7596. (if (and (not (equal current-prefix-arg '(4)))
  7597. (save-excursion
  7598. (re-search-forward org-ts-regexp-both
  7599. (save-excursion
  7600. (org-end-of-subtree t)
  7601. (point)) t)))
  7602. (read-from-minibuffer
  7603. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7604. ""))) ;; No time shift
  7605. (n-no-remove -1)
  7606. (drawer-re org-drawer-regexp)
  7607. beg end template task idprop
  7608. shift-n shift-what doshift nmin nmax)
  7609. (if (not (and (integerp n) (> n 0)))
  7610. (error "Invalid number of replications %s" n))
  7611. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7612. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7613. shift)))
  7614. (error "Invalid shift specification %s" shift))
  7615. (when doshift
  7616. (setq shift-n (string-to-number (match-string 1 shift))
  7617. shift-what (cdr (assoc (match-string 2 shift)
  7618. '(("d" . day) ("w" . week)
  7619. ("m" . month) ("y" . year))))))
  7620. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7621. (setq nmin 1 nmax n)
  7622. (org-back-to-heading t)
  7623. (setq beg (point))
  7624. (setq idprop (org-entry-get nil "ID"))
  7625. (org-end-of-subtree t t)
  7626. (or (bolp) (insert "\n"))
  7627. (setq end (point))
  7628. (setq template (buffer-substring beg end))
  7629. (when (and doshift
  7630. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7631. (delete-region beg end)
  7632. (setq end beg)
  7633. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7634. (goto-char end)
  7635. (loop for n from nmin to nmax do
  7636. ;; prepare clone
  7637. (with-temp-buffer
  7638. (insert template)
  7639. (org-mode)
  7640. (goto-char (point-min))
  7641. (org-show-subtree)
  7642. (and idprop (if org-clone-delete-id
  7643. (org-entry-delete nil "ID")
  7644. (org-id-get-create t)))
  7645. (unless (= n 0)
  7646. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7647. (kill-whole-line))
  7648. (goto-char (point-min))
  7649. (while (re-search-forward drawer-re nil t)
  7650. (mapc (lambda (d)
  7651. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7652. (goto-char (point-min))
  7653. (when doshift
  7654. (while (re-search-forward org-ts-regexp-both nil t)
  7655. (org-timestamp-change (* n shift-n) shift-what))
  7656. (unless (= n n-no-remove)
  7657. (goto-char (point-min))
  7658. (while (re-search-forward org-ts-regexp nil t)
  7659. (save-excursion
  7660. (goto-char (match-beginning 0))
  7661. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7662. (delete-region (match-beginning 1) (match-end 1)))))))
  7663. (setq task (buffer-string)))
  7664. (insert task))
  7665. (goto-char beg)))
  7666. ;;; Outline Sorting
  7667. (defun org-sort (with-case)
  7668. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7669. Optional argument WITH-CASE means sort case-sensitively."
  7670. (interactive "P")
  7671. (cond
  7672. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7673. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7674. (t
  7675. (org-call-with-arg 'org-sort-entries with-case))))
  7676. (defun org-sort-remove-invisible (s)
  7677. "Remove invisible links from string S."
  7678. (remove-text-properties 0 (length s) org-rm-props s)
  7679. (while (string-match org-bracket-link-regexp s)
  7680. (setq s (replace-match (if (match-end 2)
  7681. (match-string 3 s)
  7682. (match-string 1 s)) t t s)))
  7683. (let ((st (format " %s " s)))
  7684. (while (string-match org-emph-re st)
  7685. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7686. (setq s (substring st 1 -1)))
  7687. s)
  7688. (defvar org-priority-regexp) ; defined later in the file
  7689. (defvar org-after-sorting-entries-or-items-hook nil
  7690. "Hook that is run after a bunch of entries or items have been sorted.
  7691. When children are sorted, the cursor is in the parent line when this
  7692. hook gets called. When a region or a plain list is sorted, the cursor
  7693. will be in the first entry of the sorted region/list.")
  7694. (defun org-sort-entries
  7695. (&optional with-case sorting-type getkey-func compare-func property)
  7696. "Sort entries on a certain level of an outline tree.
  7697. If there is an active region, the entries in the region are sorted.
  7698. Else, if the cursor is before the first entry, sort the top-level items.
  7699. Else, the children of the entry at point are sorted.
  7700. Sorting can be alphabetically, numerically, by date/time as given by
  7701. a time stamp, by a property, by priority order, or by a custom function.
  7702. The command prompts for the sorting type unless it has been given to the
  7703. function through the SORTING-TYPE argument, which needs to be a character,
  7704. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7705. precise meaning of each character:
  7706. n Numerically, by converting the beginning of the entry/item to a number.
  7707. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7708. o By order of TODO keywords.
  7709. t By date/time, either the first active time stamp in the entry, or, if
  7710. none exist, by the first inactive one.
  7711. s By the scheduled date/time.
  7712. d By deadline date/time.
  7713. c By creation time, which is assumed to be the first inactive time stamp
  7714. at the beginning of a line.
  7715. p By priority according to the cookie.
  7716. r By the value of a property.
  7717. Capital letters will reverse the sort order.
  7718. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7719. called with point at the beginning of the record. It must return either
  7720. a string or a number that should serve as the sorting key for that record.
  7721. Comparing entries ignores case by default. However, with an optional argument
  7722. WITH-CASE, the sorting considers case as well.
  7723. Sorting is done against the visible part of the headlines, it ignores hidden
  7724. links."
  7725. (interactive "P")
  7726. (let ((case-func (if with-case 'identity 'downcase))
  7727. (cmstr
  7728. ;; The clock marker is lost when using `sort-subr', let's
  7729. ;; store the clocking string.
  7730. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7731. (save-excursion
  7732. (goto-char org-clock-marker)
  7733. (looking-back "^.*") (match-string-no-properties 0))))
  7734. start beg end stars re re2
  7735. txt what tmp)
  7736. ;; Find beginning and end of region to sort
  7737. (cond
  7738. ((org-region-active-p)
  7739. ;; we will sort the region
  7740. (setq end (region-end)
  7741. what "region")
  7742. (goto-char (region-beginning))
  7743. (if (not (org-at-heading-p)) (outline-next-heading))
  7744. (setq start (point)))
  7745. ((or (org-at-heading-p)
  7746. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7747. ;; we will sort the children of the current headline
  7748. (org-back-to-heading)
  7749. (setq start (point)
  7750. end (progn (org-end-of-subtree t t)
  7751. (or (bolp) (insert "\n"))
  7752. (org-back-over-empty-lines)
  7753. (point))
  7754. what "children")
  7755. (goto-char start)
  7756. (show-subtree)
  7757. (outline-next-heading))
  7758. (t
  7759. ;; we will sort the top-level entries in this file
  7760. (goto-char (point-min))
  7761. (or (org-at-heading-p) (outline-next-heading))
  7762. (setq start (point))
  7763. (goto-char (point-max))
  7764. (beginning-of-line 1)
  7765. (when (looking-at ".*?\\S-")
  7766. ;; File ends in a non-white line
  7767. (end-of-line 1)
  7768. (insert "\n"))
  7769. (setq end (point-max))
  7770. (setq what "top-level")
  7771. (goto-char start)
  7772. (show-all)))
  7773. (setq beg (point))
  7774. (if (>= beg end) (user-error "Nothing to sort"))
  7775. (looking-at "\\(\\*+\\)")
  7776. (setq stars (match-string 1)
  7777. re (concat "^" (regexp-quote stars) " +")
  7778. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7779. txt (buffer-substring beg end))
  7780. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7781. (if (and (not (equal stars "*")) (string-match re2 txt))
  7782. (user-error "Region to sort contains a level above the first entry"))
  7783. (unless sorting-type
  7784. (message
  7785. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7786. [t]ime [s]cheduled [d]eadline [c]reated
  7787. A/N/P/R/O/F/T/S/D/C means reversed:"
  7788. what)
  7789. (setq sorting-type (read-char-exclusive))
  7790. (unless getkey-func
  7791. (and (= (downcase sorting-type) ?f)
  7792. (setq getkey-func
  7793. (org-icompleting-read "Sort using function: "
  7794. obarray 'fboundp t nil nil))
  7795. (setq getkey-func (intern getkey-func))))
  7796. (and (= (downcase sorting-type) ?r)
  7797. (not property)
  7798. (setq property
  7799. (org-icompleting-read "Property: "
  7800. (mapcar 'list (org-buffer-property-keys t))
  7801. nil t))))
  7802. (message "Sorting entries...")
  7803. (save-restriction
  7804. (narrow-to-region start end)
  7805. (let ((dcst (downcase sorting-type))
  7806. (case-fold-search nil)
  7807. (now (current-time)))
  7808. (sort-subr
  7809. (/= dcst sorting-type)
  7810. ;; This function moves to the beginning character of the "record" to
  7811. ;; be sorted.
  7812. (lambda nil
  7813. (if (re-search-forward re nil t)
  7814. (goto-char (match-beginning 0))
  7815. (goto-char (point-max))))
  7816. ;; This function moves to the last character of the "record" being
  7817. ;; sorted.
  7818. (lambda nil
  7819. (save-match-data
  7820. (condition-case nil
  7821. (outline-forward-same-level 1)
  7822. (error
  7823. (goto-char (point-max))))))
  7824. ;; This function returns the value that gets sorted against.
  7825. (lambda nil
  7826. (cond
  7827. ((= dcst ?n)
  7828. (if (looking-at org-complex-heading-regexp)
  7829. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7830. nil))
  7831. ((= dcst ?a)
  7832. (if (looking-at org-complex-heading-regexp)
  7833. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7834. nil))
  7835. ((= dcst ?t)
  7836. (let ((end (save-excursion (outline-next-heading) (point))))
  7837. (if (or (re-search-forward org-ts-regexp end t)
  7838. (re-search-forward org-ts-regexp-both end t))
  7839. (org-time-string-to-seconds (match-string 0))
  7840. (org-float-time now))))
  7841. ((= dcst ?c)
  7842. (let ((end (save-excursion (outline-next-heading) (point))))
  7843. (if (re-search-forward
  7844. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7845. end t)
  7846. (org-time-string-to-seconds (match-string 0))
  7847. (org-float-time now))))
  7848. ((= dcst ?s)
  7849. (let ((end (save-excursion (outline-next-heading) (point))))
  7850. (if (re-search-forward org-scheduled-time-regexp end t)
  7851. (org-time-string-to-seconds (match-string 1))
  7852. (org-float-time now))))
  7853. ((= dcst ?d)
  7854. (let ((end (save-excursion (outline-next-heading) (point))))
  7855. (if (re-search-forward org-deadline-time-regexp end t)
  7856. (org-time-string-to-seconds (match-string 1))
  7857. (org-float-time now))))
  7858. ((= dcst ?p)
  7859. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7860. (string-to-char (match-string 2))
  7861. org-default-priority))
  7862. ((= dcst ?r)
  7863. (or (org-entry-get nil property) ""))
  7864. ((= dcst ?o)
  7865. (if (looking-at org-complex-heading-regexp)
  7866. (- 9999 (length (member (match-string 2)
  7867. org-todo-keywords-1)))))
  7868. ((= dcst ?f)
  7869. (if getkey-func
  7870. (progn
  7871. (setq tmp (funcall getkey-func))
  7872. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7873. tmp)
  7874. (error "Invalid key function `%s'" getkey-func)))
  7875. (t (error "Invalid sorting type `%c'" sorting-type))))
  7876. nil
  7877. (cond
  7878. ((= dcst ?a) 'string<)
  7879. ((= dcst ?f) compare-func)
  7880. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7881. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7882. ;; Reset the clock marker if needed
  7883. (when cmstr
  7884. (save-excursion
  7885. (goto-char start)
  7886. (search-forward cmstr nil t)
  7887. (move-marker org-clock-marker (point))))
  7888. (message "Sorting entries...done")))
  7889. (defun org-do-sort (table what &optional with-case sorting-type)
  7890. "Sort TABLE of WHAT according to SORTING-TYPE.
  7891. The user will be prompted for the SORTING-TYPE if the call to this
  7892. function does not specify it. WHAT is only for the prompt, to indicate
  7893. what is being sorted. The sorting key will be extracted from
  7894. the car of the elements of the table.
  7895. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7896. (unless sorting-type
  7897. (message
  7898. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7899. what)
  7900. (setq sorting-type (read-char-exclusive)))
  7901. (let ((dcst (downcase sorting-type))
  7902. extractfun comparefun)
  7903. ;; Define the appropriate functions
  7904. (cond
  7905. ((= dcst ?n)
  7906. (setq extractfun 'string-to-number
  7907. comparefun (if (= dcst sorting-type) '< '>)))
  7908. ((= dcst ?a)
  7909. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7910. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7911. comparefun (if (= dcst sorting-type)
  7912. 'string<
  7913. (lambda (a b) (and (not (string< a b))
  7914. (not (string= a b)))))))
  7915. ((= dcst ?t)
  7916. (setq extractfun
  7917. (lambda (x)
  7918. (if (or (string-match org-ts-regexp x)
  7919. (string-match org-ts-regexp-both x))
  7920. (org-float-time
  7921. (org-time-string-to-time (match-string 0 x)))
  7922. 0))
  7923. comparefun (if (= dcst sorting-type) '< '>)))
  7924. (t (error "Invalid sorting type `%c'" sorting-type)))
  7925. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7926. table)
  7927. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7928. ;;; The orgstruct minor mode
  7929. ;; Define a minor mode which can be used in other modes in order to
  7930. ;; integrate the org-mode structure editing commands.
  7931. ;; This is really a hack, because the org-mode structure commands use
  7932. ;; keys which normally belong to the major mode. Here is how it
  7933. ;; works: The minor mode defines all the keys necessary to operate the
  7934. ;; structure commands, but wraps the commands into a function which
  7935. ;; tests if the cursor is currently at a headline or a plain list
  7936. ;; item. If that is the case, the structure command is used,
  7937. ;; temporarily setting many Org-mode variables like regular
  7938. ;; expressions for filling etc. However, when any of those keys is
  7939. ;; used at a different location, function uses `key-binding' to look
  7940. ;; up if the key has an associated command in another currently active
  7941. ;; keymap (minor modes, major mode, global), and executes that
  7942. ;; command. There might be problems if any of the keys is otherwise
  7943. ;; used as a prefix key.
  7944. (defcustom orgstruct-heading-prefix-regexp nil
  7945. "Regexp that matches the custom prefix of Org headlines in
  7946. orgstruct(++)-mode."
  7947. :group 'org
  7948. :version "24.4"
  7949. :package-version '(Org . "8.0")
  7950. :type 'string)
  7951. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7952. (defcustom orgstruct-setup-hook nil
  7953. "Hook run after orgstruct-mode-map is filled."
  7954. :group 'org
  7955. :version "24.4"
  7956. :package-version '(Org . "8.0")
  7957. :type 'hook)
  7958. (defvar orgstruct-initialized nil)
  7959. (defvar org-local-vars nil
  7960. "List of local variables, for use by `orgstruct-mode'.")
  7961. ;;;###autoload
  7962. (define-minor-mode orgstruct-mode
  7963. "Toggle the minor mode `orgstruct-mode'.
  7964. This mode is for using Org-mode structure commands in other
  7965. modes. The following keys behave as if Org-mode were active, if
  7966. the cursor is on a headline, or on a plain list item (both as
  7967. defined by Org-mode)."
  7968. nil " OrgStruct" (make-sparse-keymap)
  7969. (funcall (if orgstruct-mode
  7970. 'add-to-invisibility-spec
  7971. 'remove-from-invisibility-spec)
  7972. '(outline . t))
  7973. (when orgstruct-mode
  7974. (org-load-modules-maybe)
  7975. (unless orgstruct-initialized
  7976. (orgstruct-setup)
  7977. (setq orgstruct-initialized t))))
  7978. ;;;###autoload
  7979. (defun turn-on-orgstruct ()
  7980. "Unconditionally turn on `orgstruct-mode'."
  7981. (orgstruct-mode 1))
  7982. (defvar org-fb-vars nil)
  7983. (make-variable-buffer-local 'org-fb-vars)
  7984. (defun orgstruct++-mode (&optional arg)
  7985. "Toggle `orgstruct-mode', the enhanced version of it.
  7986. In addition to setting orgstruct-mode, this also exports all
  7987. indentation and autofilling variables from org-mode into the
  7988. buffer. It will also recognize item context in multiline items."
  7989. (interactive "P")
  7990. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7991. (if (< arg 1)
  7992. (progn (orgstruct-mode -1)
  7993. (mapc (lambda(v)
  7994. (org-set-local (car v)
  7995. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7996. org-fb-vars))
  7997. (orgstruct-mode 1)
  7998. (setq org-fb-vars nil)
  7999. (let (var val)
  8000. (mapc
  8001. (lambda (x)
  8002. (when (string-match
  8003. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8004. (symbol-name (car x)))
  8005. (setq var (car x) val (nth 1 x))
  8006. (push (list var `(quote ,(eval var))) org-fb-vars)
  8007. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8008. org-local-vars)
  8009. (org-set-local 'orgstruct-is-++ t))))
  8010. (defvar orgstruct-is-++ nil
  8011. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8012. (make-variable-buffer-local 'orgstruct-is-++)
  8013. ;;;###autoload
  8014. (defun turn-on-orgstruct++ ()
  8015. "Unconditionally turn on `orgstruct++-mode'."
  8016. (orgstruct++-mode 1))
  8017. (defun orgstruct-error ()
  8018. "Error when there is no default binding for a structure key."
  8019. (interactive)
  8020. (funcall (if (fboundp 'user-error)
  8021. 'user-error
  8022. 'error)
  8023. "This key has no function outside structure elements"))
  8024. (defun orgstruct-setup ()
  8025. "Setup orgstruct keymap."
  8026. (dolist (cell '((org-demote . t)
  8027. (org-metaleft . t)
  8028. (org-metaright . t)
  8029. (org-promote . t)
  8030. (org-shiftmetaleft . t)
  8031. (org-shiftmetaright . t)
  8032. org-backward-element
  8033. org-backward-heading-same-level
  8034. org-ctrl-c-ret
  8035. org-ctrl-c-minus
  8036. org-ctrl-c-star
  8037. org-cycle
  8038. org-forward-heading-same-level
  8039. org-insert-heading
  8040. org-insert-heading-respect-content
  8041. org-kill-note-or-show-branches
  8042. org-mark-subtree
  8043. org-meta-return
  8044. org-metadown
  8045. org-metaup
  8046. org-narrow-to-subtree
  8047. org-promote-subtree
  8048. org-reveal
  8049. org-shiftdown
  8050. org-shiftleft
  8051. org-shiftmetadown
  8052. org-shiftmetaup
  8053. org-shiftright
  8054. org-shifttab
  8055. org-shifttab
  8056. org-shiftup
  8057. org-show-subtree
  8058. org-sort
  8059. org-up-element
  8060. outline-demote
  8061. outline-next-visible-heading
  8062. outline-previous-visible-heading
  8063. outline-promote
  8064. outline-up-heading
  8065. show-children))
  8066. (let ((f (or (car-safe cell) cell))
  8067. (disable-when-heading-prefix (cdr-safe cell)))
  8068. (when (fboundp f)
  8069. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8070. (where-is-internal f outline-mode-map)))
  8071. ;; TODO use local-function-key-map
  8072. (dolist (rep '(("<tab>" . "TAB")
  8073. ("<return>" . "RET")
  8074. ("<escape>" . "ESC")
  8075. ("<delete>" . "DEL")))
  8076. (setq binding (read-kbd-macro
  8077. (let ((case-fold-search))
  8078. (replace-regexp-in-string
  8079. (regexp-quote (cdr rep))
  8080. (car rep)
  8081. (key-description binding))))))
  8082. (let ((key (lookup-key orgstruct-mode-map binding)))
  8083. (when (or (not key) (numberp key))
  8084. (condition-case nil
  8085. (org-defkey orgstruct-mode-map
  8086. binding
  8087. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8088. (error nil))))))))
  8089. (run-hooks 'orgstruct-setup-hook))
  8090. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8091. "Create a function for binding in the structure minor mode.
  8092. FUN is the command to call inside a table. KEY is the key that
  8093. should be checked in for a command to execute outside of tables.
  8094. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8095. if `orgstruct-heading-prefix-regexp' is non-nil."
  8096. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8097. (let ((nname name)
  8098. (i 0))
  8099. (while (fboundp (intern nname))
  8100. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8101. (setq name (intern nname)))
  8102. (eval
  8103. (let ((bindings '((org-heading-regexp
  8104. (concat "^"
  8105. orgstruct-heading-prefix-regexp
  8106. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8107. (org-outline-regexp
  8108. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8109. (org-outline-regexp-bol
  8110. (concat "^" org-outline-regexp))
  8111. (outline-regexp org-outline-regexp)
  8112. (outline-heading-end-regexp "\n")
  8113. (outline-level 'org-outline-level)
  8114. (outline-heading-alist))))
  8115. `(defun ,name (arg)
  8116. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8117. "Outside of structure, run the binding of `"
  8118. (key-description key) "'."
  8119. (when disable-when-heading-prefix
  8120. (concat
  8121. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8122. "back to the default binding due to limitations of Org's implementation of\n"
  8123. "`" (symbol-name fun) "'.")))
  8124. (interactive "p")
  8125. (let* ((disable
  8126. ,(when disable-when-heading-prefix
  8127. '(and orgstruct-heading-prefix-regexp
  8128. (not (string= orgstruct-heading-prefix-regexp "")))))
  8129. (fallback
  8130. (or disable
  8131. (not
  8132. (let* ,bindings
  8133. (org-context-p 'headline 'item
  8134. ,(when (memq fun
  8135. '(org-insert-heading
  8136. org-insert-heading-respect-content
  8137. org-meta-return))
  8138. '(when orgstruct-is-++
  8139. 'item-body))))))))
  8140. (if fallback
  8141. (let* ((orgstruct-mode)
  8142. (binding
  8143. (loop with key = ,key
  8144. for rep in
  8145. '(nil
  8146. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8147. ("<\\([^>]*\\)return>" . "\\1RET")
  8148. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8149. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8150. do
  8151. (when rep
  8152. (setq key (read-kbd-macro
  8153. (let ((case-fold-search))
  8154. (replace-regexp-in-string
  8155. (car rep)
  8156. (cdr rep)
  8157. (key-description key))))))
  8158. thereis (key-binding key))))
  8159. (if (keymapp binding)
  8160. (set-temporary-overlay-map binding)
  8161. (let ((func (or binding
  8162. (unless disable
  8163. 'orgstruct-error))))
  8164. (when func
  8165. (call-interactively func)))))
  8166. (org-run-like-in-org-mode
  8167. (lambda ()
  8168. (interactive)
  8169. (let* ,bindings
  8170. (call-interactively ',fun)))))))))
  8171. name))
  8172. (defun org-contextualize-keys (alist contexts)
  8173. "Return valid elements in ALIST depending on CONTEXTS.
  8174. `org-agenda-custom-commands' or `org-capture-templates' are the
  8175. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8176. or `org-capture-templates-contexts' are the associated contexts
  8177. definitions."
  8178. (let ((contexts
  8179. ;; normalize contexts
  8180. (mapcar
  8181. (lambda(c) (cond ((listp (cadr c))
  8182. (list (car c) (car c) (cadr c)))
  8183. ((string= "" (cadr c))
  8184. (list (car c) (car c) (caddr c)))
  8185. (t c))) contexts))
  8186. (a alist) c r s)
  8187. ;; loop over all commands or templates
  8188. (while (setq c (pop a))
  8189. (let (vrules repl)
  8190. (cond
  8191. ((not (assoc (car c) contexts))
  8192. (push c r))
  8193. ((and (assoc (car c) contexts)
  8194. (setq vrules (org-contextualize-validate-key
  8195. (car c) contexts)))
  8196. (mapc (lambda (vr)
  8197. (when (not (equal (car vr) (cadr vr)))
  8198. (setq repl vr))) vrules)
  8199. (if (not repl) (push c r)
  8200. (push (cadr repl) s)
  8201. (push
  8202. (cons (car c)
  8203. (cdr (or (assoc (cadr repl) alist)
  8204. (error "Undefined key `%s' as contextual replacement for `%s'"
  8205. (cadr repl) (car c)))))
  8206. r))))))
  8207. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8208. (delq
  8209. nil
  8210. (delete-dups
  8211. (mapcar (lambda (x)
  8212. (let ((tpl (car x)))
  8213. (when (not (delq
  8214. nil
  8215. (mapcar (lambda(y)
  8216. (equal y tpl)) s))) x)))
  8217. (reverse r))))))
  8218. (defun org-contextualize-validate-key (key contexts)
  8219. "Check CONTEXTS for agenda or capture KEY."
  8220. (let (r rr res)
  8221. (while (setq r (pop contexts))
  8222. (mapc
  8223. (lambda (rr)
  8224. (when
  8225. (and (equal key (car r))
  8226. (if (functionp rr) (funcall rr)
  8227. (or (and (eq (car rr) 'in-file)
  8228. (buffer-file-name)
  8229. (string-match (cdr rr) (buffer-file-name)))
  8230. (and (eq (car rr) 'in-mode)
  8231. (string-match (cdr rr) (symbol-name major-mode)))
  8232. (and (eq (car rr) 'in-buffer)
  8233. (string-match (cdr rr) (buffer-name)))
  8234. (when (and (eq (car rr) 'not-in-file)
  8235. (buffer-file-name))
  8236. (not (string-match (cdr rr) (buffer-file-name))))
  8237. (when (eq (car rr) 'not-in-mode)
  8238. (not (string-match (cdr rr) (symbol-name major-mode))))
  8239. (when (eq (car rr) 'not-in-buffer)
  8240. (not (string-match (cdr rr) (buffer-name)))))))
  8241. (push r res)))
  8242. (car (last r))))
  8243. (delete-dups (delq nil res))))
  8244. (defun org-context-p (&rest contexts)
  8245. "Check if local context is any of CONTEXTS.
  8246. Possible values in the list of contexts are `table', `headline', and `item'."
  8247. (let ((pos (point)))
  8248. (goto-char (point-at-bol))
  8249. (prog1 (or (and (memq 'table contexts)
  8250. (looking-at "[ \t]*|"))
  8251. (and (memq 'headline contexts)
  8252. (looking-at org-outline-regexp))
  8253. (and (memq 'item contexts)
  8254. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8255. (and (memq 'item-body contexts)
  8256. (org-in-item-p)))
  8257. (goto-char pos))))
  8258. (defun org-get-local-variables ()
  8259. "Return a list of all local variables in an Org mode buffer."
  8260. (let (varlist)
  8261. (with-current-buffer (get-buffer-create "*Org tmp*")
  8262. (erase-buffer)
  8263. (org-mode)
  8264. (setq varlist (buffer-local-variables)))
  8265. (kill-buffer "*Org tmp*")
  8266. (delq nil
  8267. (mapcar
  8268. (lambda (x)
  8269. (setq x
  8270. (if (symbolp x)
  8271. (list x)
  8272. (list (car x) (cdr x))))
  8273. (if (and (not (get (car x) 'org-state))
  8274. (string-match
  8275. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8276. (symbol-name (car x))))
  8277. x nil))
  8278. varlist))))
  8279. (defun org-clone-local-variables (from-buffer &optional regexp)
  8280. "Clone local variables from FROM-BUFFER.
  8281. Optional argument REGEXP selects variables to clone."
  8282. (mapc
  8283. (lambda (pair)
  8284. (and (symbolp (car pair))
  8285. (or (null regexp)
  8286. (string-match regexp (symbol-name (car pair))))
  8287. (set (make-local-variable (car pair))
  8288. (cdr pair))))
  8289. (buffer-local-variables from-buffer)))
  8290. ;;;###autoload
  8291. (defun org-run-like-in-org-mode (cmd)
  8292. "Run a command, pretending that the current buffer is in Org-mode.
  8293. This will temporarily bind local variables that are typically bound in
  8294. Org-mode to the values they have in Org-mode, and then interactively
  8295. call CMD."
  8296. (org-load-modules-maybe)
  8297. (unless org-local-vars
  8298. (setq org-local-vars (org-get-local-variables)))
  8299. (let (binds)
  8300. (dolist (var org-local-vars)
  8301. (when (or (not (boundp (car var)))
  8302. (eq (symbol-value (car var))
  8303. (default-value (car var))))
  8304. (push (list (car var) `(quote ,(cadr var))) binds)))
  8305. (eval `(let ,binds
  8306. (call-interactively (quote ,cmd))))))
  8307. ;;;; Archiving
  8308. (defun org-get-category (&optional pos force-refresh)
  8309. "Get the category applying to position POS."
  8310. (save-match-data
  8311. (if force-refresh (org-refresh-category-properties))
  8312. (let ((pos (or pos (point))))
  8313. (or (get-text-property pos 'org-category)
  8314. (progn (org-refresh-category-properties)
  8315. (get-text-property pos 'org-category))))))
  8316. (defun org-refresh-category-properties ()
  8317. "Refresh category text properties in the buffer."
  8318. (let ((case-fold-search t)
  8319. (inhibit-read-only t)
  8320. (def-cat (cond
  8321. ((null org-category)
  8322. (if buffer-file-name
  8323. (file-name-sans-extension
  8324. (file-name-nondirectory buffer-file-name))
  8325. "???"))
  8326. ((symbolp org-category) (symbol-name org-category))
  8327. (t org-category)))
  8328. beg end cat pos optionp)
  8329. (org-with-silent-modifications
  8330. (save-excursion
  8331. (save-restriction
  8332. (widen)
  8333. (goto-char (point-min))
  8334. (put-text-property (point) (point-max) 'org-category def-cat)
  8335. (while (re-search-forward
  8336. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8337. (setq pos (match-end 0)
  8338. optionp (equal (char-after (match-beginning 0)) ?#)
  8339. cat (org-trim (match-string 2)))
  8340. (if optionp
  8341. (setq beg (point-at-bol) end (point-max))
  8342. (org-back-to-heading t)
  8343. (setq beg (point) end (org-end-of-subtree t t)))
  8344. (put-text-property beg end 'org-category cat)
  8345. (put-text-property beg end 'org-category-position beg)
  8346. (goto-char pos)))))))
  8347. (defun org-refresh-properties (dprop tprop)
  8348. "Refresh buffer text properties.
  8349. DPROP is the drawer property and TPROP is the corresponding text
  8350. property to set."
  8351. (let ((case-fold-search t)
  8352. (inhibit-read-only t) p)
  8353. (org-with-silent-modifications
  8354. (save-excursion
  8355. (save-restriction
  8356. (widen)
  8357. (goto-char (point-min))
  8358. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8359. (setq p (org-match-string-no-properties 1))
  8360. (save-excursion
  8361. (org-back-to-heading t)
  8362. (put-text-property
  8363. (point-at-bol) (point-at-eol) tprop p))))))))
  8364. ;;;; Link Stuff
  8365. ;;; Link abbreviations
  8366. (defun org-link-expand-abbrev (link)
  8367. "Apply replacements as defined in `org-link-abbrev-alist'."
  8368. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8369. (let* ((key (match-string 1 link))
  8370. (as (or (assoc key org-link-abbrev-alist-local)
  8371. (assoc key org-link-abbrev-alist)))
  8372. (tag (and (match-end 2) (match-string 3 link)))
  8373. rpl)
  8374. (if (not as)
  8375. link
  8376. (setq rpl (cdr as))
  8377. (cond
  8378. ((symbolp rpl) (funcall rpl tag))
  8379. ((string-match "%(\\([^)]+\\))" rpl)
  8380. (replace-match
  8381. (save-match-data
  8382. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8383. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8384. ((string-match "%h" rpl)
  8385. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8386. (t (concat rpl tag)))))
  8387. link))
  8388. ;;; Storing and inserting links
  8389. (defvar org-insert-link-history nil
  8390. "Minibuffer history for links inserted with `org-insert-link'.")
  8391. (defvar org-stored-links nil
  8392. "Contains the links stored with `org-store-link'.")
  8393. (defvar org-store-link-plist nil
  8394. "Plist with info about the most recently link created with `org-store-link'.")
  8395. (defvar org-link-protocols nil
  8396. "Link protocols added to Org-mode using `org-add-link-type'.")
  8397. (defvar org-store-link-functions nil
  8398. "List of functions that are called to create and store a link.
  8399. Each function will be called in turn until one returns a non-nil
  8400. value. Each function should check if it is responsible for creating
  8401. this link (for example by looking at the major mode).
  8402. If not, it must exit and return nil.
  8403. If yes, it should return a non-nil value after a calling
  8404. `org-store-link-props' with a list of properties and values.
  8405. Special properties are:
  8406. :type The link prefix, like \"http\". This must be given.
  8407. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8408. This is obligatory as well.
  8409. :description Optional default description for the second pair
  8410. of brackets in an Org-mode link. The user can still change
  8411. this when inserting this link into an Org-mode buffer.
  8412. In addition to these, any additional properties can be specified
  8413. and then used in capture templates.")
  8414. (defun org-add-link-type (type &optional follow export)
  8415. "Add TYPE to the list of `org-link-types'.
  8416. Re-compute all regular expressions depending on `org-link-types'
  8417. FOLLOW and EXPORT are two functions.
  8418. FOLLOW should take the link path as the single argument and do whatever
  8419. is necessary to follow the link, for example find a file or display
  8420. a mail message.
  8421. EXPORT should format the link path for export to one of the export formats.
  8422. It should be a function accepting three arguments:
  8423. path the path of the link, the text after the prefix (like \"http:\")
  8424. desc the description of the link, if any, or a description added by
  8425. org-export-normalize-links if there is none
  8426. format the export format, a symbol like `html' or `latex' or `ascii'..
  8427. The function may use the FORMAT information to return different values
  8428. depending on the format. The return value will be put literally into
  8429. the exported file. If the return value is nil, this means Org should
  8430. do what it normally does with links which do not have EXPORT defined.
  8431. Org-mode has a built-in default for exporting links. If you are happy with
  8432. this default, there is no need to define an export function for the link
  8433. type. For a simple example of an export function, see `org-bbdb.el'."
  8434. (add-to-list 'org-link-types type t)
  8435. (org-make-link-regexps)
  8436. (if (assoc type org-link-protocols)
  8437. (setcdr (assoc type org-link-protocols) (list follow export))
  8438. (push (list type follow export) org-link-protocols)))
  8439. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8440. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8441. ;;;###autoload
  8442. (defun org-store-link (arg)
  8443. "\\<org-mode-map>Store an org-link to the current location.
  8444. This link is added to `org-stored-links' and can later be inserted
  8445. into an org-buffer with \\[org-insert-link].
  8446. For some link types, a prefix arg is interpreted.
  8447. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8448. For file links, arg negates `org-context-in-file-links'.
  8449. A double prefix arg force skipping storing functions that are not
  8450. part of Org's core.
  8451. A triple prefix arg force storing a link for each line in the
  8452. active region."
  8453. (interactive "P")
  8454. (org-load-modules-maybe)
  8455. (if (and (equal arg '(64)) (org-region-active-p))
  8456. (save-excursion
  8457. (let ((end (region-end)))
  8458. (goto-char (region-beginning))
  8459. (set-mark (point))
  8460. (while (< (point-at-eol) end)
  8461. (move-end-of-line 1) (activate-mark)
  8462. (let (current-prefix-arg)
  8463. (call-interactively 'org-store-link))
  8464. (move-beginning-of-line 2)
  8465. (set-mark (point)))))
  8466. (org-with-limited-levels
  8467. (setq org-store-link-plist nil)
  8468. (let (link cpltxt desc description search
  8469. txt custom-id agenda-link sfuns sfunsn)
  8470. (cond
  8471. ;; Store a link using an external link type
  8472. ((and (not (equal arg '(16)))
  8473. (setq sfuns
  8474. (delq
  8475. nil (mapcar (lambda (f)
  8476. (let (fs) (if (funcall f) (push f fs))))
  8477. org-store-link-functions))
  8478. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8479. (or (and (cdr sfuns)
  8480. (funcall (intern
  8481. (completing-read
  8482. "Which function for creating the link? "
  8483. sfunsn t (car sfunsn)))))
  8484. (funcall (caar sfuns)))
  8485. (setq link (plist-get org-store-link-plist :link)
  8486. desc (or (plist-get org-store-link-plist
  8487. :description) link))))
  8488. ;; Store a link from a source code buffer
  8489. ((org-src-edit-buffer-p)
  8490. (let (label gc)
  8491. (while (or (not label)
  8492. (save-excursion
  8493. (save-restriction
  8494. (widen)
  8495. (goto-char (point-min))
  8496. (re-search-forward
  8497. (regexp-quote (format org-coderef-label-format label))
  8498. nil t))))
  8499. (when label (message "Label exists already") (sit-for 2))
  8500. (setq label (read-string "Code line label: " label)))
  8501. (end-of-line 1)
  8502. (setq link (format org-coderef-label-format label))
  8503. (setq gc (- 79 (length link)))
  8504. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8505. (insert link)
  8506. (setq link (concat "(" label ")") desc nil)))
  8507. ;; We are in the agenda, link to referenced location
  8508. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8509. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8510. (get-text-property (point) 'org-marker))))
  8511. (when m
  8512. (org-with-point-at m
  8513. (setq agenda-link
  8514. (if (org-called-interactively-p 'any)
  8515. (call-interactively 'org-store-link)
  8516. (org-store-link nil)))))))
  8517. ((eq major-mode 'calendar-mode)
  8518. (let ((cd (calendar-cursor-to-date)))
  8519. (setq link
  8520. (format-time-string
  8521. (car org-time-stamp-formats)
  8522. (apply 'encode-time
  8523. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8524. nil nil nil))))
  8525. (org-store-link-props :type "calendar" :date cd)))
  8526. ((eq major-mode 'help-mode)
  8527. (setq link (concat "help:" (save-excursion
  8528. (goto-char (point-min))
  8529. (looking-at "^[^ ]+")
  8530. (match-string 0))))
  8531. (org-store-link-props :type "help"))
  8532. ((eq major-mode 'w3-mode)
  8533. (setq cpltxt (if (and (buffer-name)
  8534. (not (string-match "Untitled" (buffer-name))))
  8535. (buffer-name)
  8536. (url-view-url t))
  8537. link (url-view-url t))
  8538. (org-store-link-props :type "w3" :url (url-view-url t)))
  8539. ((eq major-mode 'image-mode)
  8540. (setq cpltxt (concat "file:"
  8541. (abbreviate-file-name buffer-file-name))
  8542. link cpltxt)
  8543. (org-store-link-props :type "image" :file buffer-file-name))
  8544. ;; In dired, store a link to the file of the current line
  8545. ((eq major-mode 'dired-mode)
  8546. (let ((file (dired-get-filename nil t)))
  8547. (setq file (if file
  8548. (abbreviate-file-name
  8549. (expand-file-name (dired-get-filename nil t)))
  8550. ;; otherwise, no file so use current directory.
  8551. default-directory))
  8552. (setq cpltxt (concat "file:" file)
  8553. link cpltxt)))
  8554. ((setq search (run-hook-with-args-until-success
  8555. 'org-create-file-search-functions))
  8556. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8557. "::" search))
  8558. (setq cpltxt (or description link)))
  8559. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8560. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8561. (cond
  8562. ;; Store a link using the target at point
  8563. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8564. (setq cpltxt
  8565. (concat "file:"
  8566. (abbreviate-file-name
  8567. (buffer-file-name (buffer-base-buffer)))
  8568. "::" (match-string 1))
  8569. link cpltxt))
  8570. ((and (featurep 'org-id)
  8571. (or (eq org-id-link-to-org-use-id t)
  8572. (and (org-called-interactively-p 'any)
  8573. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8574. (and (eq org-id-link-to-org-use-id
  8575. 'create-if-interactive-and-no-custom-id)
  8576. (not custom-id))))
  8577. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8578. ;; Store a link using the ID at point
  8579. (setq link (condition-case nil
  8580. (prog1 (org-id-store-link)
  8581. (setq desc (plist-get org-store-link-plist
  8582. :description)))
  8583. (error
  8584. ;; Probably before first headline, link only to file
  8585. (concat "file:"
  8586. (abbreviate-file-name
  8587. (buffer-file-name (buffer-base-buffer))))))))
  8588. (t
  8589. ;; Just link to current headline
  8590. (setq cpltxt (concat "file:"
  8591. (abbreviate-file-name
  8592. (buffer-file-name (buffer-base-buffer)))))
  8593. ;; Add a context search string
  8594. (when (org-xor org-context-in-file-links arg)
  8595. (let* ((ee (org-element-at-point))
  8596. (et (org-element-type ee))
  8597. (ev (plist-get (cadr ee) :value))
  8598. (ek (plist-get (cadr ee) :key))
  8599. (eok (and (stringp ek) (string-match "name" ek))))
  8600. (setq txt (cond
  8601. ((org-at-heading-p) nil)
  8602. ((and (eq et 'keyword) eok) ev)
  8603. ((org-region-active-p)
  8604. (buffer-substring (region-beginning) (region-end)))))
  8605. (when (or (null txt) (string-match "\\S-" txt))
  8606. (setq cpltxt
  8607. (concat cpltxt "::"
  8608. (condition-case nil
  8609. (org-make-org-heading-search-string txt)
  8610. (error "")))
  8611. desc (or (and (eq et 'keyword) eok ev)
  8612. (nth 4 (ignore-errors (org-heading-components)))
  8613. "NONE")))))
  8614. (if (string-match "::\\'" cpltxt)
  8615. (setq cpltxt (substring cpltxt 0 -2)))
  8616. (setq link cpltxt))))
  8617. ((buffer-file-name (buffer-base-buffer))
  8618. ;; Just link to this file here.
  8619. (setq cpltxt (concat "file:"
  8620. (abbreviate-file-name
  8621. (buffer-file-name (buffer-base-buffer)))))
  8622. ;; Add a context string.
  8623. (when (org-xor org-context-in-file-links arg)
  8624. (setq txt (if (org-region-active-p)
  8625. (buffer-substring (region-beginning) (region-end))
  8626. (buffer-substring (point-at-bol) (point-at-eol))))
  8627. ;; Only use search option if there is some text.
  8628. (when (string-match "\\S-" txt)
  8629. (setq cpltxt
  8630. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8631. desc "NONE")))
  8632. (setq link cpltxt))
  8633. ((org-called-interactively-p 'interactive)
  8634. (user-error "No method for storing a link from this buffer"))
  8635. (t (setq link nil)))
  8636. ;; We're done setting link and desc, clean up
  8637. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8638. (setq link (or link cpltxt)
  8639. desc (or desc cpltxt))
  8640. (cond ((equal desc "NONE") (setq desc nil))
  8641. ((string-match org-bracket-link-analytic-regexp desc)
  8642. (let ((d0 (match-string 3 desc))
  8643. (p0 (match-string 5 desc)))
  8644. (setq desc
  8645. (replace-regexp-in-string
  8646. org-bracket-link-regexp
  8647. (concat (or p0 d0)
  8648. (if (equal (length (match-string 0 desc))
  8649. (length desc)) "*" "")) desc)))))
  8650. ;; Return the link
  8651. (if (not (and (or (org-called-interactively-p 'any)
  8652. executing-kbd-macro) link))
  8653. (or agenda-link (and link (org-make-link-string link desc)))
  8654. (push (list link desc) org-stored-links)
  8655. (message "Stored: %s" (or desc link))
  8656. (when custom-id
  8657. (setq link (concat "file:" (abbreviate-file-name
  8658. (buffer-file-name)) "::#" custom-id))
  8659. (push (list link desc) org-stored-links)))))))
  8660. (defun org-store-link-props (&rest plist)
  8661. "Store link properties, extract names and addresses."
  8662. (let (x adr)
  8663. (when (setq x (plist-get plist :from))
  8664. (setq adr (mail-extract-address-components x))
  8665. (setq plist (plist-put plist :fromname (car adr)))
  8666. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8667. (when (setq x (plist-get plist :to))
  8668. (setq adr (mail-extract-address-components x))
  8669. (setq plist (plist-put plist :toname (car adr)))
  8670. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8671. (let ((from (plist-get plist :from))
  8672. (to (plist-get plist :to)))
  8673. (when (and from to org-from-is-user-regexp)
  8674. (setq plist
  8675. (plist-put plist :fromto
  8676. (if (string-match org-from-is-user-regexp from)
  8677. (concat "to %t")
  8678. (concat "from %f"))))))
  8679. (setq org-store-link-plist plist))
  8680. (defun org-add-link-props (&rest plist)
  8681. "Add these properties to the link property list."
  8682. (let (key value)
  8683. (while plist
  8684. (setq key (pop plist) value (pop plist))
  8685. (setq org-store-link-plist
  8686. (plist-put org-store-link-plist key value)))))
  8687. (defun org-email-link-description (&optional fmt)
  8688. "Return the description part of an email link.
  8689. This takes information from `org-store-link-plist' and formats it
  8690. according to FMT (default from `org-email-link-description-format')."
  8691. (setq fmt (or fmt org-email-link-description-format))
  8692. (let* ((p org-store-link-plist)
  8693. (to (plist-get p :toaddress))
  8694. (from (plist-get p :fromaddress))
  8695. (table
  8696. (list
  8697. (cons "%c" (plist-get p :fromto))
  8698. (cons "%F" (plist-get p :from))
  8699. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8700. (cons "%T" (plist-get p :to))
  8701. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8702. (cons "%s" (plist-get p :subject))
  8703. (cons "%d" (plist-get p :date))
  8704. (cons "%m" (plist-get p :message-id)))))
  8705. (when (string-match "%c" fmt)
  8706. ;; Check if the user wrote this message
  8707. (if (and org-from-is-user-regexp from to
  8708. (save-match-data (string-match org-from-is-user-regexp from)))
  8709. (setq fmt (replace-match "to %t" t t fmt))
  8710. (setq fmt (replace-match "from %f" t t fmt))))
  8711. (org-replace-escapes fmt table)))
  8712. (defun org-make-org-heading-search-string (&optional string)
  8713. "Make search string for the current headline or STRING."
  8714. (let ((s (or string
  8715. (and (derived-mode-p 'org-mode)
  8716. (save-excursion
  8717. (org-back-to-heading t)
  8718. (org-element-property :raw-value (org-element-at-point))))))
  8719. (lines org-context-in-file-links))
  8720. (or string (setq s (concat "*" s))) ; Add * for headlines
  8721. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8722. (when (and string (integerp lines) (> lines 0))
  8723. (let ((slines (org-split-string s "\n")))
  8724. (when (< lines (length slines))
  8725. (setq s (mapconcat
  8726. 'identity
  8727. (reverse (nthcdr (- (length slines) lines)
  8728. (reverse slines))) "\n")))))
  8729. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8730. (defun org-make-link-string (link &optional description)
  8731. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8732. (unless (string-match "\\S-" link)
  8733. (error "Empty link"))
  8734. (when (and description
  8735. (stringp description)
  8736. (not (string-match "\\S-" description)))
  8737. (setq description nil))
  8738. (when (stringp description)
  8739. ;; Remove brackets from the description, they are fatal.
  8740. (while (string-match "\\[" description)
  8741. (setq description (replace-match "{" t t description)))
  8742. (while (string-match "\\]" description)
  8743. (setq description (replace-match "}" t t description))))
  8744. (when (equal link description)
  8745. ;; No description needed, it is identical
  8746. (setq description nil))
  8747. (when (and (not description)
  8748. (not (string-match (org-image-file-name-regexp) link))
  8749. (not (equal link (org-link-escape link))))
  8750. (setq description (org-extract-attributes link)))
  8751. (setq link
  8752. (cond ((string-match (org-image-file-name-regexp) link) link)
  8753. ((string-match org-link-types-re link)
  8754. (concat (match-string 1 link)
  8755. (org-link-escape (substring link (match-end 1)))))
  8756. (t (org-link-escape link))))
  8757. (concat "[[" link "]"
  8758. (if description (concat "[" description "]") "")
  8759. "]"))
  8760. (defconst org-link-escape-chars
  8761. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8762. "List of characters that should be escaped in link.
  8763. This is the list that is used for internal purposes.")
  8764. (defconst org-link-escape-chars-browser
  8765. '(?\ ?\")
  8766. "List of escapes for characters that are problematic in links.
  8767. This is the list that is used before handing over to the browser.")
  8768. (defun org-link-escape (text &optional table merge)
  8769. "Return percent escaped representation of TEXT.
  8770. TEXT is a string with the text to escape.
  8771. Optional argument TABLE is a list with characters that should be
  8772. escaped. When nil, `org-link-escape-chars' is used.
  8773. If optional argument MERGE is set, merge TABLE into
  8774. `org-link-escape-chars'."
  8775. (cond
  8776. ((and table merge)
  8777. (mapc (lambda (defchr)
  8778. (unless (member defchr table)
  8779. (setq table (cons defchr table)))) org-link-escape-chars))
  8780. ((null table)
  8781. (setq table org-link-escape-chars)))
  8782. (mapconcat
  8783. (lambda (char)
  8784. (if (or (member char table)
  8785. (and (or (< char 32) (= char 37) (> char 126))
  8786. org-url-hexify-p))
  8787. (mapconcat (lambda (sequence-element)
  8788. (format "%%%.2X" sequence-element))
  8789. (or (encode-coding-char char 'utf-8)
  8790. (error "Unable to percent escape character: %s"
  8791. (char-to-string char))) "")
  8792. (char-to-string char))) text ""))
  8793. (defun org-link-unescape (str)
  8794. "Unhex hexified Unicode strings as returned from the JavaScript function
  8795. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8796. (unless (and (null str) (string= "" str))
  8797. (let ((pos 0) (case-fold-search t) unhexed)
  8798. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8799. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8800. (setq str (replace-match unhexed t t str))
  8801. (setq pos (+ pos (length unhexed))))))
  8802. str)
  8803. (defun org-link-unescape-compound (hex)
  8804. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8805. Note: this function also decodes single byte encodings like
  8806. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8807. (save-match-data
  8808. (let* ((bytes (cdr (split-string hex "%")))
  8809. (ret "")
  8810. (eat 0)
  8811. (sum 0))
  8812. (while bytes
  8813. (let* ((val (string-to-number (pop bytes) 16))
  8814. (shift-xor
  8815. (if (= 0 eat)
  8816. (cond
  8817. ((>= val 252) (cons 6 252))
  8818. ((>= val 248) (cons 5 248))
  8819. ((>= val 240) (cons 4 240))
  8820. ((>= val 224) (cons 3 224))
  8821. ((>= val 192) (cons 2 192))
  8822. (t (cons 0 0)))
  8823. (cons 6 128))))
  8824. (if (>= val 192) (setq eat (car shift-xor)))
  8825. (setq val (logxor val (cdr shift-xor)))
  8826. (setq sum (+ (lsh sum (car shift-xor)) val))
  8827. (if (> eat 0) (setq eat (- eat 1)))
  8828. (cond
  8829. ((= 0 eat) ;multi byte
  8830. (setq ret (concat ret (org-char-to-string sum)))
  8831. (setq sum 0))
  8832. ((not bytes) ; single byte(s)
  8833. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8834. )) ;; end (while bytes
  8835. ret )))
  8836. (defun org-link-unescape-single-byte-sequence (hex)
  8837. "Unhexify hex-encoded single byte character sequences."
  8838. (mapconcat (lambda (byte)
  8839. (char-to-string (string-to-number byte 16)))
  8840. (cdr (split-string hex "%")) ""))
  8841. (defun org-xor (a b)
  8842. "Exclusive or."
  8843. (if a (not b) b))
  8844. (defun org-fixup-message-id-for-http (s)
  8845. "Replace special characters in a message id, so it can be used in an http query."
  8846. (when (string-match "%" s)
  8847. (setq s (mapconcat (lambda (c)
  8848. (if (eq c ?%)
  8849. "%25"
  8850. (char-to-string c)))
  8851. s "")))
  8852. (while (string-match "<" s)
  8853. (setq s (replace-match "%3C" t t s)))
  8854. (while (string-match ">" s)
  8855. (setq s (replace-match "%3E" t t s)))
  8856. (while (string-match "@" s)
  8857. (setq s (replace-match "%40" t t s)))
  8858. s)
  8859. (defun org-link-prettify (link)
  8860. "Return a human-readable representation of LINK.
  8861. The car of LINK must be a raw link the cdr of LINK must be either
  8862. a link description or nil."
  8863. (let ((desc (or (cadr link) "<no description>")))
  8864. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8865. "<" (car link) ">")))
  8866. ;;;###autoload
  8867. (defun org-insert-link-global ()
  8868. "Insert a link like Org-mode does.
  8869. This command can be called in any mode to insert a link in Org-mode syntax."
  8870. (interactive)
  8871. (org-load-modules-maybe)
  8872. (org-run-like-in-org-mode 'org-insert-link))
  8873. (defun org-insert-all-links (&optional keep)
  8874. "Insert all links in `org-stored-links'."
  8875. (interactive "P")
  8876. (let ((links (copy-sequence org-stored-links)) l)
  8877. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8878. (insert "- ")
  8879. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8880. (insert "\n"))))
  8881. (defun org-link-fontify-links-to-this-file ()
  8882. "Fontify links to the current file in `org-stored-links'."
  8883. (let ((f (buffer-file-name)) a b)
  8884. (setq a (mapcar (lambda(l)
  8885. (let ((ll (car l)))
  8886. (when (and (string-match "^file:\\(.+\\)::" ll)
  8887. (equal f (expand-file-name (match-string 1 ll))))
  8888. ll)))
  8889. org-stored-links))
  8890. (when (featurep 'org-id)
  8891. (setq b (mapcar (lambda(l)
  8892. (let ((ll (car l)))
  8893. (when (and (string-match "^id:\\(.+\\)$" ll)
  8894. (equal f (expand-file-name
  8895. (or (org-id-find-id-file
  8896. (match-string 1 ll)) ""))))
  8897. ll)))
  8898. org-stored-links)))
  8899. (mapcar (lambda(l)
  8900. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8901. (delq nil (append a b)))))
  8902. (defvar org-link-links-in-this-file nil)
  8903. (defun org-insert-link (&optional complete-file link-location default-description)
  8904. "Insert a link. At the prompt, enter the link.
  8905. Completion can be used to insert any of the link protocol prefixes like
  8906. http or ftp in use.
  8907. The history can be used to select a link previously stored with
  8908. `org-store-link'. When the empty string is entered (i.e. if you just
  8909. press RET at the prompt), the link defaults to the most recently
  8910. stored link. As SPC triggers completion in the minibuffer, you need to
  8911. use M-SPC or C-q SPC to force the insertion of a space character.
  8912. You will also be prompted for a description, and if one is given, it will
  8913. be displayed in the buffer instead of the link.
  8914. If there is already a link at point, this command will allow you to edit link
  8915. and description parts.
  8916. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8917. be selected using completion. The path to the file will be relative to the
  8918. current directory if the file is in the current directory or a subdirectory.
  8919. Otherwise, the link will be the absolute path as completed in the minibuffer
  8920. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8921. option `org-link-file-path-type'.
  8922. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8923. the current directory or below.
  8924. With three \\[universal-argument] prefixes, negate the meaning of
  8925. `org-keep-stored-link-after-insertion'.
  8926. If `org-make-link-description-function' is non-nil, this function will be
  8927. called with the link target, and the result will be the default
  8928. link description.
  8929. If the LINK-LOCATION parameter is non-nil, this value will be
  8930. used as the link location instead of reading one interactively.
  8931. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8932. be used as the default description."
  8933. (interactive "P")
  8934. (let* ((wcf (current-window-configuration))
  8935. (origbuf (current-buffer))
  8936. (region (if (org-region-active-p)
  8937. (buffer-substring (region-beginning) (region-end))))
  8938. (remove (and region (list (region-beginning) (region-end))))
  8939. (desc region)
  8940. tmphist ; byte-compile incorrectly complains about this
  8941. (link link-location)
  8942. (abbrevs org-link-abbrev-alist-local)
  8943. entry file all-prefixes auto-desc)
  8944. (cond
  8945. (link-location) ; specified by arg, just use it.
  8946. ((org-in-regexp org-bracket-link-regexp 1)
  8947. ;; We do have a link at point, and we are going to edit it.
  8948. (setq remove (list (match-beginning 0) (match-end 0)))
  8949. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8950. (setq link (read-string "Link: "
  8951. (org-link-unescape
  8952. (org-match-string-no-properties 1)))))
  8953. ((or (org-in-regexp org-angle-link-re)
  8954. (org-in-regexp org-plain-link-re))
  8955. ;; Convert to bracket link
  8956. (setq remove (list (match-beginning 0) (match-end 0))
  8957. link (read-string "Link: "
  8958. (org-remove-angle-brackets (match-string 0)))))
  8959. ((member complete-file '((4) (16)))
  8960. ;; Completing read for file names.
  8961. (setq link (org-file-complete-link complete-file)))
  8962. (t
  8963. ;; Read link, with completion for stored links.
  8964. (org-link-fontify-links-to-this-file)
  8965. (org-switch-to-buffer-other-window "*Org Links*")
  8966. (with-current-buffer "*Org Links*"
  8967. (erase-buffer)
  8968. (insert "Insert a link.
  8969. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8970. (when org-stored-links
  8971. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8972. (insert (mapconcat 'org-link-prettify
  8973. (reverse org-stored-links) "\n")))
  8974. (goto-char (point-min)))
  8975. (let ((cw (selected-window)))
  8976. (select-window (get-buffer-window "*Org Links*" 'visible))
  8977. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8978. (unless (pos-visible-in-window-p (point-max))
  8979. (org-fit-window-to-buffer))
  8980. (and (window-live-p cw) (select-window cw)))
  8981. ;; Fake a link history, containing the stored links.
  8982. (setq tmphist (append (mapcar 'car org-stored-links)
  8983. org-insert-link-history))
  8984. (setq all-prefixes (append (mapcar 'car abbrevs)
  8985. (mapcar 'car org-link-abbrev-alist)
  8986. org-link-types))
  8987. (unwind-protect
  8988. (progn
  8989. (setq link
  8990. (org-completing-read
  8991. "Link: "
  8992. (append
  8993. (mapcar (lambda (x) (concat x ":"))
  8994. all-prefixes)
  8995. (mapcar 'car org-stored-links))
  8996. nil nil nil
  8997. 'tmphist
  8998. (caar org-stored-links)))
  8999. (if (not (string-match "\\S-" link))
  9000. (user-error "No link selected"))
  9001. (mapc (lambda(l)
  9002. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9003. org-stored-links)
  9004. (if (or (member link all-prefixes)
  9005. (and (equal ":" (substring link -1))
  9006. (member (substring link 0 -1) all-prefixes)
  9007. (setq link (substring link 0 -1))))
  9008. (setq link (with-current-buffer origbuf
  9009. (org-link-try-special-completion link)))))
  9010. (set-window-configuration wcf)
  9011. (kill-buffer "*Org Links*"))
  9012. (setq entry (assoc link org-stored-links))
  9013. (or entry (push link org-insert-link-history))
  9014. (setq desc (or desc (nth 1 entry)))))
  9015. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9016. (not org-keep-stored-link-after-insertion))
  9017. (setq org-stored-links (delq (assoc link org-stored-links)
  9018. org-stored-links)))
  9019. (if (string-match org-plain-link-re link)
  9020. ;; URL-like link, normalize the use of angular brackets.
  9021. (setq link (org-remove-angle-brackets link)))
  9022. ;; Check if we are linking to the current file with a search
  9023. ;; option If yes, simplify the link by using only the search
  9024. ;; option.
  9025. (when (and buffer-file-name
  9026. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9027. (let* ((path (match-string 1 link))
  9028. (case-fold-search nil)
  9029. (search (match-string 2 link)))
  9030. (save-match-data
  9031. (if (equal (file-truename buffer-file-name) (file-truename path))
  9032. ;; We are linking to this same file, with a search option
  9033. (setq link search)))))
  9034. ;; Check if we can/should use a relative path. If yes, simplify the link
  9035. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9036. (let* ((type (match-string 1 link))
  9037. (path (match-string 2 link))
  9038. (origpath path)
  9039. (case-fold-search nil))
  9040. (cond
  9041. ((or (eq org-link-file-path-type 'absolute)
  9042. (equal complete-file '(16)))
  9043. (setq path (abbreviate-file-name (expand-file-name path))))
  9044. ((eq org-link-file-path-type 'noabbrev)
  9045. (setq path (expand-file-name path)))
  9046. ((eq org-link-file-path-type 'relative)
  9047. (setq path (file-relative-name path)))
  9048. (t
  9049. (save-match-data
  9050. (if (string-match (concat "^" (regexp-quote
  9051. (expand-file-name
  9052. (file-name-as-directory
  9053. default-directory))))
  9054. (expand-file-name path))
  9055. ;; We are linking a file with relative path name.
  9056. (setq path (substring (expand-file-name path)
  9057. (match-end 0)))
  9058. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9059. (setq link (concat type path))
  9060. (if (equal desc origpath)
  9061. (setq desc path))))
  9062. (if org-make-link-description-function
  9063. (setq desc
  9064. (or (condition-case nil
  9065. (funcall org-make-link-description-function link desc)
  9066. (error (progn (message "Can't get link description from `%s'"
  9067. (symbol-name org-make-link-description-function))
  9068. (sit-for 2) nil)))
  9069. (read-string "Description: " default-description)))
  9070. (if default-description (setq desc default-description)
  9071. (setq desc (or (and auto-desc desc)
  9072. (read-string "Description: " desc)))))
  9073. (unless (string-match "\\S-" desc) (setq desc nil))
  9074. (if remove (apply 'delete-region remove))
  9075. (insert (org-make-link-string link desc))))
  9076. (defun org-link-try-special-completion (type)
  9077. "If there is completion support for link type TYPE, offer it."
  9078. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9079. (if (functionp fun)
  9080. (funcall fun)
  9081. (read-string "Link (no completion support): " (concat type ":")))))
  9082. (defun org-file-complete-link (&optional arg)
  9083. "Create a file link using completion."
  9084. (let (file link)
  9085. (setq file (org-iread-file-name "File: "))
  9086. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9087. (pwd1 (file-name-as-directory (abbreviate-file-name
  9088. (expand-file-name ".")))))
  9089. (cond
  9090. ((equal arg '(16))
  9091. (setq link (concat
  9092. "file:"
  9093. (abbreviate-file-name (expand-file-name file)))))
  9094. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9095. (setq link (concat "file:" (match-string 1 file))))
  9096. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9097. (expand-file-name file))
  9098. (setq link (concat
  9099. "file:" (match-string 1 (expand-file-name file)))))
  9100. (t (setq link (concat "file:" file)))))
  9101. link))
  9102. (defun org-iread-file-name (&rest args)
  9103. "Read-file-name using `ido-mode' speedup if available.
  9104. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9105. See `read-file-name' for a description of parameters."
  9106. (org-without-partial-completion
  9107. (if (and org-completion-use-ido
  9108. (fboundp 'ido-read-file-name)
  9109. (boundp 'ido-mode) ido-mode
  9110. (listp (second args)))
  9111. (let ((ido-enter-matching-directory nil))
  9112. (apply 'ido-read-file-name args))
  9113. (apply 'read-file-name args))))
  9114. (defun org-completing-read (&rest args)
  9115. "Completing-read with SPACE being a normal character."
  9116. (let ((enable-recursive-minibuffers t)
  9117. (minibuffer-local-completion-map
  9118. (copy-keymap minibuffer-local-completion-map)))
  9119. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9120. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9121. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9122. (apply 'org-icompleting-read args)))
  9123. (defun org-completing-read-no-i (&rest args)
  9124. (let (org-completion-use-ido org-completion-use-iswitchb)
  9125. (apply 'org-completing-read args)))
  9126. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9127. "Use iswitch as a completing-read replacement to choose from choices.
  9128. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9129. from."
  9130. (let* ((iswitchb-use-virtual-buffers nil)
  9131. (iswitchb-make-buflist-hook
  9132. (lambda ()
  9133. (setq iswitchb-temp-buflist choices))))
  9134. (iswitchb-read-buffer prompt)))
  9135. (defun org-icompleting-read (&rest args)
  9136. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9137. (org-without-partial-completion
  9138. (if (and org-completion-use-ido
  9139. (fboundp 'ido-completing-read)
  9140. (boundp 'ido-mode) ido-mode
  9141. (listp (second args)))
  9142. (let ((ido-enter-matching-directory nil))
  9143. (apply 'ido-completing-read (concat (car args))
  9144. (if (consp (car (nth 1 args)))
  9145. (mapcar 'car (nth 1 args))
  9146. (nth 1 args))
  9147. (cddr args)))
  9148. (if (and org-completion-use-iswitchb
  9149. (boundp 'iswitchb-mode) iswitchb-mode
  9150. (listp (second args)))
  9151. (apply 'org-iswitchb-completing-read (concat (car args))
  9152. (if (consp (car (nth 1 args)))
  9153. (mapcar 'car (nth 1 args))
  9154. (nth 1 args))
  9155. (cddr args))
  9156. (apply 'completing-read args)))))
  9157. (defun org-extract-attributes (s)
  9158. "Extract the attributes cookie from a string and set as text property."
  9159. (let (a attr (start 0) key value)
  9160. (save-match-data
  9161. (when (string-match "{{\\([^}]+\\)}}$" s)
  9162. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9163. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9164. (setq key (match-string 1 a) value (match-string 2 a)
  9165. start (match-end 0)
  9166. attr (plist-put attr (intern key) value))))
  9167. (org-add-props s nil 'org-attr attr))
  9168. s))
  9169. ;;; Opening/following a link
  9170. (defvar org-link-search-failed nil)
  9171. (defvar org-open-link-functions nil
  9172. "Hook for functions finding a plain text link.
  9173. These functions must take a single argument, the link content.
  9174. They will be called for links that look like [[link text][description]]
  9175. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9176. like a filename (e.g. \"./blue.png\").
  9177. These functions will be called *before* Org attempts to resolve the
  9178. link by doing text searches in the current buffer - so if you want a
  9179. link \"[[target]]\" to still find \"<<target>>\", your function should
  9180. handle this as a special case.
  9181. When the function does handle the link, it must return a non-nil value.
  9182. If it decides that it is not responsible for this link, it must return
  9183. nil to indicate that that Org-mode can continue with other options
  9184. like exact and fuzzy text search.")
  9185. (defun org-next-link (&optional search-backward)
  9186. "Move forward to the next link.
  9187. If the link is in hidden text, expose it."
  9188. (interactive "P")
  9189. (when (and org-link-search-failed (eq this-command last-command))
  9190. (goto-char (point-min))
  9191. (message "Link search wrapped back to beginning of buffer"))
  9192. (setq org-link-search-failed nil)
  9193. (let* ((pos (point))
  9194. (ct (org-context))
  9195. (a (assoc :link ct))
  9196. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9197. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9198. ((looking-at org-any-link-re)
  9199. ;; Don't stay stuck at link without an org-link face
  9200. (forward-char (if search-backward -1 1))))
  9201. (if (funcall srch-fun org-any-link-re nil t)
  9202. (progn
  9203. (goto-char (match-beginning 0))
  9204. (if (outline-invisible-p) (org-show-context)))
  9205. (goto-char pos)
  9206. (setq org-link-search-failed t)
  9207. (message "No further link found"))))
  9208. (defun org-previous-link ()
  9209. "Move backward to the previous link.
  9210. If the link is in hidden text, expose it."
  9211. (interactive)
  9212. (funcall 'org-next-link t))
  9213. (defun org-translate-link (s)
  9214. "Translate a link string if a translation function has been defined."
  9215. (if (and org-link-translation-function
  9216. (fboundp org-link-translation-function)
  9217. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9218. (progn
  9219. (setq s (funcall org-link-translation-function
  9220. (match-string 1 s) (match-string 2 s)))
  9221. (concat (car s) ":" (cdr s)))
  9222. s))
  9223. (defun org-translate-link-from-planner (type path)
  9224. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9225. This is still an experimental function, your mileage may vary."
  9226. (cond
  9227. ((member type '("http" "https" "news" "ftp"))
  9228. ;; standard Internet links are the same.
  9229. nil)
  9230. ((and (equal type "irc") (string-match "^//" path))
  9231. ;; Planner has two / at the beginning of an irc link, we have 1.
  9232. ;; We should have zero, actually....
  9233. (setq path (substring path 1)))
  9234. ((and (equal type "lisp") (string-match "^/" path))
  9235. ;; Planner has a slash, we do not.
  9236. (setq type "elisp" path (substring path 1)))
  9237. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9238. ;; A typical message link. Planner has the id after the final slash,
  9239. ;; we separate it with a hash mark
  9240. (setq path (concat (match-string 1 path) "#"
  9241. (org-remove-angle-brackets (match-string 2 path))))))
  9242. (cons type path))
  9243. (defun org-find-file-at-mouse (ev)
  9244. "Open file link or URL at mouse."
  9245. (interactive "e")
  9246. (mouse-set-point ev)
  9247. (org-open-at-point 'in-emacs))
  9248. (defun org-open-at-mouse (ev)
  9249. "Open file link or URL at mouse.
  9250. See the docstring of `org-open-file' for details."
  9251. (interactive "e")
  9252. (mouse-set-point ev)
  9253. (if (eq major-mode 'org-agenda-mode)
  9254. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9255. (org-open-at-point))
  9256. (defvar org-window-config-before-follow-link nil
  9257. "The window configuration before following a link.
  9258. This is saved in case the need arises to restore it.")
  9259. (defvar org-open-link-marker (make-marker)
  9260. "Marker pointing to the location where `org-open-at-point; was called.")
  9261. ;;;###autoload
  9262. (defun org-open-at-point-global ()
  9263. "Follow a link like Org-mode does.
  9264. This command can be called in any mode to follow a link that has
  9265. Org-mode syntax."
  9266. (interactive)
  9267. (org-run-like-in-org-mode 'org-open-at-point))
  9268. ;;;###autoload
  9269. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9270. "Open a link in the string S, as if it was in Org-mode."
  9271. (interactive "sLink: \nP")
  9272. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9273. (with-temp-buffer
  9274. (let ((org-inhibit-startup (not reference-buffer)))
  9275. (org-mode)
  9276. (insert s)
  9277. (goto-char (point-min))
  9278. (when reference-buffer
  9279. (setq org-link-abbrev-alist-local
  9280. (with-current-buffer reference-buffer
  9281. org-link-abbrev-alist-local)))
  9282. (org-open-at-point arg reference-buffer)))))
  9283. (defvar org-open-at-point-functions nil
  9284. "Hook that is run when following a link at point.
  9285. Functions in this hook must return t if they identify and follow
  9286. a link at point. If they don't find anything interesting at point,
  9287. they must return nil.")
  9288. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9289. (defun org-open-at-point (&optional arg reference-buffer)
  9290. "Open link at or after point.
  9291. If there is no link at point, this function will search forward up to
  9292. the end of the current line.
  9293. Normally, files will be opened by an appropriate application. If the
  9294. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9295. With a double prefix argument, try to open outside of Emacs, in the
  9296. application the system uses for this file type."
  9297. (interactive "P")
  9298. ;; if in a code block, then open the block's results
  9299. (unless (call-interactively #'org-babel-open-src-block-result)
  9300. (org-load-modules-maybe)
  9301. (move-marker org-open-link-marker (point))
  9302. (setq org-window-config-before-follow-link (current-window-configuration))
  9303. (org-remove-occur-highlights nil nil t)
  9304. (cond
  9305. ((and (org-at-heading-p)
  9306. (not (org-at-timestamp-p t))
  9307. (not (org-in-regexp
  9308. (concat org-plain-link-re "\\|"
  9309. org-bracket-link-regexp "\\|"
  9310. org-angle-link-re "\\|"
  9311. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9312. (not (get-text-property (point) 'org-linked-text)))
  9313. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9314. (lk0 (car lkall))
  9315. (lk (if (stringp lk0) (list lk0) lk0))
  9316. (lkend (cdr lkall)))
  9317. (mapcar (lambda(l)
  9318. (search-forward l nil lkend)
  9319. (goto-char (match-beginning 0))
  9320. (org-open-at-point))
  9321. lk))
  9322. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9323. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9324. ((and (org-at-timestamp-p t)
  9325. (not (org-in-regexp org-bracket-link-regexp)))
  9326. (org-follow-timestamp-link))
  9327. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9328. (not (org-in-regexp org-any-link-re)))
  9329. (org-footnote-action))
  9330. (t
  9331. (let (type path link line search (pos (point)))
  9332. (catch 'match
  9333. (save-excursion
  9334. (skip-chars-forward "^]\n\r")
  9335. (when (org-in-regexp org-bracket-link-regexp 1)
  9336. (setq link (org-extract-attributes
  9337. (org-link-unescape (org-match-string-no-properties 1))))
  9338. (while (string-match " *\n *" link)
  9339. (setq link (replace-match " " t t link)))
  9340. (setq link (org-link-expand-abbrev link))
  9341. (cond
  9342. ((or (file-name-absolute-p link)
  9343. (string-match "^\\.\\.?/" link))
  9344. (setq type "file" path link))
  9345. ((string-match org-link-re-with-space3 link)
  9346. (setq type (match-string 1 link) path (match-string 2 link)))
  9347. ((string-match "^help:+\\(.+\\)" link)
  9348. (setq type "help" path (match-string 1 link)))
  9349. (t (setq type "thisfile" path link)))
  9350. (throw 'match t)))
  9351. (when (get-text-property (point) 'org-linked-text)
  9352. (setq type "thisfile"
  9353. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9354. (1+ (point)) (point))
  9355. path (buffer-substring
  9356. (or (previous-single-property-change pos 'org-linked-text)
  9357. (point-min))
  9358. (or (next-single-property-change pos 'org-linked-text)
  9359. (point-max)))
  9360. ;; Ensure we will search for a <<<radio>>> link, not
  9361. ;; a simple reference like <<ref>>
  9362. path (concat "<" path))
  9363. (throw 'match t))
  9364. (save-excursion
  9365. (when (or (org-in-regexp org-angle-link-re)
  9366. (let ((match (org-in-regexp org-plain-link-re)))
  9367. ;; Check a plain link is not within a bracket link
  9368. (and match
  9369. (save-excursion
  9370. (progn
  9371. (goto-char (car match))
  9372. (not (org-in-regexp org-bracket-link-regexp))))))
  9373. (let ((line_ending (save-excursion (end-of-line) (point))))
  9374. ;; We are in a line before a plain or bracket link
  9375. (or (re-search-forward org-plain-link-re line_ending t)
  9376. (re-search-forward org-bracket-link-regexp line_ending t))))
  9377. (setq type (match-string 1)
  9378. path (org-link-unescape (match-string 2)))
  9379. (throw 'match t)))
  9380. (save-excursion
  9381. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9382. (setq type "tags"
  9383. path (match-string 1))
  9384. (while (string-match ":" path)
  9385. (setq path (replace-match "+" t t path)))
  9386. (throw 'match t)))
  9387. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9388. (setq type "tree-match"
  9389. path (match-string 1))
  9390. (throw 'match t)))
  9391. (unless path
  9392. (user-error "No link found"))
  9393. ;; switch back to reference buffer
  9394. ;; needed when if called in a temporary buffer through
  9395. ;; org-open-link-from-string
  9396. (with-current-buffer (or reference-buffer (current-buffer))
  9397. ;; Remove any trailing spaces in path
  9398. (if (string-match " +\\'" path)
  9399. (setq path (replace-match "" t t path)))
  9400. (if (and org-link-translation-function
  9401. (fboundp org-link-translation-function))
  9402. ;; Check if we need to translate the link
  9403. (let ((tmp (funcall org-link-translation-function type path)))
  9404. (setq type (car tmp) path (cdr tmp))))
  9405. (cond
  9406. ((assoc type org-link-protocols)
  9407. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9408. ((equal type "help")
  9409. (let ((f-or-v (intern path)))
  9410. (cond ((fboundp f-or-v)
  9411. (describe-function f-or-v))
  9412. ((boundp f-or-v)
  9413. (describe-variable f-or-v))
  9414. (t (error "Not a known function or variable")))))
  9415. ((equal type "mailto")
  9416. (let ((cmd (car org-link-mailto-program))
  9417. (args (cdr org-link-mailto-program)) args1
  9418. (address path) (subject "") a)
  9419. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9420. (setq address (match-string 1 path)
  9421. subject (org-link-escape (match-string 2 path))))
  9422. (while args
  9423. (cond
  9424. ((not (stringp (car args))) (push (pop args) args1))
  9425. (t (setq a (pop args))
  9426. (if (string-match "%a" a)
  9427. (setq a (replace-match address t t a)))
  9428. (if (string-match "%s" a)
  9429. (setq a (replace-match subject t t a)))
  9430. (push a args1))))
  9431. (apply cmd (nreverse args1))))
  9432. ((member type '("http" "https" "ftp" "news"))
  9433. (browse-url
  9434. (concat type ":"
  9435. (if (org-string-match-p
  9436. (concat "[[:nonascii:]"
  9437. org-link-escape-chars-browser "]")
  9438. path)
  9439. (org-link-escape path org-link-escape-chars-browser)
  9440. path))))
  9441. ((string= type "doi")
  9442. (browse-url
  9443. (concat org-doi-server-url
  9444. (if (org-string-match-p
  9445. (concat "[[:nonascii:]"
  9446. org-link-escape-chars-browser "]")
  9447. path)
  9448. (org-link-escape path org-link-escape-chars-browser)
  9449. path))))
  9450. ((member type '("message"))
  9451. (browse-url (concat type ":" path)))
  9452. ((string= type "tags")
  9453. (org-tags-view arg path))
  9454. ((string= type "tree-match")
  9455. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9456. ((string= type "file")
  9457. (if (string-match "::\\([0-9]+\\)\\'" path)
  9458. (setq line (string-to-number (match-string 1 path))
  9459. path (substring path 0 (match-beginning 0)))
  9460. (if (string-match "::\\(.+\\)\\'" path)
  9461. (setq search (match-string 1 path)
  9462. path (substring path 0 (match-beginning 0)))))
  9463. (if (string-match "[*?{]" (file-name-nondirectory path))
  9464. (dired path)
  9465. (org-open-file path arg line search)))
  9466. ((string= type "shell")
  9467. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9468. (cmd path))
  9469. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9470. (string-match org-confirm-shell-link-not-regexp cmd))
  9471. (not org-confirm-shell-link-function)
  9472. (funcall org-confirm-shell-link-function
  9473. (format "Execute \"%s\" in shell? "
  9474. (org-add-props cmd nil
  9475. 'face 'org-warning))))
  9476. (progn
  9477. (message "Executing %s" cmd)
  9478. (shell-command cmd buf)
  9479. (if (featurep 'midnight)
  9480. (setq clean-buffer-list-kill-buffer-names
  9481. (cons buf clean-buffer-list-kill-buffer-names))))
  9482. (error "Abort"))))
  9483. ((string= type "elisp")
  9484. (let ((cmd path))
  9485. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9486. (string-match org-confirm-elisp-link-not-regexp cmd))
  9487. (not org-confirm-elisp-link-function)
  9488. (funcall org-confirm-elisp-link-function
  9489. (format "Execute \"%s\" as elisp? "
  9490. (org-add-props cmd nil
  9491. 'face 'org-warning))))
  9492. (message "%s => %s" cmd
  9493. (if (equal (string-to-char cmd) ?\()
  9494. (eval (read cmd))
  9495. (call-interactively (read cmd))))
  9496. (error "Abort"))))
  9497. ((and (string= type "thisfile")
  9498. (run-hook-with-args-until-success
  9499. 'org-open-link-functions path)))
  9500. ((string= type "thisfile")
  9501. (if arg
  9502. (switch-to-buffer-other-window
  9503. (org-get-buffer-for-internal-link (current-buffer)))
  9504. (org-mark-ring-push))
  9505. (let ((cmd `(org-link-search
  9506. ,path
  9507. ,(cond ((equal arg '(4)) ''occur)
  9508. ((equal arg '(16)) ''org-occur))
  9509. ,pos)))
  9510. (condition-case nil (let ((org-link-search-inhibit-query t))
  9511. (eval cmd))
  9512. (error (progn (widen) (eval cmd))))))
  9513. (t (browse-url-at-point)))))))
  9514. (move-marker org-open-link-marker nil)
  9515. (run-hook-with-args 'org-follow-link-hook)))
  9516. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9517. "Offer links in the current entry and return the selected link.
  9518. If there is only one link, return it.
  9519. If NTH is an integer, return the NTH link found.
  9520. If ZERO is a string, check also this string for a link, and if
  9521. there is one, return it."
  9522. (with-current-buffer buffer
  9523. (save-excursion
  9524. (save-restriction
  9525. (widen)
  9526. (goto-char marker)
  9527. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9528. "\\(" org-angle-link-re "\\)\\|"
  9529. "\\(" org-plain-link-re "\\)"))
  9530. (cnt ?0)
  9531. (in-emacs (if (integerp nth) nil nth))
  9532. have-zero end links link c)
  9533. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9534. (push (match-string 0 zero) links)
  9535. (setq cnt (1- cnt) have-zero t))
  9536. (save-excursion
  9537. (org-back-to-heading t)
  9538. (setq end (save-excursion (outline-next-heading) (point)))
  9539. (while (re-search-forward re end t)
  9540. (push (match-string 0) links))
  9541. (setq links (org-uniquify (reverse links))))
  9542. (cond
  9543. ((null links)
  9544. (message "No links"))
  9545. ((equal (length links) 1)
  9546. (setq link (car links)))
  9547. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9548. (setq link (nth (if have-zero nth (1- nth)) links)))
  9549. (t ; we have to select a link
  9550. (save-excursion
  9551. (save-window-excursion
  9552. (delete-other-windows)
  9553. (with-output-to-temp-buffer "*Select Link*"
  9554. (mapc (lambda (l)
  9555. (if (not (string-match org-bracket-link-regexp l))
  9556. (princ (format "[%c] %s\n" (incf cnt)
  9557. (org-remove-angle-brackets l)))
  9558. (if (match-end 3)
  9559. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9560. (match-string 3 l) (match-string 1 l)))
  9561. (princ (format "[%c] %s\n" (incf cnt)
  9562. (match-string 1 l))))))
  9563. links))
  9564. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9565. (message "Select link to open, RET to open all:")
  9566. (setq c (read-char-exclusive))
  9567. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9568. (when (equal c ?q) (error "Abort"))
  9569. (if (equal c ?\C-m)
  9570. (setq link links)
  9571. (setq nth (- c ?0))
  9572. (if have-zero (setq nth (1+ nth)))
  9573. (unless (and (integerp nth) (>= (length links) nth))
  9574. (user-error "Invalid link selection"))
  9575. (setq link (nth (1- nth) links)))))
  9576. (cons link end))))))
  9577. ;; Add special file links that specify the way of opening
  9578. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9579. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9580. (defun org-open-file-with-system (path)
  9581. "Open file at PATH using the system way of opening it."
  9582. (org-open-file path 'system))
  9583. (defun org-open-file-with-emacs (path)
  9584. "Open file at PATH in Emacs."
  9585. (org-open-file path 'emacs))
  9586. ;;; File search
  9587. (defvar org-create-file-search-functions nil
  9588. "List of functions to construct the right search string for a file link.
  9589. These functions are called in turn with point at the location to
  9590. which the link should point.
  9591. A function in the hook should first test if it would like to
  9592. handle this file type, for example by checking the `major-mode'
  9593. or the file extension. If it decides not to handle this file, it
  9594. should just return nil to give other functions a chance. If it
  9595. does handle the file, it must return the search string to be used
  9596. when following the link. The search string will be part of the
  9597. file link, given after a double colon, and `org-open-at-point'
  9598. will automatically search for it. If special measures must be
  9599. taken to make the search successful, another function should be
  9600. added to the companion hook `org-execute-file-search-functions',
  9601. which see.
  9602. A function in this hook may also use `setq' to set the variable
  9603. `description' to provide a suggestion for the descriptive text to
  9604. be used for this link when it gets inserted into an Org-mode
  9605. buffer with \\[org-insert-link].")
  9606. (defvar org-execute-file-search-functions nil
  9607. "List of functions to execute a file search triggered by a link.
  9608. Functions added to this hook must accept a single argument, the
  9609. search string that was part of the file link, the part after the
  9610. double colon. The function must first check if it would like to
  9611. handle this search, for example by checking the `major-mode' or
  9612. the file extension. If it decides not to handle this search, it
  9613. should just return nil to give other functions a chance. If it
  9614. does handle the search, it must return a non-nil value to keep
  9615. other functions from trying.
  9616. Each function can access the current prefix argument through the
  9617. variable `current-prefix-arg'. Note that a single prefix is used
  9618. to force opening a link in Emacs, so it may be good to only use a
  9619. numeric or double prefix to guide the search function.
  9620. In case this is needed, a function in this hook can also restore
  9621. the window configuration before `org-open-at-point' was called using:
  9622. (set-window-configuration org-window-config-before-follow-link)")
  9623. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9624. (defun org-link-search (s &optional type avoid-pos stealth)
  9625. "Search for a link search option.
  9626. If S is surrounded by forward slashes, it is interpreted as a
  9627. regular expression. In org-mode files, this will create an `org-occur'
  9628. sparse tree. In ordinary files, `occur' will be used to list matches.
  9629. If the current buffer is in `dired-mode', grep will be used to search
  9630. in all files. If AVOID-POS is given, ignore matches near that position.
  9631. When optional argument STEALTH is non-nil, do not modify
  9632. visibility around point, thus ignoring
  9633. `org-show-hierarchy-above', `org-show-following-heading' and
  9634. `org-show-siblings' variables."
  9635. (let ((case-fold-search t)
  9636. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9637. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9638. (append '(("") (" ") ("\t") ("\n"))
  9639. org-emphasis-alist)
  9640. "\\|") "\\)"))
  9641. (pos (point))
  9642. (pre nil) (post nil)
  9643. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9644. (cond
  9645. ;; First check if there are any special search functions
  9646. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9647. ;; Now try the builtin stuff
  9648. ((and (equal (string-to-char s0) ?#)
  9649. (> (length s0) 1)
  9650. (save-excursion
  9651. (goto-char (point-min))
  9652. (and
  9653. (re-search-forward
  9654. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9655. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9656. (setq type 'dedicated
  9657. pos (match-beginning 0))))
  9658. ;; There is an exact target for this
  9659. (goto-char pos)
  9660. (org-back-to-heading t)))
  9661. ((save-excursion
  9662. (goto-char (point-min))
  9663. (and
  9664. (re-search-forward
  9665. (concat "<<" (regexp-quote s0) ">>") nil t)
  9666. (setq type 'dedicated
  9667. pos (match-beginning 0))))
  9668. ;; There is an exact target for this
  9669. (goto-char pos))
  9670. ((save-excursion
  9671. (goto-char (point-min))
  9672. (and
  9673. (re-search-forward
  9674. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9675. (setq type 'dedicated pos (match-beginning 0))))
  9676. ;; Found an element with a matching #+name affiliated keyword.
  9677. (goto-char pos))
  9678. ((and (string-match "^(\\(.*\\))$" s0)
  9679. (save-excursion
  9680. (goto-char (point-min))
  9681. (and
  9682. (re-search-forward
  9683. (concat "[^[]" (regexp-quote
  9684. (format org-coderef-label-format
  9685. (match-string 1 s0))))
  9686. nil t)
  9687. (setq type 'dedicated
  9688. pos (1+ (match-beginning 0))))))
  9689. ;; There is a coderef target for this
  9690. (goto-char pos))
  9691. ((string-match "^/\\(.*\\)/$" s)
  9692. ;; A regular expression
  9693. (cond
  9694. ((derived-mode-p 'org-mode)
  9695. (org-occur (match-string 1 s)))
  9696. (t (org-do-occur (match-string 1 s)))))
  9697. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9698. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9699. (goto-char (point-min))
  9700. (cond
  9701. ((let (case-fold-search)
  9702. (re-search-forward (format org-complex-heading-regexp-format
  9703. (regexp-quote s))
  9704. nil t))
  9705. ;; OK, found a match
  9706. (setq type 'dedicated)
  9707. (goto-char (match-beginning 0)))
  9708. ((and (not org-link-search-inhibit-query)
  9709. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9710. (y-or-n-p "No match - create this as a new heading? "))
  9711. (goto-char (point-max))
  9712. (or (bolp) (newline))
  9713. (insert "* " s "\n")
  9714. (beginning-of-line 0))
  9715. (t
  9716. (goto-char pos)
  9717. (error "No match"))))
  9718. (t
  9719. ;; A normal search string
  9720. (when (equal (string-to-char s) ?*)
  9721. ;; Anchor on headlines, post may include tags.
  9722. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9723. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9724. s (substring s 1)))
  9725. (remove-text-properties
  9726. 0 (length s)
  9727. '(face nil mouse-face nil keymap nil fontified nil) s)
  9728. ;; Make a series of regular expressions to find a match
  9729. (setq words (org-split-string s "[ \n\r\t]+")
  9730. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9731. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9732. "\\)" markers)
  9733. re2a_ (concat "\\(" (mapconcat 'downcase words
  9734. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9735. re2a (concat "[ \t\r\n]" re2a_)
  9736. re4_ (concat "\\(" (mapconcat 'downcase words
  9737. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9738. re4 (concat "[^a-zA-Z_]" re4_)
  9739. re1 (concat pre re2 post)
  9740. re3 (concat pre (if pre re4_ re4) post)
  9741. re5 (concat pre ".*" re4)
  9742. re2 (concat pre re2)
  9743. re2a (concat pre (if pre re2a_ re2a))
  9744. re4 (concat pre (if pre re4_ re4))
  9745. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9746. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9747. re5 "\\)"))
  9748. (cond
  9749. ((eq type 'org-occur) (org-occur reall))
  9750. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9751. (t (goto-char (point-min))
  9752. (setq type 'fuzzy)
  9753. (if (or (and (org-search-not-self 1 re0 nil t)
  9754. (setq type 'dedicated))
  9755. (org-search-not-self 1 re1 nil t)
  9756. (org-search-not-self 1 re2 nil t)
  9757. (org-search-not-self 1 re2a nil t)
  9758. (org-search-not-self 1 re3 nil t)
  9759. (org-search-not-self 1 re4 nil t)
  9760. (org-search-not-self 1 re5 nil t))
  9761. (goto-char (match-beginning 1))
  9762. (goto-char pos)
  9763. (error "No match"))))))
  9764. (and (derived-mode-p 'org-mode)
  9765. (not stealth)
  9766. (org-show-context 'link-search))
  9767. type))
  9768. (defun org-search-not-self (group &rest args)
  9769. "Execute `re-search-forward', but only accept matches that do not
  9770. enclose the position of `org-open-link-marker'."
  9771. (let ((m org-open-link-marker))
  9772. (catch 'exit
  9773. (while (apply 're-search-forward args)
  9774. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9775. (goto-char (match-end group))
  9776. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9777. (> (match-beginning 0) (marker-position m))
  9778. (< (match-end 0) (marker-position m)))
  9779. (save-match-data
  9780. (or (not (org-in-regexp
  9781. org-bracket-link-analytic-regexp 1))
  9782. (not (match-end 4)) ; no description
  9783. (and (<= (match-beginning 4) (point))
  9784. (>= (match-end 4) (point))))))
  9785. (throw 'exit (point))))))))
  9786. (defun org-get-buffer-for-internal-link (buffer)
  9787. "Return a buffer to be used for displaying the link target of internal links."
  9788. (cond
  9789. ((not org-display-internal-link-with-indirect-buffer)
  9790. buffer)
  9791. ((string-match "(Clone)$" (buffer-name buffer))
  9792. (message "Buffer is already a clone, not making another one")
  9793. ;; we also do not modify visibility in this case
  9794. buffer)
  9795. (t ; make a new indirect buffer for displaying the link
  9796. (let* ((bn (buffer-name buffer))
  9797. (ibn (concat bn "(Clone)"))
  9798. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9799. (with-current-buffer ib (org-overview))
  9800. ib))))
  9801. (defun org-do-occur (regexp &optional cleanup)
  9802. "Call the Emacs command `occur'.
  9803. If CLEANUP is non-nil, remove the printout of the regular expression
  9804. in the *Occur* buffer. This is useful if the regex is long and not useful
  9805. to read."
  9806. (occur regexp)
  9807. (when cleanup
  9808. (let ((cwin (selected-window)) win beg end)
  9809. (when (setq win (get-buffer-window "*Occur*"))
  9810. (select-window win))
  9811. (goto-char (point-min))
  9812. (when (re-search-forward "match[a-z]+" nil t)
  9813. (setq beg (match-end 0))
  9814. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9815. (setq end (1- (match-beginning 0)))))
  9816. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9817. (goto-char (point-min))
  9818. (select-window cwin))))
  9819. ;;; The mark ring for links jumps
  9820. (defvar org-mark-ring nil
  9821. "Mark ring for positions before jumps in Org-mode.")
  9822. (defvar org-mark-ring-last-goto nil
  9823. "Last position in the mark ring used to go back.")
  9824. ;; Fill and close the ring
  9825. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9826. (loop for i from 1 to org-mark-ring-length do
  9827. (push (make-marker) org-mark-ring))
  9828. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9829. org-mark-ring)
  9830. (defun org-mark-ring-push (&optional pos buffer)
  9831. "Put the current position or POS into the mark ring and rotate it."
  9832. (interactive)
  9833. (setq pos (or pos (point)))
  9834. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9835. (move-marker (car org-mark-ring)
  9836. (or pos (point))
  9837. (or buffer (current-buffer)))
  9838. (message "%s"
  9839. (substitute-command-keys
  9840. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9841. (defun org-mark-ring-goto (&optional n)
  9842. "Jump to the previous position in the mark ring.
  9843. With prefix arg N, jump back that many stored positions. When
  9844. called several times in succession, walk through the entire ring.
  9845. Org-mode commands jumping to a different position in the current file,
  9846. or to another Org-mode file, automatically push the old position
  9847. onto the ring."
  9848. (interactive "p")
  9849. (let (p m)
  9850. (if (eq last-command this-command)
  9851. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9852. (setq p org-mark-ring))
  9853. (setq org-mark-ring-last-goto p)
  9854. (setq m (car p))
  9855. (org-pop-to-buffer-same-window (marker-buffer m))
  9856. (goto-char m)
  9857. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9858. (defun org-remove-angle-brackets (s)
  9859. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9860. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9861. s)
  9862. (defun org-add-angle-brackets (s)
  9863. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9864. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9865. s)
  9866. (defun org-remove-double-quotes (s)
  9867. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9868. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9869. s)
  9870. ;;; Following specific links
  9871. (defun org-follow-timestamp-link ()
  9872. "Open an agenda view for the time-stamp date/range at point."
  9873. (cond
  9874. ((org-at-date-range-p t)
  9875. (let ((org-agenda-start-on-weekday)
  9876. (t1 (match-string 1))
  9877. (t2 (match-string 2)) tt1 tt2)
  9878. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9879. tt2 (time-to-days (org-time-string-to-time t2)))
  9880. (let ((org-agenda-buffer-tmp-name
  9881. (format "*Org Agenda(a:%s)"
  9882. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9883. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9884. ((org-at-timestamp-p t)
  9885. (let ((org-agenda-buffer-tmp-name
  9886. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9887. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9888. (substring (match-string 1) 0 10)))
  9889. 1)))
  9890. (t (error "This should not happen"))))
  9891. ;;; Following file links
  9892. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9893. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9894. (declare-function mailcap-mime-info
  9895. "mailcap" (string &optional request no-decode))
  9896. (defvar org-wait nil)
  9897. (defun org-open-file (path &optional in-emacs line search)
  9898. "Open the file at PATH.
  9899. First, this expands any special file name abbreviations. Then the
  9900. configuration variable `org-file-apps' is checked if it contains an
  9901. entry for this file type, and if yes, the corresponding command is launched.
  9902. If no application is found, Emacs simply visits the file.
  9903. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9904. With a double \\[universal-argument] \\[universal-argument] \
  9905. prefix arg, Org tries to avoid opening in Emacs
  9906. and to use an external application to visit the file.
  9907. Optional LINE specifies a line to go to, optional SEARCH a string
  9908. to search for. If LINE or SEARCH is given, the file will be
  9909. opened in Emacs, unless an entry from org-file-apps that makes
  9910. use of groups in a regexp matches.
  9911. If you want to change the way frames are used when following a
  9912. link, please customize `org-link-frame-setup'.
  9913. If the file does not exist, an error is thrown."
  9914. (let* ((file (if (equal path "")
  9915. buffer-file-name
  9916. (substitute-in-file-name (expand-file-name path))))
  9917. (file-apps (append org-file-apps (org-default-apps)))
  9918. (apps (org-remove-if
  9919. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9920. (apps-dlink (org-remove-if-not
  9921. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9922. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9923. (dirp (if remp nil (file-directory-p file)))
  9924. (file (if (and dirp org-open-directory-means-index-dot-org)
  9925. (concat (file-name-as-directory file) "index.org")
  9926. file))
  9927. (a-m-a-p (assq 'auto-mode apps))
  9928. (dfile (downcase file))
  9929. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9930. (link (cond ((and (eq line nil)
  9931. (eq search nil))
  9932. file)
  9933. (line
  9934. (concat file "::" (number-to-string line)))
  9935. (search
  9936. (concat file "::" search))))
  9937. (dlink (downcase link))
  9938. (old-buffer (current-buffer))
  9939. (old-pos (point))
  9940. (old-mode major-mode)
  9941. ext cmd link-match-data)
  9942. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9943. (setq ext (match-string 1 dfile))
  9944. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9945. (setq ext (match-string 1 dfile))))
  9946. (cond
  9947. ((member in-emacs '((16) system))
  9948. (setq cmd (cdr (assoc 'system apps))))
  9949. (in-emacs (setq cmd 'emacs))
  9950. (t
  9951. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9952. (and dirp (cdr (assoc 'directory apps)))
  9953. ; first, try matching against apps-dlink
  9954. ; if we get a match here, store the match data for later
  9955. (let ((match (assoc-default dlink apps-dlink
  9956. 'string-match)))
  9957. (if match
  9958. (progn (setq link-match-data (match-data))
  9959. match)
  9960. (progn (setq in-emacs (or in-emacs line search))
  9961. nil))) ; if we have no match in apps-dlink,
  9962. ; always open the file in emacs if line or search
  9963. ; is given (for backwards compatibility)
  9964. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9965. 'string-match)
  9966. (cdr (assoc ext apps))
  9967. (cdr (assoc t apps))))))
  9968. (when (eq cmd 'system)
  9969. (setq cmd (cdr (assoc 'system apps))))
  9970. (when (eq cmd 'default)
  9971. (setq cmd (cdr (assoc t apps))))
  9972. (when (eq cmd 'mailcap)
  9973. (require 'mailcap)
  9974. (mailcap-parse-mailcaps)
  9975. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9976. (command (mailcap-mime-info mime-type)))
  9977. (if (stringp command)
  9978. (setq cmd command)
  9979. (setq cmd 'emacs))))
  9980. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9981. (not (file-exists-p file))
  9982. (not org-open-non-existing-files))
  9983. (user-error "No such file: %s" file))
  9984. (cond
  9985. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9986. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9987. (while (string-match "['\"]%s['\"]" cmd)
  9988. (setq cmd (replace-match "%s" t t cmd)))
  9989. (while (string-match "%s" cmd)
  9990. (setq cmd (replace-match
  9991. (save-match-data
  9992. (shell-quote-argument
  9993. (convert-standard-filename file)))
  9994. t t cmd)))
  9995. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9996. (save-match-data
  9997. (let ((match-index 1)
  9998. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9999. (set-match-data link-match-data)
  10000. (while (<= match-index number-of-groups)
  10001. (let ((regex (concat "%" (number-to-string match-index)))
  10002. (replace-with (match-string match-index dlink)))
  10003. (while (string-match regex cmd)
  10004. (setq cmd (replace-match replace-with t t cmd))))
  10005. (setq match-index (+ match-index 1)))))
  10006. (save-window-excursion
  10007. (message "Running %s...done" cmd)
  10008. (start-process-shell-command cmd nil cmd)
  10009. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10010. ((or (stringp cmd)
  10011. (eq cmd 'emacs))
  10012. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10013. (widen)
  10014. (if line (org-goto-line line)
  10015. (if search (org-link-search search))))
  10016. ((consp cmd)
  10017. (let ((file (convert-standard-filename file)))
  10018. (save-match-data
  10019. (set-match-data link-match-data)
  10020. (eval cmd))))
  10021. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10022. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10023. (or (not (equal old-buffer (current-buffer)))
  10024. (not (equal old-pos (point))))
  10025. (org-mark-ring-push old-pos old-buffer))))
  10026. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10027. "This function returns non-nil if `entry' uses a regular
  10028. expression which should be matched against the whole link by
  10029. org-open-file.
  10030. It assumes that is the case when the entry uses a regular
  10031. expression which has at least one grouping construct and the
  10032. action is either a lisp form or a command string containing
  10033. '%1', i.e. using at least one subexpression match as a
  10034. parameter."
  10035. (let ((selector (car entry))
  10036. (action (cdr entry)))
  10037. (if (stringp selector)
  10038. (and (> (regexp-opt-depth selector) 0)
  10039. (or (and (stringp action)
  10040. (string-match "%[0-9]" action))
  10041. (consp action)))
  10042. nil)))
  10043. (defun org-default-apps ()
  10044. "Return the default applications for this operating system."
  10045. (cond
  10046. ((eq system-type 'darwin)
  10047. org-file-apps-defaults-macosx)
  10048. ((eq system-type 'windows-nt)
  10049. org-file-apps-defaults-windowsnt)
  10050. (t org-file-apps-defaults-gnu)))
  10051. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10052. "Convert extensions to regular expressions in the cars of LIST.
  10053. Also, weed out any non-string entries, because the return value is used
  10054. only for regexp matching.
  10055. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10056. point to the symbol `emacs', indicating that the file should
  10057. be opened in Emacs."
  10058. (append
  10059. (delq nil
  10060. (mapcar (lambda (x)
  10061. (if (not (stringp (car x)))
  10062. nil
  10063. (if (string-match "\\W" (car x))
  10064. x
  10065. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10066. list))
  10067. (if add-auto-mode
  10068. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10069. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10070. (defun org-file-remote-p (file)
  10071. "Test whether FILE specifies a location on a remote system.
  10072. Return non-nil if the location is indeed remote.
  10073. For example, the filename \"/user@host:/foo\" specifies a location
  10074. on the system \"/user@host:\"."
  10075. (cond ((fboundp 'file-remote-p)
  10076. (file-remote-p file))
  10077. ((fboundp 'tramp-handle-file-remote-p)
  10078. (tramp-handle-file-remote-p file))
  10079. ((and (boundp 'ange-ftp-name-format)
  10080. (string-match (car ange-ftp-name-format) file))
  10081. t)))
  10082. ;;;; Refiling
  10083. (defun org-get-org-file ()
  10084. "Read a filename, with default directory `org-directory'."
  10085. (let ((default (or org-default-notes-file remember-data-file)))
  10086. (read-file-name (format "File name [%s]: " default)
  10087. (file-name-as-directory org-directory)
  10088. default)))
  10089. (defun org-notes-order-reversed-p ()
  10090. "Check if the current file should receive notes in reversed order."
  10091. (cond
  10092. ((not org-reverse-note-order) nil)
  10093. ((eq t org-reverse-note-order) t)
  10094. ((not (listp org-reverse-note-order)) nil)
  10095. (t (catch 'exit
  10096. (let ((all org-reverse-note-order)
  10097. entry)
  10098. (while (setq entry (pop all))
  10099. (if (string-match (car entry) buffer-file-name)
  10100. (throw 'exit (cdr entry))))
  10101. nil)))))
  10102. (defvar org-refile-target-table nil
  10103. "The list of refile targets, created by `org-refile'.")
  10104. (defvar org-agenda-new-buffers nil
  10105. "Buffers created to visit agenda files.")
  10106. (defvar org-refile-cache nil
  10107. "Cache for refile targets.")
  10108. (defvar org-refile-markers nil
  10109. "All the markers used for caching refile locations.")
  10110. (defun org-refile-marker (pos)
  10111. "Get a new refile marker, but only if caching is in use."
  10112. (if (not org-refile-use-cache)
  10113. pos
  10114. (let ((m (make-marker)))
  10115. (move-marker m pos)
  10116. (push m org-refile-markers)
  10117. m)))
  10118. (defun org-refile-cache-clear ()
  10119. "Clear the refile cache and disable all the markers."
  10120. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10121. (setq org-refile-markers nil)
  10122. (setq org-refile-cache nil)
  10123. (message "Refile cache has been cleared"))
  10124. (defun org-refile-cache-check-set (set)
  10125. "Check if all the markers in the cache still have live buffers."
  10126. (let (marker)
  10127. (catch 'exit
  10128. (while (and set (setq marker (nth 3 (pop set))))
  10129. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10130. (when (and marker (null (marker-buffer marker)))
  10131. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10132. (sit-for 3)
  10133. (throw 'exit nil)))
  10134. t)))
  10135. (defun org-refile-cache-put (set &rest identifiers)
  10136. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10137. (let* ((key (sha1 (prin1-to-string identifiers)))
  10138. (entry (assoc key org-refile-cache)))
  10139. (if entry
  10140. (setcdr entry set)
  10141. (push (cons key set) org-refile-cache))))
  10142. (defun org-refile-cache-get (&rest identifiers)
  10143. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10144. (cond
  10145. ((not org-refile-cache) nil)
  10146. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10147. (t
  10148. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10149. org-refile-cache))))
  10150. (and set (org-refile-cache-check-set set) set)))))
  10151. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10152. "Produce a table with refile targets."
  10153. (let ((case-fold-search nil)
  10154. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10155. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10156. targets tgs txt re files f desc descre fast-path-p level pos0)
  10157. (message "Getting targets...")
  10158. (with-current-buffer (or default-buffer (current-buffer))
  10159. (while (setq entry (pop entries))
  10160. (setq files (car entry) desc (cdr entry))
  10161. (setq fast-path-p nil)
  10162. (cond
  10163. ((null files) (setq files (list (current-buffer))))
  10164. ((eq files 'org-agenda-files)
  10165. (setq files (org-agenda-files 'unrestricted)))
  10166. ((and (symbolp files) (fboundp files))
  10167. (setq files (funcall files)))
  10168. ((and (symbolp files) (boundp files))
  10169. (setq files (symbol-value files))))
  10170. (if (stringp files) (setq files (list files)))
  10171. (cond
  10172. ((eq (car desc) :tag)
  10173. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10174. ((eq (car desc) :todo)
  10175. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10176. ((eq (car desc) :regexp)
  10177. (setq descre (cdr desc)))
  10178. ((eq (car desc) :level)
  10179. (setq descre (concat "^\\*\\{" (number-to-string
  10180. (if org-odd-levels-only
  10181. (1- (* 2 (cdr desc)))
  10182. (cdr desc)))
  10183. "\\}[ \t]")))
  10184. ((eq (car desc) :maxlevel)
  10185. (setq fast-path-p t)
  10186. (setq descre (concat "^\\*\\{1," (number-to-string
  10187. (if org-odd-levels-only
  10188. (1- (* 2 (cdr desc)))
  10189. (cdr desc)))
  10190. "\\}[ \t]")))
  10191. (t (error "Bad refiling target description %s" desc)))
  10192. (while (setq f (pop files))
  10193. (with-current-buffer
  10194. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10195. (or
  10196. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10197. (progn
  10198. (if (bufferp f) (setq f (buffer-file-name
  10199. (buffer-base-buffer f))))
  10200. (setq f (and f (expand-file-name f)))
  10201. (if (eq org-refile-use-outline-path 'file)
  10202. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10203. (save-excursion
  10204. (save-restriction
  10205. (widen)
  10206. (goto-char (point-min))
  10207. (while (re-search-forward descre nil t)
  10208. (goto-char (setq pos0 (point-at-bol)))
  10209. (catch 'next
  10210. (when org-refile-target-verify-function
  10211. (save-match-data
  10212. (or (funcall org-refile-target-verify-function)
  10213. (throw 'next t))))
  10214. (when (and (looking-at org-complex-heading-regexp)
  10215. (not (member (match-string 4) excluded-entries))
  10216. (match-string 4))
  10217. (setq level (org-reduced-level
  10218. (- (match-end 1) (match-beginning 1)))
  10219. txt (org-link-display-format (match-string 4))
  10220. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10221. re (format org-complex-heading-regexp-format
  10222. (regexp-quote (match-string 4))))
  10223. (when org-refile-use-outline-path
  10224. (setq txt (mapconcat
  10225. 'org-protect-slash
  10226. (append
  10227. (if (eq org-refile-use-outline-path
  10228. 'file)
  10229. (list (file-name-nondirectory
  10230. (buffer-file-name
  10231. (buffer-base-buffer))))
  10232. (if (eq org-refile-use-outline-path
  10233. 'full-file-path)
  10234. (list (buffer-file-name
  10235. (buffer-base-buffer)))))
  10236. (org-get-outline-path fast-path-p
  10237. level txt)
  10238. (list txt))
  10239. "/")))
  10240. (push (list txt f re (org-refile-marker (point)))
  10241. tgs)))
  10242. (when (= (point) pos0)
  10243. ;; verification function has not moved point
  10244. (goto-char (point-at-eol))))))))
  10245. (when org-refile-use-cache
  10246. (org-refile-cache-put tgs (buffer-file-name) descre))
  10247. (setq targets (append tgs targets))))))
  10248. (message "Getting targets...done")
  10249. (nreverse targets)))
  10250. (defun org-protect-slash (s)
  10251. (while (string-match "/" s)
  10252. (setq s (replace-match "\\" t t s)))
  10253. s)
  10254. (defvar org-olpa (make-vector 20 nil))
  10255. (defun org-get-outline-path (&optional fastp level heading)
  10256. "Return the outline path to the current entry, as a list.
  10257. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10258. routine which makes outline path derivations for an entire file,
  10259. avoiding backtracing. Refile target collection makes use of that."
  10260. (if fastp
  10261. (progn
  10262. (if (> level 19)
  10263. (error "Outline path failure, more than 19 levels"))
  10264. (loop for i from level upto 19 do
  10265. (aset org-olpa i nil))
  10266. (prog1
  10267. (delq nil (append org-olpa nil))
  10268. (aset org-olpa level heading)))
  10269. (let (rtn case-fold-search)
  10270. (save-excursion
  10271. (save-restriction
  10272. (widen)
  10273. (while (org-up-heading-safe)
  10274. (when (looking-at org-complex-heading-regexp)
  10275. (push (org-trim
  10276. (replace-regexp-in-string
  10277. ;; Remove statistical/checkboxes cookies
  10278. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10279. (org-match-string-no-properties 4)))
  10280. rtn)))
  10281. rtn)))))
  10282. (defun org-format-outline-path (path &optional width prefix separator)
  10283. "Format the outline path PATH for display.
  10284. WIDTH is the maximum number of characters that is available.
  10285. PREFIX is a prefix to be included in the returned string,
  10286. such as the file name.
  10287. SEPARATOR is inserted between the different parts of the path,
  10288. the default is \"/\"."
  10289. (setq width (or width 79))
  10290. (if prefix (setq width (- width (length prefix))))
  10291. (if (not path)
  10292. (or prefix "")
  10293. (let* ((nsteps (length path))
  10294. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10295. (maxwidth (if (<= total-width width)
  10296. 10000 ;; everything fits
  10297. ;; we need to shorten the level headings
  10298. (/ (- width nsteps) nsteps)))
  10299. (org-odd-levels-only nil)
  10300. (n 0)
  10301. (total (1+ (length prefix))))
  10302. (setq maxwidth (max maxwidth 10))
  10303. (concat prefix
  10304. (if prefix (or separator "/"))
  10305. (mapconcat
  10306. (lambda (h)
  10307. (setq n (1+ n))
  10308. (if (and (= n nsteps) (< maxwidth 10000))
  10309. (setq maxwidth (- total-width total)))
  10310. (if (< (length h) maxwidth)
  10311. (progn (setq total (+ total (length h) 1)) h)
  10312. (setq h (substring h 0 (- maxwidth 2))
  10313. total (+ total maxwidth 1))
  10314. (if (string-match "[ \t]+\\'" h)
  10315. (setq h (substring h 0 (match-beginning 0))))
  10316. (setq h (concat h "..")))
  10317. (org-add-props h nil 'face
  10318. (nth (% (1- n) org-n-level-faces)
  10319. org-level-faces))
  10320. h)
  10321. path (or separator "/"))))))
  10322. (defun org-display-outline-path (&optional file current separator just-return-string)
  10323. "Display the current outline path in the echo area.
  10324. If FILE is non-nil, prepend the output with the file name.
  10325. If CURRENT is non-nil, append the current heading to the output.
  10326. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10327. the different parts of the path and defaults to \"/\".
  10328. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10329. (interactive "P")
  10330. (let* (case-fold-search
  10331. message-log-max ; Don't populate the *Messages* buffer
  10332. (bfn (buffer-file-name (buffer-base-buffer)))
  10333. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10334. res)
  10335. (if current (setq path (append path
  10336. (save-excursion
  10337. (org-back-to-heading t)
  10338. (if (looking-at org-complex-heading-regexp)
  10339. (list (match-string 4)))))))
  10340. (setq res
  10341. (org-format-outline-path
  10342. path
  10343. (1- (frame-width))
  10344. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10345. separator))
  10346. (if just-return-string
  10347. (org-no-properties res)
  10348. (message "%s" res))))
  10349. (defvar org-refile-history nil
  10350. "History for refiling operations.")
  10351. (defvar org-after-refile-insert-hook nil
  10352. "Hook run after `org-refile' has inserted its stuff at the new location.
  10353. Note that this is still *before* the stuff will be removed from
  10354. the *old* location.")
  10355. (defvar org-capture-last-stored-marker)
  10356. (defvar org-refile-keep nil
  10357. "Non-nil means `org-refile' will copy instead of refile.")
  10358. (defun org-copy ()
  10359. "Like `org-refile', but copy."
  10360. (interactive)
  10361. (let ((org-refile-keep t))
  10362. (funcall 'org-refile nil nil nil "Copy")))
  10363. (defun org-refile (&optional goto default-buffer rfloc msg)
  10364. "Move the entry or entries at point to another heading.
  10365. The list of target headings is compiled using the information in
  10366. `org-refile-targets', which see.
  10367. At the target location, the entry is filed as a subitem of the target
  10368. heading. Depending on `org-reverse-note-order', the new subitem will
  10369. either be the first or the last subitem.
  10370. If there is an active region, all entries in that region will be moved.
  10371. However, the region must fulfill the requirement that the first heading
  10372. is the first one sets the top-level of the moved text - at most siblings
  10373. below it are allowed.
  10374. With prefix arg GOTO, the command will only visit the target location
  10375. and not actually move anything.
  10376. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10377. go to the location where the last refiling operation has put the subtree.
  10378. With a prefix argument of `2', refile to the running clock.
  10379. RFLOC can be a refile location obtained in a different way.
  10380. MSG is a string to replace \"Refile\" in the default prompt with
  10381. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10382. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10383. If you are using target caching (see `org-refile-use-cache'),
  10384. you have to clear the target cache in order to find new targets.
  10385. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10386. prefix argument (`C-u C-u C-u C-c C-w')."
  10387. (interactive "P")
  10388. (if (member goto '(0 (64)))
  10389. (org-refile-cache-clear)
  10390. (let* ((actionmsg (or msg "Refile"))
  10391. (cbuf (current-buffer))
  10392. (regionp (org-region-active-p))
  10393. (region-start (and regionp (region-beginning)))
  10394. (region-end (and regionp (region-end)))
  10395. (region-length (and regionp (- region-end region-start)))
  10396. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10397. pos it nbuf file re level reversed)
  10398. (setq last-command nil)
  10399. (when regionp
  10400. (goto-char region-start)
  10401. (or (bolp) (goto-char (point-at-bol)))
  10402. (setq region-start (point))
  10403. (unless (or (org-kill-is-subtree-p
  10404. (buffer-substring region-start region-end))
  10405. (prog1 org-refile-active-region-within-subtree
  10406. (org-toggle-heading)))
  10407. (user-error "The region is not a (sequence of) subtree(s)")))
  10408. (if (equal goto '(16))
  10409. (org-refile-goto-last-stored)
  10410. (when (or
  10411. (and (equal goto 2)
  10412. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10413. (prog1
  10414. (setq it (list (or org-clock-heading "running clock")
  10415. (buffer-file-name
  10416. (marker-buffer org-clock-hd-marker))
  10417. ""
  10418. (marker-position org-clock-hd-marker)))
  10419. (setq goto nil)))
  10420. (setq it (or rfloc
  10421. (let (heading-text)
  10422. (save-excursion
  10423. (unless goto
  10424. (org-back-to-heading t)
  10425. (setq heading-text
  10426. (nth 4 (org-heading-components))))
  10427. (org-refile-get-location
  10428. (cond (goto "Goto")
  10429. (regionp (concat actionmsg " region to"))
  10430. (t (concat actionmsg " subtree \""
  10431. heading-text "\" to")))
  10432. default-buffer
  10433. (and (not (equal '(4) goto))
  10434. org-refile-allow-creating-parent-nodes)
  10435. goto))))))
  10436. (setq file (nth 1 it)
  10437. re (nth 2 it)
  10438. pos (nth 3 it))
  10439. (if (and (not goto)
  10440. pos
  10441. (equal (buffer-file-name) file)
  10442. (if regionp
  10443. (and (>= pos region-start)
  10444. (<= pos region-end))
  10445. (and (>= pos (point))
  10446. (< pos (save-excursion
  10447. (org-end-of-subtree t t))))))
  10448. (error "Cannot refile to position inside the tree or region"))
  10449. (setq nbuf (or (find-buffer-visiting file)
  10450. (find-file-noselect file)))
  10451. (if goto
  10452. (progn
  10453. (org-pop-to-buffer-same-window nbuf)
  10454. (goto-char pos)
  10455. (org-show-context 'org-goto))
  10456. (if regionp
  10457. (progn
  10458. (org-kill-new (buffer-substring region-start region-end))
  10459. (org-save-markers-in-region region-start region-end))
  10460. (org-copy-subtree 1 nil t))
  10461. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10462. (find-file-noselect file)))
  10463. (setq reversed (org-notes-order-reversed-p))
  10464. (save-excursion
  10465. (save-restriction
  10466. (widen)
  10467. (if pos
  10468. (progn
  10469. (goto-char pos)
  10470. (looking-at org-outline-regexp)
  10471. (setq level (org-get-valid-level (funcall outline-level) 1))
  10472. (goto-char
  10473. (if reversed
  10474. (or (outline-next-heading) (point-max))
  10475. (or (save-excursion (org-get-next-sibling))
  10476. (org-end-of-subtree t t)
  10477. (point-max)))))
  10478. (setq level 1)
  10479. (if (not reversed)
  10480. (goto-char (point-max))
  10481. (goto-char (point-min))
  10482. (or (outline-next-heading) (goto-char (point-max)))))
  10483. (if (not (bolp)) (newline))
  10484. (org-paste-subtree level)
  10485. (when org-log-refile
  10486. (org-add-log-setup 'refile nil nil 'findpos
  10487. org-log-refile)
  10488. (unless (eq org-log-refile 'note)
  10489. (save-excursion (org-add-log-note))))
  10490. (and org-auto-align-tags
  10491. (let ((org-loop-over-headlines-in-active-region nil))
  10492. (org-set-tags nil t)))
  10493. (with-demoted-errors
  10494. (bookmark-set "org-refile-last-stored"))
  10495. ;; If we are refiling for capture, make sure that the
  10496. ;; last-capture pointers point here
  10497. (when (org-bound-and-true-p org-refile-for-capture)
  10498. (with-demoted-errors
  10499. (bookmark-set "org-capture-last-stored-marker"))
  10500. (move-marker org-capture-last-stored-marker (point)))
  10501. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10502. (run-hooks 'org-after-refile-insert-hook))))
  10503. (unless org-refile-keep
  10504. (if regionp
  10505. (delete-region (point) (+ (point) region-length))
  10506. (org-cut-subtree)))
  10507. (when (featurep 'org-inlinetask)
  10508. (org-inlinetask-remove-END-maybe))
  10509. (setq org-markers-to-move nil)
  10510. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10511. (defun org-refile-goto-last-stored ()
  10512. "Go to the location where the last refile was stored."
  10513. (interactive)
  10514. (bookmark-jump "org-refile-last-stored")
  10515. (message "This is the location of the last refile"))
  10516. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10517. no-exclude)
  10518. "Prompt the user for a refile location, using PROMPT.
  10519. PROMPT should not be suffixed with a colon and a space, because
  10520. this function appends the default value from
  10521. `org-refile-history' automatically, if that is not empty.
  10522. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10523. this is used for the GOTO interface."
  10524. (let ((org-refile-targets org-refile-targets)
  10525. (org-refile-use-outline-path org-refile-use-outline-path)
  10526. excluded-entries)
  10527. (when (and (derived-mode-p 'org-mode)
  10528. (not org-refile-use-cache)
  10529. (not no-exclude))
  10530. (org-map-tree
  10531. (lambda()
  10532. (setq excluded-entries
  10533. (append excluded-entries (list (org-get-heading t t)))))))
  10534. (setq org-refile-target-table
  10535. (org-refile-get-targets default-buffer excluded-entries)))
  10536. (unless org-refile-target-table
  10537. (user-error "No refile targets"))
  10538. (let* ((cbuf (current-buffer))
  10539. (partial-completion-mode nil)
  10540. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10541. (cfunc (if (and org-refile-use-outline-path
  10542. org-outline-path-complete-in-steps)
  10543. 'org-olpath-completing-read
  10544. 'org-icompleting-read))
  10545. (extra (if org-refile-use-outline-path "/" ""))
  10546. (cbnex (concat (buffer-name) extra))
  10547. (filename (and cfn (expand-file-name cfn)))
  10548. (tbl (mapcar
  10549. (lambda (x)
  10550. (if (and (not (member org-refile-use-outline-path
  10551. '(file full-file-path)))
  10552. (not (equal filename (nth 1 x))))
  10553. (cons (concat (car x) extra " ("
  10554. (file-name-nondirectory (nth 1 x)) ")")
  10555. (cdr x))
  10556. (cons (concat (car x) extra) (cdr x))))
  10557. org-refile-target-table))
  10558. (completion-ignore-case t)
  10559. cdef
  10560. (prompt (concat prompt
  10561. (or (and (car org-refile-history)
  10562. (concat " (default " (car org-refile-history) ")"))
  10563. (and (assoc cbnex tbl) (setq cdef cbnex)
  10564. (concat " (default " cbnex ")"))) ": "))
  10565. pa answ parent-target child parent old-hist)
  10566. (setq old-hist org-refile-history)
  10567. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10568. nil 'org-refile-history (or cdef (car org-refile-history))))
  10569. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10570. (org-refile-check-position pa)
  10571. (if pa
  10572. (progn
  10573. (when (or (not org-refile-history)
  10574. (not (eq old-hist org-refile-history))
  10575. (not (equal (car pa) (car org-refile-history))))
  10576. (setq org-refile-history
  10577. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10578. org-refile-history
  10579. (cdr org-refile-history))))
  10580. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10581. (pop org-refile-history)))
  10582. pa)
  10583. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10584. (progn
  10585. (setq parent (match-string 1 answ)
  10586. child (match-string 2 answ))
  10587. (setq parent-target (or (assoc parent tbl)
  10588. (assoc (concat parent "/") tbl)))
  10589. (when (and parent-target
  10590. (or (eq new-nodes t)
  10591. (and (eq new-nodes 'confirm)
  10592. (y-or-n-p (format "Create new node \"%s\"? "
  10593. child)))))
  10594. (org-refile-new-child parent-target child)))
  10595. (user-error "Invalid target location")))))
  10596. (declare-function org-string-nw-p "org-macs" (s))
  10597. (defun org-refile-check-position (refile-pointer)
  10598. "Check if the refile pointer matches the headline to which it points."
  10599. (let* ((file (nth 1 refile-pointer))
  10600. (re (nth 2 refile-pointer))
  10601. (pos (nth 3 refile-pointer))
  10602. buffer)
  10603. (if (and (not (markerp pos)) (not file))
  10604. (user-error "Please save the buffer to a file before refiling")
  10605. (when (org-string-nw-p re)
  10606. (setq buffer (if (markerp pos)
  10607. (marker-buffer pos)
  10608. (or (find-buffer-visiting file)
  10609. (find-file-noselect file))))
  10610. (with-current-buffer buffer
  10611. (save-excursion
  10612. (save-restriction
  10613. (widen)
  10614. (goto-char pos)
  10615. (beginning-of-line 1)
  10616. (unless (org-looking-at-p re)
  10617. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10618. (defun org-refile-new-child (parent-target child)
  10619. "Use refile target PARENT-TARGET to add new CHILD below it."
  10620. (unless parent-target
  10621. (error "Cannot find parent for new node"))
  10622. (let ((file (nth 1 parent-target))
  10623. (pos (nth 3 parent-target))
  10624. level)
  10625. (with-current-buffer (or (find-buffer-visiting file)
  10626. (find-file-noselect file))
  10627. (save-excursion
  10628. (save-restriction
  10629. (widen)
  10630. (if pos
  10631. (goto-char pos)
  10632. (goto-char (point-max))
  10633. (if (not (bolp)) (newline)))
  10634. (when (looking-at org-outline-regexp)
  10635. (setq level (funcall outline-level))
  10636. (org-end-of-subtree t t))
  10637. (org-back-over-empty-lines)
  10638. (insert "\n" (make-string
  10639. (if pos (org-get-valid-level level 1) 1) ?*)
  10640. " " child "\n")
  10641. (beginning-of-line 0)
  10642. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10643. (defun org-olpath-completing-read (prompt collection &rest args)
  10644. "Read an outline path like a file name."
  10645. (let ((thetable collection)
  10646. (org-completion-use-ido nil) ; does not work with ido.
  10647. (org-completion-use-iswitchb nil)) ; or iswitchb
  10648. (apply
  10649. 'org-icompleting-read prompt
  10650. (lambda (string predicate &optional flag)
  10651. (let (rtn r f (l (length string)))
  10652. (cond
  10653. ((eq flag nil)
  10654. ;; try completion
  10655. (try-completion string thetable))
  10656. ((eq flag t)
  10657. ;; all-completions
  10658. (setq rtn (all-completions string thetable predicate))
  10659. (mapcar
  10660. (lambda (x)
  10661. (setq r (substring x l))
  10662. (if (string-match " ([^)]*)$" x)
  10663. (setq f (match-string 0 x))
  10664. (setq f ""))
  10665. (if (string-match "/" r)
  10666. (concat string (substring r 0 (match-end 0)) f)
  10667. x))
  10668. rtn))
  10669. ((eq flag 'lambda)
  10670. ;; exact match?
  10671. (assoc string thetable)))))
  10672. args)))
  10673. ;;;; Dynamic blocks
  10674. (defun org-find-dblock (name)
  10675. "Find the first dynamic block with name NAME in the buffer.
  10676. If not found, stay at current position and return nil."
  10677. (let ((case-fold-search t) pos)
  10678. (save-excursion
  10679. (goto-char (point-min))
  10680. (setq pos (and (re-search-forward
  10681. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10682. (match-beginning 0))))
  10683. (if pos (goto-char pos))
  10684. pos))
  10685. (defconst org-dblock-start-re
  10686. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10687. "Matches the start line of a dynamic block, with parameters.")
  10688. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10689. "Matches the end of a dynamic block.")
  10690. (defun org-create-dblock (plist)
  10691. "Create a dynamic block section, with parameters taken from PLIST.
  10692. PLIST must contain a :name entry which is used as name of the block."
  10693. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10694. (end-of-line 1)
  10695. (newline))
  10696. (let ((col (current-column))
  10697. (name (plist-get plist :name)))
  10698. (insert "#+BEGIN: " name)
  10699. (while plist
  10700. (if (eq (car plist) :name)
  10701. (setq plist (cddr plist))
  10702. (insert " " (prin1-to-string (pop plist)))))
  10703. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10704. (beginning-of-line -2)))
  10705. (defun org-prepare-dblock ()
  10706. "Prepare dynamic block for refresh.
  10707. This empties the block, puts the cursor at the insert position and returns
  10708. the property list including an extra property :name with the block name."
  10709. (unless (looking-at org-dblock-start-re)
  10710. (user-error "Not at a dynamic block"))
  10711. (let* ((begdel (1+ (match-end 0)))
  10712. (name (org-no-properties (match-string 1)))
  10713. (params (append (list :name name)
  10714. (read (concat "(" (match-string 3) ")")))))
  10715. (save-excursion
  10716. (beginning-of-line 1)
  10717. (skip-chars-forward " \t")
  10718. (setq params (plist-put params :indentation-column (current-column))))
  10719. (unless (re-search-forward org-dblock-end-re nil t)
  10720. (error "Dynamic block not terminated"))
  10721. (setq params
  10722. (append params
  10723. (list :content (buffer-substring
  10724. begdel (match-beginning 0)))))
  10725. (delete-region begdel (match-beginning 0))
  10726. (goto-char begdel)
  10727. (open-line 1)
  10728. params))
  10729. (defun org-map-dblocks (&optional command)
  10730. "Apply COMMAND to all dynamic blocks in the current buffer.
  10731. If COMMAND is not given, use `org-update-dblock'."
  10732. (let ((cmd (or command 'org-update-dblock)))
  10733. (save-excursion
  10734. (goto-char (point-min))
  10735. (while (re-search-forward org-dblock-start-re nil t)
  10736. (goto-char (match-beginning 0))
  10737. (save-excursion
  10738. (condition-case nil
  10739. (funcall cmd)
  10740. (error (message "Error during update of dynamic block"))))
  10741. (unless (re-search-forward org-dblock-end-re nil t)
  10742. (error "Dynamic block not terminated"))))))
  10743. (defun org-dblock-update (&optional arg)
  10744. "User command for updating dynamic blocks.
  10745. Update the dynamic block at point. With prefix ARG, update all dynamic
  10746. blocks in the buffer."
  10747. (interactive "P")
  10748. (if arg
  10749. (org-update-all-dblocks)
  10750. (or (looking-at org-dblock-start-re)
  10751. (org-beginning-of-dblock))
  10752. (org-update-dblock)))
  10753. (defun org-update-dblock ()
  10754. "Update the dynamic block at point.
  10755. This means to empty the block, parse for parameters and then call
  10756. the correct writing function."
  10757. (interactive)
  10758. (save-window-excursion
  10759. (let* ((pos (point))
  10760. (line (org-current-line))
  10761. (params (org-prepare-dblock))
  10762. (name (plist-get params :name))
  10763. (indent (plist-get params :indentation-column))
  10764. (cmd (intern (concat "org-dblock-write:" name))))
  10765. (message "Updating dynamic block `%s' at line %d..." name line)
  10766. (funcall cmd params)
  10767. (message "Updating dynamic block `%s' at line %d...done" name line)
  10768. (goto-char pos)
  10769. (when (and indent (> indent 0))
  10770. (setq indent (make-string indent ?\ ))
  10771. (save-excursion
  10772. (org-beginning-of-dblock)
  10773. (forward-line 1)
  10774. (while (not (looking-at org-dblock-end-re))
  10775. (insert indent)
  10776. (beginning-of-line 2))
  10777. (when (looking-at org-dblock-end-re)
  10778. (and (looking-at "[ \t]+")
  10779. (replace-match ""))
  10780. (insert indent)))))))
  10781. (defun org-beginning-of-dblock ()
  10782. "Find the beginning of the dynamic block at point.
  10783. Error if there is no such block at point."
  10784. (let ((pos (point))
  10785. beg)
  10786. (end-of-line 1)
  10787. (if (and (re-search-backward org-dblock-start-re nil t)
  10788. (setq beg (match-beginning 0))
  10789. (re-search-forward org-dblock-end-re nil t)
  10790. (> (match-end 0) pos))
  10791. (goto-char beg)
  10792. (goto-char pos)
  10793. (error "Not in a dynamic block"))))
  10794. (defun org-update-all-dblocks ()
  10795. "Update all dynamic blocks in the buffer.
  10796. This function can be used in a hook."
  10797. (interactive)
  10798. (when (derived-mode-p 'org-mode)
  10799. (org-map-dblocks 'org-update-dblock)))
  10800. ;;;; Completion
  10801. (defun org-get-export-keywords ()
  10802. "Return a list of all currently understood export keywords.
  10803. Export keywords include options, block names, attributes and
  10804. keywords relative to each registered export back-end."
  10805. (delq nil
  10806. (let (keywords)
  10807. (mapc
  10808. (lambda (back-end)
  10809. (let ((props (cdr back-end)))
  10810. ;; Back-end name (for keywords, like #+LATEX:)
  10811. (push (upcase (symbol-name (car back-end))) keywords)
  10812. ;; Back-end options.
  10813. (mapc (lambda (option) (push (cadr option) keywords))
  10814. (plist-get (cdr back-end) :options-alist))))
  10815. (org-bound-and-true-p org-export-registered-backends))
  10816. keywords)))
  10817. (defconst org-options-keywords
  10818. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10819. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10820. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10821. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10822. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10823. (defcustom org-structure-template-alist
  10824. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10825. "<src lang=\"?\">\n\n</src>")
  10826. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10827. "<example>\n?\n</example>")
  10828. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10829. "<quote>\n?\n</quote>")
  10830. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10831. "<verse>\n?\n</verse>")
  10832. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10833. "<verbatim>\n?\n</verbatim>")
  10834. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10835. "<center>\n?\n</center>")
  10836. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10837. "<literal style=\"latex\">\n?\n</literal>")
  10838. ("L" "#+LaTeX: "
  10839. "<literal style=\"latex\">?</literal>")
  10840. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10841. "<literal style=\"html\">\n?\n</literal>")
  10842. ("H" "#+HTML: "
  10843. "<literal style=\"html\">?</literal>")
  10844. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10845. ("A" "#+ASCII: ")
  10846. ("i" "#+INDEX: ?"
  10847. "#+INDEX: ?")
  10848. ("I" "#+INCLUDE: %file ?"
  10849. "<include file=%file markup=\"?\">"))
  10850. "Structure completion elements.
  10851. This is a list of abbreviation keys and values. The value gets inserted
  10852. if you type `<' followed by the key and then press the completion key,
  10853. usually `M-TAB'. %file will be replaced by a file name after prompting
  10854. for the file using completion. The cursor will be placed at the position
  10855. of the `?` in the template.
  10856. There are two templates for each key, the first uses the original Org syntax,
  10857. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10858. the default when the /org-mtags.el/ module has been loaded. See also the
  10859. variable `org-mtags-prefer-muse-templates'."
  10860. :group 'org-completion
  10861. :type '(repeat
  10862. (string :tag "Key")
  10863. (string :tag "Template")
  10864. (string :tag "Muse Template")))
  10865. (defun org-try-structure-completion ()
  10866. "Try to complete a structure template before point.
  10867. This looks for strings like \"<e\" on an otherwise empty line and
  10868. expands them."
  10869. (let ((l (buffer-substring (point-at-bol) (point)))
  10870. a)
  10871. (when (and (looking-at "[ \t]*$")
  10872. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10873. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10874. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10875. (match-beginning 1)) a)
  10876. t)))
  10877. (defun org-complete-expand-structure-template (start cell)
  10878. "Expand a structure template."
  10879. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10880. (rpl (nth (if musep 2 1) cell))
  10881. (ind ""))
  10882. (delete-region start (point))
  10883. (when (string-match "\\`#\\+" rpl)
  10884. (cond
  10885. ((bolp))
  10886. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10887. (setq ind (buffer-substring (point-at-bol) (point))))
  10888. (t (newline))))
  10889. (setq start (point))
  10890. (if (string-match "%file" rpl)
  10891. (setq rpl (replace-match
  10892. (concat
  10893. "\""
  10894. (save-match-data
  10895. (abbreviate-file-name (read-file-name "Include file: ")))
  10896. "\"")
  10897. t t rpl)))
  10898. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10899. (concat "\n" ind)))
  10900. (insert rpl)
  10901. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10902. ;;;; TODO, DEADLINE, Comments
  10903. (defun org-toggle-comment ()
  10904. "Change the COMMENT state of an entry."
  10905. (interactive)
  10906. (save-excursion
  10907. (org-back-to-heading)
  10908. (let (case-fold-search)
  10909. (cond
  10910. ((looking-at (format org-heading-keyword-regexp-format
  10911. org-comment-string))
  10912. (goto-char (match-end 1))
  10913. (looking-at (concat " +" org-comment-string))
  10914. (replace-match "" t t)
  10915. (when (eolp) (insert " ")))
  10916. ((looking-at org-outline-regexp)
  10917. (goto-char (match-end 0))
  10918. (insert org-comment-string " "))))))
  10919. (defvar org-last-todo-state-is-todo nil
  10920. "This is non-nil when the last TODO state change led to a TODO state.
  10921. If the last change removed the TODO tag or switched to DONE, then
  10922. this is nil.")
  10923. (defvar org-setting-tags nil) ; dynamically skipped
  10924. (defvar org-todo-setup-filter-hook nil
  10925. "Hook for functions that pre-filter todo specs.
  10926. Each function takes a todo spec and returns either nil or the spec
  10927. transformed into canonical form." )
  10928. (defvar org-todo-get-default-hook nil
  10929. "Hook for functions that get a default item for todo.
  10930. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10931. nil or a string to be used for the todo mark." )
  10932. (defvar org-agenda-headline-snapshot-before-repeat)
  10933. (defun org-current-effective-time ()
  10934. "Return current time adjusted for `org-extend-today-until' variable."
  10935. (let* ((ct (org-current-time))
  10936. (dct (decode-time ct))
  10937. (ct1
  10938. (cond
  10939. (org-use-last-clock-out-time-as-effective-time
  10940. (or (org-clock-get-last-clock-out-time) ct))
  10941. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10942. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10943. (t ct))))
  10944. ct1))
  10945. (defun org-todo-yesterday (&optional arg)
  10946. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10947. (interactive "P")
  10948. (if (eq major-mode 'org-agenda-mode)
  10949. (apply 'org-agenda-todo-yesterday arg)
  10950. (let* ((hour (third (decode-time
  10951. (org-current-time))))
  10952. (org-extend-today-until (1+ hour)))
  10953. (org-todo arg))))
  10954. (defvar org-block-entry-blocking ""
  10955. "First entry preventing the TODO state change.")
  10956. (defun org-todo (&optional arg)
  10957. "Change the TODO state of an item.
  10958. The state of an item is given by a keyword at the start of the heading,
  10959. like
  10960. *** TODO Write paper
  10961. *** DONE Call mom
  10962. The different keywords are specified in the variable `org-todo-keywords'.
  10963. By default the available states are \"TODO\" and \"DONE\".
  10964. So for this example: when the item starts with TODO, it is changed to DONE.
  10965. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10966. DONE are present, add TODO at the beginning of the heading.
  10967. With \\[universal-argument] prefix arg, use completion to determine the new \
  10968. state.
  10969. With numeric prefix arg, switch to that state.
  10970. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10971. keywords (nextset).
  10972. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10973. With a numeric prefix arg of 0, inhibit note taking for the change.
  10974. For calling through lisp, arg is also interpreted in the following way:
  10975. 'none -> empty state
  10976. \"\"(empty string) -> switch to empty state
  10977. 'done -> switch to DONE
  10978. 'nextset -> switch to the next set of keywords
  10979. 'previousset -> switch to the previous set of keywords
  10980. \"WAITING\" -> switch to the specified keyword, but only if it
  10981. really is a member of `org-todo-keywords'."
  10982. (interactive "P")
  10983. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10984. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10985. 'region-start-level 'region))
  10986. org-loop-over-headlines-in-active-region)
  10987. (org-map-entries
  10988. `(org-todo ,arg)
  10989. org-loop-over-headlines-in-active-region
  10990. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10991. (if (equal arg '(16)) (setq arg 'nextset))
  10992. (let ((org-blocker-hook org-blocker-hook)
  10993. commentp
  10994. case-fold-search)
  10995. (when (equal arg '(64))
  10996. (setq arg nil org-blocker-hook nil))
  10997. (when (and org-blocker-hook
  10998. (or org-inhibit-blocking
  10999. (org-entry-get nil "NOBLOCKING")))
  11000. (setq org-blocker-hook nil))
  11001. (save-excursion
  11002. (catch 'exit
  11003. (org-back-to-heading t)
  11004. (when (looking-at (concat "^\\*+ " org-comment-string))
  11005. (org-toggle-comment)
  11006. (setq commentp t))
  11007. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11008. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11009. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11010. (let* ((match-data (match-data))
  11011. (startpos (point-at-bol))
  11012. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11013. (org-log-done org-log-done)
  11014. (org-log-repeat org-log-repeat)
  11015. (org-todo-log-states org-todo-log-states)
  11016. (org-inhibit-logging
  11017. (if (equal arg 0)
  11018. (progn (setq arg nil) 'note) org-inhibit-logging))
  11019. (this (match-string 1))
  11020. (hl-pos (match-beginning 0))
  11021. (head (org-get-todo-sequence-head this))
  11022. (ass (assoc head org-todo-kwd-alist))
  11023. (interpret (nth 1 ass))
  11024. (done-word (nth 3 ass))
  11025. (final-done-word (nth 4 ass))
  11026. (org-last-state (or this ""))
  11027. (completion-ignore-case t)
  11028. (member (member this org-todo-keywords-1))
  11029. (tail (cdr member))
  11030. (org-state (cond
  11031. ((and org-todo-key-trigger
  11032. (or (and (equal arg '(4))
  11033. (eq org-use-fast-todo-selection 'prefix))
  11034. (and (not arg) org-use-fast-todo-selection
  11035. (not (eq org-use-fast-todo-selection
  11036. 'prefix)))))
  11037. ;; Use fast selection
  11038. (org-fast-todo-selection))
  11039. ((and (equal arg '(4))
  11040. (or (not org-use-fast-todo-selection)
  11041. (not org-todo-key-trigger)))
  11042. ;; Read a state with completion
  11043. (org-icompleting-read
  11044. "State: " (mapcar 'list org-todo-keywords-1)
  11045. nil t))
  11046. ((eq arg 'right)
  11047. (if this
  11048. (if tail (car tail) nil)
  11049. (car org-todo-keywords-1)))
  11050. ((eq arg 'left)
  11051. (if (equal member org-todo-keywords-1)
  11052. nil
  11053. (if this
  11054. (nth (- (length org-todo-keywords-1)
  11055. (length tail) 2)
  11056. org-todo-keywords-1)
  11057. (org-last org-todo-keywords-1))))
  11058. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11059. (setq arg nil))) ; hack to fall back to cycling
  11060. (arg
  11061. ;; user or caller requests a specific state
  11062. (cond
  11063. ((equal arg "") nil)
  11064. ((eq arg 'none) nil)
  11065. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11066. ((eq arg 'nextset)
  11067. (or (car (cdr (member head org-todo-heads)))
  11068. (car org-todo-heads)))
  11069. ((eq arg 'previousset)
  11070. (let ((org-todo-heads (reverse org-todo-heads)))
  11071. (or (car (cdr (member head org-todo-heads)))
  11072. (car org-todo-heads))))
  11073. ((car (member arg org-todo-keywords-1)))
  11074. ((stringp arg)
  11075. (user-error "State `%s' not valid in this file" arg))
  11076. ((nth (1- (prefix-numeric-value arg))
  11077. org-todo-keywords-1))))
  11078. ((null member) (or head (car org-todo-keywords-1)))
  11079. ((equal this final-done-word) nil) ;; -> make empty
  11080. ((null tail) nil) ;; -> first entry
  11081. ((memq interpret '(type priority))
  11082. (if (eq this-command last-command)
  11083. (car tail)
  11084. (if (> (length tail) 0)
  11085. (or done-word (car org-done-keywords))
  11086. nil)))
  11087. (t
  11088. (car tail))))
  11089. (org-state (or
  11090. (run-hook-with-args-until-success
  11091. 'org-todo-get-default-hook org-state org-last-state)
  11092. org-state))
  11093. (next (if org-state (concat " " org-state " ") " "))
  11094. (change-plist (list :type 'todo-state-change :from this :to org-state
  11095. :position startpos))
  11096. dolog now-done-p)
  11097. (when org-blocker-hook
  11098. (setq org-last-todo-state-is-todo
  11099. (not (member this org-done-keywords)))
  11100. (unless (save-excursion
  11101. (save-match-data
  11102. (org-with-wide-buffer
  11103. (run-hook-with-args-until-failure
  11104. 'org-blocker-hook change-plist))))
  11105. (if (org-called-interactively-p 'interactive)
  11106. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11107. this org-state org-block-entry-blocking)
  11108. ;; fail silently
  11109. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11110. this org-state org-block-entry-blocking)
  11111. (throw 'exit nil))))
  11112. (store-match-data match-data)
  11113. (replace-match next t t)
  11114. (unless (pos-visible-in-window-p hl-pos)
  11115. (message "TODO state changed to %s" (org-trim next)))
  11116. (unless head
  11117. (setq head (org-get-todo-sequence-head org-state)
  11118. ass (assoc head org-todo-kwd-alist)
  11119. interpret (nth 1 ass)
  11120. done-word (nth 3 ass)
  11121. final-done-word (nth 4 ass)))
  11122. (when (memq arg '(nextset previousset))
  11123. (message "Keyword-Set %d/%d: %s"
  11124. (- (length org-todo-sets) -1
  11125. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11126. (length org-todo-sets)
  11127. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11128. (setq org-last-todo-state-is-todo
  11129. (not (member org-state org-done-keywords)))
  11130. (setq now-done-p (and (member org-state org-done-keywords)
  11131. (not (member this org-done-keywords))))
  11132. (and logging (org-local-logging logging))
  11133. (when (and (or org-todo-log-states org-log-done)
  11134. (not (eq org-inhibit-logging t))
  11135. (not (memq arg '(nextset previousset))))
  11136. ;; we need to look at recording a time and note
  11137. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11138. (nth 2 (assoc this org-todo-log-states))))
  11139. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11140. (setq dolog 'time))
  11141. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11142. (and org-state
  11143. (member org-state org-not-done-keywords)
  11144. (not (member this org-not-done-keywords))))
  11145. ;; This is now a todo state and was not one before
  11146. ;; If there was a CLOSED time stamp, get rid of it.
  11147. (org-add-planning-info nil nil 'closed))
  11148. (when (and now-done-p org-log-done)
  11149. ;; It is now done, and it was not done before
  11150. (org-add-planning-info 'closed (org-current-effective-time))
  11151. (if (and (not dolog) (eq 'note org-log-done))
  11152. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11153. (when (and org-state dolog)
  11154. ;; This is a non-nil state, and we need to log it
  11155. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11156. ;; Fixup tag positioning
  11157. (org-todo-trigger-tag-changes org-state)
  11158. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11159. (when org-provide-todo-statistics
  11160. (org-update-parent-todo-statistics))
  11161. (run-hooks 'org-after-todo-state-change-hook)
  11162. (if (and arg (not (member org-state org-done-keywords)))
  11163. (setq head (org-get-todo-sequence-head org-state)))
  11164. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11165. ;; Do we need to trigger a repeat?
  11166. (when now-done-p
  11167. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11168. ;; This is for the agenda, take a snapshot of the headline.
  11169. (save-match-data
  11170. (setq org-agenda-headline-snapshot-before-repeat
  11171. (org-get-heading))))
  11172. (org-auto-repeat-maybe org-state))
  11173. ;; Fixup cursor location if close to the keyword
  11174. (if (and (outline-on-heading-p)
  11175. (not (bolp))
  11176. (save-excursion (beginning-of-line 1)
  11177. (looking-at org-todo-line-regexp))
  11178. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11179. (progn
  11180. (goto-char (or (match-end 2) (match-end 1)))
  11181. (and (looking-at " ") (just-one-space))))
  11182. (when org-trigger-hook
  11183. (save-excursion
  11184. (run-hook-with-args 'org-trigger-hook change-plist)))
  11185. (when commentp (org-toggle-comment))))))))
  11186. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11187. "Block turning an entry into a TODO, using the hierarchy.
  11188. This checks whether the current task should be blocked from state
  11189. changes. Such blocking occurs when:
  11190. 1. The task has children which are not all in a completed state.
  11191. 2. A task has a parent with the property :ORDERED:, and there
  11192. are siblings prior to the current task with incomplete
  11193. status.
  11194. 3. The parent of the task is blocked because it has siblings that should
  11195. be done first, or is child of a block grandparent TODO entry."
  11196. (if (not org-enforce-todo-dependencies)
  11197. t ; if locally turned off don't block
  11198. (catch 'dont-block
  11199. ;; If this is not a todo state change, or if this entry is already DONE,
  11200. ;; do not block
  11201. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11202. (member (plist-get change-plist :from)
  11203. (cons 'done org-done-keywords))
  11204. (member (plist-get change-plist :to)
  11205. (cons 'todo org-not-done-keywords))
  11206. (not (plist-get change-plist :to)))
  11207. (throw 'dont-block t))
  11208. ;; If this task has children, and any are undone, it's blocked
  11209. (save-excursion
  11210. (org-back-to-heading t)
  11211. (let ((this-level (funcall outline-level)))
  11212. (outline-next-heading)
  11213. (let ((child-level (funcall outline-level)))
  11214. (while (and (not (eobp))
  11215. (> child-level this-level))
  11216. ;; this todo has children, check whether they are all
  11217. ;; completed
  11218. (if (and (not (org-entry-is-done-p))
  11219. (org-entry-is-todo-p))
  11220. (progn (setq org-block-entry-blocking (org-get-heading))
  11221. (throw 'dont-block nil)))
  11222. (outline-next-heading)
  11223. (setq child-level (funcall outline-level))))))
  11224. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11225. ;; any previous siblings are undone, it's blocked
  11226. (save-excursion
  11227. (org-back-to-heading t)
  11228. (let* ((pos (point))
  11229. (parent-pos (and (org-up-heading-safe) (point))))
  11230. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11231. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11232. (forward-line 1)
  11233. (re-search-forward org-not-done-heading-regexp pos t))
  11234. (setq org-block-entry-blocking (match-string 0))
  11235. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11236. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11237. (while t
  11238. (goto-char parent-pos)
  11239. (if (not (looking-at org-not-done-heading-regexp))
  11240. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11241. (setq pos (point))
  11242. (setq parent-pos (and (org-up-heading-safe) (point)))
  11243. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11244. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11245. (forward-line 1)
  11246. (re-search-forward org-not-done-heading-regexp pos t)
  11247. (setq org-block-entry-blocking (org-get-heading)))
  11248. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11249. (defcustom org-track-ordered-property-with-tag nil
  11250. "Should the ORDERED property also be shown as a tag?
  11251. The ORDERED property decides if an entry should require subtasks to be
  11252. completed in sequence. Since a property is not very visible, setting
  11253. this option means that toggling the ORDERED property with the command
  11254. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11255. not relevant for the behavior, but it makes things more visible.
  11256. Note that toggling the tag with tags commands will not change the property
  11257. and therefore not influence behavior!
  11258. This can be t, meaning the tag ORDERED should be used, It can also be a
  11259. string to select a different tag for this task."
  11260. :group 'org-todo
  11261. :type '(choice
  11262. (const :tag "No tracking" nil)
  11263. (const :tag "Track with ORDERED tag" t)
  11264. (string :tag "Use other tag")))
  11265. (defun org-toggle-ordered-property ()
  11266. "Toggle the ORDERED property of the current entry.
  11267. For better visibility, you can track the value of this property with a tag.
  11268. See variable `org-track-ordered-property-with-tag'."
  11269. (interactive)
  11270. (let* ((t1 org-track-ordered-property-with-tag)
  11271. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11272. (save-excursion
  11273. (org-back-to-heading)
  11274. (if (org-entry-get nil "ORDERED")
  11275. (progn
  11276. (org-delete-property "ORDERED" "PROPERTIES")
  11277. (and tag (org-toggle-tag tag 'off))
  11278. (message "Subtasks can be completed in arbitrary order"))
  11279. (org-entry-put nil "ORDERED" "t")
  11280. (and tag (org-toggle-tag tag 'on))
  11281. (message "Subtasks must be completed in sequence")))))
  11282. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11283. (defun org-block-todo-from-checkboxes (change-plist)
  11284. "Block turning an entry into a TODO, using checkboxes.
  11285. This checks whether the current task should be blocked from state
  11286. changes because there are unchecked boxes in this entry."
  11287. (if (not org-enforce-todo-checkbox-dependencies)
  11288. t ; if locally turned off don't block
  11289. (catch 'dont-block
  11290. ;; If this is not a todo state change, or if this entry is already DONE,
  11291. ;; do not block
  11292. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11293. (member (plist-get change-plist :from)
  11294. (cons 'done org-done-keywords))
  11295. (member (plist-get change-plist :to)
  11296. (cons 'todo org-not-done-keywords))
  11297. (not (plist-get change-plist :to)))
  11298. (throw 'dont-block t))
  11299. ;; If this task has checkboxes that are not checked, it's blocked
  11300. (save-excursion
  11301. (org-back-to-heading t)
  11302. (let ((beg (point)) end)
  11303. (outline-next-heading)
  11304. (setq end (point))
  11305. (goto-char beg)
  11306. (if (org-list-search-forward
  11307. (concat (org-item-beginning-re)
  11308. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11309. "\\[[- ]\\]")
  11310. end t)
  11311. (progn
  11312. (if (boundp 'org-blocked-by-checkboxes)
  11313. (setq org-blocked-by-checkboxes t))
  11314. (throw 'dont-block nil)))))
  11315. t))) ; do not block
  11316. (defun org-entry-blocked-p ()
  11317. "Is the current entry blocked?"
  11318. (org-with-silent-modifications
  11319. (if (org-entry-get nil "NOBLOCKING")
  11320. nil ;; Never block this entry
  11321. (not (run-hook-with-args-until-failure
  11322. 'org-blocker-hook
  11323. (list :type 'todo-state-change
  11324. :position (point)
  11325. :from 'todo
  11326. :to 'done))))))
  11327. (defun org-update-statistics-cookies (all)
  11328. "Update the statistics cookie, either from TODO or from checkboxes.
  11329. This should be called with the cursor in a line with a statistics cookie."
  11330. (interactive "P")
  11331. (if all
  11332. (progn
  11333. (org-update-checkbox-count 'all)
  11334. (org-map-entries 'org-update-parent-todo-statistics))
  11335. (if (not (org-at-heading-p))
  11336. (org-update-checkbox-count)
  11337. (let ((pos (point-marker))
  11338. end l1 l2)
  11339. (ignore-errors (org-back-to-heading t))
  11340. (if (not (org-at-heading-p))
  11341. (org-update-checkbox-count)
  11342. (setq l1 (org-outline-level))
  11343. (setq end (save-excursion
  11344. (outline-next-heading)
  11345. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11346. (point)))
  11347. (if (and (save-excursion
  11348. (re-search-forward
  11349. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11350. (not (save-excursion (re-search-forward
  11351. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11352. (org-update-checkbox-count)
  11353. (if (and l2 (> l2 l1))
  11354. (progn
  11355. (goto-char end)
  11356. (org-update-parent-todo-statistics))
  11357. (goto-char pos)
  11358. (beginning-of-line 1)
  11359. (while (re-search-forward
  11360. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11361. (point-at-eol) t)
  11362. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11363. (goto-char pos)
  11364. (move-marker pos nil)))))
  11365. (defvar org-entry-property-inherited-from) ;; defined below
  11366. (defun org-update-parent-todo-statistics ()
  11367. "Update any statistics cookie in the parent of the current headline.
  11368. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11369. the entire subtree and this will travel up the hierarchy and update
  11370. statistics everywhere."
  11371. (let* ((prop (save-excursion (org-up-heading-safe)
  11372. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11373. (recursive (or (not org-hierarchical-todo-statistics)
  11374. (and prop (string-match "\\<recursive\\>" prop))))
  11375. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11376. 0))
  11377. (first t)
  11378. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11379. level ltoggle l1 new ndel
  11380. (cnt-all 0) (cnt-done 0) is-percent kwd
  11381. checkbox-beg ov ovs ove cookie-present)
  11382. (catch 'exit
  11383. (save-excursion
  11384. (beginning-of-line 1)
  11385. (setq ltoggle (funcall outline-level))
  11386. ;; Three situations are to consider:
  11387. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11388. ;; to the top-level ancestor on the headline;
  11389. ;; 2. If parent has "recursive" property, repeat up to the
  11390. ;; headline setting that property, taking inheritance into
  11391. ;; account;
  11392. ;; 3. Else, move up to direct parent and proceed only once.
  11393. (while (and (setq level (org-up-heading-safe))
  11394. (or recursive first)
  11395. (>= (point) lim))
  11396. (setq first nil cookie-present nil)
  11397. (unless (and level
  11398. (not (string-match
  11399. "\\<checkbox\\>"
  11400. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11401. "")))))
  11402. (throw 'exit nil))
  11403. (while (re-search-forward box-re (point-at-eol) t)
  11404. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11405. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11406. (save-match-data
  11407. (unless (outline-next-heading) (throw 'exit nil))
  11408. (while (and (looking-at org-complex-heading-regexp)
  11409. (> (setq l1 (length (match-string 1))) level))
  11410. (setq kwd (and (or recursive (= l1 ltoggle))
  11411. (match-string 2)))
  11412. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11413. (and (listp org-provide-todo-statistics)
  11414. (or (member kwd org-provide-todo-statistics)
  11415. (member kwd org-done-keywords))))
  11416. (setq cnt-all (1+ cnt-all))
  11417. (if (eq org-provide-todo-statistics t)
  11418. (and kwd (setq cnt-all (1+ cnt-all)))))
  11419. (and (member kwd org-done-keywords)
  11420. (setq cnt-done (1+ cnt-done)))
  11421. (outline-next-heading)))
  11422. (setq new
  11423. (if is-percent
  11424. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11425. (format "[%d/%d]" cnt-done cnt-all))
  11426. ndel (- (match-end 0) checkbox-beg))
  11427. ;; handle overlays when updating cookie from column view
  11428. (when (setq ov (car (overlays-at checkbox-beg)))
  11429. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11430. (delete-overlay ov))
  11431. (goto-char checkbox-beg)
  11432. (insert new)
  11433. (delete-region (point) (+ (point) ndel))
  11434. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11435. (when ov (move-overlay ov ovs ove)))
  11436. (when cookie-present
  11437. (run-hook-with-args 'org-after-todo-statistics-hook
  11438. cnt-done (- cnt-all cnt-done))))))
  11439. (run-hooks 'org-todo-statistics-hook)))
  11440. (defvar org-after-todo-statistics-hook nil
  11441. "Hook that is called after a TODO statistics cookie has been updated.
  11442. Each function is called with two arguments: the number of not-done entries
  11443. and the number of done entries.
  11444. For example, the following function, when added to this hook, will switch
  11445. an entry to DONE when all children are done, and back to TODO when new
  11446. entries are set to a TODO status. Note that this hook is only called
  11447. when there is a statistics cookie in the headline!
  11448. (defun org-summary-todo (n-done n-not-done)
  11449. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11450. (let (org-log-done org-log-states) ; turn off logging
  11451. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11452. ")
  11453. (defvar org-todo-statistics-hook nil
  11454. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11455. This hook runs even if there is no statistics cookie present, in which case
  11456. `org-after-todo-statistics-hook' would not run.")
  11457. (defun org-todo-trigger-tag-changes (state)
  11458. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11459. (let ((l org-todo-state-tags-triggers)
  11460. changes)
  11461. (when (or (not state) (equal state ""))
  11462. (setq changes (append changes (cdr (assoc "" l)))))
  11463. (when (and (stringp state) (> (length state) 0))
  11464. (setq changes (append changes (cdr (assoc state l)))))
  11465. (when (member state org-not-done-keywords)
  11466. (setq changes (append changes (cdr (assoc 'todo l)))))
  11467. (when (member state org-done-keywords)
  11468. (setq changes (append changes (cdr (assoc 'done l)))))
  11469. (dolist (c changes)
  11470. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11471. (defun org-local-logging (value)
  11472. "Get logging settings from a property VALUE."
  11473. (let* (words w a)
  11474. ;; directly set the variables, they are already local.
  11475. (setq org-log-done nil
  11476. org-log-repeat nil
  11477. org-todo-log-states nil)
  11478. (setq words (org-split-string value))
  11479. (while (setq w (pop words))
  11480. (cond
  11481. ((setq a (assoc w org-startup-options))
  11482. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11483. (set (nth 1 a) (nth 2 a))))
  11484. ((setq a (org-extract-log-state-settings w))
  11485. (and (member (car a) org-todo-keywords-1)
  11486. (push a org-todo-log-states)))))))
  11487. (defun org-get-todo-sequence-head (kwd)
  11488. "Return the head of the TODO sequence to which KWD belongs.
  11489. If KWD is not set, check if there is a text property remembering the
  11490. right sequence."
  11491. (let (p)
  11492. (cond
  11493. ((not kwd)
  11494. (or (get-text-property (point-at-bol) 'org-todo-head)
  11495. (progn
  11496. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11497. nil (point-at-eol)))
  11498. (get-text-property p 'org-todo-head))))
  11499. ((not (member kwd org-todo-keywords-1))
  11500. (car org-todo-keywords-1))
  11501. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11502. (defun org-fast-todo-selection ()
  11503. "Fast TODO keyword selection with single keys.
  11504. Returns the new TODO keyword, or nil if no state change should occur."
  11505. (let* ((fulltable org-todo-key-alist)
  11506. (done-keywords org-done-keywords) ;; needed for the faces.
  11507. (maxlen (apply 'max (mapcar
  11508. (lambda (x)
  11509. (if (stringp (car x)) (string-width (car x)) 0))
  11510. fulltable)))
  11511. (expert nil)
  11512. (fwidth (+ maxlen 3 1 3))
  11513. (ncol (/ (- (window-width) 4) fwidth))
  11514. tg cnt e c tbl
  11515. groups ingroup)
  11516. (save-excursion
  11517. (save-window-excursion
  11518. (if expert
  11519. (set-buffer (get-buffer-create " *Org todo*"))
  11520. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11521. (erase-buffer)
  11522. (org-set-local 'org-done-keywords done-keywords)
  11523. (setq tbl fulltable cnt 0)
  11524. (while (setq e (pop tbl))
  11525. (cond
  11526. ((equal e '(:startgroup))
  11527. (push '() groups) (setq ingroup t)
  11528. (when (not (= cnt 0))
  11529. (setq cnt 0)
  11530. (insert "\n"))
  11531. (insert "{ "))
  11532. ((equal e '(:endgroup))
  11533. (setq ingroup nil cnt 0)
  11534. (insert "}\n"))
  11535. ((equal e '(:newline))
  11536. (when (not (= cnt 0))
  11537. (setq cnt 0)
  11538. (insert "\n")
  11539. (setq e (car tbl))
  11540. (while (equal (car tbl) '(:newline))
  11541. (insert "\n")
  11542. (setq tbl (cdr tbl)))))
  11543. (t
  11544. (setq tg (car e) c (cdr e))
  11545. (if ingroup (push tg (car groups)))
  11546. (setq tg (org-add-props tg nil 'face
  11547. (org-get-todo-face tg)))
  11548. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11549. (insert "[" c "] " tg (make-string
  11550. (- fwidth 4 (length tg)) ?\ ))
  11551. (when (= (setq cnt (1+ cnt)) ncol)
  11552. (insert "\n")
  11553. (if ingroup (insert " "))
  11554. (setq cnt 0)))))
  11555. (insert "\n")
  11556. (goto-char (point-min))
  11557. (if (not expert) (org-fit-window-to-buffer))
  11558. (message "[a-z..]:Set [SPC]:clear")
  11559. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11560. (cond
  11561. ((or (= c ?\C-g)
  11562. (and (= c ?q) (not (rassoc c fulltable))))
  11563. (setq quit-flag t))
  11564. ((= c ?\ ) nil)
  11565. ((setq e (rassoc c fulltable) tg (car e))
  11566. tg)
  11567. (t (setq quit-flag t)))))))
  11568. (defun org-entry-is-todo-p ()
  11569. (member (org-get-todo-state) org-not-done-keywords))
  11570. (defun org-entry-is-done-p ()
  11571. (member (org-get-todo-state) org-done-keywords))
  11572. (defun org-get-todo-state ()
  11573. "Return the TODO keyword of the current subtree."
  11574. (save-excursion
  11575. (org-back-to-heading t)
  11576. (and (looking-at org-todo-line-regexp)
  11577. (match-end 2)
  11578. (match-string 2))))
  11579. (defun org-at-date-range-p (&optional inactive-ok)
  11580. "Is the cursor inside a date range?"
  11581. (interactive)
  11582. (save-excursion
  11583. (catch 'exit
  11584. (let ((pos (point)))
  11585. (skip-chars-backward "^[<\r\n")
  11586. (skip-chars-backward "<[")
  11587. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11588. (>= (match-end 0) pos)
  11589. (throw 'exit t))
  11590. (skip-chars-backward "^<[\r\n")
  11591. (skip-chars-backward "<[")
  11592. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11593. (>= (match-end 0) pos)
  11594. (throw 'exit t)))
  11595. nil)))
  11596. (defun org-get-repeat (&optional tagline)
  11597. "Check if there is a deadline/schedule with repeater in this entry."
  11598. (save-match-data
  11599. (save-excursion
  11600. (org-back-to-heading t)
  11601. (and (re-search-forward (if tagline
  11602. (concat tagline "\\s-*" org-repeat-re)
  11603. org-repeat-re)
  11604. (org-entry-end-position) t)
  11605. (match-string-no-properties 1)))))
  11606. (defvar org-last-changed-timestamp)
  11607. (defvar org-last-inserted-timestamp)
  11608. (defvar org-log-post-message)
  11609. (defvar org-log-note-purpose)
  11610. (defvar org-log-note-how)
  11611. (defvar org-log-note-extra)
  11612. (defun org-auto-repeat-maybe (done-word)
  11613. "Check if the current headline contains a repeated deadline/schedule.
  11614. If yes, set TODO state back to what it was and change the base date
  11615. of repeating deadline/scheduled time stamps to new date.
  11616. This function is run automatically after each state change to a DONE state."
  11617. ;; last-state is dynamically scoped into this function
  11618. (let* ((repeat (org-get-repeat))
  11619. (aa (assoc org-last-state org-todo-kwd-alist))
  11620. (interpret (nth 1 aa))
  11621. (head (nth 2 aa))
  11622. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11623. (msg "Entry repeats: ")
  11624. (org-log-done nil)
  11625. (org-todo-log-states nil)
  11626. re type n what ts time to-state)
  11627. (when repeat
  11628. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11629. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11630. org-todo-repeat-to-state))
  11631. (unless (and to-state (member to-state org-todo-keywords-1))
  11632. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11633. (org-todo to-state)
  11634. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11635. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11636. (org-time-stamp-format t t))))
  11637. (when org-log-repeat
  11638. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11639. (memq 'org-add-log-note post-command-hook))
  11640. ;; OK, we are already setup for some record
  11641. (if (eq org-log-repeat 'note)
  11642. ;; make sure we take a note, not only a time stamp
  11643. (setq org-log-note-how 'note))
  11644. ;; Set up for taking a record
  11645. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11646. org-last-state
  11647. 'findpos org-log-repeat)))
  11648. (org-back-to-heading t)
  11649. (org-add-planning-info nil nil 'closed)
  11650. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11651. org-deadline-time-regexp "\\)\\|\\("
  11652. org-ts-regexp "\\)"))
  11653. (while (re-search-forward
  11654. re (save-excursion (outline-next-heading) (point)) t)
  11655. (setq type (if (match-end 1) org-scheduled-string
  11656. (if (match-end 3) org-deadline-string "Plain:"))
  11657. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11658. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11659. (setq n (string-to-number (match-string 2 ts))
  11660. what (match-string 3 ts))
  11661. (if (equal what "w") (setq n (* n 7) what "d"))
  11662. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11663. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11664. ;; Preparation, see if we need to modify the start date for the change
  11665. (when (match-end 1)
  11666. (setq time (save-match-data (org-time-string-to-time ts)))
  11667. (cond
  11668. ((equal (match-string 1 ts) ".")
  11669. ;; Shift starting date to today
  11670. (org-timestamp-change
  11671. (- (org-today) (time-to-days time))
  11672. 'day))
  11673. ((equal (match-string 1 ts) "+")
  11674. (let ((nshiftmax 10) (nshift 0))
  11675. (while (or (= nshift 0)
  11676. (<= (time-to-days time)
  11677. (time-to-days (current-time))))
  11678. (when (= (incf nshift) nshiftmax)
  11679. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11680. (error "Abort")))
  11681. (org-timestamp-change n (cdr (assoc what whata)))
  11682. (org-at-timestamp-p t)
  11683. (setq ts (match-string 1))
  11684. (setq time (save-match-data (org-time-string-to-time ts)))))
  11685. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11686. ;; rematch, so that we have everything in place for the real shift
  11687. (org-at-timestamp-p t)
  11688. (setq ts (match-string 1))
  11689. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11690. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11691. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11692. (setq org-log-post-message msg)
  11693. (message "%s" msg))))
  11694. (defun org-show-todo-tree (arg)
  11695. "Make a compact tree which shows all headlines marked with TODO.
  11696. The tree will show the lines where the regexp matches, and all higher
  11697. headlines above the match.
  11698. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11699. With a numeric prefix N, construct a sparse tree for the Nth element
  11700. of `org-todo-keywords-1'."
  11701. (interactive "P")
  11702. (let ((case-fold-search nil)
  11703. (kwd-re
  11704. (cond ((null arg) org-not-done-regexp)
  11705. ((equal arg '(4))
  11706. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11707. (mapcar 'list org-todo-keywords-1))))
  11708. (concat "\\("
  11709. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11710. "\\)\\>")))
  11711. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11712. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11713. org-todo-keywords-1)))
  11714. (t (user-error "Invalid prefix argument: %s" arg)))))
  11715. (message "%d TODO entries found"
  11716. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11717. (defun org-deadline (arg &optional time)
  11718. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11719. With one universal prefix argument, remove any deadline from the item.
  11720. With two universal prefix arguments, prompt for a warning delay.
  11721. With argument TIME, set the deadline at the corresponding date. TIME
  11722. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11723. (interactive "P")
  11724. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11725. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11726. 'region-start-level 'region))
  11727. org-loop-over-headlines-in-active-region)
  11728. (org-map-entries
  11729. `(org-deadline ',arg ,time)
  11730. org-loop-over-headlines-in-active-region
  11731. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11732. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11733. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11734. (repeater (and old-date
  11735. (string-match
  11736. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11737. old-date)
  11738. (match-string 1 old-date))))
  11739. (cond
  11740. ((equal arg '(4))
  11741. (when (and old-date org-log-redeadline)
  11742. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11743. org-log-redeadline))
  11744. (org-remove-timestamp-with-keyword org-deadline-string)
  11745. (message "Item no longer has a deadline."))
  11746. ((equal arg '(16))
  11747. (save-excursion
  11748. (if (re-search-forward
  11749. org-deadline-time-regexp
  11750. (save-excursion (outline-next-heading) (point)) t)
  11751. (let* ((rpl0 (match-string 1))
  11752. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11753. (replace-match
  11754. (concat org-deadline-string
  11755. " <" rpl
  11756. (format " -%dd"
  11757. (abs
  11758. (- (time-to-days
  11759. (save-match-data
  11760. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11761. (time-to-days old-date-time))))
  11762. ">") t t))
  11763. (user-error "No deadline information to update"))))
  11764. (t
  11765. (org-add-planning-info 'deadline time 'closed)
  11766. (when (and old-date org-log-redeadline
  11767. (not (equal old-date
  11768. (substring org-last-inserted-timestamp 1 -1))))
  11769. (org-add-log-setup 'redeadline nil old-date 'findpos
  11770. org-log-redeadline))
  11771. (when repeater
  11772. (save-excursion
  11773. (org-back-to-heading t)
  11774. (when (re-search-forward (concat org-deadline-string " "
  11775. org-last-inserted-timestamp)
  11776. (save-excursion
  11777. (outline-next-heading) (point)) t)
  11778. (goto-char (1- (match-end 0)))
  11779. (insert " " repeater)
  11780. (setq org-last-inserted-timestamp
  11781. (concat (substring org-last-inserted-timestamp 0 -1)
  11782. " " repeater
  11783. (substring org-last-inserted-timestamp -1))))))
  11784. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11785. (defun org-schedule (arg &optional time)
  11786. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11787. With one universal prefix argument, remove any scheduling date from the item.
  11788. With two universal prefix arguments, prompt for a delay cookie.
  11789. With argument TIME, scheduled at the corresponding date. TIME can
  11790. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11791. (interactive "P")
  11792. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11793. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11794. 'region-start-level 'region))
  11795. org-loop-over-headlines-in-active-region)
  11796. (org-map-entries
  11797. `(org-schedule ',arg ,time)
  11798. org-loop-over-headlines-in-active-region
  11799. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11800. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11801. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11802. (repeater (and old-date
  11803. (string-match
  11804. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11805. old-date)
  11806. (match-string 1 old-date))))
  11807. (cond
  11808. ((equal arg '(4))
  11809. (progn
  11810. (when (and old-date org-log-reschedule)
  11811. (org-add-log-setup 'delschedule nil old-date 'findpos
  11812. org-log-reschedule))
  11813. (org-remove-timestamp-with-keyword org-scheduled-string)
  11814. (message "Item is no longer scheduled.")))
  11815. ((equal arg '(16))
  11816. (save-excursion
  11817. (if (re-search-forward
  11818. org-scheduled-time-regexp
  11819. (save-excursion (outline-next-heading) (point)) t)
  11820. (let* ((rpl0 (match-string 1))
  11821. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11822. (replace-match
  11823. (concat org-scheduled-string
  11824. " <" rpl
  11825. (format " -%dd"
  11826. (abs
  11827. (- (time-to-days
  11828. (save-match-data
  11829. (org-read-date nil t nil "Delay until" old-date-time)))
  11830. (time-to-days old-date-time))))
  11831. ">") t t))
  11832. (user-error "No scheduled information to update"))))
  11833. (t
  11834. (org-add-planning-info 'scheduled time 'closed)
  11835. (when (and old-date org-log-reschedule
  11836. (not (equal old-date
  11837. (substring org-last-inserted-timestamp 1 -1))))
  11838. (org-add-log-setup 'reschedule nil old-date 'findpos
  11839. org-log-reschedule))
  11840. (when repeater
  11841. (save-excursion
  11842. (org-back-to-heading t)
  11843. (when (re-search-forward (concat org-scheduled-string " "
  11844. org-last-inserted-timestamp)
  11845. (save-excursion
  11846. (outline-next-heading) (point)) t)
  11847. (goto-char (1- (match-end 0)))
  11848. (insert " " repeater)
  11849. (setq org-last-inserted-timestamp
  11850. (concat (substring org-last-inserted-timestamp 0 -1)
  11851. " " repeater
  11852. (substring org-last-inserted-timestamp -1))))))
  11853. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11854. (defun org-get-scheduled-time (pom &optional inherit)
  11855. "Get the scheduled time as a time tuple, of a format suitable
  11856. for calling org-schedule with, or if there is no scheduling,
  11857. returns nil."
  11858. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11859. (when time
  11860. (apply 'encode-time (org-parse-time-string time)))))
  11861. (defun org-get-deadline-time (pom &optional inherit)
  11862. "Get the deadline as a time tuple, of a format suitable for
  11863. calling org-deadline with, or if there is no scheduling, returns
  11864. nil."
  11865. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11866. (when time
  11867. (apply 'encode-time (org-parse-time-string time)))))
  11868. (defun org-remove-timestamp-with-keyword (keyword)
  11869. "Remove all time stamps with KEYWORD in the current entry."
  11870. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11871. beg)
  11872. (save-excursion
  11873. (org-back-to-heading t)
  11874. (setq beg (point))
  11875. (outline-next-heading)
  11876. (while (re-search-backward re beg t)
  11877. (replace-match "")
  11878. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11879. (equal (char-before) ?\ ))
  11880. (backward-delete-char 1)
  11881. (if (string-match "^[ \t]*$" (buffer-substring
  11882. (point-at-bol) (point-at-eol)))
  11883. (delete-region (point-at-bol)
  11884. (min (point-max) (1+ (point-at-eol))))))))))
  11885. (defun org-add-planning-info (what &optional time &rest remove)
  11886. "Insert new timestamp with keyword in the line directly after the headline.
  11887. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11888. If non is given, the user is prompted for a date.
  11889. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11890. be removed."
  11891. (interactive)
  11892. (let (org-time-was-given org-end-time-was-given ts
  11893. end default-time default-input)
  11894. (catch 'exit
  11895. (when (and (memq what '(scheduled deadline))
  11896. (or (not time)
  11897. (and (stringp time)
  11898. (string-match "^[-+]+[0-9]" time))))
  11899. ;; Try to get a default date/time from existing timestamp
  11900. (save-excursion
  11901. (org-back-to-heading t)
  11902. (setq end (save-excursion (outline-next-heading) (point)))
  11903. (when (re-search-forward (if (eq what 'scheduled)
  11904. org-scheduled-time-regexp
  11905. org-deadline-time-regexp)
  11906. end t)
  11907. (setq ts (match-string 1)
  11908. default-time
  11909. (apply 'encode-time (org-parse-time-string ts))
  11910. default-input (and ts (org-get-compact-tod ts))))))
  11911. (when what
  11912. (setq time
  11913. (if (stringp time)
  11914. ;; This is a string (relative or absolute), set proper date
  11915. (apply 'encode-time
  11916. (org-read-date-analyze
  11917. time default-time (decode-time default-time)))
  11918. ;; If necessary, get the time from the user
  11919. (or time (org-read-date nil 'to-time nil nil
  11920. default-time default-input)))))
  11921. (when (and org-insert-labeled-timestamps-at-point
  11922. (member what '(scheduled deadline)))
  11923. (insert
  11924. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11925. (org-insert-time-stamp time org-time-was-given
  11926. nil nil nil (list org-end-time-was-given))
  11927. (setq what nil))
  11928. (save-excursion
  11929. (save-restriction
  11930. (let (col list elt ts buffer-invisibility-spec)
  11931. (org-back-to-heading t)
  11932. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11933. (goto-char (match-end 1))
  11934. (setq col (current-column))
  11935. (goto-char (match-end 0))
  11936. (if (eobp) (insert "\n") (forward-char 1))
  11937. (when (and (not what)
  11938. (not (looking-at
  11939. (concat "[ \t]*"
  11940. org-keyword-time-not-clock-regexp))))
  11941. ;; Nothing to add, nothing to remove...... :-)
  11942. (throw 'exit nil))
  11943. (if (and (not (looking-at org-outline-regexp))
  11944. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11945. "[^\r\n]*"))
  11946. (not (equal (match-string 1) org-clock-string)))
  11947. (narrow-to-region (match-beginning 0) (match-end 0))
  11948. (insert-before-markers "\n")
  11949. (backward-char 1)
  11950. (narrow-to-region (point) (point))
  11951. (and org-adapt-indentation (org-indent-to-column col)))
  11952. ;; Check if we have to remove something.
  11953. (setq list (cons what remove))
  11954. (while list
  11955. (setq elt (pop list))
  11956. (when (or (and (eq elt 'scheduled)
  11957. (re-search-forward org-scheduled-time-regexp nil t))
  11958. (and (eq elt 'deadline)
  11959. (re-search-forward org-deadline-time-regexp nil t))
  11960. (and (eq elt 'closed)
  11961. (re-search-forward org-closed-time-regexp nil t)))
  11962. (replace-match "")
  11963. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11964. (and (looking-at "[ \t]+") (replace-match ""))
  11965. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11966. (when what
  11967. (insert
  11968. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11969. (cond ((eq what 'scheduled) org-scheduled-string)
  11970. ((eq what 'deadline) org-deadline-string)
  11971. ((eq what 'closed) org-closed-string))
  11972. " ")
  11973. (setq ts (org-insert-time-stamp
  11974. time
  11975. (or org-time-was-given
  11976. (and (eq what 'closed) org-log-done-with-time))
  11977. (eq what 'closed)
  11978. nil nil (list org-end-time-was-given)))
  11979. (insert
  11980. (if (not (or (bolp) (eq (char-before) ?\ )
  11981. (memq (char-after) '(32 10))
  11982. (eobp))) " " ""))
  11983. (end-of-line 1))
  11984. (goto-char (point-min))
  11985. (widen)
  11986. (if (and (looking-at "[ \t]*\n")
  11987. (equal (char-before) ?\n))
  11988. (delete-region (1- (point)) (point-at-eol)))
  11989. ts))))))
  11990. (defvar org-log-note-marker (make-marker))
  11991. (defvar org-log-note-purpose nil)
  11992. (defvar org-log-note-state nil)
  11993. (defvar org-log-note-previous-state nil)
  11994. (defvar org-log-note-how nil)
  11995. (defvar org-log-note-extra nil)
  11996. (defvar org-log-note-window-configuration nil)
  11997. (defvar org-log-note-return-to (make-marker))
  11998. (defvar org-log-note-effective-time nil
  11999. "Remembered current time so that dynamically scoped
  12000. `org-extend-today-until' affects tha timestamps in state change
  12001. log")
  12002. (defvar org-log-post-message nil
  12003. "Message to be displayed after a log note has been stored.
  12004. The auto-repeater uses this.")
  12005. (defun org-add-note ()
  12006. "Add a note to the current entry.
  12007. This is done in the same way as adding a state change note."
  12008. (interactive)
  12009. (org-add-log-setup 'note nil nil 'findpos nil))
  12010. (defvar org-property-end-re)
  12011. (defun org-add-log-setup (&optional purpose state prev-state
  12012. findpos how extra)
  12013. "Set up the post command hook to take a note.
  12014. If this is about to TODO state change, the new state is expected in STATE.
  12015. When FINDPOS is non-nil, find the correct position for the note in
  12016. the current entry. If not, assume that it can be inserted at point.
  12017. HOW is an indicator what kind of note should be created.
  12018. EXTRA is additional text that will be inserted into the notes buffer."
  12019. (let* ((org-log-into-drawer (org-log-into-drawer))
  12020. (drawer (cond ((stringp org-log-into-drawer)
  12021. org-log-into-drawer)
  12022. (org-log-into-drawer "LOGBOOK"))))
  12023. (save-restriction
  12024. (save-excursion
  12025. (when findpos
  12026. (org-back-to-heading t)
  12027. (narrow-to-region (point) (save-excursion
  12028. (outline-next-heading) (point)))
  12029. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12030. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12031. "[^\r\n]*\\)?"))
  12032. (goto-char (match-end 0))
  12033. (cond
  12034. (drawer
  12035. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12036. nil t)
  12037. (progn
  12038. (goto-char (match-end 0))
  12039. (or org-log-states-order-reversed
  12040. (and (re-search-forward org-property-end-re nil t)
  12041. (goto-char (1- (match-beginning 0))))))
  12042. (insert "\n:" drawer ":\n:END:")
  12043. (beginning-of-line 0)
  12044. (org-indent-line)
  12045. (beginning-of-line 2)
  12046. (org-indent-line)
  12047. (end-of-line 0)))
  12048. ((and org-log-state-notes-insert-after-drawers
  12049. (save-excursion
  12050. (forward-line) (looking-at org-drawer-regexp)))
  12051. (forward-line)
  12052. (while (looking-at org-drawer-regexp)
  12053. (goto-char (match-end 0))
  12054. (re-search-forward org-property-end-re (point-max) t)
  12055. (forward-line))
  12056. (forward-line -1)))
  12057. (unless org-log-states-order-reversed
  12058. (and (= (char-after) ?\n) (forward-char 1))
  12059. (org-skip-over-state-notes)
  12060. (skip-chars-backward " \t\n\r")))
  12061. (move-marker org-log-note-marker (point))
  12062. (setq org-log-note-purpose purpose
  12063. org-log-note-state state
  12064. org-log-note-previous-state prev-state
  12065. org-log-note-how how
  12066. org-log-note-extra extra
  12067. org-log-note-effective-time (org-current-effective-time))
  12068. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12069. (defun org-skip-over-state-notes ()
  12070. "Skip past the list of State notes in an entry."
  12071. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12072. (when (ignore-errors (goto-char (org-in-item-p)))
  12073. (let* ((struct (org-list-struct))
  12074. (prevs (org-list-prevs-alist struct)))
  12075. (while (looking-at "[ \t]*- State")
  12076. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12077. (org-list-get-item-end (point) struct)))))))
  12078. (defun org-add-log-note (&optional purpose)
  12079. "Pop up a window for taking a note, and add this note later at point."
  12080. (remove-hook 'post-command-hook 'org-add-log-note)
  12081. (setq org-log-note-window-configuration (current-window-configuration))
  12082. (delete-other-windows)
  12083. (move-marker org-log-note-return-to (point))
  12084. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12085. (goto-char org-log-note-marker)
  12086. (org-switch-to-buffer-other-window "*Org Note*")
  12087. (erase-buffer)
  12088. (if (memq org-log-note-how '(time state))
  12089. (let (current-prefix-arg) (org-store-log-note))
  12090. (let ((org-inhibit-startup t)) (org-mode))
  12091. (insert (format "# Insert note for %s.
  12092. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12093. (cond
  12094. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12095. ((eq org-log-note-purpose 'done) "closed todo item")
  12096. ((eq org-log-note-purpose 'state)
  12097. (format "state change from \"%s\" to \"%s\""
  12098. (or org-log-note-previous-state "")
  12099. (or org-log-note-state "")))
  12100. ((eq org-log-note-purpose 'reschedule)
  12101. "rescheduling")
  12102. ((eq org-log-note-purpose 'delschedule)
  12103. "no longer scheduled")
  12104. ((eq org-log-note-purpose 'redeadline)
  12105. "changing deadline")
  12106. ((eq org-log-note-purpose 'deldeadline)
  12107. "removing deadline")
  12108. ((eq org-log-note-purpose 'refile)
  12109. "refiling")
  12110. ((eq org-log-note-purpose 'note)
  12111. "this entry")
  12112. (t (error "This should not happen")))))
  12113. (if org-log-note-extra (insert org-log-note-extra))
  12114. (org-set-local 'org-finish-function 'org-store-log-note)
  12115. (run-hooks 'org-log-buffer-setup-hook)))
  12116. (defvar org-note-abort nil) ; dynamically scoped
  12117. (defun org-store-log-note ()
  12118. "Finish taking a log note, and insert it to where it belongs."
  12119. (let ((txt (buffer-string)))
  12120. (kill-buffer (current-buffer))
  12121. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12122. lines ind bul)
  12123. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12124. (setq txt (replace-match "" t t txt)))
  12125. (if (string-match "\\s-+\\'" txt)
  12126. (setq txt (replace-match "" t t txt)))
  12127. (setq lines (org-split-string txt "\n"))
  12128. (when (and note (string-match "\\S-" note))
  12129. (setq note
  12130. (org-replace-escapes
  12131. note
  12132. (list (cons "%u" (user-login-name))
  12133. (cons "%U" user-full-name)
  12134. (cons "%t" (format-time-string
  12135. (org-time-stamp-format 'long 'inactive)
  12136. org-log-note-effective-time))
  12137. (cons "%T" (format-time-string
  12138. (org-time-stamp-format 'long nil)
  12139. org-log-note-effective-time))
  12140. (cons "%d" (format-time-string
  12141. (org-time-stamp-format nil 'inactive)
  12142. org-log-note-effective-time))
  12143. (cons "%D" (format-time-string
  12144. (org-time-stamp-format nil nil)
  12145. org-log-note-effective-time))
  12146. (cons "%s" (if org-log-note-state
  12147. (concat "\"" org-log-note-state "\"")
  12148. ""))
  12149. (cons "%S" (if org-log-note-previous-state
  12150. (concat "\"" org-log-note-previous-state "\"")
  12151. "\"\"")))))
  12152. (if lines (setq note (concat note " \\\\")))
  12153. (push note lines))
  12154. (when (or current-prefix-arg org-note-abort)
  12155. (when org-log-into-drawer
  12156. (org-remove-empty-drawer-at
  12157. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12158. org-log-note-marker))
  12159. (setq lines nil))
  12160. (when lines
  12161. (with-current-buffer (marker-buffer org-log-note-marker)
  12162. (save-excursion
  12163. (goto-char org-log-note-marker)
  12164. (move-marker org-log-note-marker nil)
  12165. (end-of-line 1)
  12166. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12167. (setq ind (save-excursion
  12168. (if (ignore-errors (goto-char (org-in-item-p)))
  12169. (let ((struct (org-list-struct)))
  12170. (org-list-get-ind
  12171. (org-list-get-top-point struct) struct))
  12172. (skip-chars-backward " \r\t\n")
  12173. (cond
  12174. ((and (org-at-heading-p)
  12175. org-adapt-indentation)
  12176. (1+ (org-current-level)))
  12177. ((org-at-heading-p) 0)
  12178. (t (org-get-indentation))))))
  12179. (setq bul (org-list-bullet-string "-"))
  12180. (org-indent-line-to ind)
  12181. (insert bul (pop lines))
  12182. (let ((ind-body (+ (length bul) ind)))
  12183. (while lines
  12184. (insert "\n")
  12185. (org-indent-line-to ind-body)
  12186. (insert (pop lines))))
  12187. (message "Note stored")
  12188. (org-back-to-heading t)
  12189. (org-cycle-hide-drawers 'children))
  12190. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12191. ;; from within `org-add-log-note' because `buffer-undo-list'
  12192. ;; is then modified outside of `org-with-remote-undo'.
  12193. (when (eq this-command 'org-agenda-todo)
  12194. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12195. ;; Don't add undo information when called from `org-agenda-todo'
  12196. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12197. (set-window-configuration org-log-note-window-configuration)
  12198. (with-current-buffer (marker-buffer org-log-note-return-to)
  12199. (goto-char org-log-note-return-to))
  12200. (move-marker org-log-note-return-to nil)
  12201. (and org-log-post-message (message "%s" org-log-post-message))))
  12202. (defun org-remove-empty-drawer-at (drawer pos)
  12203. "Remove an empty drawer DRAWER at position POS.
  12204. POS may also be a marker."
  12205. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12206. (save-excursion
  12207. (save-restriction
  12208. (widen)
  12209. (goto-char pos)
  12210. (if (org-in-regexp
  12211. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12212. (replace-match ""))))))
  12213. (defvar org-ts-type nil)
  12214. (defun org-sparse-tree (&optional arg type)
  12215. "Create a sparse tree, prompt for the details.
  12216. This command can create sparse trees. You first need to select the type
  12217. of match used to create the tree:
  12218. t Show all TODO entries.
  12219. T Show entries with a specific TODO keyword.
  12220. m Show entries selected by a tags/property match.
  12221. p Enter a property name and its value (both with completion on existing
  12222. names/values) and show entries with that property.
  12223. r Show entries matching a regular expression (`/' can be used as well).
  12224. b Show deadlines and scheduled items before a date.
  12225. a Show deadlines and scheduled items after a date.
  12226. d Show deadlines due within `org-deadline-warning-days'.
  12227. D Show deadlines and scheduled items between a date range."
  12228. (interactive "P")
  12229. (let (ans kwd value ts-type)
  12230. (setq type (or type org-sparse-tree-default-date-type))
  12231. (setq org-ts-type type)
  12232. (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"
  12233. (cond ((eq type 'all) "all timestamps")
  12234. ((eq type 'scheduled) "only scheduled")
  12235. ((eq type 'deadline) "only deadline")
  12236. ((eq type 'active) "only active timestamps")
  12237. ((eq type 'inactive) "only inactive timestamps")
  12238. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12239. ((eq type 'closed) "with a closed time-stamp")
  12240. (t "scheduled/deadline")))
  12241. (setq ans (read-char-exclusive))
  12242. (cond
  12243. ((equal ans ?c)
  12244. (org-sparse-tree
  12245. arg (cadr (member type '(scheduled-or-deadline
  12246. all scheduled deadline active inactive closed)))))
  12247. ((equal ans ?d)
  12248. (call-interactively 'org-check-deadlines))
  12249. ((equal ans ?b)
  12250. (call-interactively 'org-check-before-date))
  12251. ((equal ans ?a)
  12252. (call-interactively 'org-check-after-date))
  12253. ((equal ans ?D)
  12254. (call-interactively 'org-check-dates-range))
  12255. ((equal ans ?t)
  12256. (call-interactively 'org-show-todo-tree))
  12257. ((equal ans ?T)
  12258. (org-show-todo-tree '(4)))
  12259. ((member ans '(?T ?m))
  12260. (call-interactively 'org-match-sparse-tree))
  12261. ((member ans '(?p ?P))
  12262. (setq kwd (org-icompleting-read "Property: "
  12263. (mapcar 'list (org-buffer-property-keys))))
  12264. (setq value (org-icompleting-read "Value: "
  12265. (mapcar 'list (org-property-values kwd))))
  12266. (unless (string-match "\\`{.*}\\'" value)
  12267. (setq value (concat "\"" value "\"")))
  12268. (org-match-sparse-tree arg (concat kwd "=" value)))
  12269. ((member ans '(?r ?R ?/))
  12270. (call-interactively 'org-occur))
  12271. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12272. (defvar org-occur-highlights nil
  12273. "List of overlays used for occur matches.")
  12274. (make-variable-buffer-local 'org-occur-highlights)
  12275. (defvar org-occur-parameters nil
  12276. "Parameters of the active org-occur calls.
  12277. This is a list, each call to org-occur pushes as cons cell,
  12278. containing the regular expression and the callback, onto the list.
  12279. The list can contain several entries if `org-occur' has been called
  12280. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12281. will only contain one set of parameters. When the highlights are
  12282. removed (for example with `C-c C-c', or with the next edit (depending
  12283. on `org-remove-highlights-with-change'), this variable is emptied
  12284. as well.")
  12285. (make-variable-buffer-local 'org-occur-parameters)
  12286. (defun org-occur (regexp &optional keep-previous callback)
  12287. "Make a compact tree which shows all matches of REGEXP.
  12288. The tree will show the lines where the regexp matches, and all higher
  12289. headlines above the match. It will also show the heading after the match,
  12290. to make sure editing the matching entry is easy.
  12291. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12292. call to `org-occur' will be kept, to allow stacking of calls to this
  12293. command.
  12294. If CALLBACK is non-nil, it is a function which is called to confirm
  12295. that the match should indeed be shown."
  12296. (interactive "sRegexp: \nP")
  12297. (when (equal regexp "")
  12298. (user-error "Regexp cannot be empty"))
  12299. (unless keep-previous
  12300. (org-remove-occur-highlights nil nil t))
  12301. (push (cons regexp callback) org-occur-parameters)
  12302. (let ((cnt 0))
  12303. (save-excursion
  12304. (goto-char (point-min))
  12305. (if (or (not keep-previous) ; do not want to keep
  12306. (not org-occur-highlights)) ; no previous matches
  12307. ;; hide everything
  12308. (org-overview))
  12309. (while (re-search-forward regexp nil t)
  12310. (when (or (not callback)
  12311. (save-match-data (funcall callback)))
  12312. (setq cnt (1+ cnt))
  12313. (when org-highlight-sparse-tree-matches
  12314. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12315. (org-show-context 'occur-tree))))
  12316. (when org-remove-highlights-with-change
  12317. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12318. nil 'local))
  12319. (unless org-sparse-tree-open-archived-trees
  12320. (org-hide-archived-subtrees (point-min) (point-max)))
  12321. (run-hooks 'org-occur-hook)
  12322. (if (org-called-interactively-p 'interactive)
  12323. (message "%d match(es) for regexp %s" cnt regexp))
  12324. cnt))
  12325. (defun org-occur-next-match (&optional n reset)
  12326. "Function for `next-error-function' to find sparse tree matches.
  12327. N is the number of matches to move, when negative move backwards.
  12328. RESET is entirely ignored - this function always goes back to the
  12329. starting point when no match is found."
  12330. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12331. (search-func (if (< n 0)
  12332. 'previous-single-char-property-change
  12333. 'next-single-char-property-change))
  12334. (n (abs n))
  12335. (pos (point))
  12336. p1)
  12337. (catch 'exit
  12338. (while (setq p1 (funcall search-func (point) 'org-type))
  12339. (when (equal p1 limit)
  12340. (goto-char pos)
  12341. (error "No more matches"))
  12342. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12343. (setq n (1- n))
  12344. (when (= n 0)
  12345. (goto-char p1)
  12346. (throw 'exit (point))))
  12347. (goto-char p1))
  12348. (goto-char p1)
  12349. (error "No more matches"))))
  12350. (defun org-show-context (&optional key)
  12351. "Make sure point and context are visible.
  12352. How much context is shown depends upon the variables
  12353. `org-show-hierarchy-above', `org-show-following-heading',
  12354. `org-show-entry-below' and `org-show-siblings'."
  12355. (let ((heading-p (org-at-heading-p t))
  12356. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12357. (following-p (org-get-alist-option org-show-following-heading key))
  12358. (entry-p (org-get-alist-option org-show-entry-below key))
  12359. (siblings-p (org-get-alist-option org-show-siblings key)))
  12360. ;; Show heading or entry text
  12361. (if (and heading-p (not entry-p))
  12362. (org-flag-heading nil) ; only show the heading
  12363. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12364. (org-show-hidden-entry))) ; show entire entry
  12365. (when following-p
  12366. ;; Show next sibling, or heading below text
  12367. (save-excursion
  12368. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12369. (org-flag-heading nil))))
  12370. (when siblings-p (org-show-siblings))
  12371. (when hierarchy-p
  12372. ;; show all higher headings, possibly with siblings
  12373. (save-excursion
  12374. (while (and (condition-case nil
  12375. (progn (org-up-heading-all 1) t)
  12376. (error nil))
  12377. (not (bobp)))
  12378. (org-flag-heading nil)
  12379. (when siblings-p (org-show-siblings)))))
  12380. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12381. (defvar org-reveal-start-hook nil
  12382. "Hook run before revealing a location.")
  12383. (defun org-reveal (&optional siblings)
  12384. "Show current entry, hierarchy above it, and the following headline.
  12385. This can be used to show a consistent set of context around locations
  12386. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12387. not t for the search context.
  12388. With optional argument SIBLINGS, on each level of the hierarchy all
  12389. siblings are shown. This repairs the tree structure to what it would
  12390. look like when opened with hierarchical calls to `org-cycle'.
  12391. With double optional argument \\[universal-argument] \\[universal-argument], \
  12392. go to the parent and show the
  12393. entire tree."
  12394. (interactive "P")
  12395. (run-hooks 'org-reveal-start-hook)
  12396. (let ((org-show-hierarchy-above t)
  12397. (org-show-following-heading t)
  12398. (org-show-siblings (if siblings t org-show-siblings)))
  12399. (org-show-context nil))
  12400. (when (equal siblings '(16))
  12401. (save-excursion
  12402. (when (org-up-heading-safe)
  12403. (org-show-subtree)
  12404. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12405. (defun org-highlight-new-match (beg end)
  12406. "Highlight from BEG to END and mark the highlight is an occur headline."
  12407. (let ((ov (make-overlay beg end)))
  12408. (overlay-put ov 'face 'secondary-selection)
  12409. (overlay-put ov 'org-type 'org-occur)
  12410. (push ov org-occur-highlights)))
  12411. (defun org-remove-occur-highlights (&optional beg end noremove)
  12412. "Remove the occur highlights from the buffer.
  12413. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12414. from the `before-change-functions' in the current buffer."
  12415. (interactive)
  12416. (unless org-inhibit-highlight-removal
  12417. (mapc 'delete-overlay org-occur-highlights)
  12418. (setq org-occur-highlights nil)
  12419. (setq org-occur-parameters nil)
  12420. (unless noremove
  12421. (remove-hook 'before-change-functions
  12422. 'org-remove-occur-highlights 'local))))
  12423. ;;;; Priorities
  12424. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12425. "Regular expression matching the priority indicator.")
  12426. (defvar org-remove-priority-next-time nil)
  12427. (defun org-priority-up ()
  12428. "Increase the priority of the current item."
  12429. (interactive)
  12430. (org-priority 'up))
  12431. (defun org-priority-down ()
  12432. "Decrease the priority of the current item."
  12433. (interactive)
  12434. (org-priority 'down))
  12435. (defun org-priority (&optional action show)
  12436. "Change the priority of an item.
  12437. ACTION can be `set', `up', `down', or a character."
  12438. (interactive "P")
  12439. (if (equal action '(4))
  12440. (org-show-priority)
  12441. (unless org-enable-priority-commands
  12442. (user-error "Priority commands are disabled"))
  12443. (setq action (or action 'set))
  12444. (let (current new news have remove)
  12445. (save-excursion
  12446. (org-back-to-heading t)
  12447. (if (looking-at org-priority-regexp)
  12448. (setq current (string-to-char (match-string 2))
  12449. have t))
  12450. (cond
  12451. ((eq action 'remove)
  12452. (setq remove t new ?\ ))
  12453. ((or (eq action 'set)
  12454. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12455. (if (not (eq action 'set))
  12456. (setq new action)
  12457. (message "Priority %c-%c, SPC to remove: "
  12458. org-highest-priority org-lowest-priority)
  12459. (save-match-data
  12460. (setq new (read-char-exclusive))))
  12461. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12462. (= (upcase org-lowest-priority) org-lowest-priority))
  12463. (setq new (upcase new)))
  12464. (cond ((equal new ?\ ) (setq remove t))
  12465. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12466. (user-error "Priority must be between `%c' and `%c'"
  12467. org-highest-priority org-lowest-priority))))
  12468. ((eq action 'up)
  12469. (setq new (if have
  12470. (1- current) ; normal cycling
  12471. ;; last priority was empty
  12472. (if (eq last-command this-command)
  12473. org-lowest-priority ; wrap around empty to lowest
  12474. ;; default
  12475. (if org-priority-start-cycle-with-default
  12476. org-default-priority
  12477. (1- org-default-priority))))))
  12478. ((eq action 'down)
  12479. (setq new (if have
  12480. (1+ current) ; normal cycling
  12481. ;; last priority was empty
  12482. (if (eq last-command this-command)
  12483. org-highest-priority ; wrap around empty to highest
  12484. ;; default
  12485. (if org-priority-start-cycle-with-default
  12486. org-default-priority
  12487. (1+ org-default-priority))))))
  12488. (t (user-error "Invalid action")))
  12489. (if (or (< (upcase new) org-highest-priority)
  12490. (> (upcase new) org-lowest-priority))
  12491. (if (and (memq action '(up down))
  12492. (not have) (not (eq last-command this-command)))
  12493. ;; `new' is from default priority
  12494. (error
  12495. "The default can not be set, see `org-default-priority' why")
  12496. ;; normal cycling: `new' is beyond highest/lowest priority
  12497. ;; and is wrapped around to the empty priority
  12498. (setq remove t)))
  12499. (setq news (format "%c" new))
  12500. (if have
  12501. (if remove
  12502. (replace-match "" t t nil 1)
  12503. (replace-match news t t nil 2))
  12504. (if remove
  12505. (user-error "No priority cookie found in line")
  12506. (let ((case-fold-search nil))
  12507. (looking-at org-todo-line-regexp))
  12508. (if (match-end 2)
  12509. (progn
  12510. (goto-char (match-end 2))
  12511. (insert " [#" news "]"))
  12512. (goto-char (match-beginning 3))
  12513. (insert "[#" news "] "))))
  12514. (org-preserve-lc (org-set-tags nil 'align)))
  12515. (if remove
  12516. (message "Priority removed")
  12517. (message "Priority of current item set to %s" news)))))
  12518. (defun org-show-priority ()
  12519. "Show the priority of the current item.
  12520. This priority is composed of the main priority given with the [#A] cookies,
  12521. and by additional input from the age of a schedules or deadline entry."
  12522. (interactive)
  12523. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12524. (org-get-at-bol 'priority)
  12525. (save-excursion
  12526. (save-match-data
  12527. (beginning-of-line)
  12528. (and (looking-at org-heading-regexp)
  12529. (org-get-priority (match-string 0))))))))
  12530. (message "Priority is %d" (if pri pri -1000))))
  12531. (defun org-get-priority (s)
  12532. "Find priority cookie and return priority."
  12533. (save-match-data
  12534. (if (functionp org-get-priority-function)
  12535. (funcall org-get-priority-function)
  12536. (if (not (string-match org-priority-regexp s))
  12537. (* 1000 (- org-lowest-priority org-default-priority))
  12538. (* 1000 (- org-lowest-priority
  12539. (string-to-char (match-string 2 s))))))))
  12540. ;;;; Tags
  12541. (defvar org-agenda-archives-mode)
  12542. (defvar org-map-continue-from nil
  12543. "Position from where mapping should continue.
  12544. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12545. (defvar org-scanner-tags nil
  12546. "The current tag list while the tags scanner is running.")
  12547. (defvar org-trust-scanner-tags nil
  12548. "Should `org-get-tags-at' use the tags for the scanner.
  12549. This is for internal dynamical scoping only.
  12550. When this is non-nil, the function `org-get-tags-at' will return the value
  12551. of `org-scanner-tags' instead of building the list by itself. This
  12552. can lead to large speed-ups when the tags scanner is used in a file with
  12553. many entries, and when the list of tags is retrieved, for example to
  12554. obtain a list of properties. Building the tags list for each entry in such
  12555. a file becomes an N^2 operation - but with this variable set, it scales
  12556. as N.")
  12557. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12558. "Sca headline tags with inheritance and produce output ACTION.
  12559. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12560. or `agenda' to produce an entry list for an agenda view. It can also be
  12561. a Lisp form or a function that should be called at each matched headline, in
  12562. this case the return value is a list of all return values from these calls.
  12563. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12564. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12565. only lines with a not-done TODO keyword are included in the output.
  12566. This should be the same variable that was scoped into
  12567. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12568. START-LEVEL can be a string with asterisks, reducing the scope to
  12569. headlines matching this string."
  12570. (require 'org-agenda)
  12571. (let* ((re (concat "^"
  12572. (if start-level
  12573. ;; Get the correct level to match
  12574. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12575. org-outline-regexp)
  12576. " *\\(\\<\\("
  12577. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12578. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12579. (props (list 'face 'default
  12580. 'done-face 'org-agenda-done
  12581. 'undone-face 'default
  12582. 'mouse-face 'highlight
  12583. 'org-not-done-regexp org-not-done-regexp
  12584. 'org-todo-regexp org-todo-regexp
  12585. 'org-complex-heading-regexp org-complex-heading-regexp
  12586. 'help-echo
  12587. (format "mouse-2 or RET jump to org file %s"
  12588. (abbreviate-file-name
  12589. (or (buffer-file-name (buffer-base-buffer))
  12590. (buffer-name (buffer-base-buffer)))))))
  12591. (case-fold-search nil)
  12592. (org-map-continue-from nil)
  12593. lspos tags tags-list
  12594. (tags-alist (list (cons 0 org-file-tags)))
  12595. (llast 0) rtn rtn1 level category i txt
  12596. todo marker entry priority)
  12597. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12598. (setq action (list 'lambda nil action)))
  12599. (save-excursion
  12600. (goto-char (point-min))
  12601. (when (eq action 'sparse-tree)
  12602. (org-overview)
  12603. (org-remove-occur-highlights))
  12604. (while (re-search-forward re nil t)
  12605. (setq org-map-continue-from nil)
  12606. (catch :skip
  12607. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12608. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12609. (goto-char (setq lspos (match-beginning 0)))
  12610. (setq level (org-reduced-level (org-outline-level))
  12611. category (org-get-category))
  12612. (setq i llast llast level)
  12613. ;; remove tag lists from same and sublevels
  12614. (while (>= i level)
  12615. (when (setq entry (assoc i tags-alist))
  12616. (setq tags-alist (delete entry tags-alist)))
  12617. (setq i (1- i)))
  12618. ;; add the next tags
  12619. (when tags
  12620. (setq tags (org-split-string tags ":")
  12621. tags-alist
  12622. (cons (cons level tags) tags-alist)))
  12623. ;; compile tags for current headline
  12624. (setq tags-list
  12625. (if org-use-tag-inheritance
  12626. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12627. tags)
  12628. org-scanner-tags tags-list)
  12629. (when org-use-tag-inheritance
  12630. (setcdr (car tags-alist)
  12631. (mapcar (lambda (x)
  12632. (setq x (copy-sequence x))
  12633. (org-add-prop-inherited x))
  12634. (cdar tags-alist))))
  12635. (when (and tags org-use-tag-inheritance
  12636. (or (not (eq t org-use-tag-inheritance))
  12637. org-tags-exclude-from-inheritance))
  12638. ;; selective inheritance, remove uninherited ones
  12639. (setcdr (car tags-alist)
  12640. (org-remove-uninherited-tags (cdar tags-alist))))
  12641. (when (and
  12642. ;; eval matcher only when the todo condition is OK
  12643. (and (or (not todo-only) (member todo org-not-done-keywords))
  12644. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12645. (eval matcher)))
  12646. ;; Call the skipper, but return t if it does not skip,
  12647. ;; so that the `and' form continues evaluating
  12648. (progn
  12649. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12650. t)
  12651. ;; Check if timestamps are deselecting this entry
  12652. (or (not todo-only)
  12653. (and (member todo org-not-done-keywords)
  12654. (or (not org-agenda-tags-todo-honor-ignore-options)
  12655. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12656. ;; select this headline
  12657. (cond
  12658. ((eq action 'sparse-tree)
  12659. (and org-highlight-sparse-tree-matches
  12660. (org-get-heading) (match-end 0)
  12661. (org-highlight-new-match
  12662. (match-beginning 1) (match-end 1)))
  12663. (org-show-context 'tags-tree))
  12664. ((eq action 'agenda)
  12665. (setq txt (org-agenda-format-item
  12666. ""
  12667. (concat
  12668. (if (eq org-tags-match-list-sublevels 'indented)
  12669. (make-string (1- level) ?.) "")
  12670. (org-get-heading))
  12671. level category
  12672. tags-list)
  12673. priority (org-get-priority txt))
  12674. (goto-char lspos)
  12675. (setq marker (org-agenda-new-marker))
  12676. (org-add-props txt props
  12677. 'org-marker marker 'org-hd-marker marker 'org-category category
  12678. 'todo-state todo
  12679. 'priority priority 'type "tagsmatch")
  12680. (push txt rtn))
  12681. ((functionp action)
  12682. (setq org-map-continue-from nil)
  12683. (save-excursion
  12684. (setq rtn1 (funcall action))
  12685. (push rtn1 rtn)))
  12686. (t (user-error "Invalid action")))
  12687. ;; if we are to skip sublevels, jump to end of subtree
  12688. (unless org-tags-match-list-sublevels
  12689. (org-end-of-subtree t)
  12690. (backward-char 1))))
  12691. ;; Get the correct position from where to continue
  12692. (if org-map-continue-from
  12693. (goto-char org-map-continue-from)
  12694. (and (= (point) lspos) (end-of-line 1)))))
  12695. (when (and (eq action 'sparse-tree)
  12696. (not org-sparse-tree-open-archived-trees))
  12697. (org-hide-archived-subtrees (point-min) (point-max)))
  12698. (nreverse rtn)))
  12699. (defun org-remove-uninherited-tags (tags)
  12700. "Remove all tags that are not inherited from the list TAGS."
  12701. (cond
  12702. ((eq org-use-tag-inheritance t)
  12703. (if org-tags-exclude-from-inheritance
  12704. (org-delete-all org-tags-exclude-from-inheritance tags)
  12705. tags))
  12706. ((not org-use-tag-inheritance) nil)
  12707. ((stringp org-use-tag-inheritance)
  12708. (delq nil (mapcar
  12709. (lambda (x)
  12710. (if (and (string-match org-use-tag-inheritance x)
  12711. (not (member x org-tags-exclude-from-inheritance)))
  12712. x nil))
  12713. tags)))
  12714. ((listp org-use-tag-inheritance)
  12715. (delq nil (mapcar
  12716. (lambda (x)
  12717. (if (member x org-use-tag-inheritance) x nil))
  12718. tags)))))
  12719. (defun org-match-sparse-tree (&optional todo-only match)
  12720. "Create a sparse tree according to tags string MATCH.
  12721. MATCH can contain positive and negative selection of tags, like
  12722. \"+WORK+URGENT-WITHBOSS\".
  12723. If optional argument TODO-ONLY is non-nil, only select lines that are
  12724. also TODO lines."
  12725. (interactive "P")
  12726. (org-agenda-prepare-buffers (list (current-buffer)))
  12727. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12728. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12729. (defvar org-cached-props nil)
  12730. (defun org-cached-entry-get (pom property)
  12731. (if (or (eq t org-use-property-inheritance)
  12732. (and (stringp org-use-property-inheritance)
  12733. (string-match org-use-property-inheritance property))
  12734. (and (listp org-use-property-inheritance)
  12735. (member property org-use-property-inheritance)))
  12736. ;; Caching is not possible, check it directly
  12737. (org-entry-get pom property 'inherit)
  12738. ;; Get all properties, so that we can do complicated checks easily
  12739. (cdr (assoc property (or org-cached-props
  12740. (setq org-cached-props
  12741. (org-entry-properties pom)))))))
  12742. (defun org-global-tags-completion-table (&optional files)
  12743. "Return the list of all tags in all agenda buffer/files.
  12744. Optional FILES argument is a list of files which can be used
  12745. instead of the agenda files."
  12746. (save-excursion
  12747. (org-uniquify
  12748. (delq nil
  12749. (apply 'append
  12750. (mapcar
  12751. (lambda (file)
  12752. (set-buffer (find-file-noselect file))
  12753. (append (org-get-buffer-tags)
  12754. (mapcar (lambda (x) (if (stringp (car-safe x))
  12755. (list (car-safe x)) nil))
  12756. org-tag-alist)))
  12757. (if (and files (car files))
  12758. files
  12759. (org-agenda-files))))))))
  12760. (defun org-make-tags-matcher (match)
  12761. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12762. The variable `todo-only' is scoped dynamically into this function.
  12763. It will be set to t if the matcher restricts matching to TODO entries,
  12764. otherwise will not be touched.
  12765. Returns a cons of the selection string MATCH and the constructed
  12766. lisp form implementing the matcher. The matcher is to be evaluated
  12767. at an Org entry, with point on the headline, and returns t if the
  12768. entry matches the selection string MATCH. The returned lisp form
  12769. references two variables with information about the entry, which
  12770. must be bound around the form's evaluation: todo, the TODO keyword
  12771. at the entry (or nil of none); and tags-list, the list of all tags
  12772. at the entry including inherited ones. Additionally, the category
  12773. of the entry (if any) must be specified as the text property
  12774. 'org-category on the headline.
  12775. See also `org-scan-tags'.
  12776. "
  12777. (declare (special todo-only))
  12778. (unless (boundp 'todo-only)
  12779. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12780. (unless match
  12781. ;; Get a new match request, with completion against the global
  12782. ;; tags table and the local tags in current buffer
  12783. (let ((org-last-tags-completion-table
  12784. (org-uniquify
  12785. (delq nil (append (org-get-buffer-tags)
  12786. (org-global-tags-completion-table))))))
  12787. (setq match (org-completing-read-no-i
  12788. "Match: " 'org-tags-completion-function nil nil nil
  12789. 'org-tags-history))))
  12790. ;; Parse the string and create a lisp form
  12791. (let ((match0 match)
  12792. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12793. minus tag mm
  12794. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12795. orterms term orlist re-p str-p level-p level-op time-p
  12796. prop-p pn pv po gv rest)
  12797. ;; Expand group tags
  12798. (setq match (org-tags-expand match))
  12799. (if (string-match "/+" match)
  12800. ;; match contains also a todo-matching request
  12801. (progn
  12802. (setq tagsmatch (substring match 0 (match-beginning 0))
  12803. todomatch (substring match (match-end 0)))
  12804. (if (string-match "^!" todomatch)
  12805. (setq todo-only t todomatch (substring todomatch 1)))
  12806. (if (string-match "^\\s-*$" todomatch)
  12807. (setq todomatch nil)))
  12808. ;; only matching tags
  12809. (setq tagsmatch match todomatch nil))
  12810. ;; Make the tags matcher
  12811. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12812. (setq tagsmatcher t)
  12813. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12814. (while (setq term (pop orterms))
  12815. (while (and (equal (substring term -1) "\\") orterms)
  12816. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12817. (while (string-match re term)
  12818. (setq rest (substring term (match-end 0))
  12819. minus (and (match-end 1)
  12820. (equal (match-string 1 term) "-"))
  12821. tag (save-match-data (replace-regexp-in-string
  12822. "\\\\-" "-"
  12823. (match-string 2 term)))
  12824. re-p (equal (string-to-char tag) ?{)
  12825. level-p (match-end 4)
  12826. prop-p (match-end 5)
  12827. mm (cond
  12828. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12829. (level-p
  12830. (setq level-op (org-op-to-function (match-string 3 term)))
  12831. `(,level-op level ,(string-to-number
  12832. (match-string 4 term))))
  12833. (prop-p
  12834. (setq pn (match-string 5 term)
  12835. po (match-string 6 term)
  12836. pv (match-string 7 term)
  12837. re-p (equal (string-to-char pv) ?{)
  12838. str-p (equal (string-to-char pv) ?\")
  12839. time-p (save-match-data
  12840. (string-match "^\"[[<].*[]>]\"$" pv))
  12841. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12842. (if time-p (setq pv (org-matcher-time pv)))
  12843. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12844. (cond
  12845. ((equal pn "CATEGORY")
  12846. (setq gv '(get-text-property (point) 'org-category)))
  12847. ((equal pn "TODO")
  12848. (setq gv 'todo))
  12849. (t
  12850. (setq gv `(org-cached-entry-get nil ,pn))))
  12851. (if re-p
  12852. (if (eq po 'org<>)
  12853. `(not (string-match ,pv (or ,gv "")))
  12854. `(string-match ,pv (or ,gv "")))
  12855. (if str-p
  12856. `(,po (or ,gv "") ,pv)
  12857. `(,po (string-to-number (or ,gv ""))
  12858. ,(string-to-number pv) ))))
  12859. (t `(member ,tag tags-list)))
  12860. mm (if minus (list 'not mm) mm)
  12861. term rest)
  12862. (push mm tagsmatcher))
  12863. (push (if (> (length tagsmatcher) 1)
  12864. (cons 'and tagsmatcher)
  12865. (car tagsmatcher))
  12866. orlist)
  12867. (setq tagsmatcher nil))
  12868. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12869. (setq tagsmatcher
  12870. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12871. ;; Make the todo matcher
  12872. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12873. (setq todomatcher t)
  12874. (setq orterms (org-split-string todomatch "|") orlist nil)
  12875. (while (setq term (pop orterms))
  12876. (while (string-match re term)
  12877. (setq minus (and (match-end 1)
  12878. (equal (match-string 1 term) "-"))
  12879. kwd (match-string 2 term)
  12880. re-p (equal (string-to-char kwd) ?{)
  12881. term (substring term (match-end 0))
  12882. mm (if re-p
  12883. `(string-match ,(substring kwd 1 -1) todo)
  12884. (list 'equal 'todo kwd))
  12885. mm (if minus (list 'not mm) mm))
  12886. (push mm todomatcher))
  12887. (push (if (> (length todomatcher) 1)
  12888. (cons 'and todomatcher)
  12889. (car todomatcher))
  12890. orlist)
  12891. (setq todomatcher nil))
  12892. (setq todomatcher (if (> (length orlist) 1)
  12893. (cons 'or orlist) (car orlist))))
  12894. ;; Return the string and lisp forms of the matcher
  12895. (setq matcher (if todomatcher
  12896. (list 'and tagsmatcher todomatcher)
  12897. tagsmatcher))
  12898. (when todo-only
  12899. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12900. matcher)))
  12901. (cons match0 matcher)))
  12902. (defun org-tags-expand (match &optional single-as-list downcased)
  12903. "Expand group tags in MATCH.
  12904. This replaces every group tag in MATCH with a regexp tag search.
  12905. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12906. will be replaced like this:
  12907. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12908. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12909. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12910. Replacing by a regexp preserves the structure of the match.
  12911. E.g., this expansion
  12912. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12913. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12914. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12915. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12916. assumed to be a single group tag, and the function will return
  12917. the list of tags in this group.
  12918. When DOWNCASE is non-nil, expand downcased TAGS."
  12919. (if org-group-tags
  12920. (let* ((case-fold-search t)
  12921. (stable org-mode-syntax-table)
  12922. (tal (or org-tag-groups-alist-for-agenda
  12923. org-tag-groups-alist))
  12924. (tal (if downcased
  12925. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12926. (tml (mapcar 'car tal))
  12927. (rtnmatch match) rpl)
  12928. ;; @ and _ are allowed as word-components in tags
  12929. (modify-syntax-entry ?@ "w" stable)
  12930. (modify-syntax-entry ?_ "w" stable)
  12931. (while (and tml
  12932. (string-match
  12933. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12934. (regexp-opt tml) "\\>\\)") rtnmatch))
  12935. (let* ((dir (match-string 1 rtnmatch))
  12936. (tag (match-string 2 rtnmatch))
  12937. (tag (if downcased (downcase tag) tag)))
  12938. (setq tml (delete tag tml))
  12939. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12940. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12941. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12942. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12943. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12944. (if single-as-list
  12945. (or (reverse rpl) (list rtnmatch))
  12946. rtnmatch))
  12947. (if single-as-list (list (if downcased (downcase match) match))
  12948. match)))
  12949. (defun org-op-to-function (op &optional stringp)
  12950. "Turn an operator into the appropriate function."
  12951. (setq op
  12952. (cond
  12953. ((equal op "<" ) '(< string< org-time<))
  12954. ((equal op ">" ) '(> org-string> org-time>))
  12955. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12956. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12957. ((member op '("=" "==")) '(= string= org-time=))
  12958. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12959. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12960. (defun org<> (a b) (not (= a b)))
  12961. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12962. (defun org-string>= (a b) (not (string< a b)))
  12963. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12964. (defun org-string<> (a b) (not (string= a b)))
  12965. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12966. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12967. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12968. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12969. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12970. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12971. (defun org-2ft (s)
  12972. "Convert S to a floating point time.
  12973. If S is already a number, just return it. If it is a string, parse
  12974. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12975. (cond
  12976. ((numberp s) s)
  12977. ((stringp s)
  12978. (condition-case nil
  12979. (float-time (apply 'encode-time (org-parse-time-string s)))
  12980. (error 0.)))
  12981. (t 0.)))
  12982. (defun org-time-today ()
  12983. "Time in seconds today at 0:00.
  12984. Returns the float number of seconds since the beginning of the
  12985. epoch to the beginning of today (00:00)."
  12986. (float-time (apply 'encode-time
  12987. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12988. (defun org-matcher-time (s)
  12989. "Interpret a time comparison value."
  12990. (save-match-data
  12991. (cond
  12992. ((string= s "<now>") (float-time))
  12993. ((string= s "<today>") (org-time-today))
  12994. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12995. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12996. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12997. (+ (org-time-today)
  12998. (* (string-to-number (match-string 1 s))
  12999. (cdr (assoc (match-string 2 s)
  13000. '(("d" . 86400.0) ("w" . 604800.0)
  13001. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13002. (t (org-2ft s)))))
  13003. (defun org-match-any-p (re list)
  13004. "Does re match any element of list?"
  13005. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13006. (delq nil list))
  13007. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13008. (defvar org-tags-overlay (make-overlay 1 1))
  13009. (org-detach-overlay org-tags-overlay)
  13010. (defun org-get-local-tags-at (&optional pos)
  13011. "Get a list of tags defined in the current headline."
  13012. (org-get-tags-at pos 'local))
  13013. (defun org-get-local-tags ()
  13014. "Get a list of tags defined in the current headline."
  13015. (org-get-tags-at nil 'local))
  13016. (defun org-get-tags-at (&optional pos local)
  13017. "Get a list of all headline tags applicable at POS.
  13018. POS defaults to point. If tags are inherited, the list contains
  13019. the targets in the same sequence as the headlines appear, i.e.
  13020. the tags of the current headline come last.
  13021. When LOCAL is non-nil, only return tags from the current headline,
  13022. ignore inherited ones."
  13023. (interactive)
  13024. (if (and org-trust-scanner-tags
  13025. (or (not pos) (equal pos (point)))
  13026. (not local))
  13027. org-scanner-tags
  13028. (let (tags ltags lastpos parent)
  13029. (save-excursion
  13030. (save-restriction
  13031. (widen)
  13032. (goto-char (or pos (point)))
  13033. (save-match-data
  13034. (catch 'done
  13035. (condition-case nil
  13036. (progn
  13037. (org-back-to-heading t)
  13038. (while (not (equal lastpos (point)))
  13039. (setq lastpos (point))
  13040. (when (looking-at
  13041. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13042. (setq ltags (org-split-string
  13043. (org-match-string-no-properties 1) ":"))
  13044. (when parent
  13045. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13046. (setq tags (append
  13047. (if parent
  13048. (org-remove-uninherited-tags ltags)
  13049. ltags)
  13050. tags)))
  13051. (or org-use-tag-inheritance (throw 'done t))
  13052. (if local (throw 'done t))
  13053. (or (org-up-heading-safe) (error nil))
  13054. (setq parent t)))
  13055. (error nil)))))
  13056. (if local
  13057. tags
  13058. (reverse (delete-dups
  13059. (reverse (append
  13060. (org-remove-uninherited-tags
  13061. org-file-tags) tags)))))))))
  13062. (defun org-add-prop-inherited (s)
  13063. (add-text-properties 0 (length s) '(inherited t) s)
  13064. s)
  13065. (defun org-toggle-tag (tag &optional onoff)
  13066. "Toggle the tag TAG for the current line.
  13067. If ONOFF is `on' or `off', don't toggle but set to this state."
  13068. (let (res current)
  13069. (save-excursion
  13070. (org-back-to-heading t)
  13071. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13072. (point-at-eol) t)
  13073. (progn
  13074. (setq current (match-string 1))
  13075. (replace-match ""))
  13076. (setq current ""))
  13077. (setq current (nreverse (org-split-string current ":")))
  13078. (cond
  13079. ((eq onoff 'on)
  13080. (setq res t)
  13081. (or (member tag current) (push tag current)))
  13082. ((eq onoff 'off)
  13083. (or (not (member tag current)) (setq current (delete tag current))))
  13084. (t (if (member tag current)
  13085. (setq current (delete tag current))
  13086. (setq res t)
  13087. (push tag current))))
  13088. (end-of-line 1)
  13089. (if current
  13090. (progn
  13091. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13092. (org-set-tags nil t))
  13093. (delete-horizontal-space))
  13094. (run-hooks 'org-after-tags-change-hook))
  13095. res))
  13096. (defun org-align-tags-here (to-col)
  13097. ;; Assumes that this is a headline
  13098. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13099. (beginning-of-line 1)
  13100. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13101. (< pos (match-beginning 2)))
  13102. (progn
  13103. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13104. (goto-char (match-beginning 1))
  13105. (insert " ")
  13106. (delete-region (point) (1+ (match-beginning 2)))
  13107. (setq ncol (max (current-column)
  13108. (1+ col)
  13109. (if (> to-col 0)
  13110. to-col
  13111. (- (abs to-col) tags-l))))
  13112. (setq p (point))
  13113. (insert (make-string (- ncol (current-column)) ?\ ))
  13114. (setq ncol (current-column))
  13115. (when indent-tabs-mode (tabify p (point-at-eol)))
  13116. (org-move-to-column (min ncol col) t))
  13117. (goto-char pos))))
  13118. (defun org-set-tags-command (&optional arg just-align)
  13119. "Call the set-tags command for the current entry."
  13120. (interactive "P")
  13121. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13122. (org-set-tags arg just-align)
  13123. (save-excursion
  13124. (unless (and (org-region-active-p)
  13125. org-loop-over-headlines-in-active-region)
  13126. (org-back-to-heading t))
  13127. (org-set-tags arg just-align))))
  13128. (defun org-set-tags-to (data)
  13129. "Set the tags of the current entry to DATA, replacing the current tags.
  13130. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13131. If DATA is nil or the empty string, any tags will be removed."
  13132. (interactive "sTags: ")
  13133. (setq data
  13134. (cond
  13135. ((eq data nil) "")
  13136. ((equal data "") "")
  13137. ((stringp data)
  13138. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13139. ":"))
  13140. ((listp data)
  13141. (concat ":" (mapconcat 'identity data ":") ":"))))
  13142. (when data
  13143. (save-excursion
  13144. (org-back-to-heading t)
  13145. (when (looking-at org-complex-heading-regexp)
  13146. (if (match-end 5)
  13147. (progn
  13148. (goto-char (match-beginning 5))
  13149. (insert data)
  13150. (delete-region (point) (point-at-eol))
  13151. (org-set-tags nil 'align))
  13152. (goto-char (point-at-eol))
  13153. (insert " " data)
  13154. (org-set-tags nil 'align)))
  13155. (beginning-of-line 1)
  13156. (if (looking-at ".*?\\([ \t]+\\)$")
  13157. (delete-region (match-beginning 1) (match-end 1))))))
  13158. (defun org-align-all-tags ()
  13159. "Align the tags i all headings."
  13160. (interactive)
  13161. (save-excursion
  13162. (or (ignore-errors (org-back-to-heading t))
  13163. (outline-next-heading))
  13164. (if (org-at-heading-p)
  13165. (org-set-tags t)
  13166. (message "No headings"))))
  13167. (defvar org-indent-indentation-per-level)
  13168. (defun org-set-tags (&optional arg just-align)
  13169. "Set the tags for the current headline.
  13170. With prefix ARG, realign all tags in headings in the current buffer."
  13171. (interactive "P")
  13172. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13173. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13174. 'region-start-level 'region))
  13175. org-loop-over-headlines-in-active-region)
  13176. (org-map-entries
  13177. ;; We don't use ARG and JUST-ALIGN here these args are not
  13178. ;; useful when looping over headlines
  13179. `(org-set-tags)
  13180. org-loop-over-headlines-in-active-region
  13181. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13182. (let* ((re org-outline-regexp-bol)
  13183. (current (unless arg (org-get-tags-string)))
  13184. (col (current-column))
  13185. (org-setting-tags t)
  13186. table current-tags inherited-tags ; computed below when needed
  13187. tags p0 c0 c1 rpl di tc level)
  13188. (if arg
  13189. (save-excursion
  13190. (goto-char (point-min))
  13191. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13192. (while (re-search-forward re nil t)
  13193. (org-set-tags nil t)
  13194. (end-of-line 1)))
  13195. (message "All tags realigned to column %d" org-tags-column))
  13196. (if just-align
  13197. (setq tags current)
  13198. ;; Get a new set of tags from the user
  13199. (save-excursion
  13200. (setq table (append org-tag-persistent-alist
  13201. (or org-tag-alist (org-get-buffer-tags))
  13202. (and
  13203. org-complete-tags-always-offer-all-agenda-tags
  13204. (org-global-tags-completion-table
  13205. (org-agenda-files))))
  13206. org-last-tags-completion-table table
  13207. current-tags (org-split-string current ":")
  13208. inherited-tags (nreverse
  13209. (nthcdr (length current-tags)
  13210. (nreverse (org-get-tags-at))))
  13211. tags
  13212. (if (or (eq t org-use-fast-tag-selection)
  13213. (and org-use-fast-tag-selection
  13214. (delq nil (mapcar 'cdr table))))
  13215. (org-fast-tag-selection
  13216. current-tags inherited-tags table
  13217. (if org-fast-tag-selection-include-todo
  13218. org-todo-key-alist))
  13219. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13220. (org-trim
  13221. (org-icompleting-read "Tags: "
  13222. 'org-tags-completion-function
  13223. nil nil current 'org-tags-history))))))
  13224. (while (string-match "[-+&]+" tags)
  13225. ;; No boolean logic, just a list
  13226. (setq tags (replace-match ":" t t tags))))
  13227. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13228. (if org-tags-sort-function
  13229. (setq tags (mapconcat 'identity
  13230. (sort (org-split-string
  13231. tags (org-re "[^[:alnum:]_@#%]+"))
  13232. org-tags-sort-function) ":")))
  13233. (if (string-match "\\`[\t ]*\\'" tags)
  13234. (setq tags "")
  13235. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13236. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13237. ;; Insert new tags at the correct column
  13238. (beginning-of-line 1)
  13239. (setq level (or (and (looking-at org-outline-regexp)
  13240. (- (match-end 0) (point) 1))
  13241. 1))
  13242. (cond
  13243. ((and (equal current "") (equal tags "")))
  13244. ((re-search-forward
  13245. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13246. (point-at-eol) t)
  13247. (if (equal tags "")
  13248. (setq rpl "")
  13249. (goto-char (match-beginning 0))
  13250. (setq c0 (current-column)
  13251. ;; compute offset for the case of org-indent-mode active
  13252. di (if org-indent-mode
  13253. (* (1- org-indent-indentation-per-level) (1- level))
  13254. 0)
  13255. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13256. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13257. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13258. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13259. (replace-match rpl t t)
  13260. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13261. tags)
  13262. (t (error "Tags alignment failed")))
  13263. (org-move-to-column col)
  13264. (unless just-align
  13265. (run-hooks 'org-after-tags-change-hook))))))
  13266. (defun org-change-tag-in-region (beg end tag off)
  13267. "Add or remove TAG for each entry in the region.
  13268. This works in the agenda, and also in an org-mode buffer."
  13269. (interactive
  13270. (list (region-beginning) (region-end)
  13271. (let ((org-last-tags-completion-table
  13272. (if (derived-mode-p 'org-mode)
  13273. (org-uniquify
  13274. (delq nil (append (org-get-buffer-tags)
  13275. (org-global-tags-completion-table))))
  13276. (org-global-tags-completion-table))))
  13277. (org-icompleting-read
  13278. "Tag: " 'org-tags-completion-function nil nil nil
  13279. 'org-tags-history))
  13280. (progn
  13281. (message "[s]et or [r]emove? ")
  13282. (equal (read-char-exclusive) ?r))))
  13283. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13284. (let ((agendap (equal major-mode 'org-agenda-mode))
  13285. l1 l2 m buf pos newhead (cnt 0))
  13286. (goto-char end)
  13287. (setq l2 (1- (org-current-line)))
  13288. (goto-char beg)
  13289. (setq l1 (org-current-line))
  13290. (loop for l from l1 to l2 do
  13291. (org-goto-line l)
  13292. (setq m (get-text-property (point) 'org-hd-marker))
  13293. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13294. (and agendap m))
  13295. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13296. pos (if agendap m (point)))
  13297. (with-current-buffer buf
  13298. (save-excursion
  13299. (save-restriction
  13300. (goto-char pos)
  13301. (setq cnt (1+ cnt))
  13302. (org-toggle-tag tag (if off 'off 'on))
  13303. (setq newhead (org-get-heading)))))
  13304. (and agendap (org-agenda-change-all-lines newhead m))))
  13305. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13306. (defun org-tags-completion-function (string predicate &optional flag)
  13307. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13308. (confirm (lambda (x) (stringp (car x)))))
  13309. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13310. (setq s1 (match-string 1 string)
  13311. s2 (match-string 2 string))
  13312. (setq s1 "" s2 string))
  13313. (cond
  13314. ((eq flag nil)
  13315. ;; try completion
  13316. (setq rtn (try-completion s2 ctable confirm))
  13317. (if (stringp rtn)
  13318. (setq rtn
  13319. (concat s1 s2 (substring rtn (length s2))
  13320. (if (and org-add-colon-after-tag-completion
  13321. (assoc rtn ctable))
  13322. ":" ""))))
  13323. rtn)
  13324. ((eq flag t)
  13325. ;; all-completions
  13326. (all-completions s2 ctable confirm))
  13327. ((eq flag 'lambda)
  13328. ;; exact match?
  13329. (assoc s2 ctable)))))
  13330. (defun org-fast-tag-insert (kwd tags face &optional end)
  13331. "Insert KDW, and the TAGS, the latter with face FACE.
  13332. Also insert END."
  13333. (insert (format "%-12s" (concat kwd ":"))
  13334. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13335. (or end "")))
  13336. (defun org-fast-tag-show-exit (flag)
  13337. (save-excursion
  13338. (org-goto-line 3)
  13339. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13340. (replace-match ""))
  13341. (when flag
  13342. (end-of-line 1)
  13343. (org-move-to-column (- (window-width) 19) t)
  13344. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13345. (defun org-set-current-tags-overlay (current prefix)
  13346. "Add an overlay to CURRENT tag with PREFIX."
  13347. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13348. (if (featurep 'xemacs)
  13349. (org-overlay-display org-tags-overlay (concat prefix s)
  13350. 'secondary-selection)
  13351. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13352. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13353. (defvar org-last-tag-selection-key nil)
  13354. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13355. "Fast tag selection with single keys.
  13356. CURRENT is the current list of tags in the headline, INHERITED is the
  13357. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13358. possibly with grouping information. TODO-TABLE is a similar table with
  13359. TODO keywords, should these have keys assigned to them.
  13360. If the keys are nil, a-z are automatically assigned.
  13361. Returns the new tags string, or nil to not change the current settings."
  13362. (let* ((fulltable (append table todo-table))
  13363. (maxlen (apply 'max (mapcar
  13364. (lambda (x)
  13365. (if (stringp (car x)) (string-width (car x)) 0))
  13366. fulltable)))
  13367. (buf (current-buffer))
  13368. (expert (eq org-fast-tag-selection-single-key 'expert))
  13369. (buffer-tags nil)
  13370. (fwidth (+ maxlen 3 1 3))
  13371. (ncol (/ (- (window-width) 4) fwidth))
  13372. (i-face 'org-done)
  13373. (c-face 'org-todo)
  13374. tg cnt e c char c1 c2 ntable tbl rtn
  13375. ov-start ov-end ov-prefix
  13376. (exit-after-next org-fast-tag-selection-single-key)
  13377. (done-keywords org-done-keywords)
  13378. groups ingroup)
  13379. (save-excursion
  13380. (beginning-of-line 1)
  13381. (if (looking-at
  13382. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13383. (setq ov-start (match-beginning 1)
  13384. ov-end (match-end 1)
  13385. ov-prefix "")
  13386. (setq ov-start (1- (point-at-eol))
  13387. ov-end (1+ ov-start))
  13388. (skip-chars-forward "^\n\r")
  13389. (setq ov-prefix
  13390. (concat
  13391. (buffer-substring (1- (point)) (point))
  13392. (if (> (current-column) org-tags-column)
  13393. " "
  13394. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13395. (move-overlay org-tags-overlay ov-start ov-end)
  13396. (save-window-excursion
  13397. (if expert
  13398. (set-buffer (get-buffer-create " *Org tags*"))
  13399. (delete-other-windows)
  13400. (split-window-vertically)
  13401. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13402. (erase-buffer)
  13403. (org-set-local 'org-done-keywords done-keywords)
  13404. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13405. (org-fast-tag-insert "Current" current c-face "\n\n")
  13406. (org-fast-tag-show-exit exit-after-next)
  13407. (org-set-current-tags-overlay current ov-prefix)
  13408. (setq tbl fulltable char ?a cnt 0)
  13409. (while (setq e (pop tbl))
  13410. (cond
  13411. ((equal (car e) :startgroup)
  13412. (push '() groups) (setq ingroup t)
  13413. (when (not (= cnt 0))
  13414. (setq cnt 0)
  13415. (insert "\n"))
  13416. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13417. ((equal (car e) :endgroup)
  13418. (setq ingroup nil cnt 0)
  13419. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13420. ((equal e '(:newline))
  13421. (when (not (= cnt 0))
  13422. (setq cnt 0)
  13423. (insert "\n")
  13424. (setq e (car tbl))
  13425. (while (equal (car tbl) '(:newline))
  13426. (insert "\n")
  13427. (setq tbl (cdr tbl)))))
  13428. ((equal e '(:grouptags)) nil)
  13429. (t
  13430. (setq tg (copy-sequence (car e)) c2 nil)
  13431. (if (cdr e)
  13432. (setq c (cdr e))
  13433. ;; automatically assign a character.
  13434. (setq c1 (string-to-char
  13435. (downcase (substring
  13436. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13437. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13438. (while (or (rassoc char ntable) (rassoc char table))
  13439. (setq char (1+ char)))
  13440. (setq c2 c1))
  13441. (setq c (or c2 char)))
  13442. (if ingroup (push tg (car groups)))
  13443. (setq tg (org-add-props tg nil 'face
  13444. (cond
  13445. ((not (assoc tg table))
  13446. (org-get-todo-face tg))
  13447. ((member tg current) c-face)
  13448. ((member tg inherited) i-face))))
  13449. (if (equal (caar tbl) :grouptags)
  13450. (org-add-props tg nil 'face 'org-tag-group))
  13451. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13452. (insert "[" c "] " tg (make-string
  13453. (- fwidth 4 (length tg)) ?\ ))
  13454. (push (cons tg c) ntable)
  13455. (when (= (setq cnt (1+ cnt)) ncol)
  13456. (insert "\n")
  13457. (if ingroup (insert " "))
  13458. (setq cnt 0)))))
  13459. (setq ntable (nreverse ntable))
  13460. (insert "\n")
  13461. (goto-char (point-min))
  13462. (if (not expert) (org-fit-window-to-buffer))
  13463. (setq rtn
  13464. (catch 'exit
  13465. (while t
  13466. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13467. (if (not groups) "no " "")
  13468. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13469. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13470. (setq org-last-tag-selection-key c)
  13471. (cond
  13472. ((= c ?\r) (throw 'exit t))
  13473. ((= c ?!)
  13474. (setq groups (not groups))
  13475. (goto-char (point-min))
  13476. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13477. ((= c ?\C-c)
  13478. (if (not expert)
  13479. (org-fast-tag-show-exit
  13480. (setq exit-after-next (not exit-after-next)))
  13481. (setq expert nil)
  13482. (delete-other-windows)
  13483. (set-window-buffer (split-window-vertically) " *Org tags*")
  13484. (org-switch-to-buffer-other-window " *Org tags*")
  13485. (org-fit-window-to-buffer)))
  13486. ((or (= c ?\C-g)
  13487. (and (= c ?q) (not (rassoc c ntable))))
  13488. (org-detach-overlay org-tags-overlay)
  13489. (setq quit-flag t))
  13490. ((= c ?\ )
  13491. (setq current nil)
  13492. (if exit-after-next (setq exit-after-next 'now)))
  13493. ((= c ?\t)
  13494. (condition-case nil
  13495. (setq tg (org-icompleting-read
  13496. "Tag: "
  13497. (or buffer-tags
  13498. (with-current-buffer buf
  13499. (org-get-buffer-tags)))))
  13500. (quit (setq tg "")))
  13501. (when (string-match "\\S-" tg)
  13502. (add-to-list 'buffer-tags (list tg))
  13503. (if (member tg current)
  13504. (setq current (delete tg current))
  13505. (push tg current)))
  13506. (if exit-after-next (setq exit-after-next 'now)))
  13507. ((setq e (rassoc c todo-table) tg (car e))
  13508. (with-current-buffer buf
  13509. (save-excursion (org-todo tg)))
  13510. (if exit-after-next (setq exit-after-next 'now)))
  13511. ((setq e (rassoc c ntable) tg (car e))
  13512. (if (member tg current)
  13513. (setq current (delete tg current))
  13514. (loop for g in groups do
  13515. (if (member tg g)
  13516. (mapc (lambda (x)
  13517. (setq current (delete x current)))
  13518. g)))
  13519. (push tg current))
  13520. (if exit-after-next (setq exit-after-next 'now))))
  13521. ;; Create a sorted list
  13522. (setq current
  13523. (sort current
  13524. (lambda (a b)
  13525. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13526. (if (eq exit-after-next 'now) (throw 'exit t))
  13527. (goto-char (point-min))
  13528. (beginning-of-line 2)
  13529. (delete-region (point) (point-at-eol))
  13530. (org-fast-tag-insert "Current" current c-face)
  13531. (org-set-current-tags-overlay current ov-prefix)
  13532. (while (re-search-forward
  13533. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13534. (setq tg (match-string 1))
  13535. (add-text-properties
  13536. (match-beginning 1) (match-end 1)
  13537. (list 'face
  13538. (cond
  13539. ((member tg current) c-face)
  13540. ((member tg inherited) i-face)
  13541. (t (get-text-property (match-beginning 1) 'face))))))
  13542. (goto-char (point-min)))))
  13543. (org-detach-overlay org-tags-overlay)
  13544. (if rtn
  13545. (mapconcat 'identity current ":")
  13546. nil))))
  13547. (defun org-get-tags-string ()
  13548. "Get the TAGS string in the current headline."
  13549. (unless (org-at-heading-p t)
  13550. (user-error "Not on a heading"))
  13551. (save-excursion
  13552. (beginning-of-line 1)
  13553. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13554. (org-match-string-no-properties 1)
  13555. "")))
  13556. (defun org-get-tags ()
  13557. "Get the list of tags specified in the current headline."
  13558. (org-split-string (org-get-tags-string) ":"))
  13559. (defun org-get-buffer-tags ()
  13560. "Get a table of all tags used in the buffer, for completion."
  13561. (let (tags)
  13562. (save-excursion
  13563. (goto-char (point-min))
  13564. (while (re-search-forward
  13565. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13566. (when (equal (char-after (point-at-bol 0)) ?*)
  13567. (mapc (lambda (x) (add-to-list 'tags x))
  13568. (org-split-string (org-match-string-no-properties 1) ":")))))
  13569. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13570. (mapcar 'list tags)))
  13571. ;;;; The mapping API
  13572. (defun org-map-entries (func &optional match scope &rest skip)
  13573. "Call FUNC at each headline selected by MATCH in SCOPE.
  13574. FUNC is a function or a lisp form. The function will be called without
  13575. arguments, with the cursor positioned at the beginning of the headline.
  13576. The return values of all calls to the function will be collected and
  13577. returned as a list.
  13578. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13579. does not need to preserve point. After evaluation, the cursor will be
  13580. moved to the end of the line (presumably of the headline of the
  13581. processed entry) and search continues from there. Under some
  13582. circumstances, this may not produce the wanted results. For example,
  13583. if you have removed (e.g. archived) the current (sub)tree it could
  13584. mean that the next entry will be skipped entirely. In such cases, you
  13585. can specify the position from where search should continue by making
  13586. FUNC set the variable `org-map-continue-from' to the desired buffer
  13587. position.
  13588. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13589. Only headlines that are matched by this query will be considered during
  13590. the iteration. When MATCH is nil or t, all headlines will be
  13591. visited by the iteration.
  13592. SCOPE determines the scope of this command. It can be any of:
  13593. nil The current buffer, respecting the restriction if any
  13594. tree The subtree started with the entry at point
  13595. region The entries within the active region, if any
  13596. region-start-level
  13597. The entries within the active region, but only those at
  13598. the same level than the first one.
  13599. file The current buffer, without restriction
  13600. file-with-archives
  13601. The current buffer, and any archives associated with it
  13602. agenda All agenda files
  13603. agenda-with-archives
  13604. All agenda files with any archive files associated with them
  13605. \(file1 file2 ...)
  13606. If this is a list, all files in the list will be scanned
  13607. The remaining args are treated as settings for the skipping facilities of
  13608. the scanner. The following items can be given here:
  13609. archive skip trees with the archive tag
  13610. comment skip trees with the COMMENT keyword
  13611. function or Emacs Lisp form:
  13612. will be used as value for `org-agenda-skip-function', so
  13613. whenever the function returns a position, FUNC will not be
  13614. called for that entry and search will continue from the
  13615. position returned
  13616. If your function needs to retrieve the tags including inherited tags
  13617. at the *current* entry, you can use the value of the variable
  13618. `org-scanner-tags' which will be much faster than getting the value
  13619. with `org-get-tags-at'. If your function gets properties with
  13620. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13621. to t around the call to `org-entry-properties' to get the same speedup.
  13622. Note that if your function moves around to retrieve tags and properties at
  13623. a *different* entry, you cannot use these techniques."
  13624. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13625. (not (org-region-active-p)))
  13626. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13627. (org-agenda-skip-archived-trees (memq 'archive skip))
  13628. (org-agenda-skip-comment-trees (memq 'comment skip))
  13629. (org-agenda-skip-function
  13630. (car (org-delete-all '(comment archive) skip)))
  13631. (org-tags-match-list-sublevels t)
  13632. (start-level (eq scope 'region-start-level))
  13633. matcher file res
  13634. org-todo-keywords-for-agenda
  13635. org-done-keywords-for-agenda
  13636. org-todo-keyword-alist-for-agenda
  13637. org-drawers-for-agenda
  13638. org-tag-alist-for-agenda
  13639. todo-only)
  13640. (cond
  13641. ((eq match t) (setq matcher t))
  13642. ((eq match nil) (setq matcher t))
  13643. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13644. (save-window-excursion
  13645. (save-restriction
  13646. (cond ((eq scope 'tree)
  13647. (org-back-to-heading t)
  13648. (org-narrow-to-subtree)
  13649. (setq scope nil))
  13650. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13651. (org-region-active-p))
  13652. ;; If needed, set start-level to a string like "2"
  13653. (when start-level
  13654. (save-excursion
  13655. (goto-char (region-beginning))
  13656. (unless (org-at-heading-p) (outline-next-heading))
  13657. (setq start-level (org-current-level))))
  13658. (narrow-to-region (region-beginning)
  13659. (save-excursion
  13660. (goto-char (region-end))
  13661. (unless (and (bolp) (org-at-heading-p))
  13662. (outline-next-heading))
  13663. (point)))
  13664. (setq scope nil)))
  13665. (if (not scope)
  13666. (progn
  13667. (org-agenda-prepare-buffers
  13668. (list (buffer-file-name (current-buffer))))
  13669. (setq res (org-scan-tags func matcher todo-only start-level)))
  13670. ;; Get the right scope
  13671. (cond
  13672. ((and scope (listp scope) (symbolp (car scope)))
  13673. (setq scope (eval scope)))
  13674. ((eq scope 'agenda)
  13675. (setq scope (org-agenda-files t)))
  13676. ((eq scope 'agenda-with-archives)
  13677. (setq scope (org-agenda-files t))
  13678. (setq scope (org-add-archive-files scope)))
  13679. ((eq scope 'file)
  13680. (setq scope (list (buffer-file-name))))
  13681. ((eq scope 'file-with-archives)
  13682. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13683. (org-agenda-prepare-buffers scope)
  13684. (while (setq file (pop scope))
  13685. (with-current-buffer (org-find-base-buffer-visiting file)
  13686. (save-excursion
  13687. (save-restriction
  13688. (widen)
  13689. (goto-char (point-min))
  13690. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13691. res)))
  13692. ;;;; Properties
  13693. ;;; Setting and retrieving properties
  13694. (defconst org-special-properties
  13695. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13696. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13697. "The special properties valid in Org-mode.
  13698. These are properties that are not defined in the property drawer,
  13699. but in some other way.")
  13700. (defconst org-default-properties
  13701. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13702. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13703. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13704. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13705. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13706. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13707. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13708. "Some properties that are used by Org-mode for various purposes.
  13709. Being in this list makes sure that they are offered for completion.")
  13710. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13711. "Regular expression matching the first line of a property drawer.")
  13712. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13713. "Regular expression matching the last line of a property drawer.")
  13714. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13715. "Regular expression matching the first line of a property drawer.")
  13716. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13717. "Regular expression matching the first line of a property drawer.")
  13718. (defconst org-property-drawer-re
  13719. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13720. org-property-end-re "\\)\n?")
  13721. "Matches an entire property drawer.")
  13722. (defconst org-clock-drawer-re
  13723. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13724. org-property-end-re "\\)\n?")
  13725. "Matches an entire clock drawer.")
  13726. (defsubst org-re-property (property)
  13727. "Return a regexp matching a PROPERTY line.
  13728. Match group 1 will be set to the value."
  13729. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13730. (defsubst org-re-property-keyword (property)
  13731. "Return a regexp matching a PROPERTY line, possibly with no
  13732. value for the property."
  13733. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13734. (defun org-property-action ()
  13735. "Do an action on properties."
  13736. (interactive)
  13737. (let (c)
  13738. (org-at-property-p)
  13739. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13740. (setq c (read-char-exclusive))
  13741. (cond
  13742. ((equal c ?s)
  13743. (call-interactively 'org-set-property))
  13744. ((equal c ?d)
  13745. (call-interactively 'org-delete-property))
  13746. ((equal c ?D)
  13747. (call-interactively 'org-delete-property-globally))
  13748. ((equal c ?c)
  13749. (call-interactively 'org-compute-property-at-point))
  13750. (t (user-error "No such property action %c" c)))))
  13751. (defun org-inc-effort ()
  13752. "Increment the value of the effort property in the current entry."
  13753. (interactive)
  13754. (org-set-effort nil t))
  13755. (defvar org-clock-effort) ;; Defined in org-clock.el
  13756. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13757. (defun org-set-effort (&optional value increment)
  13758. "Set the effort property of the current entry.
  13759. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13760. 10th allowed value.
  13761. When INCREMENT is non-nil, set the property to the next allowed value."
  13762. (interactive "P")
  13763. (if (equal value 0) (setq value 10))
  13764. (let* ((completion-ignore-case t)
  13765. (prop org-effort-property)
  13766. (cur (org-entry-get nil prop))
  13767. (allowed (org-property-get-allowed-values nil prop 'table))
  13768. (existing (mapcar 'list (org-property-values prop)))
  13769. (heading (nth 4 (org-heading-components)))
  13770. rpl
  13771. (val (cond
  13772. ((stringp value) value)
  13773. ((and allowed (integerp value))
  13774. (or (car (nth (1- value) allowed))
  13775. (car (org-last allowed))))
  13776. ((and allowed increment)
  13777. (or (caadr (member (list cur) allowed))
  13778. (user-error "Allowed effort values are not set")))
  13779. (allowed
  13780. (message "Select 1-9,0, [RET%s]: %s"
  13781. (if cur (concat "=" cur) "")
  13782. (mapconcat 'car allowed " "))
  13783. (setq rpl (read-char-exclusive))
  13784. (if (equal rpl ?\r)
  13785. cur
  13786. (setq rpl (- rpl ?0))
  13787. (if (equal rpl 0) (setq rpl 10))
  13788. (if (and (> rpl 0) (<= rpl (length allowed)))
  13789. (car (nth (1- rpl) allowed))
  13790. (org-completing-read "Effort: " allowed nil))))
  13791. (t
  13792. (let (org-completion-use-ido org-completion-use-iswitchb)
  13793. (org-completing-read
  13794. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13795. (concat "[" cur "]") "")
  13796. ": ")
  13797. existing nil nil "" nil cur))))))
  13798. (unless (equal (org-entry-get nil prop) val)
  13799. (org-entry-put nil prop val))
  13800. (save-excursion
  13801. (org-back-to-heading t)
  13802. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13803. (when (string= heading org-clock-current-task)
  13804. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13805. (org-clock-update-mode-line))
  13806. (message "%s is now %s" prop val)))
  13807. (defun org-at-property-p ()
  13808. "Is cursor inside a property drawer?"
  13809. (save-excursion
  13810. (beginning-of-line 1)
  13811. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13812. (save-match-data ;; Used by calling procedures
  13813. (let ((p (point))
  13814. (range (unless (org-before-first-heading-p)
  13815. (org-get-property-block))))
  13816. (and range (<= (car range) p) (< p (cdr range))))))))
  13817. (defun org-get-property-block (&optional beg end force)
  13818. "Return the (beg . end) range of the body of the property drawer.
  13819. BEG and END are the beginning and end of the current subtree, or of
  13820. the part before the first headline. If they are not given, they will
  13821. be found. If the drawer does not exist and FORCE is non-nil, create
  13822. the drawer."
  13823. (catch 'exit
  13824. (save-excursion
  13825. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13826. (progn (org-back-to-heading t) (point))))
  13827. (end (or end (and (not (outline-next-heading)) (point-max))
  13828. (point))))
  13829. (goto-char beg)
  13830. (if (re-search-forward org-property-start-re end t)
  13831. (setq beg (1+ (match-end 0)))
  13832. (if force
  13833. (save-excursion
  13834. (org-insert-property-drawer)
  13835. (setq end (progn (outline-next-heading) (point))))
  13836. (throw 'exit nil))
  13837. (goto-char beg)
  13838. (if (re-search-forward org-property-start-re end t)
  13839. (setq beg (1+ (match-end 0)))))
  13840. (if (re-search-forward org-property-end-re end t)
  13841. (setq end (match-beginning 0))
  13842. (or force (throw 'exit nil))
  13843. (goto-char beg)
  13844. (setq end beg)
  13845. (org-indent-line)
  13846. (insert ":END:\n"))
  13847. (cons beg end)))))
  13848. (defun org-entry-properties (&optional pom which specific)
  13849. "Get all properties of the entry at point-or-marker POM.
  13850. This includes the TODO keyword, the tags, time strings for deadline,
  13851. scheduled, and clocking, and any additional properties defined in the
  13852. entry. The return value is an alist, keys may occur multiple times
  13853. if the property key was used several times.
  13854. POM may also be nil, in which case the current entry is used.
  13855. If WHICH is nil or `all', get all properties. If WHICH is
  13856. `special' or `standard', only get that subclass. If WHICH
  13857. is a string only get exactly this property. SPECIFIC can be a string, the
  13858. specific property we are interested in. Specifying it can speed
  13859. things up because then unnecessary parsing is avoided."
  13860. (setq which (or which 'all))
  13861. (org-with-point-at pom
  13862. (let ((clockstr (substring org-clock-string 0 -1))
  13863. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13864. (case-fold-search nil)
  13865. beg end range props sum-props key key1 value string clocksum clocksumt)
  13866. (save-excursion
  13867. (when (condition-case nil
  13868. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13869. (error nil))
  13870. (setq beg (point))
  13871. (setq sum-props (get-text-property (point) 'org-summaries))
  13872. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13873. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13874. (outline-next-heading)
  13875. (setq end (point))
  13876. (when (memq which '(all special))
  13877. ;; Get the special properties, like TODO and tags
  13878. (goto-char beg)
  13879. (when (and (or (not specific) (string= specific "TODO"))
  13880. (looking-at org-todo-line-regexp) (match-end 2))
  13881. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13882. (when (and (or (not specific) (string= specific "PRIORITY"))
  13883. (looking-at org-priority-regexp))
  13884. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13885. (when (or (not specific) (string= specific "FILE"))
  13886. (push (cons "FILE" buffer-file-name) props))
  13887. (when (and (or (not specific) (string= specific "TAGS"))
  13888. (setq value (org-get-tags-string))
  13889. (string-match "\\S-" value))
  13890. (push (cons "TAGS" value) props))
  13891. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13892. (setq value (org-get-tags-at)))
  13893. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13894. ":"))
  13895. props))
  13896. (when (or (not specific) (string= specific "BLOCKED"))
  13897. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13898. (when (or (not specific)
  13899. (member specific
  13900. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13901. "TIMESTAMP" "TIMESTAMP_IA")))
  13902. (catch 'match
  13903. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13904. (setq key (if (match-end 1)
  13905. (substring (org-match-string-no-properties 1)
  13906. 0 -1))
  13907. string (if (equal key clockstr)
  13908. (org-trim
  13909. (buffer-substring-no-properties
  13910. (match-beginning 3) (goto-char
  13911. (point-at-eol))))
  13912. (substring (org-match-string-no-properties 3)
  13913. 1 -1)))
  13914. ;; Get the correct property name from the key. This is
  13915. ;; necessary if the user has configured time keywords.
  13916. (setq key1 (concat key ":"))
  13917. (cond
  13918. ((not key)
  13919. (setq key
  13920. (if (= (char-after (match-beginning 3)) ?\[)
  13921. "TIMESTAMP_IA" "TIMESTAMP")))
  13922. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13923. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13924. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13925. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13926. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13927. (progn
  13928. (push (cons key string) props)
  13929. ;; no need to search further if match is found
  13930. (throw 'match t))
  13931. (when (or (equal key "CLOCK") (not (assoc key props)))
  13932. (push (cons key string) props)))))))
  13933. (when (memq which '(all standard))
  13934. ;; Get the standard properties, like :PROP: ...
  13935. (setq range (org-get-property-block beg end))
  13936. (when range
  13937. (goto-char (car range))
  13938. (while (re-search-forward
  13939. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13940. (cdr range) t)
  13941. (setq key (org-match-string-no-properties 1)
  13942. value (org-trim (or (org-match-string-no-properties 2) "")))
  13943. (unless (member key excluded)
  13944. (push (cons key (or value "")) props)))))
  13945. (if clocksum
  13946. (push (cons "CLOCKSUM"
  13947. (org-columns-number-to-string (/ (float clocksum) 60.)
  13948. 'add_times))
  13949. props))
  13950. (if clocksumt
  13951. (push (cons "CLOCKSUM_T"
  13952. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13953. 'add_times))
  13954. props))
  13955. (unless (assoc "CATEGORY" props)
  13956. (push (cons "CATEGORY" (org-get-category)) props))
  13957. (append sum-props (nreverse props)))))))
  13958. (defun org-entry-get (pom property &optional inherit literal-nil)
  13959. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13960. If INHERIT is non-nil and the entry does not have the property,
  13961. then also check higher levels of the hierarchy.
  13962. If INHERIT is the symbol `selective', use inheritance only if the setting
  13963. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13964. If the property is present but empty, the return value is the empty string.
  13965. If the property is not present at all, nil is returned.
  13966. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13967. do not interpret it as the list atom nil. This is used for inheritance
  13968. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13969. (org-with-point-at pom
  13970. (if (and inherit (if (eq inherit 'selective)
  13971. (org-property-inherit-p property)
  13972. t))
  13973. (org-entry-get-with-inheritance property literal-nil)
  13974. (if (member property org-special-properties)
  13975. ;; We need a special property. Use `org-entry-properties' to
  13976. ;; retrieve it, but specify the wanted property
  13977. (cdr (assoc property (org-entry-properties nil 'special property)))
  13978. (let ((range (org-get-property-block)))
  13979. (when (and range (not (eq (car range) (cdr range))))
  13980. (let* ((props (list (or (assoc property org-file-properties)
  13981. (assoc property org-global-properties)
  13982. (assoc property org-global-properties-fixed))))
  13983. (ap (lambda (key)
  13984. (when (re-search-forward
  13985. (org-re-property key) (cdr range) t)
  13986. (setq props
  13987. (org-update-property-plist
  13988. key
  13989. (if (match-end 1)
  13990. (org-match-string-no-properties 1) "")
  13991. props)))))
  13992. val)
  13993. (goto-char (car range))
  13994. (funcall ap property)
  13995. (goto-char (car range))
  13996. (while (funcall ap (concat property "+")))
  13997. (setq val (cdr (assoc property props)))
  13998. (when val (if literal-nil val (org-not-nil val))))))))))
  13999. (defun org-property-or-variable-value (var &optional inherit)
  14000. "Check if there is a property fixing the value of VAR.
  14001. If yes, return this value. If not, return the current value of the variable."
  14002. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14003. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14004. (read prop)
  14005. (symbol-value var))))
  14006. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  14007. "Delete the property PROPERTY from entry at point-or-marker POM.
  14008. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14009. an empty drawer to delete."
  14010. (org-with-point-at pom
  14011. (if (member property org-special-properties)
  14012. nil ; cannot delete these properties.
  14013. (let ((range (org-get-property-block)))
  14014. (if (and range
  14015. (goto-char (car range))
  14016. (re-search-forward
  14017. (org-re-property property)
  14018. (cdr range) t))
  14019. (progn
  14020. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14021. (and delete-empty-drawer
  14022. (org-remove-empty-drawer-at
  14023. delete-empty-drawer (car range)))
  14024. t)
  14025. nil)))))
  14026. ;; Multi-values properties are properties that contain multiple values
  14027. ;; These values are assumed to be single words, separated by whitespace.
  14028. (defun org-entry-add-to-multivalued-property (pom property value)
  14029. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14030. (let* ((old (org-entry-get pom property))
  14031. (values (and old (org-split-string old "[ \t]"))))
  14032. (setq value (org-entry-protect-space value))
  14033. (unless (member value values)
  14034. (setq values (append values (list value)))
  14035. (org-entry-put pom property
  14036. (mapconcat 'identity values " ")))))
  14037. (defun org-entry-remove-from-multivalued-property (pom property value)
  14038. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14039. (let* ((old (org-entry-get pom property))
  14040. (values (and old (org-split-string old "[ \t]"))))
  14041. (setq value (org-entry-protect-space value))
  14042. (when (member value values)
  14043. (setq values (delete value values))
  14044. (org-entry-put pom property
  14045. (mapconcat 'identity values " ")))))
  14046. (defun org-entry-member-in-multivalued-property (pom property value)
  14047. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14048. (let* ((old (org-entry-get pom property))
  14049. (values (and old (org-split-string old "[ \t]"))))
  14050. (setq value (org-entry-protect-space value))
  14051. (member value values)))
  14052. (defun org-entry-get-multivalued-property (pom property)
  14053. "Return a list of values in a multivalued property."
  14054. (let* ((value (org-entry-get pom property))
  14055. (values (and value (org-split-string value "[ \t]"))))
  14056. (mapcar 'org-entry-restore-space values)))
  14057. (defun org-entry-put-multivalued-property (pom property &rest values)
  14058. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14059. VALUES should be a list of strings. Spaces will be protected."
  14060. (org-entry-put pom property
  14061. (mapconcat 'org-entry-protect-space values " "))
  14062. (let* ((value (org-entry-get pom property))
  14063. (values (and value (org-split-string value "[ \t]"))))
  14064. (mapcar 'org-entry-restore-space values)))
  14065. (defun org-entry-protect-space (s)
  14066. "Protect spaces and newline in string S."
  14067. (while (string-match " " s)
  14068. (setq s (replace-match "%20" t t s)))
  14069. (while (string-match "\n" s)
  14070. (setq s (replace-match "%0A" t t s)))
  14071. s)
  14072. (defun org-entry-restore-space (s)
  14073. "Restore spaces and newline in string S."
  14074. (while (string-match "%20" s)
  14075. (setq s (replace-match " " t t s)))
  14076. (while (string-match "%0A" s)
  14077. (setq s (replace-match "\n" t t s)))
  14078. s)
  14079. (defvar org-entry-property-inherited-from (make-marker)
  14080. "Marker pointing to the entry from where a property was inherited.
  14081. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14082. location of the entry where the inheritance search matched. If there was
  14083. no match, the marker will point nowhere.
  14084. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14085. is set.")
  14086. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14087. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14088. The search will stop at the first ancestor which has the property defined.
  14089. If the value found is \"nil\", return nil to show that the property
  14090. should be considered as undefined (this is the meaning of nil here).
  14091. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14092. (move-marker org-entry-property-inherited-from nil)
  14093. (let (tmp)
  14094. (save-excursion
  14095. (save-restriction
  14096. (widen)
  14097. (catch 'ex
  14098. (while t
  14099. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14100. (or (ignore-errors (org-back-to-heading t))
  14101. (goto-char (point-min)))
  14102. (move-marker org-entry-property-inherited-from (point))
  14103. (throw 'ex tmp))
  14104. (or (ignore-errors (org-up-heading-safe))
  14105. (throw 'ex nil))))))
  14106. (setq tmp (or tmp
  14107. (cdr (assoc property org-file-properties))
  14108. (cdr (assoc property org-global-properties))
  14109. (cdr (assoc property org-global-properties-fixed))))
  14110. (if literal-nil tmp (org-not-nil tmp))))
  14111. (defvar org-property-changed-functions nil
  14112. "Hook called when the value of a property has changed.
  14113. Each hook function should accept two arguments, the name of the property
  14114. and the new value.")
  14115. (defun org-entry-put (pom property value)
  14116. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14117. (org-with-point-at pom
  14118. (org-back-to-heading t)
  14119. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14120. range)
  14121. (cond
  14122. ((equal property "TODO")
  14123. (when (and (stringp value) (string-match "\\S-" value)
  14124. (not (member value org-todo-keywords-1)))
  14125. (user-error "\"%s\" is not a valid TODO state" value))
  14126. (if (or (not value)
  14127. (not (string-match "\\S-" value)))
  14128. (setq value 'none))
  14129. (org-todo value)
  14130. (org-set-tags nil 'align))
  14131. ((equal property "PRIORITY")
  14132. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14133. (string-to-char value) ?\ ))
  14134. (org-set-tags nil 'align))
  14135. ((equal property "CLOCKSUM")
  14136. (if (not (re-search-forward
  14137. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14138. (error "Cannot find a clock log")
  14139. (goto-char (- (match-end 1) 2))
  14140. (cond
  14141. ((eq value 'earlier) (org-timestamp-down))
  14142. ((eq value 'later) (org-timestamp-up)))
  14143. (org-clock-sum-current-item)))
  14144. ((equal property "SCHEDULED")
  14145. (if (re-search-forward org-scheduled-time-regexp end t)
  14146. (cond
  14147. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14148. ((eq value 'later) (org-timestamp-change 1 'day))
  14149. (t (call-interactively 'org-schedule)))
  14150. (call-interactively 'org-schedule)))
  14151. ((equal property "DEADLINE")
  14152. (if (re-search-forward org-deadline-time-regexp end t)
  14153. (cond
  14154. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14155. ((eq value 'later) (org-timestamp-change 1 'day))
  14156. (t (call-interactively 'org-deadline)))
  14157. (call-interactively 'org-deadline)))
  14158. ((member property org-special-properties)
  14159. (error "The %s property can not yet be set with `org-entry-put'"
  14160. property))
  14161. (t ; a non-special property
  14162. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14163. (setq range (org-get-property-block beg end 'force))
  14164. (goto-char (car range))
  14165. (if (re-search-forward
  14166. (org-re-property-keyword property) (cdr range) t)
  14167. (progn
  14168. (delete-region (match-beginning 0) (match-end 0))
  14169. (goto-char (match-beginning 0)))
  14170. (goto-char (cdr range))
  14171. (insert "\n")
  14172. (backward-char 1)
  14173. (org-indent-line))
  14174. (insert ":" property ":")
  14175. (and value (insert " " value))
  14176. (org-indent-line)))))
  14177. (run-hook-with-args 'org-property-changed-functions property value)))
  14178. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14179. "Get all property keys in the current buffer.
  14180. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14181. like tags and TODO state.
  14182. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14183. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14184. and others.
  14185. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14186. formats in the current buffer."
  14187. (let (rtn range cfmt s p)
  14188. (save-excursion
  14189. (save-restriction
  14190. (widen)
  14191. (goto-char (point-min))
  14192. (while (re-search-forward org-property-start-re nil t)
  14193. (setq range (org-get-property-block))
  14194. (goto-char (car range))
  14195. (while (re-search-forward
  14196. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14197. (cdr range) t)
  14198. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14199. (outline-next-heading))))
  14200. (when include-specials
  14201. (setq rtn (append org-special-properties rtn)))
  14202. (when include-defaults
  14203. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14204. (add-to-list 'rtn org-effort-property))
  14205. (when include-columns
  14206. (save-excursion
  14207. (save-restriction
  14208. (widen)
  14209. (goto-char (point-min))
  14210. (while (re-search-forward
  14211. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14212. nil t)
  14213. (setq cfmt (match-string 2) s 0)
  14214. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14215. cfmt s)
  14216. (setq s (match-end 0)
  14217. p (match-string 1 cfmt))
  14218. (unless (or (equal p "ITEM")
  14219. (member p org-special-properties))
  14220. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14221. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14222. (defun org-property-values (key)
  14223. "Return a list of all values of property KEY in the current buffer."
  14224. (save-excursion
  14225. (save-restriction
  14226. (widen)
  14227. (goto-char (point-min))
  14228. (let ((re (org-re-property key))
  14229. values)
  14230. (while (re-search-forward re nil t)
  14231. (add-to-list 'values (org-trim (match-string 1))))
  14232. (delete "" values)))))
  14233. (defun org-insert-property-drawer ()
  14234. "Insert a property drawer into the current entry."
  14235. (org-back-to-heading t)
  14236. (looking-at org-outline-regexp)
  14237. (let ((indent (if org-adapt-indentation
  14238. (- (match-end 0) (match-beginning 0))
  14239. 0))
  14240. (beg (point))
  14241. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14242. end hiddenp)
  14243. (outline-next-heading)
  14244. (setq end (point))
  14245. (goto-char beg)
  14246. (while (re-search-forward re end t))
  14247. (setq hiddenp (outline-invisible-p))
  14248. (end-of-line 1)
  14249. (and (equal (char-after) ?\n) (forward-char 1))
  14250. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14251. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14252. ;; just skip this line
  14253. (beginning-of-line 2)
  14254. ;; Drawer start, find the end
  14255. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14256. (beginning-of-line 1)))
  14257. (org-skip-over-state-notes)
  14258. (skip-chars-backward " \t\n\r")
  14259. (if (eq (char-before) ?*) (forward-char 1))
  14260. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14261. (beginning-of-line 0)
  14262. (org-indent-to-column indent)
  14263. (beginning-of-line 2)
  14264. (org-indent-to-column indent)
  14265. (beginning-of-line 0)
  14266. (if hiddenp
  14267. (save-excursion
  14268. (org-back-to-heading t)
  14269. (hide-entry))
  14270. (org-flag-drawer t))))
  14271. (defun org-insert-drawer (&optional arg drawer)
  14272. "Insert a drawer at point.
  14273. Optional argument DRAWER, when non-nil, is a string representing
  14274. drawer's name. Otherwise, the user is prompted for a name.
  14275. If a region is active, insert the drawer around that region
  14276. instead.
  14277. Point is left between drawer's boundaries."
  14278. (interactive "P")
  14279. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14280. "LOGBOOK"))
  14281. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14282. ;; internally by Org. They are meant to be inserted
  14283. ;; automatically.
  14284. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14285. ;; Remove system drawers from list. Note: For some reason,
  14286. ;; `org-completing-read' ignores the predicate while
  14287. ;; `completing-read' handles it fine.
  14288. (drawer (if arg "PROPERTIES"
  14289. (or drawer
  14290. (completing-read
  14291. "Drawer: " org-drawers
  14292. (lambda (d) (not (member d system-drawers))))))))
  14293. (cond
  14294. ;; With C-u, fall back on `org-insert-property-drawer'
  14295. (arg (org-insert-property-drawer))
  14296. ;; With an active region, insert a drawer at point.
  14297. ((not (org-region-active-p))
  14298. (progn
  14299. (unless (bolp) (insert "\n"))
  14300. (insert (format ":%s:\n\n:END:\n" drawer))
  14301. (forward-line -2)))
  14302. ;; Otherwise, insert the drawer at point
  14303. (t
  14304. (let ((rbeg (region-beginning))
  14305. (rend (copy-marker (region-end))))
  14306. (unwind-protect
  14307. (progn
  14308. (goto-char rbeg)
  14309. (beginning-of-line)
  14310. (when (save-excursion
  14311. (re-search-forward org-outline-regexp-bol rend t))
  14312. (user-error "Drawers cannot contain headlines"))
  14313. ;; Position point at the beginning of the first
  14314. ;; non-blank line in region. Insert drawer's opening
  14315. ;; there, then indent it.
  14316. (org-skip-whitespace)
  14317. (beginning-of-line)
  14318. (insert ":" drawer ":\n")
  14319. (forward-line -1)
  14320. (indent-for-tab-command)
  14321. ;; Move point to the beginning of the first blank line
  14322. ;; after the last non-blank line in region. Insert
  14323. ;; drawer's closing, then indent it.
  14324. (goto-char rend)
  14325. (skip-chars-backward " \r\t\n")
  14326. (insert "\n:END:")
  14327. (deactivate-mark t)
  14328. (indent-for-tab-command)
  14329. (unless (eolp) (insert "\n")))
  14330. ;; Clear marker, whatever the outcome of insertion is.
  14331. (set-marker rend nil)))))))
  14332. (defvar org-property-set-functions-alist nil
  14333. "Property set function alist.
  14334. Each entry should have the following format:
  14335. (PROPERTY . READ-FUNCTION)
  14336. The read function will be called with the same argument as
  14337. `org-completing-read'.")
  14338. (defun org-set-property-function (property)
  14339. "Get the function that should be used to set PROPERTY.
  14340. This is computed according to `org-property-set-functions-alist'."
  14341. (or (cdr (assoc property org-property-set-functions-alist))
  14342. 'org-completing-read))
  14343. (defun org-read-property-value (property)
  14344. "Read PROPERTY value from user."
  14345. (let* ((completion-ignore-case t)
  14346. (allowed (org-property-get-allowed-values nil property 'table))
  14347. (cur (org-entry-get nil property))
  14348. (prompt (concat property " value"
  14349. (if (and cur (string-match "\\S-" cur))
  14350. (concat " [" cur "]") "") ": "))
  14351. (set-function (org-set-property-function property))
  14352. (val (if allowed
  14353. (funcall set-function prompt allowed nil
  14354. (not (get-text-property 0 'org-unrestricted
  14355. (caar allowed))))
  14356. (let (org-completion-use-ido org-completion-use-iswitchb)
  14357. (funcall set-function prompt
  14358. (mapcar 'list (org-property-values property))
  14359. nil nil "" nil cur)))))
  14360. (if (equal val "")
  14361. cur
  14362. val)))
  14363. (defvar org-last-set-property nil)
  14364. (defvar org-last-set-property-value nil)
  14365. (defun org-read-property-name ()
  14366. "Read a property name."
  14367. (let* ((completion-ignore-case t)
  14368. (keys (org-buffer-property-keys nil t t))
  14369. (default-prop (or (save-excursion
  14370. (save-match-data
  14371. (beginning-of-line)
  14372. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14373. (null (string= (match-string 1) "END"))
  14374. (match-string 1))))
  14375. org-last-set-property))
  14376. (property (org-icompleting-read
  14377. (concat "Property"
  14378. (if default-prop (concat " [" default-prop "]") "")
  14379. ": ")
  14380. (mapcar 'list keys)
  14381. nil nil nil nil
  14382. default-prop)))
  14383. (if (member property keys)
  14384. property
  14385. (or (cdr (assoc (downcase property)
  14386. (mapcar (lambda (x) (cons (downcase x) x))
  14387. keys)))
  14388. property))))
  14389. (defun org-set-property-and-value (use-last)
  14390. "Allow to set [PROPERTY]: [value] direction from prompt.
  14391. When use-default, don't even ask, just use the last
  14392. \"[PROPERTY]: [value]\" string from the history."
  14393. (interactive "P")
  14394. (let* ((completion-ignore-case t)
  14395. (pv (or (and use-last org-last-set-property-value)
  14396. (org-completing-read
  14397. "Enter a \"[Property]: [value]\" pair: "
  14398. nil nil nil nil nil
  14399. org-last-set-property-value)))
  14400. prop val)
  14401. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14402. (setq prop (match-string 1 pv)
  14403. val (match-string 2 pv))
  14404. (org-set-property prop val))))
  14405. (defun org-set-property (property value)
  14406. "In the current entry, set PROPERTY to VALUE.
  14407. When called interactively, this will prompt for a property name, offering
  14408. completion on existing and default properties. And then it will prompt
  14409. for a value, offering completion either on allowed values (via an inherited
  14410. xxx_ALL property) or on existing values in other instances of this property
  14411. in the current file."
  14412. (interactive (list nil nil))
  14413. (let* ((property (or property (org-read-property-name)))
  14414. (value (or value (org-read-property-value property)))
  14415. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14416. (setq org-last-set-property property)
  14417. (setq org-last-set-property-value (concat property ": " value))
  14418. ;; Possibly postprocess the inserted value:
  14419. (when fn (setq value (funcall fn value)))
  14420. (unless (equal (org-entry-get nil property) value)
  14421. (org-entry-put nil property value))))
  14422. (defun org-delete-property (property &optional delete-empty-drawer)
  14423. "In the current entry, delete PROPERTY.
  14424. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14425. an empty drawer to delete."
  14426. (interactive
  14427. (let* ((completion-ignore-case t)
  14428. (prop (org-icompleting-read "Property: "
  14429. (org-entry-properties nil 'standard))))
  14430. (list prop)))
  14431. (message "Property %s %s" property
  14432. (if (org-entry-delete nil property delete-empty-drawer)
  14433. "deleted"
  14434. "was not present in the entry")))
  14435. (defun org-delete-property-globally (property)
  14436. "Remove PROPERTY globally, from all entries."
  14437. (interactive
  14438. (let* ((completion-ignore-case t)
  14439. (prop (org-icompleting-read
  14440. "Globally remove property: "
  14441. (mapcar 'list (org-buffer-property-keys)))))
  14442. (list prop)))
  14443. (save-excursion
  14444. (save-restriction
  14445. (widen)
  14446. (goto-char (point-min))
  14447. (let ((cnt 0))
  14448. (while (re-search-forward
  14449. (org-re-property property)
  14450. nil t)
  14451. (setq cnt (1+ cnt))
  14452. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14453. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14454. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14455. (defun org-compute-property-at-point ()
  14456. "Compute the property at point.
  14457. This looks for an enclosing column format, extracts the operator and
  14458. then applies it to the property in the column format's scope."
  14459. (interactive)
  14460. (unless (org-at-property-p)
  14461. (user-error "Not at a property"))
  14462. (let ((prop (org-match-string-no-properties 2)))
  14463. (org-columns-get-format-and-top-level)
  14464. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14465. (user-error "No operator defined for property %s" prop))
  14466. (org-columns-compute prop)))
  14467. (defvar org-property-allowed-value-functions nil
  14468. "Hook for functions supplying allowed values for a specific property.
  14469. The functions must take a single argument, the name of the property, and
  14470. return a flat list of allowed values. If \":ETC\" is one of
  14471. the values, this means that these values are intended as defaults for
  14472. completion, but that other values should be allowed too.
  14473. The functions must return nil if they are not responsible for this
  14474. property.")
  14475. (defun org-property-get-allowed-values (pom property &optional table)
  14476. "Get allowed values for the property PROPERTY.
  14477. When TABLE is non-nil, return an alist that can directly be used for
  14478. completion."
  14479. (let (vals)
  14480. (cond
  14481. ((equal property "TODO")
  14482. (setq vals (org-with-point-at pom
  14483. (append org-todo-keywords-1 '("")))))
  14484. ((equal property "PRIORITY")
  14485. (let ((n org-lowest-priority))
  14486. (while (>= n org-highest-priority)
  14487. (push (char-to-string n) vals)
  14488. (setq n (1- n)))))
  14489. ((member property org-special-properties))
  14490. ((setq vals (run-hook-with-args-until-success
  14491. 'org-property-allowed-value-functions property)))
  14492. (t
  14493. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14494. (when (and vals (string-match "\\S-" vals))
  14495. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14496. (setq vals (mapcar (lambda (x)
  14497. (cond ((stringp x) x)
  14498. ((numberp x) (number-to-string x))
  14499. ((symbolp x) (symbol-name x))
  14500. (t "???")))
  14501. vals)))))
  14502. (when (member ":ETC" vals)
  14503. (setq vals (remove ":ETC" vals))
  14504. (org-add-props (car vals) '(org-unrestricted t)))
  14505. (if table (mapcar 'list vals) vals)))
  14506. (defun org-property-previous-allowed-value (&optional previous)
  14507. "Switch to the next allowed value for this property."
  14508. (interactive)
  14509. (org-property-next-allowed-value t))
  14510. (defun org-property-next-allowed-value (&optional previous)
  14511. "Switch to the next allowed value for this property."
  14512. (interactive)
  14513. (unless (org-at-property-p)
  14514. (user-error "Not at a property"))
  14515. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14516. (key (match-string 2))
  14517. (value (match-string 3))
  14518. (allowed (or (org-property-get-allowed-values (point) key)
  14519. (and (member value '("[ ]" "[-]" "[X]"))
  14520. '("[ ]" "[X]"))))
  14521. (heading (save-match-data (nth 4 (org-heading-components))))
  14522. nval)
  14523. (unless allowed
  14524. (user-error "Allowed values for this property have not been defined"))
  14525. (if previous (setq allowed (reverse allowed)))
  14526. (if (member value allowed)
  14527. (setq nval (car (cdr (member value allowed)))))
  14528. (setq nval (or nval (car allowed)))
  14529. (if (equal nval value)
  14530. (user-error "Only one allowed value for this property"))
  14531. (org-at-property-p)
  14532. (replace-match (concat " :" key ": " nval) t t)
  14533. (org-indent-line)
  14534. (beginning-of-line 1)
  14535. (skip-chars-forward " \t")
  14536. (when (equal prop org-effort-property)
  14537. (save-excursion
  14538. (org-back-to-heading t)
  14539. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14540. (when (string= org-clock-current-task heading)
  14541. (setq org-clock-effort nval)
  14542. (org-clock-update-mode-line)))
  14543. (run-hook-with-args 'org-property-changed-functions key nval)))
  14544. (defun org-find-olp (path &optional this-buffer)
  14545. "Return a marker pointing to the entry at outline path OLP.
  14546. If anything goes wrong, throw an error.
  14547. You can wrap this call to catch the error like this:
  14548. (condition-case msg
  14549. (org-mobile-locate-entry (match-string 4))
  14550. (error (nth 1 msg)))
  14551. The return value will then be either a string with the error message,
  14552. or a marker if everything is OK.
  14553. If THIS-BUFFER is set, the outline path does not contain a file,
  14554. only headings."
  14555. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14556. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14557. (level 1)
  14558. (lmin 1)
  14559. (lmax 1)
  14560. limit re end found pos heading cnt flevel)
  14561. (unless buffer (error "File not found :%s" file))
  14562. (with-current-buffer buffer
  14563. (save-excursion
  14564. (save-restriction
  14565. (widen)
  14566. (setq limit (point-max))
  14567. (goto-char (point-min))
  14568. (while (setq heading (pop path))
  14569. (setq re (format org-complex-heading-regexp-format
  14570. (regexp-quote heading)))
  14571. (setq cnt 0 pos (point))
  14572. (while (re-search-forward re end t)
  14573. (setq level (- (match-end 1) (match-beginning 1)))
  14574. (if (and (>= level lmin) (<= level lmax))
  14575. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14576. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14577. lmax heading))
  14578. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14579. lmax heading))
  14580. (goto-char found)
  14581. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14582. (setq end (save-excursion (org-end-of-subtree t t))))
  14583. (when (org-at-heading-p)
  14584. (point-marker)))))))
  14585. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14586. "Find node HEADING in BUFFER.
  14587. Return a marker to the heading if it was found, or nil if not.
  14588. If POS-ONLY is set, return just the position instead of a marker.
  14589. The heading text must match exact, but it may have a TODO keyword,
  14590. a priority cookie and tags in the standard locations."
  14591. (with-current-buffer (or buffer (current-buffer))
  14592. (save-excursion
  14593. (save-restriction
  14594. (widen)
  14595. (goto-char (point-min))
  14596. (let (case-fold-search)
  14597. (if (re-search-forward
  14598. (format org-complex-heading-regexp-format
  14599. (regexp-quote heading)) nil t)
  14600. (if pos-only
  14601. (match-beginning 0)
  14602. (move-marker (make-marker) (match-beginning 0)))))))))
  14603. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14604. "Find Org node headline HEADING in all .org files in directory DIR.
  14605. When the target headline is found, return a marker to this location."
  14606. (let ((files (directory-files (or dir default-directory)
  14607. nil "\\`[^.#].*\\.org\\'"))
  14608. file visiting m buffer)
  14609. (catch 'found
  14610. (while (setq file (pop files))
  14611. (message "trying %s" file)
  14612. (setq visiting (org-find-base-buffer-visiting file))
  14613. (setq buffer (or visiting (find-file-noselect file)))
  14614. (setq m (org-find-exact-headline-in-buffer
  14615. heading buffer))
  14616. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14617. (and m (throw 'found m))))))
  14618. (defun org-find-entry-with-id (ident)
  14619. "Locate the entry that contains the ID property with exact value IDENT.
  14620. IDENT can be a string, a symbol or a number, this function will search for
  14621. the string representation of it.
  14622. Return the position where this entry starts, or nil if there is no such entry."
  14623. (interactive "sID: ")
  14624. (let ((id (cond
  14625. ((stringp ident) ident)
  14626. ((symbol-name ident) (symbol-name ident))
  14627. ((numberp ident) (number-to-string ident))
  14628. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14629. (case-fold-search nil))
  14630. (save-excursion
  14631. (save-restriction
  14632. (widen)
  14633. (goto-char (point-min))
  14634. (when (re-search-forward
  14635. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14636. nil t)
  14637. (org-back-to-heading t)
  14638. (point))))))
  14639. ;;;; Timestamps
  14640. (defvar org-last-changed-timestamp nil)
  14641. (defvar org-last-inserted-timestamp nil
  14642. "The last time stamp inserted with `org-insert-time-stamp'.")
  14643. (defvar org-time-was-given) ; dynamically scoped parameter
  14644. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14645. (defvar org-ts-what) ; dynamically scoped parameter
  14646. (defun org-time-stamp (arg &optional inactive)
  14647. "Prompt for a date/time and insert a time stamp.
  14648. If the user specifies a time like HH:MM or if this command is
  14649. called with at least one prefix argument, the time stamp contains
  14650. the date and the time. Otherwise, only the date is be included.
  14651. All parts of a date not specified by the user is filled in from
  14652. the current date/time. So if you just press return without
  14653. typing anything, the time stamp will represent the current
  14654. date/time.
  14655. If there is already a timestamp at the cursor, it will be
  14656. modified.
  14657. With two universal prefix arguments, insert an active timestamp
  14658. with the current time without prompting the user."
  14659. (interactive "P")
  14660. (let* ((ts nil)
  14661. (default-time
  14662. ;; Default time is either today, or, when entering a range,
  14663. ;; the range start.
  14664. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14665. (save-excursion
  14666. (re-search-backward
  14667. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14668. (- (point) 20) t)))
  14669. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14670. (current-time)))
  14671. (default-input (and ts (org-get-compact-tod ts)))
  14672. (repeater (save-excursion
  14673. (save-match-data
  14674. (beginning-of-line)
  14675. (when (re-search-forward
  14676. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14677. (save-excursion (progn (end-of-line) (point))) t)
  14678. (match-string 0)))))
  14679. org-time-was-given org-end-time-was-given time)
  14680. (cond
  14681. ((and (org-at-timestamp-p t)
  14682. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14683. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14684. (insert "--")
  14685. (setq time (let ((this-command this-command))
  14686. (org-read-date arg 'totime nil nil
  14687. default-time default-input inactive)))
  14688. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14689. ((org-at-timestamp-p t)
  14690. (setq time (let ((this-command this-command))
  14691. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14692. (when (org-at-timestamp-p t) ; just to get the match data
  14693. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14694. (replace-match "")
  14695. (setq org-last-changed-timestamp
  14696. (org-insert-time-stamp
  14697. time (or org-time-was-given arg)
  14698. inactive nil nil (list org-end-time-was-given)))
  14699. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14700. (setq org-last-changed-timestamp
  14701. (concat (substring org-last-inserted-timestamp 0 -1)
  14702. " " repeater ">"))))
  14703. (message "Timestamp updated"))
  14704. ((equal arg '(16))
  14705. (org-insert-time-stamp (current-time) t))
  14706. (t
  14707. (setq time (let ((this-command this-command))
  14708. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14709. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14710. nil nil (list org-end-time-was-given))))))
  14711. ;; FIXME: can we use this for something else, like computing time differences?
  14712. (defun org-get-compact-tod (s)
  14713. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14714. (let* ((t1 (match-string 1 s))
  14715. (h1 (string-to-number (match-string 2 s)))
  14716. (m1 (string-to-number (match-string 3 s)))
  14717. (t2 (and (match-end 4) (match-string 5 s)))
  14718. (h2 (and t2 (string-to-number (match-string 6 s))))
  14719. (m2 (and t2 (string-to-number (match-string 7 s))))
  14720. dh dm)
  14721. (if (not t2)
  14722. t1
  14723. (setq dh (- h2 h1) dm (- m2 m1))
  14724. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14725. (concat t1 "+" (number-to-string dh)
  14726. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14727. (defun org-time-stamp-inactive (&optional arg)
  14728. "Insert an inactive time stamp.
  14729. An inactive time stamp is enclosed in square brackets instead of angle
  14730. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14731. does not link to the calendar and cannot be changed with the S-cursor keys.
  14732. So these are more for recording a certain time/date."
  14733. (interactive "P")
  14734. (org-time-stamp arg 'inactive))
  14735. (defvar org-date-ovl (make-overlay 1 1))
  14736. (overlay-put org-date-ovl 'face 'org-date-selected)
  14737. (org-detach-overlay org-date-ovl)
  14738. (defvar org-ans1) ; dynamically scoped parameter
  14739. (defvar org-ans2) ; dynamically scoped parameter
  14740. (defvar org-plain-time-of-day-regexp) ; defined below
  14741. (defvar org-overriding-default-time nil) ; dynamically scoped
  14742. (defvar org-read-date-overlay nil)
  14743. (defvar org-dcst nil) ; dynamically scoped
  14744. (defvar org-read-date-history nil)
  14745. (defvar org-read-date-final-answer nil)
  14746. (defvar org-read-date-analyze-futurep nil)
  14747. (defvar org-read-date-analyze-forced-year nil)
  14748. (defvar org-read-date-inactive)
  14749. (defvar org-read-date-minibuffer-local-map
  14750. (let ((map (make-sparse-keymap)))
  14751. (set-keymap-parent map minibuffer-local-map)
  14752. (org-defkey map (kbd ".")
  14753. (lambda () (interactive)
  14754. ;; Are we at the beginning of the prompt?
  14755. (if (looking-back "^[^:]+: ")
  14756. (org-eval-in-calendar '(calendar-goto-today))
  14757. (insert "."))))
  14758. (org-defkey map (kbd "C-.")
  14759. (lambda () (interactive)
  14760. (org-eval-in-calendar '(calendar-goto-today))))
  14761. (org-defkey map [(meta shift left)]
  14762. (lambda () (interactive)
  14763. (org-eval-in-calendar '(calendar-backward-month 1))))
  14764. (org-defkey map [(meta shift right)]
  14765. (lambda () (interactive)
  14766. (org-eval-in-calendar '(calendar-forward-month 1))))
  14767. (org-defkey map [(meta shift up)]
  14768. (lambda () (interactive)
  14769. (org-eval-in-calendar '(calendar-backward-year 1))))
  14770. (org-defkey map [(meta shift down)]
  14771. (lambda () (interactive)
  14772. (org-eval-in-calendar '(calendar-forward-year 1))))
  14773. (org-defkey map [?\e (shift left)]
  14774. (lambda () (interactive)
  14775. (org-eval-in-calendar '(calendar-backward-month 1))))
  14776. (org-defkey map [?\e (shift right)]
  14777. (lambda () (interactive)
  14778. (org-eval-in-calendar '(calendar-forward-month 1))))
  14779. (org-defkey map [?\e (shift up)]
  14780. (lambda () (interactive)
  14781. (org-eval-in-calendar '(calendar-backward-year 1))))
  14782. (org-defkey map [?\e (shift down)]
  14783. (lambda () (interactive)
  14784. (org-eval-in-calendar '(calendar-forward-year 1))))
  14785. (org-defkey map [(shift up)]
  14786. (lambda () (interactive)
  14787. (org-eval-in-calendar '(calendar-backward-week 1))))
  14788. (org-defkey map [(shift down)]
  14789. (lambda () (interactive)
  14790. (org-eval-in-calendar '(calendar-forward-week 1))))
  14791. (org-defkey map [(shift left)]
  14792. (lambda () (interactive)
  14793. (org-eval-in-calendar '(calendar-backward-day 1))))
  14794. (org-defkey map [(shift right)]
  14795. (lambda () (interactive)
  14796. (org-eval-in-calendar '(calendar-forward-day 1))))
  14797. (org-defkey map "!"
  14798. (lambda () (interactive)
  14799. (org-eval-in-calendar '(diary-view-entries))
  14800. (message "")))
  14801. (org-defkey map ">"
  14802. (lambda () (interactive)
  14803. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14804. (org-defkey map "<"
  14805. (lambda () (interactive)
  14806. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14807. (org-defkey map "\C-v"
  14808. (lambda () (interactive)
  14809. (org-eval-in-calendar
  14810. '(calendar-scroll-left-three-months 1))))
  14811. (org-defkey map "\M-v"
  14812. (lambda () (interactive)
  14813. (org-eval-in-calendar
  14814. '(calendar-scroll-right-three-months 1))))
  14815. map)
  14816. "Keymap for minibuffer commands when using `org-read-date'.")
  14817. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14818. default-time default-input inactive)
  14819. "Read a date, possibly a time, and make things smooth for the user.
  14820. The prompt will suggest to enter an ISO date, but you can also enter anything
  14821. which will at least partially be understood by `parse-time-string'.
  14822. Unrecognized parts of the date will default to the current day, month, year,
  14823. hour and minute. If this command is called to replace a timestamp at point,
  14824. or to enter the second timestamp of a range, the default time is taken
  14825. from the existing stamp. Furthermore, the command prefers the future,
  14826. so if you are giving a date where the year is not given, and the day-month
  14827. combination is already past in the current year, it will assume you
  14828. mean next year. For details, see the manual. A few examples:
  14829. 3-2-5 --> 2003-02-05
  14830. feb 15 --> currentyear-02-15
  14831. 2/15 --> currentyear-02-15
  14832. sep 12 9 --> 2009-09-12
  14833. 12:45 --> today 12:45
  14834. 22 sept 0:34 --> currentyear-09-22 0:34
  14835. 12 --> currentyear-currentmonth-12
  14836. Fri --> nearest Friday after today
  14837. -Tue --> last Tuesday
  14838. etc.
  14839. Furthermore you can specify a relative date by giving, as the *first* thing
  14840. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14841. change in days weeks, months, years.
  14842. With a single plus or minus, the date is relative to today. With a double
  14843. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14844. +4d --> four days from today
  14845. +4 --> same as above
  14846. +2w --> two weeks from today
  14847. ++5 --> five days from default date
  14848. The function understands only English month and weekday abbreviations.
  14849. While prompting, a calendar is popped up - you can also select the
  14850. date with the mouse (button 1). The calendar shows a period of three
  14851. months. To scroll it to other months, use the keys `>' and `<'.
  14852. If you don't like the calendar, turn it off with
  14853. \(setq org-read-date-popup-calendar nil)
  14854. With optional argument TO-TIME, the date will immediately be converted
  14855. to an internal time.
  14856. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14857. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14858. still enter a time, and this function will inform the calling routine
  14859. about this change. The calling routine may then choose to change the
  14860. format used to insert the time stamp into the buffer to include the time.
  14861. With optional argument FROM-STRING, read from this string instead from
  14862. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14863. the time/date that is used for everything that is not specified by the
  14864. user."
  14865. (require 'parse-time)
  14866. (let* ((org-time-stamp-rounding-minutes
  14867. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14868. (org-dcst org-display-custom-times)
  14869. (ct (org-current-time))
  14870. (org-def (or org-overriding-default-time default-time ct))
  14871. (org-defdecode (decode-time org-def))
  14872. (dummy (progn
  14873. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14874. (setcar (nthcdr 2 org-defdecode) -1)
  14875. (setcar (nthcdr 1 org-defdecode) 59)
  14876. (setq org-def (apply 'encode-time org-defdecode)
  14877. org-defdecode (decode-time org-def)))))
  14878. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14879. (calendar-frame-setup nil)
  14880. (calendar-setup nil)
  14881. (calendar-move-hook nil)
  14882. (calendar-view-diary-initially-flag nil)
  14883. (calendar-view-holidays-initially-flag nil)
  14884. (timestr (format-time-string
  14885. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14886. (prompt (concat (if prompt (concat prompt " ") "")
  14887. (format "Date+time [%s]: " timestr)))
  14888. ans (org-ans0 "") org-ans1 org-ans2 final)
  14889. (cond
  14890. (from-string (setq ans from-string))
  14891. (org-read-date-popup-calendar
  14892. (save-excursion
  14893. (save-window-excursion
  14894. (calendar)
  14895. (org-eval-in-calendar '(setq cursor-type nil) t)
  14896. (unwind-protect
  14897. (progn
  14898. (calendar-forward-day (- (time-to-days org-def)
  14899. (calendar-absolute-from-gregorian
  14900. (calendar-current-date))))
  14901. (org-eval-in-calendar nil t)
  14902. (let* ((old-map (current-local-map))
  14903. (map (copy-keymap calendar-mode-map))
  14904. (minibuffer-local-map
  14905. (copy-keymap org-read-date-minibuffer-local-map)))
  14906. (org-defkey map (kbd "RET") 'org-calendar-select)
  14907. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14908. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14909. (unwind-protect
  14910. (progn
  14911. (use-local-map map)
  14912. (setq org-read-date-inactive inactive)
  14913. (add-hook 'post-command-hook 'org-read-date-display)
  14914. (setq org-ans0 (read-string prompt default-input
  14915. 'org-read-date-history nil))
  14916. ;; org-ans0: from prompt
  14917. ;; org-ans1: from mouse click
  14918. ;; org-ans2: from calendar motion
  14919. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14920. (remove-hook 'post-command-hook 'org-read-date-display)
  14921. (use-local-map old-map)
  14922. (when org-read-date-overlay
  14923. (delete-overlay org-read-date-overlay)
  14924. (setq org-read-date-overlay nil)))))
  14925. (bury-buffer "*Calendar*")))))
  14926. (t ; Naked prompt only
  14927. (unwind-protect
  14928. (setq ans (read-string prompt default-input
  14929. 'org-read-date-history timestr))
  14930. (when org-read-date-overlay
  14931. (delete-overlay org-read-date-overlay)
  14932. (setq org-read-date-overlay nil)))))
  14933. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14934. (when org-read-date-analyze-forced-year
  14935. (message "Year was forced into %s"
  14936. (if org-read-date-force-compatible-dates
  14937. "compatible range (1970-2037)"
  14938. "range representable on this machine"))
  14939. (ding))
  14940. ;; One round trip to get rid of 34th of August and stuff like that....
  14941. (setq final (decode-time (apply 'encode-time final)))
  14942. (setq org-read-date-final-answer ans)
  14943. (if to-time
  14944. (apply 'encode-time final)
  14945. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14946. (format "%04d-%02d-%02d %02d:%02d"
  14947. (nth 5 final) (nth 4 final) (nth 3 final)
  14948. (nth 2 final) (nth 1 final))
  14949. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14950. (defvar org-def)
  14951. (defvar org-defdecode)
  14952. (defvar org-with-time)
  14953. (defun org-read-date-display ()
  14954. "Display the current date prompt interpretation in the minibuffer."
  14955. (when org-read-date-display-live
  14956. (when org-read-date-overlay
  14957. (delete-overlay org-read-date-overlay))
  14958. (when (minibufferp (current-buffer))
  14959. (save-excursion
  14960. (end-of-line 1)
  14961. (while (not (equal (buffer-substring
  14962. (max (point-min) (- (point) 4)) (point))
  14963. " "))
  14964. (insert " ")))
  14965. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14966. " " (or org-ans1 org-ans2)))
  14967. (org-end-time-was-given nil)
  14968. (f (org-read-date-analyze ans org-def org-defdecode))
  14969. (fmts (if org-dcst
  14970. org-time-stamp-custom-formats
  14971. org-time-stamp-formats))
  14972. (fmt (if (or org-with-time
  14973. (and (boundp 'org-time-was-given) org-time-was-given))
  14974. (cdr fmts)
  14975. (car fmts)))
  14976. (txt (format-time-string fmt (apply 'encode-time f)))
  14977. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14978. (txt (concat "=> " txt)))
  14979. (when (and org-end-time-was-given
  14980. (string-match org-plain-time-of-day-regexp txt))
  14981. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14982. org-end-time-was-given
  14983. (substring txt (match-end 0)))))
  14984. (when org-read-date-analyze-futurep
  14985. (setq txt (concat txt " (=>F)")))
  14986. (setq org-read-date-overlay
  14987. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14988. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14989. (defun org-read-date-analyze (ans org-def org-defdecode)
  14990. "Analyze the combined answer of the date prompt."
  14991. ;; FIXME: cleanup and comment
  14992. (let ((nowdecode (decode-time (current-time)))
  14993. delta deltan deltaw deltadef year month day
  14994. hour minute second wday pm h2 m2 tl wday1
  14995. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14996. (setq org-read-date-analyze-futurep nil
  14997. org-read-date-analyze-forced-year nil)
  14998. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14999. (setq ans "+0"))
  15000. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15001. (setq ans (replace-match "" t t ans)
  15002. deltan (car delta)
  15003. deltaw (nth 1 delta)
  15004. deltadef (nth 2 delta)))
  15005. ;; Check if there is an iso week date in there. If yes, store the
  15006. ;; info and postpone interpreting it until the rest of the parsing
  15007. ;; is done.
  15008. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15009. (setq iso-year (if (match-end 1)
  15010. (org-small-year-to-year
  15011. (string-to-number (match-string 1 ans))))
  15012. iso-weekday (if (match-end 3)
  15013. (string-to-number (match-string 3 ans)))
  15014. iso-week (string-to-number (match-string 2 ans)))
  15015. (setq ans (replace-match "" t t ans)))
  15016. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15017. (when (string-match
  15018. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15019. (setq year (if (match-end 2)
  15020. (string-to-number (match-string 2 ans))
  15021. (progn (setq kill-year t)
  15022. (string-to-number (format-time-string "%Y"))))
  15023. month (string-to-number (match-string 3 ans))
  15024. day (string-to-number (match-string 4 ans)))
  15025. (if (< year 100) (setq year (+ 2000 year)))
  15026. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15027. t nil ans)))
  15028. ;; Help matching dotted european dates
  15029. (when (string-match
  15030. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15031. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15032. (setq kill-year t)
  15033. (string-to-number (format-time-string "%Y")))
  15034. day (string-to-number (match-string 1 ans))
  15035. month (string-to-number (match-string 2 ans))
  15036. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15037. t nil ans)))
  15038. ;; Help matching american dates, like 5/30 or 5/30/7
  15039. (when (string-match
  15040. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15041. (setq year (if (match-end 4)
  15042. (string-to-number (match-string 4 ans))
  15043. (progn (setq kill-year t)
  15044. (string-to-number (format-time-string "%Y"))))
  15045. month (string-to-number (match-string 1 ans))
  15046. day (string-to-number (match-string 2 ans)))
  15047. (if (< year 100) (setq year (+ 2000 year)))
  15048. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15049. t nil ans)))
  15050. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15051. ;; If there is a time with am/pm, and *no* time without it, we convert
  15052. ;; so that matching will be successful.
  15053. (loop for i from 1 to 2 do ; twice, for end time as well
  15054. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15055. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15056. (setq hour (string-to-number (match-string 1 ans))
  15057. minute (if (match-end 3)
  15058. (string-to-number (match-string 3 ans))
  15059. 0)
  15060. pm (equal ?p
  15061. (string-to-char (downcase (match-string 4 ans)))))
  15062. (if (and (= hour 12) (not pm))
  15063. (setq hour 0)
  15064. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15065. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15066. t t ans))))
  15067. ;; Check if a time range is given as a duration
  15068. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15069. (setq hour (string-to-number (match-string 1 ans))
  15070. h2 (+ hour (string-to-number (match-string 3 ans)))
  15071. minute (string-to-number (match-string 2 ans))
  15072. m2 (+ minute (if (match-end 5) (string-to-number
  15073. (match-string 5 ans))0)))
  15074. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15075. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15076. t t ans)))
  15077. ;; Check if there is a time range
  15078. (when (boundp 'org-end-time-was-given)
  15079. (setq org-time-was-given nil)
  15080. (when (and (string-match org-plain-time-of-day-regexp ans)
  15081. (match-end 8))
  15082. (setq org-end-time-was-given (match-string 8 ans))
  15083. (setq ans (concat (substring ans 0 (match-beginning 7))
  15084. (substring ans (match-end 7))))))
  15085. (setq tl (parse-time-string ans)
  15086. day (or (nth 3 tl) (nth 3 org-defdecode))
  15087. month (or (nth 4 tl)
  15088. (if (and org-read-date-prefer-future
  15089. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15090. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15091. (nth 4 org-defdecode)))
  15092. year (or (and (not kill-year) (nth 5 tl))
  15093. (if (and org-read-date-prefer-future
  15094. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15095. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15096. (nth 5 org-defdecode)))
  15097. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15098. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15099. second (or (nth 0 tl) 0)
  15100. wday (nth 6 tl))
  15101. (when (and (eq org-read-date-prefer-future 'time)
  15102. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15103. (equal day (nth 3 nowdecode))
  15104. (equal month (nth 4 nowdecode))
  15105. (equal year (nth 5 nowdecode))
  15106. (nth 2 tl)
  15107. (or (< (nth 2 tl) (nth 2 nowdecode))
  15108. (and (= (nth 2 tl) (nth 2 nowdecode))
  15109. (nth 1 tl)
  15110. (< (nth 1 tl) (nth 1 nowdecode)))))
  15111. (setq day (1+ day)
  15112. futurep t))
  15113. ;; Special date definitions below
  15114. (cond
  15115. (iso-week
  15116. ;; There was an iso week
  15117. (require 'cal-iso)
  15118. (setq futurep nil)
  15119. (setq year (or iso-year year)
  15120. day (or iso-weekday wday 1)
  15121. wday nil ; to make sure that the trigger below does not match
  15122. iso-date (calendar-gregorian-from-absolute
  15123. (calendar-absolute-from-iso
  15124. (list iso-week day year))))
  15125. ; FIXME: Should we also push ISO weeks into the future?
  15126. ; (when (and org-read-date-prefer-future
  15127. ; (not iso-year)
  15128. ; (< (calendar-absolute-from-gregorian iso-date)
  15129. ; (time-to-days (current-time))))
  15130. ; (setq year (1+ year)
  15131. ; iso-date (calendar-gregorian-from-absolute
  15132. ; (calendar-absolute-from-iso
  15133. ; (list iso-week day year)))))
  15134. (setq month (car iso-date)
  15135. year (nth 2 iso-date)
  15136. day (nth 1 iso-date)))
  15137. (deltan
  15138. (setq futurep nil)
  15139. (unless deltadef
  15140. (let ((now (decode-time (current-time))))
  15141. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15142. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15143. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15144. ((equal deltaw "m") (setq month (+ month deltan)))
  15145. ((equal deltaw "y") (setq year (+ year deltan)))))
  15146. ((and wday (not (nth 3 tl)))
  15147. ;; Weekday was given, but no day, so pick that day in the week
  15148. ;; on or after the derived date.
  15149. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15150. (unless (equal wday wday1)
  15151. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15152. (if (and (boundp 'org-time-was-given)
  15153. (nth 2 tl))
  15154. (setq org-time-was-given t))
  15155. (if (< year 100) (setq year (+ 2000 year)))
  15156. ;; Check of the date is representable
  15157. (if org-read-date-force-compatible-dates
  15158. (progn
  15159. (if (< year 1970)
  15160. (setq year 1970 org-read-date-analyze-forced-year t))
  15161. (if (> year 2037)
  15162. (setq year 2037 org-read-date-analyze-forced-year t)))
  15163. (condition-case nil
  15164. (ignore (encode-time second minute hour day month year))
  15165. (error
  15166. (setq year (nth 5 org-defdecode))
  15167. (setq org-read-date-analyze-forced-year t))))
  15168. (setq org-read-date-analyze-futurep futurep)
  15169. (list second minute hour day month year)))
  15170. (defvar parse-time-weekdays)
  15171. (defun org-read-date-get-relative (s today default)
  15172. "Check string S for special relative date string.
  15173. TODAY and DEFAULT are internal times, for today and for a default.
  15174. Return shift list (N what def-flag)
  15175. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15176. N is the number of WHATs to shift.
  15177. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15178. the DEFAULT date rather than TODAY."
  15179. (require 'parse-time)
  15180. (when (and
  15181. (string-match
  15182. (concat
  15183. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15184. "\\([0-9]+\\)?"
  15185. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15186. "\\([ \t]\\|$\\)") s)
  15187. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15188. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15189. (string-to-char (substring (match-string 1 s) -1))
  15190. ?+))
  15191. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15192. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15193. (what (if (match-end 3) (match-string 3 s) "d"))
  15194. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15195. (date (if rel default today))
  15196. (wday (nth 6 (decode-time date)))
  15197. delta)
  15198. (if wday1
  15199. (progn
  15200. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15201. (if (= delta 0) (setq delta 7))
  15202. (if (= dir ?-)
  15203. (progn
  15204. (setq delta (- delta 7))
  15205. (if (= delta 0) (setq delta -7))))
  15206. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15207. (list delta "d" rel))
  15208. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15209. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15210. "Turn a user-specified date into the internal representation.
  15211. The internal representation needed by the calendar is (month day year).
  15212. This is a wrapper to handle the brain-dead convention in calendar that
  15213. user function argument order change dependent on argument order."
  15214. (if (boundp 'calendar-date-style)
  15215. (cond
  15216. ((eq calendar-date-style 'american)
  15217. (list arg1 arg2 arg3))
  15218. ((eq calendar-date-style 'european)
  15219. (list arg2 arg1 arg3))
  15220. ((eq calendar-date-style 'iso)
  15221. (list arg2 arg3 arg1)))
  15222. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15223. (if (org-bound-and-true-p european-calendar-style)
  15224. (list arg2 arg1 arg3)
  15225. (list arg1 arg2 arg3)))))
  15226. (defun org-eval-in-calendar (form &optional keepdate)
  15227. "Eval FORM in the calendar window and return to current window.
  15228. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15229. otherwise stick to the current value of `org-ans2'."
  15230. (let ((sf (selected-frame))
  15231. (sw (selected-window)))
  15232. (select-window (get-buffer-window "*Calendar*" t))
  15233. (eval form)
  15234. (when (and (not keepdate) (calendar-cursor-to-date))
  15235. (let* ((date (calendar-cursor-to-date))
  15236. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15237. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15238. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15239. (select-window sw)
  15240. (org-select-frame-set-input-focus sf)))
  15241. (defun org-calendar-select ()
  15242. "Return to `org-read-date' with the date currently selected.
  15243. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15244. (interactive)
  15245. (when (calendar-cursor-to-date)
  15246. (let* ((date (calendar-cursor-to-date))
  15247. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15248. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15249. (if (active-minibuffer-window) (exit-minibuffer))))
  15250. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15251. "Insert a date stamp for the date given by the internal TIME.
  15252. WITH-HM means use the stamp format that includes the time of the day.
  15253. INACTIVE means use square brackets instead of angular ones, so that the
  15254. stamp will not contribute to the agenda.
  15255. PRE and POST are optional strings to be inserted before and after the
  15256. stamp.
  15257. The command returns the inserted time stamp."
  15258. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15259. stamp)
  15260. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15261. (insert-before-markers (or pre ""))
  15262. (when (listp extra)
  15263. (setq extra (car extra))
  15264. (if (and (stringp extra)
  15265. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15266. (setq extra (format "-%02d:%02d"
  15267. (string-to-number (match-string 1 extra))
  15268. (string-to-number (match-string 2 extra))))
  15269. (setq extra nil)))
  15270. (when extra
  15271. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15272. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15273. (insert-before-markers (or post ""))
  15274. (setq org-last-inserted-timestamp stamp)))
  15275. (defun org-toggle-time-stamp-overlays ()
  15276. "Toggle the use of custom time stamp formats."
  15277. (interactive)
  15278. (setq org-display-custom-times (not org-display-custom-times))
  15279. (unless org-display-custom-times
  15280. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15281. (while (setq p (next-single-property-change p 'display))
  15282. (if (and (get-text-property p 'display)
  15283. (eq (get-text-property p 'face) 'org-date))
  15284. (remove-text-properties
  15285. p (setq p (next-single-property-change p 'display))
  15286. '(display t))))
  15287. (set-buffer-modified-p bmp)))
  15288. (if (featurep 'xemacs)
  15289. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15290. (org-restart-font-lock)
  15291. (setq org-table-may-need-update t)
  15292. (if org-display-custom-times
  15293. (message "Time stamps are overlaid with custom format")
  15294. (message "Time stamp overlays removed")))
  15295. (defun org-display-custom-time (beg end)
  15296. "Overlay modified time stamp format over timestamp between BEG and END."
  15297. (let* ((ts (buffer-substring beg end))
  15298. t1 w1 with-hm tf time str w2 (off 0))
  15299. (save-match-data
  15300. (setq t1 (org-parse-time-string ts t))
  15301. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15302. (setq off (- (match-end 0) (match-beginning 0)))))
  15303. (setq end (- end off))
  15304. (setq w1 (- end beg)
  15305. with-hm (and (nth 1 t1) (nth 2 t1))
  15306. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15307. time (org-fix-decoded-time t1)
  15308. str (org-add-props
  15309. (format-time-string
  15310. (substring tf 1 -1) (apply 'encode-time time))
  15311. nil 'mouse-face 'highlight)
  15312. w2 (length str))
  15313. (if (not (= w2 w1))
  15314. (add-text-properties (1+ beg) (+ 2 beg)
  15315. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15316. (if (featurep 'xemacs)
  15317. (progn
  15318. (put-text-property beg end 'invisible t)
  15319. (put-text-property beg end 'end-glyph (make-glyph str)))
  15320. (put-text-property beg end 'display str))))
  15321. (defun org-translate-time (string)
  15322. "Translate all timestamps in STRING to custom format.
  15323. But do this only if the variable `org-display-custom-times' is set."
  15324. (when org-display-custom-times
  15325. (save-match-data
  15326. (let* ((start 0)
  15327. (re org-ts-regexp-both)
  15328. t1 with-hm inactive tf time str beg end)
  15329. (while (setq start (string-match re string start))
  15330. (setq beg (match-beginning 0)
  15331. end (match-end 0)
  15332. t1 (save-match-data
  15333. (org-parse-time-string (substring string beg end) t))
  15334. with-hm (and (nth 1 t1) (nth 2 t1))
  15335. inactive (equal (substring string beg (1+ beg)) "[")
  15336. tf (funcall (if with-hm 'cdr 'car)
  15337. org-time-stamp-custom-formats)
  15338. time (org-fix-decoded-time t1)
  15339. str (format-time-string
  15340. (concat
  15341. (if inactive "[" "<") (substring tf 1 -1)
  15342. (if inactive "]" ">"))
  15343. (apply 'encode-time time))
  15344. string (replace-match str t t string)
  15345. start (+ start (length str)))))))
  15346. string)
  15347. (defun org-fix-decoded-time (time)
  15348. "Set 0 instead of nil for the first 6 elements of time.
  15349. Don't touch the rest."
  15350. (let ((n 0))
  15351. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15352. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15353. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15354. "Difference between TIMESTAMP-STRING and now in days.
  15355. If SECONDS is non-nil, return the difference in seconds."
  15356. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15357. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15358. (funcall fdiff (current-time)))))
  15359. (defun org-deadline-close (timestamp-string &optional ndays)
  15360. "Is the time in TIMESTAMP-STRING close to the current date?"
  15361. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15362. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15363. (not (org-entry-is-done-p))))
  15364. (defun org-get-wdays (ts &optional delay zero-delay)
  15365. "Get the deadline lead time appropriate for timestring TS.
  15366. When DELAY is non-nil, get the delay time for scheduled items
  15367. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15368. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15369. don't try to find the delay cookie in the scheduled timestamp."
  15370. (let ((tv (if delay org-scheduled-delay-days
  15371. org-deadline-warning-days)))
  15372. (cond
  15373. ((or (and delay (< tv 0))
  15374. (and delay zero-delay (<= tv 0))
  15375. (and (not delay) (<= tv 0)))
  15376. ;; Enforce this value no matter what
  15377. (- tv))
  15378. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15379. ;; lead time is specified.
  15380. (floor (* (string-to-number (match-string 1 ts))
  15381. (cdr (assoc (match-string 2 ts)
  15382. '(("d" . 1) ("w" . 7)
  15383. ("m" . 30.4) ("y" . 365.25)
  15384. ("h" . 0.041667)))))))
  15385. ;; go for the default.
  15386. (t tv))))
  15387. (defun org-calendar-select-mouse (ev)
  15388. "Return to `org-read-date' with the date currently selected.
  15389. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15390. (interactive "e")
  15391. (mouse-set-point ev)
  15392. (when (calendar-cursor-to-date)
  15393. (let* ((date (calendar-cursor-to-date))
  15394. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15395. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15396. (if (active-minibuffer-window) (exit-minibuffer))))
  15397. (defun org-check-deadlines (ndays)
  15398. "Check if there are any deadlines due or past due.
  15399. A deadline is considered due if it happens within `org-deadline-warning-days'
  15400. days from today's date. If the deadline appears in an entry marked DONE,
  15401. it is not shown. The prefix arg NDAYS can be used to test that many
  15402. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15403. (interactive "P")
  15404. (let* ((org-warn-days
  15405. (cond
  15406. ((equal ndays '(4)) 100000)
  15407. (ndays (prefix-numeric-value ndays))
  15408. (t (abs org-deadline-warning-days))))
  15409. (case-fold-search nil)
  15410. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15411. (callback
  15412. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15413. (message "%d deadlines past-due or due within %d days"
  15414. (org-occur regexp nil callback)
  15415. org-warn-days)))
  15416. (defsubst org-re-timestamp (type)
  15417. "Return a regexp for timestamp TYPE.
  15418. Allowed values for TYPE are:
  15419. all: all timestamps
  15420. active: only active timestamps (<...>)
  15421. inactive: only inactive timestamps ([...])
  15422. scheduled: only scheduled timestamps
  15423. deadline: only deadline timestamps
  15424. closed: only closed time-stamps
  15425. When TYPE is nil, fall back on returning a regexp that matches
  15426. both scheduled and deadline timestamps."
  15427. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15428. ((eq type 'active) org-ts-regexp)
  15429. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15430. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15431. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15432. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15433. ((eq type 'scheduled-or-deadline)
  15434. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15435. (defun org-check-before-date (date)
  15436. "Check if there are deadlines or scheduled entries before DATE."
  15437. (interactive (list (org-read-date)))
  15438. (let ((case-fold-search nil)
  15439. (regexp (org-re-timestamp org-ts-type))
  15440. (callback
  15441. (lambda () (time-less-p
  15442. (org-time-string-to-time (match-string 1))
  15443. (org-time-string-to-time date)))))
  15444. (message "%d entries before %s"
  15445. (org-occur regexp nil callback) date)))
  15446. (defun org-check-after-date (date)
  15447. "Check if there are deadlines or scheduled entries after DATE."
  15448. (interactive (list (org-read-date)))
  15449. (let ((case-fold-search nil)
  15450. (regexp (org-re-timestamp org-ts-type))
  15451. (callback
  15452. (lambda () (not
  15453. (time-less-p
  15454. (org-time-string-to-time (match-string 1))
  15455. (org-time-string-to-time date))))))
  15456. (message "%d entries after %s"
  15457. (org-occur regexp nil callback) date)))
  15458. (defun org-check-dates-range (start-date end-date)
  15459. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15460. (interactive (list (org-read-date nil nil nil "Range starts")
  15461. (org-read-date nil nil nil "Range end")))
  15462. (let ((case-fold-search nil)
  15463. (regexp (org-re-timestamp org-ts-type))
  15464. (callback
  15465. (lambda ()
  15466. (let ((match (match-string 1)))
  15467. (and
  15468. (not (time-less-p
  15469. (org-time-string-to-time match)
  15470. (org-time-string-to-time start-date)))
  15471. (time-less-p
  15472. (org-time-string-to-time match)
  15473. (org-time-string-to-time end-date)))))))
  15474. (message "%d entries between %s and %s"
  15475. (org-occur regexp nil callback) start-date end-date)))
  15476. (defun org-evaluate-time-range (&optional to-buffer)
  15477. "Evaluate a time range by computing the difference between start and end.
  15478. Normally the result is just printed in the echo area, but with prefix arg
  15479. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15480. If the time range is actually in a table, the result is inserted into the
  15481. next column.
  15482. For time difference computation, a year is assumed to be exactly 365
  15483. days in order to avoid rounding problems."
  15484. (interactive "P")
  15485. (or
  15486. (org-clock-update-time-maybe)
  15487. (save-excursion
  15488. (unless (org-at-date-range-p t)
  15489. (goto-char (point-at-bol))
  15490. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15491. (if (not (org-at-date-range-p t))
  15492. (user-error "Not at a time-stamp range, and none found in current line")))
  15493. (let* ((ts1 (match-string 1))
  15494. (ts2 (match-string 2))
  15495. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15496. (match-end (match-end 0))
  15497. (time1 (org-time-string-to-time ts1))
  15498. (time2 (org-time-string-to-time ts2))
  15499. (t1 (org-float-time time1))
  15500. (t2 (org-float-time time2))
  15501. (diff (abs (- t2 t1)))
  15502. (negative (< (- t2 t1) 0))
  15503. ;; (ys (floor (* 365 24 60 60)))
  15504. (ds (* 24 60 60))
  15505. (hs (* 60 60))
  15506. (fy "%dy %dd %02d:%02d")
  15507. (fy1 "%dy %dd")
  15508. (fd "%dd %02d:%02d")
  15509. (fd1 "%dd")
  15510. (fh "%02d:%02d")
  15511. y d h m align)
  15512. (if havetime
  15513. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15514. y 0
  15515. d (floor (/ diff ds)) diff (mod diff ds)
  15516. h (floor (/ diff hs)) diff (mod diff hs)
  15517. m (floor (/ diff 60)))
  15518. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15519. y 0
  15520. d (floor (+ (/ diff ds) 0.5))
  15521. h 0 m 0))
  15522. (if (not to-buffer)
  15523. (message "%s" (org-make-tdiff-string y d h m))
  15524. (if (org-at-table-p)
  15525. (progn
  15526. (goto-char match-end)
  15527. (setq align t)
  15528. (and (looking-at " *|") (goto-char (match-end 0))))
  15529. (goto-char match-end))
  15530. (if (looking-at
  15531. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15532. (replace-match ""))
  15533. (if negative (insert " -"))
  15534. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15535. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15536. (insert " " (format fh h m))))
  15537. (if align (org-table-align))
  15538. (message "Time difference inserted")))))
  15539. (defun org-make-tdiff-string (y d h m)
  15540. (let ((fmt "")
  15541. (l nil))
  15542. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15543. l (push y l)))
  15544. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15545. l (push d l)))
  15546. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15547. l (push h l)))
  15548. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15549. l (push m l)))
  15550. (apply 'format fmt (nreverse l))))
  15551. (defun org-time-string-to-time (s &optional buffer pos)
  15552. "Convert a timestamp string into internal time."
  15553. (condition-case errdata
  15554. (apply 'encode-time (org-parse-time-string s))
  15555. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15556. s (if (not (and buffer pos))
  15557. ""
  15558. (format " at %d in buffer `%s'" pos buffer))
  15559. (cdr errdata)))))
  15560. (defun org-time-string-to-seconds (s)
  15561. "Convert a timestamp string to a number of seconds."
  15562. (org-float-time (org-time-string-to-time s)))
  15563. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15564. "Convert a time stamp to an absolute day number.
  15565. If there is a specifier for a cyclic time stamp, get the closest
  15566. date to DAYNR.
  15567. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15568. The variable `date' is bound by the calendar when this is called."
  15569. (cond
  15570. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15571. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15572. daynr
  15573. (+ daynr 1000)))
  15574. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15575. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15576. (time-to-days (current-time))) (match-string 0 s)
  15577. prefer show-all))
  15578. (t (time-to-days
  15579. (condition-case errdata
  15580. (apply 'encode-time (org-parse-time-string s))
  15581. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15582. s (if (not (and buffer pos))
  15583. ""
  15584. (format " at %d in buffer `%s'" pos buffer))
  15585. (cdr errdata))))))))
  15586. (defun org-days-to-iso-week (days)
  15587. "Return the iso week number."
  15588. (require 'cal-iso)
  15589. (car (calendar-iso-from-absolute days)))
  15590. (defun org-small-year-to-year (year)
  15591. "Convert 2-digit years into 4-digit years.
  15592. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15593. The year 2000 cannot be abbreviated. Any year larger than 99
  15594. is returned unchanged."
  15595. (if (< year 38)
  15596. (setq year (+ 2000 year))
  15597. (if (< year 100)
  15598. (setq year (+ 1900 year))))
  15599. year)
  15600. (defun org-time-from-absolute (d)
  15601. "Return the time corresponding to date D.
  15602. D may be an absolute day number, or a calendar-type list (month day year)."
  15603. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15604. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15605. (defun org-calendar-holiday ()
  15606. "List of holidays, for Diary display in Org-mode."
  15607. (require 'holidays)
  15608. (let ((hl (funcall
  15609. (if (fboundp 'calendar-check-holidays)
  15610. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15611. (if hl (mapconcat 'identity hl "; "))))
  15612. (defun org-diary-sexp-entry (sexp entry date)
  15613. "Process a SEXP diary ENTRY for DATE."
  15614. (require 'diary-lib)
  15615. (let ((result (if calendar-debug-sexp
  15616. (let ((stack-trace-on-error t))
  15617. (eval (car (read-from-string sexp))))
  15618. (condition-case nil
  15619. (eval (car (read-from-string sexp)))
  15620. (error
  15621. (beep)
  15622. (message "Bad sexp at line %d in %s: %s"
  15623. (org-current-line)
  15624. (buffer-file-name) sexp)
  15625. (sleep-for 2))))))
  15626. (cond ((stringp result) (split-string result "; "))
  15627. ((and (consp result)
  15628. (not (consp (cdr result)))
  15629. (stringp (cdr result))) (cdr result))
  15630. ((and (consp result)
  15631. (stringp (car result))) result)
  15632. (result entry))))
  15633. (defun org-diary-to-ical-string (frombuf)
  15634. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15635. This uses the icalendar.el library."
  15636. (let* ((tmpdir (if (featurep 'xemacs)
  15637. (temp-directory)
  15638. temporary-file-directory))
  15639. (tmpfile (make-temp-name
  15640. (expand-file-name "orgics" tmpdir)))
  15641. buf rtn b e)
  15642. (with-current-buffer frombuf
  15643. (icalendar-export-region (point-min) (point-max) tmpfile)
  15644. (setq buf (find-buffer-visiting tmpfile))
  15645. (set-buffer buf)
  15646. (goto-char (point-min))
  15647. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15648. (setq b (match-beginning 0)))
  15649. (goto-char (point-max))
  15650. (if (re-search-backward "^END:VEVENT" nil t)
  15651. (setq e (match-end 0)))
  15652. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15653. (kill-buffer buf)
  15654. (delete-file tmpfile)
  15655. rtn))
  15656. (defun org-closest-date (start current change prefer show-all)
  15657. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15658. When PREFER is `past', return a date that is either CURRENT or past.
  15659. When PREFER is `future', return a date that is either CURRENT or future.
  15660. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15661. ;; Make the proper lists from the dates
  15662. (catch 'exit
  15663. (let ((a1 '(("h" . hour)
  15664. ("d" . day)
  15665. ("w" . week)
  15666. ("m" . month)
  15667. ("y" . year)))
  15668. (shour (nth 2 (org-parse-time-string start)))
  15669. dn dw sday cday n1 n2 n0
  15670. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15671. (setq start (org-date-to-gregorian start)
  15672. current (org-date-to-gregorian
  15673. (if show-all
  15674. current
  15675. (time-to-days (current-time))))
  15676. sday (calendar-absolute-from-gregorian start)
  15677. cday (calendar-absolute-from-gregorian current))
  15678. (if (<= cday sday) (throw 'exit sday))
  15679. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15680. (setq dn (string-to-number (match-string 1 change))
  15681. dw (cdr (assoc (match-string 2 change) a1)))
  15682. (user-error "Invalid change specifier: %s" change))
  15683. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15684. (cond
  15685. ((eq dw 'hour)
  15686. (let ((missing-hours
  15687. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15688. dn)))
  15689. (setq n1 (if (zerop missing-hours) cday
  15690. (- cday (1+ (floor (/ missing-hours 24)))))
  15691. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15692. ((eq dw 'day)
  15693. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15694. n2 (+ n1 dn)))
  15695. ((eq dw 'year)
  15696. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15697. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15698. (setq date1 (list m d y1)
  15699. n1 (calendar-absolute-from-gregorian date1)
  15700. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15701. n2 (calendar-absolute-from-gregorian date2)))
  15702. ((eq dw 'month)
  15703. ;; approx number of month between the two dates
  15704. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15705. ;; How often does dn fit in there?
  15706. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15707. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15708. m (+ m nm)
  15709. ny (floor (/ m 12))
  15710. y (+ y ny)
  15711. m (- m (* ny 12)))
  15712. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15713. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15714. (setq m2 (+ m dn) y2 y)
  15715. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15716. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15717. (while (<= n2 cday)
  15718. (setq n1 n2 m m2 y y2)
  15719. (setq m2 (+ m dn) y2 y)
  15720. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15721. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15722. ;; Make sure n1 is the earlier date
  15723. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15724. (if show-all
  15725. (cond
  15726. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15727. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15728. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15729. (cond
  15730. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15731. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15732. (t (if (= cday n1) n1 n2)))))))
  15733. (defun org-date-to-gregorian (date)
  15734. "Turn any specification of DATE into a Gregorian date for the calendar."
  15735. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15736. ((and (listp date) (= (length date) 3)) date)
  15737. ((stringp date)
  15738. (setq date (org-parse-time-string date))
  15739. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15740. ((listp date)
  15741. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15742. (defun org-parse-time-string (s &optional nodefault)
  15743. "Parse the standard Org-mode time string.
  15744. This should be a lot faster than the normal `parse-time-string'.
  15745. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15746. hour and minute fields will be nil if not given."
  15747. (cond ((string-match org-ts-regexp0 s)
  15748. (list 0
  15749. (if (or (match-beginning 8) (not nodefault))
  15750. (string-to-number (or (match-string 8 s) "0")))
  15751. (if (or (match-beginning 7) (not nodefault))
  15752. (string-to-number (or (match-string 7 s) "0")))
  15753. (string-to-number (match-string 4 s))
  15754. (string-to-number (match-string 3 s))
  15755. (string-to-number (match-string 2 s))
  15756. nil nil nil))
  15757. ((string-match "^<[^>]+>$" s)
  15758. (decode-time (seconds-to-time (org-matcher-time s))))
  15759. (t (error "Not a standard Org-mode time string: %s" s))))
  15760. (defun org-timestamp-up (&optional arg)
  15761. "Increase the date item at the cursor by one.
  15762. If the cursor is on the year, change the year. If it is on the month,
  15763. the day or the time, change that.
  15764. With prefix ARG, change by that many units."
  15765. (interactive "p")
  15766. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15767. (defun org-timestamp-down (&optional arg)
  15768. "Decrease the date item at the cursor by one.
  15769. If the cursor is on the year, change the year. If it is on the month,
  15770. the day or the time, change that.
  15771. With prefix ARG, change by that many units."
  15772. (interactive "p")
  15773. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15774. (defun org-timestamp-up-day (&optional arg)
  15775. "Increase the date in the time stamp by one day.
  15776. With prefix ARG, change that many days."
  15777. (interactive "p")
  15778. (if (and (not (org-at-timestamp-p t))
  15779. (org-at-heading-p))
  15780. (org-todo 'up)
  15781. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15782. (defun org-timestamp-down-day (&optional arg)
  15783. "Decrease the date in the time stamp by one day.
  15784. With prefix ARG, change that many days."
  15785. (interactive "p")
  15786. (if (and (not (org-at-timestamp-p t))
  15787. (org-at-heading-p))
  15788. (org-todo 'down)
  15789. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15790. (defun org-at-timestamp-p (&optional inactive-ok)
  15791. "Determine if the cursor is in or at a timestamp."
  15792. (interactive)
  15793. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15794. (pos (point))
  15795. (ans (or (looking-at tsr)
  15796. (save-excursion
  15797. (skip-chars-backward "^[<\n\r\t")
  15798. (if (> (point) (point-min)) (backward-char 1))
  15799. (and (looking-at tsr)
  15800. (> (- (match-end 0) pos) -1))))))
  15801. (and ans
  15802. (boundp 'org-ts-what)
  15803. (setq org-ts-what
  15804. (cond
  15805. ((= pos (match-beginning 0)) 'bracket)
  15806. ;; Point is considered to be "on the bracket" whether
  15807. ;; it's really on it or right after it.
  15808. ((= pos (1- (match-end 0))) 'bracket)
  15809. ((= pos (match-end 0)) 'after)
  15810. ((org-pos-in-match-range pos 2) 'year)
  15811. ((org-pos-in-match-range pos 3) 'month)
  15812. ((org-pos-in-match-range pos 7) 'hour)
  15813. ((org-pos-in-match-range pos 8) 'minute)
  15814. ((or (org-pos-in-match-range pos 4)
  15815. (org-pos-in-match-range pos 5)) 'day)
  15816. ((and (> pos (or (match-end 8) (match-end 5)))
  15817. (< pos (match-end 0)))
  15818. (- pos (or (match-end 8) (match-end 5))))
  15819. (t 'day))))
  15820. ans))
  15821. (defun org-toggle-timestamp-type ()
  15822. "Toggle the type (<active> or [inactive]) of a time stamp."
  15823. (interactive)
  15824. (when (org-at-timestamp-p t)
  15825. (let ((beg (match-beginning 0)) (end (match-end 0))
  15826. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15827. (save-excursion
  15828. (goto-char beg)
  15829. (while (re-search-forward "[][<>]" end t)
  15830. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15831. t t)))
  15832. (message "Timestamp is now %sactive"
  15833. (if (equal (char-after beg) ?<) "" "in")))))
  15834. (defun org-at-clock-log-p nil
  15835. "Is the cursor on the clock log line?"
  15836. (save-excursion
  15837. (move-beginning-of-line 1)
  15838. (looking-at "^[ \t]*CLOCK:")))
  15839. (defvar org-clock-history) ; defined in org-clock.el
  15840. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15841. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15842. "Change the date in the time stamp at point.
  15843. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15844. `year', `minute', `second'. If WHAT is not given, the cursor position
  15845. in the timestamp determines what will be changed.
  15846. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15847. (let ((origin (point)) origin-cat
  15848. with-hm inactive
  15849. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15850. org-ts-what
  15851. extra rem
  15852. ts time time0 fixnext clrgx)
  15853. (if (not (org-at-timestamp-p t))
  15854. (user-error "Not at a timestamp"))
  15855. (if (and (not what) (eq org-ts-what 'bracket))
  15856. (org-toggle-timestamp-type)
  15857. ;; Point isn't on brackets. Remember the part of the time-stamp
  15858. ;; the point was in. Indeed, size of time-stamps may change,
  15859. ;; but point must be kept in the same category nonetheless.
  15860. (setq origin-cat org-ts-what)
  15861. (if (and (not what) (not (eq org-ts-what 'day))
  15862. org-display-custom-times
  15863. (get-text-property (point) 'display)
  15864. (not (get-text-property (1- (point)) 'display)))
  15865. (setq org-ts-what 'day))
  15866. (setq org-ts-what (or what org-ts-what)
  15867. inactive (= (char-after (match-beginning 0)) ?\[)
  15868. ts (match-string 0))
  15869. (replace-match "")
  15870. (when (string-match
  15871. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15872. ts)
  15873. (setq extra (match-string 1 ts))
  15874. (if suppress-tmp-delay
  15875. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15876. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15877. (setq with-hm t))
  15878. (setq time0 (org-parse-time-string ts))
  15879. (when (and updown
  15880. (eq org-ts-what 'minute)
  15881. (not current-prefix-arg))
  15882. ;; This looks like s-up and s-down. Change by one rounding step.
  15883. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15884. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15885. (setcar (cdr time0) (+ (nth 1 time0)
  15886. (if (> n 0) (- rem) (- dm rem))))))
  15887. (setq time
  15888. (encode-time (or (car time0) 0)
  15889. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15890. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15891. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15892. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15893. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15894. (nthcdr 6 time0)))
  15895. (when (and (member org-ts-what '(hour minute))
  15896. extra
  15897. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15898. (setq extra (org-modify-ts-extra
  15899. extra
  15900. (if (eq org-ts-what 'hour) 2 5)
  15901. n dm)))
  15902. (when (integerp org-ts-what)
  15903. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15904. (if (eq what 'calendar)
  15905. (let ((cal-date (org-get-date-from-calendar)))
  15906. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15907. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15908. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15909. (setcar time0 (or (car time0) 0))
  15910. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15911. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15912. (setq time (apply 'encode-time time0))))
  15913. ;; Insert the new time-stamp, and ensure point stays in the same
  15914. ;; category as before (i.e. not after the last position in that
  15915. ;; category).
  15916. (let ((pos (point)))
  15917. ;; Stay before inserted string. `save-excursion' is of no use.
  15918. (setq org-last-changed-timestamp
  15919. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15920. (goto-char pos))
  15921. (save-match-data
  15922. (looking-at org-ts-regexp3)
  15923. (goto-char (cond
  15924. ;; `day' category ends before `hour' if any, or at
  15925. ;; the end of the day name.
  15926. ((eq origin-cat 'day)
  15927. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15928. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15929. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15930. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15931. ;; `year' and `month' have both fixed size: point
  15932. ;; couldn't have moved into another part.
  15933. (t origin))))
  15934. ;; Update clock if on a CLOCK line.
  15935. (org-clock-update-time-maybe)
  15936. ;; Maybe adjust the closest clock in `org-clock-history'
  15937. (when org-clock-adjust-closest
  15938. (if (not (and (org-at-clock-log-p)
  15939. (< 1 (length (delq nil (mapcar 'marker-position
  15940. org-clock-history))))))
  15941. (message "No clock to adjust")
  15942. (cond ((save-excursion ; fix previous clock?
  15943. (re-search-backward org-ts-regexp0 nil t)
  15944. (org-looking-back (concat org-clock-string " \\[")))
  15945. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15946. ((save-excursion ; fix next clock?
  15947. (re-search-backward org-ts-regexp0 nil t)
  15948. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15949. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15950. (save-window-excursion
  15951. ;; Find closest clock to point, adjust the previous/next one in history
  15952. (let* ((p (save-excursion (org-back-to-heading t)))
  15953. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15954. (clfixnth
  15955. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15956. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15957. (if (not clfixpos)
  15958. (message "No clock to adjust")
  15959. (save-excursion
  15960. (org-goto-marker-or-bmk clfixpos)
  15961. (org-show-subtree)
  15962. (when (re-search-forward clrgx nil t)
  15963. (goto-char (match-beginning 1))
  15964. (let (org-clock-adjust-closest)
  15965. (org-timestamp-change n org-ts-what updown))
  15966. (message "Clock adjusted in %s for heading: %s"
  15967. (file-name-nondirectory (buffer-file-name))
  15968. (org-get-heading t t)))))))))
  15969. ;; Try to recenter the calendar window, if any.
  15970. (if (and org-calendar-follow-timestamp-change
  15971. (get-buffer-window "*Calendar*" t)
  15972. (memq org-ts-what '(day month year)))
  15973. (org-recenter-calendar (time-to-days time))))))
  15974. (defun org-modify-ts-extra (s pos n dm)
  15975. "Change the different parts of the lead-time and repeat fields in timestamp."
  15976. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15977. ng h m new rem)
  15978. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15979. (cond
  15980. ((or (org-pos-in-match-range pos 2)
  15981. (org-pos-in-match-range pos 3))
  15982. (setq m (string-to-number (match-string 3 s))
  15983. h (string-to-number (match-string 2 s)))
  15984. (if (org-pos-in-match-range pos 2)
  15985. (setq h (+ h n))
  15986. (setq n (* dm (org-no-warnings (signum n))))
  15987. (when (not (= 0 (setq rem (% m dm))))
  15988. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15989. (setq m (+ m n)))
  15990. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15991. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15992. (setq h (min 24 (max 0 h)))
  15993. (setq ng 1 new (format "-%02d:%02d" h m)))
  15994. ((org-pos-in-match-range pos 6)
  15995. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15996. ((org-pos-in-match-range pos 5)
  15997. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15998. ((org-pos-in-match-range pos 9)
  15999. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16000. ((org-pos-in-match-range pos 8)
  16001. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16002. (when ng
  16003. (setq s (concat
  16004. (substring s 0 (match-beginning ng))
  16005. new
  16006. (substring s (match-end ng))))))
  16007. s))
  16008. (defun org-recenter-calendar (date)
  16009. "If the calendar is visible, recenter it to DATE."
  16010. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16011. (when cwin
  16012. (let ((calendar-move-hook nil))
  16013. (with-selected-window cwin
  16014. (calendar-goto-date (if (listp date) date
  16015. (calendar-gregorian-from-absolute date))))))))
  16016. (defun org-goto-calendar (&optional arg)
  16017. "Go to the Emacs calendar at the current date.
  16018. If there is a time stamp in the current line, go to that date.
  16019. A prefix ARG can be used to force the current date."
  16020. (interactive "P")
  16021. (let ((tsr org-ts-regexp) diff
  16022. (calendar-move-hook nil)
  16023. (calendar-view-holidays-initially-flag nil)
  16024. (calendar-view-diary-initially-flag nil))
  16025. (if (or (org-at-timestamp-p)
  16026. (save-excursion
  16027. (beginning-of-line 1)
  16028. (looking-at (concat ".*" tsr))))
  16029. (let ((d1 (time-to-days (current-time)))
  16030. (d2 (time-to-days
  16031. (org-time-string-to-time (match-string 1)))))
  16032. (setq diff (- d2 d1))))
  16033. (calendar)
  16034. (calendar-goto-today)
  16035. (if (and diff (not arg)) (calendar-forward-day diff))))
  16036. (defun org-get-date-from-calendar ()
  16037. "Return a list (month day year) of date at point in calendar."
  16038. (with-current-buffer "*Calendar*"
  16039. (save-match-data
  16040. (calendar-cursor-to-date))))
  16041. (defun org-date-from-calendar ()
  16042. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16043. If there is already a time stamp at the cursor position, update it."
  16044. (interactive)
  16045. (if (org-at-timestamp-p t)
  16046. (org-timestamp-change 0 'calendar)
  16047. (let ((cal-date (org-get-date-from-calendar)))
  16048. (org-insert-time-stamp
  16049. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16050. (defcustom org-effort-durations
  16051. `(("h" . 60)
  16052. ("d" . ,(* 60 8))
  16053. ("w" . ,(* 60 8 5))
  16054. ("m" . ,(* 60 8 5 4))
  16055. ("y" . ,(* 60 8 5 40)))
  16056. "Conversion factor to minutes for an effort modifier.
  16057. Each entry has the form (MODIFIER . MINUTES).
  16058. In an effort string, a number followed by MODIFIER is multiplied
  16059. by the specified number of MINUTES to obtain an effort in
  16060. minutes.
  16061. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16062. effort string \"2hours\" is equivalent to 120 minutes."
  16063. :group 'org-agenda
  16064. :version "24.1"
  16065. :type '(alist :key-type (string :tag "Modifier")
  16066. :value-type (number :tag "Minutes")))
  16067. (defun org-minutes-to-clocksum-string (m)
  16068. "Format number of minutes as a clocksum string.
  16069. The format is determined by `org-time-clocksum-format',
  16070. `org-time-clocksum-use-fractional' and
  16071. `org-time-clocksum-fractional-format' and
  16072. `org-time-clocksum-use-effort-durations'."
  16073. (let ((clocksum "")
  16074. (m (round m)) ; Don't allow fractions of minutes
  16075. h d w mo y fmt n)
  16076. (setq h (if org-time-clocksum-use-effort-durations
  16077. (cdr (assoc "h" org-effort-durations)) 60)
  16078. d (if org-time-clocksum-use-effort-durations
  16079. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16080. w (if org-time-clocksum-use-effort-durations
  16081. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16082. mo (if org-time-clocksum-use-effort-durations
  16083. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16084. y (if org-time-clocksum-use-effort-durations
  16085. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16086. ;; fractional format
  16087. (if org-time-clocksum-use-fractional
  16088. (cond
  16089. ;; single format string
  16090. ((stringp org-time-clocksum-fractional-format)
  16091. (format org-time-clocksum-fractional-format (/ m (float h))))
  16092. ;; choice of fractional formats for different time units
  16093. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16094. (> (/ (truncate m) (* y d h)) 0))
  16095. (format fmt (/ m (* y d (float h)))))
  16096. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16097. (> (/ (truncate m) (* mo d h)) 0))
  16098. (format fmt (/ m (* mo d (float h)))))
  16099. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16100. (> (/ (truncate m) (* w d h)) 0))
  16101. (format fmt (/ m (* w d (float h)))))
  16102. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16103. (> (/ (truncate m) (* d h)) 0))
  16104. (format fmt (/ m (* d (float h)))))
  16105. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16106. (> (/ (truncate m) h) 0))
  16107. (format fmt (/ m (float h))))
  16108. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16109. (format fmt m))
  16110. ;; fall back to smallest time unit with a format
  16111. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16112. (format fmt (/ m (float h))))
  16113. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16114. (format fmt (/ m (* d (float h)))))
  16115. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16116. (format fmt (/ m (* w d (float h)))))
  16117. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16118. (format fmt (/ m (* mo d (float h)))))
  16119. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16120. (format fmt (/ m (* y d (float h))))))
  16121. ;; standard (non-fractional) format, with single format string
  16122. (if (stringp org-time-clocksum-format)
  16123. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16124. ;; separate formats components
  16125. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16126. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16127. (plist-get org-time-clocksum-format :require-years))
  16128. (setq clocksum (concat clocksum (format fmt n))
  16129. m (- m (* n y d h))))
  16130. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16131. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16132. (plist-get org-time-clocksum-format :require-months))
  16133. (setq clocksum (concat clocksum (format fmt n))
  16134. m (- m (* n mo d h))))
  16135. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16136. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16137. (plist-get org-time-clocksum-format :require-weeks))
  16138. (setq clocksum (concat clocksum (format fmt n))
  16139. m (- m (* n w d h))))
  16140. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16141. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16142. (plist-get org-time-clocksum-format :require-days))
  16143. (setq clocksum (concat clocksum (format fmt n))
  16144. m (- m (* n d h))))
  16145. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16146. (or (> (setq n (/ (truncate m) h)) 0)
  16147. (plist-get org-time-clocksum-format :require-hours))
  16148. (setq clocksum (concat clocksum (format fmt n))
  16149. m (- m (* n h))))
  16150. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16151. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16152. (setq clocksum (concat clocksum (format fmt m))))
  16153. ;; return formatted time duration
  16154. clocksum))))
  16155. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16156. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16157. "Org mode version 8.0")
  16158. (defun org-hours-to-clocksum-string (n)
  16159. (org-minutes-to-clocksum-string (* n 60)))
  16160. (defun org-hh:mm-string-to-minutes (s)
  16161. "Convert a string H:MM to a number of minutes.
  16162. If the string is just a number, interpret it as minutes.
  16163. In fact, the first hh:mm or number in the string will be taken,
  16164. there can be extra stuff in the string.
  16165. If no number is found, the return value is 0."
  16166. (cond
  16167. ((integerp s) s)
  16168. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16169. (+ (* (string-to-number (match-string 1 s)) 60)
  16170. (string-to-number (match-string 2 s))))
  16171. ((string-match "\\([0-9]+\\)" s)
  16172. (string-to-number (match-string 1 s)))
  16173. (t 0)))
  16174. (defcustom org-image-actual-width t
  16175. "Should we use the actual width of images when inlining them?
  16176. When set to `t', always use the image width.
  16177. When set to a number, use imagemagick (when available) to set
  16178. the image's width to this value.
  16179. When set to a number in a list, try to get the width from any
  16180. #+ATTR.* keyword if it matches a width specification like
  16181. #+ATTR_HTML: :width 300px
  16182. and fall back on that number if none is found.
  16183. When set to nil, try to get the width from an #+ATTR.* keyword
  16184. and fall back on the original width if none is found.
  16185. This requires Emacs >= 24.1, build with imagemagick support."
  16186. :group 'org-appearance
  16187. :version "24.4"
  16188. :package-version '(Org . "8.0")
  16189. :type '(choice
  16190. (const :tag "Use the image width" t)
  16191. (integer :tag "Use a number of pixels")
  16192. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16193. (const :tag "Use #+ATTR* or don't resize" nil)))
  16194. (defcustom org-agenda-inhibit-startup nil
  16195. "Inhibit startup when preparing agenda buffers.
  16196. When this variable is `t' (the default), the initialization of
  16197. the Org agenda buffers is inhibited: e.g. the visibility state
  16198. is not set, the tables are not re-aligned, etc."
  16199. :type 'boolean
  16200. :version "24.3"
  16201. :group 'org-agenda)
  16202. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16203. "Convert a duration string S to minutes.
  16204. A bare number is interpreted as minutes, modifiers can be set by
  16205. customizing `org-effort-durations' (which see).
  16206. Entries containing a colon are interpreted as H:MM by
  16207. `org-hh:mm-string-to-minutes'."
  16208. (let ((result 0)
  16209. (re (concat "\\([0-9.]+\\) *\\("
  16210. (regexp-opt (mapcar 'car org-effort-durations))
  16211. "\\)")))
  16212. (while (string-match re s)
  16213. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16214. (string-to-number (match-string 1 s))))
  16215. (setq s (replace-match "" nil t s)))
  16216. (setq result (floor result))
  16217. (incf result (org-hh:mm-string-to-minutes s))
  16218. (if output-to-string (number-to-string result) result)))
  16219. ;;;; Files
  16220. (defun org-save-all-org-buffers ()
  16221. "Save all Org-mode buffers without user confirmation."
  16222. (interactive)
  16223. (message "Saving all Org-mode buffers...")
  16224. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16225. (when (featurep 'org-id) (org-id-locations-save))
  16226. (message "Saving all Org-mode buffers... done"))
  16227. (defun org-revert-all-org-buffers ()
  16228. "Revert all Org-mode buffers.
  16229. Prompt for confirmation when there are unsaved changes.
  16230. Be sure you know what you are doing before letting this function
  16231. overwrite your changes.
  16232. This function is useful in a setup where one tracks org files
  16233. with a version control system, to revert on one machine after pulling
  16234. changes from another. I believe the procedure must be like this:
  16235. 1. M-x org-save-all-org-buffers
  16236. 2. Pull changes from the other machine, resolve conflicts
  16237. 3. M-x org-revert-all-org-buffers"
  16238. (interactive)
  16239. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16240. (user-error "Abort"))
  16241. (save-excursion
  16242. (save-window-excursion
  16243. (mapc
  16244. (lambda (b)
  16245. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16246. (with-current-buffer b buffer-file-name))
  16247. (org-pop-to-buffer-same-window b)
  16248. (revert-buffer t 'no-confirm)))
  16249. (buffer-list))
  16250. (when (and (featurep 'org-id) org-id-track-globally)
  16251. (org-id-locations-load)))))
  16252. ;;;; Agenda files
  16253. ;;;###autoload
  16254. (defun org-switchb (&optional arg)
  16255. "Switch between Org buffers.
  16256. With one prefix argument, restrict available buffers to files.
  16257. With two prefix arguments, restrict available buffers to agenda files.
  16258. Defaults to `iswitchb' for buffer name completion.
  16259. Set `org-completion-use-ido' to make it use ido instead."
  16260. (interactive "P")
  16261. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16262. ((equal arg '(16)) (org-buffer-list 'agenda))
  16263. (t (org-buffer-list))))
  16264. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16265. (org-completion-use-ido org-completion-use-ido))
  16266. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16267. (setq org-completion-use-iswitchb t))
  16268. (org-pop-to-buffer-same-window
  16269. (org-icompleting-read "Org buffer: "
  16270. (mapcar 'list (mapcar 'buffer-name blist))
  16271. nil t))))
  16272. ;;; Define some older names previously used for this functionality
  16273. ;;;###autoload
  16274. (defalias 'org-ido-switchb 'org-switchb)
  16275. ;;;###autoload
  16276. (defalias 'org-iswitchb 'org-switchb)
  16277. (defun org-buffer-list (&optional predicate exclude-tmp)
  16278. "Return a list of Org buffers.
  16279. PREDICATE can be `export', `files' or `agenda'.
  16280. export restrict the list to Export buffers.
  16281. files restrict the list to buffers visiting Org files.
  16282. agenda restrict the list to buffers visiting agenda files.
  16283. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16284. (let* ((bfn nil)
  16285. (agenda-files (and (eq predicate 'agenda)
  16286. (mapcar 'file-truename (org-agenda-files t))))
  16287. (filter
  16288. (cond
  16289. ((eq predicate 'files)
  16290. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16291. ((eq predicate 'export)
  16292. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16293. ((eq predicate 'agenda)
  16294. (lambda (b)
  16295. (with-current-buffer b
  16296. (and (derived-mode-p 'org-mode)
  16297. (setq bfn (buffer-file-name b))
  16298. (member (file-truename bfn) agenda-files)))))
  16299. (t (lambda (b) (with-current-buffer b
  16300. (or (derived-mode-p 'org-mode)
  16301. (string-match "\*Org .*Export"
  16302. (buffer-name b)))))))))
  16303. (delq nil
  16304. (mapcar
  16305. (lambda(b)
  16306. (if (and (funcall filter b)
  16307. (or (not exclude-tmp)
  16308. (not (string-match "tmp" (buffer-name b)))))
  16309. b
  16310. nil))
  16311. (buffer-list)))))
  16312. (defun org-agenda-files (&optional unrestricted archives)
  16313. "Get the list of agenda files.
  16314. Optional UNRESTRICTED means return the full list even if a restriction
  16315. is currently in place.
  16316. When ARCHIVES is t, include all archive files that are really being
  16317. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16318. `org-agenda-archives-mode' is t."
  16319. (let ((files
  16320. (cond
  16321. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16322. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16323. ((listp org-agenda-files) org-agenda-files)
  16324. (t (error "Invalid value of `org-agenda-files'")))))
  16325. (setq files (apply 'append
  16326. (mapcar (lambda (f)
  16327. (if (file-directory-p f)
  16328. (directory-files
  16329. f t org-agenda-file-regexp)
  16330. (list f)))
  16331. files)))
  16332. (when org-agenda-skip-unavailable-files
  16333. (setq files (delq nil
  16334. (mapcar (function
  16335. (lambda (file)
  16336. (and (file-readable-p file) file)))
  16337. files))))
  16338. (when (or (eq archives t)
  16339. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16340. (setq files (org-add-archive-files files)))
  16341. files))
  16342. (defun org-agenda-file-p (&optional file)
  16343. "Return non-nil, if FILE is an agenda file.
  16344. If FILE is omitted, use the file associated with the current
  16345. buffer."
  16346. (member (or file (buffer-file-name))
  16347. (org-agenda-files t)))
  16348. (defun org-edit-agenda-file-list ()
  16349. "Edit the list of agenda files.
  16350. Depending on setup, this either uses customize to edit the variable
  16351. `org-agenda-files', or it visits the file that is holding the list. In the
  16352. latter case, the buffer is set up in a way that saving it automatically kills
  16353. the buffer and restores the previous window configuration."
  16354. (interactive)
  16355. (if (stringp org-agenda-files)
  16356. (let ((cw (current-window-configuration)))
  16357. (find-file org-agenda-files)
  16358. (org-set-local 'org-window-configuration cw)
  16359. (org-add-hook 'after-save-hook
  16360. (lambda ()
  16361. (set-window-configuration
  16362. (prog1 org-window-configuration
  16363. (kill-buffer (current-buffer))))
  16364. (org-install-agenda-files-menu)
  16365. (message "New agenda file list installed"))
  16366. nil 'local)
  16367. (message "%s" (substitute-command-keys
  16368. "Edit list and finish with \\[save-buffer]")))
  16369. (customize-variable 'org-agenda-files)))
  16370. (defun org-store-new-agenda-file-list (list)
  16371. "Set new value for the agenda file list and save it correctly."
  16372. (if (stringp org-agenda-files)
  16373. (let ((fe (org-read-agenda-file-list t)) b u)
  16374. (while (setq b (find-buffer-visiting org-agenda-files))
  16375. (kill-buffer b))
  16376. (with-temp-file org-agenda-files
  16377. (insert
  16378. (mapconcat
  16379. (lambda (f) ;; Keep un-expanded entries.
  16380. (if (setq u (assoc f fe))
  16381. (cdr u)
  16382. f))
  16383. list "\n")
  16384. "\n")))
  16385. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16386. (org-insert-mode-line-in-empty-file nil))
  16387. (setq org-agenda-files list)
  16388. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16389. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16390. "Read the list of agenda files from a file.
  16391. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16392. filenames, used by `org-store-new-agenda-file-list' to write back
  16393. un-expanded file names."
  16394. (when (file-directory-p org-agenda-files)
  16395. (error "`org-agenda-files' cannot be a single directory"))
  16396. (when (stringp org-agenda-files)
  16397. (with-temp-buffer
  16398. (insert-file-contents org-agenda-files)
  16399. (mapcar
  16400. (lambda (f)
  16401. (let ((e (expand-file-name (substitute-in-file-name f)
  16402. org-directory)))
  16403. (if pair-with-expansion
  16404. (cons e f)
  16405. e)))
  16406. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16407. ;;;###autoload
  16408. (defun org-cycle-agenda-files ()
  16409. "Cycle through the files in `org-agenda-files'.
  16410. If the current buffer visits an agenda file, find the next one in the list.
  16411. If the current buffer does not, find the first agenda file."
  16412. (interactive)
  16413. (let* ((fs (org-agenda-files t))
  16414. (files (append fs (list (car fs))))
  16415. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16416. file)
  16417. (unless files (user-error "No agenda files"))
  16418. (catch 'exit
  16419. (while (setq file (pop files))
  16420. (if (equal (file-truename file) tcf)
  16421. (when (car files)
  16422. (find-file (car files))
  16423. (throw 'exit t))))
  16424. (find-file (car fs)))
  16425. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16426. (defun org-agenda-file-to-front (&optional to-end)
  16427. "Move/add the current file to the top of the agenda file list.
  16428. If the file is not present in the list, it is added to the front. If it is
  16429. present, it is moved there. With optional argument TO-END, add/move to the
  16430. end of the list."
  16431. (interactive "P")
  16432. (let ((org-agenda-skip-unavailable-files nil)
  16433. (file-alist (mapcar (lambda (x)
  16434. (cons (file-truename x) x))
  16435. (org-agenda-files t)))
  16436. (ctf (file-truename
  16437. (or buffer-file-name
  16438. (user-error "Please save the current buffer to a file"))))
  16439. x had)
  16440. (setq x (assoc ctf file-alist) had x)
  16441. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16442. (if to-end
  16443. (setq file-alist (append (delq x file-alist) (list x)))
  16444. (setq file-alist (cons x (delq x file-alist))))
  16445. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16446. (org-install-agenda-files-menu)
  16447. (message "File %s to %s of agenda file list"
  16448. (if had "moved" "added") (if to-end "end" "front"))))
  16449. (defun org-remove-file (&optional file)
  16450. "Remove current file from the list of files in variable `org-agenda-files'.
  16451. These are the files which are being checked for agenda entries.
  16452. Optional argument FILE means use this file instead of the current."
  16453. (interactive)
  16454. (let* ((org-agenda-skip-unavailable-files nil)
  16455. (file (or file buffer-file-name
  16456. (user-error "Current buffer does not visit a file")))
  16457. (true-file (file-truename file))
  16458. (afile (abbreviate-file-name file))
  16459. (files (delq nil (mapcar
  16460. (lambda (x)
  16461. (if (equal true-file
  16462. (file-truename x))
  16463. nil x))
  16464. (org-agenda-files t)))))
  16465. (if (not (= (length files) (length (org-agenda-files t))))
  16466. (progn
  16467. (org-store-new-agenda-file-list files)
  16468. (org-install-agenda-files-menu)
  16469. (message "Removed file: %s" afile))
  16470. (message "File was not in list: %s (not removed)" afile))))
  16471. (defun org-file-menu-entry (file)
  16472. (vector file (list 'find-file file) t))
  16473. (defun org-check-agenda-file (file)
  16474. "Make sure FILE exists. If not, ask user what to do."
  16475. (when (not (file-exists-p file))
  16476. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16477. (abbreviate-file-name file))
  16478. (let ((r (downcase (read-char-exclusive))))
  16479. (cond
  16480. ((equal r ?r)
  16481. (org-remove-file file)
  16482. (throw 'nextfile t))
  16483. (t (error "Abort"))))))
  16484. (defun org-get-agenda-file-buffer (file)
  16485. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16486. it to the list of buffers which might be released later."
  16487. (let ((buf (org-find-base-buffer-visiting file)))
  16488. (if buf
  16489. buf ; just return it
  16490. ;; Make a new buffer and remember it
  16491. (setq buf (find-file-noselect file))
  16492. (if buf (push buf org-agenda-new-buffers))
  16493. buf)))
  16494. (defun org-release-buffers (blist)
  16495. "Release all buffers in list, asking the user for confirmation when needed.
  16496. When a buffer is unmodified, it is just killed. When modified, it is saved
  16497. \(if the user agrees) and then killed."
  16498. (let (buf file)
  16499. (while (setq buf (pop blist))
  16500. (setq file (buffer-file-name buf))
  16501. (when (and (buffer-modified-p buf)
  16502. file
  16503. (y-or-n-p (format "Save file %s? " file)))
  16504. (with-current-buffer buf (save-buffer)))
  16505. (kill-buffer buf))))
  16506. (defun org-agenda-prepare-buffers (files)
  16507. "Create buffers for all agenda files, protect archived trees and comments."
  16508. (interactive)
  16509. (let ((pa '(:org-archived t))
  16510. (pc '(:org-comment t))
  16511. (pall '(:org-archived t :org-comment t))
  16512. (inhibit-read-only t)
  16513. (org-inhibit-startup org-agenda-inhibit-startup)
  16514. (rea (concat ":" org-archive-tag ":"))
  16515. file re)
  16516. (setq org-tag-alist-for-agenda nil
  16517. org-tag-groups-alist-for-agenda nil)
  16518. (save-excursion
  16519. (save-restriction
  16520. (while (setq file (pop files))
  16521. (catch 'nextfile
  16522. (if (bufferp file)
  16523. (set-buffer file)
  16524. (org-check-agenda-file file)
  16525. (set-buffer (org-get-agenda-file-buffer file)))
  16526. (widen)
  16527. (org-set-regexps-and-options-for-tags)
  16528. (org-refresh-category-properties)
  16529. (org-refresh-properties org-effort-property 'org-effort)
  16530. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16531. (setq org-todo-keywords-for-agenda
  16532. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16533. (setq org-done-keywords-for-agenda
  16534. (append org-done-keywords-for-agenda org-done-keywords))
  16535. (setq org-todo-keyword-alist-for-agenda
  16536. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16537. (setq org-drawers-for-agenda
  16538. (append org-drawers-for-agenda org-drawers))
  16539. (setq org-tag-alist-for-agenda
  16540. (org-uniquify
  16541. (append org-tag-alist-for-agenda
  16542. org-tag-alist
  16543. org-tag-persistent-alist)))
  16544. (if org-group-tags
  16545. (setq org-tag-groups-alist-for-agenda
  16546. (org-uniquify-alist
  16547. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16548. (org-with-silent-modifications
  16549. (save-excursion
  16550. (remove-text-properties (point-min) (point-max) pall)
  16551. (when org-agenda-skip-archived-trees
  16552. (goto-char (point-min))
  16553. (while (re-search-forward rea nil t)
  16554. (if (org-at-heading-p t)
  16555. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16556. (goto-char (point-min))
  16557. (setq re (format org-heading-keyword-regexp-format
  16558. org-comment-string))
  16559. (while (re-search-forward re nil t)
  16560. (add-text-properties
  16561. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16562. (setq org-todo-keywords-for-agenda
  16563. (org-uniquify org-todo-keywords-for-agenda))
  16564. (setq org-todo-keyword-alist-for-agenda
  16565. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16566. ;;;; CDLaTeX minor mode
  16567. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16568. "Keymap for the minor `org-cdlatex-mode'.")
  16569. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16570. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16571. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16572. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16573. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16574. (defvar org-cdlatex-texmathp-advice-is-done nil
  16575. "Flag remembering if we have applied the advice to texmathp already.")
  16576. (define-minor-mode org-cdlatex-mode
  16577. "Toggle the minor `org-cdlatex-mode'.
  16578. This mode supports entering LaTeX environment and math in LaTeX fragments
  16579. in Org-mode.
  16580. \\{org-cdlatex-mode-map}"
  16581. nil " OCDL" nil
  16582. (when org-cdlatex-mode
  16583. (require 'cdlatex)
  16584. (run-hooks 'cdlatex-mode-hook)
  16585. (cdlatex-compute-tables))
  16586. (unless org-cdlatex-texmathp-advice-is-done
  16587. (setq org-cdlatex-texmathp-advice-is-done t)
  16588. (defadvice texmathp (around org-math-always-on activate)
  16589. "Always return t in org-mode buffers.
  16590. This is because we want to insert math symbols without dollars even outside
  16591. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16592. an embedded LaTeX fragment, let texmathp do its job.
  16593. \\[org-cdlatex-mode-map]"
  16594. (interactive)
  16595. (let (p)
  16596. (cond
  16597. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16598. ((eq this-command 'cdlatex-math-symbol)
  16599. (setq ad-return-value t
  16600. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16601. (t
  16602. (let ((p (org-inside-LaTeX-fragment-p)))
  16603. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16604. (setq ad-return-value t
  16605. texmathp-why '("Org-mode embedded math" . 0))
  16606. (if p ad-do-it)))))))))
  16607. (defun turn-on-org-cdlatex ()
  16608. "Unconditionally turn on `org-cdlatex-mode'."
  16609. (org-cdlatex-mode 1))
  16610. (defun org-try-cdlatex-tab ()
  16611. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16612. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16613. - inside a LaTeX fragment, or
  16614. - after the first word in a line, where an abbreviation expansion could
  16615. insert a LaTeX environment."
  16616. (when org-cdlatex-mode
  16617. (cond
  16618. ;; Before any word on the line: No expansion possible.
  16619. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16620. ;; Just after first word on the line: Expand it. Make sure it
  16621. ;; cannot happen on headlines, though.
  16622. ((save-excursion
  16623. (skip-chars-backward "a-zA-Z0-9*")
  16624. (skip-chars-backward " \t")
  16625. (and (bolp) (not (org-at-heading-p))))
  16626. (cdlatex-tab) t)
  16627. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16628. (defun org-cdlatex-underscore-caret (&optional arg)
  16629. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16630. Revert to the normal definition outside of these fragments."
  16631. (interactive "P")
  16632. (if (org-inside-LaTeX-fragment-p)
  16633. (call-interactively 'cdlatex-sub-superscript)
  16634. (let (org-cdlatex-mode)
  16635. (call-interactively (key-binding (vector last-input-event))))))
  16636. (defun org-cdlatex-math-modify (&optional arg)
  16637. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16638. Revert to the normal definition outside of these fragments."
  16639. (interactive "P")
  16640. (if (org-inside-LaTeX-fragment-p)
  16641. (call-interactively 'cdlatex-math-modify)
  16642. (let (org-cdlatex-mode)
  16643. (call-interactively (key-binding (vector last-input-event))))))
  16644. ;;;; LaTeX fragments
  16645. (defvar org-latex-regexps
  16646. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16647. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16648. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16649. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16650. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16651. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16652. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16653. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16654. "Regular expressions for matching embedded LaTeX.")
  16655. (defun org-inside-LaTeX-fragment-p ()
  16656. "Test if point is inside a LaTeX fragment.
  16657. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16658. sequence appearing also before point.
  16659. Even though the matchers for math are configurable, this function assumes
  16660. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16661. delimiters are skipped when they have been removed by customization.
  16662. The return value is nil, or a cons cell with the delimiter and the
  16663. position of this delimiter.
  16664. This function does a reasonably good job, but can locally be fooled by
  16665. for example currency specifications. For example it will assume being in
  16666. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16667. fragments that are properly closed, but during editing, we have to live
  16668. with the uncertainty caused by missing closing delimiters. This function
  16669. looks only before point, not after."
  16670. (catch 'exit
  16671. (let ((pos (point))
  16672. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16673. (lim (progn
  16674. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16675. (point)))
  16676. dd-on str (start 0) m re)
  16677. (goto-char pos)
  16678. (when dodollar
  16679. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16680. re (nth 1 (assoc "$" org-latex-regexps)))
  16681. (while (string-match re str start)
  16682. (cond
  16683. ((= (match-end 0) (length str))
  16684. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16685. ((= (match-end 0) (- (length str) 5))
  16686. (throw 'exit nil))
  16687. (t (setq start (match-end 0))))))
  16688. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16689. (goto-char pos)
  16690. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16691. (and (match-beginning 2) (throw 'exit nil))
  16692. ;; count $$
  16693. (while (re-search-backward "\\$\\$" lim t)
  16694. (setq dd-on (not dd-on)))
  16695. (goto-char pos)
  16696. (if dd-on (cons "$$" m))))))
  16697. (defun org-inside-latex-macro-p ()
  16698. "Is point inside a LaTeX macro or its arguments?"
  16699. (save-match-data
  16700. (org-in-regexp
  16701. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16702. (defvar org-latex-fragment-image-overlays nil
  16703. "List of overlays carrying the images of latex fragments.")
  16704. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16705. (defun org-remove-latex-fragment-image-overlays ()
  16706. "Remove all overlays with LaTeX fragment images in current buffer."
  16707. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16708. (setq org-latex-fragment-image-overlays nil))
  16709. (defun org-preview-latex-fragment (&optional subtree)
  16710. "Preview the LaTeX fragment at point, or all locally or globally.
  16711. If the cursor is in a LaTeX fragment, create the image and overlay
  16712. it over the source code. If there is no fragment at point, display
  16713. all fragments in the current text, from one headline to the next. With
  16714. prefix SUBTREE, display all fragments in the current subtree. With a
  16715. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16716. the cursor is before the first headline,
  16717. display all fragments in the buffer.
  16718. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16719. (interactive "P")
  16720. (unless buffer-file-name
  16721. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16722. (when (display-graphic-p)
  16723. (org-remove-latex-fragment-image-overlays)
  16724. (save-excursion
  16725. (save-restriction
  16726. (let (beg end at msg)
  16727. (cond
  16728. ((or (equal subtree '(16))
  16729. (not (save-excursion
  16730. (re-search-backward org-outline-regexp-bol nil t))))
  16731. (setq beg (point-min) end (point-max)
  16732. msg "Creating images for buffer...%s"))
  16733. ((equal subtree '(4))
  16734. (org-back-to-heading)
  16735. (setq beg (point) end (org-end-of-subtree t)
  16736. msg "Creating images for subtree...%s"))
  16737. (t
  16738. (if (setq at (org-inside-LaTeX-fragment-p))
  16739. (goto-char (max (point-min) (- (cdr at) 2)))
  16740. (org-back-to-heading))
  16741. (setq beg (point) end (progn (outline-next-heading) (point))
  16742. msg (if at "Creating image...%s"
  16743. "Creating images for entry...%s"))))
  16744. (message msg "")
  16745. (narrow-to-region beg end)
  16746. (goto-char beg)
  16747. (org-format-latex
  16748. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16749. (file-name-nondirectory
  16750. buffer-file-name)))
  16751. default-directory 'overlays msg at 'forbuffer
  16752. org-latex-create-formula-image-program)
  16753. (message msg "done. Use `C-c C-c' to remove images."))))))
  16754. (defun org-format-latex (prefix &optional dir overlays msg at
  16755. forbuffer processing-type)
  16756. "Replace LaTeX fragments with links to an image, and produce images.
  16757. Some of the options can be changed using the variable
  16758. `org-format-latex-options'."
  16759. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16760. (let* ((prefixnodir (file-name-nondirectory prefix))
  16761. (absprefix (expand-file-name prefix dir))
  16762. (todir (file-name-directory absprefix))
  16763. (opt org-format-latex-options)
  16764. (optnew org-format-latex-options)
  16765. (matchers (plist-get opt :matchers))
  16766. (re-list org-latex-regexps)
  16767. (cnt 0) txt hash link beg end re e checkdir
  16768. string
  16769. m n block-type block linkfile movefile ov)
  16770. ;; Check the different regular expressions
  16771. (while (setq e (pop re-list))
  16772. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16773. block (if block-type "\n\n" ""))
  16774. (when (member m matchers)
  16775. (goto-char (point-min))
  16776. (while (re-search-forward re nil t)
  16777. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16778. (or (not overlays)
  16779. (not (eq (get-char-property (match-beginning n)
  16780. 'org-overlay-type)
  16781. 'org-latex-overlay))))
  16782. (cond
  16783. ((eq processing-type 'verbatim))
  16784. ((eq processing-type 'mathjax)
  16785. ;; Prepare for MathJax processing.
  16786. (setq string (match-string n))
  16787. (when (member m '("$" "$1"))
  16788. (save-excursion
  16789. (delete-region (match-beginning n) (match-end n))
  16790. (goto-char (match-beginning n))
  16791. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16792. ((or (eq processing-type 'dvipng)
  16793. (eq processing-type 'imagemagick))
  16794. ;; Process to an image.
  16795. (setq txt (match-string n)
  16796. beg (match-beginning n) end (match-end n)
  16797. cnt (1+ cnt))
  16798. (let ((face (face-at-point))
  16799. (fg (plist-get opt :foreground))
  16800. (bg (plist-get opt :background))
  16801. ;; Ensure full list is printed.
  16802. print-length print-level)
  16803. (when forbuffer
  16804. ;; Get the colors from the face at point.
  16805. (goto-char beg)
  16806. (when (eq fg 'auto)
  16807. (setq fg (face-attribute face :foreground nil 'default)))
  16808. (when (eq bg 'auto)
  16809. (setq bg (face-attribute face :background nil 'default)))
  16810. (setq optnew (copy-sequence opt))
  16811. (plist-put optnew :foreground fg)
  16812. (plist-put optnew :background bg))
  16813. (setq hash (sha1 (prin1-to-string
  16814. (list org-format-latex-header
  16815. org-latex-default-packages-alist
  16816. org-latex-packages-alist
  16817. org-format-latex-options
  16818. forbuffer txt fg bg)))
  16819. linkfile (format "%s_%s.png" prefix hash)
  16820. movefile (format "%s_%s.png" absprefix hash)))
  16821. (setq link (concat block "[[file:" linkfile "]]" block))
  16822. (if msg (message msg cnt))
  16823. (goto-char beg)
  16824. (unless checkdir ; Ensure the directory exists.
  16825. (setq checkdir t)
  16826. (or (file-directory-p todir) (make-directory todir t)))
  16827. (unless (file-exists-p movefile)
  16828. (org-create-formula-image
  16829. txt movefile optnew forbuffer processing-type))
  16830. (if overlays
  16831. (progn
  16832. (mapc (lambda (o)
  16833. (if (eq (overlay-get o 'org-overlay-type)
  16834. 'org-latex-overlay)
  16835. (delete-overlay o)))
  16836. (overlays-in beg end))
  16837. (setq ov (make-overlay beg end))
  16838. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16839. (if (featurep 'xemacs)
  16840. (progn
  16841. (overlay-put ov 'invisible t)
  16842. (overlay-put
  16843. ov 'end-glyph
  16844. (make-glyph (vector 'png :file movefile))))
  16845. (overlay-put
  16846. ov 'display
  16847. (list 'image :type 'png :file movefile :ascent 'center)))
  16848. (push ov org-latex-fragment-image-overlays)
  16849. (goto-char end))
  16850. (delete-region beg end)
  16851. (insert (org-add-props link
  16852. (list 'org-latex-src
  16853. (replace-regexp-in-string
  16854. "\"" "" txt)
  16855. 'org-latex-src-embed-type
  16856. (if block-type 'paragraph 'character))))))
  16857. ((eq processing-type 'mathml)
  16858. ;; Process to MathML
  16859. (unless (save-match-data (org-format-latex-mathml-available-p))
  16860. (user-error "LaTeX to MathML converter not configured"))
  16861. (setq txt (match-string n)
  16862. beg (match-beginning n) end (match-end n)
  16863. cnt (1+ cnt))
  16864. (if msg (message msg cnt))
  16865. (goto-char beg)
  16866. (delete-region beg end)
  16867. (insert (org-format-latex-as-mathml
  16868. txt block-type prefix dir)))
  16869. (t
  16870. (error "Unknown conversion type %s for LaTeX fragments"
  16871. processing-type)))))))))
  16872. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16873. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16874. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16875. sucessful, return the portion between \"<math...> </math>\"
  16876. elements otherwise return nil. When MATHML-FILE is specified,
  16877. write the results in to that file. When invoked as an
  16878. interactive command, prompt for LATEX-FRAG, with initial value
  16879. set to the current active region and echo the results for user
  16880. inspection."
  16881. (interactive (list (let ((frag (when (org-region-active-p)
  16882. (buffer-substring-no-properties
  16883. (region-beginning) (region-end)))))
  16884. (read-string "LaTeX Fragment: " frag nil frag))))
  16885. (unless latex-frag (error "Invalid LaTeX fragment"))
  16886. (let* ((tmp-in-file (file-relative-name
  16887. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16888. (ignore (write-region latex-frag nil tmp-in-file))
  16889. (tmp-out-file (file-relative-name
  16890. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16891. (cmd (format-spec
  16892. org-latex-to-mathml-convert-command
  16893. `((?j . ,(shell-quote-argument
  16894. (expand-file-name org-latex-to-mathml-jar-file)))
  16895. (?I . ,(shell-quote-argument tmp-in-file))
  16896. (?o . ,(shell-quote-argument tmp-out-file)))))
  16897. mathml shell-command-output)
  16898. (when (org-called-interactively-p 'any)
  16899. (unless (org-format-latex-mathml-available-p)
  16900. (user-error "LaTeX to MathML converter not configured")))
  16901. (message "Running %s" cmd)
  16902. (setq shell-command-output (shell-command-to-string cmd))
  16903. (setq mathml
  16904. (when (file-readable-p tmp-out-file)
  16905. (with-current-buffer (find-file-noselect tmp-out-file t)
  16906. (goto-char (point-min))
  16907. (when (re-search-forward
  16908. (concat
  16909. (regexp-quote
  16910. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16911. "\\(.\\|\n\\)*"
  16912. (regexp-quote "</math>")) nil t)
  16913. (prog1 (match-string 0) (kill-buffer))))))
  16914. (cond
  16915. (mathml
  16916. (setq mathml
  16917. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16918. (when mathml-file
  16919. (write-region mathml nil mathml-file))
  16920. (when (org-called-interactively-p 'any)
  16921. (message mathml)))
  16922. ((message "LaTeX to MathML conversion failed")
  16923. (message shell-command-output)))
  16924. (delete-file tmp-in-file)
  16925. (when (file-exists-p tmp-out-file)
  16926. (delete-file tmp-out-file))
  16927. mathml))
  16928. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16929. prefix &optional dir)
  16930. "Use `org-create-math-formula' but check local cache first."
  16931. (let* ((absprefix (expand-file-name prefix dir))
  16932. (print-length nil) (print-level nil)
  16933. (formula-id (concat
  16934. "formula-"
  16935. (sha1
  16936. (prin1-to-string
  16937. (list latex-frag
  16938. org-latex-to-mathml-convert-command)))))
  16939. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16940. (formula-cache-dir (file-name-directory formula-cache)))
  16941. (unless (file-directory-p formula-cache-dir)
  16942. (make-directory formula-cache-dir t))
  16943. (unless (file-exists-p formula-cache)
  16944. (org-create-math-formula latex-frag formula-cache))
  16945. (if (file-exists-p formula-cache)
  16946. ;; Successful conversion. Return the link to MathML file.
  16947. (org-add-props
  16948. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16949. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16950. 'org-latex-src-embed-type (if latex-frag-type
  16951. 'paragraph 'character)))
  16952. ;; Failed conversion. Return the LaTeX fragment verbatim
  16953. latex-frag)))
  16954. (defun org-create-formula-image (string tofile options buffer &optional type)
  16955. "Create an image from LaTeX source using dvipng or convert.
  16956. This function calls either `org-create-formula-image-with-dvipng'
  16957. or `org-create-formula-image-with-imagemagick' depending on the
  16958. value of `org-latex-create-formula-image-program' or on the value
  16959. of the optional TYPE variable.
  16960. Note: ultimately these two function should be combined as they
  16961. share a good deal of logic."
  16962. (org-check-external-command
  16963. "latex" "needed to convert LaTeX fragments to images")
  16964. (funcall
  16965. (case (or type org-latex-create-formula-image-program)
  16966. ('dvipng
  16967. (org-check-external-command
  16968. "dvipng" "needed to convert LaTeX fragments to images")
  16969. #'org-create-formula-image-with-dvipng)
  16970. ('imagemagick
  16971. (org-check-external-command
  16972. "convert" "you need to install imagemagick")
  16973. #'org-create-formula-image-with-imagemagick)
  16974. (t (error
  16975. "Invalid value of `org-latex-create-formula-image-program'")))
  16976. string tofile options buffer))
  16977. (declare-function org-export--get-global-options "ox" (&optional backend))
  16978. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16979. (defun org-create-formula--latex-header ()
  16980. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16981. (org-latex-guess-inputenc
  16982. (org-splice-latex-header
  16983. org-format-latex-header
  16984. org-latex-default-packages-alist
  16985. org-latex-packages-alist t
  16986. (plist-get
  16987. (org-combine-plists
  16988. (org-export--get-global-options 'latex)
  16989. (org-export--get-inbuffer-options 'latex))
  16990. :latex-header))))
  16991. ;; This function borrows from Ganesh Swami's latex2png.el
  16992. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16993. "This calls dvipng."
  16994. (require 'ox-latex)
  16995. (let* ((tmpdir (if (featurep 'xemacs)
  16996. (temp-directory)
  16997. temporary-file-directory))
  16998. (texfilebase (make-temp-name
  16999. (expand-file-name "orgtex" tmpdir)))
  17000. (texfile (concat texfilebase ".tex"))
  17001. (dvifile (concat texfilebase ".dvi"))
  17002. (pngfile (concat texfilebase ".png"))
  17003. (fnh (if (featurep 'xemacs)
  17004. (font-height (face-font 'default))
  17005. (face-attribute 'default :height nil)))
  17006. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17007. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17008. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17009. "Black"))
  17010. (bg (or (plist-get options (if buffer :background :html-background))
  17011. "Transparent")))
  17012. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17013. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17014. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17015. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17016. (let ((latex-header (org-create-formula--latex-header)))
  17017. (with-temp-file texfile
  17018. (insert latex-header)
  17019. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17020. (let ((dir default-directory))
  17021. (condition-case nil
  17022. (progn
  17023. (cd tmpdir)
  17024. (call-process "latex" nil nil nil texfile))
  17025. (error nil))
  17026. (cd dir))
  17027. (if (not (file-exists-p dvifile))
  17028. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17029. (condition-case nil
  17030. (if (featurep 'xemacs)
  17031. (call-process "dvipng" nil nil nil
  17032. "-fg" fg "-bg" bg
  17033. "-T" "tight"
  17034. "-o" pngfile
  17035. dvifile)
  17036. (call-process "dvipng" nil nil nil
  17037. "-fg" fg "-bg" bg
  17038. "-D" dpi
  17039. ;;"-x" scale "-y" scale
  17040. "-T" "tight"
  17041. "-o" pngfile
  17042. dvifile))
  17043. (error nil))
  17044. (if (not (file-exists-p pngfile))
  17045. (if org-format-latex-signal-error
  17046. (error "Failed to create png file from %s" texfile)
  17047. (message "Failed to create png file from %s" texfile)
  17048. nil)
  17049. ;; Use the requested file name and clean up
  17050. (copy-file pngfile tofile 'replace)
  17051. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17052. (if (file-exists-p (concat texfilebase e))
  17053. (delete-file (concat texfilebase e))))
  17054. pngfile))))
  17055. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17056. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17057. "This calls convert, which is included into imagemagick."
  17058. (require 'ox-latex)
  17059. (let* ((tmpdir (if (featurep 'xemacs)
  17060. (temp-directory)
  17061. temporary-file-directory))
  17062. (texfilebase (make-temp-name
  17063. (expand-file-name "orgtex" tmpdir)))
  17064. (texfile (concat texfilebase ".tex"))
  17065. (pdffile (concat texfilebase ".pdf"))
  17066. (pngfile (concat texfilebase ".png"))
  17067. (fnh (if (featurep 'xemacs)
  17068. (font-height (face-font 'default))
  17069. (face-attribute 'default :height nil)))
  17070. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17071. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17072. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17073. "black"))
  17074. (bg (or (plist-get options (if buffer :background :html-background))
  17075. "white")))
  17076. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17077. (setq fg (org-latex-color-format fg)))
  17078. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17079. (setq bg (org-latex-color-format
  17080. (if (string= bg "Transparent") "white" bg))))
  17081. (let ((latex-header (org-create-formula--latex-header)))
  17082. (with-temp-file texfile
  17083. (insert latex-header)
  17084. (insert "\n\\begin{document}\n"
  17085. "\\definecolor{fg}{rgb}{" fg "}\n"
  17086. "\\definecolor{bg}{rgb}{" bg "}\n"
  17087. "\n\\pagecolor{bg}\n"
  17088. "\n{\\color{fg}\n"
  17089. string
  17090. "\n}\n"
  17091. "\n\\end{document}\n")))
  17092. (org-latex-compile texfile t)
  17093. (if (not (file-exists-p pdffile))
  17094. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17095. (condition-case nil
  17096. (if (featurep 'xemacs)
  17097. (call-process "convert" nil nil nil
  17098. "-density" "96"
  17099. "-trim"
  17100. "-antialias"
  17101. pdffile
  17102. "-quality" "100"
  17103. ;; "-sharpen" "0x1.0"
  17104. pngfile)
  17105. (call-process "convert" nil nil nil
  17106. "-density" dpi
  17107. "-trim"
  17108. "-antialias"
  17109. pdffile
  17110. "-quality" "100"
  17111. ;; "-sharpen" "0x1.0"
  17112. pngfile))
  17113. (error nil))
  17114. (if (not (file-exists-p pngfile))
  17115. (if org-format-latex-signal-error
  17116. (error "Failed to create png file from %s" texfile)
  17117. (message "Failed to create png file from %s" texfile)
  17118. nil)
  17119. ;; Use the requested file name and clean up
  17120. (copy-file pngfile tofile 'replace)
  17121. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17122. (if (file-exists-p (concat texfilebase e))
  17123. (delete-file (concat texfilebase e))))
  17124. pngfile))))
  17125. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17126. "Fill a LaTeX header template TPL.
  17127. In the template, the following place holders will be recognized:
  17128. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17129. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17130. [PACKAGES] \\usepackage statements for PKG
  17131. [NO-PACKAGES] do not include PKG
  17132. [EXTRA] the string EXTRA
  17133. [NO-EXTRA] do not include EXTRA
  17134. For backward compatibility, if both the positive and the negative place
  17135. holder is missing, the positive one (without the \"NO-\") will be
  17136. assumed to be present at the end of the template.
  17137. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17138. EXTRA is a string.
  17139. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17140. (let (rpl (end ""))
  17141. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17142. (setq rpl (if (or (match-end 1) (not def-pkg))
  17143. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17144. tpl (replace-match rpl t t tpl))
  17145. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17146. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17147. (setq rpl (if (or (match-end 1) (not pkg))
  17148. "" (org-latex-packages-to-string pkg snippets-p t))
  17149. tpl (replace-match rpl t t tpl))
  17150. (if pkg (setq end
  17151. (concat end "\n"
  17152. (org-latex-packages-to-string pkg snippets-p)))))
  17153. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17154. (setq rpl (if (or (match-end 1) (not extra))
  17155. "" (concat extra "\n"))
  17156. tpl (replace-match rpl t t tpl))
  17157. (if (and extra (string-match "\\S-" extra))
  17158. (setq end (concat end "\n" extra))))
  17159. (if (string-match "\\S-" end)
  17160. (concat tpl "\n" end)
  17161. tpl)))
  17162. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17163. "Turn an alist of packages into a string with the \\usepackage macros."
  17164. (setq pkg (mapconcat (lambda(p)
  17165. (cond
  17166. ((stringp p) p)
  17167. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17168. (format "%% Package %s omitted" (cadr p)))
  17169. ((equal "" (car p))
  17170. (format "\\usepackage{%s}" (cadr p)))
  17171. (t
  17172. (format "\\usepackage[%s]{%s}"
  17173. (car p) (cadr p)))))
  17174. pkg
  17175. "\n"))
  17176. (if newline (concat pkg "\n") pkg))
  17177. (defun org-dvipng-color (attr)
  17178. "Return a RGB color specification for dvipng."
  17179. (apply 'format "rgb %s %s %s"
  17180. (mapcar 'org-normalize-color
  17181. (if (featurep 'xemacs)
  17182. (color-rgb-components
  17183. (face-property 'default
  17184. (cond ((eq attr :foreground) 'foreground)
  17185. ((eq attr :background) 'background))))
  17186. (color-values (face-attribute 'default attr nil))))))
  17187. (defun org-dvipng-color-format (color-name)
  17188. "Convert COLOR-NAME to a RGB color value for dvipng."
  17189. (apply 'format "rgb %s %s %s"
  17190. (mapcar 'org-normalize-color
  17191. (color-values color-name))))
  17192. (defun org-latex-color (attr)
  17193. "Return a RGB color for the LaTeX color package."
  17194. (apply 'format "%s,%s,%s"
  17195. (mapcar 'org-normalize-color
  17196. (if (featurep 'xemacs)
  17197. (color-rgb-components
  17198. (face-property 'default
  17199. (cond ((eq attr :foreground) 'foreground)
  17200. ((eq attr :background) 'background))))
  17201. (color-values (face-attribute 'default attr nil))))))
  17202. (defun org-latex-color-format (color-name)
  17203. "Convert COLOR-NAME to a RGB color value."
  17204. (apply 'format "%s,%s,%s"
  17205. (mapcar 'org-normalize-color
  17206. (color-values color-name))))
  17207. (defun org-normalize-color (value)
  17208. "Return string to be used as color value for an RGB component."
  17209. (format "%g" (/ value 65535.0)))
  17210. ;; Image display
  17211. (defvar org-inline-image-overlays nil)
  17212. (make-variable-buffer-local 'org-inline-image-overlays)
  17213. (defun org-toggle-inline-images (&optional include-linked)
  17214. "Toggle the display of inline images.
  17215. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17216. (interactive "P")
  17217. (if org-inline-image-overlays
  17218. (progn
  17219. (org-remove-inline-images)
  17220. (message "Inline image display turned off"))
  17221. (org-display-inline-images include-linked)
  17222. (if (and (org-called-interactively-p)
  17223. org-inline-image-overlays)
  17224. (message "%d images displayed inline"
  17225. (length org-inline-image-overlays))
  17226. (message "No images to display inline"))))
  17227. (defun org-redisplay-inline-images ()
  17228. "Refresh the display of inline images."
  17229. (interactive)
  17230. (if (not org-inline-image-overlays)
  17231. (org-toggle-inline-images)
  17232. (org-toggle-inline-images)
  17233. (org-toggle-inline-images)))
  17234. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17235. "Display inline images.
  17236. Normally only links without a description part are inlined, because this
  17237. is how it will work for export. When INCLUDE-LINKED is set, also links
  17238. with a description part will be inlined. This can be nice for a quick
  17239. look at those images, but it does not reflect what exported files will look
  17240. like.
  17241. When REFRESH is set, refresh existing images between BEG and END.
  17242. This will create new image displays only if necessary.
  17243. BEG and END default to the buffer boundaries."
  17244. (interactive "P")
  17245. (when (display-graphic-p)
  17246. (unless refresh
  17247. (org-remove-inline-images)
  17248. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17249. (save-excursion
  17250. (save-restriction
  17251. (widen)
  17252. (setq beg (or beg (point-min)) end (or end (point-max)))
  17253. (goto-char beg)
  17254. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17255. (substring (org-image-file-name-regexp) 0 -2)
  17256. "\\)\\]" (if include-linked "" "\\]")))
  17257. (case-fold-search t)
  17258. old file ov img type attrwidth width)
  17259. (while (re-search-forward re end t)
  17260. (setq old (get-char-property-and-overlay (match-beginning 1)
  17261. 'org-image-overlay)
  17262. file (expand-file-name
  17263. (concat (or (match-string 3) "") (match-string 4))))
  17264. (when (image-type-available-p 'imagemagick)
  17265. (setq attrwidth (if (or (listp org-image-actual-width)
  17266. (null org-image-actual-width))
  17267. (save-excursion
  17268. (save-match-data
  17269. (when (re-search-backward
  17270. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17271. (save-excursion
  17272. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17273. (string-to-number (match-string 1))))))
  17274. width (cond ((eq org-image-actual-width t) nil)
  17275. ((null org-image-actual-width) attrwidth)
  17276. ((numberp org-image-actual-width)
  17277. org-image-actual-width)
  17278. ((listp org-image-actual-width)
  17279. (or attrwidth (car org-image-actual-width))))
  17280. type (if width 'imagemagick)))
  17281. (when (file-exists-p file)
  17282. (if (and (car-safe old) refresh)
  17283. (image-refresh (overlay-get (cdr old) 'display))
  17284. (setq img (save-match-data (create-image file type nil :width width)))
  17285. (when img
  17286. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17287. (overlay-put ov 'display img)
  17288. (overlay-put ov 'face 'default)
  17289. (overlay-put ov 'org-image-overlay t)
  17290. (overlay-put ov 'modification-hooks
  17291. (list 'org-display-inline-remove-overlay))
  17292. (push ov org-inline-image-overlays))))))))))
  17293. (define-obsolete-function-alias
  17294. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17295. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17296. "Remove inline-display overlay if a corresponding region is modified."
  17297. (let ((inhibit-modification-hooks t))
  17298. (when (and ov after)
  17299. (delete ov org-inline-image-overlays)
  17300. (delete-overlay ov))))
  17301. (defun org-remove-inline-images ()
  17302. "Remove inline display of images."
  17303. (interactive)
  17304. (mapc 'delete-overlay org-inline-image-overlays)
  17305. (setq org-inline-image-overlays nil))
  17306. ;;;; Key bindings
  17307. ;; Outline functions from `outline-mode-prefix-map'
  17308. ;; that can be remapped in Org:
  17309. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17310. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17311. (define-key org-mode-map [remap outline-forward-same-level]
  17312. 'org-forward-heading-same-level)
  17313. (define-key org-mode-map [remap outline-backward-same-level]
  17314. 'org-backward-heading-same-level)
  17315. (define-key org-mode-map [remap show-branches]
  17316. 'org-kill-note-or-show-branches)
  17317. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17318. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17319. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17320. ;; Outline functions from `outline-mode-prefix-map' that can not
  17321. ;; be remapped in Org:
  17322. ;;
  17323. ;; - the column "key binding" shows whether the Outline function is still
  17324. ;; available in Org mode on the same key that it has been bound to in
  17325. ;; Outline mode:
  17326. ;; - "overridden": key used for a different functionality in Org mode
  17327. ;; - else: key still bound to the same Outline function in Org mode
  17328. ;;
  17329. ;; | Outline function | key binding | Org replacement |
  17330. ;; |------------------------------------+-------------+-----------------------|
  17331. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17332. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17333. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17334. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17335. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17336. ;; | `show-entry' | overridden | no replacement |
  17337. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17338. ;; | `show-branches' | `C-c C-k' | still same function |
  17339. ;; | `show-subtree' | overridden | visibility cycling |
  17340. ;; | `show-all' | overridden | no replacement |
  17341. ;; | `hide-subtree' | overridden | visibility cycling |
  17342. ;; | `hide-body' | overridden | no replacement |
  17343. ;; | `hide-entry' | overridden | visibility cycling |
  17344. ;; | `hide-leaves' | overridden | no replacement |
  17345. ;; | `hide-sublevels' | overridden | no replacement |
  17346. ;; | `hide-other' | overridden | no replacement |
  17347. ;; Make `C-c C-x' a prefix key
  17348. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17349. ;; TAB key with modifiers
  17350. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17351. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17352. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17353. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17354. ;; The following line is necessary under Suse GNU/Linux
  17355. (unless (featurep 'xemacs)
  17356. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17357. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17358. (define-key org-mode-map [backtab] 'org-shifttab)
  17359. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17360. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17361. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17362. ;; Cursor keys with modifiers
  17363. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17364. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17365. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17366. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17367. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17368. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17369. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17370. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17371. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17372. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17373. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17374. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17375. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17376. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17377. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17378. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17379. ;; Babel keys
  17380. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17381. (mapc (lambda (pair)
  17382. (define-key org-babel-map (car pair) (cdr pair)))
  17383. org-babel-key-bindings)
  17384. ;;; Extra keys for tty access.
  17385. ;; We only set them when really needed because otherwise the
  17386. ;; menus don't show the simple keys
  17387. (when (or org-use-extra-keys
  17388. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17389. (not window-system))
  17390. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17391. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17392. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17393. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17394. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17395. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17396. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17397. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17398. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17399. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17400. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17401. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17402. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17403. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17404. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17405. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17406. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17407. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17408. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17409. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17410. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17411. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17412. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17413. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17414. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17415. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17416. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17417. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17418. ;; All the other keys
  17419. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17420. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17421. (if (boundp 'narrow-map)
  17422. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17423. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17424. (if (boundp 'narrow-map)
  17425. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17426. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17427. (if (boundp 'narrow-map)
  17428. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17429. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17430. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17431. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17432. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17433. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17434. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17435. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17436. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17437. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17438. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17439. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17440. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17441. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17442. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17443. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17444. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17445. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17446. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17447. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17448. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17449. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17450. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17451. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17452. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17453. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17454. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17455. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17456. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17457. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17458. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17459. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17460. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17461. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17462. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17463. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17464. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17465. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17466. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17467. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17468. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17469. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17470. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17471. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17472. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17473. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17474. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17475. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17476. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17477. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17478. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17479. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17480. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17481. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17482. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17483. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17484. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17485. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17486. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17487. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17488. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17489. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17490. (org-defkey org-mode-map "\C-m" 'org-return)
  17491. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17492. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17493. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17494. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17495. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17496. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17497. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17498. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17499. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17500. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17501. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17502. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17503. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17504. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17505. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17506. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17507. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17508. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17509. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17510. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17511. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17512. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17513. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17514. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17515. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17516. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17517. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17518. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17519. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17520. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17521. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17522. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17523. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17524. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17525. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17526. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17527. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17528. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17529. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17530. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17531. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17532. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17533. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17534. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17535. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17536. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17537. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17538. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17539. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17540. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17541. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17542. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17543. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17544. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17545. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17546. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17547. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17548. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17549. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17550. (when (featurep 'xemacs)
  17551. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17552. (defconst org-speed-commands-default
  17553. '(
  17554. ("Outline Navigation")
  17555. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17556. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17557. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17558. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17559. ("F" . org-next-block)
  17560. ("B" . org-previous-block)
  17561. ("u" . (org-speed-move-safe 'outline-up-heading))
  17562. ("j" . org-goto)
  17563. ("g" . (org-refile t))
  17564. ("Outline Visibility")
  17565. ("c" . org-cycle)
  17566. ("C" . org-shifttab)
  17567. (" " . org-display-outline-path)
  17568. ("s" . org-narrow-to-subtree)
  17569. ("=" . org-columns)
  17570. ("Outline Structure Editing")
  17571. ("U" . org-shiftmetaup)
  17572. ("D" . org-shiftmetadown)
  17573. ("r" . org-metaright)
  17574. ("l" . org-metaleft)
  17575. ("R" . org-shiftmetaright)
  17576. ("L" . org-shiftmetaleft)
  17577. ("i" . (progn (forward-char 1) (call-interactively
  17578. 'org-insert-heading-respect-content)))
  17579. ("^" . org-sort)
  17580. ("w" . org-refile)
  17581. ("a" . org-archive-subtree-default-with-confirmation)
  17582. ("@" . org-mark-subtree)
  17583. ("#" . org-toggle-comment)
  17584. ("Clock Commands")
  17585. ("I" . org-clock-in)
  17586. ("O" . org-clock-out)
  17587. ("Meta Data Editing")
  17588. ("t" . org-todo)
  17589. ("," . (org-priority))
  17590. ("0" . (org-priority ?\ ))
  17591. ("1" . (org-priority ?A))
  17592. ("2" . (org-priority ?B))
  17593. ("3" . (org-priority ?C))
  17594. (":" . org-set-tags-command)
  17595. ("e" . org-set-effort)
  17596. ("E" . org-inc-effort)
  17597. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17598. (org-entry-put (point) "APPT_WARNTIME" m)))
  17599. ("Agenda Views etc")
  17600. ("v" . org-agenda)
  17601. ("/" . org-sparse-tree)
  17602. ("Misc")
  17603. ("o" . org-open-at-point)
  17604. ("?" . org-speed-command-help)
  17605. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17606. (">" . (org-agenda-remove-restriction-lock))
  17607. )
  17608. "The default speed commands.")
  17609. (defun org-print-speed-command (e)
  17610. (if (> (length (car e)) 1)
  17611. (progn
  17612. (princ "\n")
  17613. (princ (car e))
  17614. (princ "\n")
  17615. (princ (make-string (length (car e)) ?-))
  17616. (princ "\n"))
  17617. (princ (car e))
  17618. (princ " ")
  17619. (if (symbolp (cdr e))
  17620. (princ (symbol-name (cdr e)))
  17621. (prin1 (cdr e)))
  17622. (princ "\n")))
  17623. (defun org-speed-command-help ()
  17624. "Show the available speed commands."
  17625. (interactive)
  17626. (if (not org-use-speed-commands)
  17627. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17628. (with-output-to-temp-buffer "*Help*"
  17629. (princ "User-defined Speed commands\n===========================\n")
  17630. (mapc 'org-print-speed-command org-speed-commands-user)
  17631. (princ "\n")
  17632. (princ "Built-in Speed commands\n=======================\n")
  17633. (mapc 'org-print-speed-command org-speed-commands-default))
  17634. (with-current-buffer "*Help*"
  17635. (setq truncate-lines t))))
  17636. (defun org-speed-move-safe (cmd)
  17637. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17638. If not, return to the original position and throw an error."
  17639. (interactive)
  17640. (let ((pos (point)))
  17641. (call-interactively cmd)
  17642. (unless (and (bolp) (org-at-heading-p))
  17643. (goto-char pos)
  17644. (error "Boundary reached while executing %s" cmd))))
  17645. (defvar org-self-insert-command-undo-counter 0)
  17646. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17647. (defvar org-speed-command nil)
  17648. (define-obsolete-function-alias
  17649. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17650. (defun org-speed-command-activate (keys)
  17651. "Hook for activating single-letter speed commands.
  17652. `org-speed-commands-default' specifies a minimal command set.
  17653. Use `org-speed-commands-user' for further customization."
  17654. (when (or (and (bolp) (looking-at org-outline-regexp))
  17655. (and (functionp org-use-speed-commands)
  17656. (funcall org-use-speed-commands)))
  17657. (cdr (assoc keys (append org-speed-commands-user
  17658. org-speed-commands-default)))))
  17659. (define-obsolete-function-alias
  17660. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17661. (defun org-babel-speed-command-activate (keys)
  17662. "Hook for activating single-letter code block commands."
  17663. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17664. (cdr (assoc keys org-babel-key-bindings))))
  17665. (defcustom org-speed-command-hook
  17666. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17667. "Hook for activating speed commands at strategic locations.
  17668. Hook functions are called in sequence until a valid handler is
  17669. found.
  17670. Each hook takes a single argument, a user-pressed command key
  17671. which is also a `self-insert-command' from the global map.
  17672. Within the hook, examine the cursor position and the command key
  17673. and return nil or a valid handler as appropriate. Handler could
  17674. be one of an interactive command, a function, or a form.
  17675. Set `org-use-speed-commands' to non-nil value to enable this
  17676. hook. The default setting is `org-speed-command-activate'."
  17677. :group 'org-structure
  17678. :version "24.1"
  17679. :type 'hook)
  17680. (defun org-self-insert-command (N)
  17681. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17682. If the cursor is in a table looking at whitespace, the whitespace is
  17683. overwritten, and the table is not marked as requiring realignment."
  17684. (interactive "p")
  17685. (org-check-before-invisible-edit 'insert)
  17686. (cond
  17687. ((and org-use-speed-commands
  17688. (setq org-speed-command
  17689. (run-hook-with-args-until-success
  17690. 'org-speed-command-hook (this-command-keys))))
  17691. (cond
  17692. ((commandp org-speed-command)
  17693. (setq this-command org-speed-command)
  17694. (call-interactively org-speed-command))
  17695. ((functionp org-speed-command)
  17696. (funcall org-speed-command))
  17697. ((and org-speed-command (listp org-speed-command))
  17698. (eval org-speed-command))
  17699. (t (let (org-use-speed-commands)
  17700. (call-interactively 'org-self-insert-command)))))
  17701. ((and
  17702. (org-table-p)
  17703. (progn
  17704. ;; check if we blank the field, and if that triggers align
  17705. (and (featurep 'org-table) org-table-auto-blank-field
  17706. (member last-command
  17707. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17708. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17709. ;; got extra space, this field does not determine column width
  17710. (let (org-table-may-need-update) (org-table-blank-field))
  17711. ;; no extra space, this field may determine column width
  17712. (org-table-blank-field)))
  17713. t)
  17714. (eq N 1)
  17715. (looking-at "[^|\n]* |"))
  17716. (let (org-table-may-need-update)
  17717. (goto-char (1- (match-end 0)))
  17718. (backward-delete-char 1)
  17719. (goto-char (match-beginning 0))
  17720. (self-insert-command N)))
  17721. (t
  17722. (setq org-table-may-need-update t)
  17723. (self-insert-command N)
  17724. (org-fix-tags-on-the-fly)
  17725. (if org-self-insert-cluster-for-undo
  17726. (if (not (eq last-command 'org-self-insert-command))
  17727. (setq org-self-insert-command-undo-counter 1)
  17728. (if (>= org-self-insert-command-undo-counter 20)
  17729. (setq org-self-insert-command-undo-counter 1)
  17730. (and (> org-self-insert-command-undo-counter 0)
  17731. buffer-undo-list (listp buffer-undo-list)
  17732. (not (cadr buffer-undo-list)) ; remove nil entry
  17733. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17734. (setq org-self-insert-command-undo-counter
  17735. (1+ org-self-insert-command-undo-counter))))))))
  17736. (defun org-check-before-invisible-edit (kind)
  17737. "Check is editing if kind KIND would be dangerous with invisible text around.
  17738. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17739. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17740. (if (and org-catch-invisible-edits
  17741. (or (not (boundp 'visible-mode)) (not visible-mode))
  17742. (or (get-char-property (point) 'invisible)
  17743. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17744. ;; OK, we need to take a closer look
  17745. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17746. (invisible-before-point (if (bobp) nil (get-char-property
  17747. (1- (point)) 'invisible)))
  17748. (border-and-ok-direction
  17749. (or
  17750. ;; Check if we are acting predictably before invisible text
  17751. (and invisible-at-point (not invisible-before-point)
  17752. (memq kind '(insert delete-backward)))
  17753. ;; Check if we are acting predictably after invisible text
  17754. ;; This works not well, and I have turned it off. It seems
  17755. ;; better to always show and stop after invisible text.
  17756. ;; (and (not invisible-at-point) invisible-before-point
  17757. ;; (memq kind '(insert delete)))
  17758. )))
  17759. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17760. (memq invisible-before-point '(outline org-hide-block t)))
  17761. (if (eq org-catch-invisible-edits 'error)
  17762. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17763. (if (and org-custom-properties-overlays
  17764. (y-or-n-p "Display invisible properties in this buffer? "))
  17765. (org-toggle-custom-properties-visibility)
  17766. ;; Make the area visible
  17767. (save-excursion
  17768. (if invisible-before-point
  17769. (goto-char (previous-single-char-property-change
  17770. (point) 'invisible)))
  17771. (org-cycle))
  17772. (cond
  17773. ((eq org-catch-invisible-edits 'show)
  17774. ;; That's it, we do the edit after showing
  17775. (message
  17776. "Unfolding invisible region around point before editing")
  17777. (sit-for 1))
  17778. ((and (eq org-catch-invisible-edits 'smart)
  17779. border-and-ok-direction)
  17780. (message "Unfolding invisible region around point before editing"))
  17781. (t
  17782. ;; Don't do the edit, make the user repeat it in full visibility
  17783. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17784. (defun org-fix-tags-on-the-fly ()
  17785. (when (and (equal (char-after (point-at-bol)) ?*)
  17786. (org-at-heading-p))
  17787. (org-align-tags-here org-tags-column)))
  17788. (defun org-delete-backward-char (N)
  17789. "Like `delete-backward-char', insert whitespace at field end in tables.
  17790. When deleting backwards, in tables this function will insert whitespace in
  17791. front of the next \"|\" separator, to keep the table aligned. The table will
  17792. still be marked for re-alignment if the field did fill the entire column,
  17793. because, in this case the deletion might narrow the column."
  17794. (interactive "p")
  17795. (save-match-data
  17796. (org-check-before-invisible-edit 'delete-backward)
  17797. (if (and (org-table-p)
  17798. (eq N 1)
  17799. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17800. (looking-at ".*?|"))
  17801. (let ((pos (point))
  17802. (noalign (looking-at "[^|\n\r]* |"))
  17803. (c org-table-may-need-update))
  17804. (backward-delete-char N)
  17805. (if (not overwrite-mode)
  17806. (progn
  17807. (skip-chars-forward "^|")
  17808. (insert " ")
  17809. (goto-char (1- pos))))
  17810. ;; noalign: if there were two spaces at the end, this field
  17811. ;; does not determine the width of the column.
  17812. (if noalign (setq org-table-may-need-update c)))
  17813. (backward-delete-char N)
  17814. (org-fix-tags-on-the-fly))))
  17815. (defun org-delete-char (N)
  17816. "Like `delete-char', but insert whitespace at field end in tables.
  17817. When deleting characters, in tables this function will insert whitespace in
  17818. front of the next \"|\" separator, to keep the table aligned. The table will
  17819. still be marked for re-alignment if the field did fill the entire column,
  17820. because, in this case the deletion might narrow the column."
  17821. (interactive "p")
  17822. (save-match-data
  17823. (org-check-before-invisible-edit 'delete)
  17824. (if (and (org-table-p)
  17825. (not (bolp))
  17826. (not (= (char-after) ?|))
  17827. (eq N 1))
  17828. (if (looking-at ".*?|")
  17829. (let ((pos (point))
  17830. (noalign (looking-at "[^|\n\r]* |"))
  17831. (c org-table-may-need-update))
  17832. (replace-match
  17833. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17834. (goto-char pos)
  17835. ;; noalign: if there were two spaces at the end, this field
  17836. ;; does not determine the width of the column.
  17837. (if noalign (setq org-table-may-need-update c)))
  17838. (delete-char N))
  17839. (delete-char N)
  17840. (org-fix-tags-on-the-fly))))
  17841. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17842. (put 'org-self-insert-command 'delete-selection t)
  17843. (put 'orgtbl-self-insert-command 'delete-selection t)
  17844. (put 'org-delete-char 'delete-selection 'supersede)
  17845. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17846. (put 'org-yank 'delete-selection 'yank)
  17847. ;; Make `flyspell-mode' delay after some commands
  17848. (put 'org-self-insert-command 'flyspell-delayed t)
  17849. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17850. (put 'org-delete-char 'flyspell-delayed t)
  17851. (put 'org-delete-backward-char 'flyspell-delayed t)
  17852. ;; Make pabbrev-mode expand after org-mode commands
  17853. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17854. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17855. ;; How to do this: Measure non-white length of current string
  17856. ;; If equal to column width, we should realign.
  17857. (defun org-remap (map &rest commands)
  17858. "In MAP, remap the functions given in COMMANDS.
  17859. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17860. (let (new old)
  17861. (while commands
  17862. (setq old (pop commands) new (pop commands))
  17863. (if (fboundp 'command-remapping)
  17864. (org-defkey map (vector 'remap old) new)
  17865. (substitute-key-definition old new map global-map)))))
  17866. (defun org-transpose-words ()
  17867. "Transpose words for Org.
  17868. This uses the `org-mode-transpose-word-syntax-table' syntax
  17869. table, which interprets characters in `org-emphasis-alist' as
  17870. word constituants."
  17871. (interactive)
  17872. (with-syntax-table org-mode-transpose-word-syntax-table
  17873. (call-interactively 'transpose-words)))
  17874. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17875. (when (eq org-enable-table-editor 'optimized)
  17876. ;; If the user wants maximum table support, we need to hijack
  17877. ;; some standard editing functions
  17878. (org-remap org-mode-map
  17879. 'self-insert-command 'org-self-insert-command
  17880. 'delete-char 'org-delete-char
  17881. 'delete-backward-char 'org-delete-backward-char)
  17882. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17883. (defvar org-ctrl-c-ctrl-c-hook nil
  17884. "Hook for functions attaching themselves to `C-c C-c'.
  17885. This can be used to add additional functionality to the C-c C-c
  17886. key which executes context-dependent commands. This hook is run
  17887. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17888. run after the last test.
  17889. Each function will be called with no arguments. The function
  17890. must check if the context is appropriate for it to act. If yes,
  17891. it should do its thing and then return a non-nil value. If the
  17892. context is wrong, just do nothing and return nil.")
  17893. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17894. "Hook for functions attaching themselves to `C-c C-c'.
  17895. This can be used to add additional functionality to the C-c C-c
  17896. key which executes context-dependent commands. This hook is run
  17897. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17898. before the first test.
  17899. Each function will be called with no arguments. The function
  17900. must check if the context is appropriate for it to act. If yes,
  17901. it should do its thing and then return a non-nil value. If the
  17902. context is wrong, just do nothing and return nil.")
  17903. (defvar org-tab-first-hook nil
  17904. "Hook for functions to attach themselves to TAB.
  17905. See `org-ctrl-c-ctrl-c-hook' for more information.
  17906. This hook runs as the first action when TAB is pressed, even before
  17907. `org-cycle' messes around with the `outline-regexp' to cater for
  17908. inline tasks and plain list item folding.
  17909. If any function in this hook returns t, any other actions that
  17910. would have been caused by TAB (such as table field motion or visibility
  17911. cycling) will not occur.")
  17912. (defvar org-tab-after-check-for-table-hook nil
  17913. "Hook for functions to attach themselves to TAB.
  17914. See `org-ctrl-c-ctrl-c-hook' for more information.
  17915. This hook runs after it has been established that the cursor is not in a
  17916. table, but before checking if the cursor is in a headline or if global cycling
  17917. should be done.
  17918. If any function in this hook returns t, not other actions like visibility
  17919. cycling will be done.")
  17920. (defvar org-tab-after-check-for-cycling-hook nil
  17921. "Hook for functions to attach themselves to TAB.
  17922. See `org-ctrl-c-ctrl-c-hook' for more information.
  17923. This hook runs after it has been established that not table field motion and
  17924. not visibility should be done because of current context. This is probably
  17925. the place where a package like yasnippets can hook in.")
  17926. (defvar org-tab-before-tab-emulation-hook nil
  17927. "Hook for functions to attach themselves to TAB.
  17928. See `org-ctrl-c-ctrl-c-hook' for more information.
  17929. This hook runs after every other options for TAB have been exhausted, but
  17930. before indentation and \t insertion takes place.")
  17931. (defvar org-metaleft-hook nil
  17932. "Hook for functions attaching themselves to `M-left'.
  17933. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17934. (defvar org-metaright-hook nil
  17935. "Hook for functions attaching themselves to `M-right'.
  17936. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17937. (defvar org-metaup-hook nil
  17938. "Hook for functions attaching themselves to `M-up'.
  17939. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17940. (defvar org-metadown-hook nil
  17941. "Hook for functions attaching themselves to `M-down'.
  17942. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17943. (defvar org-shiftmetaleft-hook nil
  17944. "Hook for functions attaching themselves to `M-S-left'.
  17945. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17946. (defvar org-shiftmetaright-hook nil
  17947. "Hook for functions attaching themselves to `M-S-right'.
  17948. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17949. (defvar org-shiftmetaup-hook nil
  17950. "Hook for functions attaching themselves to `M-S-up'.
  17951. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17952. (defvar org-shiftmetadown-hook nil
  17953. "Hook for functions attaching themselves to `M-S-down'.
  17954. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17955. (defvar org-metareturn-hook nil
  17956. "Hook for functions attaching themselves to `M-RET'.
  17957. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17958. (defvar org-shiftup-hook nil
  17959. "Hook for functions attaching themselves to `S-up'.
  17960. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17961. (defvar org-shiftup-final-hook nil
  17962. "Hook for functions attaching themselves to `S-up'.
  17963. This one runs after all other options except shift-select have been excluded.
  17964. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17965. (defvar org-shiftdown-hook nil
  17966. "Hook for functions attaching themselves to `S-down'.
  17967. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17968. (defvar org-shiftdown-final-hook nil
  17969. "Hook for functions attaching themselves to `S-down'.
  17970. This one runs after all other options except shift-select have been excluded.
  17971. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17972. (defvar org-shiftleft-hook nil
  17973. "Hook for functions attaching themselves to `S-left'.
  17974. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17975. (defvar org-shiftleft-final-hook nil
  17976. "Hook for functions attaching themselves to `S-left'.
  17977. This one runs after all other options except shift-select have been excluded.
  17978. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17979. (defvar org-shiftright-hook nil
  17980. "Hook for functions attaching themselves to `S-right'.
  17981. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17982. (defvar org-shiftright-final-hook nil
  17983. "Hook for functions attaching themselves to `S-right'.
  17984. This one runs after all other options except shift-select have been excluded.
  17985. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17986. (defun org-modifier-cursor-error ()
  17987. "Throw an error, a modified cursor command was applied in wrong context."
  17988. (user-error "This command is active in special context like tables, headlines or items"))
  17989. (defun org-shiftselect-error ()
  17990. "Throw an error because Shift-Cursor command was applied in wrong context."
  17991. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17992. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17993. (user-error "This command works only in special context like headlines or timestamps")))
  17994. (defun org-call-for-shift-select (cmd)
  17995. (let ((this-command-keys-shift-translated t))
  17996. (call-interactively cmd)))
  17997. (defun org-shifttab (&optional arg)
  17998. "Global visibility cycling or move to previous table field.
  17999. Call `org-table-previous-field' within a table.
  18000. When ARG is nil, cycle globally through visibility states.
  18001. When ARG is a numeric prefix, show contents of this level."
  18002. (interactive "P")
  18003. (cond
  18004. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18005. ((integerp arg)
  18006. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18007. (message "Content view to level: %d" arg)
  18008. (org-content (prefix-numeric-value arg2))
  18009. (org-cycle-show-empty-lines t)
  18010. (setq org-cycle-global-status 'overview)))
  18011. (t (call-interactively 'org-global-cycle))))
  18012. (defun org-shiftmetaleft ()
  18013. "Promote subtree or delete table column.
  18014. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18015. `org-table-delete-column', depending on context. See the
  18016. individual commands for more information."
  18017. (interactive)
  18018. (cond
  18019. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18020. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18021. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18022. ((if (not (org-region-active-p)) (org-at-item-p)
  18023. (save-excursion (goto-char (region-beginning))
  18024. (org-at-item-p)))
  18025. (call-interactively 'org-outdent-item-tree))
  18026. (t (org-modifier-cursor-error))))
  18027. (defun org-shiftmetaright ()
  18028. "Demote subtree or insert table column.
  18029. Calls `org-demote-subtree', `org-indent-item-tree', or
  18030. `org-table-insert-column', depending on context. See the
  18031. individual commands for more information."
  18032. (interactive)
  18033. (cond
  18034. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18035. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18036. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18037. ((if (not (org-region-active-p)) (org-at-item-p)
  18038. (save-excursion (goto-char (region-beginning))
  18039. (org-at-item-p)))
  18040. (call-interactively 'org-indent-item-tree))
  18041. (t (org-modifier-cursor-error))))
  18042. (defun org-shiftmetaup (&optional arg)
  18043. "Move subtree up or kill table row.
  18044. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18045. `org-move-item-up' or `org-timestamp-up', depending on context.
  18046. See the individual commands for more information."
  18047. (interactive "P")
  18048. (cond
  18049. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18050. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18051. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18052. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18053. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18054. (call-interactively 'org-timestamp-up)))
  18055. (t (call-interactively 'org-drag-line-backward))))
  18056. (defun org-shiftmetadown (&optional arg)
  18057. "Move subtree down or insert table row.
  18058. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18059. `org-move-item-down' or `org-timestamp-up', depending on context.
  18060. See the individual commands for more information."
  18061. (interactive "P")
  18062. (cond
  18063. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18064. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18065. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18066. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18067. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18068. (call-interactively 'org-timestamp-down)))
  18069. (t (call-interactively 'org-drag-line-forward))))
  18070. (defsubst org-hidden-tree-error ()
  18071. (user-error
  18072. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18073. (defun org-metaleft (&optional arg)
  18074. "Promote heading or move table column to left.
  18075. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18076. With no specific context, calls the Emacs default `backward-word'.
  18077. See the individual commands for more information."
  18078. (interactive "P")
  18079. (cond
  18080. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18081. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18082. ((org-with-limited-levels
  18083. (or (org-at-heading-p)
  18084. (and (org-region-active-p)
  18085. (save-excursion
  18086. (goto-char (region-beginning))
  18087. (org-at-heading-p)))))
  18088. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18089. (call-interactively 'org-do-promote))
  18090. ;; At an inline task.
  18091. ((org-at-heading-p)
  18092. (call-interactively 'org-inlinetask-promote))
  18093. ((or (org-at-item-p)
  18094. (and (org-region-active-p)
  18095. (save-excursion
  18096. (goto-char (region-beginning))
  18097. (org-at-item-p))))
  18098. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18099. (call-interactively 'org-outdent-item))
  18100. (t (call-interactively 'backward-word))))
  18101. (defun org-metaright (&optional arg)
  18102. "Demote a subtree, a list item or move table column to right.
  18103. In front of a drawer or a block keyword, indent it correctly.
  18104. With no specific context, calls the Emacs default `forward-word'.
  18105. See the individual commands for more information."
  18106. (interactive "P")
  18107. (cond
  18108. ((run-hook-with-args-until-success 'org-metaright-hook))
  18109. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18110. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18111. ((org-at-block-p) (call-interactively 'org-indent-block))
  18112. ((org-with-limited-levels
  18113. (or (org-at-heading-p)
  18114. (and (org-region-active-p)
  18115. (save-excursion
  18116. (goto-char (region-beginning))
  18117. (org-at-heading-p)))))
  18118. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18119. (call-interactively 'org-do-demote))
  18120. ;; At an inline task.
  18121. ((org-at-heading-p)
  18122. (call-interactively 'org-inlinetask-demote))
  18123. ((or (org-at-item-p)
  18124. (and (org-region-active-p)
  18125. (save-excursion
  18126. (goto-char (region-beginning))
  18127. (org-at-item-p))))
  18128. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18129. (call-interactively 'org-indent-item))
  18130. (t (call-interactively 'forward-word))))
  18131. (defun org-check-for-hidden (what)
  18132. "Check if there are hidden headlines/items in the current visual line.
  18133. WHAT can be either `headlines' or `items'. If the current line is
  18134. an outline or item heading and it has a folded subtree below it,
  18135. this function returns t, nil otherwise."
  18136. (let ((re (cond
  18137. ((eq what 'headlines) org-outline-regexp-bol)
  18138. ((eq what 'items) (org-item-beginning-re))
  18139. (t (error "This should not happen"))))
  18140. beg end)
  18141. (save-excursion
  18142. (catch 'exit
  18143. (unless (org-region-active-p)
  18144. (setq beg (point-at-bol))
  18145. (beginning-of-line 2)
  18146. (while (and (not (eobp)) ;; this is like `next-line'
  18147. (get-char-property (1- (point)) 'invisible))
  18148. (beginning-of-line 2))
  18149. (setq end (point))
  18150. (goto-char beg)
  18151. (goto-char (point-at-eol))
  18152. (setq end (max end (point)))
  18153. (while (re-search-forward re end t)
  18154. (if (get-char-property (match-beginning 0) 'invisible)
  18155. (throw 'exit t))))
  18156. nil))))
  18157. (defun org-metaup (&optional arg)
  18158. "Move subtree up or move table row up.
  18159. Calls `org-move-subtree-up' or `org-table-move-row' or
  18160. `org-move-item-up', depending on context. See the individual commands
  18161. for more information."
  18162. (interactive "P")
  18163. (cond
  18164. ((run-hook-with-args-until-success 'org-metaup-hook))
  18165. ((org-region-active-p)
  18166. (let* ((a (min (region-beginning) (region-end)))
  18167. (b (1- (max (region-beginning) (region-end))))
  18168. (c (save-excursion (goto-char a)
  18169. (move-beginning-of-line 0)))
  18170. (d (save-excursion (goto-char a)
  18171. (move-end-of-line 0) (point))))
  18172. (transpose-regions a b c d)
  18173. (goto-char c)))
  18174. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18175. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18176. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18177. (t (org-drag-element-backward))))
  18178. (defun org-metadown (&optional arg)
  18179. "Move subtree down or move table row down.
  18180. Calls `org-move-subtree-down' or `org-table-move-row' or
  18181. `org-move-item-down', depending on context. See the individual
  18182. commands for more information."
  18183. (interactive "P")
  18184. (cond
  18185. ((run-hook-with-args-until-success 'org-metadown-hook))
  18186. ((org-region-active-p)
  18187. (let* ((a (min (region-beginning) (region-end)))
  18188. (b (max (region-beginning) (region-end)))
  18189. (c (save-excursion (goto-char b)
  18190. (move-beginning-of-line 1)))
  18191. (d (save-excursion (goto-char b)
  18192. (move-end-of-line 1) (1+ (point)))))
  18193. (transpose-regions a b c d)
  18194. (goto-char d)))
  18195. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18196. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18197. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18198. (t (org-drag-element-forward))))
  18199. (defun org-shiftup (&optional arg)
  18200. "Increase item in timestamp or increase priority of current headline.
  18201. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18202. depending on context. See the individual commands for more information."
  18203. (interactive "P")
  18204. (cond
  18205. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18206. ((and org-support-shift-select (org-region-active-p))
  18207. (org-call-for-shift-select 'previous-line))
  18208. ((org-at-timestamp-p t)
  18209. (call-interactively (if org-edit-timestamp-down-means-later
  18210. 'org-timestamp-down 'org-timestamp-up)))
  18211. ((and (not (eq org-support-shift-select 'always))
  18212. org-enable-priority-commands
  18213. (org-at-heading-p))
  18214. (call-interactively 'org-priority-up))
  18215. ((and (not org-support-shift-select) (org-at-item-p))
  18216. (call-interactively 'org-previous-item))
  18217. ((org-clocktable-try-shift 'up arg))
  18218. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18219. (org-support-shift-select
  18220. (org-call-for-shift-select 'previous-line))
  18221. (t (org-shiftselect-error))))
  18222. (defun org-shiftdown (&optional arg)
  18223. "Decrease item in timestamp or decrease priority of current headline.
  18224. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18225. depending on context. See the individual commands for more information."
  18226. (interactive "P")
  18227. (cond
  18228. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18229. ((and org-support-shift-select (org-region-active-p))
  18230. (org-call-for-shift-select 'next-line))
  18231. ((org-at-timestamp-p t)
  18232. (call-interactively (if org-edit-timestamp-down-means-later
  18233. 'org-timestamp-up 'org-timestamp-down)))
  18234. ((and (not (eq org-support-shift-select 'always))
  18235. org-enable-priority-commands
  18236. (org-at-heading-p))
  18237. (call-interactively 'org-priority-down))
  18238. ((and (not org-support-shift-select) (org-at-item-p))
  18239. (call-interactively 'org-next-item))
  18240. ((org-clocktable-try-shift 'down arg))
  18241. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18242. (org-support-shift-select
  18243. (org-call-for-shift-select 'next-line))
  18244. (t (org-shiftselect-error))))
  18245. (defun org-shiftright (&optional arg)
  18246. "Cycle the thing at point or in the current line, depending on context.
  18247. Depending on context, this does one of the following:
  18248. - switch a timestamp at point one day into the future
  18249. - on a headline, switch to the next TODO keyword.
  18250. - on an item, switch entire list to the next bullet type
  18251. - on a property line, switch to the next allowed value
  18252. - on a clocktable definition line, move time block into the future"
  18253. (interactive "P")
  18254. (cond
  18255. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18256. ((and org-support-shift-select (org-region-active-p))
  18257. (org-call-for-shift-select 'forward-char))
  18258. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18259. ((and (not (eq org-support-shift-select 'always))
  18260. (org-at-heading-p))
  18261. (let ((org-inhibit-logging
  18262. (not org-treat-S-cursor-todo-selection-as-state-change))
  18263. (org-inhibit-blocking
  18264. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18265. (org-call-with-arg 'org-todo 'right)))
  18266. ((or (and org-support-shift-select
  18267. (not (eq org-support-shift-select 'always))
  18268. (org-at-item-bullet-p))
  18269. (and (not org-support-shift-select) (org-at-item-p)))
  18270. (org-call-with-arg 'org-cycle-list-bullet nil))
  18271. ((and (not (eq org-support-shift-select 'always))
  18272. (org-at-property-p))
  18273. (call-interactively 'org-property-next-allowed-value))
  18274. ((org-clocktable-try-shift 'right arg))
  18275. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18276. (org-support-shift-select
  18277. (org-call-for-shift-select 'forward-char))
  18278. (t (org-shiftselect-error))))
  18279. (defun org-shiftleft (&optional arg)
  18280. "Cycle the thing at point or in the current line, depending on context.
  18281. Depending on context, this does one of the following:
  18282. - switch a timestamp at point one day into the past
  18283. - on a headline, switch to the previous TODO keyword.
  18284. - on an item, switch entire list to the previous bullet type
  18285. - on a property line, switch to the previous allowed value
  18286. - on a clocktable definition line, move time block into the past"
  18287. (interactive "P")
  18288. (cond
  18289. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18290. ((and org-support-shift-select (org-region-active-p))
  18291. (org-call-for-shift-select 'backward-char))
  18292. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18293. ((and (not (eq org-support-shift-select 'always))
  18294. (org-at-heading-p))
  18295. (let ((org-inhibit-logging
  18296. (not org-treat-S-cursor-todo-selection-as-state-change))
  18297. (org-inhibit-blocking
  18298. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18299. (org-call-with-arg 'org-todo 'left)))
  18300. ((or (and org-support-shift-select
  18301. (not (eq org-support-shift-select 'always))
  18302. (org-at-item-bullet-p))
  18303. (and (not org-support-shift-select) (org-at-item-p)))
  18304. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18305. ((and (not (eq org-support-shift-select 'always))
  18306. (org-at-property-p))
  18307. (call-interactively 'org-property-previous-allowed-value))
  18308. ((org-clocktable-try-shift 'left arg))
  18309. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18310. (org-support-shift-select
  18311. (org-call-for-shift-select 'backward-char))
  18312. (t (org-shiftselect-error))))
  18313. (defun org-shiftcontrolright ()
  18314. "Switch to next TODO set."
  18315. (interactive)
  18316. (cond
  18317. ((and org-support-shift-select (org-region-active-p))
  18318. (org-call-for-shift-select 'forward-word))
  18319. ((and (not (eq org-support-shift-select 'always))
  18320. (org-at-heading-p))
  18321. (org-call-with-arg 'org-todo 'nextset))
  18322. (org-support-shift-select
  18323. (org-call-for-shift-select 'forward-word))
  18324. (t (org-shiftselect-error))))
  18325. (defun org-shiftcontrolleft ()
  18326. "Switch to previous TODO set."
  18327. (interactive)
  18328. (cond
  18329. ((and org-support-shift-select (org-region-active-p))
  18330. (org-call-for-shift-select 'backward-word))
  18331. ((and (not (eq org-support-shift-select 'always))
  18332. (org-at-heading-p))
  18333. (org-call-with-arg 'org-todo 'previousset))
  18334. (org-support-shift-select
  18335. (org-call-for-shift-select 'backward-word))
  18336. (t (org-shiftselect-error))))
  18337. (defun org-shiftcontrolup (&optional n)
  18338. "Change timestamps synchronously up in CLOCK log lines.
  18339. Optional argument N tells to change by that many units."
  18340. (interactive "P")
  18341. (cond ((and (not org-support-shift-select)
  18342. (org-at-clock-log-p)
  18343. (org-at-timestamp-p t))
  18344. (org-clock-timestamps-up n))
  18345. (t (org-shiftselect-error))))
  18346. (defun org-shiftcontroldown (&optional n)
  18347. "Change timestamps synchronously down in CLOCK log lines.
  18348. Optional argument N tells to change by that many units."
  18349. (interactive "P")
  18350. (cond ((and (not org-support-shift-select)
  18351. (org-at-clock-log-p)
  18352. (org-at-timestamp-p t))
  18353. (org-clock-timestamps-down n))
  18354. (t (org-shiftselect-error))))
  18355. (defun org-ctrl-c-ret ()
  18356. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18357. (interactive)
  18358. (cond
  18359. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18360. (t (call-interactively 'org-insert-heading))))
  18361. (defun org-find-visible ()
  18362. (let ((s (point)))
  18363. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18364. (get-char-property s 'invisible)))
  18365. s))
  18366. (defun org-find-invisible ()
  18367. (let ((s (point)))
  18368. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18369. (not (get-char-property s 'invisible))))
  18370. s))
  18371. (defun org-copy-visible (beg end)
  18372. "Copy the visible parts of the region."
  18373. (interactive "r")
  18374. (let (snippets s)
  18375. (save-excursion
  18376. (save-restriction
  18377. (narrow-to-region beg end)
  18378. (setq s (goto-char (point-min)))
  18379. (while (not (= (point) (point-max)))
  18380. (goto-char (org-find-invisible))
  18381. (push (buffer-substring s (point)) snippets)
  18382. (setq s (goto-char (org-find-visible))))))
  18383. (kill-new (apply 'concat (nreverse snippets)))))
  18384. (defun org-copy-special ()
  18385. "Copy region in table or copy current subtree.
  18386. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18387. See the individual commands for more information."
  18388. (interactive)
  18389. (call-interactively
  18390. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18391. (defun org-cut-special ()
  18392. "Cut region in table or cut current subtree.
  18393. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18394. See the individual commands for more information."
  18395. (interactive)
  18396. (call-interactively
  18397. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18398. (defun org-paste-special (arg)
  18399. "Paste rectangular region into table, or past subtree relative to level.
  18400. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18401. See the individual commands for more information."
  18402. (interactive "P")
  18403. (if (org-at-table-p)
  18404. (org-table-paste-rectangle)
  18405. (org-paste-subtree arg)))
  18406. (defsubst org-in-fixed-width-region-p ()
  18407. "Is point in a fixed-width region?"
  18408. (save-match-data
  18409. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18410. (defun org-edit-special (&optional arg)
  18411. "Call a special editor for the element at point.
  18412. When at a table, call the formula editor with `org-table-edit-formulas'.
  18413. When in a source code block, call `org-edit-src-code'.
  18414. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18415. When at an #+INCLUDE keyword, visit the included file.
  18416. On a link, call `ffap' to visit the link at point.
  18417. Otherwise, return a user error."
  18418. (interactive "P")
  18419. (let ((element (org-element-at-point)))
  18420. (assert (not buffer-read-only) nil
  18421. "Buffer is read-only: %s" (buffer-name))
  18422. (case (org-element-type element)
  18423. (src-block
  18424. (if (not arg) (org-edit-src-code)
  18425. (let* ((info (org-babel-get-src-block-info))
  18426. (lang (nth 0 info))
  18427. (params (nth 2 info))
  18428. (session (cdr (assq :session params))))
  18429. (if (not session) (org-edit-src-code)
  18430. ;; At a src-block with a session and function called with
  18431. ;; an ARG: switch to the buffer related to the inferior
  18432. ;; process.
  18433. (switch-to-buffer
  18434. (funcall (intern (concat "org-babel-prep-session:" lang))
  18435. session params))))))
  18436. (keyword
  18437. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18438. (find-file
  18439. (org-remove-double-quotes
  18440. (car (org-split-string (org-element-property :value element)))))
  18441. (user-error "No special environment to edit here")))
  18442. (table
  18443. (if (eq (org-element-property :type element) 'table.el)
  18444. (org-edit-src-code)
  18445. (call-interactively 'org-table-edit-formulas)))
  18446. ;; Only Org tables contain `table-row' type elements.
  18447. (table-row (call-interactively 'org-table-edit-formulas))
  18448. ((example-block export-block) (org-edit-src-code))
  18449. (fixed-width (org-edit-fixed-width-region))
  18450. (otherwise
  18451. ;; No notable element at point. Though, we may be at a link,
  18452. ;; which is an object. Thus, scan deeper.
  18453. (if (eq (org-element-type (org-element-context element)) 'link)
  18454. (call-interactively 'ffap)
  18455. (user-error "No special environment to edit here"))))))
  18456. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18457. (defun org-ctrl-c-ctrl-c (&optional arg)
  18458. "Set tags in headline, or update according to changed information at point.
  18459. This command does many different things, depending on context:
  18460. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18461. this is what we do.
  18462. - If the cursor is on a statistics cookie, update it.
  18463. - If the cursor is in a headline, prompt for tags and insert them
  18464. into the current line, aligned to `org-tags-column'. When called
  18465. with prefix arg, realign all tags in the current buffer.
  18466. - If the cursor is in one of the special #+KEYWORD lines, this
  18467. triggers scanning the buffer for these lines and updating the
  18468. information.
  18469. - If the cursor is inside a table, realign the table. This command
  18470. works even if the automatic table editor has been turned off.
  18471. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18472. the entire table.
  18473. - If the cursor is at a footnote reference or definition, jump to
  18474. the corresponding definition or references, respectively.
  18475. - If the cursor is a the beginning of a dynamic block, update it.
  18476. - If the current buffer is a capture buffer, close note and file it.
  18477. - If the cursor is on a <<<target>>>, update radio targets and
  18478. corresponding links in this buffer.
  18479. - If the cursor is on a numbered item in a plain list, renumber the
  18480. ordered list.
  18481. - If the cursor is on a checkbox, toggle it.
  18482. - If the cursor is on a code block, evaluate it. The variable
  18483. `org-confirm-babel-evaluate' can be used to control prompting
  18484. before code block evaluation, by default every code block
  18485. evaluation requires confirmation. Code block evaluation can be
  18486. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18487. (interactive "P")
  18488. (cond
  18489. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18490. org-occur-highlights
  18491. org-latex-fragment-image-overlays)
  18492. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18493. (org-remove-occur-highlights)
  18494. (org-remove-latex-fragment-image-overlays)
  18495. (message "Temporary highlights/overlays removed from current buffer"))
  18496. ((and (local-variable-p 'org-finish-function (current-buffer))
  18497. (fboundp org-finish-function))
  18498. (funcall org-finish-function))
  18499. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18500. (t
  18501. (let* ((context (org-element-context)) (type (org-element-type context)))
  18502. ;; Test if point is within a blank line.
  18503. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18504. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18505. (user-error "C-c C-c can do nothing useful at this location"))
  18506. ;; For convenience: at the first line of a paragraph on the
  18507. ;; same line as an item, apply function on that item instead.
  18508. (when (eq type 'paragraph)
  18509. (let ((parent (org-element-property :parent context)))
  18510. (when (and (eq (org-element-type parent) 'item)
  18511. (= (point-at-bol) (org-element-property :begin parent)))
  18512. (setq context parent type 'item))))
  18513. ;; Act according to type of element or object at point.
  18514. (case type
  18515. (clock (org-clock-update-time-maybe))
  18516. (dynamic-block
  18517. (save-excursion
  18518. (goto-char (org-element-property :post-affiliated context))
  18519. (org-update-dblock)))
  18520. (footnote-definition
  18521. (goto-char (org-element-property :post-affiliated context))
  18522. (call-interactively 'org-footnote-action))
  18523. (footnote-reference (call-interactively 'org-footnote-action))
  18524. ((headline inlinetask)
  18525. (save-excursion (goto-char (org-element-property :begin context))
  18526. (call-interactively 'org-set-tags)))
  18527. (item
  18528. ;; At an item: a double C-u set checkbox to "[-]"
  18529. ;; unconditionally, whereas a single one will toggle its
  18530. ;; presence. Without an universal argument, if the item
  18531. ;; has a checkbox, toggle it. Otherwise repair the list.
  18532. (let* ((box (org-element-property :checkbox context))
  18533. (struct (org-element-property :structure context))
  18534. (old-struct (copy-tree struct))
  18535. (parents (org-list-parents-alist struct))
  18536. (prevs (org-list-prevs-alist struct))
  18537. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18538. (org-list-set-checkbox
  18539. (org-element-property :begin context) struct
  18540. (cond ((equal arg '(16)) "[-]")
  18541. ((and (not box) (equal arg '(4))) "[ ]")
  18542. ((or (not box) (equal arg '(4))) nil)
  18543. ((eq box 'on) "[ ]")
  18544. (t "[X]")))
  18545. ;; Mimic `org-list-write-struct' but with grabbing
  18546. ;; a return value from `org-list-struct-fix-box'.
  18547. (org-list-struct-fix-ind struct parents 2)
  18548. (org-list-struct-fix-item-end struct)
  18549. (org-list-struct-fix-bul struct prevs)
  18550. (org-list-struct-fix-ind struct parents)
  18551. (let ((block-item
  18552. (org-list-struct-fix-box struct parents prevs orderedp)))
  18553. (if (and box (equal struct old-struct))
  18554. (if (equal arg '(16))
  18555. (message "Checkboxes already reset")
  18556. (user-error "Cannot toggle this checkbox: %s"
  18557. (if (eq box 'on)
  18558. "all subitems checked"
  18559. "unchecked subitems")))
  18560. (org-list-struct-apply-struct struct old-struct)
  18561. (org-update-checkbox-count-maybe))
  18562. (when block-item
  18563. (message "Checkboxes were removed due to empty box at line %d"
  18564. (org-current-line block-item))))))
  18565. (keyword
  18566. (let ((org-inhibit-startup-visibility-stuff t)
  18567. (org-startup-align-all-tables nil))
  18568. (when (boundp 'org-table-coordinate-overlays)
  18569. (mapc 'delete-overlay org-table-coordinate-overlays)
  18570. (setq org-table-coordinate-overlays nil))
  18571. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18572. (message "Local setup has been refreshed"))
  18573. (plain-list
  18574. ;; At a plain list, with a double C-u argument, set
  18575. ;; checkboxes of each item to "[-]", whereas a single one
  18576. ;; will toggle their presence according to the state of the
  18577. ;; first item in the list. Without an argument, repair the
  18578. ;; list.
  18579. (let* ((begin (org-element-property :contents-begin context))
  18580. (beginm (move-marker (make-marker) begin))
  18581. (struct (org-element-property :structure context))
  18582. (old-struct (copy-tree struct))
  18583. (first-box (save-excursion
  18584. (goto-char begin)
  18585. (looking-at org-list-full-item-re)
  18586. (match-string-no-properties 3)))
  18587. (new-box (cond ((equal arg '(16)) "[-]")
  18588. ((equal arg '(4)) (unless first-box "[ ]"))
  18589. ((equal first-box "[X]") "[ ]")
  18590. (t "[X]"))))
  18591. (cond
  18592. (arg
  18593. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18594. (org-list-get-all-items
  18595. begin struct (org-list-prevs-alist struct))))
  18596. ((and first-box (eq (point) begin))
  18597. ;; For convenience, when point is at bol on the first
  18598. ;; item of the list and no argument is provided, simply
  18599. ;; toggle checkbox of that item, if any.
  18600. (org-list-set-checkbox begin struct new-box)))
  18601. (org-list-write-struct
  18602. struct (org-list-parents-alist struct) old-struct)
  18603. (org-update-checkbox-count-maybe)
  18604. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18605. ((property-drawer node-property)
  18606. (call-interactively 'org-property-action))
  18607. ((radio-target target)
  18608. (call-interactively 'org-update-radio-target-regexp))
  18609. (statistics-cookie
  18610. (call-interactively 'org-update-statistics-cookies))
  18611. ((table table-cell table-row)
  18612. ;; At a table, recalculate every field and align it. Also
  18613. ;; send the table if necessary. If the table has
  18614. ;; a `table.el' type, just give up. At a table row or
  18615. ;; cell, maybe recalculate line but always align table.
  18616. (if (eq (org-element-property :type context) 'table.el)
  18617. (message "Use C-c ' to edit table.el tables")
  18618. (let ((org-enable-table-editor t))
  18619. (if (or (eq type 'table)
  18620. ;; Check if point is at a TBLFM line.
  18621. (and (eq type 'table-row)
  18622. (= (point) (org-element-property :end context))))
  18623. (save-excursion
  18624. (if (org-at-TBLFM-p) (org-calc-current-TBLFM)
  18625. (goto-char (org-element-property :contents-begin context))
  18626. (org-call-with-arg 'org-table-recalculate (or arg t))
  18627. (orgtbl-send-table 'maybe)))
  18628. (org-table-maybe-eval-formula)
  18629. (cond (arg (call-interactively 'org-table-recalculate))
  18630. ((org-table-maybe-recalculate-line))
  18631. (t (org-table-align)))))))
  18632. (timestamp (org-timestamp-change 0 'day))
  18633. (otherwise
  18634. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18635. (user-error
  18636. "C-c C-c can do nothing useful at this location")))))))))
  18637. (defun org-mode-restart ()
  18638. "Restart Org-mode, to scan again for special lines.
  18639. Also updates the keyword regular expressions."
  18640. (interactive)
  18641. (org-mode)
  18642. (message "Org-mode restarted"))
  18643. (defun org-kill-note-or-show-branches ()
  18644. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18645. (interactive)
  18646. (if (not org-finish-function)
  18647. (progn
  18648. (hide-subtree)
  18649. (call-interactively 'show-branches))
  18650. (let ((org-note-abort t))
  18651. (funcall org-finish-function))))
  18652. (defun org-open-line (n)
  18653. "Insert a new row in tables, call `open-line' elsewhere."
  18654. (interactive "*p")
  18655. (if (org-at-table-p)
  18656. (org-table-insert-row)
  18657. (open-line n)))
  18658. (defun org-return (&optional indent)
  18659. "Goto next table row or insert a newline.
  18660. Calls `org-table-next-row' or `newline', depending on context.
  18661. See the individual commands for more information."
  18662. (interactive)
  18663. (let (org-ts-what)
  18664. (cond
  18665. ((or (bobp) (org-in-src-block-p))
  18666. (if indent (newline-and-indent) (newline)))
  18667. ((org-at-table-p)
  18668. (org-table-justify-field-maybe)
  18669. (call-interactively 'org-table-next-row))
  18670. ;; when `newline-and-indent' is called within a list, make sure
  18671. ;; text moved stays inside the item.
  18672. ((and (org-in-item-p) indent)
  18673. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18674. (progn
  18675. (save-match-data (newline))
  18676. (org-indent-line-to (length (match-string 0))))
  18677. (let ((ind (org-get-indentation)))
  18678. (newline)
  18679. (if (org-looking-back org-list-end-re)
  18680. (org-indent-line)
  18681. (org-indent-line-to ind)))))
  18682. ((and org-return-follows-link
  18683. (org-at-timestamp-p t)
  18684. (not (eq org-ts-what 'after)))
  18685. (org-follow-timestamp-link))
  18686. ((and org-return-follows-link
  18687. (let ((tprop (get-text-property (point) 'face)))
  18688. (or (eq tprop 'org-link)
  18689. (and (listp tprop) (memq 'org-link tprop)))))
  18690. (call-interactively 'org-open-at-point))
  18691. ((and (org-at-heading-p)
  18692. (looking-at
  18693. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18694. (org-show-entry)
  18695. (end-of-line 1)
  18696. (newline))
  18697. (t (if indent (newline-and-indent) (newline))))))
  18698. (defun org-return-indent ()
  18699. "Goto next table row or insert a newline and indent.
  18700. Calls `org-table-next-row' or `newline-and-indent', depending on
  18701. context. See the individual commands for more information."
  18702. (interactive)
  18703. (org-return t))
  18704. (defun org-ctrl-c-star ()
  18705. "Compute table, or change heading status of lines.
  18706. Calls `org-table-recalculate' or `org-toggle-heading',
  18707. depending on context."
  18708. (interactive)
  18709. (cond
  18710. ((org-at-table-p)
  18711. (call-interactively 'org-table-recalculate))
  18712. (t
  18713. ;; Convert all lines in region to list items
  18714. (call-interactively 'org-toggle-heading))))
  18715. (defun org-ctrl-c-minus ()
  18716. "Insert separator line in table or modify bullet status of line.
  18717. Also turns a plain line or a region of lines into list items.
  18718. Calls `org-table-insert-hline', `org-toggle-item', or
  18719. `org-cycle-list-bullet', depending on context."
  18720. (interactive)
  18721. (cond
  18722. ((org-at-table-p)
  18723. (call-interactively 'org-table-insert-hline))
  18724. ((org-region-active-p)
  18725. (call-interactively 'org-toggle-item))
  18726. ((org-in-item-p)
  18727. (call-interactively 'org-cycle-list-bullet))
  18728. (t
  18729. (call-interactively 'org-toggle-item))))
  18730. (defun org-toggle-item (arg)
  18731. "Convert headings or normal lines to items, items to normal lines.
  18732. If there is no active region, only the current line is considered.
  18733. If the first non blank line in the region is a headline, convert
  18734. all headlines to items, shifting text accordingly.
  18735. If it is an item, convert all items to normal lines.
  18736. If it is normal text, change region into a list of items.
  18737. With a prefix argument ARG, change the region in a single item."
  18738. (interactive "P")
  18739. (let ((shift-text
  18740. (function
  18741. ;; Shift text in current section to IND, from point to END.
  18742. ;; The function leaves point to END line.
  18743. (lambda (ind end)
  18744. (let ((min-i 1000) (end (copy-marker end)))
  18745. ;; First determine the minimum indentation (MIN-I) of
  18746. ;; the text.
  18747. (save-excursion
  18748. (catch 'exit
  18749. (while (< (point) end)
  18750. (let ((i (org-get-indentation)))
  18751. (cond
  18752. ;; Skip blank lines and inline tasks.
  18753. ((looking-at "^[ \t]*$"))
  18754. ((looking-at org-outline-regexp-bol))
  18755. ;; We can't find less than 0 indentation.
  18756. ((zerop i) (throw 'exit (setq min-i 0)))
  18757. ((< i min-i) (setq min-i i))))
  18758. (forward-line))))
  18759. ;; Then indent each line so that a line indented to
  18760. ;; MIN-I becomes indented to IND. Ignore blank lines
  18761. ;; and inline tasks in the process.
  18762. (let ((delta (- ind min-i)))
  18763. (while (< (point) end)
  18764. (unless (or (looking-at "^[ \t]*$")
  18765. (looking-at org-outline-regexp-bol))
  18766. (org-indent-line-to (+ (org-get-indentation) delta)))
  18767. (forward-line)))))))
  18768. (skip-blanks
  18769. (function
  18770. ;; Return beginning of first non-blank line, starting from
  18771. ;; line at POS.
  18772. (lambda (pos)
  18773. (save-excursion
  18774. (goto-char pos)
  18775. (skip-chars-forward " \r\t\n")
  18776. (point-at-bol)))))
  18777. beg end)
  18778. ;; Determine boundaries of changes.
  18779. (if (org-region-active-p)
  18780. (setq beg (funcall skip-blanks (region-beginning))
  18781. end (copy-marker (region-end)))
  18782. (setq beg (funcall skip-blanks (point-at-bol))
  18783. end (copy-marker (point-at-eol))))
  18784. ;; Depending on the starting line, choose an action on the text
  18785. ;; between BEG and END.
  18786. (org-with-limited-levels
  18787. (save-excursion
  18788. (goto-char beg)
  18789. (cond
  18790. ;; Case 1. Start at an item: de-itemize. Note that it only
  18791. ;; happens when a region is active: `org-ctrl-c-minus'
  18792. ;; would call `org-cycle-list-bullet' otherwise.
  18793. ((org-at-item-p)
  18794. (while (< (point) end)
  18795. (when (org-at-item-p)
  18796. (skip-chars-forward " \t")
  18797. (delete-region (point) (match-end 0)))
  18798. (forward-line)))
  18799. ;; Case 2. Start at an heading: convert to items.
  18800. ((org-at-heading-p)
  18801. (let* ((bul (org-list-bullet-string "-"))
  18802. (bul-len (length bul))
  18803. ;; Indentation of the first heading. It should be
  18804. ;; relative to the indentation of its parent, if any.
  18805. (start-ind (save-excursion
  18806. (cond
  18807. ((not org-adapt-indentation) 0)
  18808. ((not (outline-previous-heading)) 0)
  18809. (t (length (match-string 0))))))
  18810. ;; Level of first heading. Further headings will be
  18811. ;; compared to it to determine hierarchy in the list.
  18812. (ref-level (org-reduced-level (org-outline-level))))
  18813. (while (< (point) end)
  18814. (let* ((level (org-reduced-level (org-outline-level)))
  18815. (delta (max 0 (- level ref-level))))
  18816. ;; If current headline is less indented than the first
  18817. ;; one, set it as reference, in order to preserve
  18818. ;; subtrees.
  18819. (when (< level ref-level) (setq ref-level level))
  18820. (replace-match bul t t)
  18821. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18822. ;; Ensure all text down to END (or SECTION-END) belongs
  18823. ;; to the newly created item.
  18824. (let ((section-end (save-excursion
  18825. (or (outline-next-heading) (point)))))
  18826. (forward-line)
  18827. (funcall shift-text
  18828. (+ start-ind (* (1+ delta) bul-len))
  18829. (min end section-end)))))))
  18830. ;; Case 3. Normal line with ARG: make the first line of region
  18831. ;; an item, and shift indentation of others lines to
  18832. ;; set them as item's body.
  18833. (arg (let* ((bul (org-list-bullet-string "-"))
  18834. (bul-len (length bul))
  18835. (ref-ind (org-get-indentation)))
  18836. (skip-chars-forward " \t")
  18837. (insert bul)
  18838. (forward-line)
  18839. (while (< (point) end)
  18840. ;; Ensure that lines less indented than first one
  18841. ;; still get included in item body.
  18842. (funcall shift-text
  18843. (+ ref-ind bul-len)
  18844. (min end (save-excursion (or (outline-next-heading)
  18845. (point)))))
  18846. (forward-line))))
  18847. ;; Case 4. Normal line without ARG: turn each non-item line
  18848. ;; into an item.
  18849. (t
  18850. (while (< (point) end)
  18851. (unless (or (org-at-heading-p) (org-at-item-p))
  18852. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18853. (replace-match
  18854. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18855. (forward-line))))))))
  18856. (defun org-toggle-heading (&optional nstars)
  18857. "Convert headings to normal text, or items or text to headings.
  18858. If there is no active region, only convert the current line.
  18859. With a \\[universal-argument] prefix, convert the whole list at
  18860. point into heading.
  18861. In a region:
  18862. - If the first non blank line is a headline, remove the stars
  18863. from all headlines in the region.
  18864. - If it is a normal line, turn each and every normal line (i.e.,
  18865. not an heading or an item) in the region into headings. If you
  18866. want to convert only the first line of this region, use one
  18867. universal prefix argument.
  18868. - If it is a plain list item, turn all plain list items into headings.
  18869. When converting a line into a heading, the number of stars is chosen
  18870. such that the lines become children of the current entry. However,
  18871. when a numeric prefix argument is given, its value determines the
  18872. number of stars to add."
  18873. (interactive "P")
  18874. (let ((skip-blanks
  18875. (function
  18876. ;; Return beginning of first non-blank line, starting from
  18877. ;; line at POS.
  18878. (lambda (pos)
  18879. (save-excursion
  18880. (goto-char pos)
  18881. (while (org-at-comment-p) (forward-line))
  18882. (skip-chars-forward " \r\t\n")
  18883. (point-at-bol)))))
  18884. beg end toggled)
  18885. ;; Determine boundaries of changes. If a universal prefix has
  18886. ;; been given, put the list in a region. If region ends at a bol,
  18887. ;; do not consider the last line to be in the region.
  18888. (when (and current-prefix-arg (org-at-item-p))
  18889. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18890. (org-mark-element))
  18891. (if (org-region-active-p)
  18892. (setq beg (funcall skip-blanks (region-beginning))
  18893. end (copy-marker (save-excursion
  18894. (goto-char (region-end))
  18895. (if (bolp) (point) (point-at-eol)))))
  18896. (setq beg (funcall skip-blanks (point-at-bol))
  18897. end (copy-marker (point-at-eol))))
  18898. ;; Ensure inline tasks don't count as headings.
  18899. (org-with-limited-levels
  18900. (save-excursion
  18901. (goto-char beg)
  18902. (cond
  18903. ;; Case 1. Started at an heading: de-star headings.
  18904. ((org-at-heading-p)
  18905. (while (< (point) end)
  18906. (when (org-at-heading-p t)
  18907. (looking-at org-outline-regexp) (replace-match "")
  18908. (setq toggled t))
  18909. (forward-line)))
  18910. ;; Case 2. Started at an item: change items into headlines.
  18911. ;; One star will be added by `org-list-to-subtree'.
  18912. ((org-at-item-p)
  18913. (let* ((stars (make-string
  18914. ;; subtract the star that will be added again by
  18915. ;; `org-list-to-subtree'
  18916. (if (numberp nstars) (1- nstars)
  18917. (or (org-current-level) 0))
  18918. ?*))
  18919. (add-stars
  18920. (cond (nstars "") ; stars from prefix only
  18921. ((equal stars "") "") ; before first heading
  18922. (org-odd-levels-only "*") ; inside heading, odd
  18923. (t "")))) ; inside heading, oddeven
  18924. (while (< (point) end)
  18925. (when (org-at-item-p)
  18926. ;; Pay attention to cases when region ends before list.
  18927. (let* ((struct (org-list-struct))
  18928. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18929. (save-restriction
  18930. (narrow-to-region (point) list-end)
  18931. (insert
  18932. (org-list-to-subtree
  18933. (org-list-parse-list t)
  18934. '(:istart (concat stars add-stars (funcall get-stars depth))
  18935. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18936. (setq toggled t))
  18937. (forward-line))))
  18938. ;; Case 3. Started at normal text: make every line an heading,
  18939. ;; skipping headlines and items.
  18940. (t (let* ((stars
  18941. (make-string
  18942. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18943. (add-stars
  18944. (cond (nstars "") ; stars from prefix only
  18945. ((equal stars "") "*") ; before first heading
  18946. (org-odd-levels-only "**") ; inside heading, odd
  18947. (t "*"))) ; inside heading, oddeven
  18948. (rpl (concat stars add-stars " "))
  18949. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18950. (while (< (point) (if (equal nstars '(4)) lend end))
  18951. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18952. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18953. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18954. (forward-line)))))))
  18955. (unless toggled (message "Cannot toggle heading from here"))))
  18956. (defun org-meta-return (&optional arg)
  18957. "Insert a new heading or wrap a region in a table.
  18958. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18959. See the individual commands for more information."
  18960. (interactive "P")
  18961. (org-check-before-invisible-edit 'insert)
  18962. (cond
  18963. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18964. ((or (org-at-drawer-p) (org-in-drawer-p) (org-at-property-p))
  18965. (newline-and-indent))
  18966. ((org-at-table-p)
  18967. (call-interactively 'org-table-wrap-region))
  18968. (t (call-interactively 'org-insert-heading))))
  18969. ;;; Menu entries
  18970. (defsubst org-in-subtree-not-table-p ()
  18971. "Are we in a subtree and not in a table?"
  18972. (and (not (org-before-first-heading-p))
  18973. (not (org-at-table-p))))
  18974. ;; Define the Org-mode menus
  18975. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18976. '("Tbl"
  18977. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18978. ["Next Field" org-cycle (org-at-table-p)]
  18979. ["Previous Field" org-shifttab (org-at-table-p)]
  18980. ["Next Row" org-return (org-at-table-p)]
  18981. "--"
  18982. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18983. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18984. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18985. "--"
  18986. ("Column"
  18987. ["Move Column Left" org-metaleft (org-at-table-p)]
  18988. ["Move Column Right" org-metaright (org-at-table-p)]
  18989. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18990. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18991. ("Row"
  18992. ["Move Row Up" org-metaup (org-at-table-p)]
  18993. ["Move Row Down" org-metadown (org-at-table-p)]
  18994. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18995. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18996. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18997. "--"
  18998. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18999. ("Rectangle"
  19000. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19001. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19002. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19003. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19004. "--"
  19005. ("Calculate"
  19006. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19007. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19008. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19009. "--"
  19010. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19011. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19012. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19013. "--"
  19014. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19015. "--"
  19016. ["Sum Column/Rectangle" org-table-sum
  19017. (or (org-at-table-p) (org-region-active-p))]
  19018. ["Which Column?" org-table-current-column (org-at-table-p)])
  19019. ["Debug Formulas"
  19020. org-table-toggle-formula-debugger
  19021. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19022. ["Show Col/Row Numbers"
  19023. org-table-toggle-coordinate-overlays
  19024. :style toggle
  19025. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19026. "--"
  19027. ["Create" org-table-create (and (not (org-at-table-p))
  19028. org-enable-table-editor)]
  19029. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19030. ["Import from File" org-table-import (not (org-at-table-p))]
  19031. ["Export to File" org-table-export (org-at-table-p)]
  19032. "--"
  19033. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19034. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19035. '("Org"
  19036. ("Show/Hide"
  19037. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19038. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19039. ["Sparse Tree..." org-sparse-tree t]
  19040. ["Reveal Context" org-reveal t]
  19041. ["Show All" show-all t]
  19042. "--"
  19043. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19044. "--"
  19045. ["New Heading" org-insert-heading t]
  19046. ("Navigate Headings"
  19047. ["Up" outline-up-heading t]
  19048. ["Next" outline-next-visible-heading t]
  19049. ["Previous" outline-previous-visible-heading t]
  19050. ["Next Same Level" outline-forward-same-level t]
  19051. ["Previous Same Level" outline-backward-same-level t]
  19052. "--"
  19053. ["Jump" org-goto t])
  19054. ("Edit Structure"
  19055. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19056. "--"
  19057. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19058. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19059. "--"
  19060. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19061. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19062. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19063. "--"
  19064. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19065. "--"
  19066. ["Copy visible text" org-copy-visible t]
  19067. "--"
  19068. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19069. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19070. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19071. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19072. "--"
  19073. ["Sort Region/Children" org-sort t]
  19074. "--"
  19075. ["Convert to odd levels" org-convert-to-odd-levels t]
  19076. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19077. ("Editing"
  19078. ["Emphasis..." org-emphasize t]
  19079. ["Edit Source Example" org-edit-special t]
  19080. "--"
  19081. ["Footnote new/jump" org-footnote-action t]
  19082. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19083. ("Archive"
  19084. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19085. "--"
  19086. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19087. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19088. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19089. )
  19090. "--"
  19091. ("Hyperlinks"
  19092. ["Store Link (Global)" org-store-link t]
  19093. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19094. ["Insert Link" org-insert-link t]
  19095. ["Follow Link" org-open-at-point t]
  19096. "--"
  19097. ["Next link" org-next-link t]
  19098. ["Previous link" org-previous-link t]
  19099. "--"
  19100. ["Descriptive Links"
  19101. org-toggle-link-display
  19102. :style radio
  19103. :selected org-descriptive-links
  19104. ]
  19105. ["Literal Links"
  19106. org-toggle-link-display
  19107. :style radio
  19108. :selected (not org-descriptive-links)])
  19109. "--"
  19110. ("TODO Lists"
  19111. ["TODO/DONE/-" org-todo t]
  19112. ("Select keyword"
  19113. ["Next keyword" org-shiftright (org-at-heading-p)]
  19114. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19115. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19116. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19117. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19118. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19119. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19120. "--"
  19121. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19122. :selected org-enforce-todo-dependencies :style toggle :active t]
  19123. "Settings for tree at point"
  19124. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19125. :selected (org-entry-get nil "ORDERED")
  19126. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19127. ["Do Children parallel" org-toggle-ordered-property :style radio
  19128. :selected (not (org-entry-get nil "ORDERED"))
  19129. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19130. "--"
  19131. ["Set Priority" org-priority t]
  19132. ["Priority Up" org-shiftup t]
  19133. ["Priority Down" org-shiftdown t]
  19134. "--"
  19135. ["Get news from all feeds" org-feed-update-all t]
  19136. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19137. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19138. ("TAGS and Properties"
  19139. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19140. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19141. "--"
  19142. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19143. ["Column view of properties" org-columns t]
  19144. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19145. ("Dates and Scheduling"
  19146. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19147. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19148. ("Change Date"
  19149. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19150. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19151. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19152. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19153. ["Compute Time Range" org-evaluate-time-range t]
  19154. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19155. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19156. "--"
  19157. ["Custom time format" org-toggle-time-stamp-overlays
  19158. :style radio :selected org-display-custom-times]
  19159. "--"
  19160. ["Goto Calendar" org-goto-calendar t]
  19161. ["Date from Calendar" org-date-from-calendar t]
  19162. "--"
  19163. ["Start/Restart Timer" org-timer-start t]
  19164. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19165. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19166. ["Insert Timer String" org-timer t]
  19167. ["Insert Timer Item" org-timer-item t])
  19168. ("Logging work"
  19169. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19170. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19171. ["Clock out" org-clock-out t]
  19172. ["Clock cancel" org-clock-cancel t]
  19173. "--"
  19174. ["Mark as default task" org-clock-mark-default-task t]
  19175. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19176. ["Goto running clock" org-clock-goto t]
  19177. "--"
  19178. ["Display times" org-clock-display t]
  19179. ["Create clock table" org-clock-report t]
  19180. "--"
  19181. ["Record DONE time"
  19182. (progn (setq org-log-done (not org-log-done))
  19183. (message "Switching to %s will %s record a timestamp"
  19184. (car org-done-keywords)
  19185. (if org-log-done "automatically" "not")))
  19186. :style toggle :selected org-log-done])
  19187. "--"
  19188. ["Agenda Command..." org-agenda t]
  19189. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19190. ("File List for Agenda")
  19191. ("Special views current file"
  19192. ["TODO Tree" org-show-todo-tree t]
  19193. ["Check Deadlines" org-check-deadlines t]
  19194. ["Timeline" org-timeline t]
  19195. ["Tags/Property tree" org-match-sparse-tree t])
  19196. "--"
  19197. ["Export/Publish..." org-export-dispatch t]
  19198. ("LaTeX"
  19199. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19200. :selected org-cdlatex-mode]
  19201. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19202. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19203. ["Modify math symbol" org-cdlatex-math-modify
  19204. (org-inside-LaTeX-fragment-p)]
  19205. ["Insert citation" org-reftex-citation t]
  19206. "--"
  19207. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19208. "--"
  19209. ("MobileOrg"
  19210. ["Push Files and Views" org-mobile-push t]
  19211. ["Get Captured and Flagged" org-mobile-pull t]
  19212. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19213. "--"
  19214. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19215. "--"
  19216. ("Documentation"
  19217. ["Show Version" org-version t]
  19218. ["Info Documentation" org-info t])
  19219. ("Customize"
  19220. ["Browse Org Group" org-customize t]
  19221. "--"
  19222. ["Expand This Menu" org-create-customize-menu
  19223. (fboundp 'customize-menu-create)])
  19224. ["Send bug report" org-submit-bug-report t]
  19225. "--"
  19226. ("Refresh/Reload"
  19227. ["Refresh setup current buffer" org-mode-restart t]
  19228. ["Reload Org (after update)" org-reload t]
  19229. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19230. ))
  19231. (defun org-info (&optional node)
  19232. "Read documentation for Org-mode in the info system.
  19233. With optional NODE, go directly to that node."
  19234. (interactive)
  19235. (info (format "(org)%s" (or node ""))))
  19236. ;;;###autoload
  19237. (defun org-submit-bug-report ()
  19238. "Submit a bug report on Org-mode via mail.
  19239. Don't hesitate to report any problems or inaccurate documentation.
  19240. If you don't have setup sending mail from (X)Emacs, please copy the
  19241. output buffer into your mail program, as it gives us important
  19242. information about your Org-mode version and configuration."
  19243. (interactive)
  19244. (require 'reporter)
  19245. (org-load-modules-maybe)
  19246. (org-require-autoloaded-modules)
  19247. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19248. (reporter-submit-bug-report
  19249. "emacs-orgmode@gnu.org"
  19250. (org-version nil 'full)
  19251. (let (list)
  19252. (save-window-excursion
  19253. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19254. (delete-other-windows)
  19255. (erase-buffer)
  19256. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19257. We would like to add your full Org-mode and Outline configuration to the
  19258. bug report. This greatly simplifies the work of the maintainer and
  19259. other experts on the mailing list.
  19260. HOWEVER, some variables you have customized may contain private
  19261. information. The names of customers, colleagues, or friends, might
  19262. appear in the form of file names, tags, todo states, or search strings.
  19263. If you answer yes to the prompt, you might want to check and remove
  19264. such private information before sending the email.")
  19265. (add-text-properties (point-min) (point-max) '(face org-warning))
  19266. (when (yes-or-no-p "Include your Org-mode configuration ")
  19267. (mapatoms
  19268. (lambda (v)
  19269. (and (boundp v)
  19270. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19271. (or (and (symbol-value v)
  19272. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19273. (and
  19274. (get v 'custom-type) (get v 'standard-value)
  19275. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19276. (push v list)))))
  19277. (kill-buffer (get-buffer "*Warn about privacy*"))
  19278. list))
  19279. nil nil
  19280. "Remember to cover the basics, that is, what you expected to happen and
  19281. what in fact did happen. You don't know how to make a good report? See
  19282. http://orgmode.org/manual/Feedback.html#Feedback
  19283. Your bug report will be posted to the Org-mode mailing list.
  19284. ------------------------------------------------------------------------")
  19285. (save-excursion
  19286. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19287. (replace-match "\\1Bug: \\3 [\\2]")))))
  19288. (defun org-install-agenda-files-menu ()
  19289. (let ((bl (buffer-list)))
  19290. (save-excursion
  19291. (while bl
  19292. (set-buffer (pop bl))
  19293. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19294. (when (derived-mode-p 'org-mode)
  19295. (easy-menu-change
  19296. '("Org") "File List for Agenda"
  19297. (append
  19298. (list
  19299. ["Edit File List" (org-edit-agenda-file-list) t]
  19300. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19301. ["Remove Current File from List" org-remove-file t]
  19302. ["Cycle through agenda files" org-cycle-agenda-files t]
  19303. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19304. "--")
  19305. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19306. ;;;; Documentation
  19307. (defun org-require-autoloaded-modules ()
  19308. (interactive)
  19309. (mapc 'require
  19310. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19311. org-remember org-table org-timer)))
  19312. ;;;###autoload
  19313. (defun org-reload (&optional uncompiled)
  19314. "Reload all org lisp files.
  19315. With prefix arg UNCOMPILED, load the uncompiled versions."
  19316. (interactive "P")
  19317. (require 'loadhist)
  19318. (let* ((org-dir (org-find-library-dir "org"))
  19319. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19320. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19321. (remove-re (mapconcat 'identity
  19322. (mapcar (lambda (f) (concat "^" f "$"))
  19323. (list (if (featurep 'xemacs)
  19324. "org-colview"
  19325. "org-colview-xemacs")
  19326. "org" "org-loaddefs" "org-version"))
  19327. "\\|"))
  19328. (feats (delete-dups
  19329. (mapcar 'file-name-sans-extension
  19330. (mapcar 'file-name-nondirectory
  19331. (delq nil
  19332. (mapcar 'feature-file
  19333. features))))))
  19334. (lfeat (append
  19335. (sort
  19336. (setq feats
  19337. (delq nil (mapcar
  19338. (lambda (f)
  19339. (if (and (string-match feature-re f)
  19340. (not (string-match remove-re f)))
  19341. f nil))
  19342. feats)))
  19343. 'string-lessp)
  19344. (list "org-version" "org")))
  19345. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19346. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19347. load-uncore load-misses)
  19348. (setq load-misses
  19349. (delq 't
  19350. (mapcar (lambda (f)
  19351. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19352. (and (string= org-dir contrib-dir)
  19353. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19354. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19355. (add-to-list 'load-uncore f 'append)
  19356. 't)
  19357. f))
  19358. lfeat)))
  19359. (if load-uncore
  19360. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19361. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19362. (if load-misses
  19363. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19364. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19365. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19366. ;;;###autoload
  19367. (defun org-customize ()
  19368. "Call the customize function with org as argument."
  19369. (interactive)
  19370. (org-load-modules-maybe)
  19371. (org-require-autoloaded-modules)
  19372. (customize-browse 'org))
  19373. (defun org-create-customize-menu ()
  19374. "Create a full customization menu for Org-mode, insert it into the menu."
  19375. (interactive)
  19376. (org-load-modules-maybe)
  19377. (org-require-autoloaded-modules)
  19378. (if (fboundp 'customize-menu-create)
  19379. (progn
  19380. (easy-menu-change
  19381. '("Org") "Customize"
  19382. `(["Browse Org group" org-customize t]
  19383. "--"
  19384. ,(customize-menu-create 'org)
  19385. ["Set" Custom-set t]
  19386. ["Save" Custom-save t]
  19387. ["Reset to Current" Custom-reset-current t]
  19388. ["Reset to Saved" Custom-reset-saved t]
  19389. ["Reset to Standard Settings" Custom-reset-standard t]))
  19390. (message "\"Org\"-menu now contains full customization menu"))
  19391. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19392. ;;;; Miscellaneous stuff
  19393. ;;; Generally useful functions
  19394. (defun org-get-at-bol (property)
  19395. "Get text property PROPERTY at beginning of line."
  19396. (get-text-property (point-at-bol) property))
  19397. (defun org-find-text-property-in-string (prop s)
  19398. "Return the first non-nil value of property PROP in string S."
  19399. (or (get-text-property 0 prop s)
  19400. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19401. prop s)))
  19402. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19403. "Display the given MESSAGE as a warning."
  19404. (if (fboundp 'display-warning)
  19405. (display-warning 'org message
  19406. (if (featurep 'xemacs) 'warning :warning))
  19407. (let ((buf (get-buffer-create "*Org warnings*")))
  19408. (with-current-buffer buf
  19409. (goto-char (point-max))
  19410. (insert "Warning (Org): " message)
  19411. (unless (bolp)
  19412. (newline)))
  19413. (display-buffer buf)
  19414. (sit-for 0))))
  19415. (defun org-eval (form)
  19416. "Eval FORM and return result."
  19417. (condition-case error
  19418. (eval form)
  19419. (error (format "%%![Error: %s]" error))))
  19420. (defun org-in-clocktable-p ()
  19421. "Check if the cursor is in a clocktable."
  19422. (let ((pos (point)) start)
  19423. (save-excursion
  19424. (end-of-line 1)
  19425. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19426. (setq start (match-beginning 0))
  19427. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19428. (>= (match-end 0) pos)
  19429. start))))
  19430. (defun org-in-commented-line ()
  19431. "Is point in a line starting with `#'?"
  19432. (equal (char-after (point-at-bol)) ?#))
  19433. (defun org-in-indented-comment-line ()
  19434. "Is point in a line starting with `#' after some white space?"
  19435. (save-excursion
  19436. (save-match-data
  19437. (goto-char (point-at-bol))
  19438. (looking-at "[ \t]*#"))))
  19439. (defun org-in-verbatim-emphasis ()
  19440. (save-match-data
  19441. (and (org-in-regexp org-emph-re 2)
  19442. (>= (point) (match-beginning 3))
  19443. (<= (point) (match-end 4))
  19444. (member (match-string 3) '("=" "~")))))
  19445. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19446. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19447. (if (and marker (marker-buffer marker)
  19448. (buffer-live-p (marker-buffer marker)))
  19449. (progn
  19450. (org-pop-to-buffer-same-window (marker-buffer marker))
  19451. (if (or (> marker (point-max)) (< marker (point-min)))
  19452. (widen))
  19453. (goto-char marker)
  19454. (org-show-context 'org-goto))
  19455. (if bookmark
  19456. (bookmark-jump bookmark)
  19457. (error "Cannot find location"))))
  19458. (defun org-quote-csv-field (s)
  19459. "Quote field for inclusion in CSV material."
  19460. (if (string-match "[\",]" s)
  19461. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19462. s))
  19463. (defun org-force-self-insert (N)
  19464. "Needed to enforce self-insert under remapping."
  19465. (interactive "p")
  19466. (self-insert-command N))
  19467. (defun org-string-width (s)
  19468. "Compute width of string, ignoring invisible characters.
  19469. This ignores character with invisibility property `org-link', and also
  19470. characters with property `org-cwidth', because these will become invisible
  19471. upon the next fontification round."
  19472. (let (b l)
  19473. (when (or (eq t buffer-invisibility-spec)
  19474. (assq 'org-link buffer-invisibility-spec))
  19475. (while (setq b (text-property-any 0 (length s)
  19476. 'invisible 'org-link s))
  19477. (setq s (concat (substring s 0 b)
  19478. (substring s (or (next-single-property-change
  19479. b 'invisible s) (length s)))))))
  19480. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19481. (setq s (concat (substring s 0 b)
  19482. (substring s (or (next-single-property-change
  19483. b 'org-cwidth s) (length s))))))
  19484. (setq l (string-width s) b -1)
  19485. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19486. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19487. l))
  19488. (defun org-shorten-string (s maxlength)
  19489. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19490. If the string is shorter or has length MAXLENGTH, just return the
  19491. original string. If it is longer, the functions finds a space in the
  19492. string, breaks this string off at that locations and adds three dots
  19493. as ellipsis. Including the ellipsis, the string will not be longer
  19494. than MAXLENGTH. If finding a good breaking point in the string does
  19495. not work, the string is just chopped off in the middle of a word
  19496. if necessary."
  19497. (if (<= (length s) maxlength)
  19498. s
  19499. (let* ((n (max (- maxlength 4) 1))
  19500. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19501. (if (string-match re s)
  19502. (concat (match-string 1 s) "...")
  19503. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19504. (defun org-get-indentation (&optional line)
  19505. "Get the indentation of the current line, interpreting tabs.
  19506. When LINE is given, assume it represents a line and compute its indentation."
  19507. (if line
  19508. (if (string-match "^ *" (org-remove-tabs line))
  19509. (match-end 0))
  19510. (save-excursion
  19511. (beginning-of-line 1)
  19512. (skip-chars-forward " \t")
  19513. (current-column))))
  19514. (defun org-get-string-indentation (s)
  19515. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19516. (let ((n -1) (i 0) (w tab-width) c)
  19517. (catch 'exit
  19518. (while (< (setq n (1+ n)) (length s))
  19519. (setq c (aref s n))
  19520. (cond ((= c ?\ ) (setq i (1+ i)))
  19521. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19522. (t (throw 'exit t)))))
  19523. i))
  19524. (defun org-remove-tabs (s &optional width)
  19525. "Replace tabulators in S with spaces.
  19526. Assumes that s is a single line, starting in column 0."
  19527. (setq width (or width tab-width))
  19528. (while (string-match "\t" s)
  19529. (setq s (replace-match
  19530. (make-string
  19531. (- (* width (/ (+ (match-beginning 0) width) width))
  19532. (match-beginning 0)) ?\ )
  19533. t t s)))
  19534. s)
  19535. (defun org-fix-indentation (line ind)
  19536. "Fix indentation in LINE.
  19537. IND is a cons cell with target and minimum indentation.
  19538. If the current indentation in LINE is smaller than the minimum,
  19539. leave it alone. If it is larger than ind, set it to the target."
  19540. (let* ((l (org-remove-tabs line))
  19541. (i (org-get-indentation l))
  19542. (i1 (car ind)) (i2 (cdr ind)))
  19543. (if (>= i i2) (setq l (substring line i2)))
  19544. (if (> i1 0)
  19545. (concat (make-string i1 ?\ ) l)
  19546. l)))
  19547. (defun org-remove-indentation (code &optional n)
  19548. "Remove the maximum common indentation from the lines in CODE.
  19549. N may optionally be the number of spaces to remove."
  19550. (with-temp-buffer
  19551. (insert code)
  19552. (org-do-remove-indentation n)
  19553. (buffer-string)))
  19554. (defun org-do-remove-indentation (&optional n)
  19555. "Remove the maximum common indentation from the buffer."
  19556. (untabify (point-min) (point-max))
  19557. (let ((min 10000) re)
  19558. (if n
  19559. (setq min n)
  19560. (goto-char (point-min))
  19561. (while (re-search-forward "^ *[^ \n]" nil t)
  19562. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19563. (unless (or (= min 0) (= min 10000))
  19564. (setq re (format "^ \\{%d\\}" min))
  19565. (goto-char (point-min))
  19566. (while (re-search-forward re nil t)
  19567. (replace-match "")
  19568. (end-of-line 1))
  19569. min)))
  19570. (defun org-fill-template (template alist)
  19571. "Find each %key of ALIST in TEMPLATE and replace it."
  19572. (let ((case-fold-search nil)
  19573. entry key value)
  19574. (setq alist (sort (copy-sequence alist)
  19575. (lambda (a b) (< (length (car a)) (length (car b))))))
  19576. (while (setq entry (pop alist))
  19577. (setq template
  19578. (replace-regexp-in-string
  19579. (concat "%" (regexp-quote (car entry)))
  19580. (or (cdr entry) "") template t t)))
  19581. template))
  19582. (defun org-base-buffer (buffer)
  19583. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19584. (if (not buffer)
  19585. buffer
  19586. (or (buffer-base-buffer buffer)
  19587. buffer)))
  19588. (defun org-trim (s)
  19589. "Remove whitespace at beginning and end of string."
  19590. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19591. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19592. s)
  19593. (defun org-wrap (string &optional width lines)
  19594. "Wrap string to either a number of lines, or a width in characters.
  19595. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19596. that costs. If there is a word longer than WIDTH, the text is actually
  19597. wrapped to the length of that word.
  19598. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19599. many lines, whatever width that takes.
  19600. The return value is a list of lines, without newlines at the end."
  19601. (let* ((words (org-split-string string "[ \t\n]+"))
  19602. (maxword (apply 'max (mapcar 'org-string-width words)))
  19603. w ll)
  19604. (cond (width
  19605. (org-do-wrap words (max maxword width)))
  19606. (lines
  19607. (setq w maxword)
  19608. (setq ll (org-do-wrap words maxword))
  19609. (if (<= (length ll) lines)
  19610. ll
  19611. (setq ll words)
  19612. (while (> (length ll) lines)
  19613. (setq w (1+ w))
  19614. (setq ll (org-do-wrap words w)))
  19615. ll))
  19616. (t (error "Cannot wrap this")))))
  19617. (defun org-do-wrap (words width)
  19618. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19619. (let (lines line)
  19620. (while words
  19621. (setq line (pop words))
  19622. (while (and words (< (+ (length line) (length (car words))) width))
  19623. (setq line (concat line " " (pop words))))
  19624. (setq lines (push line lines)))
  19625. (nreverse lines)))
  19626. (defun org-split-string (string &optional separators)
  19627. "Splits STRING into substrings at SEPARATORS.
  19628. No empty strings are returned if there are matches at the beginning
  19629. and end of string."
  19630. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19631. (start 0)
  19632. notfirst
  19633. (list nil))
  19634. (while (and (string-match rexp string
  19635. (if (and notfirst
  19636. (= start (match-beginning 0))
  19637. (< start (length string)))
  19638. (1+ start) start))
  19639. (< (match-beginning 0) (length string)))
  19640. (setq notfirst t)
  19641. (or (eq (match-beginning 0) 0)
  19642. (and (eq (match-beginning 0) (match-end 0))
  19643. (eq (match-beginning 0) start))
  19644. (setq list
  19645. (cons (substring string start (match-beginning 0))
  19646. list)))
  19647. (setq start (match-end 0)))
  19648. (or (eq start (length string))
  19649. (setq list
  19650. (cons (substring string start)
  19651. list)))
  19652. (nreverse list)))
  19653. (defun org-quote-vert (s)
  19654. "Replace \"|\" with \"\\vert\"."
  19655. (while (string-match "|" s)
  19656. (setq s (replace-match "\\vert" t t s)))
  19657. s)
  19658. (defun org-uuidgen-p (s)
  19659. "Is S an ID created by UUIDGEN?"
  19660. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19661. (defun org-in-src-block-p (&optional inside)
  19662. "Whether point is in a code source block.
  19663. When INSIDE is non-nil, don't consider we are within a src block
  19664. when point is at #+BEGIN_SRC or #+END_SRC."
  19665. (let ((case-fold-search t) ov)
  19666. (or (and (setq ov (overlays-at (point)))
  19667. (memq 'org-block-background
  19668. (overlay-properties (car ov))))
  19669. (and (not inside)
  19670. (save-match-data
  19671. (save-excursion
  19672. (beginning-of-line)
  19673. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19674. (defun org-context ()
  19675. "Return a list of contexts of the current cursor position.
  19676. If several contexts apply, all are returned.
  19677. Each context entry is a list with a symbol naming the context, and
  19678. two positions indicating start and end of the context. Possible
  19679. contexts are:
  19680. :headline anywhere in a headline
  19681. :headline-stars on the leading stars in a headline
  19682. :todo-keyword on a TODO keyword (including DONE) in a headline
  19683. :tags on the TAGS in a headline
  19684. :priority on the priority cookie in a headline
  19685. :item on the first line of a plain list item
  19686. :item-bullet on the bullet/number of a plain list item
  19687. :checkbox on the checkbox in a plain list item
  19688. :table in an org-mode table
  19689. :table-special on a special filed in a table
  19690. :table-table in a table.el table
  19691. :clocktable in a clocktable
  19692. :src-block in a source block
  19693. :link on a hyperlink
  19694. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19695. :target on a <<target>>
  19696. :radio-target on a <<<radio-target>>>
  19697. :latex-fragment on a LaTeX fragment
  19698. :latex-preview on a LaTeX fragment with overlaid preview image
  19699. This function expects the position to be visible because it uses font-lock
  19700. faces as a help to recognize the following contexts: :table-special, :link,
  19701. and :keyword."
  19702. (let* ((f (get-text-property (point) 'face))
  19703. (faces (if (listp f) f (list f)))
  19704. (case-fold-search t)
  19705. (p (point)) clist o)
  19706. ;; First the large context
  19707. (cond
  19708. ((org-at-heading-p t)
  19709. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19710. (when (progn
  19711. (beginning-of-line 1)
  19712. (looking-at org-todo-line-tags-regexp))
  19713. (push (org-point-in-group p 1 :headline-stars) clist)
  19714. (push (org-point-in-group p 2 :todo-keyword) clist)
  19715. (push (org-point-in-group p 4 :tags) clist))
  19716. (goto-char p)
  19717. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19718. (if (looking-at "\\[#[A-Z0-9]\\]")
  19719. (push (org-point-in-group p 0 :priority) clist)))
  19720. ((org-at-item-p)
  19721. (push (org-point-in-group p 2 :item-bullet) clist)
  19722. (push (list :item (point-at-bol)
  19723. (save-excursion (org-end-of-item) (point)))
  19724. clist)
  19725. (and (org-at-item-checkbox-p)
  19726. (push (org-point-in-group p 0 :checkbox) clist)))
  19727. ((org-at-table-p)
  19728. (push (list :table (org-table-begin) (org-table-end)) clist)
  19729. (if (memq 'org-formula faces)
  19730. (push (list :table-special
  19731. (previous-single-property-change p 'face)
  19732. (next-single-property-change p 'face)) clist)))
  19733. ((org-at-table-p 'any)
  19734. (push (list :table-table) clist)))
  19735. (goto-char p)
  19736. (let ((case-fold-search t))
  19737. ;; New the "medium" contexts: clocktables, source blocks
  19738. (cond ((org-in-clocktable-p)
  19739. (push (list :clocktable
  19740. (and (or (looking-at "#\\+BEGIN: clocktable")
  19741. (search-backward "#+BEGIN: clocktable" nil t))
  19742. (match-beginning 0))
  19743. (and (re-search-forward "#\\+END:?" nil t)
  19744. (match-end 0))) clist))
  19745. ((org-in-src-block-p)
  19746. (push (list :src-block
  19747. (and (or (looking-at "#\\+BEGIN_SRC")
  19748. (search-backward "#+BEGIN_SRC" nil t))
  19749. (match-beginning 0))
  19750. (and (search-forward "#+END_SRC" nil t)
  19751. (match-beginning 0))) clist))))
  19752. (goto-char p)
  19753. ;; Now the small context
  19754. (cond
  19755. ((org-at-timestamp-p)
  19756. (push (org-point-in-group p 0 :timestamp) clist))
  19757. ((memq 'org-link faces)
  19758. (push (list :link
  19759. (previous-single-property-change p 'face)
  19760. (next-single-property-change p 'face)) clist))
  19761. ((memq 'org-special-keyword faces)
  19762. (push (list :keyword
  19763. (previous-single-property-change p 'face)
  19764. (next-single-property-change p 'face)) clist))
  19765. ((org-at-target-p)
  19766. (push (org-point-in-group p 0 :target) clist)
  19767. (goto-char (1- (match-beginning 0)))
  19768. (if (looking-at org-radio-target-regexp)
  19769. (push (org-point-in-group p 0 :radio-target) clist))
  19770. (goto-char p))
  19771. ((setq o (car (delq nil
  19772. (mapcar
  19773. (lambda (x)
  19774. (if (memq x org-latex-fragment-image-overlays) x))
  19775. (overlays-at (point))))))
  19776. (push (list :latex-fragment
  19777. (overlay-start o) (overlay-end o)) clist)
  19778. (push (list :latex-preview
  19779. (overlay-start o) (overlay-end o)) clist))
  19780. ((org-inside-LaTeX-fragment-p)
  19781. ;; FIXME: positions wrong.
  19782. (push (list :latex-fragment (point) (point)) clist)))
  19783. (setq clist (nreverse (delq nil clist)))
  19784. clist))
  19785. ;; FIXME: Compare with at-regexp-p Do we need both?
  19786. (defun org-in-regexp (re &optional nlines visually)
  19787. "Check if point is inside a match of regexp.
  19788. Normally only the current line is checked, but you can include NLINES extra
  19789. lines both before and after point into the search.
  19790. If VISUALLY is set, require that the cursor is not after the match but
  19791. really on, so that the block visually is on the match."
  19792. (catch 'exit
  19793. (let ((pos (point))
  19794. (eol (point-at-eol (+ 1 (or nlines 0))))
  19795. (inc (if visually 1 0)))
  19796. (save-excursion
  19797. (beginning-of-line (- 1 (or nlines 0)))
  19798. (while (re-search-forward re eol t)
  19799. (if (and (<= (match-beginning 0) pos)
  19800. (>= (+ inc (match-end 0)) pos))
  19801. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19802. (defun org-at-regexp-p (regexp)
  19803. "Is point inside a match of REGEXP in the current line?"
  19804. (catch 'exit
  19805. (save-excursion
  19806. (let ((pos (point)) (end (point-at-eol)))
  19807. (beginning-of-line 1)
  19808. (while (re-search-forward regexp end t)
  19809. (if (and (<= (match-beginning 0) pos)
  19810. (>= (match-end 0) pos))
  19811. (throw 'exit t)))
  19812. nil))))
  19813. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19814. "Non-nil when point is between matches of START-RE and END-RE.
  19815. Also return a non-nil value when point is on one of the matches.
  19816. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19817. buffer positions. Default values are the positions of headlines
  19818. surrounding the point.
  19819. The functions returns a cons cell whose car (resp. cdr) is the
  19820. position before START-RE (resp. after END-RE)."
  19821. (save-match-data
  19822. (let ((pos (point))
  19823. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19824. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19825. beg end)
  19826. (save-excursion
  19827. ;; Point is on a block when on START-RE or if START-RE can be
  19828. ;; found before it...
  19829. (and (or (org-at-regexp-p start-re)
  19830. (re-search-backward start-re limit-up t))
  19831. (setq beg (match-beginning 0))
  19832. ;; ... and END-RE after it...
  19833. (goto-char (match-end 0))
  19834. (re-search-forward end-re limit-down t)
  19835. (> (setq end (match-end 0)) pos)
  19836. ;; ... without another START-RE in-between.
  19837. (goto-char (match-beginning 0))
  19838. (not (re-search-backward start-re (1+ beg) t))
  19839. ;; Return value.
  19840. (cons beg end))))))
  19841. (defun org-in-block-p (names)
  19842. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19843. NAMES is a list of strings containing names of blocks.
  19844. Return first block name matched, or nil. Beware that in case of
  19845. nested blocks, the returned name may not belong to the closest
  19846. block from point."
  19847. (save-match-data
  19848. (catch 'exit
  19849. (let ((case-fold-search t)
  19850. (lim-up (save-excursion (outline-previous-heading)))
  19851. (lim-down (save-excursion (outline-next-heading))))
  19852. (mapc (lambda (name)
  19853. (let ((n (regexp-quote name)))
  19854. (when (org-between-regexps-p
  19855. (concat "^[ \t]*#\\+begin_" n)
  19856. (concat "^[ \t]*#\\+end_" n)
  19857. lim-up lim-down)
  19858. (throw 'exit n))))
  19859. names))
  19860. nil)))
  19861. (defun org-in-drawer-p ()
  19862. "Is point within a drawer?"
  19863. (save-match-data
  19864. (let ((case-fold-search t)
  19865. (lim-up (save-excursion (outline-previous-heading)))
  19866. (lim-down (save-excursion (outline-next-heading))))
  19867. (org-between-regexps-p
  19868. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  19869. "^[ \t]*:end:.*$"
  19870. lim-up lim-down))))
  19871. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19872. "Call `multi-occur' with buffers for all agenda files."
  19873. (interactive "sOrg-files matching: \np")
  19874. (let* ((files (org-agenda-files))
  19875. (tnames (mapcar 'file-truename files))
  19876. (extra org-agenda-text-search-extra-files)
  19877. f)
  19878. (when (eq (car extra) 'agenda-archives)
  19879. (setq extra (cdr extra))
  19880. (setq files (org-add-archive-files files)))
  19881. (while (setq f (pop extra))
  19882. (unless (member (file-truename f) tnames)
  19883. (add-to-list 'files f 'append)
  19884. (add-to-list 'tnames (file-truename f) 'append)))
  19885. (multi-occur
  19886. (mapcar (lambda (x)
  19887. (with-current-buffer
  19888. (or (get-file-buffer x) (find-file-noselect x))
  19889. (widen)
  19890. (current-buffer)))
  19891. files)
  19892. regexp)))
  19893. (if (boundp 'occur-mode-find-occurrence-hook)
  19894. ;; Emacs 23
  19895. (add-hook 'occur-mode-find-occurrence-hook
  19896. (lambda ()
  19897. (when (derived-mode-p 'org-mode)
  19898. (org-reveal))))
  19899. ;; Emacs 22
  19900. (defadvice occur-mode-goto-occurrence
  19901. (after org-occur-reveal activate)
  19902. (and (derived-mode-p 'org-mode) (org-reveal)))
  19903. (defadvice occur-mode-goto-occurrence-other-window
  19904. (after org-occur-reveal activate)
  19905. (and (derived-mode-p 'org-mode) (org-reveal)))
  19906. (defadvice occur-mode-display-occurrence
  19907. (after org-occur-reveal activate)
  19908. (when (derived-mode-p 'org-mode)
  19909. (let ((pos (occur-mode-find-occurrence)))
  19910. (with-current-buffer (marker-buffer pos)
  19911. (save-excursion
  19912. (goto-char pos)
  19913. (org-reveal)))))))
  19914. (defun org-occur-link-in-agenda-files ()
  19915. "Create a link and search for it in the agendas.
  19916. The link is not stored in `org-stored-links', it is just created
  19917. for the search purpose."
  19918. (interactive)
  19919. (let ((link (condition-case nil
  19920. (org-store-link nil)
  19921. (error "Unable to create a link to here"))))
  19922. (org-occur-in-agenda-files (regexp-quote link))))
  19923. (defun org-reverse-string (string)
  19924. "Return the reverse of STRING."
  19925. (apply 'string (reverse (string-to-list string))))
  19926. (defsubst org-uniquify (list)
  19927. "Non-destructively remove duplicate elements from LIST."
  19928. (let ((res (copy-sequence list))) (delete-dups res)))
  19929. (defun org-uniquify-alist (alist)
  19930. "Merge elements of ALIST with the same key.
  19931. For example, in this alist:
  19932. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19933. => '((a 1 3) (b 2))
  19934. merge (a 1) and (a 3) into (a 1 3).
  19935. The function returns the new ALIST."
  19936. (let (rtn)
  19937. (mapc
  19938. (lambda (e)
  19939. (let (n)
  19940. (if (not (assoc (car e) rtn))
  19941. (push e rtn)
  19942. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19943. (setq rtn (assq-delete-all (car e) rtn))
  19944. (push n rtn))))
  19945. alist)
  19946. rtn))
  19947. (defun org-delete-all (elts list)
  19948. "Remove all elements in ELTS from LIST."
  19949. (while elts
  19950. (setq list (delete (pop elts) list)))
  19951. list)
  19952. (defun org-count (cl-item cl-seq)
  19953. "Count the number of occurrences of ITEM in SEQ.
  19954. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19955. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19956. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19957. (while (< cl-start cl-end)
  19958. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19959. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19960. (setq cl-start (1+ cl-start)))
  19961. cl-count))
  19962. (defun org-remove-if (predicate seq)
  19963. "Remove everything from SEQ that fulfills PREDICATE."
  19964. (let (res e)
  19965. (while seq
  19966. (setq e (pop seq))
  19967. (if (not (funcall predicate e)) (push e res)))
  19968. (nreverse res)))
  19969. (defun org-remove-if-not (predicate seq)
  19970. "Remove everything from SEQ that does not fulfill PREDICATE."
  19971. (let (res e)
  19972. (while seq
  19973. (setq e (pop seq))
  19974. (if (funcall predicate e) (push e res)))
  19975. (nreverse res)))
  19976. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19977. "Reduce two-argument FUNCTION across SEQ.
  19978. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19979. \":initial-value\" removed."
  19980. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19981. (cadr (memq :initial-value cl-keys)))
  19982. (cl-seq (pop cl-seq))
  19983. (t (funcall cl-func)))))
  19984. (while cl-seq
  19985. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19986. cl-accum))
  19987. (defun org-back-over-empty-lines ()
  19988. "Move backwards over whitespace, to the beginning of the first empty line.
  19989. Returns the number of empty lines passed."
  19990. (let ((pos (point)))
  19991. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19992. (skip-chars-backward " \t\n\r")
  19993. (unless (eobp)
  19994. (forward-line -1)))
  19995. (beginning-of-line 2)
  19996. (goto-char (min (point) pos))
  19997. (count-lines (point) pos)))
  19998. (defun org-skip-whitespace ()
  19999. (skip-chars-forward " \t\n\r"))
  20000. (defun org-point-in-group (point group &optional context)
  20001. "Check if POINT is in match-group GROUP.
  20002. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20003. match. If the match group does not exist or point is not inside it,
  20004. return nil."
  20005. (and (match-beginning group)
  20006. (>= point (match-beginning group))
  20007. (<= point (match-end group))
  20008. (if context
  20009. (list context (match-beginning group) (match-end group))
  20010. t)))
  20011. (defun org-switch-to-buffer-other-window (&rest args)
  20012. "Switch to buffer in a second window on the current frame.
  20013. In particular, do not allow pop-up frames.
  20014. Returns the newly created buffer."
  20015. (org-no-popups
  20016. (apply 'switch-to-buffer-other-window args)))
  20017. (defun org-combine-plists (&rest plists)
  20018. "Create a single property list from all plists in PLISTS.
  20019. The process starts by copying the first list, and then setting properties
  20020. from the other lists. Settings in the last list are the most significant
  20021. ones and overrule settings in the other lists."
  20022. (let ((rtn (copy-sequence (pop plists)))
  20023. p v ls)
  20024. (while plists
  20025. (setq ls (pop plists))
  20026. (while ls
  20027. (setq p (pop ls) v (pop ls))
  20028. (setq rtn (plist-put rtn p v))))
  20029. rtn))
  20030. (defun org-replace-escapes (string table)
  20031. "Replace %-escapes in STRING with values in TABLE.
  20032. TABLE is an association list with keys like \"%a\" and string values.
  20033. The sequences in STRING may contain normal field width and padding information,
  20034. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20035. so values can contain further %-escapes if they are define later in TABLE."
  20036. (let ((tbl (copy-alist table))
  20037. (case-fold-search nil)
  20038. (pchg 0)
  20039. e re rpl)
  20040. (while (setq e (pop tbl))
  20041. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20042. (when (and (cdr e) (string-match re (cdr e)))
  20043. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20044. (safe "SREF"))
  20045. (add-text-properties 0 3 (list 'sref sref) safe)
  20046. (setcdr e (replace-match safe t t (cdr e)))))
  20047. (while (string-match re string)
  20048. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20049. (cdr e)))
  20050. (setq string (replace-match rpl t t string))))
  20051. (while (setq pchg (next-property-change pchg string))
  20052. (let ((sref (get-text-property pchg 'sref string)))
  20053. (when (and sref (string-match "SREF" string pchg))
  20054. (setq string (replace-match sref t t string)))))
  20055. string))
  20056. (defun org-sublist (list start end)
  20057. "Return a section of LIST, from START to END.
  20058. Counting starts at 1."
  20059. (let (rtn (c start))
  20060. (setq list (nthcdr (1- start) list))
  20061. (while (and list (<= c end))
  20062. (push (pop list) rtn)
  20063. (setq c (1+ c)))
  20064. (nreverse rtn)))
  20065. (defun org-find-base-buffer-visiting (file)
  20066. "Like `find-buffer-visiting' but always return the base buffer and
  20067. not an indirect buffer."
  20068. (let ((buf (or (get-file-buffer file)
  20069. (find-buffer-visiting file))))
  20070. (if buf
  20071. (or (buffer-base-buffer buf) buf)
  20072. nil)))
  20073. (defun org-image-file-name-regexp (&optional extensions)
  20074. "Return regexp matching the file names of images.
  20075. If EXTENSIONS is given, only match these."
  20076. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20077. (image-file-name-regexp)
  20078. (let ((image-file-name-extensions
  20079. (or extensions
  20080. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20081. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20082. (concat "\\."
  20083. (regexp-opt (nconc (mapcar 'upcase
  20084. image-file-name-extensions)
  20085. image-file-name-extensions)
  20086. t)
  20087. "\\'"))))
  20088. (defun org-file-image-p (file &optional extensions)
  20089. "Return non-nil if FILE is an image."
  20090. (save-match-data
  20091. (string-match (org-image-file-name-regexp extensions) file)))
  20092. (defun org-get-cursor-date (&optional with-time)
  20093. "Return the date at cursor in as a time.
  20094. This works in the calendar and in the agenda, anywhere else it just
  20095. returns the current time.
  20096. If WITH-TIME is non-nil, returns the time of the event at point (in
  20097. the agenda) or the current time of the day."
  20098. (let (date day defd tp tm hod mod)
  20099. (when with-time
  20100. (setq tp (get-text-property (point) 'time))
  20101. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20102. (setq hod (string-to-number (match-string 1 tp))
  20103. mod (string-to-number (match-string 2 tp))))
  20104. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20105. mod (nth 1 (decode-time (current-time))))))
  20106. (cond
  20107. ((eq major-mode 'calendar-mode)
  20108. (setq date (calendar-cursor-to-date)
  20109. defd (encode-time 0 (or mod 0) (or hod 0)
  20110. (nth 1 date) (nth 0 date) (nth 2 date))))
  20111. ((eq major-mode 'org-agenda-mode)
  20112. (setq day (get-text-property (point) 'day))
  20113. (if day
  20114. (setq date (calendar-gregorian-from-absolute day)
  20115. defd (encode-time 0 (or mod 0) (or hod 0)
  20116. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20117. (or defd (current-time))))
  20118. (defun org-mark-subtree (&optional up)
  20119. "Mark the current subtree.
  20120. This puts point at the start of the current subtree, and mark at
  20121. the end. If a numeric prefix UP is given, move up into the
  20122. hierarchy of headlines by UP levels before marking the subtree."
  20123. (interactive "P")
  20124. (org-with-limited-levels
  20125. (cond ((org-at-heading-p) (beginning-of-line))
  20126. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20127. (t (outline-previous-visible-heading 1))))
  20128. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20129. (if (org-called-interactively-p 'any)
  20130. (call-interactively 'org-mark-element)
  20131. (org-mark-element)))
  20132. ;;; Indentation
  20133. (defun org-indent-line ()
  20134. "Indent line depending on context."
  20135. (interactive)
  20136. (let* ((pos (point))
  20137. (itemp (org-at-item-p))
  20138. (case-fold-search t)
  20139. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20140. (inline-task-p (and (featurep 'org-inlinetask)
  20141. (org-inlinetask-in-task-p)))
  20142. (inline-re (and inline-task-p
  20143. (org-inlinetask-outline-regexp)))
  20144. column)
  20145. (if (and orgstruct-is-++ (eq pos (point)))
  20146. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20147. (indent-according-to-mode))
  20148. (beginning-of-line 1)
  20149. (cond
  20150. ;; Headings
  20151. ((looking-at org-outline-regexp) (setq column 0))
  20152. ;; Footnote definition
  20153. ((looking-at org-footnote-definition-re) (setq column 0))
  20154. ;; Literal examples
  20155. ((looking-at "[ \t]*:\\( \\|$\\)")
  20156. (setq column (org-get-indentation))) ; do nothing
  20157. ;; Lists
  20158. ((ignore-errors (goto-char (org-in-item-p)))
  20159. (setq column (if itemp
  20160. (org-get-indentation)
  20161. (org-list-item-body-column (point))))
  20162. (goto-char pos))
  20163. ;; Drawers
  20164. ((and (looking-at "[ \t]*:END:")
  20165. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20166. (save-excursion
  20167. (goto-char (1- (match-beginning 1)))
  20168. (setq column (current-column))))
  20169. ;; Special blocks
  20170. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20171. (save-excursion
  20172. (re-search-backward
  20173. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20174. (setq column (org-get-indentation (match-string 0))))
  20175. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20176. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20177. (save-excursion
  20178. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20179. (setq column
  20180. (cond ((equal (downcase (match-string 1)) "src")
  20181. ;; src blocks: let `org-edit-src-exit' handle them
  20182. (org-get-indentation))
  20183. ((equal (downcase (match-string 1)) "example")
  20184. (max (org-get-indentation)
  20185. (org-get-indentation (match-string 0))))
  20186. (t
  20187. (org-get-indentation (match-string 0))))))
  20188. ;; This line has nothing special, look at the previous relevant
  20189. ;; line to compute indentation
  20190. (t
  20191. (beginning-of-line 0)
  20192. (while (and (not (bobp))
  20193. (not (looking-at org-table-line-regexp))
  20194. (not (looking-at org-drawer-regexp))
  20195. ;; When point started in an inline task, do not move
  20196. ;; above task starting line.
  20197. (not (and inline-task-p (looking-at inline-re)))
  20198. ;; Skip drawers, blocks, empty lines, verbatim,
  20199. ;; comments, tables, footnotes definitions, lists,
  20200. ;; inline tasks.
  20201. (or (and (looking-at "[ \t]*:END:")
  20202. (re-search-backward org-drawer-regexp nil t))
  20203. (and (looking-at "[ \t]*#\\+end_")
  20204. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20205. (looking-at "[ \t]*[\n:#|]")
  20206. (looking-at org-footnote-definition-re)
  20207. (and (not inline-task-p)
  20208. (featurep 'org-inlinetask)
  20209. (org-inlinetask-in-task-p)
  20210. (or (org-inlinetask-goto-beginning) t))))
  20211. (beginning-of-line 0))
  20212. (cond
  20213. ;; There was a list item above.
  20214. ((save-excursion
  20215. (and (ignore-errors (goto-char (org-in-item-p)))
  20216. (goto-char
  20217. (org-list-get-top-point (org-list-struct)))))
  20218. (looking-at org-list-full-item-re)
  20219. (setq column (length (match-string 0))))
  20220. ;; There was an heading above.
  20221. ((looking-at "\\*+[ \t]+")
  20222. (if (not org-adapt-indentation)
  20223. (setq column 0)
  20224. (goto-char (match-end 0))
  20225. (setq column (current-column))))
  20226. ;; A drawer had started and is unfinished
  20227. ((looking-at org-drawer-regexp)
  20228. (goto-char (1- (match-beginning 1)))
  20229. (setq column (current-column)))
  20230. ;; Else, nothing noticeable found: get indentation and go on.
  20231. (t (setq column (org-get-indentation))))))
  20232. ;; Now apply indentation and move cursor accordingly
  20233. (goto-char pos)
  20234. (if (<= (current-column) (current-indentation))
  20235. (org-indent-line-to column)
  20236. (save-excursion (org-indent-line-to column)))
  20237. ;; Special polishing for properties, see `org-property-format'
  20238. (setq column (current-column))
  20239. (beginning-of-line 1)
  20240. (if (looking-at
  20241. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20242. (replace-match (concat (match-string 1)
  20243. (format org-property-format
  20244. (match-string 2) (match-string 3)))
  20245. t t))
  20246. (org-move-to-column column))))
  20247. (defun org-indent-drawer ()
  20248. "Indent the drawer at point."
  20249. (interactive)
  20250. (let ((p (point))
  20251. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20252. (match-end 0)))
  20253. (folded
  20254. (save-excursion
  20255. (end-of-line)
  20256. (when (overlays-at (point))
  20257. (member 'invisible (overlay-properties
  20258. (car (overlays-at (point)))))))))
  20259. (when folded (org-cycle))
  20260. (indent-for-tab-command)
  20261. (while (and (move-beginning-of-line 2) (< (point) e))
  20262. (indent-for-tab-command))
  20263. (goto-char p)
  20264. (when folded (org-cycle)))
  20265. (message "Drawer at point indented"))
  20266. (defun org-indent-block ()
  20267. "Indent the block at point."
  20268. (interactive)
  20269. (let ((p (point))
  20270. (case-fold-search t)
  20271. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20272. (match-end 0)))
  20273. (folded
  20274. (save-excursion
  20275. (end-of-line)
  20276. (when (overlays-at (point))
  20277. (member 'invisible (overlay-properties
  20278. (car (overlays-at (point)))))))))
  20279. (when folded (org-cycle))
  20280. (indent-for-tab-command)
  20281. (while (and (move-beginning-of-line 2) (< (point) e))
  20282. (indent-for-tab-command))
  20283. (goto-char p)
  20284. (when folded (org-cycle)))
  20285. (message "Block at point indented"))
  20286. (defun org-indent-region (start end)
  20287. "Indent region."
  20288. (interactive "r")
  20289. (save-excursion
  20290. (let ((line-end (org-current-line end)))
  20291. (goto-char start)
  20292. (while (< (org-current-line) line-end)
  20293. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20294. (t (call-interactively 'org-indent-line)))
  20295. (move-beginning-of-line 2)))))
  20296. ;;; Filling
  20297. ;; We use our own fill-paragraph and auto-fill functions.
  20298. ;; `org-fill-paragraph' relies on adaptive filling and context
  20299. ;; checking. Appropriate `fill-prefix' is computed with
  20300. ;; `org-adaptive-fill-function'.
  20301. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20302. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20303. ;; `org-adaptive-fill-function' value. Internally,
  20304. ;; `org-comment-line-break-function' breaks the line.
  20305. ;; `org-setup-filling' installs filling and auto-filling related
  20306. ;; variables during `org-mode' initialization.
  20307. (defun org-setup-filling ()
  20308. (interactive)
  20309. ;; Prevent auto-fill from inserting unwanted new items.
  20310. (when (boundp 'fill-nobreak-predicate)
  20311. (org-set-local
  20312. 'fill-nobreak-predicate
  20313. (org-uniquify
  20314. (append fill-nobreak-predicate
  20315. '(org-fill-paragraph-separate-nobreak-p
  20316. org-fill-line-break-nobreak-p
  20317. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20318. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20319. (org-set-local 'auto-fill-inhibit-regexp nil)
  20320. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20321. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20322. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20323. (defvar org-element-paragraph-separate) ; org-element.el
  20324. (defun org-fill-paragraph-separate-nobreak-p ()
  20325. "Non-nil when a line break at point would insert a new item."
  20326. (looking-at (substring org-element-paragraph-separate 1)))
  20327. (defun org-fill-line-break-nobreak-p ()
  20328. "Non-nil when a line break at point would create an Org line break."
  20329. (save-excursion
  20330. (skip-chars-backward "[ \t]")
  20331. (skip-chars-backward "\\\\")
  20332. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20333. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20334. "Non-nil when a line break at point would insert a new item."
  20335. (and (org-at-timestamp-p t)
  20336. (not (looking-at org-ts-regexp-both))))
  20337. (declare-function message-in-body-p "message" ())
  20338. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20339. (defun org-adaptive-fill-function ()
  20340. "Compute a fill prefix for the current line.
  20341. Return fill prefix, as a string, or nil if current line isn't
  20342. meant to be filled."
  20343. (let (prefix)
  20344. (catch 'exit
  20345. (when (derived-mode-p 'message-mode)
  20346. (save-excursion
  20347. (beginning-of-line)
  20348. (cond ((or (not (message-in-body-p))
  20349. (looking-at orgtbl-line-start-regexp))
  20350. (throw 'exit nil))
  20351. ((looking-at message-cite-prefix-regexp)
  20352. (throw 'exit (match-string-no-properties 0)))
  20353. ((looking-at org-outline-regexp)
  20354. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20355. (org-with-wide-buffer
  20356. (let* ((p (line-beginning-position))
  20357. (element (save-excursion
  20358. (beginning-of-line)
  20359. (or (ignore-errors (org-element-at-point))
  20360. (user-error "An element cannot be parsed line %d"
  20361. (line-number-at-pos (point))))))
  20362. (type (org-element-type element))
  20363. (post-affiliated (org-element-property :post-affiliated element)))
  20364. (unless (and post-affiliated (< p post-affiliated))
  20365. (case type
  20366. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20367. (footnote-definition "")
  20368. ((item plain-list)
  20369. (make-string (org-list-item-body-column
  20370. (or post-affiliated
  20371. (org-element-property :begin element)))
  20372. ? ))
  20373. (paragraph
  20374. ;; Fill prefix is usually the same as the current line,
  20375. ;; except if the paragraph is at the beginning of an item.
  20376. (let ((parent (org-element-property :parent element)))
  20377. (cond ((eq (org-element-type parent) 'item)
  20378. (make-string (org-list-item-body-column
  20379. (org-element-property :begin parent))
  20380. ? ))
  20381. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20382. (match-string 0))
  20383. (t ""))))
  20384. (comment-block
  20385. ;; Only fill contents if P is within block boundaries.
  20386. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20387. (forward-line)
  20388. (point)))
  20389. (cend (save-excursion
  20390. (goto-char (org-element-property :end element))
  20391. (skip-chars-backward " \r\t\n")
  20392. (line-beginning-position))))
  20393. (when (and (>= p cbeg) (< p cend))
  20394. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20395. (match-string 0)
  20396. "")))))))))))
  20397. (declare-function message-goto-body "message" ())
  20398. (defvar message-cite-prefix-regexp) ; From message.el
  20399. (defun org-fill-paragraph (&optional justify)
  20400. "Fill element at point, when applicable.
  20401. This function only applies to comment blocks, comments, example
  20402. blocks and paragraphs. Also, as a special case, re-align table
  20403. when point is at one.
  20404. If JUSTIFY is non-nil (interactively, with prefix argument),
  20405. justify as well. If `sentence-end-double-space' is non-nil, then
  20406. period followed by one space does not end a sentence, so don't
  20407. break a line there. The variable `fill-column' controls the
  20408. width for filling.
  20409. For convenience, when point is at a plain list, an item or
  20410. a footnote definition, try to fill the first paragraph within."
  20411. (interactive)
  20412. (if (and (derived-mode-p 'message-mode)
  20413. (or (not (message-in-body-p))
  20414. (save-excursion (move-beginning-of-line 1)
  20415. (looking-at message-cite-prefix-regexp))))
  20416. ;; First ensure filling is correct in message-mode.
  20417. (let ((fill-paragraph-function
  20418. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20419. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20420. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20421. (paragraph-separate
  20422. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20423. (fill-paragraph nil))
  20424. (with-syntax-table org-mode-transpose-word-syntax-table
  20425. ;; Move to end of line in order to get the first paragraph
  20426. ;; within a plain list or a footnote definition.
  20427. (let ((element (save-excursion
  20428. (end-of-line)
  20429. (or (ignore-errors (org-element-at-point))
  20430. (user-error "An element cannot be parsed line %d"
  20431. (line-number-at-pos (point)))))))
  20432. ;; First check if point is in a blank line at the beginning of
  20433. ;; the buffer. In that case, ignore filling.
  20434. (case (org-element-type element)
  20435. ;; Use major mode filling function is src blocks.
  20436. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20437. ;; Align Org tables, leave table.el tables as-is.
  20438. (table-row (org-table-align) t)
  20439. (table
  20440. (when (eq (org-element-property :type element) 'org)
  20441. (save-excursion
  20442. (goto-char (org-element-property :post-affiliated element))
  20443. (org-table-align)))
  20444. t)
  20445. (paragraph
  20446. ;; Paragraphs may contain `line-break' type objects.
  20447. (let ((beg (max (point-min)
  20448. (org-element-property :contents-begin element)))
  20449. (end (min (point-max)
  20450. (org-element-property :contents-end element))))
  20451. ;; Do nothing if point is at an affiliated keyword.
  20452. (if (< (line-end-position) beg) t
  20453. (when (derived-mode-p 'message-mode)
  20454. ;; In `message-mode', do not fill following citation
  20455. ;; in current paragraph nor text before message body.
  20456. (let ((body-start (save-excursion (message-goto-body))))
  20457. (when body-start (setq beg (max body-start beg))))
  20458. (when (save-excursion
  20459. (re-search-forward
  20460. (concat "^" message-cite-prefix-regexp) end t))
  20461. (setq end (match-beginning 0))))
  20462. ;; Fill paragraph, taking line breaks into account.
  20463. ;; For that, slice the paragraph using line breaks as
  20464. ;; separators, and fill the parts in reverse order to
  20465. ;; avoid messing with markers.
  20466. (save-excursion
  20467. (goto-char end)
  20468. (mapc
  20469. (lambda (pos)
  20470. (fill-region-as-paragraph pos (point) justify)
  20471. (goto-char pos))
  20472. ;; Find the list of ending positions for line breaks
  20473. ;; in the current paragraph. Add paragraph
  20474. ;; beginning to include first slice.
  20475. (nreverse
  20476. (cons beg
  20477. (org-element-map
  20478. (org-element--parse-objects
  20479. beg end nil (org-element-restriction 'paragraph))
  20480. 'line-break
  20481. (lambda (lb) (org-element-property :end lb)))))))
  20482. t)))
  20483. ;; Contents of `comment-block' type elements should be
  20484. ;; filled as plain text, but only if point is within block
  20485. ;; markers.
  20486. (comment-block
  20487. (let* ((case-fold-search t)
  20488. (beg (save-excursion
  20489. (goto-char (org-element-property :begin element))
  20490. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20491. (forward-line)
  20492. (point)))
  20493. (end (save-excursion
  20494. (goto-char (org-element-property :end element))
  20495. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20496. (line-beginning-position))))
  20497. (when (and (>= (point) beg) (< (point) end))
  20498. (fill-region-as-paragraph
  20499. (save-excursion
  20500. (end-of-line)
  20501. (re-search-backward "^[ \t]*$" beg 'move)
  20502. (line-beginning-position))
  20503. (save-excursion
  20504. (beginning-of-line)
  20505. (re-search-forward "^[ \t]*$" end 'move)
  20506. (line-beginning-position))
  20507. justify)))
  20508. t)
  20509. ;; Fill comments.
  20510. (comment (fill-comment-paragraph justify))
  20511. ;; Ignore every other element.
  20512. (otherwise t))))))
  20513. (defun org-auto-fill-function ()
  20514. "Auto-fill function."
  20515. ;; Check if auto-filling is meaningful.
  20516. (let ((fc (current-fill-column)))
  20517. (when (and fc (> (current-column) fc))
  20518. (let* ((fill-prefix (org-adaptive-fill-function))
  20519. ;; Enforce empty fill prefix, if required. Otherwise, it
  20520. ;; will be computed again.
  20521. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20522. (when fill-prefix (do-auto-fill))))))
  20523. (defun org-comment-line-break-function (&optional soft)
  20524. "Break line at point and indent, continuing comment if within one.
  20525. The inserted newline is marked hard if variable
  20526. `use-hard-newlines' is true, unless optional argument SOFT is
  20527. non-nil."
  20528. (if soft (insert-and-inherit ?\n) (newline 1))
  20529. (save-excursion (forward-char -1) (delete-horizontal-space))
  20530. (delete-horizontal-space)
  20531. (indent-to-left-margin)
  20532. (insert-before-markers-and-inherit fill-prefix))
  20533. ;;; Comments
  20534. ;; Org comments syntax is quite complex. It requires the entire line
  20535. ;; to be just a comment. Also, even with the right syntax at the
  20536. ;; beginning of line, some some elements (i.e. verse-block or
  20537. ;; example-block) don't accept comments. Usual Emacs comment commands
  20538. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20539. ;; Org still relies on `comment-dwim', but cannot trust
  20540. ;; `comment-only-p'. So, `comment-region-function' and
  20541. ;; `uncomment-region-function' both point
  20542. ;; to`org-comment-or-uncomment-region'. Eventually,
  20543. ;; `org-insert-comment' takes care of insertion of comments at the
  20544. ;; beginning of line.
  20545. ;; `org-setup-comments-handling' install comments related variables
  20546. ;; during `org-mode' initialization.
  20547. (defun org-setup-comments-handling ()
  20548. (interactive)
  20549. (org-set-local 'comment-use-syntax nil)
  20550. (org-set-local 'comment-start "# ")
  20551. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20552. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20553. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20554. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20555. (defun org-insert-comment ()
  20556. "Insert an empty comment above current line.
  20557. If the line is empty, insert comment at its beginning."
  20558. (beginning-of-line)
  20559. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20560. (org-indent-line)
  20561. (insert "# "))
  20562. (defvar comment-empty-lines) ; From newcomment.el.
  20563. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20564. "Comment or uncomment each non-blank line in the region.
  20565. Uncomment each non-blank line between BEG and END if it only
  20566. contains commented lines. Otherwise, comment them."
  20567. (save-restriction
  20568. ;; Restrict region
  20569. (narrow-to-region (save-excursion (goto-char beg)
  20570. (skip-chars-forward " \r\t\n" end)
  20571. (line-beginning-position))
  20572. (save-excursion (goto-char end)
  20573. (skip-chars-backward " \r\t\n" beg)
  20574. (line-end-position)))
  20575. (let ((uncommentp
  20576. ;; UNCOMMENTP is non-nil when every non blank line between
  20577. ;; BEG and END is a comment.
  20578. (save-excursion
  20579. (goto-char (point-min))
  20580. (while (and (not (eobp))
  20581. (let ((element (org-element-at-point)))
  20582. (and (eq (org-element-type element) 'comment)
  20583. (goto-char (min (point-max)
  20584. (org-element-property
  20585. :end element)))))))
  20586. (eobp))))
  20587. (if uncommentp
  20588. ;; Only blank lines and comments in region: uncomment it.
  20589. (save-excursion
  20590. (goto-char (point-min))
  20591. (while (not (eobp))
  20592. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20593. (replace-match "" nil nil nil 1))
  20594. (forward-line)))
  20595. ;; Comment each line in region.
  20596. (let ((min-indent (point-max)))
  20597. ;; First find the minimum indentation across all lines.
  20598. (save-excursion
  20599. (goto-char (point-min))
  20600. (while (and (not (eobp)) (not (zerop min-indent)))
  20601. (unless (looking-at "[ \t]*$")
  20602. (setq min-indent (min min-indent (current-indentation))))
  20603. (forward-line)))
  20604. ;; Then loop over all lines.
  20605. (save-excursion
  20606. (goto-char (point-min))
  20607. (while (not (eobp))
  20608. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20609. ;; Don't get fooled by invisible text (e.g. link path)
  20610. ;; when moving to column MIN-INDENT.
  20611. (let ((buffer-invisibility-spec nil))
  20612. (org-move-to-column min-indent t))
  20613. (insert comment-start))
  20614. (forward-line))))))))
  20615. ;;; Planning
  20616. ;; This section contains tools to operate on timestamp objects, as
  20617. ;; returned by, e.g. `org-element-context'.
  20618. (defun org-timestamp-has-time-p (timestamp)
  20619. "Non-nil when TIMESTAMP has a time specified."
  20620. (org-element-property :hour-start timestamp))
  20621. (defun org-timestamp-format (timestamp format &optional end utc)
  20622. "Format a TIMESTAMP element into a string.
  20623. FORMAT is a format specifier to be passed to
  20624. `format-time-string'.
  20625. When optional argument END is non-nil, use end of date-range or
  20626. time-range, if possible.
  20627. When optional argument UTC is non-nil, time will be expressed as
  20628. Universal Time."
  20629. (format-time-string
  20630. format
  20631. (apply 'encode-time
  20632. (cons 0
  20633. (mapcar
  20634. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20635. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20636. '(:minute-start :hour-start :day-start :month-start
  20637. :year-start)))))
  20638. utc))
  20639. (defun org-timestamp-split-range (timestamp &optional end)
  20640. "Extract a timestamp object from a date or time range.
  20641. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20642. the end of the range. Otherwise, extract its start.
  20643. Return a new timestamp object sharing the same parent as
  20644. TIMESTAMP."
  20645. (let ((type (org-element-property :type timestamp)))
  20646. (if (memq type '(active inactive diary)) timestamp
  20647. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20648. ;; Set new type.
  20649. (org-element-put-property
  20650. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20651. ;; Copy start properties over end properties if END is
  20652. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20653. (let ((p-alist '((:minute-start . :minute-end)
  20654. (:hour-start . :hour-end)
  20655. (:day-start . :day-end)
  20656. (:month-start . :month-end)
  20657. (:year-start . :year-end))))
  20658. (dolist (p-cell p-alist)
  20659. (org-element-put-property
  20660. split-ts
  20661. (funcall (if end 'car 'cdr) p-cell)
  20662. (org-element-property
  20663. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20664. ;; Eventually refresh `:raw-value'.
  20665. (org-element-put-property split-ts :raw-value nil)
  20666. (org-element-put-property
  20667. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20668. (defun org-timestamp-translate (timestamp &optional boundary)
  20669. "Apply `org-translate-time' on a TIMESTAMP object.
  20670. When optional argument BOUNDARY is non-nil, it is either the
  20671. symbol `start' or `end'. In this case, only translate the
  20672. starting or ending part of TIMESTAMP if it is a date or time
  20673. range. Otherwise, translate both parts."
  20674. (if (and (not boundary)
  20675. (memq (org-element-property :type timestamp)
  20676. '(active-range inactive-range)))
  20677. (concat
  20678. (org-translate-time
  20679. (org-element-property :raw-value
  20680. (org-timestamp-split-range timestamp)))
  20681. "--"
  20682. (org-translate-time
  20683. (org-element-property :raw-value
  20684. (org-timestamp-split-range timestamp t))))
  20685. (org-translate-time
  20686. (org-element-property
  20687. :raw-value
  20688. (if (not boundary) timestamp
  20689. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20690. ;;; Other stuff.
  20691. (defun org-toggle-fixed-width-section (arg)
  20692. "Toggle the fixed-width export.
  20693. If there is no active region, the QUOTE keyword at the current headline is
  20694. inserted or removed. When present, it causes the text between this headline
  20695. and the next to be exported as fixed-width text, and unmodified.
  20696. If there is an active region, this command adds or removes a colon as the
  20697. first character of this line. If the first character of a line is a colon,
  20698. this line is also exported in fixed-width font."
  20699. (interactive "P")
  20700. (let* ((cc 0)
  20701. (regionp (org-region-active-p))
  20702. (beg (if regionp (region-beginning) (point)))
  20703. (end (if regionp (region-end)))
  20704. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20705. (case-fold-search nil)
  20706. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20707. off)
  20708. (if regionp
  20709. (save-excursion
  20710. (goto-char beg)
  20711. (setq cc (current-column))
  20712. (beginning-of-line 1)
  20713. (setq off (looking-at re))
  20714. (while (> nlines 0)
  20715. (setq nlines (1- nlines))
  20716. (beginning-of-line 1)
  20717. (cond
  20718. (arg
  20719. (org-move-to-column cc t)
  20720. (insert ": \n")
  20721. (forward-line -1))
  20722. ((and off (looking-at re))
  20723. (replace-match "" t t nil 1))
  20724. ((not off) (org-move-to-column cc t) (insert ": ")))
  20725. (forward-line 1)))
  20726. (save-excursion
  20727. (org-back-to-heading)
  20728. (cond
  20729. ((looking-at (format org-heading-keyword-regexp-format
  20730. org-quote-string))
  20731. (goto-char (match-end 1))
  20732. (looking-at (concat " +" org-quote-string))
  20733. (replace-match "" t t)
  20734. (when (eolp) (insert " ")))
  20735. ((looking-at org-outline-regexp)
  20736. (goto-char (match-end 0))
  20737. (insert org-quote-string " ")))))))
  20738. (defun org-reftex-citation ()
  20739. "Use reftex-citation to insert a citation into the buffer.
  20740. This looks for a line like
  20741. #+BIBLIOGRAPHY: foo plain option:-d
  20742. and derives from it that foo.bib is the bibliography file relevant
  20743. for this document. It then installs the necessary environment for RefTeX
  20744. to work in this buffer and calls `reftex-citation' to insert a citation
  20745. into the buffer.
  20746. Export of such citations to both LaTeX and HTML is handled by the contributed
  20747. package org-exp-bibtex by Taru Karttunen."
  20748. (interactive)
  20749. (let ((reftex-docstruct-symbol 'rds)
  20750. (reftex-cite-format "\\cite{%l}")
  20751. rds bib)
  20752. (save-excursion
  20753. (save-restriction
  20754. (widen)
  20755. (let ((case-fold-search t)
  20756. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20757. (if (not (save-excursion
  20758. (or (re-search-forward re nil t)
  20759. (re-search-backward re nil t))))
  20760. (error "No bibliography defined in file")
  20761. (setq bib (concat (match-string 1) ".bib")
  20762. rds (list (list 'bib bib)))))))
  20763. (call-interactively 'reftex-citation)))
  20764. ;;;; Functions extending outline functionality
  20765. (defun org-beginning-of-line (&optional arg)
  20766. "Go to the beginning of the current line. If that is invisible, continue
  20767. to a visible line beginning. This makes the function of C-a more intuitive.
  20768. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20769. first attempt, and only move to after the tags when the cursor is already
  20770. beyond the end of the headline."
  20771. (interactive "P")
  20772. (let ((pos (point))
  20773. (special (if (consp org-special-ctrl-a/e)
  20774. (car org-special-ctrl-a/e)
  20775. org-special-ctrl-a/e))
  20776. refpos)
  20777. (if (org-bound-and-true-p visual-line-mode)
  20778. (beginning-of-visual-line 1)
  20779. (beginning-of-line 1))
  20780. (if (and arg (fboundp 'move-beginning-of-line))
  20781. (call-interactively 'move-beginning-of-line)
  20782. (if (bobp)
  20783. nil
  20784. (backward-char 1)
  20785. (if (org-truely-invisible-p)
  20786. (while (and (not (bobp)) (org-truely-invisible-p))
  20787. (backward-char 1)
  20788. (beginning-of-line 1))
  20789. (forward-char 1))))
  20790. (when special
  20791. (cond
  20792. ((and (looking-at org-complex-heading-regexp)
  20793. (= (char-after (match-end 1)) ?\ ))
  20794. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20795. (point-at-eol)))
  20796. (goto-char
  20797. (if (eq special t)
  20798. (cond ((> pos refpos) refpos)
  20799. ((= pos (point)) refpos)
  20800. (t (point)))
  20801. (cond ((> pos (point)) (point))
  20802. ((not (eq last-command this-command)) (point))
  20803. (t refpos)))))
  20804. ((org-at-item-p)
  20805. ;; Being at an item and not looking at an the item means point
  20806. ;; was previously moved to beginning of a visual line, which
  20807. ;; doesn't contain the item. Therefore, do nothing special,
  20808. ;; just stay here.
  20809. (when (looking-at org-list-full-item-re)
  20810. ;; Set special position at first white space character after
  20811. ;; bullet, and check-box, if any.
  20812. (let ((after-bullet
  20813. (let ((box (match-end 3)))
  20814. (if (not box) (match-end 1)
  20815. (let ((after (char-after box)))
  20816. (if (and after (= after ? )) (1+ box) box))))))
  20817. ;; Special case: Move point to special position when
  20818. ;; currently after it or at beginning of line.
  20819. (if (eq special t)
  20820. (when (or (> pos after-bullet) (= (point) pos))
  20821. (goto-char after-bullet))
  20822. ;; Reversed case: Move point to special position when
  20823. ;; point was already at beginning of line and command is
  20824. ;; repeated.
  20825. (when (and (= (point) pos) (eq last-command this-command))
  20826. (goto-char after-bullet))))))))
  20827. (org-no-warnings
  20828. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20829. (defun org-end-of-line (&optional arg)
  20830. "Go to the end of the line.
  20831. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20832. tags on the first attempt, and only move to after the tags when
  20833. the cursor is already beyond the end of the headline."
  20834. (interactive "P")
  20835. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20836. org-special-ctrl-a/e))
  20837. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20838. 'end-of-visual-line)
  20839. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20840. (t 'end-of-line))))
  20841. (if (or (not special) arg) (call-interactively move-fun)
  20842. (let* ((element (save-excursion (beginning-of-line)
  20843. (org-element-at-point)))
  20844. (type (org-element-type element)))
  20845. (cond
  20846. ((memq type '(headline inlinetask))
  20847. (let ((pos (point)))
  20848. (beginning-of-line 1)
  20849. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20850. (if (eq special t)
  20851. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20852. (goto-char (match-beginning 1))
  20853. (goto-char (match-end 0)))
  20854. (if (or (< pos (match-end 0))
  20855. (not (eq this-command last-command)))
  20856. (goto-char (match-end 0))
  20857. (goto-char (match-beginning 1))))
  20858. (call-interactively move-fun))))
  20859. ((org-element-property :hiddenp element)
  20860. ;; If element is hidden, `move-end-of-line' would put point
  20861. ;; after it. Use `end-of-line' to stay on current line.
  20862. (call-interactively 'end-of-line))
  20863. (t (call-interactively move-fun)))))
  20864. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20865. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20866. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20867. (defun org-backward-sentence (&optional arg)
  20868. "Go to beginning of sentence, or beginning of table field.
  20869. This will call `backward-sentence' or `org-table-beginning-of-field',
  20870. depending on context."
  20871. (interactive "P")
  20872. (cond
  20873. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20874. (t (call-interactively 'backward-sentence))))
  20875. (defun org-forward-sentence (&optional arg)
  20876. "Go to end of sentence, or end of table field.
  20877. This will call `forward-sentence' or `org-table-end-of-field',
  20878. depending on context."
  20879. (interactive "P")
  20880. (cond
  20881. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20882. (t (call-interactively 'forward-sentence))))
  20883. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20884. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20885. (defun org-kill-line (&optional arg)
  20886. "Kill line, to tags or end of line."
  20887. (interactive "P")
  20888. (cond
  20889. ((or (not org-special-ctrl-k)
  20890. (bolp)
  20891. (not (org-at-heading-p)))
  20892. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20893. org-ctrl-k-protect-subtree)
  20894. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20895. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20896. (user-error "C-k aborted as it would kill a hidden subtree")))
  20897. (call-interactively
  20898. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20899. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20900. (kill-region (point) (match-beginning 1))
  20901. (org-set-tags nil t))
  20902. (t (kill-region (point) (point-at-eol)))))
  20903. (define-key org-mode-map "\C-k" 'org-kill-line)
  20904. (defun org-yank (&optional arg)
  20905. "Yank. If the kill is a subtree, treat it specially.
  20906. This command will look at the current kill and check if is a single
  20907. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20908. cursor is at the beginning of a line or after the stars of a currently
  20909. empty headline, then the yank is handled specially. How exactly depends
  20910. on the value of the following variables, both set by default.
  20911. org-yank-folded-subtrees
  20912. When set, the subtree(s) will be folded after insertion, but only
  20913. if doing so would now swallow text after the yanked text.
  20914. org-yank-adjusted-subtrees
  20915. When set, the subtree will be promoted or demoted in order to
  20916. fit into the local outline tree structure, which means that the level
  20917. will be adjusted so that it becomes the smaller one of the two
  20918. *visible* surrounding headings.
  20919. Any prefix to this command will cause `yank' to be called directly with
  20920. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20921. will just
  20922. plainly yank the text as it is.
  20923. \[1] The test checks if the first non-white line is a heading
  20924. and if there are no other headings with fewer stars."
  20925. (interactive "P")
  20926. (org-yank-generic 'yank arg))
  20927. (defun org-yank-generic (command arg)
  20928. "Perform some yank-like command.
  20929. This function implements the behavior described in the `org-yank'
  20930. documentation. However, it has been generalized to work for any
  20931. interactive command with similar behavior."
  20932. ;; pretend to be command COMMAND
  20933. (setq this-command command)
  20934. (if arg
  20935. (call-interactively command)
  20936. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20937. (and (org-kill-is-subtree-p)
  20938. (or (bolp)
  20939. (and (looking-at "[ \t]*$")
  20940. (string-match
  20941. "\\`\\*+\\'"
  20942. (buffer-substring (point-at-bol) (point)))))))
  20943. swallowp)
  20944. (cond
  20945. ((and subtreep org-yank-folded-subtrees)
  20946. (let ((beg (point))
  20947. end)
  20948. (if (and subtreep org-yank-adjusted-subtrees)
  20949. (org-paste-subtree nil nil 'for-yank)
  20950. (call-interactively command))
  20951. (setq end (point))
  20952. (goto-char beg)
  20953. (when (and (bolp) subtreep
  20954. (not (setq swallowp
  20955. (org-yank-folding-would-swallow-text beg end))))
  20956. (org-with-limited-levels
  20957. (or (looking-at org-outline-regexp)
  20958. (re-search-forward org-outline-regexp-bol end t))
  20959. (while (and (< (point) end) (looking-at org-outline-regexp))
  20960. (hide-subtree)
  20961. (org-cycle-show-empty-lines 'folded)
  20962. (condition-case nil
  20963. (outline-forward-same-level 1)
  20964. (error (goto-char end))))))
  20965. (when swallowp
  20966. (message
  20967. "Inserted text not folded because that would swallow text"))
  20968. (goto-char end)
  20969. (skip-chars-forward " \t\n\r")
  20970. (beginning-of-line 1)
  20971. (push-mark beg 'nomsg)))
  20972. ((and subtreep org-yank-adjusted-subtrees)
  20973. (let ((beg (point-at-bol)))
  20974. (org-paste-subtree nil nil 'for-yank)
  20975. (push-mark beg 'nomsg)))
  20976. (t
  20977. (call-interactively command))))))
  20978. (defun org-yank-folding-would-swallow-text (beg end)
  20979. "Would hide-subtree at BEG swallow any text after END?"
  20980. (let (level)
  20981. (org-with-limited-levels
  20982. (save-excursion
  20983. (goto-char beg)
  20984. (when (or (looking-at org-outline-regexp)
  20985. (re-search-forward org-outline-regexp-bol end t))
  20986. (setq level (org-outline-level)))
  20987. (goto-char end)
  20988. (skip-chars-forward " \t\r\n\v\f")
  20989. (if (or (eobp)
  20990. (and (bolp) (looking-at org-outline-regexp)
  20991. (<= (org-outline-level) level)))
  20992. nil ; Nothing would be swallowed
  20993. t))))) ; something would swallow
  20994. (define-key org-mode-map "\C-y" 'org-yank)
  20995. (defun org-truely-invisible-p ()
  20996. "Check if point is at a character currently not visible.
  20997. This version does not only check the character property, but also
  20998. `visible-mode'."
  20999. ;; Early versions of noutline don't have `outline-invisible-p'.
  21000. (if (org-bound-and-true-p visible-mode)
  21001. nil
  21002. (outline-invisible-p)))
  21003. (defun org-invisible-p2 ()
  21004. "Check if point is at a character currently not visible."
  21005. (save-excursion
  21006. (if (and (eolp) (not (bobp))) (backward-char 1))
  21007. ;; Early versions of noutline don't have `outline-invisible-p'.
  21008. (outline-invisible-p)))
  21009. (defun org-back-to-heading (&optional invisible-ok)
  21010. "Call `outline-back-to-heading', but provide a better error message."
  21011. (condition-case nil
  21012. (outline-back-to-heading invisible-ok)
  21013. (error (error "Before first headline at position %d in buffer %s"
  21014. (point) (current-buffer)))))
  21015. (defun org-before-first-heading-p ()
  21016. "Before first heading?"
  21017. (save-excursion
  21018. (end-of-line)
  21019. (null (re-search-backward org-outline-regexp-bol nil t))))
  21020. (defun org-at-heading-p (&optional ignored)
  21021. (outline-on-heading-p t))
  21022. ;; Compatibility alias with Org versions < 7.8.03
  21023. (defalias 'org-on-heading-p 'org-at-heading-p)
  21024. (defun org-at-comment-p nil
  21025. "Is cursor in a line starting with a # character?"
  21026. (save-excursion
  21027. (beginning-of-line)
  21028. (looking-at "^#")))
  21029. (defun org-at-drawer-p nil
  21030. "Is cursor at a drawer keyword?"
  21031. (save-excursion
  21032. (move-beginning-of-line 1)
  21033. (looking-at org-drawer-regexp)))
  21034. (defun org-at-block-p nil
  21035. "Is cursor at a block keyword?"
  21036. (save-excursion
  21037. (move-beginning-of-line 1)
  21038. (looking-at org-block-regexp)))
  21039. (defun org-point-at-end-of-empty-headline ()
  21040. "If point is at the end of an empty headline, return t, else nil.
  21041. If the heading only contains a TODO keyword, it is still still considered
  21042. empty."
  21043. (and (looking-at "[ \t]*$")
  21044. (when org-todo-line-regexp
  21045. (save-excursion
  21046. (beginning-of-line 1)
  21047. (let ((case-fold-search nil))
  21048. (looking-at org-todo-line-regexp)
  21049. (string= (match-string 3) ""))))))
  21050. (defun org-at-heading-or-item-p ()
  21051. (or (org-at-heading-p) (org-at-item-p)))
  21052. (defun org-at-target-p ()
  21053. (or (org-in-regexp org-radio-target-regexp)
  21054. (org-in-regexp org-target-regexp)))
  21055. ;; Compatibility alias with Org versions < 7.8.03
  21056. (defalias 'org-on-target-p 'org-at-target-p)
  21057. (defun org-up-heading-all (arg)
  21058. "Move to the heading line of which the present line is a subheading.
  21059. This function considers both visible and invisible heading lines.
  21060. With argument, move up ARG levels."
  21061. (if (fboundp 'outline-up-heading-all)
  21062. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21063. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21064. (defun org-up-heading-safe ()
  21065. "Move to the heading line of which the present line is a subheading.
  21066. This version will not throw an error. It will return the level of the
  21067. headline found, or nil if no higher level is found.
  21068. Also, this function will be a lot faster than `outline-up-heading',
  21069. because it relies on stars being the outline starters. This can really
  21070. make a significant difference in outlines with very many siblings."
  21071. (let (start-level re)
  21072. (org-back-to-heading t)
  21073. (setq start-level (funcall outline-level))
  21074. (if (equal start-level 1)
  21075. nil
  21076. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21077. (if (re-search-backward re nil t)
  21078. (funcall outline-level)))))
  21079. (defun org-first-sibling-p ()
  21080. "Is this heading the first child of its parents?"
  21081. (interactive)
  21082. (let ((re org-outline-regexp-bol)
  21083. level l)
  21084. (unless (org-at-heading-p t)
  21085. (user-error "Not at a heading"))
  21086. (setq level (funcall outline-level))
  21087. (save-excursion
  21088. (if (not (re-search-backward re nil t))
  21089. t
  21090. (setq l (funcall outline-level))
  21091. (< l level)))))
  21092. (defun org-goto-sibling (&optional previous)
  21093. "Goto the next sibling, even if it is invisible.
  21094. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21095. when a sibling was found. When none is found, return nil and don't
  21096. move point."
  21097. (let ((fun (if previous 're-search-backward 're-search-forward))
  21098. (pos (point))
  21099. (re org-outline-regexp-bol)
  21100. level l)
  21101. (when (condition-case nil (org-back-to-heading t) (error nil))
  21102. (setq level (funcall outline-level))
  21103. (catch 'exit
  21104. (or previous (forward-char 1))
  21105. (while (funcall fun re nil t)
  21106. (setq l (funcall outline-level))
  21107. (when (< l level) (goto-char pos) (throw 'exit nil))
  21108. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21109. (goto-char pos)
  21110. nil))))
  21111. (defun org-show-siblings ()
  21112. "Show all siblings of the current headline."
  21113. (save-excursion
  21114. (while (org-goto-sibling) (org-flag-heading nil)))
  21115. (save-excursion
  21116. (while (org-goto-sibling 'previous)
  21117. (org-flag-heading nil))))
  21118. (defun org-goto-first-child ()
  21119. "Goto the first child, even if it is invisible.
  21120. Return t when a child was found. Otherwise don't move point and
  21121. return nil."
  21122. (let (level (pos (point)) (re org-outline-regexp-bol))
  21123. (when (condition-case nil (org-back-to-heading t) (error nil))
  21124. (setq level (outline-level))
  21125. (forward-char 1)
  21126. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21127. (progn (goto-char (match-beginning 0)) t)
  21128. (goto-char pos) nil))))
  21129. (defun org-show-hidden-entry ()
  21130. "Show an entry where even the heading is hidden."
  21131. (save-excursion
  21132. (org-show-entry)))
  21133. (defun org-flag-heading (flag &optional entry)
  21134. "Flag the current heading. FLAG non-nil means make invisible.
  21135. When ENTRY is non-nil, show the entire entry."
  21136. (save-excursion
  21137. (org-back-to-heading t)
  21138. ;; Check if we should show the entire entry
  21139. (if entry
  21140. (progn
  21141. (org-show-entry)
  21142. (save-excursion
  21143. (and (outline-next-heading)
  21144. (org-flag-heading nil))))
  21145. (outline-flag-region (max (point-min) (1- (point)))
  21146. (save-excursion (outline-end-of-heading) (point))
  21147. flag))))
  21148. (defun org-get-next-sibling ()
  21149. "Move to next heading of the same level, and return point.
  21150. If there is no such heading, return nil.
  21151. This is like outline-next-sibling, but invisible headings are ok."
  21152. (let ((level (funcall outline-level)))
  21153. (outline-next-heading)
  21154. (while (and (not (eobp)) (> (funcall outline-level) level))
  21155. (outline-next-heading))
  21156. (if (or (eobp) (< (funcall outline-level) level))
  21157. nil
  21158. (point))))
  21159. (defun org-get-last-sibling ()
  21160. "Move to previous heading of the same level, and return point.
  21161. If there is no such heading, return nil."
  21162. (let ((opoint (point))
  21163. (level (funcall outline-level)))
  21164. (outline-previous-heading)
  21165. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21166. (while (and (> (funcall outline-level) level)
  21167. (not (bobp)))
  21168. (outline-previous-heading))
  21169. (if (< (funcall outline-level) level)
  21170. nil
  21171. (point)))))
  21172. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21173. "Goto to the end of a subtree."
  21174. ;; This contains an exact copy of the original function, but it uses
  21175. ;; `org-back-to-heading', to make it work also in invisible
  21176. ;; trees. And is uses an invisible-ok argument.
  21177. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21178. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21179. ;; with many children and grandchildren etc, this can be much faster
  21180. ;; than the outline version.
  21181. (org-back-to-heading invisible-ok)
  21182. (let ((first t)
  21183. (level (funcall outline-level)))
  21184. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21185. ;; A true heading (not a plain list item), in Org-mode
  21186. ;; This means we can easily find the end by looking
  21187. ;; only for the right number of stars. Using a regexp to do
  21188. ;; this is so much faster than using a Lisp loop.
  21189. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21190. (forward-char 1)
  21191. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21192. ;; something else, do it the slow way
  21193. (while (and (not (eobp))
  21194. (or first (> (funcall outline-level) level)))
  21195. (setq first nil)
  21196. (outline-next-heading)))
  21197. (unless to-heading
  21198. (if (memq (preceding-char) '(?\n ?\^M))
  21199. (progn
  21200. ;; Go to end of line before heading
  21201. (forward-char -1)
  21202. (if (memq (preceding-char) '(?\n ?\^M))
  21203. ;; leave blank line before heading
  21204. (forward-char -1))))))
  21205. (point))
  21206. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21207. "Use Org version in org-mode, for dramatic speed-up."
  21208. (if (derived-mode-p 'org-mode)
  21209. (progn
  21210. (org-end-of-subtree nil t)
  21211. (unless (eobp) (backward-char 1)))
  21212. ad-do-it))
  21213. (defun org-end-of-meta-data-and-drawers ()
  21214. "Jump to the first text after meta data and drawers in the current entry.
  21215. This will move over empty lines, lines with planning time stamps,
  21216. clocking lines, and drawers."
  21217. (org-back-to-heading t)
  21218. (let ((end (save-excursion (outline-next-heading) (point)))
  21219. (re (concat "\\(" org-drawer-regexp "\\)"
  21220. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21221. (forward-line 1)
  21222. (while (re-search-forward re end t)
  21223. (if (not (match-end 1))
  21224. ;; empty or planning line
  21225. (forward-line 1)
  21226. ;; a drawer, find the end
  21227. (re-search-forward "^[ \t]*:END:" end 'move)
  21228. (forward-line 1)))
  21229. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21230. (point)))
  21231. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21232. "Move forward to the ARG'th subheading at same level as this one.
  21233. Stop at the first and last subheadings of a superior heading.
  21234. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21235. non-nil it will also look at invisible ones."
  21236. (interactive "p")
  21237. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21238. (if (and arg (< arg 0))
  21239. (goto-char (point-min))
  21240. (outline-next-heading))
  21241. (org-at-heading-p)
  21242. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21243. (f (if (and arg (< arg 0))
  21244. 're-search-backward
  21245. 're-search-forward))
  21246. (count (if arg (abs arg) 1))
  21247. (result (point)))
  21248. (while (and (prog1 (> count 0)
  21249. (forward-char (if (and arg (< arg 0)) -1 1)))
  21250. (funcall f org-outline-regexp-bol nil 'move))
  21251. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21252. (cond ((< l level) (setq count 0))
  21253. ((and (= l level)
  21254. (or invisible-ok
  21255. (progn
  21256. (goto-char (line-beginning-position))
  21257. (not (outline-invisible-p)))))
  21258. (setq count (1- count))
  21259. (when (eq l level)
  21260. (setq result (point)))))))
  21261. (goto-char result))
  21262. (beginning-of-line 1)))
  21263. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21264. "Move backward to the ARG'th subheading at same level as this one.
  21265. Stop at the first and last subheadings of a superior heading."
  21266. (interactive "p")
  21267. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21268. (defun org-next-block (arg &optional backward block-regexp)
  21269. "Jump to the next block.
  21270. With a prefix argument ARG, jump forward ARG many source blocks.
  21271. When BACKWARD is non-nil, jump to the previous block.
  21272. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21273. (interactive "p")
  21274. (let ((re (or block-regexp org-block-regexp))
  21275. (re-search-fn (or (and backward 're-search-backward)
  21276. 're-search-forward)))
  21277. (if (looking-at re) (forward-char 1))
  21278. (condition-case nil
  21279. (funcall re-search-fn re nil nil arg)
  21280. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21281. (goto-char (match-beginning 0)) (org-show-context)))
  21282. (defun org-previous-block (arg &optional block-regexp)
  21283. "Jump to the previous block.
  21284. With a prefix argument ARG, jump backward ARG many source blocks.
  21285. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21286. (interactive "p")
  21287. (org-next-block arg t block-regexp))
  21288. (defun org-forward-element ()
  21289. "Move forward by one element.
  21290. Move to the next element at the same level, when possible."
  21291. (interactive)
  21292. (cond ((eobp) (user-error "Cannot move further down"))
  21293. ((org-with-limited-levels (org-at-heading-p))
  21294. (let ((origin (point)))
  21295. (goto-char (org-end-of-subtree nil t))
  21296. (unless (org-with-limited-levels (org-at-heading-p))
  21297. (goto-char origin)
  21298. (user-error "Cannot move further down"))))
  21299. (t
  21300. (let* ((elem (org-element-at-point))
  21301. (end (org-element-property :end elem))
  21302. (parent (org-element-property :parent elem)))
  21303. (if (and parent (= (org-element-property :contents-end parent) end))
  21304. (goto-char (org-element-property :end parent))
  21305. (goto-char end))))))
  21306. (defun org-backward-element ()
  21307. "Move backward by one element.
  21308. Move to the previous element at the same level, when possible."
  21309. (interactive)
  21310. (cond ((bobp) (user-error "Cannot move further up"))
  21311. ((org-with-limited-levels (org-at-heading-p))
  21312. ;; At a headline, move to the previous one, if any, or stay
  21313. ;; here.
  21314. (let ((origin (point)))
  21315. (org-with-limited-levels (org-backward-heading-same-level 1))
  21316. ;; When current headline has no sibling above, move to its
  21317. ;; parent.
  21318. (when (= (point) origin)
  21319. (or (org-with-limited-levels (org-up-heading-safe))
  21320. (progn (goto-char origin)
  21321. (user-error "Cannot move further up"))))))
  21322. (t
  21323. (let* ((trail (org-element-at-point 'keep-trail))
  21324. (elem (car trail))
  21325. (prev-elem (nth 1 trail))
  21326. (beg (org-element-property :begin elem)))
  21327. (cond
  21328. ;; Move to beginning of current element if point isn't
  21329. ;; there already.
  21330. ((/= (point) beg) (goto-char beg))
  21331. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21332. ((org-before-first-heading-p) (goto-char (point-min)))
  21333. (t (org-back-to-heading)))))))
  21334. (defun org-up-element ()
  21335. "Move to upper element."
  21336. (interactive)
  21337. (if (org-with-limited-levels (org-at-heading-p))
  21338. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21339. (let* ((elem (org-element-at-point))
  21340. (parent (org-element-property :parent elem)))
  21341. (if parent (goto-char (org-element-property :begin parent))
  21342. (if (org-with-limited-levels (org-before-first-heading-p))
  21343. (user-error "No surrounding element")
  21344. (org-with-limited-levels (org-back-to-heading)))))))
  21345. (defvar org-element-greater-elements)
  21346. (defun org-down-element ()
  21347. "Move to inner element."
  21348. (interactive)
  21349. (let ((element (org-element-at-point)))
  21350. (cond
  21351. ((memq (org-element-type element) '(plain-list table))
  21352. (goto-char (org-element-property :contents-begin element))
  21353. (forward-char))
  21354. ((memq (org-element-type element) org-element-greater-elements)
  21355. ;; If contents are hidden, first disclose them.
  21356. (when (org-element-property :hiddenp element) (org-cycle))
  21357. (goto-char (or (org-element-property :contents-begin element)
  21358. (user-error "No content for this element"))))
  21359. (t (user-error "No inner element")))))
  21360. (defun org-drag-element-backward ()
  21361. "Move backward element at point."
  21362. (interactive)
  21363. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21364. (let* ((trail (org-element-at-point 'keep-trail))
  21365. (elem (car trail))
  21366. (prev-elem (nth 1 trail)))
  21367. ;; Error out if no previous element or previous element is
  21368. ;; a parent of the current one.
  21369. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21370. (user-error "Cannot drag element backward")
  21371. (let ((pos (point)))
  21372. (org-element-swap-A-B prev-elem elem)
  21373. (goto-char (+ (org-element-property :begin prev-elem)
  21374. (- pos (org-element-property :begin elem)))))))))
  21375. (defun org-drag-element-forward ()
  21376. "Move forward element at point."
  21377. (interactive)
  21378. (let* ((pos (point))
  21379. (elem (org-element-at-point)))
  21380. (when (= (point-max) (org-element-property :end elem))
  21381. (user-error "Cannot drag element forward"))
  21382. (goto-char (org-element-property :end elem))
  21383. (let ((next-elem (org-element-at-point)))
  21384. (when (or (org-element-nested-p elem next-elem)
  21385. (and (eq (org-element-type next-elem) 'headline)
  21386. (not (eq (org-element-type elem) 'headline))))
  21387. (goto-char pos)
  21388. (user-error "Cannot drag element forward"))
  21389. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21390. ;; body's length (without final blanks) and by the length of
  21391. ;; blanks between ELEM and NEXT-ELEM.
  21392. (let ((size-next (- (save-excursion
  21393. (goto-char (org-element-property :end next-elem))
  21394. (skip-chars-backward " \r\t\n")
  21395. (forward-line)
  21396. ;; Small correction if buffer doesn't end
  21397. ;; with a newline character.
  21398. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21399. (org-element-property :begin next-elem)))
  21400. (size-blank (- (org-element-property :end elem)
  21401. (save-excursion
  21402. (goto-char (org-element-property :end elem))
  21403. (skip-chars-backward " \r\t\n")
  21404. (forward-line)
  21405. (point)))))
  21406. (org-element-swap-A-B elem next-elem)
  21407. (goto-char (+ pos size-next size-blank))))))
  21408. (defun org-drag-line-forward (arg)
  21409. "Drag the line at point ARG lines forward."
  21410. (interactive "p")
  21411. (dotimes (n (abs arg))
  21412. (let ((c (current-column)))
  21413. (if (< 0 arg)
  21414. (progn
  21415. (beginning-of-line 2)
  21416. (transpose-lines 1)
  21417. (beginning-of-line 0))
  21418. (transpose-lines 1)
  21419. (beginning-of-line -1))
  21420. (org-move-to-column c))))
  21421. (defun org-drag-line-backward (arg)
  21422. "Drag the line at point ARG lines backward."
  21423. (interactive "p")
  21424. (org-drag-line-forward (- arg)))
  21425. (defun org-mark-element ()
  21426. "Put point at beginning of this element, mark at end.
  21427. Interactively, if this command is repeated or (in Transient Mark
  21428. mode) if the mark is active, it marks the next element after the
  21429. ones already marked."
  21430. (interactive)
  21431. (let (deactivate-mark)
  21432. (if (and (org-called-interactively-p 'any)
  21433. (or (and (eq last-command this-command) (mark t))
  21434. (and transient-mark-mode mark-active)))
  21435. (set-mark
  21436. (save-excursion
  21437. (goto-char (mark))
  21438. (goto-char (org-element-property :end (org-element-at-point)))))
  21439. (let ((element (org-element-at-point)))
  21440. (end-of-line)
  21441. (push-mark (org-element-property :end element) t t)
  21442. (goto-char (org-element-property :begin element))))))
  21443. (defun org-narrow-to-element ()
  21444. "Narrow buffer to current element."
  21445. (interactive)
  21446. (let ((elem (org-element-at-point)))
  21447. (cond
  21448. ((eq (car elem) 'headline)
  21449. (narrow-to-region
  21450. (org-element-property :begin elem)
  21451. (org-element-property :end elem)))
  21452. ((memq (car elem) org-element-greater-elements)
  21453. (narrow-to-region
  21454. (org-element-property :contents-begin elem)
  21455. (org-element-property :contents-end elem)))
  21456. (t
  21457. (narrow-to-region
  21458. (org-element-property :begin elem)
  21459. (org-element-property :end elem))))))
  21460. (defun org-transpose-element ()
  21461. "Transpose current and previous elements, keeping blank lines between.
  21462. Point is moved after both elements."
  21463. (interactive)
  21464. (org-skip-whitespace)
  21465. (let ((end (org-element-property :end (org-element-at-point))))
  21466. (org-drag-element-backward)
  21467. (goto-char end)))
  21468. (defun org-unindent-buffer ()
  21469. "Un-indent the visible part of the buffer.
  21470. Relative indentation (between items, inside blocks, etc.) isn't
  21471. modified."
  21472. (interactive)
  21473. (unless (eq major-mode 'org-mode)
  21474. (user-error "Cannot un-indent a buffer not in Org mode"))
  21475. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21476. unindent-tree ; For byte-compiler.
  21477. (unindent-tree
  21478. (function
  21479. (lambda (contents)
  21480. (mapc
  21481. (lambda (element)
  21482. (if (memq (org-element-type element) '(headline section))
  21483. (funcall unindent-tree (org-element-contents element))
  21484. (save-excursion
  21485. (save-restriction
  21486. (narrow-to-region
  21487. (org-element-property :begin element)
  21488. (org-element-property :end element))
  21489. (org-do-remove-indentation)))))
  21490. (reverse contents))))))
  21491. (funcall unindent-tree (org-element-contents parse-tree))))
  21492. (defun org-show-subtree ()
  21493. "Show everything after this heading at deeper levels."
  21494. (interactive)
  21495. (outline-flag-region
  21496. (point)
  21497. (save-excursion
  21498. (org-end-of-subtree t t))
  21499. nil))
  21500. (defun org-show-entry ()
  21501. "Show the body directly following this heading.
  21502. Show the heading too, if it is currently invisible."
  21503. (interactive)
  21504. (save-excursion
  21505. (condition-case nil
  21506. (progn
  21507. (org-back-to-heading t)
  21508. (outline-flag-region
  21509. (max (point-min) (1- (point)))
  21510. (save-excursion
  21511. (if (re-search-forward
  21512. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21513. (match-beginning 1)
  21514. (point-max)))
  21515. nil)
  21516. (org-cycle-hide-drawers 'children))
  21517. (error nil))))
  21518. (defun org-make-options-regexp (kwds &optional extra)
  21519. "Make a regular expression for keyword lines."
  21520. (concat
  21521. "^#\\+\\("
  21522. (mapconcat 'regexp-quote kwds "\\|")
  21523. (if extra (concat "\\|" extra))
  21524. "\\):[ \t]*\\(.*\\)"))
  21525. ;; Make isearch reveal the necessary context
  21526. (defun org-isearch-end ()
  21527. "Reveal context after isearch exits."
  21528. (when isearch-success ; only if search was successful
  21529. (if (featurep 'xemacs)
  21530. ;; Under XEmacs, the hook is run in the correct place,
  21531. ;; we directly show the context.
  21532. (org-show-context 'isearch)
  21533. ;; In Emacs the hook runs *before* restoring the overlays.
  21534. ;; So we have to use a one-time post-command-hook to do this.
  21535. ;; (Emacs 22 has a special variable, see function `org-mode')
  21536. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21537. isearch-mode-end-hook-quit)
  21538. ;; Only when the isearch was not quitted.
  21539. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21540. 'append 'local)))
  21541. (org-fix-ellipsis-at-bol)))
  21542. (defun org-isearch-post-command ()
  21543. "Remove self from hook, and show context."
  21544. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21545. (org-show-context 'isearch))
  21546. ;;;; Integration with and fixes for other packages
  21547. ;;; Imenu support
  21548. (defvar org-imenu-markers nil
  21549. "All markers currently used by Imenu.")
  21550. (make-variable-buffer-local 'org-imenu-markers)
  21551. (defun org-imenu-new-marker (&optional pos)
  21552. "Return a new marker for use by Imenu, and remember the marker."
  21553. (let ((m (make-marker)))
  21554. (move-marker m (or pos (point)))
  21555. (push m org-imenu-markers)
  21556. m))
  21557. (defun org-imenu-get-tree ()
  21558. "Produce the index for Imenu."
  21559. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21560. (setq org-imenu-markers nil)
  21561. (let* ((n org-imenu-depth)
  21562. (re (concat "^" (org-get-limited-outline-regexp)))
  21563. (subs (make-vector (1+ n) nil))
  21564. (last-level 0)
  21565. m level head0 head)
  21566. (save-excursion
  21567. (save-restriction
  21568. (widen)
  21569. (goto-char (point-max))
  21570. (while (re-search-backward re nil t)
  21571. (setq level (org-reduced-level (funcall outline-level)))
  21572. (when (and (<= level n)
  21573. (looking-at org-complex-heading-regexp)
  21574. (setq head0 (org-match-string-no-properties 4)))
  21575. (setq head (org-link-display-format head0)
  21576. m (org-imenu-new-marker))
  21577. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21578. (if (>= level last-level)
  21579. (push (cons head m) (aref subs level))
  21580. (push (cons head (aref subs (1+ level))) (aref subs level))
  21581. (loop for i from (1+ level) to n do (aset subs i nil)))
  21582. (setq last-level level)))))
  21583. (aref subs 1)))
  21584. (eval-after-load "imenu"
  21585. '(progn
  21586. (add-hook 'imenu-after-jump-hook
  21587. (lambda ()
  21588. (if (derived-mode-p 'org-mode)
  21589. (org-show-context 'org-goto))))))
  21590. (defun org-link-display-format (link)
  21591. "Replace a link with its the description.
  21592. If there is no description, use the link target."
  21593. (save-match-data
  21594. (if (string-match org-bracket-link-analytic-regexp link)
  21595. (replace-match (if (match-end 5)
  21596. (match-string 5 link)
  21597. (concat (match-string 1 link)
  21598. (match-string 3 link)))
  21599. nil t link)
  21600. link)))
  21601. (defun org-toggle-link-display ()
  21602. "Toggle the literal or descriptive display of links."
  21603. (interactive)
  21604. (if org-descriptive-links
  21605. (progn (org-remove-from-invisibility-spec '(org-link))
  21606. (org-restart-font-lock)
  21607. (setq org-descriptive-links nil))
  21608. (progn (add-to-invisibility-spec '(org-link))
  21609. (org-restart-font-lock)
  21610. (setq org-descriptive-links t))))
  21611. ;; Speedbar support
  21612. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21613. "Overlay marking the agenda restriction line in speedbar.")
  21614. (overlay-put org-speedbar-restriction-lock-overlay
  21615. 'face 'org-agenda-restriction-lock)
  21616. (overlay-put org-speedbar-restriction-lock-overlay
  21617. 'help-echo "Agendas are currently limited to this item.")
  21618. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21619. (defun org-speedbar-set-agenda-restriction ()
  21620. "Restrict future agenda commands to the location at point in speedbar.
  21621. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21622. (interactive)
  21623. (require 'org-agenda)
  21624. (let (p m tp np dir txt)
  21625. (cond
  21626. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21627. 'org-imenu t))
  21628. (setq m (get-text-property p 'org-imenu-marker))
  21629. (with-current-buffer (marker-buffer m)
  21630. (goto-char m)
  21631. (org-agenda-set-restriction-lock 'subtree)))
  21632. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21633. 'speedbar-function 'speedbar-find-file))
  21634. (setq tp (previous-single-property-change
  21635. (1+ p) 'speedbar-function)
  21636. np (next-single-property-change
  21637. tp 'speedbar-function)
  21638. dir (speedbar-line-directory)
  21639. txt (buffer-substring-no-properties (or tp (point-min))
  21640. (or np (point-max))))
  21641. (with-current-buffer (find-file-noselect
  21642. (let ((default-directory dir))
  21643. (expand-file-name txt)))
  21644. (unless (derived-mode-p 'org-mode)
  21645. (user-error "Cannot restrict to non-Org-mode file"))
  21646. (org-agenda-set-restriction-lock 'file)))
  21647. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21648. (move-overlay org-speedbar-restriction-lock-overlay
  21649. (point-at-bol) (point-at-eol))
  21650. (setq current-prefix-arg nil)
  21651. (org-agenda-maybe-redo)))
  21652. (eval-after-load "speedbar"
  21653. '(progn
  21654. (speedbar-add-supported-extension ".org")
  21655. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21656. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21657. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21658. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21659. (add-hook 'speedbar-visiting-tag-hook
  21660. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21661. ;;; Fixes and Hacks for problems with other packages
  21662. ;; Make flyspell not check words in links, to not mess up our keymap
  21663. (defvar org-element-affiliated-keywords) ; From org-element.el
  21664. (defvar org-element-block-name-alist) ; From org-element.el
  21665. (defun org-mode-flyspell-verify ()
  21666. "Don't let flyspell put overlays at active buttons, or on
  21667. {todo,all-time,additional-option-like}-keywords."
  21668. (require 'org-element) ; For `org-element-affiliated-keywords'
  21669. (let ((pos (max (1- (point)) (point-min)))
  21670. (word (thing-at-point 'word)))
  21671. (and (not (get-text-property pos 'keymap))
  21672. (not (get-text-property pos 'org-no-flyspell))
  21673. (not (member word org-todo-keywords-1))
  21674. (not (member word org-all-time-keywords))
  21675. (not (member word org-options-keywords))
  21676. (not (member word (mapcar 'car org-startup-options)))
  21677. (not (member-ignore-case word org-element-affiliated-keywords))
  21678. (not (member-ignore-case word (org-get-export-keywords)))
  21679. (not (member-ignore-case
  21680. word (mapcar 'car org-element-block-name-alist)))
  21681. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21682. (defun org-remove-flyspell-overlays-in (beg end)
  21683. "Remove flyspell overlays in region."
  21684. (and (org-bound-and-true-p flyspell-mode)
  21685. (fboundp 'flyspell-delete-region-overlays)
  21686. (flyspell-delete-region-overlays beg end))
  21687. (add-text-properties beg end '(org-no-flyspell t)))
  21688. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21689. (eval-after-load "bookmark"
  21690. '(if (boundp 'bookmark-after-jump-hook)
  21691. ;; We can use the hook
  21692. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21693. ;; Hook not available, use advice
  21694. (defadvice bookmark-jump (after org-make-visible activate)
  21695. "Make the position visible."
  21696. (org-bookmark-jump-unhide))))
  21697. ;; Make sure saveplace shows the location if it was hidden
  21698. (eval-after-load "saveplace"
  21699. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21700. "Make the position visible."
  21701. (org-bookmark-jump-unhide)))
  21702. ;; Make sure ecb shows the location if it was hidden
  21703. (eval-after-load "ecb"
  21704. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21705. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21706. (if (derived-mode-p 'org-mode)
  21707. (org-show-context))))
  21708. (defun org-bookmark-jump-unhide ()
  21709. "Unhide the current position, to show the bookmark location."
  21710. (and (derived-mode-p 'org-mode)
  21711. (or (outline-invisible-p)
  21712. (save-excursion (goto-char (max (point-min) (1- (point))))
  21713. (outline-invisible-p)))
  21714. (org-show-context 'bookmark-jump)))
  21715. ;; Make session.el ignore our circular variable
  21716. (eval-after-load "session"
  21717. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21718. ;;;; Finish up
  21719. (provide 'org)
  21720. (run-hooks 'org-load-hook)
  21721. ;;; org.el ends here