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-table-calc-current-TBLFM "org-table" (&optional arg))
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-agenda-redo "org-agenda" (&optional all))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-restriction "org-element" (element))
  145. (declare-function org-element-type "org-element" (element))
  146. ;; load languages based on value of `org-babel-load-languages'
  147. (defvar org-babel-load-languages)
  148. ;;;###autoload
  149. (defun org-babel-do-load-languages (sym value)
  150. "Load the languages defined in `org-babel-load-languages'."
  151. (set-default sym value)
  152. (mapc (lambda (pair)
  153. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  154. (if active
  155. (progn
  156. (require (intern (concat "ob-" lang))))
  157. (progn
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-execute:" lang)))
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-expand-body:" lang)))))))
  162. org-babel-load-languages))
  163. ;;;###autoload
  164. (defun org-babel-load-file (file &optional compile)
  165. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  166. This function exports the source code using `org-babel-tangle'
  167. and then loads the resulting file using `load-file'. With prefix
  168. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  169. file to byte-code before it is loaded."
  170. (interactive "fFile to load: \nP")
  171. (require 'ob-core)
  172. (let* ((age (lambda (file)
  173. (float-time
  174. (time-subtract (current-time)
  175. (nth 5 (or (file-attributes (file-truename file))
  176. (file-attributes file)))))))
  177. (base-name (file-name-sans-extension file))
  178. (exported-file (concat base-name ".el")))
  179. ;; tangle if the org-mode file is newer than the elisp file
  180. (unless (and (file-exists-p exported-file)
  181. (> (funcall age file) (funcall age exported-file)))
  182. (org-babel-tangle-file file exported-file "emacs-lisp"))
  183. (message "%s %s"
  184. (if compile
  185. (progn (byte-compile-file exported-file 'load)
  186. "Compiled and loaded")
  187. (progn (load-file exported-file) "Loaded"))
  188. exported-file)))
  189. (defcustom org-babel-load-languages '((emacs-lisp . t))
  190. "Languages which can be evaluated in Org-mode buffers.
  191. This list can be used to load support for any of the languages
  192. below, note that each language will depend on a different set of
  193. system executables and/or Emacs modes. When a language is
  194. \"loaded\", then code blocks in that language can be evaluated
  195. with `org-babel-execute-src-block' bound by default to C-c
  196. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  197. be set to remove code block evaluation from the C-c C-c
  198. keybinding. By default only Emacs Lisp (which has no
  199. requirements) is loaded."
  200. :group 'org-babel
  201. :set 'org-babel-do-load-languages
  202. :version "24.1"
  203. :type '(alist :tag "Babel Languages"
  204. :key-type
  205. (choice
  206. (const :tag "Awk" awk)
  207. (const :tag "C" C)
  208. (const :tag "R" R)
  209. (const :tag "Asymptote" asymptote)
  210. (const :tag "Calc" calc)
  211. (const :tag "Clojure" clojure)
  212. (const :tag "CSS" css)
  213. (const :tag "Ditaa" ditaa)
  214. (const :tag "Dot" dot)
  215. (const :tag "Emacs Lisp" emacs-lisp)
  216. (const :tag "Fortran" fortran)
  217. (const :tag "Gnuplot" gnuplot)
  218. (const :tag "Haskell" haskell)
  219. (const :tag "IO" io)
  220. (const :tag "Java" java)
  221. (const :tag "Javascript" js)
  222. (const :tag "LaTeX" latex)
  223. (const :tag "Ledger" ledger)
  224. (const :tag "Lilypond" lilypond)
  225. (const :tag "Lisp" lisp)
  226. (const :tag "Makefile" makefile)
  227. (const :tag "Maxima" maxima)
  228. (const :tag "Matlab" matlab)
  229. (const :tag "Mscgen" mscgen)
  230. (const :tag "Ocaml" ocaml)
  231. (const :tag "Octave" octave)
  232. (const :tag "Org" org)
  233. (const :tag "Perl" perl)
  234. (const :tag "Pico Lisp" picolisp)
  235. (const :tag "PlantUML" plantuml)
  236. (const :tag "Python" python)
  237. (const :tag "Ruby" ruby)
  238. (const :tag "Sass" sass)
  239. (const :tag "Scala" scala)
  240. (const :tag "Scheme" scheme)
  241. (const :tag "Screen" screen)
  242. (const :tag "Shell Script" sh)
  243. (const :tag "Shen" shen)
  244. (const :tag "Sql" sql)
  245. (const :tag "Sqlite" sqlite))
  246. :value-type (boolean :tag "Activate" :value t)))
  247. ;;;; Customization variables
  248. (defcustom org-clone-delete-id nil
  249. "Remove ID property of clones of a subtree.
  250. When non-nil, clones of a subtree don't inherit the ID property.
  251. Otherwise they inherit the ID property with a new unique
  252. identifier."
  253. :type 'boolean
  254. :version "24.1"
  255. :group 'org-id)
  256. ;;; Version
  257. (org-check-version)
  258. ;;;###autoload
  259. (defun org-version (&optional here full message)
  260. "Show the org-mode version in the echo area.
  261. With prefix argument HERE, insert it at point.
  262. When FULL is non-nil, use a verbose version string.
  263. When MESSAGE is non-nil, display a message with the version."
  264. (interactive "P")
  265. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  266. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  267. (load-suffixes (list ".el"))
  268. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  269. (org-trash (or
  270. (and (fboundp 'org-release) (fboundp 'org-git-version))
  271. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  272. (load-suffixes save-load-suffixes)
  273. (org-version (org-release))
  274. (git-version (org-git-version))
  275. (version (format "Org-mode version %s (%s @ %s)"
  276. org-version
  277. git-version
  278. (if org-install-dir
  279. (if (string= org-dir org-install-dir)
  280. org-install-dir
  281. (concat "mixed installation! " org-install-dir " and " org-dir))
  282. "org-loaddefs.el can not be found!")))
  283. (_version (if full version org-version)))
  284. (if (org-called-interactively-p 'interactive)
  285. (if here
  286. (insert version)
  287. (message version))
  288. (if message (message _version))
  289. _version)))
  290. (defconst org-version (org-version))
  291. ;;; Compatibility constants
  292. ;;; The custom variables
  293. (defgroup org nil
  294. "Outline-based notes management and organizer."
  295. :tag "Org"
  296. :group 'outlines
  297. :group 'calendar)
  298. (defcustom org-mode-hook nil
  299. "Mode hook for Org-mode, run after the mode was turned on."
  300. :group 'org
  301. :type 'hook)
  302. (defcustom org-load-hook nil
  303. "Hook that is run after org.el has been loaded."
  304. :group 'org
  305. :type 'hook)
  306. (defcustom org-log-buffer-setup-hook nil
  307. "Hook that is run after an Org log buffer is created."
  308. :group 'org
  309. :version "24.1"
  310. :type 'hook)
  311. (defvar org-modules) ; defined below
  312. (defvar org-modules-loaded nil
  313. "Have the modules been loaded already?")
  314. (defun org-load-modules-maybe (&optional force)
  315. "Load all extensions listed in `org-modules'."
  316. (when (or force (not org-modules-loaded))
  317. (mapc (lambda (ext)
  318. (condition-case nil (require ext)
  319. (error (message "Problems while trying to load feature `%s'" ext))))
  320. org-modules)
  321. (setq org-modules-loaded t)))
  322. (defun org-set-modules (var value)
  323. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  324. (set var value)
  325. (when (featurep 'org)
  326. (org-load-modules-maybe 'force)))
  327. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  328. "Modules that should always be loaded together with org.el.
  329. If a description starts with <C>, the file is not part of Emacs
  330. and loading it will require that you have downloaded and properly
  331. installed the Org mode distribution.
  332. You can also use this system to load external packages (i.e. neither Org
  333. core modules, nor modules from the CONTRIB directory). Just add symbols
  334. to the end of the list. If the package is called org-xyz.el, then you need
  335. to add the symbol `xyz', and the package must have a call to:
  336. \(provide 'org-xyz)
  337. For export specific modules, see also `org-export-backends'."
  338. :group 'org
  339. :set 'org-set-modules
  340. :version "24.4"
  341. :package-version '(Org . "8.0")
  342. :type
  343. '(set :greedy t
  344. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  345. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  346. (const :tag " crypt: Encryption of subtrees" org-crypt)
  347. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  348. (const :tag " docview: Links to doc-view buffers" org-docview)
  349. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  350. (const :tag " habit: Track your consistency with habits" org-habit)
  351. (const :tag " id: Global IDs for identifying entries" org-id)
  352. (const :tag " info: Links to Info nodes" org-info)
  353. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  354. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  355. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  356. (const :tag " mouse: Additional mouse support" org-mouse)
  357. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  358. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  359. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  360. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  361. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  362. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  363. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  364. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  365. (const :tag "C collector: Collect properties into tables" org-collector)
  366. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  367. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  368. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  369. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  370. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  371. (const :tag "C eval: Include command output as text" org-eval)
  372. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  373. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  374. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  375. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  376. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  377. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  378. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  379. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  380. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  381. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  382. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  383. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  384. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  385. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  386. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  387. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  388. (const :tag "C registry: A registry for Org-mode links" org-registry)
  389. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  390. (const :tag "C secretary: Team management with org-mode" org-secretary)
  391. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  392. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  393. (const :tag "C track: Keep up with Org-mode development" org-track)
  394. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  395. (const :tag "C vm: Links to VM folders/messages" org-vm)
  396. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  397. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  398. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  399. (defvar org-export-registered-backends) ; From ox.el
  400. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  401. (defcustom org-export-backends '(ascii html icalendar latex)
  402. "List of export back-ends that should be always available.
  403. If a description starts with <C>, the file is not part of Emacs
  404. and loading it will require that you have downloaded and properly
  405. installed the Org mode distribution.
  406. Unlike to `org-modules', libraries in this list will not be
  407. loaded along with Org, but only once the export framework is
  408. needed.
  409. This variable needs to be set before org.el is loaded. If you
  410. need to make a change while Emacs is running, use the customize
  411. interface or run the following code, where VALUE stands for the
  412. new value of the variable, after updating it:
  413. \(progn
  414. \(setq org-export-registered-backends
  415. \(org-remove-if-not
  416. \(lambda (backend)
  417. \(or (memq backend val)
  418. \(catch 'parentp
  419. \(mapc
  420. \(lambda (b)
  421. \(and (org-export-derived-backend-p b (car backend))
  422. \(throw 'parentp t)))
  423. val)
  424. nil)))
  425. org-export-registered-backends))
  426. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  427. \(dolist (backend val)
  428. \(cond
  429. \((not (load (format \"ox-%s\" backend) t t))
  430. \(message \"Problems while trying to load export back-end `%s'\"
  431. backend))
  432. \((not (memq backend new-list)) (push backend new-list))))
  433. \(set-default var new-list)))
  434. Adding a back-end to this list will also pull the back-end it
  435. depends on, if any."
  436. :group 'org
  437. :group 'org-export
  438. :version "24.4"
  439. :package-version '(Org . "8.0")
  440. :initialize 'custom-initialize-set
  441. :set (lambda (var val)
  442. (if (not (featurep 'ox)) (set-default var val)
  443. ;; Any back-end not required anymore (not present in VAL and not
  444. ;; a parent of any back-end in the new value) is removed from the
  445. ;; list of registered back-ends.
  446. (setq org-export-registered-backends
  447. (org-remove-if-not
  448. (lambda (backend)
  449. (or (memq backend val)
  450. (catch 'parentp
  451. (mapc
  452. (lambda (b)
  453. (and (org-export-derived-backend-p b (car backend))
  454. (throw 'parentp t)))
  455. val)
  456. nil)))
  457. org-export-registered-backends))
  458. ;; Now build NEW-LIST of both new back-ends and required
  459. ;; parents.
  460. (let ((new-list (mapcar 'car org-export-registered-backends)))
  461. (dolist (backend val)
  462. (cond
  463. ((not (load (format "ox-%s" backend) t t))
  464. (message "Problems while trying to load export back-end `%s'"
  465. backend))
  466. ((not (memq backend new-list)) (push backend new-list))))
  467. ;; Set VAR to that list with fixed dependencies.
  468. (set-default var new-list))))
  469. :type '(set :greedy t
  470. (const :tag " ascii Export buffer to ASCII format" ascii)
  471. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  472. (const :tag " html Export buffer to HTML format" html)
  473. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  474. (const :tag " latex Export buffer to LaTeX format" latex)
  475. (const :tag " man Export buffer to MAN format" man)
  476. (const :tag " md Export buffer to Markdown format" md)
  477. (const :tag " odt Export buffer to ODT format" odt)
  478. (const :tag " org Export buffer to Org format" org)
  479. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  480. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  481. (const :tag "C deck Export buffer to deck.js presentations" deck)
  482. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  483. (const :tag "C groff Export buffer to Groff format" groff)
  484. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  485. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  486. (const :tag "C s5 Export buffer to s5 presentations" s5)
  487. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  488. (eval-after-load 'ox
  489. '(mapc
  490. (lambda (backend)
  491. (condition-case nil (require (intern (format "ox-%s" backend)))
  492. (error (message "Problems while trying to load export back-end `%s'"
  493. backend))))
  494. org-export-backends))
  495. (defcustom org-support-shift-select nil
  496. "Non-nil means make shift-cursor commands select text when possible.
  497. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  498. start selecting a region, or enlarge regions started in this way.
  499. In Org-mode, in special contexts, these same keys are used for
  500. other purposes, important enough to compete with shift selection.
  501. Org tries to balance these needs by supporting `shift-select-mode'
  502. outside these special contexts, under control of this variable.
  503. The default of this variable is nil, to avoid confusing behavior. Shifted
  504. cursor keys will then execute Org commands in the following contexts:
  505. - on a headline, changing TODO state (left/right) and priority (up/down)
  506. - on a time stamp, changing the time
  507. - in a plain list item, changing the bullet type
  508. - in a property definition line, switching between allowed values
  509. - in the BEGIN line of a clock table (changing the time block).
  510. Outside these contexts, the commands will throw an error.
  511. When this variable is t and the cursor is not in a special
  512. context, Org-mode will support shift-selection for making and
  513. enlarging regions. To make this more effective, the bullet
  514. cycling will no longer happen anywhere in an item line, but only
  515. if the cursor is exactly on the bullet.
  516. If you set this variable to the symbol `always', then the keys
  517. will not be special in headlines, property lines, and item lines,
  518. to make shift selection work there as well. If this is what you
  519. want, you can use the following alternative commands: `C-c C-t'
  520. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  521. can be used to switch TODO sets, `C-c -' to cycle item bullet
  522. types, and properties can be edited by hand or in column view.
  523. However, when the cursor is on a timestamp, shift-cursor commands
  524. will still edit the time stamp - this is just too good to give up.
  525. XEmacs user should have this variable set to nil, because
  526. `shift-select-mode' is in Emacs 23 or later only."
  527. :group 'org
  528. :type '(choice
  529. (const :tag "Never" nil)
  530. (const :tag "When outside special context" t)
  531. (const :tag "Everywhere except timestamps" always)))
  532. (defcustom org-loop-over-headlines-in-active-region nil
  533. "Shall some commands act upon headlines in the active region?
  534. When set to `t', some commands will be performed in all headlines
  535. within the active region.
  536. When set to `start-level', some commands will be performed in all
  537. headlines within the active region, provided that these headlines
  538. are of the same level than the first one.
  539. When set to a string, those commands will be performed on the
  540. matching headlines within the active region. Such string must be
  541. a tags/property/todo match as it is used in the agenda tags view.
  542. The list of commands is: `org-schedule', `org-deadline',
  543. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  544. `org-archive-to-archive-sibling'. The archiving commands skip
  545. already archived entries."
  546. :type '(choice (const :tag "Don't loop" nil)
  547. (const :tag "All headlines in active region" t)
  548. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  549. (string :tag "Tags/Property/Todo matcher"))
  550. :version "24.1"
  551. :group 'org-todo
  552. :group 'org-archive)
  553. (defgroup org-startup nil
  554. "Options concerning startup of Org-mode."
  555. :tag "Org Startup"
  556. :group 'org)
  557. (defcustom org-startup-folded t
  558. "Non-nil means entering Org-mode will switch to OVERVIEW.
  559. This can also be configured on a per-file basis by adding one of
  560. the following lines anywhere in the buffer:
  561. #+STARTUP: fold (or `overview', this is equivalent)
  562. #+STARTUP: nofold (or `showall', this is equivalent)
  563. #+STARTUP: content
  564. #+STARTUP: showeverything
  565. By default, this option is ignored when Org opens agenda files
  566. for the first time. If you want the agenda to honor the startup
  567. option, set `org-agenda-inhibit-startup' to nil."
  568. :group 'org-startup
  569. :type '(choice
  570. (const :tag "nofold: show all" nil)
  571. (const :tag "fold: overview" t)
  572. (const :tag "content: all headlines" content)
  573. (const :tag "show everything, even drawers" showeverything)))
  574. (defcustom org-startup-truncated t
  575. "Non-nil means entering Org-mode will set `truncate-lines'.
  576. This is useful since some lines containing links can be very long and
  577. uninteresting. Also tables look terrible when wrapped."
  578. :group 'org-startup
  579. :type 'boolean)
  580. (defcustom org-startup-indented nil
  581. "Non-nil means turn on `org-indent-mode' on startup.
  582. This can also be configured on a per-file basis by adding one of
  583. the following lines anywhere in the buffer:
  584. #+STARTUP: indent
  585. #+STARTUP: noindent"
  586. :group 'org-structure
  587. :type '(choice
  588. (const :tag "Not" nil)
  589. (const :tag "Globally (slow on startup in large files)" t)))
  590. (defcustom org-use-sub-superscripts t
  591. "Non-nil means interpret \"_\" and \"^\" for display.
  592. When this option is turned on, you can use TeX-like syntax for sub- and
  593. superscripts. Several characters after \"_\" or \"^\" will be
  594. considered as a single item - so grouping with {} is normally not
  595. needed. For example, the following things will be parsed as single
  596. sub- or superscripts.
  597. 10^24 or 10^tau several digits will be considered 1 item.
  598. 10^-12 or 10^-tau a leading sign with digits or a word
  599. x^2-y^3 will be read as x^2 - y^3, because items are
  600. terminated by almost any nonword/nondigit char.
  601. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  602. Still, ambiguity is possible - so when in doubt use {} to enclose
  603. the sub/superscript. If you set this variable to the symbol
  604. `{}', the braces are *required* in order to trigger
  605. interpretations as sub/superscript. This can be helpful in
  606. documents that need \"_\" frequently in plain text."
  607. :group 'org-startup
  608. :version "24.1"
  609. :type '(choice
  610. (const :tag "Always interpret" t)
  611. (const :tag "Only with braces" {})
  612. (const :tag "Never interpret" nil)))
  613. (defcustom org-startup-with-beamer-mode nil
  614. "Non-nil means turn on `org-beamer-mode' on startup.
  615. This can also be configured on a per-file basis by adding one of
  616. the following lines anywhere in the buffer:
  617. #+STARTUP: beamer"
  618. :group 'org-startup
  619. :version "24.1"
  620. :type 'boolean)
  621. (defcustom org-startup-align-all-tables nil
  622. "Non-nil means align all tables when visiting a file.
  623. This is useful when the column width in tables is forced with <N> cookies
  624. in table fields. Such tables will look correct only after the first re-align.
  625. This can also be configured on a per-file basis by adding one of
  626. the following lines anywhere in the buffer:
  627. #+STARTUP: align
  628. #+STARTUP: noalign"
  629. :group 'org-startup
  630. :type 'boolean)
  631. (defcustom org-startup-with-inline-images nil
  632. "Non-nil means show inline images when loading a new Org file.
  633. This can also be configured on a per-file basis by adding one of
  634. the following lines anywhere in the buffer:
  635. #+STARTUP: inlineimages
  636. #+STARTUP: noinlineimages"
  637. :group 'org-startup
  638. :version "24.1"
  639. :type 'boolean)
  640. (defcustom org-startup-with-latex-preview nil
  641. "Non-nil means preview LaTeX fragments when loading a new Org file.
  642. This can also be configured on a per-file basis by adding one of
  643. the followinglines anywhere in the buffer:
  644. #+STARTUP: latexpreview
  645. #+STARTUP: nolatexpreview"
  646. :group 'org-startup
  647. :version "24.4"
  648. :package-version '(Org . "8.0")
  649. :type 'boolean)
  650. (defcustom org-insert-mode-line-in-empty-file nil
  651. "Non-nil means insert the first line setting Org-mode in empty files.
  652. When the function `org-mode' is called interactively in an empty file, this
  653. normally means that the file name does not automatically trigger Org-mode.
  654. To ensure that the file will always be in Org-mode in the future, a
  655. line enforcing Org-mode will be inserted into the buffer, if this option
  656. has been set."
  657. :group 'org-startup
  658. :type 'boolean)
  659. (defcustom org-replace-disputed-keys nil
  660. "Non-nil means use alternative key bindings for some keys.
  661. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  662. These keys are also used by other packages like shift-selection-mode'
  663. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  664. If you want to use Org-mode together with one of these other modes,
  665. or more generally if you would like to move some Org-mode commands to
  666. other keys, set this variable and configure the keys with the variable
  667. `org-disputed-keys'.
  668. This option is only relevant at load-time of Org-mode, and must be set
  669. *before* org.el is loaded. Changing it requires a restart of Emacs to
  670. become effective."
  671. :group 'org-startup
  672. :type 'boolean)
  673. (defcustom org-use-extra-keys nil
  674. "Non-nil means use extra key sequence definitions for certain commands.
  675. This happens automatically if you run XEmacs or if `window-system'
  676. is nil. This variable lets you do the same manually. You must
  677. set it before loading org.
  678. Example: on Carbon Emacs 22 running graphically, with an external
  679. keyboard on a Powerbook, the default way of setting M-left might
  680. not work for either Alt or ESC. Setting this variable will make
  681. it work for ESC."
  682. :group 'org-startup
  683. :type 'boolean)
  684. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  685. (defcustom org-disputed-keys
  686. '(([(shift up)] . [(meta p)])
  687. ([(shift down)] . [(meta n)])
  688. ([(shift left)] . [(meta -)])
  689. ([(shift right)] . [(meta +)])
  690. ([(control shift right)] . [(meta shift +)])
  691. ([(control shift left)] . [(meta shift -)]))
  692. "Keys for which Org-mode and other modes compete.
  693. This is an alist, cars are the default keys, second element specifies
  694. the alternative to use when `org-replace-disputed-keys' is t.
  695. Keys can be specified in any syntax supported by `define-key'.
  696. The value of this option takes effect only at Org-mode's startup,
  697. therefore you'll have to restart Emacs to apply it after changing."
  698. :group 'org-startup
  699. :type 'alist)
  700. (defun org-key (key)
  701. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  702. Or return the original if not disputed.
  703. Also apply the translations defined in `org-xemacs-key-equivalents'."
  704. (when org-replace-disputed-keys
  705. (let* ((nkey (key-description key))
  706. (x (org-find-if (lambda (x)
  707. (equal (key-description (car x)) nkey))
  708. org-disputed-keys)))
  709. (setq key (if x (cdr x) key))))
  710. (when (featurep 'xemacs)
  711. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  712. key)
  713. (defun org-find-if (predicate seq)
  714. (catch 'exit
  715. (while seq
  716. (if (funcall predicate (car seq))
  717. (throw 'exit (car seq))
  718. (pop seq)))))
  719. (defun org-defkey (keymap key def)
  720. "Define a key, possibly translated, as returned by `org-key'."
  721. (define-key keymap (org-key key) def))
  722. (defcustom org-ellipsis nil
  723. "The ellipsis to use in the Org-mode outline.
  724. When nil, just use the standard three dots. When a string, use that instead,
  725. When a face, use the standard 3 dots, but with the specified face.
  726. The change affects only Org-mode (which will then use its own display table).
  727. Changing this requires executing `M-x org-mode' in a buffer to become
  728. effective."
  729. :group 'org-startup
  730. :type '(choice (const :tag "Default" nil)
  731. (face :tag "Face" :value org-warning)
  732. (string :tag "String" :value "...#")))
  733. (defvar org-display-table nil
  734. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  735. (defgroup org-keywords nil
  736. "Keywords in Org-mode."
  737. :tag "Org Keywords"
  738. :group 'org)
  739. (defcustom org-deadline-string "DEADLINE:"
  740. "String to mark deadline entries.
  741. A deadline is this string, followed by a time stamp. Should be a word,
  742. terminated by a colon. You can insert a schedule keyword and
  743. a timestamp with \\[org-deadline].
  744. Changes become only effective after restarting Emacs."
  745. :group 'org-keywords
  746. :type 'string)
  747. (defcustom org-scheduled-string "SCHEDULED:"
  748. "String to mark scheduled TODO entries.
  749. A schedule is this string, followed by a time stamp. Should be a word,
  750. terminated by a colon. You can insert a schedule keyword and
  751. a timestamp with \\[org-schedule].
  752. Changes become only effective after restarting Emacs."
  753. :group 'org-keywords
  754. :type 'string)
  755. (defcustom org-closed-string "CLOSED:"
  756. "String used as the prefix for timestamps logging closing a TODO entry."
  757. :group 'org-keywords
  758. :type 'string)
  759. (defcustom org-clock-string "CLOCK:"
  760. "String used as prefix for timestamps clocking work hours on an item."
  761. :group 'org-keywords
  762. :type 'string)
  763. (defcustom org-closed-keep-when-no-todo nil
  764. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  765. :group 'org-todo
  766. :group 'org-keywords
  767. :version "24.4"
  768. :package-version '(Org . "8.0")
  769. :type 'boolean)
  770. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  771. org-scheduled-string "\\|"
  772. org-deadline-string "\\|"
  773. org-closed-string "\\|"
  774. org-clock-string "\\)")
  775. "Matches a line with planning or clock info.")
  776. (defcustom org-comment-string "COMMENT"
  777. "Entries starting with this keyword will never be exported.
  778. An entry can be toggled between COMMENT and normal with
  779. \\[org-toggle-comment].
  780. Changes become only effective after restarting Emacs."
  781. :group 'org-keywords
  782. :type 'string)
  783. (defcustom org-quote-string "QUOTE"
  784. "Entries starting with this keyword will be exported in fixed-width font.
  785. Quoting applies only to the text in the entry following the headline, and does
  786. not extend beyond the next headline, even if that is lower level.
  787. An entry can be toggled between QUOTE and normal with
  788. \\[org-toggle-fixed-width-section]."
  789. :group 'org-keywords
  790. :type 'string)
  791. (defconst org-repeat-re
  792. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  793. "Regular expression for specifying repeated events.
  794. After a match, group 1 contains the repeat expression.")
  795. (defgroup org-structure nil
  796. "Options concerning the general structure of Org-mode files."
  797. :tag "Org Structure"
  798. :group 'org)
  799. (defgroup org-reveal-location nil
  800. "Options about how to make context of a location visible."
  801. :tag "Org Reveal Location"
  802. :group 'org-structure)
  803. (defconst org-context-choice
  804. '(choice
  805. (const :tag "Always" t)
  806. (const :tag "Never" nil)
  807. (repeat :greedy t :tag "Individual contexts"
  808. (cons
  809. (choice :tag "Context"
  810. (const agenda)
  811. (const org-goto)
  812. (const occur-tree)
  813. (const tags-tree)
  814. (const link-search)
  815. (const mark-goto)
  816. (const bookmark-jump)
  817. (const isearch)
  818. (const default))
  819. (boolean))))
  820. "Contexts for the reveal options.")
  821. (defcustom org-show-hierarchy-above '((default . t))
  822. "Non-nil means show full hierarchy when revealing a location.
  823. Org-mode often shows locations in an org-mode file which might have
  824. been invisible before. When this is set, the hierarchy of headings
  825. above the exposed location is shown.
  826. Turning this off for example for sparse trees makes them very compact.
  827. Instead of t, this can also be an alist specifying this option for different
  828. contexts. Valid contexts are
  829. agenda when exposing an entry from the agenda
  830. org-goto when using the command `org-goto' on key C-c C-j
  831. occur-tree when using the command `org-occur' on key C-c /
  832. tags-tree when constructing a sparse tree based on tags matches
  833. link-search when exposing search matches associated with a link
  834. mark-goto when exposing the jump goal of a mark
  835. bookmark-jump when exposing a bookmark location
  836. isearch when exiting from an incremental search
  837. default default for all contexts not set explicitly"
  838. :group 'org-reveal-location
  839. :type org-context-choice)
  840. (defcustom org-show-following-heading '((default . nil))
  841. "Non-nil means show following heading when revealing a location.
  842. Org-mode often shows locations in an org-mode file which might have
  843. been invisible before. When this is set, the heading following the
  844. match is shown.
  845. Turning this off for example for sparse trees makes them very compact,
  846. but makes it harder to edit the location of the match. In such a case,
  847. use the command \\[org-reveal] to show more context.
  848. Instead of t, this can also be an alist specifying this option for different
  849. contexts. See `org-show-hierarchy-above' for valid contexts."
  850. :group 'org-reveal-location
  851. :type org-context-choice)
  852. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  853. "Non-nil means show all sibling heading when revealing a location.
  854. Org-mode often shows locations in an org-mode file which might have
  855. been invisible before. When this is set, the sibling of the current entry
  856. heading are all made visible. If `org-show-hierarchy-above' is t,
  857. the same happens on each level of the hierarchy above the current entry.
  858. By default this is on for the isearch context, off for all other contexts.
  859. Turning this off for example for sparse trees makes them very compact,
  860. but makes it harder to edit the location of the match. In such a case,
  861. use the command \\[org-reveal] to show more context.
  862. Instead of t, this can also be an alist specifying this option for different
  863. contexts. See `org-show-hierarchy-above' for valid contexts."
  864. :group 'org-reveal-location
  865. :type org-context-choice
  866. :version "24.4"
  867. :package-version '(Org . "8.0"))
  868. (defcustom org-show-entry-below '((default . nil))
  869. "Non-nil means show the entry below a headline when revealing a location.
  870. Org-mode often shows locations in an org-mode file which might have
  871. been invisible before. When this is set, the text below the headline that is
  872. exposed is also shown.
  873. By default this is off for all contexts.
  874. Instead of t, this can also be an alist specifying this option for different
  875. contexts. See `org-show-hierarchy-above' for valid contexts."
  876. :group 'org-reveal-location
  877. :type org-context-choice)
  878. (defcustom org-indirect-buffer-display 'other-window
  879. "How should indirect tree buffers be displayed?
  880. This applies to indirect buffers created with the commands
  881. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  882. Valid values are:
  883. current-window Display in the current window
  884. other-window Just display in another window.
  885. dedicated-frame Create one new frame, and re-use it each time.
  886. new-frame Make a new frame each time. Note that in this case
  887. previously-made indirect buffers are kept, and you need to
  888. kill these buffers yourself."
  889. :group 'org-structure
  890. :group 'org-agenda-windows
  891. :type '(choice
  892. (const :tag "In current window" current-window)
  893. (const :tag "In current frame, other window" other-window)
  894. (const :tag "Each time a new frame" new-frame)
  895. (const :tag "One dedicated frame" dedicated-frame)))
  896. (defcustom org-use-speed-commands nil
  897. "Non-nil means activate single letter commands at beginning of a headline.
  898. This may also be a function to test for appropriate locations where speed
  899. commands should be active."
  900. :group 'org-structure
  901. :type '(choice
  902. (const :tag "Never" nil)
  903. (const :tag "At beginning of headline stars" t)
  904. (function)))
  905. (defcustom org-speed-commands-user nil
  906. "Alist of additional speed commands.
  907. This list will be checked before `org-speed-commands-default'
  908. when the variable `org-use-speed-commands' is non-nil
  909. and when the cursor is at the beginning of a headline.
  910. The car if each entry is a string with a single letter, which must
  911. be assigned to `self-insert-command' in the global map.
  912. The cdr is either a command to be called interactively, a function
  913. to be called, or a form to be evaluated.
  914. An entry that is just a list with a single string will be interpreted
  915. as a descriptive headline that will be added when listing the speed
  916. commands in the Help buffer using the `?' speed command."
  917. :group 'org-structure
  918. :type '(repeat :value ("k" . ignore)
  919. (choice :value ("k" . ignore)
  920. (list :tag "Descriptive Headline" (string :tag "Headline"))
  921. (cons :tag "Letter and Command"
  922. (string :tag "Command letter")
  923. (choice
  924. (function)
  925. (sexp))))))
  926. (defgroup org-cycle nil
  927. "Options concerning visibility cycling in Org-mode."
  928. :tag "Org Cycle"
  929. :group 'org-structure)
  930. (defcustom org-cycle-skip-children-state-if-no-children t
  931. "Non-nil means skip CHILDREN state in entries that don't have any."
  932. :group 'org-cycle
  933. :type 'boolean)
  934. (defcustom org-cycle-max-level nil
  935. "Maximum level which should still be subject to visibility cycling.
  936. Levels higher than this will, for cycling, be treated as text, not a headline.
  937. When `org-odd-levels-only' is set, a value of N in this variable actually
  938. means 2N-1 stars as the limiting headline.
  939. When nil, cycle all levels.
  940. Note that the limiting level of cycling is also influenced by
  941. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  942. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  943. than its value."
  944. :group 'org-cycle
  945. :type '(choice
  946. (const :tag "No limit" nil)
  947. (integer :tag "Maximum level")))
  948. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  949. "Names of drawers. Drawers are not opened by cycling on the headline above.
  950. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  951. this:
  952. :DRAWERNAME:
  953. .....
  954. :END:
  955. The drawer \"PROPERTIES\" is special for capturing properties through
  956. the property API.
  957. Drawers can be defined on the per-file basis with a line like:
  958. #+DRAWERS: HIDDEN STATE PROPERTIES"
  959. :group 'org-structure
  960. :group 'org-cycle
  961. :type '(repeat (string :tag "Drawer Name")))
  962. (defcustom org-hide-block-startup nil
  963. "Non-nil means entering Org-mode will fold all blocks.
  964. This can also be set in on a per-file basis with
  965. #+STARTUP: hideblocks
  966. #+STARTUP: showblocks"
  967. :group 'org-startup
  968. :group 'org-cycle
  969. :type 'boolean)
  970. (defcustom org-cycle-global-at-bob nil
  971. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  972. This makes it possible to do global cycling without having to use S-TAB or
  973. \\[universal-argument] TAB. For this special case to work, the first line
  974. of the buffer must not be a headline -- it may be empty or some other text.
  975. When used in this way, `org-cycle-hook' is disabled temporarily to make
  976. sure the cursor stays at the beginning of the buffer. When this option is
  977. nil, don't do anything special at the beginning of the buffer."
  978. :group 'org-cycle
  979. :type 'boolean)
  980. (defcustom org-cycle-level-after-item/entry-creation t
  981. "Non-nil means cycle entry level or item indentation in new empty entries.
  982. When the cursor is at the end of an empty headline, i.e., with only stars
  983. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  984. and then all possible ancestor states, before returning to the original state.
  985. This makes data entry extremely fast: M-RET to create a new headline,
  986. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  987. When the cursor is at the end of an empty plain list item, one TAB will
  988. make it a subitem, two or more tabs will back up to make this an item
  989. higher up in the item hierarchy."
  990. :group 'org-cycle
  991. :type 'boolean)
  992. (defcustom org-cycle-emulate-tab t
  993. "Where should `org-cycle' emulate TAB.
  994. nil Never
  995. white Only in completely white lines
  996. whitestart Only at the beginning of lines, before the first non-white char
  997. t Everywhere except in headlines
  998. exc-hl-bol Everywhere except at the start of a headline
  999. If TAB is used in a place where it does not emulate TAB, the current subtree
  1000. visibility is cycled."
  1001. :group 'org-cycle
  1002. :type '(choice (const :tag "Never" nil)
  1003. (const :tag "Only in completely white lines" white)
  1004. (const :tag "Before first char in a line" whitestart)
  1005. (const :tag "Everywhere except in headlines" t)
  1006. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1007. (defcustom org-cycle-separator-lines 2
  1008. "Number of empty lines needed to keep an empty line between collapsed trees.
  1009. If you leave an empty line between the end of a subtree and the following
  1010. headline, this empty line is hidden when the subtree is folded.
  1011. Org-mode will leave (exactly) one empty line visible if the number of
  1012. empty lines is equal or larger to the number given in this variable.
  1013. So the default 2 means at least 2 empty lines after the end of a subtree
  1014. are needed to produce free space between a collapsed subtree and the
  1015. following headline.
  1016. If the number is negative, and the number of empty lines is at least -N,
  1017. all empty lines are shown.
  1018. Special case: when 0, never leave empty lines in collapsed view."
  1019. :group 'org-cycle
  1020. :type 'integer)
  1021. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1022. (defcustom org-pre-cycle-hook nil
  1023. "Hook that is run before visibility cycling is happening.
  1024. The function(s) in this hook must accept a single argument which indicates
  1025. the new state that will be set right after running this hook. The
  1026. argument is a symbol. Before a global state change, it can have the values
  1027. `overview', `content', or `all'. Before a local state change, it can have
  1028. the values `folded', `children', or `subtree'."
  1029. :group 'org-cycle
  1030. :type 'hook)
  1031. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1032. org-cycle-hide-drawers
  1033. org-cycle-hide-inline-tasks
  1034. org-cycle-show-empty-lines
  1035. org-optimize-window-after-visibility-change)
  1036. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1037. The function(s) in this hook must accept a single argument which indicates
  1038. the new state that was set by the most recent `org-cycle' command. The
  1039. argument is a symbol. After a global state change, it can have the values
  1040. `overview', `contents', or `all'. After a local state change, it can have
  1041. the values `folded', `children', or `subtree'."
  1042. :group 'org-cycle
  1043. :type 'hook)
  1044. (defgroup org-edit-structure nil
  1045. "Options concerning structure editing in Org-mode."
  1046. :tag "Org Edit Structure"
  1047. :group 'org-structure)
  1048. (defcustom org-odd-levels-only nil
  1049. "Non-nil means skip even levels and only use odd levels for the outline.
  1050. This has the effect that two stars are being added/taken away in
  1051. promotion/demotion commands. It also influences how levels are
  1052. handled by the exporters.
  1053. Changing it requires restart of `font-lock-mode' to become effective
  1054. for fontification also in regions already fontified.
  1055. You may also set this on a per-file basis by adding one of the following
  1056. lines to the buffer:
  1057. #+STARTUP: odd
  1058. #+STARTUP: oddeven"
  1059. :group 'org-edit-structure
  1060. :group 'org-appearance
  1061. :type 'boolean)
  1062. (defcustom org-adapt-indentation t
  1063. "Non-nil means adapt indentation to outline node level.
  1064. When this variable is set, Org assumes that you write outlines by
  1065. indenting text in each node to align with the headline (after the stars).
  1066. The following issues are influenced by this variable:
  1067. - When this is set and the *entire* text in an entry is indented, the
  1068. indentation is increased by one space in a demotion command, and
  1069. decreased by one in a promotion command. If any line in the entry
  1070. body starts with text at column 0, indentation is not changed at all.
  1071. - Property drawers and planning information is inserted indented when
  1072. this variable s set. When nil, they will not be indented.
  1073. - TAB indents a line relative to context. The lines below a headline
  1074. will be indented when this variable is set.
  1075. Note that this is all about true indentation, by adding and removing
  1076. space characters. See also `org-indent.el' which does level-dependent
  1077. indentation in a virtual way, i.e. at display time in Emacs."
  1078. :group 'org-edit-structure
  1079. :type 'boolean)
  1080. (defcustom org-special-ctrl-a/e nil
  1081. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1082. When t, `C-a' will bring back the cursor to the beginning of the
  1083. headline text, i.e. after the stars and after a possible TODO
  1084. keyword. In an item, this will be the position after bullet and
  1085. check-box, if any. When the cursor is already at that position,
  1086. another `C-a' will bring it to the beginning of the line.
  1087. `C-e' will jump to the end of the headline, ignoring the presence
  1088. of tags in the headline. A second `C-e' will then jump to the
  1089. true end of the line, after any tags. This also means that, when
  1090. this variable is non-nil, `C-e' also will never jump beyond the
  1091. end of the heading of a folded section, i.e. not after the
  1092. ellipses.
  1093. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1094. normally, going to the true line boundary first. Only a directly
  1095. following, identical keypress will bring the cursor to the
  1096. special positions.
  1097. This may also be a cons cell where the behavior for `C-a' and
  1098. `C-e' is set separately."
  1099. :group 'org-edit-structure
  1100. :type '(choice
  1101. (const :tag "off" nil)
  1102. (const :tag "on: after stars/bullet and before tags first" t)
  1103. (const :tag "reversed: true line boundary first" reversed)
  1104. (cons :tag "Set C-a and C-e separately"
  1105. (choice :tag "Special C-a"
  1106. (const :tag "off" nil)
  1107. (const :tag "on: after stars/bullet first" t)
  1108. (const :tag "reversed: before stars/bullet first" reversed))
  1109. (choice :tag "Special C-e"
  1110. (const :tag "off" nil)
  1111. (const :tag "on: before tags first" t)
  1112. (const :tag "reversed: after tags first" reversed)))))
  1113. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1114. (defcustom org-special-ctrl-k nil
  1115. "Non-nil means `C-k' will behave specially in headlines.
  1116. When nil, `C-k' will call the default `kill-line' command.
  1117. When t, the following will happen while the cursor is in the headline:
  1118. - When the cursor is at the beginning of a headline, kill the entire
  1119. line and possible the folded subtree below the line.
  1120. - When in the middle of the headline text, kill the headline up to the tags.
  1121. - When after the headline text, kill the tags."
  1122. :group 'org-edit-structure
  1123. :type 'boolean)
  1124. (defcustom org-ctrl-k-protect-subtree nil
  1125. "Non-nil means, do not delete a hidden subtree with C-k.
  1126. When set to the symbol `error', simply throw an error when C-k is
  1127. used to kill (part-of) a headline that has hidden text behind it.
  1128. Any other non-nil value will result in a query to the user, if it is
  1129. OK to kill that hidden subtree. When nil, kill without remorse."
  1130. :group 'org-edit-structure
  1131. :version "24.1"
  1132. :type '(choice
  1133. (const :tag "Do not protect hidden subtrees" nil)
  1134. (const :tag "Protect hidden subtrees with a security query" t)
  1135. (const :tag "Never kill a hidden subtree with C-k" error)))
  1136. (defcustom org-catch-invisible-edits nil
  1137. "Check if in invisible region before inserting or deleting a character.
  1138. Valid values are:
  1139. nil Do not check, so just do invisible edits.
  1140. error Throw an error and do nothing.
  1141. show Make point visible, and do the requested edit.
  1142. show-and-error Make point visible, then throw an error and abort the edit.
  1143. smart Make point visible, and do insertion/deletion if it is
  1144. adjacent to visible text and the change feels predictable.
  1145. Never delete a previously invisible character or add in the
  1146. middle or right after an invisible region. Basically, this
  1147. allows insertion and backward-delete right before ellipses.
  1148. FIXME: maybe in this case we should not even show?"
  1149. :group 'org-edit-structure
  1150. :version "24.1"
  1151. :type '(choice
  1152. (const :tag "Do not check" nil)
  1153. (const :tag "Throw error when trying to edit" error)
  1154. (const :tag "Unhide, but do not do the edit" show-and-error)
  1155. (const :tag "Show invisible part and do the edit" show)
  1156. (const :tag "Be smart and do the right thing" smart)))
  1157. (defcustom org-yank-folded-subtrees t
  1158. "Non-nil means when yanking subtrees, fold them.
  1159. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1160. it starts with a heading and all other headings in it are either children
  1161. or siblings, then fold all the subtrees. However, do this only if no
  1162. text after the yank would be swallowed into a folded tree by this action."
  1163. :group 'org-edit-structure
  1164. :type 'boolean)
  1165. (defcustom org-yank-adjusted-subtrees nil
  1166. "Non-nil means when yanking subtrees, adjust the level.
  1167. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1168. this function for details."
  1169. :group 'org-edit-structure
  1170. :type 'boolean)
  1171. (defcustom org-M-RET-may-split-line '((default . t))
  1172. "Non-nil means M-RET will split the line at the cursor position.
  1173. When nil, it will go to the end of the line before making a
  1174. new line.
  1175. You may also set this option in a different way for different
  1176. contexts. Valid contexts are:
  1177. headline when creating a new headline
  1178. item when creating a new item
  1179. table in a table field
  1180. default the value to be used for all contexts not explicitly
  1181. customized"
  1182. :group 'org-structure
  1183. :group 'org-table
  1184. :type '(choice
  1185. (const :tag "Always" t)
  1186. (const :tag "Never" nil)
  1187. (repeat :greedy t :tag "Individual contexts"
  1188. (cons
  1189. (choice :tag "Context"
  1190. (const headline)
  1191. (const item)
  1192. (const table)
  1193. (const default))
  1194. (boolean)))))
  1195. (defcustom org-insert-heading-respect-content nil
  1196. "Non-nil means insert new headings after the current subtree.
  1197. When nil, the new heading is created directly after the current line.
  1198. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1199. this variable on for the duration of the command."
  1200. :group 'org-structure
  1201. :type 'boolean)
  1202. (defcustom org-blank-before-new-entry '((heading . auto)
  1203. (plain-list-item . auto))
  1204. "Should `org-insert-heading' leave a blank line before new heading/item?
  1205. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1206. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1207. which case Org will look at the surrounding headings/items and try to
  1208. make an intelligent decision whether to insert a blank line or not.
  1209. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1210. the setting here is ignored and no empty line is inserted to avoid breaking
  1211. the list structure."
  1212. :group 'org-edit-structure
  1213. :type '(list
  1214. (cons (const heading)
  1215. (choice (const :tag "Never" nil)
  1216. (const :tag "Always" t)
  1217. (const :tag "Auto" auto)))
  1218. (cons (const plain-list-item)
  1219. (choice (const :tag "Never" nil)
  1220. (const :tag "Always" t)
  1221. (const :tag "Auto" auto)))))
  1222. (defcustom org-insert-heading-hook nil
  1223. "Hook being run after inserting a new heading."
  1224. :group 'org-edit-structure
  1225. :type 'hook)
  1226. (defcustom org-enable-fixed-width-editor t
  1227. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1228. This currently only means they are never auto-wrapped.
  1229. When nil, such lines will be treated like ordinary lines.
  1230. See also the QUOTE keyword."
  1231. :group 'org-edit-structure
  1232. :type 'boolean)
  1233. (defcustom org-goto-auto-isearch t
  1234. "Non-nil means typing characters in `org-goto' starts incremental search.
  1235. When nil, you can use these keybindings to navigate the buffer:
  1236. q Quit the org-goto interface
  1237. n Go to the next visible heading
  1238. p Go to the previous visible heading
  1239. f Go one heading forward on same level
  1240. b Go one heading backward on same level
  1241. u Go one heading up"
  1242. :group 'org-edit-structure
  1243. :type 'boolean)
  1244. (defgroup org-sparse-trees nil
  1245. "Options concerning sparse trees in Org-mode."
  1246. :tag "Org Sparse Trees"
  1247. :group 'org-structure)
  1248. (defcustom org-highlight-sparse-tree-matches t
  1249. "Non-nil means highlight all matches that define a sparse tree.
  1250. The highlights will automatically disappear the next time the buffer is
  1251. changed by an edit command."
  1252. :group 'org-sparse-trees
  1253. :type 'boolean)
  1254. (defcustom org-remove-highlights-with-change t
  1255. "Non-nil means any change to the buffer will remove temporary highlights.
  1256. Such highlights are created by `org-occur' and `org-clock-display'.
  1257. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1258. The highlights created by `org-preview-latex-fragment' always need
  1259. `C-c C-c' to be removed."
  1260. :group 'org-sparse-trees
  1261. :group 'org-time
  1262. :type 'boolean)
  1263. (defcustom org-occur-hook '(org-first-headline-recenter)
  1264. "Hook that is run after `org-occur' has constructed a sparse tree.
  1265. This can be used to recenter the window to show as much of the structure
  1266. as possible."
  1267. :group 'org-sparse-trees
  1268. :type 'hook)
  1269. (defgroup org-imenu-and-speedbar nil
  1270. "Options concerning imenu and speedbar in Org-mode."
  1271. :tag "Org Imenu and Speedbar"
  1272. :group 'org-structure)
  1273. (defcustom org-imenu-depth 2
  1274. "The maximum level for Imenu access to Org-mode headlines.
  1275. This also applied for speedbar access."
  1276. :group 'org-imenu-and-speedbar
  1277. :type 'integer)
  1278. (defgroup org-table nil
  1279. "Options concerning tables in Org-mode."
  1280. :tag "Org Table"
  1281. :group 'org)
  1282. (defcustom org-enable-table-editor 'optimized
  1283. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1284. When nil, such lines will be treated like ordinary lines.
  1285. When equal to the symbol `optimized', the table editor will be optimized to
  1286. do the following:
  1287. - Automatic overwrite mode in front of whitespace in table fields.
  1288. This makes the structure of the table stay in tact as long as the edited
  1289. field does not exceed the column width.
  1290. - Minimize the number of realigns. Normally, the table is aligned each time
  1291. TAB or RET are pressed to move to another field. With optimization this
  1292. happens only if changes to a field might have changed the column width.
  1293. Optimization requires replacing the functions `self-insert-command',
  1294. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1295. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1296. very good at guessing when a re-align will be necessary, but you can always
  1297. force one with \\[org-ctrl-c-ctrl-c].
  1298. If you would like to use the optimized version in Org-mode, but the
  1299. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1300. This variable can be used to turn on and off the table editor during a session,
  1301. but in order to toggle optimization, a restart is required.
  1302. See also the variable `org-table-auto-blank-field'."
  1303. :group 'org-table
  1304. :type '(choice
  1305. (const :tag "off" nil)
  1306. (const :tag "on" t)
  1307. (const :tag "on, optimized" optimized)))
  1308. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1309. (version<= emacs-version "24.1"))
  1310. "Non-nil means cluster self-insert commands for undo when possible.
  1311. If this is set, then, like in the Emacs command loop, 20 consecutive
  1312. characters will be undone together.
  1313. This is configurable, because there is some impact on typing performance."
  1314. :group 'org-table
  1315. :type 'boolean)
  1316. (defcustom org-table-tab-recognizes-table.el t
  1317. "Non-nil means TAB will automatically notice a table.el table.
  1318. When it sees such a table, it moves point into it and - if necessary -
  1319. calls `table-recognize-table'."
  1320. :group 'org-table-editing
  1321. :type 'boolean)
  1322. (defgroup org-link nil
  1323. "Options concerning links in Org-mode."
  1324. :tag "Org Link"
  1325. :group 'org)
  1326. (defvar org-link-abbrev-alist-local nil
  1327. "Buffer-local version of `org-link-abbrev-alist', which see.
  1328. The value of this is taken from the #+LINK lines.")
  1329. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1330. (defcustom org-link-abbrev-alist nil
  1331. "Alist of link abbreviations.
  1332. The car of each element is a string, to be replaced at the start of a link.
  1333. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1334. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1335. [[linkkey:tag][description]]
  1336. The 'linkkey' must be a word word, starting with a letter, followed
  1337. by letters, numbers, '-' or '_'.
  1338. If REPLACE is a string, the tag will simply be appended to create the link.
  1339. If the string contains \"%s\", the tag will be inserted there. If the string
  1340. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1341. at that point (see the function `url-hexify-string'). If the string contains
  1342. the specifier \"%(my-function)\", then the custom function `my-function' will
  1343. be invoked: this function takes the tag as its only argument and must return
  1344. a string.
  1345. REPLACE may also be a function that will be called with the tag as the
  1346. only argument to create the link, which should be returned as a string.
  1347. See the manual for examples."
  1348. :group 'org-link
  1349. :type '(repeat
  1350. (cons
  1351. (string :tag "Protocol")
  1352. (choice
  1353. (string :tag "Format")
  1354. (function)))))
  1355. (defcustom org-descriptive-links t
  1356. "Non-nil means Org will display descriptive links.
  1357. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1358. \"Org Website\", hiding the link itself and just displaying its
  1359. description. When set to `nil', Org will display the full links
  1360. literally.
  1361. You can interactively set the value of this variable by calling
  1362. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1363. :group 'org-link
  1364. :type 'boolean)
  1365. (defcustom org-link-file-path-type 'adaptive
  1366. "How the path name in file links should be stored.
  1367. Valid values are:
  1368. relative Relative to the current directory, i.e. the directory of the file
  1369. into which the link is being inserted.
  1370. absolute Absolute path, if possible with ~ for home directory.
  1371. noabbrev Absolute path, no abbreviation of home directory.
  1372. adaptive Use relative path for files in the current directory and sub-
  1373. directories of it. For other files, use an absolute path."
  1374. :group 'org-link
  1375. :type '(choice
  1376. (const relative)
  1377. (const absolute)
  1378. (const noabbrev)
  1379. (const adaptive)))
  1380. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1381. "Types of links that should be activated in Org-mode files.
  1382. This is a list of symbols, each leading to the activation of a certain link
  1383. type. In principle, it does not hurt to turn on most link types - there may
  1384. be a small gain when turning off unused link types. The types are:
  1385. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1386. angle Links in angular brackets that may contain whitespace like
  1387. <bbdb:Carsten Dominik>.
  1388. plain Plain links in normal text, no whitespace, like http://google.com.
  1389. radio Text that is matched by a radio target, see manual for details.
  1390. tag Tag settings in a headline (link to tag search).
  1391. date Time stamps (link to calendar).
  1392. footnote Footnote labels.
  1393. Changing this variable requires a restart of Emacs to become effective."
  1394. :group 'org-link
  1395. :type '(set :greedy t
  1396. (const :tag "Double bracket links" bracket)
  1397. (const :tag "Angular bracket links" angle)
  1398. (const :tag "Plain text links" plain)
  1399. (const :tag "Radio target matches" radio)
  1400. (const :tag "Tags" tag)
  1401. (const :tag "Timestamps" date)
  1402. (const :tag "Footnotes" footnote)))
  1403. (defcustom org-make-link-description-function nil
  1404. "Function to use for generating link descriptions from links.
  1405. When nil, the link location will be used. This function must take
  1406. two parameters: the first one is the link, the second one is the
  1407. description generated by `org-insert-link'. The function should
  1408. return the description to use."
  1409. :group 'org-link
  1410. :type 'function)
  1411. (defgroup org-link-store nil
  1412. "Options concerning storing links in Org-mode."
  1413. :tag "Org Store Link"
  1414. :group 'org-link)
  1415. (defcustom org-url-hexify-p t
  1416. "When non-nil, hexify URL when creating a link."
  1417. :type 'boolean
  1418. :version "24.3"
  1419. :group 'org-link-store)
  1420. (defcustom org-email-link-description-format "Email %c: %.30s"
  1421. "Format of the description part of a link to an email or usenet message.
  1422. The following %-escapes will be replaced by corresponding information:
  1423. %F full \"From\" field
  1424. %f name, taken from \"From\" field, address if no name
  1425. %T full \"To\" field
  1426. %t first name in \"To\" field, address if no name
  1427. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1428. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1429. %s subject
  1430. %d date
  1431. %m message-id.
  1432. You may use normal field width specification between the % and the letter.
  1433. This is for example useful to limit the length of the subject.
  1434. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1435. :group 'org-link-store
  1436. :type 'string)
  1437. (defcustom org-from-is-user-regexp
  1438. (let (r1 r2)
  1439. (when (and user-mail-address (not (string= user-mail-address "")))
  1440. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1441. (when (and user-full-name (not (string= user-full-name "")))
  1442. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1443. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1444. "Regexp matched against the \"From:\" header of an email or usenet message.
  1445. It should match if the message is from the user him/herself."
  1446. :group 'org-link-store
  1447. :type 'regexp)
  1448. (defcustom org-context-in-file-links t
  1449. "Non-nil means file links from `org-store-link' contain context.
  1450. A search string will be added to the file name with :: as separator and
  1451. used to find the context when the link is activated by the command
  1452. `org-open-at-point'. When this option is t, the entire active region
  1453. will be placed in the search string of the file link. If set to a
  1454. positive integer, only the first n lines of context will be stored.
  1455. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1456. negates this setting for the duration of the command."
  1457. :group 'org-link-store
  1458. :type '(choice boolean integer))
  1459. (defcustom org-keep-stored-link-after-insertion nil
  1460. "Non-nil means keep link in list for entire session.
  1461. The command `org-store-link' adds a link pointing to the current
  1462. location to an internal list. These links accumulate during a session.
  1463. The command `org-insert-link' can be used to insert links into any
  1464. Org-mode file (offering completion for all stored links). When this
  1465. option is nil, every link which has been inserted once using \\[org-insert-link]
  1466. will be removed from the list, to make completing the unused links
  1467. more efficient."
  1468. :group 'org-link-store
  1469. :type 'boolean)
  1470. (defgroup org-link-follow nil
  1471. "Options concerning following links in Org-mode."
  1472. :tag "Org Follow Link"
  1473. :group 'org-link)
  1474. (defcustom org-link-translation-function nil
  1475. "Function to translate links with different syntax to Org syntax.
  1476. This can be used to translate links created for example by the Planner
  1477. or emacs-wiki packages to Org syntax.
  1478. The function must accept two parameters, a TYPE containing the link
  1479. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1480. which is everything after the link protocol. It should return a cons
  1481. with possibly modified values of type and path.
  1482. Org contains a function for this, so if you set this variable to
  1483. `org-translate-link-from-planner', you should be able follow many
  1484. links created by planner."
  1485. :group 'org-link-follow
  1486. :type 'function)
  1487. (defcustom org-follow-link-hook nil
  1488. "Hook that is run after a link has been followed."
  1489. :group 'org-link-follow
  1490. :type 'hook)
  1491. (defcustom org-tab-follows-link nil
  1492. "Non-nil means on links TAB will follow the link.
  1493. Needs to be set before org.el is loaded.
  1494. This really should not be used, it does not make sense, and the
  1495. implementation is bad."
  1496. :group 'org-link-follow
  1497. :type 'boolean)
  1498. (defcustom org-return-follows-link nil
  1499. "Non-nil means on links RET will follow the link.
  1500. In tables, the special behavior of RET has precedence."
  1501. :group 'org-link-follow
  1502. :type 'boolean)
  1503. (defcustom org-mouse-1-follows-link
  1504. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1505. "Non-nil means mouse-1 on a link will follow the link.
  1506. A longer mouse click will still set point. Does not work on XEmacs.
  1507. Needs to be set before org.el is loaded."
  1508. :group 'org-link-follow
  1509. :type 'boolean)
  1510. (defcustom org-mark-ring-length 4
  1511. "Number of different positions to be recorded in the ring.
  1512. Changing this requires a restart of Emacs to work correctly."
  1513. :group 'org-link-follow
  1514. :type 'integer)
  1515. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1516. "Non-nil means internal links in Org files must exactly match a headline.
  1517. When nil, the link search tries to match a phrase with all words
  1518. in the search text."
  1519. :group 'org-link-follow
  1520. :version "24.1"
  1521. :type '(choice
  1522. (const :tag "Use fuzzy text search" nil)
  1523. (const :tag "Match only exact headline" t)
  1524. (const :tag "Match exact headline or query to create it"
  1525. query-to-create)))
  1526. (defcustom org-link-frame-setup
  1527. '((vm . vm-visit-folder-other-frame)
  1528. (vm-imap . vm-visit-imap-folder-other-frame)
  1529. (gnus . org-gnus-no-new-news)
  1530. (file . find-file-other-window)
  1531. (wl . wl-other-frame))
  1532. "Setup the frame configuration for following links.
  1533. When following a link with Emacs, it may often be useful to display
  1534. this link in another window or frame. This variable can be used to
  1535. set this up for the different types of links.
  1536. For VM, use any of
  1537. `vm-visit-folder'
  1538. `vm-visit-folder-other-window'
  1539. `vm-visit-folder-other-frame'
  1540. For Gnus, use any of
  1541. `gnus'
  1542. `gnus-other-frame'
  1543. `org-gnus-no-new-news'
  1544. For FILE, use any of
  1545. `find-file'
  1546. `find-file-other-window'
  1547. `find-file-other-frame'
  1548. For Wanderlust use any of
  1549. `wl'
  1550. `wl-other-frame'
  1551. For the calendar, use the variable `calendar-setup'.
  1552. For BBDB, it is currently only possible to display the matches in
  1553. another window."
  1554. :group 'org-link-follow
  1555. :type '(list
  1556. (cons (const vm)
  1557. (choice
  1558. (const vm-visit-folder)
  1559. (const vm-visit-folder-other-window)
  1560. (const vm-visit-folder-other-frame)))
  1561. (cons (const gnus)
  1562. (choice
  1563. (const gnus)
  1564. (const gnus-other-frame)
  1565. (const org-gnus-no-new-news)))
  1566. (cons (const file)
  1567. (choice
  1568. (const find-file)
  1569. (const find-file-other-window)
  1570. (const find-file-other-frame)))
  1571. (cons (const wl)
  1572. (choice
  1573. (const wl)
  1574. (const wl-other-frame)))))
  1575. (defcustom org-display-internal-link-with-indirect-buffer nil
  1576. "Non-nil means use indirect buffer to display infile links.
  1577. Activating internal links (from one location in a file to another location
  1578. in the same file) normally just jumps to the location. When the link is
  1579. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1580. is displayed in
  1581. another window. When this option is set, the other window actually displays
  1582. an indirect buffer clone of the current buffer, to avoid any visibility
  1583. changes to the current buffer."
  1584. :group 'org-link-follow
  1585. :type 'boolean)
  1586. (defcustom org-open-non-existing-files nil
  1587. "Non-nil means `org-open-file' will open non-existing files.
  1588. When nil, an error will be generated.
  1589. This variable applies only to external applications because they
  1590. might choke on non-existing files. If the link is to a file that
  1591. will be opened in Emacs, the variable is ignored."
  1592. :group 'org-link-follow
  1593. :type 'boolean)
  1594. (defcustom org-open-directory-means-index-dot-org nil
  1595. "Non-nil means a link to a directory really means to index.org.
  1596. When nil, following a directory link will run dired or open a finder/explorer
  1597. window on that directory."
  1598. :group 'org-link-follow
  1599. :type 'boolean)
  1600. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1601. "Function and arguments to call for following mailto links.
  1602. This is a list with the first element being a Lisp function, and the
  1603. remaining elements being arguments to the function. In string arguments,
  1604. %a will be replaced by the address, and %s will be replaced by the subject
  1605. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1606. :group 'org-link-follow
  1607. :type '(choice
  1608. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1609. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1610. (const :tag "message-mail" (message-mail "%a" "%s"))
  1611. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1612. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1613. "Non-nil means ask for confirmation before executing shell links.
  1614. Shell links can be dangerous: just think about a link
  1615. [[shell:rm -rf ~/*][Google Search]]
  1616. This link would show up in your Org-mode document as \"Google Search\",
  1617. but really it would remove your entire home directory.
  1618. Therefore we advise against setting this variable to nil.
  1619. Just change it to `y-or-n-p' if you want to confirm with a
  1620. single keystroke rather than having to type \"yes\"."
  1621. :group 'org-link-follow
  1622. :type '(choice
  1623. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1624. (const :tag "with y-or-n (faster)" y-or-n-p)
  1625. (const :tag "no confirmation (dangerous)" nil)))
  1626. (put 'org-confirm-shell-link-function
  1627. 'safe-local-variable
  1628. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1629. (defcustom org-confirm-shell-link-not-regexp ""
  1630. "A regexp to skip confirmation for shell links."
  1631. :group 'org-link-follow
  1632. :version "24.1"
  1633. :type 'regexp)
  1634. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1635. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1636. Elisp links can be dangerous: just think about a link
  1637. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1638. This link would show up in your Org-mode document as \"Google Search\",
  1639. but really it would remove your entire home directory.
  1640. Therefore we advise against setting this variable to nil.
  1641. Just change it to `y-or-n-p' if you want to confirm with a
  1642. single keystroke rather than having to type \"yes\"."
  1643. :group 'org-link-follow
  1644. :type '(choice
  1645. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1646. (const :tag "with y-or-n (faster)" y-or-n-p)
  1647. (const :tag "no confirmation (dangerous)" nil)))
  1648. (put 'org-confirm-shell-link-function
  1649. 'safe-local-variable
  1650. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1651. (defcustom org-confirm-elisp-link-not-regexp ""
  1652. "A regexp to skip confirmation for Elisp links."
  1653. :group 'org-link-follow
  1654. :version "24.1"
  1655. :type 'regexp)
  1656. (defconst org-file-apps-defaults-gnu
  1657. '((remote . emacs)
  1658. (system . mailcap)
  1659. (t . mailcap))
  1660. "Default file applications on a UNIX or GNU/Linux system.
  1661. See `org-file-apps'.")
  1662. (defconst org-file-apps-defaults-macosx
  1663. '((remote . emacs)
  1664. (t . "open %s")
  1665. (system . "open %s")
  1666. ("ps.gz" . "gv %s")
  1667. ("eps.gz" . "gv %s")
  1668. ("dvi" . "xdvi %s")
  1669. ("fig" . "xfig %s"))
  1670. "Default file applications on a MacOS X system.
  1671. The system \"open\" is known as a default, but we use X11 applications
  1672. for some files for which the OS does not have a good default.
  1673. See `org-file-apps'.")
  1674. (defconst org-file-apps-defaults-windowsnt
  1675. (list
  1676. '(remote . emacs)
  1677. (cons t
  1678. (list (if (featurep 'xemacs)
  1679. 'mswindows-shell-execute
  1680. 'w32-shell-execute)
  1681. "open" 'file))
  1682. (cons 'system
  1683. (list (if (featurep 'xemacs)
  1684. 'mswindows-shell-execute
  1685. 'w32-shell-execute)
  1686. "open" 'file)))
  1687. "Default file applications on a Windows NT system.
  1688. The system \"open\" is used for most files.
  1689. See `org-file-apps'.")
  1690. (defcustom org-file-apps
  1691. '((auto-mode . emacs)
  1692. ("\\.mm\\'" . default)
  1693. ("\\.x?html?\\'" . default)
  1694. ("\\.pdf\\'" . default))
  1695. "External applications for opening `file:path' items in a document.
  1696. Org-mode uses system defaults for different file types, but
  1697. you can use this variable to set the application for a given file
  1698. extension. The entries in this list are cons cells where the car identifies
  1699. files and the cdr the corresponding command. Possible values for the
  1700. file identifier are
  1701. \"string\" A string as a file identifier can be interpreted in different
  1702. ways, depending on its contents:
  1703. - Alphanumeric characters only:
  1704. Match links with this file extension.
  1705. Example: (\"pdf\" . \"evince %s\")
  1706. to open PDFs with evince.
  1707. - Regular expression: Match links where the
  1708. filename matches the regexp. If you want to
  1709. use groups here, use shy groups.
  1710. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1711. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1712. to open *.html and *.xhtml with firefox.
  1713. - Regular expression which contains (non-shy) groups:
  1714. Match links where the whole link, including \"::\", and
  1715. anything after that, matches the regexp.
  1716. In a custom command string, %1, %2, etc. are replaced with
  1717. the parts of the link that were matched by the groups.
  1718. For backwards compatibility, if a command string is given
  1719. that does not use any of the group matches, this case is
  1720. handled identically to the second one (i.e. match against
  1721. file name only).
  1722. In a custom lisp form, you can access the group matches with
  1723. (match-string n link).
  1724. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1725. to open [[file:document.pdf::5]] with evince at page 5.
  1726. `directory' Matches a directory
  1727. `remote' Matches a remote file, accessible through tramp or efs.
  1728. Remote files most likely should be visited through Emacs
  1729. because external applications cannot handle such paths.
  1730. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1731. so all files Emacs knows how to handle. Using this with
  1732. command `emacs' will open most files in Emacs. Beware that this
  1733. will also open html files inside Emacs, unless you add
  1734. (\"html\" . default) to the list as well.
  1735. t Default for files not matched by any of the other options.
  1736. `system' The system command to open files, like `open' on Windows
  1737. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1738. that will be selected if you call `C-c C-o' with a double
  1739. \\[universal-argument] \\[universal-argument] prefix.
  1740. Possible values for the command are:
  1741. `emacs' The file will be visited by the current Emacs process.
  1742. `default' Use the default application for this file type, which is the
  1743. association for t in the list, most likely in the system-specific
  1744. part.
  1745. This can be used to overrule an unwanted setting in the
  1746. system-specific variable.
  1747. `system' Use the system command for opening files, like \"open\".
  1748. This command is specified by the entry whose car is `system'.
  1749. Most likely, the system-specific version of this variable
  1750. does define this command, but you can overrule/replace it
  1751. here.
  1752. string A command to be executed by a shell; %s will be replaced
  1753. by the path to the file.
  1754. sexp A Lisp form which will be evaluated. The file path will
  1755. be available in the Lisp variable `file'.
  1756. For more examples, see the system specific constants
  1757. `org-file-apps-defaults-macosx'
  1758. `org-file-apps-defaults-windowsnt'
  1759. `org-file-apps-defaults-gnu'."
  1760. :group 'org-link-follow
  1761. :type '(repeat
  1762. (cons (choice :value ""
  1763. (string :tag "Extension")
  1764. (const :tag "System command to open files" system)
  1765. (const :tag "Default for unrecognized files" t)
  1766. (const :tag "Remote file" remote)
  1767. (const :tag "Links to a directory" directory)
  1768. (const :tag "Any files that have Emacs modes"
  1769. auto-mode))
  1770. (choice :value ""
  1771. (const :tag "Visit with Emacs" emacs)
  1772. (const :tag "Use default" default)
  1773. (const :tag "Use the system command" system)
  1774. (string :tag "Command")
  1775. (sexp :tag "Lisp form")))))
  1776. (defcustom org-doi-server-url "http://dx.doi.org/"
  1777. "The URL of the DOI server."
  1778. :type 'string
  1779. :version "24.3"
  1780. :group 'org-link-follow)
  1781. (defgroup org-refile nil
  1782. "Options concerning refiling entries in Org-mode."
  1783. :tag "Org Refile"
  1784. :group 'org)
  1785. (defcustom org-directory "~/org"
  1786. "Directory with org files.
  1787. This is just a default location to look for Org files. There is no need
  1788. at all to put your files into this directory. It is only used in the
  1789. following situations:
  1790. 1. When a capture template specifies a target file that is not an
  1791. absolute path. The path will then be interpreted relative to
  1792. `org-directory'
  1793. 2. When a capture note is filed away in an interactive way (when exiting the
  1794. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1795. with `org-directory' as the default path."
  1796. :group 'org-refile
  1797. :group 'org-remember
  1798. :group 'org-capture
  1799. :type 'directory)
  1800. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1801. "Default target for storing notes.
  1802. Used as a fall back file for org-remember.el and org-capture.el, for
  1803. templates that do not specify a target file."
  1804. :group 'org-refile
  1805. :group 'org-remember
  1806. :group 'org-capture
  1807. :type '(choice
  1808. (const :tag "Default from remember-data-file" nil)
  1809. file))
  1810. (defcustom org-goto-interface 'outline
  1811. "The default interface to be used for `org-goto'.
  1812. Allowed values are:
  1813. outline The interface shows an outline of the relevant file
  1814. and the correct heading is found by moving through
  1815. the outline or by searching with incremental search.
  1816. outline-path-completion Headlines in the current buffer are offered via
  1817. completion. This is the interface also used by
  1818. the refile command."
  1819. :group 'org-refile
  1820. :type '(choice
  1821. (const :tag "Outline" outline)
  1822. (const :tag "Outline-path-completion" outline-path-completion)))
  1823. (defcustom org-goto-max-level 5
  1824. "Maximum target level when running `org-goto' with refile interface."
  1825. :group 'org-refile
  1826. :type 'integer)
  1827. (defcustom org-reverse-note-order nil
  1828. "Non-nil means store new notes at the beginning of a file or entry.
  1829. When nil, new notes will be filed to the end of a file or entry.
  1830. This can also be a list with cons cells of regular expressions that
  1831. are matched against file names, and values."
  1832. :group 'org-remember
  1833. :group 'org-capture
  1834. :group 'org-refile
  1835. :type '(choice
  1836. (const :tag "Reverse always" t)
  1837. (const :tag "Reverse never" nil)
  1838. (repeat :tag "By file name regexp"
  1839. (cons regexp boolean))))
  1840. (defcustom org-log-refile nil
  1841. "Information to record when a task is refiled.
  1842. Possible values are:
  1843. nil Don't add anything
  1844. time Add a time stamp to the task
  1845. note Prompt for a note and add it with template `org-log-note-headings'
  1846. This option can also be set with on a per-file-basis with
  1847. #+STARTUP: nologrefile
  1848. #+STARTUP: logrefile
  1849. #+STARTUP: lognoterefile
  1850. You can have local logging settings for a subtree by setting the LOGGING
  1851. property to one or more of these keywords.
  1852. When bulk-refiling from the agenda, the value `note' is forbidden and
  1853. will temporarily be changed to `time'."
  1854. :group 'org-refile
  1855. :group 'org-progress
  1856. :version "24.1"
  1857. :type '(choice
  1858. (const :tag "No logging" nil)
  1859. (const :tag "Record timestamp" time)
  1860. (const :tag "Record timestamp with note." note)))
  1861. (defcustom org-refile-targets nil
  1862. "Targets for refiling entries with \\[org-refile].
  1863. This is a list of cons cells. Each cell contains:
  1864. - a specification of the files to be considered, either a list of files,
  1865. or a symbol whose function or variable value will be used to retrieve
  1866. a file name or a list of file names. If you use `org-agenda-files' for
  1867. that, all agenda files will be scanned for targets. Nil means consider
  1868. headings in the current buffer.
  1869. - A specification of how to find candidate refile targets. This may be
  1870. any of:
  1871. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1872. This tag has to be present in all target headlines, inheritance will
  1873. not be considered.
  1874. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1875. todo keyword.
  1876. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1877. headlines that are refiling targets.
  1878. - a cons cell (:level . N). Any headline of level N is considered a target.
  1879. Note that, when `org-odd-levels-only' is set, level corresponds to
  1880. order in hierarchy, not to the number of stars.
  1881. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1882. Note that, when `org-odd-levels-only' is set, level corresponds to
  1883. order in hierarchy, not to the number of stars.
  1884. Each element of this list generates a set of possible targets.
  1885. The union of these sets is presented (with completion) to
  1886. the user by `org-refile'.
  1887. You can set the variable `org-refile-target-verify-function' to a function
  1888. to verify each headline found by the simple criteria above.
  1889. When this variable is nil, all top-level headlines in the current buffer
  1890. are used, equivalent to the value `((nil . (:level . 1))'."
  1891. :group 'org-refile
  1892. :type '(repeat
  1893. (cons
  1894. (choice :value org-agenda-files
  1895. (const :tag "All agenda files" org-agenda-files)
  1896. (const :tag "Current buffer" nil)
  1897. (function) (variable) (file))
  1898. (choice :tag "Identify target headline by"
  1899. (cons :tag "Specific tag" (const :value :tag) (string))
  1900. (cons :tag "TODO keyword" (const :value :todo) (string))
  1901. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1902. (cons :tag "Level number" (const :value :level) (integer))
  1903. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1904. (defcustom org-refile-target-verify-function nil
  1905. "Function to verify if the headline at point should be a refile target.
  1906. The function will be called without arguments, with point at the
  1907. beginning of the headline. It should return t and leave point
  1908. where it is if the headline is a valid target for refiling.
  1909. If the target should not be selected, the function must return nil.
  1910. In addition to this, it may move point to a place from where the search
  1911. should be continued. For example, the function may decide that the entire
  1912. subtree of the current entry should be excluded and move point to the end
  1913. of the subtree."
  1914. :group 'org-refile
  1915. :type 'function)
  1916. (defcustom org-refile-use-cache nil
  1917. "Non-nil means cache refile targets to speed up the process.
  1918. The cache for a particular file will be updated automatically when
  1919. the buffer has been killed, or when any of the marker used for flagging
  1920. refile targets no longer points at a live buffer.
  1921. If you have added new entries to a buffer that might themselves be targets,
  1922. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1923. find that easier, `C-u C-u C-u C-c C-w'."
  1924. :group 'org-refile
  1925. :version "24.1"
  1926. :type 'boolean)
  1927. (defcustom org-refile-use-outline-path nil
  1928. "Non-nil means provide refile targets as paths.
  1929. So a level 3 headline will be available as level1/level2/level3.
  1930. When the value is `file', also include the file name (without directory)
  1931. into the path. In this case, you can also stop the completion after
  1932. the file name, to get entries inserted as top level in the file.
  1933. When `full-file-path', include the full file path."
  1934. :group 'org-refile
  1935. :type '(choice
  1936. (const :tag "Not" nil)
  1937. (const :tag "Yes" t)
  1938. (const :tag "Start with file name" file)
  1939. (const :tag "Start with full file path" full-file-path)))
  1940. (defcustom org-outline-path-complete-in-steps t
  1941. "Non-nil means complete the outline path in hierarchical steps.
  1942. When Org-mode uses the refile interface to select an outline path
  1943. \(see variable `org-refile-use-outline-path'), the completion of
  1944. the path can be done is a single go, or if can be done in steps down
  1945. the headline hierarchy. Going in steps is probably the best if you
  1946. do not use a special completion package like `ido' or `icicles'.
  1947. However, when using these packages, going in one step can be very
  1948. fast, while still showing the whole path to the entry."
  1949. :group 'org-refile
  1950. :type 'boolean)
  1951. (defcustom org-refile-allow-creating-parent-nodes nil
  1952. "Non-nil means allow to create new nodes as refile targets.
  1953. New nodes are then created by adding \"/new node name\" to the completion
  1954. of an existing node. When the value of this variable is `confirm',
  1955. new node creation must be confirmed by the user (recommended)
  1956. When nil, the completion must match an existing entry.
  1957. Note that, if the new heading is not seen by the criteria
  1958. listed in `org-refile-targets', multiple instances of the same
  1959. heading would be created by trying again to file under the new
  1960. heading."
  1961. :group 'org-refile
  1962. :type '(choice
  1963. (const :tag "Never" nil)
  1964. (const :tag "Always" t)
  1965. (const :tag "Prompt for confirmation" confirm)))
  1966. (defcustom org-refile-active-region-within-subtree nil
  1967. "Non-nil means also refile active region within a subtree.
  1968. By default `org-refile' doesn't allow refiling regions if they
  1969. don't contain a set of subtrees, but it might be convenient to
  1970. do so sometimes: in that case, the first line of the region is
  1971. converted to a headline before refiling."
  1972. :group 'org-refile
  1973. :version "24.1"
  1974. :type 'boolean)
  1975. (defgroup org-todo nil
  1976. "Options concerning TODO items in Org-mode."
  1977. :tag "Org TODO"
  1978. :group 'org)
  1979. (defgroup org-progress nil
  1980. "Options concerning Progress logging in Org-mode."
  1981. :tag "Org Progress"
  1982. :group 'org-time)
  1983. (defvar org-todo-interpretation-widgets
  1984. '((:tag "Sequence (cycling hits every state)" sequence)
  1985. (:tag "Type (cycling directly to DONE)" type))
  1986. "The available interpretation symbols for customizing `org-todo-keywords'.
  1987. Interested libraries should add to this list.")
  1988. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1989. "List of TODO entry keyword sequences and their interpretation.
  1990. \\<org-mode-map>This is a list of sequences.
  1991. Each sequence starts with a symbol, either `sequence' or `type',
  1992. indicating if the keywords should be interpreted as a sequence of
  1993. action steps, or as different types of TODO items. The first
  1994. keywords are states requiring action - these states will select a headline
  1995. for inclusion into the global TODO list Org-mode produces. If one of
  1996. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1997. signify that no further action is necessary. If \"|\" is not found,
  1998. the last keyword is treated as the only DONE state of the sequence.
  1999. The command \\[org-todo] cycles an entry through these states, and one
  2000. additional state where no keyword is present. For details about this
  2001. cycling, see the manual.
  2002. TODO keywords and interpretation can also be set on a per-file basis with
  2003. the special #+SEQ_TODO and #+TYP_TODO lines.
  2004. Each keyword can optionally specify a character for fast state selection
  2005. \(in combination with the variable `org-use-fast-todo-selection')
  2006. and specifiers for state change logging, using the same syntax that
  2007. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2008. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2009. indicates to record a time stamp each time this state is selected.
  2010. Each keyword may also specify if a timestamp or a note should be
  2011. recorded when entering or leaving the state, by adding additional
  2012. characters in the parenthesis after the keyword. This looks like this:
  2013. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2014. record only the time of the state change. With X and Y being either
  2015. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2016. Y when leaving the state if and only if the *target* state does not
  2017. define X. You may omit any of the fast-selection key or X or /Y,
  2018. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2019. For backward compatibility, this variable may also be just a list
  2020. of keywords. In this case the interpretation (sequence or type) will be
  2021. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2022. :group 'org-todo
  2023. :group 'org-keywords
  2024. :type '(choice
  2025. (repeat :tag "Old syntax, just keywords"
  2026. (string :tag "Keyword"))
  2027. (repeat :tag "New syntax"
  2028. (cons
  2029. (choice
  2030. :tag "Interpretation"
  2031. ;;Quick and dirty way to see
  2032. ;;`org-todo-interpretations'. This takes the
  2033. ;;place of item arguments
  2034. :convert-widget
  2035. (lambda (widget)
  2036. (widget-put widget
  2037. :args (mapcar
  2038. #'(lambda (x)
  2039. (widget-convert
  2040. (cons 'const x)))
  2041. org-todo-interpretation-widgets))
  2042. widget))
  2043. (repeat
  2044. (string :tag "Keyword"))))))
  2045. (defvar org-todo-keywords-1 nil
  2046. "All TODO and DONE keywords active in a buffer.")
  2047. (make-variable-buffer-local 'org-todo-keywords-1)
  2048. (defvar org-todo-keywords-for-agenda nil)
  2049. (defvar org-done-keywords-for-agenda nil)
  2050. (defvar org-drawers-for-agenda nil)
  2051. (defvar org-todo-keyword-alist-for-agenda nil)
  2052. (defvar org-tag-alist-for-agenda nil
  2053. "Alist of all tags from all agenda files.")
  2054. (defvar org-tag-groups-alist-for-agenda nil
  2055. "Alist of all groups tags from all current agenda files.")
  2056. (defvar org-tag-groups-alist nil)
  2057. (make-variable-buffer-local 'org-tag-groups-alist)
  2058. (defvar org-agenda-contributing-files nil)
  2059. (defvar org-not-done-keywords nil)
  2060. (make-variable-buffer-local 'org-not-done-keywords)
  2061. (defvar org-done-keywords nil)
  2062. (make-variable-buffer-local 'org-done-keywords)
  2063. (defvar org-todo-heads nil)
  2064. (make-variable-buffer-local 'org-todo-heads)
  2065. (defvar org-todo-sets nil)
  2066. (make-variable-buffer-local 'org-todo-sets)
  2067. (defvar org-todo-log-states nil)
  2068. (make-variable-buffer-local 'org-todo-log-states)
  2069. (defvar org-todo-kwd-alist nil)
  2070. (make-variable-buffer-local 'org-todo-kwd-alist)
  2071. (defvar org-todo-key-alist nil)
  2072. (make-variable-buffer-local 'org-todo-key-alist)
  2073. (defvar org-todo-key-trigger nil)
  2074. (make-variable-buffer-local 'org-todo-key-trigger)
  2075. (defcustom org-todo-interpretation 'sequence
  2076. "Controls how TODO keywords are interpreted.
  2077. This variable is in principle obsolete and is only used for
  2078. backward compatibility, if the interpretation of todo keywords is
  2079. not given already in `org-todo-keywords'. See that variable for
  2080. more information."
  2081. :group 'org-todo
  2082. :group 'org-keywords
  2083. :type '(choice (const sequence)
  2084. (const type)))
  2085. (defcustom org-use-fast-todo-selection t
  2086. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2087. This variable describes if and under what circumstances the cycling
  2088. mechanism for TODO keywords will be replaced by a single-key, direct
  2089. selection scheme.
  2090. When nil, fast selection is never used.
  2091. When the symbol `prefix', it will be used when `org-todo' is called
  2092. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2093. `C-u t' in an agenda buffer.
  2094. When t, fast selection is used by default. In this case, the prefix
  2095. argument forces cycling instead.
  2096. In all cases, the special interface is only used if access keys have
  2097. actually been assigned by the user, i.e. if keywords in the configuration
  2098. are followed by a letter in parenthesis, like TODO(t)."
  2099. :group 'org-todo
  2100. :type '(choice
  2101. (const :tag "Never" nil)
  2102. (const :tag "By default" t)
  2103. (const :tag "Only with C-u C-c C-t" prefix)))
  2104. (defcustom org-provide-todo-statistics t
  2105. "Non-nil means update todo statistics after insert and toggle.
  2106. ALL-HEADLINES means update todo statistics by including headlines
  2107. with no TODO keyword as well, counting them as not done.
  2108. A list of TODO keywords means the same, but skip keywords that are
  2109. not in this list.
  2110. When this is set, todo statistics is updated in the parent of the
  2111. current entry each time a todo state is changed."
  2112. :group 'org-todo
  2113. :type '(choice
  2114. (const :tag "Yes, only for TODO entries" t)
  2115. (const :tag "Yes, including all entries" 'all-headlines)
  2116. (repeat :tag "Yes, for TODOs in this list"
  2117. (string :tag "TODO keyword"))
  2118. (other :tag "No TODO statistics" nil)))
  2119. (defcustom org-hierarchical-todo-statistics t
  2120. "Non-nil means TODO statistics covers just direct children.
  2121. When nil, all entries in the subtree are considered.
  2122. This has only an effect if `org-provide-todo-statistics' is set.
  2123. To set this to nil for only a single subtree, use a COOKIE_DATA
  2124. property and include the word \"recursive\" into the value."
  2125. :group 'org-todo
  2126. :type 'boolean)
  2127. (defcustom org-after-todo-state-change-hook nil
  2128. "Hook which is run after the state of a TODO item was changed.
  2129. The new state (a string with a TODO keyword, or nil) is available in the
  2130. Lisp variable `org-state'."
  2131. :group 'org-todo
  2132. :type 'hook)
  2133. (defvar org-blocker-hook nil
  2134. "Hook for functions that are allowed to block a state change.
  2135. Functions in this hook should not modify the buffer.
  2136. Each function gets as its single argument a property list,
  2137. see `org-trigger-hook' for more information about this list.
  2138. If any of the functions in this hook returns nil, the state change
  2139. is blocked.")
  2140. (defvar org-trigger-hook nil
  2141. "Hook for functions that are triggered by a state change.
  2142. Each function gets as its single argument a property list with at
  2143. least the following elements:
  2144. (:type type-of-change :position pos-at-entry-start
  2145. :from old-state :to new-state)
  2146. Depending on the type, more properties may be present.
  2147. This mechanism is currently implemented for:
  2148. TODO state changes
  2149. ------------------
  2150. :type todo-state-change
  2151. :from previous state (keyword as a string), or nil, or a symbol
  2152. 'todo' or 'done', to indicate the general type of state.
  2153. :to new state, like in :from")
  2154. (defcustom org-enforce-todo-dependencies nil
  2155. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2156. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2157. be blocked if any prior sibling is not yet done.
  2158. Finally, if the parent is blocked because of ordered siblings of its own,
  2159. the child will also be blocked."
  2160. :set (lambda (var val)
  2161. (set var val)
  2162. (if val
  2163. (add-hook 'org-blocker-hook
  2164. 'org-block-todo-from-children-or-siblings-or-parent)
  2165. (remove-hook 'org-blocker-hook
  2166. 'org-block-todo-from-children-or-siblings-or-parent)))
  2167. :group 'org-todo
  2168. :type 'boolean)
  2169. (defcustom org-enforce-todo-checkbox-dependencies nil
  2170. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2171. When this is nil, checkboxes have no influence on switching TODO states.
  2172. When non-nil, you first need to check off all check boxes before the TODO
  2173. entry can be switched to DONE.
  2174. This variable needs to be set before org.el is loaded, and you need to
  2175. restart Emacs after a change to make the change effective. The only way
  2176. to change is while Emacs is running is through the customize interface."
  2177. :set (lambda (var val)
  2178. (set var val)
  2179. (if val
  2180. (add-hook 'org-blocker-hook
  2181. 'org-block-todo-from-checkboxes)
  2182. (remove-hook 'org-blocker-hook
  2183. 'org-block-todo-from-checkboxes)))
  2184. :group 'org-todo
  2185. :type 'boolean)
  2186. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2187. "Non-nil means inserting a TODO heading is treated as state change.
  2188. So when the command \\[org-insert-todo-heading] is used, state change
  2189. logging will apply if appropriate. When nil, the new TODO item will
  2190. be inserted directly, and no logging will take place."
  2191. :group 'org-todo
  2192. :type 'boolean)
  2193. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2194. "Non-nil means switching TODO states with S-cursor counts as state change.
  2195. This is the default behavior. However, setting this to nil allows a
  2196. convenient way to select a TODO state and bypass any logging associated
  2197. with that."
  2198. :group 'org-todo
  2199. :type 'boolean)
  2200. (defcustom org-todo-state-tags-triggers nil
  2201. "Tag changes that should be triggered by TODO state changes.
  2202. This is a list. Each entry is
  2203. (state-change (tag . flag) .......)
  2204. State-change can be a string with a state, and empty string to indicate the
  2205. state that has no TODO keyword, or it can be one of the symbols `todo'
  2206. or `done', meaning any not-done or done state, respectively."
  2207. :group 'org-todo
  2208. :group 'org-tags
  2209. :type '(repeat
  2210. (cons (choice :tag "When changing to"
  2211. (const :tag "Not-done state" todo)
  2212. (const :tag "Done state" done)
  2213. (string :tag "State"))
  2214. (repeat
  2215. (cons :tag "Tag action"
  2216. (string :tag "Tag")
  2217. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2218. (defcustom org-log-done nil
  2219. "Information to record when a task moves to the DONE state.
  2220. Possible values are:
  2221. nil Don't add anything, just change the keyword
  2222. time Add a time stamp to the task
  2223. note Prompt for a note and add it with template `org-log-note-headings'
  2224. This option can also be set with on a per-file-basis with
  2225. #+STARTUP: nologdone
  2226. #+STARTUP: logdone
  2227. #+STARTUP: lognotedone
  2228. You can have local logging settings for a subtree by setting the LOGGING
  2229. property to one or more of these keywords."
  2230. :group 'org-todo
  2231. :group 'org-progress
  2232. :type '(choice
  2233. (const :tag "No logging" nil)
  2234. (const :tag "Record CLOSED timestamp" time)
  2235. (const :tag "Record CLOSED timestamp with note." note)))
  2236. ;; Normalize old uses of org-log-done.
  2237. (cond
  2238. ((eq org-log-done t) (setq org-log-done 'time))
  2239. ((and (listp org-log-done) (memq 'done org-log-done))
  2240. (setq org-log-done 'note)))
  2241. (defcustom org-log-reschedule nil
  2242. "Information to record when the scheduling date of a tasks is modified.
  2243. Possible values are:
  2244. nil Don't add anything, just change the date
  2245. time Add a time stamp to the task
  2246. note Prompt for a note and add it with template `org-log-note-headings'
  2247. This option can also be set with on a per-file-basis with
  2248. #+STARTUP: nologreschedule
  2249. #+STARTUP: logreschedule
  2250. #+STARTUP: lognotereschedule"
  2251. :group 'org-todo
  2252. :group 'org-progress
  2253. :type '(choice
  2254. (const :tag "No logging" nil)
  2255. (const :tag "Record timestamp" time)
  2256. (const :tag "Record timestamp with note." note)))
  2257. (defcustom org-log-redeadline nil
  2258. "Information to record when the deadline date of a tasks is modified.
  2259. Possible values are:
  2260. nil Don't add anything, just change the date
  2261. time Add a time stamp to the task
  2262. note Prompt for a note and add it with template `org-log-note-headings'
  2263. This option can also be set with on a per-file-basis with
  2264. #+STARTUP: nologredeadline
  2265. #+STARTUP: logredeadline
  2266. #+STARTUP: lognoteredeadline
  2267. You can have local logging settings for a subtree by setting the LOGGING
  2268. property to one or more of these keywords."
  2269. :group 'org-todo
  2270. :group 'org-progress
  2271. :type '(choice
  2272. (const :tag "No logging" nil)
  2273. (const :tag "Record timestamp" time)
  2274. (const :tag "Record timestamp with note." note)))
  2275. (defcustom org-log-note-clock-out nil
  2276. "Non-nil means record a note when clocking out of an item.
  2277. This can also be configured on a per-file basis by adding one of
  2278. the following lines anywhere in the buffer:
  2279. #+STARTUP: lognoteclock-out
  2280. #+STARTUP: nolognoteclock-out"
  2281. :group 'org-todo
  2282. :group 'org-progress
  2283. :type 'boolean)
  2284. (defcustom org-log-done-with-time t
  2285. "Non-nil means the CLOSED time stamp will contain date and time.
  2286. When nil, only the date will be recorded."
  2287. :group 'org-progress
  2288. :type 'boolean)
  2289. (defcustom org-log-note-headings
  2290. '((done . "CLOSING NOTE %t")
  2291. (state . "State %-12s from %-12S %t")
  2292. (note . "Note taken on %t")
  2293. (reschedule . "Rescheduled from %S on %t")
  2294. (delschedule . "Not scheduled, was %S on %t")
  2295. (redeadline . "New deadline from %S on %t")
  2296. (deldeadline . "Removed deadline, was %S on %t")
  2297. (refile . "Refiled on %t")
  2298. (clock-out . ""))
  2299. "Headings for notes added to entries.
  2300. The value is an alist, with the car being a symbol indicating the note
  2301. context, and the cdr is the heading to be used. The heading may also be the
  2302. empty string.
  2303. %t in the heading will be replaced by a time stamp.
  2304. %T will be an active time stamp instead the default inactive one
  2305. %d will be replaced by a short-format time stamp.
  2306. %D will be replaced by an active short-format time stamp.
  2307. %s will be replaced by the new TODO state, in double quotes.
  2308. %S will be replaced by the old TODO state, in double quotes.
  2309. %u will be replaced by the user name.
  2310. %U will be replaced by the full user name.
  2311. In fact, it is not a good idea to change the `state' entry, because
  2312. agenda log mode depends on the format of these entries."
  2313. :group 'org-todo
  2314. :group 'org-progress
  2315. :type '(list :greedy t
  2316. (cons (const :tag "Heading when closing an item" done) string)
  2317. (cons (const :tag
  2318. "Heading when changing todo state (todo sequence only)"
  2319. state) string)
  2320. (cons (const :tag "Heading when just taking a note" note) string)
  2321. (cons (const :tag "Heading when clocking out" clock-out) string)
  2322. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2323. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2324. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2325. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2326. (cons (const :tag "Heading when refiling" refile) string)))
  2327. (unless (assq 'note org-log-note-headings)
  2328. (push '(note . "%t") org-log-note-headings))
  2329. (defcustom org-log-into-drawer nil
  2330. "Non-nil means insert state change notes and time stamps into a drawer.
  2331. When nil, state changes notes will be inserted after the headline and
  2332. any scheduling and clock lines, but not inside a drawer.
  2333. The value of this variable should be the name of the drawer to use.
  2334. LOGBOOK is proposed as the default drawer for this purpose, you can
  2335. also set this to a string to define the drawer of your choice.
  2336. A value of t is also allowed, representing \"LOGBOOK\".
  2337. A value of t or nil can also be set with on a per-file-basis with
  2338. #+STARTUP: logdrawer
  2339. #+STARTUP: nologdrawer
  2340. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2341. will be ignored.
  2342. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2343. a subtree."
  2344. :group 'org-todo
  2345. :group 'org-progress
  2346. :type '(choice
  2347. (const :tag "Not into a drawer" nil)
  2348. (const :tag "LOGBOOK" t)
  2349. (string :tag "Other")))
  2350. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2351. (defun org-log-into-drawer ()
  2352. "Return the value of `org-log-into-drawer', but let properties overrule.
  2353. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2354. used instead of the default value."
  2355. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2356. (cond
  2357. ((not p) org-log-into-drawer)
  2358. ((equal p "nil") nil)
  2359. ((equal p "t") "LOGBOOK")
  2360. (t p))))
  2361. (defcustom org-log-state-notes-insert-after-drawers nil
  2362. "Non-nil means insert state change notes after any drawers in entry.
  2363. Only the drawers that *immediately* follow the headline and the
  2364. deadline/scheduled line are skipped.
  2365. When nil, insert notes right after the heading and perhaps the line
  2366. with deadline/scheduling if present.
  2367. This variable will have no effect if `org-log-into-drawer' is
  2368. set."
  2369. :group 'org-todo
  2370. :group 'org-progress
  2371. :type 'boolean)
  2372. (defcustom org-log-states-order-reversed t
  2373. "Non-nil means the latest state note will be directly after heading.
  2374. When nil, the state change notes will be ordered according to time.
  2375. This option can also be set with on a per-file-basis with
  2376. #+STARTUP: logstatesreversed
  2377. #+STARTUP: nologstatesreversed"
  2378. :group 'org-todo
  2379. :group 'org-progress
  2380. :type 'boolean)
  2381. (defcustom org-todo-repeat-to-state nil
  2382. "The TODO state to which a repeater should return the repeating task.
  2383. By default this is the first task in a TODO sequence, or the previous state
  2384. in a TODO_TYP set. But you can specify another task here.
  2385. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2386. :group 'org-todo
  2387. :version "24.1"
  2388. :type '(choice (const :tag "Head of sequence" nil)
  2389. (string :tag "Specific state")))
  2390. (defcustom org-log-repeat 'time
  2391. "Non-nil means record moving through the DONE state when triggering repeat.
  2392. An auto-repeating task is immediately switched back to TODO when
  2393. marked DONE. If you are not logging state changes (by adding \"@\"
  2394. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2395. record a closing note, there will be no record of the task moving
  2396. through DONE. This variable forces taking a note anyway.
  2397. nil Don't force a record
  2398. time Record a time stamp
  2399. note Prompt for a note and add it with template `org-log-note-headings'
  2400. This option can also be set with on a per-file-basis with
  2401. #+STARTUP: nologrepeat
  2402. #+STARTUP: logrepeat
  2403. #+STARTUP: lognoterepeat
  2404. You can have local logging settings for a subtree by setting the LOGGING
  2405. property to one or more of these keywords."
  2406. :group 'org-todo
  2407. :group 'org-progress
  2408. :type '(choice
  2409. (const :tag "Don't force a record" nil)
  2410. (const :tag "Force recording the DONE state" time)
  2411. (const :tag "Force recording a note with the DONE state" note)))
  2412. (defgroup org-priorities nil
  2413. "Priorities in Org-mode."
  2414. :tag "Org Priorities"
  2415. :group 'org-todo)
  2416. (defcustom org-enable-priority-commands t
  2417. "Non-nil means priority commands are active.
  2418. When nil, these commands will be disabled, so that you never accidentally
  2419. set a priority."
  2420. :group 'org-priorities
  2421. :type 'boolean)
  2422. (defcustom org-highest-priority ?A
  2423. "The highest priority of TODO items. A character like ?A, ?B etc.
  2424. Must have a smaller ASCII number than `org-lowest-priority'."
  2425. :group 'org-priorities
  2426. :type 'character)
  2427. (defcustom org-lowest-priority ?C
  2428. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2429. Must have a larger ASCII number than `org-highest-priority'."
  2430. :group 'org-priorities
  2431. :type 'character)
  2432. (defcustom org-default-priority ?B
  2433. "The default priority of TODO items.
  2434. This is the priority an item gets if no explicit priority is given.
  2435. When starting to cycle on an empty priority the first step in the cycle
  2436. depends on `org-priority-start-cycle-with-default'. The resulting first
  2437. step priority must not exceed the range from `org-highest-priority' to
  2438. `org-lowest-priority' which means that `org-default-priority' has to be
  2439. in this range exclusive or inclusive the range boundaries. Else the
  2440. first step refuses to set the default and the second will fall back
  2441. to (depending on the command used) the highest or lowest priority."
  2442. :group 'org-priorities
  2443. :type 'character)
  2444. (defcustom org-priority-start-cycle-with-default t
  2445. "Non-nil means start with default priority when starting to cycle.
  2446. When this is nil, the first step in the cycle will be (depending on the
  2447. command used) one higher or lower than the default priority.
  2448. See also `org-default-priority'."
  2449. :group 'org-priorities
  2450. :type 'boolean)
  2451. (defcustom org-get-priority-function nil
  2452. "Function to extract the priority from a string.
  2453. The string is normally the headline. If this is nil Org computes the
  2454. priority from the priority cookie like [#A] in the headline. It returns
  2455. an integer, increasing by 1000 for each priority level.
  2456. The user can set a different function here, which should take a string
  2457. as an argument and return the numeric priority."
  2458. :group 'org-priorities
  2459. :version "24.1"
  2460. :type 'function)
  2461. (defgroup org-time nil
  2462. "Options concerning time stamps and deadlines in Org-mode."
  2463. :tag "Org Time"
  2464. :group 'org)
  2465. (defcustom org-insert-labeled-timestamps-at-point nil
  2466. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2467. When nil, these labeled time stamps are forces into the second line of an
  2468. entry, just after the headline. When scheduling from the global TODO list,
  2469. the time stamp will always be forced into the second line."
  2470. :group 'org-time
  2471. :type 'boolean)
  2472. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2473. "Formats for `format-time-string' which are used for time stamps.
  2474. It is not recommended to change this constant.")
  2475. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2476. "Number of minutes to round time stamps to.
  2477. These are two values, the first applies when first creating a time stamp.
  2478. The second applies when changing it with the commands `S-up' and `S-down'.
  2479. When changing the time stamp, this means that it will change in steps
  2480. of N minutes, as given by the second value.
  2481. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2482. numbers should be factors of 60, so for example 5, 10, 15.
  2483. When this is larger than 1, you can still force an exact time stamp by using
  2484. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2485. and by using a prefix arg to `S-up/down' to specify the exact number
  2486. of minutes to shift."
  2487. :group 'org-time
  2488. :get #'(lambda (var) ; Make sure both elements are there
  2489. (if (integerp (default-value var))
  2490. (list (default-value var) 5)
  2491. (default-value var)))
  2492. :type '(list
  2493. (integer :tag "when inserting times")
  2494. (integer :tag "when modifying times")))
  2495. ;; Normalize old customizations of this variable.
  2496. (when (integerp org-time-stamp-rounding-minutes)
  2497. (setq org-time-stamp-rounding-minutes
  2498. (list org-time-stamp-rounding-minutes
  2499. org-time-stamp-rounding-minutes)))
  2500. (defcustom org-display-custom-times nil
  2501. "Non-nil means overlay custom formats over all time stamps.
  2502. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2503. To turn this on on a per-file basis, insert anywhere in the file:
  2504. #+STARTUP: customtime"
  2505. :group 'org-time
  2506. :set 'set-default
  2507. :type 'sexp)
  2508. (make-variable-buffer-local 'org-display-custom-times)
  2509. (defcustom org-time-stamp-custom-formats
  2510. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2511. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2512. These are overlaid over the default ISO format if the variable
  2513. `org-display-custom-times' is set. Time like %H:%M should be at the
  2514. end of the second format. The custom formats are also honored by export
  2515. commands, if custom time display is turned on at the time of export."
  2516. :group 'org-time
  2517. :type 'sexp)
  2518. (defun org-time-stamp-format (&optional long inactive)
  2519. "Get the right format for a time string."
  2520. (let ((f (if long (cdr org-time-stamp-formats)
  2521. (car org-time-stamp-formats))))
  2522. (if inactive
  2523. (concat "[" (substring f 1 -1) "]")
  2524. f)))
  2525. (defcustom org-time-clocksum-format
  2526. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2527. "The format string used when creating CLOCKSUM lines.
  2528. This is also used when Org mode generates a time duration.
  2529. The value can be a single format string containing two
  2530. %-sequences, which will be filled with the number of hours and
  2531. minutes in that order.
  2532. Alternatively, the value can be a plist associating any of the
  2533. keys :years, :months, :weeks, :days, :hours or :minutes with
  2534. format strings. The time duration is formatted using only the
  2535. time components that are needed and concatenating the results.
  2536. If a time unit in absent, it falls back to the next smallest
  2537. unit.
  2538. The keys :require-years, :require-months, :require-days,
  2539. :require-weeks, :require-hours, :require-minutes are also
  2540. meaningful. A non-nil value for these keys indicates that the
  2541. corresponding time component should always be included, even if
  2542. its value is 0.
  2543. For example,
  2544. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2545. :require-minutes t)
  2546. means durations longer than a day will be expressed in days,
  2547. hours and minutes, and durations less than a day will always be
  2548. expressed in hours and minutes (even for durations less than an
  2549. hour).
  2550. The value
  2551. \(:days \"%dd\" :minutes \"%dm\")
  2552. means durations longer than a day will be expressed in days and
  2553. minutes, and durations less than a day will be expressed entirely
  2554. in minutes (even for durations longer than an hour)."
  2555. :group 'org-time
  2556. :group 'org-clock
  2557. :version "24.4"
  2558. :package-version '(Org . "8.0")
  2559. :type '(choice (string :tag "Format string")
  2560. (set :tag "Plist"
  2561. (group :inline t (const :tag "Years" :years)
  2562. (string :tag "Format string"))
  2563. (group :inline t
  2564. (const :tag "Always show years" :require-years)
  2565. (const t))
  2566. (group :inline t (const :tag "Months" :months)
  2567. (string :tag "Format string"))
  2568. (group :inline t
  2569. (const :tag "Always show months" :require-months)
  2570. (const t))
  2571. (group :inline t (const :tag "Weeks" :weeks)
  2572. (string :tag "Format string"))
  2573. (group :inline t
  2574. (const :tag "Always show weeks" :require-weeks)
  2575. (const t))
  2576. (group :inline t (const :tag "Days" :days)
  2577. (string :tag "Format string"))
  2578. (group :inline t
  2579. (const :tag "Always show days" :require-days)
  2580. (const t))
  2581. (group :inline t (const :tag "Hours" :hours)
  2582. (string :tag "Format string"))
  2583. (group :inline t
  2584. (const :tag "Always show hours" :require-hours)
  2585. (const t))
  2586. (group :inline t (const :tag "Minutes" :minutes)
  2587. (string :tag "Format string"))
  2588. (group :inline t
  2589. (const :tag "Always show minutes" :require-minutes)
  2590. (const t)))))
  2591. (defcustom org-time-clocksum-use-fractional nil
  2592. "When non-nil, \\[org-clock-display] uses fractional times.
  2593. See `org-time-clocksum-format' for more on time clock formats."
  2594. :group 'org-time
  2595. :group 'org-clock
  2596. :version "24.3"
  2597. :type 'boolean)
  2598. (defcustom org-time-clocksum-use-effort-durations nil
  2599. "When non-nil, \\[org-clock-display] uses effort durations.
  2600. E.g. by default, one day is considered to be a 8 hours effort,
  2601. so a task that has been clocked for 16 hours will be displayed
  2602. as during 2 days in the clock display or in the clocktable.
  2603. See `org-effort-durations' on how to set effort durations
  2604. and `org-time-clocksum-format' for more on time clock formats."
  2605. :group 'org-time
  2606. :group 'org-clock
  2607. :version "24.4"
  2608. :package-version '(Org . "8.0")
  2609. :type 'boolean)
  2610. (defcustom org-time-clocksum-fractional-format "%.2f"
  2611. "The format string used when creating CLOCKSUM lines,
  2612. or when Org mode generates a time duration, if
  2613. `org-time-clocksum-use-fractional' is enabled.
  2614. The value can be a single format string containing one
  2615. %-sequence, which will be filled with the number of hours as
  2616. a float.
  2617. Alternatively, the value can be a plist associating any of the
  2618. keys :years, :months, :weeks, :days, :hours or :minutes with
  2619. a format string. The time duration is formatted using the
  2620. largest time unit which gives a non-zero integer part. If all
  2621. specified formats have zero integer part, the smallest time unit
  2622. is used."
  2623. :group 'org-time
  2624. :type '(choice (string :tag "Format string")
  2625. (set (group :inline t (const :tag "Years" :years)
  2626. (string :tag "Format string"))
  2627. (group :inline t (const :tag "Months" :months)
  2628. (string :tag "Format string"))
  2629. (group :inline t (const :tag "Weeks" :weeks)
  2630. (string :tag "Format string"))
  2631. (group :inline t (const :tag "Days" :days)
  2632. (string :tag "Format string"))
  2633. (group :inline t (const :tag "Hours" :hours)
  2634. (string :tag "Format string"))
  2635. (group :inline t (const :tag "Minutes" :minutes)
  2636. (string :tag "Format string")))))
  2637. (defcustom org-deadline-warning-days 14
  2638. "Number of days before expiration during which a deadline becomes active.
  2639. This variable governs the display in sparse trees and in the agenda.
  2640. When 0 or negative, it means use this number (the absolute value of it)
  2641. even if a deadline has a different individual lead time specified.
  2642. Custom commands can set this variable in the options section."
  2643. :group 'org-time
  2644. :group 'org-agenda-daily/weekly
  2645. :type 'integer)
  2646. (defcustom org-scheduled-delay-days 0
  2647. "Number of days before a scheduled item becomes active.
  2648. This variable governs the display in sparse trees and in the agenda.
  2649. The default value (i.e. 0) means: don't delay scheduled item.
  2650. When negative, it means use this number (the absolute value of it)
  2651. even if a scheduled item has a different individual delay time
  2652. specified.
  2653. Custom commands can set this variable in the options section."
  2654. :group 'org-time
  2655. :group 'org-agenda-daily/weekly
  2656. :version "24.4"
  2657. :package-version '(Org . "8.0")
  2658. :type 'integer)
  2659. (defcustom org-read-date-prefer-future t
  2660. "Non-nil means assume future for incomplete date input from user.
  2661. This affects the following situations:
  2662. 1. The user gives a month but not a year.
  2663. For example, if it is April and you enter \"feb 2\", this will be read
  2664. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2665. 2. The user gives a day, but no month.
  2666. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2667. read this as the third of *next* month. However, if you enter \"17\",
  2668. it will be considered as *this* month.
  2669. If you set this variable to the symbol `time', then also the following
  2670. will work:
  2671. 3. If the user gives a time.
  2672. If the time is before now, it will be interpreted as tomorrow.
  2673. Currently none of this works for ISO week specifications.
  2674. When this option is nil, the current day, month and year will always be
  2675. used as defaults.
  2676. See also `org-agenda-jump-prefer-future'."
  2677. :group 'org-time
  2678. :type '(choice
  2679. (const :tag "Never" nil)
  2680. (const :tag "Check month and day" t)
  2681. (const :tag "Check month, day, and time" time)))
  2682. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2683. "Should the agenda jump command prefer the future for incomplete dates?
  2684. The default is to do the same as configured in `org-read-date-prefer-future'.
  2685. But you can also set a deviating value here.
  2686. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2687. :group 'org-agenda
  2688. :group 'org-time
  2689. :version "24.1"
  2690. :type '(choice
  2691. (const :tag "Use org-read-date-prefer-future"
  2692. org-read-date-prefer-future)
  2693. (const :tag "Never" nil)
  2694. (const :tag "Always" t)))
  2695. (defcustom org-read-date-force-compatible-dates t
  2696. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2697. Depending on the system Emacs is running on, certain dates cannot
  2698. be represented with the type used internally to represent time.
  2699. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2700. correctly. Some systems allow for earlier dates, some for later,
  2701. some for both. One way to find out it to insert any date into an
  2702. Org buffer, putting the cursor on the year and hitting S-up and
  2703. S-down to test the range.
  2704. When this variable is set to t, the date/time prompt will not let
  2705. you specify dates outside the 1970-2037 range, so it is certain that
  2706. these dates will work in whatever version of Emacs you are
  2707. running, and also that you can move a file from one Emacs implementation
  2708. to another. WHenever Org is forcing the year for you, it will display
  2709. a message and beep.
  2710. When this variable is nil, Org will check if the date is
  2711. representable in the specific Emacs implementation you are using.
  2712. If not, it will force a year, usually the current year, and beep
  2713. to remind you. Currently this setting is not recommended because
  2714. the likelihood that you will open your Org files in an Emacs that
  2715. has limited date range is not negligible.
  2716. A workaround for this problem is to use diary sexp dates for time
  2717. stamps outside of this range."
  2718. :group 'org-time
  2719. :version "24.1"
  2720. :type 'boolean)
  2721. (defcustom org-read-date-display-live t
  2722. "Non-nil means display current interpretation of date prompt live.
  2723. This display will be in an overlay, in the minibuffer."
  2724. :group 'org-time
  2725. :type 'boolean)
  2726. (defcustom org-read-date-popup-calendar t
  2727. "Non-nil means pop up a calendar when prompting for a date.
  2728. In the calendar, the date can be selected with mouse-1. However, the
  2729. minibuffer will also be active, and you can simply enter the date as well.
  2730. When nil, only the minibuffer will be available."
  2731. :group 'org-time
  2732. :type 'boolean)
  2733. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2734. 'org-read-date-popup-calendar)
  2735. (make-obsolete-variable
  2736. 'org-read-date-minibuffer-setup-hook
  2737. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2738. (defcustom org-read-date-minibuffer-setup-hook nil
  2739. "Hook to be used to set up keys for the date/time interface.
  2740. Add key definitions to `minibuffer-local-map', which will be a
  2741. temporary copy.
  2742. WARNING: This option is obsolete, you should use
  2743. `org-read-date-minibuffer-local-map' to set up keys."
  2744. :group 'org-time
  2745. :type 'hook)
  2746. (defcustom org-extend-today-until 0
  2747. "The hour when your day really ends. Must be an integer.
  2748. This has influence for the following applications:
  2749. - When switching the agenda to \"today\". It it is still earlier than
  2750. the time given here, the day recognized as TODAY is actually yesterday.
  2751. - When a date is read from the user and it is still before the time given
  2752. here, the current date and time will be assumed to be yesterday, 23:59.
  2753. Also, timestamps inserted in capture templates follow this rule.
  2754. IMPORTANT: This is a feature whose implementation is and likely will
  2755. remain incomplete. Really, it is only here because past midnight seems to
  2756. be the favorite working time of John Wiegley :-)"
  2757. :group 'org-time
  2758. :type 'integer)
  2759. (defcustom org-use-effective-time nil
  2760. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2761. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2762. \"effective time\" of any timestamps between midnight and 8am will be
  2763. 23:59 of the previous day."
  2764. :group 'org-time
  2765. :version "24.1"
  2766. :type 'boolean)
  2767. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2768. "When non-nil, use the last clock out time for `org-todo'.
  2769. Note that this option has precedence over the combined use of
  2770. `org-use-effective-time' and `org-extend-today-until'."
  2771. :group 'org-time
  2772. :version "24.4"
  2773. :package-version '(Org . "8.0")
  2774. :type 'boolean)
  2775. (defcustom org-edit-timestamp-down-means-later nil
  2776. "Non-nil means S-down will increase the time in a time stamp.
  2777. When nil, S-up will increase."
  2778. :group 'org-time
  2779. :type 'boolean)
  2780. (defcustom org-calendar-follow-timestamp-change t
  2781. "Non-nil means make the calendar window follow timestamp changes.
  2782. When a timestamp is modified and the calendar window is visible, it will be
  2783. moved to the new date."
  2784. :group 'org-time
  2785. :type 'boolean)
  2786. (defgroup org-tags nil
  2787. "Options concerning tags in Org-mode."
  2788. :tag "Org Tags"
  2789. :group 'org)
  2790. (defcustom org-tag-alist nil
  2791. "List of tags allowed in Org-mode files.
  2792. When this list is nil, Org-mode will base TAG input on what is already in the
  2793. buffer.
  2794. The value of this variable is an alist, the car of each entry must be a
  2795. keyword as a string, the cdr may be a character that is used to select
  2796. that tag through the fast-tag-selection interface.
  2797. See the manual for details."
  2798. :group 'org-tags
  2799. :type '(repeat
  2800. (choice
  2801. (cons (string :tag "Tag name")
  2802. (character :tag "Access char"))
  2803. (list :tag "Start radio group"
  2804. (const :startgroup)
  2805. (option (string :tag "Group description")))
  2806. (list :tag "Group tags delimiter"
  2807. (const :grouptags))
  2808. (list :tag "End radio group"
  2809. (const :endgroup)
  2810. (option (string :tag "Group description")))
  2811. (const :tag "New line" (:newline)))))
  2812. (defcustom org-tag-persistent-alist nil
  2813. "List of tags that will always appear in all Org-mode files.
  2814. This is in addition to any in buffer settings or customizations
  2815. of `org-tag-alist'.
  2816. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2817. The value of this variable is an alist, the car of each entry must be a
  2818. keyword as a string, the cdr may be a character that is used to select
  2819. that tag through the fast-tag-selection interface.
  2820. See the manual for details.
  2821. To disable these tags on a per-file basis, insert anywhere in the file:
  2822. #+STARTUP: noptag"
  2823. :group 'org-tags
  2824. :type '(repeat
  2825. (choice
  2826. (cons (string :tag "Tag name")
  2827. (character :tag "Access char"))
  2828. (const :tag "Start radio group" (:startgroup))
  2829. (const :tag "Group tags delimiter" (:grouptags))
  2830. (const :tag "End radio group" (:endgroup))
  2831. (const :tag "New line" (:newline)))))
  2832. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2833. "If non-nil, always offer completion for all tags of all agenda files.
  2834. Instead of customizing this variable directly, you might want to
  2835. set it locally for capture buffers, because there no list of
  2836. tags in that file can be created dynamically (there are none).
  2837. (add-hook 'org-capture-mode-hook
  2838. (lambda ()
  2839. (set (make-local-variable
  2840. 'org-complete-tags-always-offer-all-agenda-tags)
  2841. t)))"
  2842. :group 'org-tags
  2843. :version "24.1"
  2844. :type 'boolean)
  2845. (defvar org-file-tags nil
  2846. "List of tags that can be inherited by all entries in the file.
  2847. The tags will be inherited if the variable `org-use-tag-inheritance'
  2848. says they should be.
  2849. This variable is populated from #+FILETAGS lines.")
  2850. (defcustom org-use-fast-tag-selection 'auto
  2851. "Non-nil means use fast tag selection scheme.
  2852. This is a special interface to select and deselect tags with single keys.
  2853. When nil, fast selection is never used.
  2854. When the symbol `auto', fast selection is used if and only if selection
  2855. characters for tags have been configured, either through the variable
  2856. `org-tag-alist' or through a #+TAGS line in the buffer.
  2857. When t, fast selection is always used and selection keys are assigned
  2858. automatically if necessary."
  2859. :group 'org-tags
  2860. :type '(choice
  2861. (const :tag "Always" t)
  2862. (const :tag "Never" nil)
  2863. (const :tag "When selection characters are configured" 'auto)))
  2864. (defcustom org-fast-tag-selection-single-key nil
  2865. "Non-nil means fast tag selection exits after first change.
  2866. When nil, you have to press RET to exit it.
  2867. During fast tag selection, you can toggle this flag with `C-c'.
  2868. This variable can also have the value `expert'. In this case, the window
  2869. displaying the tags menu is not even shown, until you press C-c again."
  2870. :group 'org-tags
  2871. :type '(choice
  2872. (const :tag "No" nil)
  2873. (const :tag "Yes" t)
  2874. (const :tag "Expert" expert)))
  2875. (defvar org-fast-tag-selection-include-todo nil
  2876. "Non-nil means fast tags selection interface will also offer TODO states.
  2877. This is an undocumented feature, you should not rely on it.")
  2878. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2879. "The column to which tags should be indented in a headline.
  2880. If this number is positive, it specifies the column. If it is negative,
  2881. it means that the tags should be flushright to that column. For example,
  2882. -80 works well for a normal 80 character screen.
  2883. When 0, place tags directly after headline text, with only one space in
  2884. between."
  2885. :group 'org-tags
  2886. :type 'integer)
  2887. (defcustom org-auto-align-tags t
  2888. "Non-nil keeps tags aligned when modifying headlines.
  2889. Some operations (i.e. demoting) change the length of a headline and
  2890. therefore shift the tags around. With this option turned on, after
  2891. each such operation the tags are again aligned to `org-tags-column'."
  2892. :group 'org-tags
  2893. :type 'boolean)
  2894. (defcustom org-use-tag-inheritance t
  2895. "Non-nil means tags in levels apply also for sublevels.
  2896. When nil, only the tags directly given in a specific line apply there.
  2897. This may also be a list of tags that should be inherited, or a regexp that
  2898. matches tags that should be inherited. Additional control is possible
  2899. with the variable `org-tags-exclude-from-inheritance' which gives an
  2900. explicit list of tags to be excluded from inheritance, even if the value of
  2901. `org-use-tag-inheritance' would select it for inheritance.
  2902. If this option is t, a match early-on in a tree can lead to a large
  2903. number of matches in the subtree when constructing the agenda or creating
  2904. a sparse tree. If you only want to see the first match in a tree during
  2905. a search, check out the variable `org-tags-match-list-sublevels'."
  2906. :group 'org-tags
  2907. :type '(choice
  2908. (const :tag "Not" nil)
  2909. (const :tag "Always" t)
  2910. (repeat :tag "Specific tags" (string :tag "Tag"))
  2911. (regexp :tag "Tags matched by regexp")))
  2912. (defcustom org-tags-exclude-from-inheritance nil
  2913. "List of tags that should never be inherited.
  2914. This is a way to exclude a few tags from inheritance. For way to do
  2915. the opposite, to actively allow inheritance for selected tags,
  2916. see the variable `org-use-tag-inheritance'."
  2917. :group 'org-tags
  2918. :type '(repeat (string :tag "Tag")))
  2919. (defun org-tag-inherit-p (tag)
  2920. "Check if TAG is one that should be inherited."
  2921. (cond
  2922. ((member tag org-tags-exclude-from-inheritance) nil)
  2923. ((eq org-use-tag-inheritance t) t)
  2924. ((not org-use-tag-inheritance) nil)
  2925. ((stringp org-use-tag-inheritance)
  2926. (string-match org-use-tag-inheritance tag))
  2927. ((listp org-use-tag-inheritance)
  2928. (member tag org-use-tag-inheritance))
  2929. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2930. (defcustom org-tags-match-list-sublevels t
  2931. "Non-nil means list also sublevels of headlines matching a search.
  2932. This variable applies to tags/property searches, and also to stuck
  2933. projects because this search is based on a tags match as well.
  2934. When set to the symbol `indented', sublevels are indented with
  2935. leading dots.
  2936. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2937. the sublevels of a headline matching a tag search often also match
  2938. the same search. Listing all of them can create very long lists.
  2939. Setting this variable to nil causes subtrees of a match to be skipped.
  2940. This variable is semi-obsolete and probably should always be true. It
  2941. is better to limit inheritance to certain tags using the variables
  2942. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2943. :group 'org-tags
  2944. :type '(choice
  2945. (const :tag "No, don't list them" nil)
  2946. (const :tag "Yes, do list them" t)
  2947. (const :tag "List them, indented with leading dots" indented)))
  2948. (defcustom org-tags-sort-function nil
  2949. "When set, tags are sorted using this function as a comparator."
  2950. :group 'org-tags
  2951. :type '(choice
  2952. (const :tag "No sorting" nil)
  2953. (const :tag "Alphabetical" string<)
  2954. (const :tag "Reverse alphabetical" string>)
  2955. (function :tag "Custom function" nil)))
  2956. (defvar org-tags-history nil
  2957. "History of minibuffer reads for tags.")
  2958. (defvar org-last-tags-completion-table nil
  2959. "The last used completion table for tags.")
  2960. (defvar org-after-tags-change-hook nil
  2961. "Hook that is run after the tags in a line have changed.")
  2962. (defgroup org-properties nil
  2963. "Options concerning properties in Org-mode."
  2964. :tag "Org Properties"
  2965. :group 'org)
  2966. (defcustom org-property-format "%-10s %s"
  2967. "How property key/value pairs should be formatted by `indent-line'.
  2968. When `indent-line' hits a property definition, it will format the line
  2969. according to this format, mainly to make sure that the values are
  2970. lined-up with respect to each other."
  2971. :group 'org-properties
  2972. :type 'string)
  2973. (defcustom org-properties-postprocess-alist nil
  2974. "Alist of properties and functions to adjust inserted values.
  2975. Elements of this alist must be of the form
  2976. ([string] [function])
  2977. where [string] must be a property name and [function] must be a
  2978. lambda expression: this lambda expression must take one argument,
  2979. the value to adjust, and return the new value as a string.
  2980. For example, this element will allow the property \"Remaining\"
  2981. to be updated wrt the relation between the \"Effort\" property
  2982. and the clock summary:
  2983. ((\"Remaining\" (lambda(value)
  2984. (let ((clocksum (org-clock-sum-current-item))
  2985. (effort (org-duration-string-to-minutes
  2986. (org-entry-get (point) \"Effort\"))))
  2987. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2988. :group 'org-properties
  2989. :version "24.1"
  2990. :type '(alist :key-type (string :tag "Property")
  2991. :value-type (function :tag "Function")))
  2992. (defcustom org-use-property-inheritance nil
  2993. "Non-nil means properties apply also for sublevels.
  2994. This setting is chiefly used during property searches. Turning it on can
  2995. cause significant overhead when doing a search, which is why it is not
  2996. on by default.
  2997. When nil, only the properties directly given in the current entry count.
  2998. When t, every property is inherited. The value may also be a list of
  2999. properties that should have inheritance, or a regular expression matching
  3000. properties that should be inherited.
  3001. However, note that some special properties use inheritance under special
  3002. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3003. and the properties ending in \"_ALL\" when they are used as descriptor
  3004. for valid values of a property.
  3005. Note for programmers:
  3006. When querying an entry with `org-entry-get', you can control if inheritance
  3007. should be used. By default, `org-entry-get' looks only at the local
  3008. properties. You can request inheritance by setting the inherit argument
  3009. to t (to force inheritance) or to `selective' (to respect the setting
  3010. in this variable)."
  3011. :group 'org-properties
  3012. :type '(choice
  3013. (const :tag "Not" nil)
  3014. (const :tag "Always" t)
  3015. (repeat :tag "Specific properties" (string :tag "Property"))
  3016. (regexp :tag "Properties matched by regexp")))
  3017. (defun org-property-inherit-p (property)
  3018. "Check if PROPERTY is one that should be inherited."
  3019. (cond
  3020. ((eq org-use-property-inheritance t) t)
  3021. ((not org-use-property-inheritance) nil)
  3022. ((stringp org-use-property-inheritance)
  3023. (string-match org-use-property-inheritance property))
  3024. ((listp org-use-property-inheritance)
  3025. (member property org-use-property-inheritance))
  3026. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3027. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3028. "The default column format, if no other format has been defined.
  3029. This variable can be set on the per-file basis by inserting a line
  3030. #+COLUMNS: %25ITEM ....."
  3031. :group 'org-properties
  3032. :type 'string)
  3033. (defcustom org-columns-ellipses ".."
  3034. "The ellipses to be used when a field in column view is truncated.
  3035. When this is the empty string, as many characters as possible are shown,
  3036. but then there will be no visual indication that the field has been truncated.
  3037. When this is a string of length N, the last N characters of a truncated
  3038. field are replaced by this string. If the column is narrower than the
  3039. ellipses string, only part of the ellipses string will be shown."
  3040. :group 'org-properties
  3041. :type 'string)
  3042. (defcustom org-columns-modify-value-for-display-function nil
  3043. "Function that modifies values for display in column view.
  3044. For example, it can be used to cut out a certain part from a time stamp.
  3045. The function must take 2 arguments:
  3046. column-title The title of the column (*not* the property name)
  3047. value The value that should be modified.
  3048. The function should return the value that should be displayed,
  3049. or nil if the normal value should be used."
  3050. :group 'org-properties
  3051. :type 'function)
  3052. (defcustom org-effort-property "Effort"
  3053. "The property that is being used to keep track of effort estimates.
  3054. Effort estimates given in this property need to have the format H:MM."
  3055. :group 'org-properties
  3056. :group 'org-progress
  3057. :type '(string :tag "Property"))
  3058. (defconst org-global-properties-fixed
  3059. '(("VISIBILITY_ALL" . "folded children content all")
  3060. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3061. "List of property/value pairs that can be inherited by any entry.
  3062. These are fixed values, for the preset properties. The user variable
  3063. that can be used to add to this list is `org-global-properties'.
  3064. The entries in this list are cons cells where the car is a property
  3065. name and cdr is a string with the value. If the value represents
  3066. multiple items like an \"_ALL\" property, separate the items by
  3067. spaces.")
  3068. (defcustom org-global-properties nil
  3069. "List of property/value pairs that can be inherited by any entry.
  3070. This list will be combined with the constant `org-global-properties-fixed'.
  3071. The entries in this list are cons cells where the car is a property
  3072. name and cdr is a string with the value.
  3073. You can set buffer-local values for the same purpose in the variable
  3074. `org-file-properties' this by adding lines like
  3075. #+PROPERTY: NAME VALUE"
  3076. :group 'org-properties
  3077. :type '(repeat
  3078. (cons (string :tag "Property")
  3079. (string :tag "Value"))))
  3080. (defvar org-file-properties nil
  3081. "List of property/value pairs that can be inherited by any entry.
  3082. Valid for the current buffer.
  3083. This variable is populated from #+PROPERTY lines.")
  3084. (make-variable-buffer-local 'org-file-properties)
  3085. (defgroup org-agenda nil
  3086. "Options concerning agenda views in Org-mode."
  3087. :tag "Org Agenda"
  3088. :group 'org)
  3089. (defvar org-category nil
  3090. "Variable used by org files to set a category for agenda display.
  3091. Such files should use a file variable to set it, for example
  3092. # -*- mode: org; org-category: \"ELisp\"
  3093. or contain a special line
  3094. #+CATEGORY: ELisp
  3095. If the file does not specify a category, then file's base name
  3096. is used instead.")
  3097. (make-variable-buffer-local 'org-category)
  3098. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3099. (defcustom org-agenda-files nil
  3100. "The files to be used for agenda display.
  3101. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3102. \\[org-remove-file]. You can also use customize to edit the list.
  3103. If an entry is a directory, all files in that directory that are matched by
  3104. `org-agenda-file-regexp' will be part of the file list.
  3105. If the value of the variable is not a list but a single file name, then
  3106. the list of agenda files is actually stored and maintained in that file, one
  3107. agenda file per line. In this file paths can be given relative to
  3108. `org-directory'. Tilde expansion and environment variable substitution
  3109. are also made."
  3110. :group 'org-agenda
  3111. :type '(choice
  3112. (repeat :tag "List of files and directories" file)
  3113. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3114. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3115. "Regular expression to match files for `org-agenda-files'.
  3116. If any element in the list in that variable contains a directory instead
  3117. of a normal file, all files in that directory that are matched by this
  3118. regular expression will be included."
  3119. :group 'org-agenda
  3120. :type 'regexp)
  3121. (defcustom org-agenda-text-search-extra-files nil
  3122. "List of extra files to be searched by text search commands.
  3123. These files will be searched in addition to the agenda files by the
  3124. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3125. Note that these files will only be searched for text search commands,
  3126. not for the other agenda views like todo lists, tag searches or the weekly
  3127. agenda. This variable is intended to list notes and possibly archive files
  3128. that should also be searched by these two commands.
  3129. In fact, if the first element in the list is the symbol `agenda-archives',
  3130. then all archive files of all agenda files will be added to the search
  3131. scope."
  3132. :group 'org-agenda
  3133. :type '(set :greedy t
  3134. (const :tag "Agenda Archives" agenda-archives)
  3135. (repeat :inline t (file))))
  3136. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3137. 'org-agenda-text-search-extra-files)
  3138. (defcustom org-agenda-skip-unavailable-files nil
  3139. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3140. A nil value means to remove them, after a query, from the list."
  3141. :group 'org-agenda
  3142. :type 'boolean)
  3143. (defcustom org-calendar-to-agenda-key [?c]
  3144. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3145. The command `org-calendar-goto-agenda' will be bound to this key. The
  3146. default is the character `c' because then `c' can be used to switch back and
  3147. forth between agenda and calendar."
  3148. :group 'org-agenda
  3149. :type 'sexp)
  3150. (defcustom org-calendar-insert-diary-entry-key [?i]
  3151. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3152. This option is irrelevant until `org-agenda-diary-file' has been configured
  3153. to point to an Org-mode file. When that is the case, the command
  3154. `org-agenda-diary-entry' will be bound to the key given here, by default
  3155. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3156. if you want to continue doing this, you need to change this to a different
  3157. key."
  3158. :group 'org-agenda
  3159. :type 'sexp)
  3160. (defcustom org-agenda-diary-file 'diary-file
  3161. "File to which to add new entries with the `i' key in agenda and calendar.
  3162. When this is the symbol `diary-file', the functionality in the Emacs
  3163. calendar will be used to add entries to the `diary-file'. But when this
  3164. points to a file, `org-agenda-diary-entry' will be used instead."
  3165. :group 'org-agenda
  3166. :type '(choice
  3167. (const :tag "The standard Emacs diary file" diary-file)
  3168. (file :tag "Special Org file diary entries")))
  3169. (eval-after-load "calendar"
  3170. '(progn
  3171. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3172. 'org-calendar-goto-agenda)
  3173. (add-hook 'calendar-mode-hook
  3174. (lambda ()
  3175. (unless (eq org-agenda-diary-file 'diary-file)
  3176. (define-key calendar-mode-map
  3177. org-calendar-insert-diary-entry-key
  3178. 'org-agenda-diary-entry))))))
  3179. (defgroup org-latex nil
  3180. "Options for embedding LaTeX code into Org-mode."
  3181. :tag "Org LaTeX"
  3182. :group 'org)
  3183. (defcustom org-format-latex-options
  3184. '(:foreground default :background default :scale 1.0
  3185. :html-foreground "Black" :html-background "Transparent"
  3186. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3187. "Options for creating images from LaTeX fragments.
  3188. This is a property list with the following properties:
  3189. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3190. `default' means use the foreground of the default face.
  3191. `auto' means use the foreground from the text face.
  3192. :background the background color, or \"Transparent\".
  3193. `default' means use the background of the default face.
  3194. `auto' means use the background from the text face.
  3195. :scale a scaling factor for the size of the images, to get more pixels
  3196. :html-foreground, :html-background, :html-scale
  3197. the same numbers for HTML export.
  3198. :matchers a list indicating which matchers should be used to
  3199. find LaTeX fragments. Valid members of this list are:
  3200. \"begin\" find environments
  3201. \"$1\" find single characters surrounded by $.$
  3202. \"$\" find math expressions surrounded by $...$
  3203. \"$$\" find math expressions surrounded by $$....$$
  3204. \"\\(\" find math expressions surrounded by \\(...\\)
  3205. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3206. :group 'org-latex
  3207. :type 'plist)
  3208. (defcustom org-format-latex-signal-error t
  3209. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3210. When nil, just push out a message."
  3211. :group 'org-latex
  3212. :version "24.1"
  3213. :type 'boolean)
  3214. (defcustom org-latex-to-mathml-jar-file nil
  3215. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3216. Use this to specify additional executable file say a jar file.
  3217. When using MathToWeb as the converter, specify the full-path to
  3218. your mathtoweb.jar file."
  3219. :group 'org-latex
  3220. :version "24.1"
  3221. :type '(choice
  3222. (const :tag "None" nil)
  3223. (file :tag "JAR file" :must-match t)))
  3224. (defcustom org-latex-to-mathml-convert-command nil
  3225. "Command to convert LaTeX fragments to MathML.
  3226. Replace format-specifiers in the command as noted below and use
  3227. `shell-command' to convert LaTeX to MathML.
  3228. %j: Executable file in fully expanded form as specified by
  3229. `org-latex-to-mathml-jar-file'.
  3230. %I: Input LaTeX file in fully expanded form
  3231. %o: Output MathML file
  3232. This command is used by `org-create-math-formula'.
  3233. When using MathToWeb as the converter, set this to
  3234. \"java -jar %j -unicode -force -df %o %I\"."
  3235. :group 'org-latex
  3236. :version "24.1"
  3237. :type '(choice
  3238. (const :tag "None" nil)
  3239. (string :tag "\nShell command")))
  3240. (defcustom org-latex-create-formula-image-program 'dvipng
  3241. "Program to convert LaTeX fragments with.
  3242. dvipng Process the LaTeX fragments to dvi file, then convert
  3243. dvi files to png files using dvipng.
  3244. This will also include processing of non-math environments.
  3245. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3246. to convert pdf files to png files"
  3247. :group 'org-latex
  3248. :version "24.1"
  3249. :type '(choice
  3250. (const :tag "dvipng" dvipng)
  3251. (const :tag "imagemagick" imagemagick)))
  3252. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3253. "Path to store latex preview images.
  3254. A relative path here creates many directories relative to the
  3255. processed org files paths. An absolute path puts all preview
  3256. images at the same place."
  3257. :group 'org-latex
  3258. :version "24.3"
  3259. :type 'string)
  3260. (defun org-format-latex-mathml-available-p ()
  3261. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3262. (save-match-data
  3263. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3264. org-latex-to-mathml-convert-command)
  3265. (let ((executable (car (split-string
  3266. org-latex-to-mathml-convert-command))))
  3267. (when (executable-find executable)
  3268. (if (string-match
  3269. "%j" org-latex-to-mathml-convert-command)
  3270. (file-readable-p org-latex-to-mathml-jar-file)
  3271. t))))))
  3272. (defcustom org-format-latex-header "\\documentclass{article}
  3273. \\usepackage[usenames]{color}
  3274. \\usepackage{amsmath}
  3275. \\usepackage[mathscr]{eucal}
  3276. \\pagestyle{empty} % do not remove
  3277. \[PACKAGES]
  3278. \[DEFAULT-PACKAGES]
  3279. % The settings below are copied from fullpage.sty
  3280. \\setlength{\\textwidth}{\\paperwidth}
  3281. \\addtolength{\\textwidth}{-3cm}
  3282. \\setlength{\\oddsidemargin}{1.5cm}
  3283. \\addtolength{\\oddsidemargin}{-2.54cm}
  3284. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3285. \\setlength{\\textheight}{\\paperheight}
  3286. \\addtolength{\\textheight}{-\\headheight}
  3287. \\addtolength{\\textheight}{-\\headsep}
  3288. \\addtolength{\\textheight}{-\\footskip}
  3289. \\addtolength{\\textheight}{-3cm}
  3290. \\setlength{\\topmargin}{1.5cm}
  3291. \\addtolength{\\topmargin}{-2.54cm}"
  3292. "The document header used for processing LaTeX fragments.
  3293. It is imperative that this header make sure that no page number
  3294. appears on the page. The package defined in the variables
  3295. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3296. will either replace the placeholder \"[PACKAGES]\" in this
  3297. header, or they will be appended."
  3298. :group 'org-latex
  3299. :type 'string)
  3300. (defun org-set-packages-alist (var val)
  3301. "Set the packages alist and make sure it has 3 elements per entry."
  3302. (set var (mapcar (lambda (x)
  3303. (if (and (consp x) (= (length x) 2))
  3304. (list (car x) (nth 1 x) t)
  3305. x))
  3306. val)))
  3307. (defun org-get-packages-alist (var)
  3308. "Get the packages alist and make sure it has 3 elements per entry."
  3309. (mapcar (lambda (x)
  3310. (if (and (consp x) (= (length x) 2))
  3311. (list (car x) (nth 1 x) t)
  3312. x))
  3313. (default-value var)))
  3314. (defcustom org-latex-default-packages-alist
  3315. '(("AUTO" "inputenc" t)
  3316. ("T1" "fontenc" t)
  3317. ("" "fixltx2e" nil)
  3318. ("" "graphicx" t)
  3319. ("" "longtable" nil)
  3320. ("" "float" nil)
  3321. ("" "wrapfig" nil)
  3322. ("normalem" "ulem" t)
  3323. ("" "textcomp" t)
  3324. ("" "marvosym" t)
  3325. ("" "wasysym" t)
  3326. ("" "latexsym" t)
  3327. ("" "amssymb" t)
  3328. ("" "amstext" nil)
  3329. ("" "hyperref" nil)
  3330. "\\tolerance=1000")
  3331. "Alist of default packages to be inserted in the header.
  3332. Change this only if one of the packages here causes an
  3333. incompatibility with another package you are using.
  3334. The packages in this list are needed by one part or another of
  3335. Org mode to function properly:
  3336. - inputenc, fontenc: for basic font and character selection
  3337. - amstext: for subscript and superscript
  3338. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3339. symbols used for interpreting the entities in `org-entities'.
  3340. You can skip some of these packages if you don't use any of the
  3341. symbols in it.
  3342. - ulem: for underline and strike-through
  3343. - graphicx: for including images
  3344. - float, wrapfig: for figure placement
  3345. - longtable: for long tables
  3346. - hyperref: for cross references
  3347. Therefore you should not modify this variable unless you know
  3348. what you are doing. The one reason to change it anyway is that
  3349. you might be loading some other package that conflicts with one
  3350. of the default packages. Each cell is of the format
  3351. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3352. the package also needs to be included when compiling LaTeX
  3353. snippets into images for inclusion into non-LaTeX output."
  3354. :group 'org-latex
  3355. :group 'org-export-latex
  3356. :set 'org-set-packages-alist
  3357. :get 'org-get-packages-alist
  3358. :version "24.1"
  3359. :type '(repeat
  3360. (choice
  3361. (list :tag "options/package pair"
  3362. (string :tag "options")
  3363. (string :tag "package")
  3364. (boolean :tag "Snippet"))
  3365. (string :tag "A line of LaTeX"))))
  3366. (defcustom org-latex-packages-alist nil
  3367. "Alist of packages to be inserted in every LaTeX header.
  3368. These will be inserted after `org-latex-default-packages-alist'.
  3369. Each cell is of the format:
  3370. \(\"options\" \"package\" snippet-flag)
  3371. SNIPPET-FLAG, when t, indicates that this package is also needed
  3372. when turning LaTeX snippets into images for inclusion into
  3373. non-LaTeX output.
  3374. Make sure that you only list packages here which:
  3375. - you want in every file
  3376. - do not conflict with the setup in `org-format-latex-header'.
  3377. - do not conflict with the default packages in
  3378. `org-latex-default-packages-alist'."
  3379. :group 'org-latex
  3380. :group 'org-export-latex
  3381. :set 'org-set-packages-alist
  3382. :get 'org-get-packages-alist
  3383. :type '(repeat
  3384. (choice
  3385. (list :tag "options/package pair"
  3386. (string :tag "options")
  3387. (string :tag "package")
  3388. (boolean :tag "Snippet"))
  3389. (string :tag "A line of LaTeX"))))
  3390. (defgroup org-appearance nil
  3391. "Settings for Org-mode appearance."
  3392. :tag "Org Appearance"
  3393. :group 'org)
  3394. (defcustom org-level-color-stars-only nil
  3395. "Non-nil means fontify only the stars in each headline.
  3396. When nil, the entire headline is fontified.
  3397. Changing it requires restart of `font-lock-mode' to become effective
  3398. also in regions already fontified."
  3399. :group 'org-appearance
  3400. :type 'boolean)
  3401. (defcustom org-hide-leading-stars nil
  3402. "Non-nil means hide the first N-1 stars in a headline.
  3403. This works by using the face `org-hide' for these stars. This
  3404. face is white for a light background, and black for a dark
  3405. background. You may have to customize the face `org-hide' to
  3406. make this work.
  3407. Changing it requires restart of `font-lock-mode' to become effective
  3408. also in regions already fontified.
  3409. You may also set this on a per-file basis by adding one of the following
  3410. lines to the buffer:
  3411. #+STARTUP: hidestars
  3412. #+STARTUP: showstars"
  3413. :group 'org-appearance
  3414. :type 'boolean)
  3415. (defcustom org-hidden-keywords nil
  3416. "List of symbols corresponding to keywords to be hidden the org buffer.
  3417. For example, a value '(title) for this list will make the document's title
  3418. appear in the buffer without the initial #+TITLE: keyword."
  3419. :group 'org-appearance
  3420. :version "24.1"
  3421. :type '(set (const :tag "#+AUTHOR" author)
  3422. (const :tag "#+DATE" date)
  3423. (const :tag "#+EMAIL" email)
  3424. (const :tag "#+TITLE" title)))
  3425. (defcustom org-custom-properties nil
  3426. "List of properties (as strings) with a special meaning.
  3427. The default use of these custom properties is to let the user
  3428. hide them with `org-toggle-custom-properties-visibility'."
  3429. :group 'org-properties
  3430. :group 'org-appearance
  3431. :version "24.3"
  3432. :type '(repeat (string :tag "Property Name")))
  3433. (defcustom org-fontify-done-headline nil
  3434. "Non-nil means change the face of a headline if it is marked DONE.
  3435. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3436. When this is non-nil, the headline after the keyword is set to the
  3437. `org-headline-done' as an additional indication."
  3438. :group 'org-appearance
  3439. :type 'boolean)
  3440. (defcustom org-fontify-emphasized-text t
  3441. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3442. Changing this variable requires a restart of Emacs to take effect."
  3443. :group 'org-appearance
  3444. :type 'boolean)
  3445. (defcustom org-fontify-whole-heading-line nil
  3446. "Non-nil means fontify the whole line for headings.
  3447. This is useful when setting a background color for the
  3448. org-level-* faces."
  3449. :group 'org-appearance
  3450. :type 'boolean)
  3451. (defcustom org-highlight-latex-and-related nil
  3452. "Non-nil means highlight LaTeX related syntax in the buffer.
  3453. When non nil, the value should be a list containing any of the
  3454. following symbols:
  3455. `latex' Highlight LaTeX snippets and environments.
  3456. `script' Highlight subscript and superscript.
  3457. `entities' Highlight entities."
  3458. :group 'org-appearance
  3459. :version "24.4"
  3460. :package-version '(Org . "8.0")
  3461. :type '(choice
  3462. (const :tag "No highlighting" nil)
  3463. (set :greedy t :tag "Highlight"
  3464. (const :tag "LaTeX snippets and environments" latex)
  3465. (const :tag "Subscript and superscript" script)
  3466. (const :tag "Entities" entities))))
  3467. (defcustom org-hide-emphasis-markers nil
  3468. "Non-nil mean font-lock should hide the emphasis marker characters."
  3469. :group 'org-appearance
  3470. :type 'boolean)
  3471. (defcustom org-pretty-entities nil
  3472. "Non-nil means show entities as UTF8 characters.
  3473. When nil, the \\name form remains in the buffer."
  3474. :group 'org-appearance
  3475. :version "24.1"
  3476. :type 'boolean)
  3477. (defcustom org-pretty-entities-include-sub-superscripts t
  3478. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3479. :group 'org-appearance
  3480. :version "24.1"
  3481. :type 'boolean)
  3482. (defvar org-emph-re nil
  3483. "Regular expression for matching emphasis.
  3484. After a match, the match groups contain these elements:
  3485. 0 The match of the full regular expression, including the characters
  3486. before and after the proper match
  3487. 1 The character before the proper match, or empty at beginning of line
  3488. 2 The proper match, including the leading and trailing markers
  3489. 3 The leading marker like * or /, indicating the type of highlighting
  3490. 4 The text between the emphasis markers, not including the markers
  3491. 5 The character after the match, empty at the end of a line")
  3492. (defvar org-verbatim-re nil
  3493. "Regular expression for matching verbatim text.")
  3494. (defvar org-emphasis-regexp-components) ; defined just below
  3495. (defvar org-emphasis-alist) ; defined just below
  3496. (defun org-set-emph-re (var val)
  3497. "Set variable and compute the emphasis regular expression."
  3498. (set var val)
  3499. (when (and (boundp 'org-emphasis-alist)
  3500. (boundp 'org-emphasis-regexp-components)
  3501. org-emphasis-alist org-emphasis-regexp-components)
  3502. (let* ((e org-emphasis-regexp-components)
  3503. (pre (car e))
  3504. (post (nth 1 e))
  3505. (border (nth 2 e))
  3506. (body (nth 3 e))
  3507. (nl (nth 4 e))
  3508. (body1 (concat body "*?"))
  3509. (markers (mapconcat 'car org-emphasis-alist ""))
  3510. (vmarkers (mapconcat
  3511. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3512. org-emphasis-alist "")))
  3513. ;; make sure special characters appear at the right position in the class
  3514. (if (string-match "\\^" markers)
  3515. (setq markers (concat (replace-match "" t t markers) "^")))
  3516. (if (string-match "-" markers)
  3517. (setq markers (concat (replace-match "" t t markers) "-")))
  3518. (if (string-match "\\^" vmarkers)
  3519. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3520. (if (string-match "-" vmarkers)
  3521. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3522. (if (> nl 0)
  3523. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3524. (int-to-string nl) "\\}")))
  3525. ;; Make the regexp
  3526. (setq org-emph-re
  3527. (concat "\\([" pre "]\\|^\\)"
  3528. "\\("
  3529. "\\([" markers "]\\)"
  3530. "\\("
  3531. "[^" border "]\\|"
  3532. "[^" border "]"
  3533. body1
  3534. "[^" border "]"
  3535. "\\)"
  3536. "\\3\\)"
  3537. "\\([" post "]\\|$\\)"))
  3538. (setq org-verbatim-re
  3539. (concat "\\([" pre "]\\|^\\)"
  3540. "\\("
  3541. "\\([" vmarkers "]\\)"
  3542. "\\("
  3543. "[^" border "]\\|"
  3544. "[^" border "]"
  3545. body1
  3546. "[^" border "]"
  3547. "\\)"
  3548. "\\3\\)"
  3549. "\\([" post "]\\|$\\)")))))
  3550. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3551. ;; set this option proved cumbersome. See this message/thread:
  3552. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3553. (defvar org-emphasis-regexp-components
  3554. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3555. "Components used to build the regular expression for emphasis.
  3556. This is a list with five entries. Terminology: In an emphasis string
  3557. like \" *strong word* \", we call the initial space PREMATCH, the final
  3558. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3559. and \"trong wor\" is the body. The different components in this variable
  3560. specify what is allowed/forbidden in each part:
  3561. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3562. post Chars allowed as postmatch. End of line will be allowed too.
  3563. border The chars *forbidden* as border characters.
  3564. body-regexp A regexp like \".\" to match a body character. Don't use
  3565. non-shy groups here, and don't allow newline here.
  3566. newline The maximum number of newlines allowed in an emphasis exp.
  3567. You need to reload Org or to restart Emacs after customizing this.")
  3568. (defcustom org-emphasis-alist
  3569. `(("*" bold)
  3570. ("/" italic)
  3571. ("_" underline)
  3572. ("=" org-code verbatim)
  3573. ("~" org-verbatim verbatim)
  3574. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3575. "Alist of characters and faces to emphasize text.
  3576. Text starting and ending with a special character will be emphasized,
  3577. for example *bold*, _underlined_ and /italic/. This variable sets the
  3578. marker characters and the face to be used by font-lock for highlighting
  3579. in Org-mode Emacs buffers.
  3580. You need to reload Org or to restart Emacs after customizing this."
  3581. :group 'org-appearance
  3582. :set 'org-set-emph-re
  3583. :version "24.4"
  3584. :package-version '(Org . "8.0")
  3585. :type '(repeat
  3586. (list
  3587. (string :tag "Marker character")
  3588. (choice
  3589. (face :tag "Font-lock-face")
  3590. (plist :tag "Face property list"))
  3591. (option (const verbatim)))))
  3592. (defvar org-protecting-blocks
  3593. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3594. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3595. This is needed for font-lock setup.")
  3596. ;;; Miscellaneous options
  3597. (defgroup org-completion nil
  3598. "Completion in Org-mode."
  3599. :tag "Org Completion"
  3600. :group 'org)
  3601. (defcustom org-completion-use-ido nil
  3602. "Non-nil means use ido completion wherever possible.
  3603. Note that `ido-mode' must be active for this variable to be relevant.
  3604. If you decide to turn this variable on, you might well want to turn off
  3605. `org-outline-path-complete-in-steps'.
  3606. See also `org-completion-use-iswitchb'."
  3607. :group 'org-completion
  3608. :type 'boolean)
  3609. (defcustom org-completion-use-iswitchb nil
  3610. "Non-nil means use iswitchb completion wherever possible.
  3611. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3612. If you decide to turn this variable on, you might well want to turn off
  3613. `org-outline-path-complete-in-steps'.
  3614. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3615. :group 'org-completion
  3616. :type 'boolean)
  3617. (defcustom org-completion-fallback-command 'hippie-expand
  3618. "The expansion command called by \\[pcomplete] in normal context.
  3619. Normal means, no org-mode-specific context."
  3620. :group 'org-completion
  3621. :type 'function)
  3622. ;;; Functions and variables from their packages
  3623. ;; Declared here to avoid compiler warnings
  3624. ;; XEmacs only
  3625. (defvar outline-mode-menu-heading)
  3626. (defvar outline-mode-menu-show)
  3627. (defvar outline-mode-menu-hide)
  3628. (defvar zmacs-regions) ; XEmacs regions
  3629. ;; Emacs only
  3630. (defvar mark-active)
  3631. ;; Various packages
  3632. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3633. (declare-function calendar-forward-day "cal-move" (arg))
  3634. (declare-function calendar-goto-date "cal-move" (date))
  3635. (declare-function calendar-goto-today "cal-move" ())
  3636. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3637. (defvar calc-embedded-close-formula)
  3638. (defvar calc-embedded-open-formula)
  3639. (declare-function cdlatex-tab "ext:cdlatex" ())
  3640. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3641. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3642. (defvar font-lock-unfontify-region-function)
  3643. (declare-function iswitchb-read-buffer "iswitchb"
  3644. (prompt &optional default require-match start matches-set))
  3645. (defvar iswitchb-temp-buflist)
  3646. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3647. (defvar org-agenda-tags-todo-honor-ignore-options)
  3648. (declare-function org-agenda-skip "org-agenda" ())
  3649. (declare-function
  3650. org-agenda-format-item "org-agenda"
  3651. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3652. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3653. (declare-function org-agenda-change-all-lines "org-agenda"
  3654. (newhead hdmarker &optional fixface just-this))
  3655. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3656. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3657. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3658. (beg end))
  3659. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3660. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3661. "org-agenda" (&optional end))
  3662. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3663. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3664. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3665. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3666. (declare-function org-indent-mode "org-indent" (&optional arg))
  3667. (declare-function parse-time-string "parse-time" (string))
  3668. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3669. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3670. (defvar remember-data-file)
  3671. (defvar texmathp-why)
  3672. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3673. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3674. (defvar org-latex-regexps)
  3675. ;;; Autoload and prepare some org modules
  3676. ;; Some table stuff that needs to be defined here, because it is used
  3677. ;; by the functions setting up org-mode or checking for table context.
  3678. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3679. "Detect an org-type or table-type table.")
  3680. (defconst org-table-line-regexp "^[ \t]*|"
  3681. "Detect an org-type table line.")
  3682. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3683. "Detect an org-type table line.")
  3684. (defconst org-table-hline-regexp "^[ \t]*|-"
  3685. "Detect an org-type table hline.")
  3686. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3687. "Detect a table-type table hline.")
  3688. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3689. "Detect the first line outside a table when searching from within it.
  3690. This works for both table types.")
  3691. ;; Autoload the functions in org-table.el that are needed by functions here.
  3692. (eval-and-compile
  3693. (org-autoload "org-table"
  3694. '(org-table-begin org-table-blank-field org-table-end)))
  3695. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3696. "Detect a #+TBLFM line.")
  3697. ;;;###autoload
  3698. (defun turn-on-orgtbl ()
  3699. "Unconditionally turn on `orgtbl-mode'."
  3700. (require 'org-table)
  3701. (orgtbl-mode 1))
  3702. (defun org-at-table-p (&optional table-type)
  3703. "Return t if the cursor is inside an org-type table.
  3704. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3705. (if org-enable-table-editor
  3706. (save-excursion
  3707. (beginning-of-line 1)
  3708. (looking-at (if table-type org-table-any-line-regexp
  3709. org-table-line-regexp)))
  3710. nil))
  3711. (defsubst org-table-p () (org-at-table-p))
  3712. (defun org-at-table.el-p ()
  3713. "Return t if and only if we are at a table.el table."
  3714. (and (org-at-table-p 'any)
  3715. (save-excursion
  3716. (goto-char (org-table-begin 'any))
  3717. (looking-at org-table1-hline-regexp))))
  3718. (defun org-table-recognize-table.el ()
  3719. "If there is a table.el table nearby, recognize it and move into it."
  3720. (if org-table-tab-recognizes-table.el
  3721. (if (org-at-table.el-p)
  3722. (progn
  3723. (beginning-of-line 1)
  3724. (if (looking-at org-table-dataline-regexp)
  3725. nil
  3726. (if (looking-at org-table1-hline-regexp)
  3727. (progn
  3728. (beginning-of-line 2)
  3729. (if (looking-at org-table-any-border-regexp)
  3730. (beginning-of-line -1)))))
  3731. (if (re-search-forward "|" (org-table-end t) t)
  3732. (progn
  3733. (require 'table)
  3734. (if (table--at-cell-p (point))
  3735. t
  3736. (message "recognizing table.el table...")
  3737. (table-recognize-table)
  3738. (message "recognizing table.el table...done")))
  3739. (error "This should not happen"))
  3740. t)
  3741. nil)
  3742. nil))
  3743. (defun org-at-table-hline-p ()
  3744. "Return t if the cursor is inside a hline in a table."
  3745. (if org-enable-table-editor
  3746. (save-excursion
  3747. (beginning-of-line 1)
  3748. (looking-at org-table-hline-regexp))
  3749. nil))
  3750. (defvar org-table-clean-did-remove-column nil)
  3751. (defun org-table-map-tables (function &optional quietly)
  3752. "Apply FUNCTION to the start of all tables in the buffer."
  3753. (save-excursion
  3754. (save-restriction
  3755. (widen)
  3756. (goto-char (point-min))
  3757. (while (re-search-forward org-table-any-line-regexp nil t)
  3758. (unless quietly
  3759. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3760. (beginning-of-line 1)
  3761. (when (and (looking-at org-table-line-regexp)
  3762. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3763. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3764. (save-excursion (funcall function))
  3765. (or (looking-at org-table-line-regexp)
  3766. (forward-char 1)))
  3767. (re-search-forward org-table-any-border-regexp nil 1))))
  3768. (unless quietly (message "Mapping tables: done")))
  3769. ;; Declare and autoload functions from ox.el and al.
  3770. (declare-function org-export-get-environment "ox"
  3771. (&optional backend subtreep ext-plist))
  3772. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3773. ;; Declare and autoload functions from org-agenda.el
  3774. (eval-and-compile
  3775. (org-autoload "org-agenda"
  3776. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3777. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3778. (declare-function org-clock-update-mode-line "org-clock" ())
  3779. (declare-function org-resolve-clocks "org-clock"
  3780. (&optional also-non-dangling-p prompt last-valid))
  3781. (defun org-at-TBLFM-p (&optional pos)
  3782. "Return t when point (or POS) is in #+TBLFM line."
  3783. (save-excursion
  3784. (let ((pos pos)))
  3785. (goto-char (or pos (point)))
  3786. (beginning-of-line 1)
  3787. (looking-at org-TBLFM-regexp)))
  3788. (defvar org-clock-start-time)
  3789. (defvar org-clock-marker (make-marker)
  3790. "Marker recording the last clock-in.")
  3791. (defvar org-clock-hd-marker (make-marker)
  3792. "Marker recording the last clock-in, but the headline position.")
  3793. (defvar org-clock-heading ""
  3794. "The heading of the current clock entry.")
  3795. (defun org-clock-is-active ()
  3796. "Return non-nil if clock is currently running.
  3797. The return value is actually the clock marker."
  3798. (marker-buffer org-clock-marker))
  3799. (eval-and-compile
  3800. (org-autoload "org-clock" '(org-clock-remove-overlays
  3801. org-clock-update-time-maybe
  3802. org-clocktable-shift)))
  3803. (defun org-check-running-clock ()
  3804. "Check if the current buffer contains the running clock.
  3805. If yes, offer to stop it and to save the buffer with the changes."
  3806. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3807. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3808. (buffer-name))))
  3809. (org-clock-out)
  3810. (when (y-or-n-p "Save changed buffer?")
  3811. (save-buffer))))
  3812. (defun org-clocktable-try-shift (dir n)
  3813. "Check if this line starts a clock table, if yes, shift the time block."
  3814. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3815. (org-clocktable-shift dir n)))
  3816. ;;;###autoload
  3817. (defun org-clock-persistence-insinuate ()
  3818. "Set up hooks for clock persistence."
  3819. (require 'org-clock)
  3820. (add-hook 'org-mode-hook 'org-clock-load)
  3821. (add-hook 'kill-emacs-hook 'org-clock-save))
  3822. ;; Define the variable already here, to make sure we have it.
  3823. (defvar org-indent-mode nil
  3824. "Non-nil if Org-Indent mode is enabled.
  3825. Use the command `org-indent-mode' to change this variable.")
  3826. ;; Autoload archiving code
  3827. ;; The stuff that is needed for cycling and tags has to be defined here.
  3828. (defgroup org-archive nil
  3829. "Options concerning archiving in Org-mode."
  3830. :tag "Org Archive"
  3831. :group 'org-structure)
  3832. (defcustom org-archive-location "%s_archive::"
  3833. "The location where subtrees should be archived.
  3834. The value of this variable is a string, consisting of two parts,
  3835. separated by a double-colon. The first part is a filename and
  3836. the second part is a headline.
  3837. When the filename is omitted, archiving happens in the same file.
  3838. %s in the filename will be replaced by the current file
  3839. name (without the directory part). Archiving to a different file
  3840. is useful to keep archived entries from contributing to the
  3841. Org-mode Agenda.
  3842. The archived entries will be filed as subtrees of the specified
  3843. headline. When the headline is omitted, the subtrees are simply
  3844. filed away at the end of the file, as top-level entries. Also in
  3845. the heading you can use %s to represent the file name, this can be
  3846. useful when using the same archive for a number of different files.
  3847. Here are a few examples:
  3848. \"%s_archive::\"
  3849. If the current file is Projects.org, archive in file
  3850. Projects.org_archive, as top-level trees. This is the default.
  3851. \"::* Archived Tasks\"
  3852. Archive in the current file, under the top-level headline
  3853. \"* Archived Tasks\".
  3854. \"~/org/archive.org::\"
  3855. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3856. \"~/org/archive.org::* From %s\"
  3857. Archive in file ~/org/archive.org (absolute path), under headlines
  3858. \"From FILENAME\" where file name is the current file name.
  3859. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3860. The \"datetree/\" string is special, signifying to archive
  3861. items to the datetree. Items are placed in either the CLOSED
  3862. date of the item, or the current date if there is no CLOSED date.
  3863. The heading will be a subentry to the current date. There doesn't
  3864. need to be a heading, but there always needs to be a slash after
  3865. datetree. For example, to store archived items directly in the
  3866. datetree, use \"~/org/datetree.org::datetree/\".
  3867. \"basement::** Finished Tasks\"
  3868. Archive in file ./basement (relative path), as level 3 trees
  3869. below the level 2 heading \"** Finished Tasks\".
  3870. You may set this option on a per-file basis by adding to the buffer a
  3871. line like
  3872. #+ARCHIVE: basement::** Finished Tasks
  3873. You may also define it locally for a subtree by setting an ARCHIVE property
  3874. in the entry. If such a property is found in an entry, or anywhere up
  3875. the hierarchy, it will be used."
  3876. :group 'org-archive
  3877. :type 'string)
  3878. (defcustom org-archive-tag "ARCHIVE"
  3879. "The tag that marks a subtree as archived.
  3880. An archived subtree does not open during visibility cycling, and does
  3881. not contribute to the agenda listings.
  3882. After changing this, font-lock must be restarted in the relevant buffers to
  3883. get the proper fontification."
  3884. :group 'org-archive
  3885. :group 'org-keywords
  3886. :type 'string)
  3887. (defcustom org-agenda-skip-archived-trees t
  3888. "Non-nil means the agenda will skip any items located in archived trees.
  3889. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3890. variable is no longer recommended, you should leave it at the value t.
  3891. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3892. :group 'org-archive
  3893. :group 'org-agenda-skip
  3894. :type 'boolean)
  3895. (defcustom org-columns-skip-archived-trees t
  3896. "Non-nil means ignore archived trees when creating column view."
  3897. :group 'org-archive
  3898. :group 'org-properties
  3899. :type 'boolean)
  3900. (defcustom org-cycle-open-archived-trees nil
  3901. "Non-nil means `org-cycle' will open archived trees.
  3902. An archived tree is a tree marked with the tag ARCHIVE.
  3903. When nil, archived trees will stay folded. You can still open them with
  3904. normal outline commands like `show-all', but not with the cycling commands."
  3905. :group 'org-archive
  3906. :group 'org-cycle
  3907. :type 'boolean)
  3908. (defcustom org-sparse-tree-open-archived-trees nil
  3909. "Non-nil means sparse tree construction shows matches in archived trees.
  3910. When nil, matches in these trees are highlighted, but the trees are kept in
  3911. collapsed state."
  3912. :group 'org-archive
  3913. :group 'org-sparse-trees
  3914. :type 'boolean)
  3915. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3916. "The default date type when building a sparse tree.
  3917. When this is nil, a date is a scheduled or a deadline timestamp.
  3918. Otherwise, these types are allowed:
  3919. all: all timestamps
  3920. active: only active timestamps (<...>)
  3921. inactive: only inactive timestamps (<...)
  3922. scheduled: only scheduled timestamps
  3923. deadline: only deadline timestamps"
  3924. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3925. (const :tag "All timestamps" all)
  3926. (const :tag "Only active timestamps" active)
  3927. (const :tag "Only inactive timestamps" inactive)
  3928. (const :tag "Only scheduled timestamps" scheduled)
  3929. (const :tag "Only deadline timestamps" deadline)
  3930. (const :tag "Only closed timestamps" closed))
  3931. :version "24.3"
  3932. :group 'org-sparse-trees)
  3933. (defun org-cycle-hide-archived-subtrees (state)
  3934. "Re-hide all archived subtrees after a visibility state change."
  3935. (when (and (not org-cycle-open-archived-trees)
  3936. (not (memq state '(overview folded))))
  3937. (save-excursion
  3938. (let* ((globalp (memq state '(contents all)))
  3939. (beg (if globalp (point-min) (point)))
  3940. (end (if globalp (point-max) (org-end-of-subtree t))))
  3941. (org-hide-archived-subtrees beg end)
  3942. (goto-char beg)
  3943. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3944. (message "%s" (substitute-command-keys
  3945. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3946. (defun org-force-cycle-archived ()
  3947. "Cycle subtree even if it is archived."
  3948. (interactive)
  3949. (setq this-command 'org-cycle)
  3950. (let ((org-cycle-open-archived-trees t))
  3951. (call-interactively 'org-cycle)))
  3952. (defun org-hide-archived-subtrees (beg end)
  3953. "Re-hide all archived subtrees after a visibility state change."
  3954. (save-excursion
  3955. (let* ((re (concat ":" org-archive-tag ":")))
  3956. (goto-char beg)
  3957. (while (re-search-forward re end t)
  3958. (when (org-at-heading-p)
  3959. (org-flag-subtree t)
  3960. (org-end-of-subtree t))))))
  3961. (declare-function outline-end-of-heading "outline" ())
  3962. (declare-function outline-flag-region "outline" (from to flag))
  3963. (defun org-flag-subtree (flag)
  3964. (save-excursion
  3965. (org-back-to-heading t)
  3966. (outline-end-of-heading)
  3967. (outline-flag-region (point)
  3968. (progn (org-end-of-subtree t) (point))
  3969. flag)))
  3970. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3971. (eval-and-compile
  3972. (org-autoload "org-archive"
  3973. '(org-add-archive-files)))
  3974. ;; Autoload Column View Code
  3975. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3976. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3977. (declare-function org-columns-compute "org-colview" (property))
  3978. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3979. '(org-columns-number-to-string
  3980. org-columns-get-format-and-top-level
  3981. org-columns-compute
  3982. org-columns-remove-overlays))
  3983. ;; Autoload ID code
  3984. (declare-function org-id-store-link "org-id")
  3985. (declare-function org-id-locations-load "org-id")
  3986. (declare-function org-id-locations-save "org-id")
  3987. (defvar org-id-track-globally)
  3988. (org-autoload "org-id"
  3989. '(org-id-new
  3990. org-id-copy
  3991. org-id-get-with-outline-path-completion
  3992. org-id-get-with-outline-drilling))
  3993. ;;; Variables for pre-computed regular expressions, all buffer local
  3994. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3995. "Matches first line of a hidden block.")
  3996. (make-variable-buffer-local 'org-drawer-regexp)
  3997. (defvar org-todo-regexp nil
  3998. "Matches any of the TODO state keywords.")
  3999. (make-variable-buffer-local 'org-todo-regexp)
  4000. (defvar org-not-done-regexp nil
  4001. "Matches any of the TODO state keywords except the last one.")
  4002. (make-variable-buffer-local 'org-not-done-regexp)
  4003. (defvar org-not-done-heading-regexp nil
  4004. "Matches a TODO headline that is not done.")
  4005. (make-variable-buffer-local 'org-not-done-regexp)
  4006. (defvar org-todo-line-regexp nil
  4007. "Matches a headline and puts TODO state into group 2 if present.")
  4008. (make-variable-buffer-local 'org-todo-line-regexp)
  4009. (defvar org-complex-heading-regexp nil
  4010. "Matches a headline and puts everything into groups:
  4011. group 1: the stars
  4012. group 2: The todo keyword, maybe
  4013. group 3: Priority cookie
  4014. group 4: True headline
  4015. group 5: Tags")
  4016. (make-variable-buffer-local 'org-complex-heading-regexp)
  4017. (defvar org-complex-heading-regexp-format nil
  4018. "Printf format to make regexp to match an exact headline.
  4019. This regexp will match the headline of any node which has the
  4020. exact headline text that is put into the format, but may have any
  4021. TODO state, priority and tags.")
  4022. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4023. (defvar org-todo-line-tags-regexp nil
  4024. "Matches a headline and puts TODO state into group 2 if present.
  4025. Also put tags into group 4 if tags are present.")
  4026. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4027. (defvar org-ds-keyword-length 12
  4028. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4029. (make-variable-buffer-local 'org-ds-keyword-length)
  4030. (defvar org-deadline-regexp nil
  4031. "Matches the DEADLINE keyword.")
  4032. (make-variable-buffer-local 'org-deadline-regexp)
  4033. (defvar org-deadline-time-regexp nil
  4034. "Matches the DEADLINE keyword together with a time stamp.")
  4035. (make-variable-buffer-local 'org-deadline-time-regexp)
  4036. (defvar org-deadline-time-hour-regexp nil
  4037. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4038. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4039. (defvar org-deadline-line-regexp nil
  4040. "Matches the DEADLINE keyword and the rest of the line.")
  4041. (make-variable-buffer-local 'org-deadline-line-regexp)
  4042. (defvar org-scheduled-regexp nil
  4043. "Matches the SCHEDULED keyword.")
  4044. (make-variable-buffer-local 'org-scheduled-regexp)
  4045. (defvar org-scheduled-time-regexp nil
  4046. "Matches the SCHEDULED keyword together with a time stamp.")
  4047. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4048. (defvar org-scheduled-time-hour-regexp nil
  4049. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4050. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4051. (defvar org-closed-time-regexp nil
  4052. "Matches the CLOSED keyword together with a time stamp.")
  4053. (make-variable-buffer-local 'org-closed-time-regexp)
  4054. (defvar org-keyword-time-regexp nil
  4055. "Matches any of the 4 keywords, together with the time stamp.")
  4056. (make-variable-buffer-local 'org-keyword-time-regexp)
  4057. (defvar org-keyword-time-not-clock-regexp nil
  4058. "Matches any of the 3 keywords, together with the time stamp.")
  4059. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4060. (defvar org-maybe-keyword-time-regexp nil
  4061. "Matches a timestamp, possibly preceded by a keyword.")
  4062. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4063. (defvar org-all-time-keywords nil
  4064. "List of time keywords.")
  4065. (make-variable-buffer-local 'org-all-time-keywords)
  4066. (defconst org-plain-time-of-day-regexp
  4067. (concat
  4068. "\\(\\<[012]?[0-9]"
  4069. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4070. "\\(--?"
  4071. "\\(\\<[012]?[0-9]"
  4072. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4073. "\\)?")
  4074. "Regular expression to match a plain time or time range.
  4075. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4076. groups carry important information:
  4077. 0 the full match
  4078. 1 the first time, range or not
  4079. 8 the second time, if it is a range.")
  4080. (defconst org-plain-time-extension-regexp
  4081. (concat
  4082. "\\(\\<[012]?[0-9]"
  4083. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4084. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4085. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4086. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4087. groups carry important information:
  4088. 0 the full match
  4089. 7 hours of duration
  4090. 9 minutes of duration")
  4091. (defconst org-stamp-time-of-day-regexp
  4092. (concat
  4093. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4094. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4095. "\\(--?"
  4096. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4097. "Regular expression to match a timestamp time or time range.
  4098. After a match, the following groups carry important information:
  4099. 0 the full match
  4100. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4101. 2 the first time, range or not
  4102. 4 the second time, if it is a range.")
  4103. (defconst org-startup-options
  4104. '(("fold" org-startup-folded t)
  4105. ("overview" org-startup-folded t)
  4106. ("nofold" org-startup-folded nil)
  4107. ("showall" org-startup-folded nil)
  4108. ("showeverything" org-startup-folded showeverything)
  4109. ("content" org-startup-folded content)
  4110. ("indent" org-startup-indented t)
  4111. ("noindent" org-startup-indented nil)
  4112. ("hidestars" org-hide-leading-stars t)
  4113. ("showstars" org-hide-leading-stars nil)
  4114. ("odd" org-odd-levels-only t)
  4115. ("oddeven" org-odd-levels-only nil)
  4116. ("align" org-startup-align-all-tables t)
  4117. ("noalign" org-startup-align-all-tables nil)
  4118. ("inlineimages" org-startup-with-inline-images t)
  4119. ("noinlineimages" org-startup-with-inline-images nil)
  4120. ("latexpreview" org-startup-with-latex-preview t)
  4121. ("nolatexpreview" org-startup-with-latex-preview nil)
  4122. ("customtime" org-display-custom-times t)
  4123. ("logdone" org-log-done time)
  4124. ("lognotedone" org-log-done note)
  4125. ("nologdone" org-log-done nil)
  4126. ("lognoteclock-out" org-log-note-clock-out t)
  4127. ("nolognoteclock-out" org-log-note-clock-out nil)
  4128. ("logrepeat" org-log-repeat state)
  4129. ("lognoterepeat" org-log-repeat note)
  4130. ("logdrawer" org-log-into-drawer t)
  4131. ("nologdrawer" org-log-into-drawer nil)
  4132. ("logstatesreversed" org-log-states-order-reversed t)
  4133. ("nologstatesreversed" org-log-states-order-reversed nil)
  4134. ("nologrepeat" org-log-repeat nil)
  4135. ("logreschedule" org-log-reschedule time)
  4136. ("lognotereschedule" org-log-reschedule note)
  4137. ("nologreschedule" org-log-reschedule nil)
  4138. ("logredeadline" org-log-redeadline time)
  4139. ("lognoteredeadline" org-log-redeadline note)
  4140. ("nologredeadline" org-log-redeadline nil)
  4141. ("logrefile" org-log-refile time)
  4142. ("lognoterefile" org-log-refile note)
  4143. ("nologrefile" org-log-refile nil)
  4144. ("fninline" org-footnote-define-inline t)
  4145. ("nofninline" org-footnote-define-inline nil)
  4146. ("fnlocal" org-footnote-section nil)
  4147. ("fnauto" org-footnote-auto-label t)
  4148. ("fnprompt" org-footnote-auto-label nil)
  4149. ("fnconfirm" org-footnote-auto-label confirm)
  4150. ("fnplain" org-footnote-auto-label plain)
  4151. ("fnadjust" org-footnote-auto-adjust t)
  4152. ("nofnadjust" org-footnote-auto-adjust nil)
  4153. ("constcgs" constants-unit-system cgs)
  4154. ("constSI" constants-unit-system SI)
  4155. ("noptag" org-tag-persistent-alist nil)
  4156. ("hideblocks" org-hide-block-startup t)
  4157. ("nohideblocks" org-hide-block-startup nil)
  4158. ("beamer" org-startup-with-beamer-mode t)
  4159. ("entitiespretty" org-pretty-entities t)
  4160. ("entitiesplain" org-pretty-entities nil))
  4161. "Variable associated with STARTUP options for org-mode.
  4162. Each element is a list of three items: the startup options (as written
  4163. in the #+STARTUP line), the corresponding variable, and the value to set
  4164. this variable to if the option is found. An optional forth element PUSH
  4165. means to push this value onto the list in the variable.")
  4166. (defun org-update-property-plist (key val props)
  4167. "Update PROPS with KEY and VAL."
  4168. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4169. (key (if appending (substring key 0 (- (length key) 1)) key))
  4170. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4171. (previous (cdr (assoc key props))))
  4172. (if appending
  4173. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4174. (cons (cons key val) remainder))))
  4175. (defconst org-block-regexp
  4176. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4177. "Regular expression for hiding blocks.")
  4178. (defconst org-heading-keyword-regexp-format
  4179. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4180. "Printf format for a regexp matching a headline with some keyword.
  4181. This regexp will match the headline of any node which has the
  4182. exact keyword that is put into the format. The keyword isn't in
  4183. any group by default, but the stars and the body are.")
  4184. (defconst org-heading-keyword-maybe-regexp-format
  4185. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4186. "Printf format for a regexp matching a headline, possibly with some keyword.
  4187. This regexp can match any headline with the specified keyword, or
  4188. without a keyword. The keyword isn't in any group by default,
  4189. but the stars and the body are.")
  4190. (defcustom org-group-tags t
  4191. "When non-nil (the default), use group tags.
  4192. This can be turned on/off through `org-toggle-tags-groups'."
  4193. :group 'org-tags
  4194. :group 'org-startup
  4195. :type 'boolean)
  4196. (defun org-toggle-tags-groups ()
  4197. "Toggle support for group tags.
  4198. Support for group tags is controlled by the option
  4199. `org-group-tags', which is non-nil by default."
  4200. (interactive)
  4201. (setq org-group-tags (not org-group-tags))
  4202. (cond ((and (derived-mode-p 'org-agenda-mode)
  4203. org-group-tags)
  4204. (org-agenda-redo))
  4205. ((derived-mode-p 'org-mode)
  4206. (let ((org-inhibit-startup t)) (org-mode))))
  4207. (message "Groups tags support has been turned %s"
  4208. (if org-group-tags "on" "off")))
  4209. (defun org-set-regexps-and-options-for-tags ()
  4210. "Precompute regular expressions used for tags in the current buffer."
  4211. (when (derived-mode-p 'org-mode)
  4212. (org-set-local 'org-file-tags nil)
  4213. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4214. (splitre "[ \t]+")
  4215. (start 0)
  4216. tags ftags key value)
  4217. (save-excursion
  4218. (save-restriction
  4219. (widen)
  4220. (goto-char (point-min))
  4221. (while (re-search-forward re nil t)
  4222. (setq key (upcase (org-match-string-no-properties 1))
  4223. value (org-match-string-no-properties 2))
  4224. (if (stringp value) (setq value (org-trim value)))
  4225. (cond
  4226. ((equal key "TAGS")
  4227. (setq tags (append tags (if tags '("\\n") nil)
  4228. (org-split-string value splitre))))
  4229. ((equal key "FILETAGS")
  4230. (when (string-match "\\S-" value)
  4231. (setq ftags
  4232. (append
  4233. ftags
  4234. (apply 'append
  4235. (mapcar (lambda (x) (org-split-string x ":"))
  4236. (org-split-string value)))))))))))
  4237. ;; Process the file tags.
  4238. (and ftags (org-set-local 'org-file-tags
  4239. (mapcar 'org-add-prop-inherited ftags)))
  4240. (org-set-local 'org-tag-groups-alist nil)
  4241. ;; Process the tags.
  4242. ;; FIXME
  4243. (when tags
  4244. (let (e tgs g)
  4245. (while (setq e (pop tags))
  4246. (cond
  4247. ((equal e "{")
  4248. (progn (push '(:startgroup) tgs)
  4249. (when (equal (nth 1 tags) ":")
  4250. (push (list (replace-regexp-in-string
  4251. "(.+)$" "" (nth 0 tags)))
  4252. org-tag-groups-alist)
  4253. (setq g 0))))
  4254. ((equal e ":") (push '(:grouptags) tgs))
  4255. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4256. ((equal e "\\n") (push '(:newline) tgs))
  4257. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4258. (push (cons (match-string 1 e)
  4259. (string-to-char (match-string 2 e))) tgs)
  4260. (if (and g (> g 0))
  4261. (setcar org-tag-groups-alist
  4262. (append (car org-tag-groups-alist)
  4263. (list (match-string 1 e)))))
  4264. (if g (setq g (1+ g))))
  4265. (t (push (list e) tgs)
  4266. (if (and g (> g 0))
  4267. (setcar org-tag-groups-alist
  4268. (append (car org-tag-groups-alist) (list e))))
  4269. (if g (setq g (1+ g))))))
  4270. (org-set-local 'org-tag-alist nil)
  4271. (while (setq e (pop tgs))
  4272. (or (and (stringp (car e))
  4273. (assoc (car e) org-tag-alist))
  4274. (push e org-tag-alist)))
  4275. ;; Return a list with tag variables
  4276. (list org-file-tags org-tag-alist org-tag-groups-alist))))))
  4277. (defun org-set-regexps-and-options ()
  4278. "Precompute regular expressions used in the current buffer."
  4279. (when (derived-mode-p 'org-mode)
  4280. (org-set-local 'org-todo-kwd-alist nil)
  4281. (org-set-local 'org-todo-key-alist nil)
  4282. (org-set-local 'org-todo-key-trigger nil)
  4283. (org-set-local 'org-todo-keywords-1 nil)
  4284. (org-set-local 'org-done-keywords nil)
  4285. (org-set-local 'org-todo-heads nil)
  4286. (org-set-local 'org-todo-sets nil)
  4287. (org-set-local 'org-todo-log-states nil)
  4288. (org-set-local 'org-file-properties nil)
  4289. (let ((re (org-make-options-regexp
  4290. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4291. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4292. "SETUPFILE" "OPTIONS")
  4293. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4294. (splitre "[ \t]+")
  4295. (scripts org-use-sub-superscripts)
  4296. kwds kws0 kwsa key log value cat arch const links hw dws
  4297. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4298. (start 0))
  4299. (save-excursion
  4300. (save-restriction
  4301. (widen)
  4302. (goto-char (point-min))
  4303. (while
  4304. (or (and
  4305. ext-setup-or-nil
  4306. (let (ret)
  4307. (with-temp-buffer
  4308. (insert ext-setup-or-nil)
  4309. (let ((major-mode 'org-mode))
  4310. (setq ret (save-match-data
  4311. (org-set-regexps-and-options-for-tags)))))
  4312. ;; Append setupfile tags to existing tags
  4313. (setq org-file-tags
  4314. (delq nil (append org-file-tags (nth 0 ret)))
  4315. org-tag-alist
  4316. (delq nil (append org-tag-alist (nth 1 ret)))
  4317. org-tag-groups-alist
  4318. (delq nil (append 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\\}[0-9-+:hdwmy \t.]*\\)>")
  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\\}[0-9-+:hdwmy \t.]*\\)>")
  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. (defun org-file-contents (file &optional noerror)
  4553. "Return the contents of FILE, as a string."
  4554. (if (or (not file)
  4555. (not (file-readable-p file)))
  4556. (if noerror
  4557. (message "Cannot read file \"%s\"" file)
  4558. (error "Cannot read file \"%s\"" file))
  4559. (with-temp-buffer
  4560. (insert-file-contents file)
  4561. (buffer-string))))
  4562. (defun org-extract-log-state-settings (x)
  4563. "Extract the log state setting from a TODO keyword string.
  4564. This will extract info from a string like \"WAIT(w@/!)\"."
  4565. (let (kw key log1 log2)
  4566. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4567. (setq kw (match-string 1 x)
  4568. key (and (match-end 2) (match-string 2 x))
  4569. log1 (and (match-end 3) (match-string 3 x))
  4570. log2 (and (match-end 4) (match-string 4 x)))
  4571. (and (or log1 log2)
  4572. (list kw
  4573. (and log1 (if (equal log1 "!") 'time 'note))
  4574. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4575. (defun org-remove-keyword-keys (list)
  4576. "Remove a pair of parenthesis at the end of each string in LIST."
  4577. (mapcar (lambda (x)
  4578. (if (string-match "(.*)$" x)
  4579. (substring x 0 (match-beginning 0))
  4580. x))
  4581. list))
  4582. (defun org-assign-fast-keys (alist)
  4583. "Assign fast keys to a keyword-key alist.
  4584. Respect keys that are already there."
  4585. (let (new e (alt ?0))
  4586. (while (setq e (pop alist))
  4587. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4588. (cdr e)) ;; Key already assigned.
  4589. (push e new)
  4590. (let ((clist (string-to-list (downcase (car e))))
  4591. (used (append new alist)))
  4592. (when (= (car clist) ?@)
  4593. (pop clist))
  4594. (while (and clist (rassoc (car clist) used))
  4595. (pop clist))
  4596. (unless clist
  4597. (while (rassoc alt used)
  4598. (incf alt)))
  4599. (push (cons (car e) (or (car clist) alt)) new))))
  4600. (nreverse new)))
  4601. ;;; Some variables used in various places
  4602. (defvar org-window-configuration nil
  4603. "Used in various places to store a window configuration.")
  4604. (defvar org-selected-window nil
  4605. "Used in various places to store a window configuration.")
  4606. (defvar org-finish-function nil
  4607. "Function to be called when `C-c C-c' is used.
  4608. This is for getting out of special buffers like capture.")
  4609. ;; FIXME: Occasionally check by commenting these, to make sure
  4610. ;; no other functions uses these, forgetting to let-bind them.
  4611. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4612. (defvar org-last-state)
  4613. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4614. ;; Defined somewhere in this file, but used before definition.
  4615. (defvar org-entities) ;; defined in org-entities.el
  4616. (defvar org-struct-menu)
  4617. (defvar org-org-menu)
  4618. (defvar org-tbl-menu)
  4619. ;;;; Define the Org-mode
  4620. ;; We use a before-change function to check if a table might need
  4621. ;; an update.
  4622. (defvar org-table-may-need-update t
  4623. "Indicates that a table might need an update.
  4624. This variable is set by `org-before-change-function'.
  4625. `org-table-align' sets it back to nil.")
  4626. (defun org-before-change-function (beg end)
  4627. "Every change indicates that a table might need an update."
  4628. (setq org-table-may-need-update t))
  4629. (defvar org-mode-map)
  4630. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4631. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4632. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4633. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4634. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4635. (defvar org-table-buffer-is-an nil)
  4636. (defvar bidi-paragraph-direction)
  4637. (defvar buffer-face-mode-face)
  4638. (require 'outline)
  4639. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4640. (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"))
  4641. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4642. ;; Other stuff we need.
  4643. (require 'time-date)
  4644. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4645. (require 'easymenu)
  4646. (require 'overlay)
  4647. ;; (require 'org-macs) moved higher up in the file before it is first used
  4648. (require 'org-entities)
  4649. ;; (require 'org-compat) moved higher up in the file before it is first used
  4650. (require 'org-faces)
  4651. (require 'org-list)
  4652. (require 'org-pcomplete)
  4653. (require 'org-src)
  4654. (require 'org-footnote)
  4655. (require 'org-macro)
  4656. ;; babel
  4657. (require 'ob)
  4658. ;;;###autoload
  4659. (define-derived-mode org-mode outline-mode "Org"
  4660. "Outline-based notes management and organizer, alias
  4661. \"Carsten's outline-mode for keeping track of everything.\"
  4662. Org-mode develops organizational tasks around a NOTES file which
  4663. contains information about projects as plain text. Org-mode is
  4664. implemented on top of outline-mode, which is ideal to keep the content
  4665. of large files well structured. It supports ToDo items, deadlines and
  4666. time stamps, which magically appear in the diary listing of the Emacs
  4667. calendar. Tables are easily created with a built-in table editor.
  4668. Plain text URL-like links connect to websites, emails (VM), Usenet
  4669. messages (Gnus), BBDB entries, and any files related to the project.
  4670. For printing and sharing of notes, an Org-mode file (or a part of it)
  4671. can be exported as a structured ASCII or HTML file.
  4672. The following commands are available:
  4673. \\{org-mode-map}"
  4674. ;; Get rid of Outline menus, they are not needed
  4675. ;; Need to do this here because define-derived-mode sets up
  4676. ;; the keymap so late. Still, it is a waste to call this each time
  4677. ;; we switch another buffer into org-mode.
  4678. (if (featurep 'xemacs)
  4679. (when (boundp 'outline-mode-menu-heading)
  4680. ;; Assume this is Greg's port, it uses easymenu
  4681. (easy-menu-remove outline-mode-menu-heading)
  4682. (easy-menu-remove outline-mode-menu-show)
  4683. (easy-menu-remove outline-mode-menu-hide))
  4684. (define-key org-mode-map [menu-bar headings] 'undefined)
  4685. (define-key org-mode-map [menu-bar hide] 'undefined)
  4686. (define-key org-mode-map [menu-bar show] 'undefined))
  4687. (org-load-modules-maybe)
  4688. (easy-menu-add org-org-menu)
  4689. (easy-menu-add org-tbl-menu)
  4690. (org-install-agenda-files-menu)
  4691. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4692. (add-to-invisibility-spec '(org-cwidth))
  4693. (add-to-invisibility-spec '(org-hide-block . t))
  4694. (when (featurep 'xemacs)
  4695. (org-set-local 'line-move-ignore-invisible t))
  4696. (org-set-local 'outline-regexp org-outline-regexp)
  4697. (org-set-local 'outline-level 'org-outline-level)
  4698. (setq bidi-paragraph-direction 'left-to-right)
  4699. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4700. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4701. (when (and org-ellipsis
  4702. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4703. (fboundp 'make-glyph-code))
  4704. (unless org-display-table
  4705. (setq org-display-table (make-display-table)))
  4706. (set-display-table-slot
  4707. org-display-table 4
  4708. (vconcat (mapcar
  4709. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4710. org-ellipsis)))
  4711. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4712. (setq buffer-display-table org-display-table))
  4713. (org-set-regexps-and-options-for-tags)
  4714. (org-set-regexps-and-options)
  4715. (org-set-font-lock-defaults)
  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. (cond
  6720. ((or (= (buffer-size) 0)
  6721. (and (not (save-excursion
  6722. (and (ignore-errors (org-back-to-heading invisible-ok))
  6723. (org-at-heading-p))))
  6724. (or arg (not (org-in-item-p)))))
  6725. (insert
  6726. (if (org-previous-line-empty-p) "" "\n")
  6727. (if (org-in-src-block-p) ",* " "* "))
  6728. (run-hooks 'org-insert-heading-hook))
  6729. ((or arg
  6730. (and (not (org-in-item-p)) org-insert-heading-respect-content)
  6731. (not (org-insert-item
  6732. (save-excursion
  6733. (beginning-of-line)
  6734. (looking-at org-list-full-item-re)
  6735. (match-string 3)))))
  6736. (let (begn endn)
  6737. (when (org-buffer-narrowed-p)
  6738. (setq begn (point-min) endn (point-max))
  6739. (widen))
  6740. (let* ((empty-line-p nil)
  6741. (eops (equal arg '(16))) ; insert at end of parent subtree
  6742. (org-insert-heading-respect-content
  6743. (or (not (null arg)) org-insert-heading-respect-content))
  6744. (level nil)
  6745. (on-heading (org-at-heading-p))
  6746. ;; Get a level to fall back on
  6747. (fix-level
  6748. (save-excursion
  6749. (org-back-to-heading t)
  6750. (looking-at org-outline-regexp)
  6751. (make-string (1- (length (match-string 0))) ?*)))
  6752. (on-empty-line
  6753. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6754. (head (save-excursion
  6755. (condition-case nil
  6756. (progn
  6757. (org-back-to-heading invisible-ok)
  6758. (when (and (not on-heading)
  6759. (featurep 'org-inlinetask)
  6760. (integerp org-inlinetask-min-level)
  6761. (>= (length (match-string 0))
  6762. org-inlinetask-min-level))
  6763. ;; Find a heading level before the inline task
  6764. (while (and (setq level (org-up-heading-safe))
  6765. (>= level org-inlinetask-min-level)))
  6766. (if (org-at-heading-p)
  6767. (org-back-to-heading invisible-ok)
  6768. (error "This should not happen")))
  6769. (unless (and (save-excursion
  6770. (save-match-data
  6771. (org-backward-heading-same-level 1 invisible-ok))
  6772. (= (point) (match-beginning 0)))
  6773. (not (org-previous-line-empty-p t)))
  6774. (setq empty-line-p (org-previous-line-empty-p)))
  6775. (match-string 0))
  6776. (error (or fix-level "* ")))))
  6777. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6778. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6779. pos hide-previous previous-pos)
  6780. (if ;; At the beginning of a heading, open a new line for insertion
  6781. (and (bolp) (org-at-heading-p)
  6782. (not eops)
  6783. (or (bobp)
  6784. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6785. (open-line (if blank 2 1))
  6786. (save-excursion
  6787. (setq previous-pos (point-at-bol))
  6788. (end-of-line)
  6789. (setq hide-previous (outline-invisible-p)))
  6790. (and org-insert-heading-respect-content
  6791. (save-excursion
  6792. (while (outline-invisible-p)
  6793. (org-show-subtree)
  6794. (org-up-heading-safe))))
  6795. (let ((split
  6796. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6797. (save-excursion
  6798. (let ((p (point)))
  6799. (goto-char (point-at-bol))
  6800. (and (looking-at org-complex-heading-regexp)
  6801. (match-beginning 4)
  6802. (> p (match-beginning 4)))))))
  6803. tags pos)
  6804. (cond
  6805. ;; Insert a new line, possibly at end of parent subtree
  6806. ((and (not arg) (not on-heading) (not on-empty-line)
  6807. (not (save-excursion
  6808. (beginning-of-line 1)
  6809. (or (looking-at org-list-full-item-re)
  6810. ;; Don't convert :end: lines to headline
  6811. (looking-at "^\\s-*:end:")
  6812. (looking-at "^\\s-*#\\+end_?")))))
  6813. (beginning-of-line 1))
  6814. (org-insert-heading-respect-content
  6815. (if (not eops)
  6816. (progn
  6817. (org-end-of-subtree nil t)
  6818. (and (looking-at "^\\*") (backward-char 1))
  6819. (while (and (not (bobp))
  6820. ;; Don't delete spaces in empty headlines
  6821. (not (looking-back org-outline-regexp))
  6822. (member (char-before) '(?\ ?\t ?\n)))
  6823. (backward-delete-char 1)))
  6824. (let ((p (point)))
  6825. (org-up-heading-safe)
  6826. (if (= p (point))
  6827. (goto-char (point-max))
  6828. (org-end-of-subtree nil t))))
  6829. (when (featurep 'org-inlinetask)
  6830. (while (and (not (eobp))
  6831. (looking-at "\\(\\*+\\)[ \t]+")
  6832. (>= (length (match-string 1))
  6833. org-inlinetask-min-level))
  6834. (org-end-of-subtree nil t)))
  6835. (or (bolp) (newline))
  6836. (or (org-previous-line-empty-p)
  6837. (and blank (newline)))
  6838. (if (or empty-line-p eops) (open-line 1)))
  6839. ;; Insert a headling containing text after point
  6840. ((org-at-heading-p)
  6841. (when hide-previous
  6842. (show-children)
  6843. (org-show-entry))
  6844. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6845. (setq tags (and (match-end 2) (match-string 2)))
  6846. (and (match-end 1)
  6847. (delete-region (match-beginning 1) (match-end 1)))
  6848. (setq pos (point-at-bol))
  6849. (or split (end-of-line 1))
  6850. (delete-horizontal-space)
  6851. (if (string-match "\\`\\*+\\'"
  6852. (buffer-substring (point-at-bol) (point)))
  6853. (insert " "))
  6854. (newline (if blank 2 1))
  6855. (when tags
  6856. (save-excursion
  6857. (goto-char pos)
  6858. (end-of-line 1)
  6859. (insert " " tags)
  6860. (org-set-tags nil 'align))))
  6861. (t
  6862. (or split (end-of-line 1))
  6863. (newline (cond ((and blank (not on-empty-line)) 2)
  6864. (blank 1)
  6865. (on-empty-line 0) (t 1)))))))
  6866. (insert head) (just-one-space)
  6867. (setq pos (point))
  6868. (end-of-line 1)
  6869. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6870. (when (and org-insert-heading-respect-content hide-previous)
  6871. (save-excursion
  6872. (goto-char previous-pos)
  6873. (hide-subtree)))
  6874. (when (and begn endn)
  6875. (narrow-to-region (min (point) begn) (max (point) endn)))
  6876. (run-hooks 'org-insert-heading-hook))))))
  6877. (defun org-get-heading (&optional no-tags no-todo)
  6878. "Return the heading of the current entry, without the stars.
  6879. When NO-TAGS is non-nil, don't include tags.
  6880. When NO-TODO is non-nil, don't include TODO keywords."
  6881. (save-excursion
  6882. (org-back-to-heading t)
  6883. (cond
  6884. ((and no-tags no-todo)
  6885. (looking-at org-complex-heading-regexp)
  6886. (match-string 4))
  6887. (no-tags
  6888. (looking-at (concat org-outline-regexp
  6889. "\\(.*?\\)"
  6890. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6891. (match-string 1))
  6892. (no-todo
  6893. (looking-at org-todo-line-regexp)
  6894. (match-string 3))
  6895. (t (looking-at org-heading-regexp)
  6896. (match-string 2)))))
  6897. (defvar orgstruct-mode) ; defined below
  6898. (defun org-heading-components ()
  6899. "Return the components of the current heading.
  6900. This is a list with the following elements:
  6901. - the level as an integer
  6902. - the reduced level, different if `org-odd-levels-only' is set.
  6903. - the TODO keyword, or nil
  6904. - the priority character, like ?A, or nil if no priority is given
  6905. - the headline text itself, or the tags string if no headline text
  6906. - the tags string, or nil."
  6907. (save-excursion
  6908. (org-back-to-heading t)
  6909. (if (let (case-fold-search)
  6910. (looking-at
  6911. (if orgstruct-mode
  6912. org-heading-regexp
  6913. org-complex-heading-regexp)))
  6914. (if orgstruct-mode
  6915. (list (length (match-string 1))
  6916. (org-reduced-level (length (match-string 1)))
  6917. nil
  6918. nil
  6919. (match-string 2)
  6920. nil)
  6921. (list (length (match-string 1))
  6922. (org-reduced-level (length (match-string 1)))
  6923. (org-match-string-no-properties 2)
  6924. (and (match-end 3) (aref (match-string 3) 2))
  6925. (org-match-string-no-properties 4)
  6926. (org-match-string-no-properties 5))))))
  6927. (defun org-get-entry ()
  6928. "Get the entry text, after heading, entire subtree."
  6929. (save-excursion
  6930. (org-back-to-heading t)
  6931. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6932. (defun org-insert-heading-after-current ()
  6933. "Insert a new heading with same level as current, after current subtree."
  6934. (interactive)
  6935. (org-back-to-heading)
  6936. (org-insert-heading)
  6937. (org-move-subtree-down)
  6938. (end-of-line 1))
  6939. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6940. "Insert heading with `org-insert-heading-respect-content' set to t."
  6941. (interactive "P")
  6942. (let ((org-insert-heading-respect-content t))
  6943. (org-insert-heading arg invisible-ok)))
  6944. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6945. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6946. (interactive "P")
  6947. (let ((org-insert-heading-respect-content t))
  6948. (org-insert-todo-heading force-state t)))
  6949. (defun org-insert-todo-heading (arg &optional force-heading)
  6950. "Insert a new heading with the same level and TODO state as current heading.
  6951. If the heading has no TODO state, or if the state is DONE, use the first
  6952. state (TODO by default). Also one prefix arg, force first state. With two
  6953. prefix args, force inserting at the end of the parent subtree."
  6954. (interactive "P")
  6955. (when (or force-heading (not (org-insert-item 'checkbox)))
  6956. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6957. force-heading))
  6958. (save-excursion
  6959. (org-back-to-heading)
  6960. (outline-previous-heading)
  6961. (looking-at org-todo-line-regexp))
  6962. (let*
  6963. ((new-mark-x
  6964. (if (or arg
  6965. (not (match-beginning 2))
  6966. (member (match-string 2) org-done-keywords))
  6967. (car org-todo-keywords-1)
  6968. (match-string 2)))
  6969. (new-mark
  6970. (or
  6971. (run-hook-with-args-until-success
  6972. 'org-todo-get-default-hook new-mark-x nil)
  6973. new-mark-x)))
  6974. (beginning-of-line 1)
  6975. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6976. (if org-treat-insert-todo-heading-as-state-change
  6977. (org-todo new-mark)
  6978. (insert new-mark " "))))
  6979. (when org-provide-todo-statistics
  6980. (org-update-parent-todo-statistics))))
  6981. (defun org-insert-subheading (arg)
  6982. "Insert a new subheading and demote it.
  6983. Works for outline headings and for plain lists alike."
  6984. (interactive "P")
  6985. (org-insert-heading arg)
  6986. (cond
  6987. ((org-at-heading-p) (org-do-demote))
  6988. ((org-at-item-p) (org-indent-item))))
  6989. (defun org-insert-todo-subheading (arg)
  6990. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6991. Works for outline headings and for plain lists alike."
  6992. (interactive "P")
  6993. (org-insert-todo-heading arg)
  6994. (cond
  6995. ((org-at-heading-p) (org-do-demote))
  6996. ((org-at-item-p) (org-indent-item))))
  6997. ;;; Promotion and Demotion
  6998. (defvar org-after-demote-entry-hook nil
  6999. "Hook run after an entry has been demoted.
  7000. The cursor will be at the beginning of the entry.
  7001. When a subtree is being demoted, the hook will be called for each node.")
  7002. (defvar org-after-promote-entry-hook nil
  7003. "Hook run after an entry has been promoted.
  7004. The cursor will be at the beginning of the entry.
  7005. When a subtree is being promoted, the hook will be called for each node.")
  7006. (defun org-promote-subtree ()
  7007. "Promote the entire subtree.
  7008. See also `org-promote'."
  7009. (interactive)
  7010. (save-excursion
  7011. (org-with-limited-levels (org-map-tree 'org-promote)))
  7012. (org-fix-position-after-promote))
  7013. (defun org-demote-subtree ()
  7014. "Demote the entire subtree. See `org-demote'.
  7015. See also `org-promote'."
  7016. (interactive)
  7017. (save-excursion
  7018. (org-with-limited-levels (org-map-tree 'org-demote)))
  7019. (org-fix-position-after-promote))
  7020. (defun org-do-promote ()
  7021. "Promote the current heading higher up the tree.
  7022. If the region is active in `transient-mark-mode', promote all headings
  7023. in the region."
  7024. (interactive)
  7025. (save-excursion
  7026. (if (org-region-active-p)
  7027. (org-map-region 'org-promote (region-beginning) (region-end))
  7028. (org-promote)))
  7029. (org-fix-position-after-promote))
  7030. (defun org-do-demote ()
  7031. "Demote the current heading lower down the tree.
  7032. If the region is active in `transient-mark-mode', demote all headings
  7033. in the region."
  7034. (interactive)
  7035. (save-excursion
  7036. (if (org-region-active-p)
  7037. (org-map-region 'org-demote (region-beginning) (region-end))
  7038. (org-demote)))
  7039. (org-fix-position-after-promote))
  7040. (defun org-fix-position-after-promote ()
  7041. "Make sure that after pro/demotion cursor position is right."
  7042. (let ((pos (point)))
  7043. (when (save-excursion
  7044. (beginning-of-line 1)
  7045. (looking-at org-todo-line-regexp)
  7046. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7047. (cond ((eobp) (insert " "))
  7048. ((eolp) (insert " "))
  7049. ((equal (char-after) ?\ ) (forward-char 1))))))
  7050. (defun org-current-level ()
  7051. "Return the level of the current entry, or nil if before the first headline.
  7052. The level is the number of stars at the beginning of the headline."
  7053. (save-excursion
  7054. (org-with-limited-levels
  7055. (if (ignore-errors (org-back-to-heading t))
  7056. (funcall outline-level)))))
  7057. (defun org-get-previous-line-level ()
  7058. "Return the outline depth of the last headline before the current line.
  7059. Returns 0 for the first headline in the buffer, and nil if before the
  7060. first headline."
  7061. (let ((current-level (org-current-level))
  7062. (prev-level (when (> (line-number-at-pos) 1)
  7063. (save-excursion
  7064. (beginning-of-line 0)
  7065. (org-current-level)))))
  7066. (cond ((null current-level) nil) ; Before first headline
  7067. ((null prev-level) 0) ; At first headline
  7068. (prev-level))))
  7069. (defun org-reduced-level (l)
  7070. "Compute the effective level of a heading.
  7071. This takes into account the setting of `org-odd-levels-only'."
  7072. (cond
  7073. ((zerop l) 0)
  7074. (org-odd-levels-only (1+ (floor (/ l 2))))
  7075. (t l)))
  7076. (defun org-level-increment ()
  7077. "Return the number of stars that will be added or removed at a
  7078. time to headlines when structure editing, based on the value of
  7079. `org-odd-levels-only'."
  7080. (if org-odd-levels-only 2 1))
  7081. (defun org-get-valid-level (level &optional change)
  7082. "Rectify a level change under the influence of `org-odd-levels-only'
  7083. LEVEL is a current level, CHANGE is by how much the level should be
  7084. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7085. even level numbers will become the next higher odd number."
  7086. (if org-odd-levels-only
  7087. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7088. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7089. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7090. (max 1 (+ level (or change 0)))))
  7091. (if (boundp 'define-obsolete-function-alias)
  7092. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7093. (define-obsolete-function-alias 'org-get-legal-level
  7094. 'org-get-valid-level)
  7095. (define-obsolete-function-alias 'org-get-legal-level
  7096. 'org-get-valid-level "23.1")))
  7097. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7098. ;; ̀org-with-limited-levels'
  7099. (defun org-promote ()
  7100. "Promote the current heading higher up the tree.
  7101. If the region is active in `transient-mark-mode', promote all headings
  7102. in the region."
  7103. (org-back-to-heading t)
  7104. (let* ((level (save-match-data (funcall outline-level)))
  7105. (after-change-functions (remove 'flyspell-after-change-function
  7106. after-change-functions))
  7107. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7108. (diff (abs (- level (length up-head) -1))))
  7109. (cond ((and (= level 1) org-called-with-limited-levels
  7110. org-allow-promoting-top-level-subtree)
  7111. (replace-match "# " nil t))
  7112. ((= level 1)
  7113. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7114. (t (replace-match up-head nil t)))
  7115. ;; Fixup tag positioning
  7116. (unless (= level 1)
  7117. (and org-auto-align-tags (org-set-tags nil t))
  7118. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7119. (run-hooks 'org-after-promote-entry-hook)))
  7120. (defun org-demote ()
  7121. "Demote the current heading lower down the tree.
  7122. If the region is active in `transient-mark-mode', demote all headings
  7123. in the region."
  7124. (org-back-to-heading t)
  7125. (let* ((level (save-match-data (funcall outline-level)))
  7126. (after-change-functions (remove 'flyspell-after-change-function
  7127. after-change-functions))
  7128. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7129. (diff (abs (- level (length down-head) -1))))
  7130. (replace-match down-head nil t)
  7131. ;; Fixup tag positioning
  7132. (and org-auto-align-tags (org-set-tags nil t))
  7133. (if org-adapt-indentation (org-fixup-indentation diff))
  7134. (run-hooks 'org-after-demote-entry-hook)))
  7135. (defun org-cycle-level ()
  7136. "Cycle the level of an empty headline through possible states.
  7137. This goes first to child, then to parent, level, then up the hierarchy.
  7138. After top level, it switches back to sibling level."
  7139. (interactive)
  7140. (let ((org-adapt-indentation nil))
  7141. (when (org-point-at-end-of-empty-headline)
  7142. (setq this-command 'org-cycle-level) ; Only needed for caching
  7143. (let ((cur-level (org-current-level))
  7144. (prev-level (org-get-previous-line-level)))
  7145. (cond
  7146. ;; If first headline in file, promote to top-level.
  7147. ((= prev-level 0)
  7148. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7149. do (org-do-promote)))
  7150. ;; If same level as prev, demote one.
  7151. ((= prev-level cur-level)
  7152. (org-do-demote))
  7153. ;; If parent is top-level, promote to top level if not already.
  7154. ((= prev-level 1)
  7155. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7156. do (org-do-promote)))
  7157. ;; If top-level, return to prev-level.
  7158. ((= cur-level 1)
  7159. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7160. do (org-do-demote)))
  7161. ;; If less than prev-level, promote one.
  7162. ((< cur-level prev-level)
  7163. (org-do-promote))
  7164. ;; If deeper than prev-level, promote until higher than
  7165. ;; prev-level.
  7166. ((> cur-level prev-level)
  7167. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7168. do (org-do-promote))))
  7169. t))))
  7170. (defun org-map-tree (fun)
  7171. "Call FUN for every heading underneath the current one."
  7172. (org-back-to-heading)
  7173. (let ((level (funcall outline-level)))
  7174. (save-excursion
  7175. (funcall fun)
  7176. (while (and (progn
  7177. (outline-next-heading)
  7178. (> (funcall outline-level) level))
  7179. (not (eobp)))
  7180. (funcall fun)))))
  7181. (defun org-map-region (fun beg end)
  7182. "Call FUN for every heading between BEG and END."
  7183. (let ((org-ignore-region t))
  7184. (save-excursion
  7185. (setq end (copy-marker end))
  7186. (goto-char beg)
  7187. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7188. (< (point) end))
  7189. (funcall fun))
  7190. (while (and (progn
  7191. (outline-next-heading)
  7192. (< (point) end))
  7193. (not (eobp)))
  7194. (funcall fun)))))
  7195. (defvar org-property-end-re) ; silence byte-compiler
  7196. (defun org-fixup-indentation (diff)
  7197. "Change the indentation in the current entry by DIFF.
  7198. However, if any line in the current entry has no indentation, or if it
  7199. would end up with no indentation after the change, nothing at all is done."
  7200. (save-excursion
  7201. (let ((end (save-excursion (outline-next-heading)
  7202. (point-marker)))
  7203. (prohibit (if (> diff 0)
  7204. "^\\S-"
  7205. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7206. col)
  7207. (unless (save-excursion (end-of-line 1)
  7208. (re-search-forward prohibit end t))
  7209. (while (and (< (point) end)
  7210. (re-search-forward "^[ \t]+" end t))
  7211. (goto-char (match-end 0))
  7212. (setq col (current-column))
  7213. (if (< diff 0) (replace-match ""))
  7214. (org-indent-to-column (+ diff col))))
  7215. (move-marker end nil))))
  7216. (defun org-convert-to-odd-levels ()
  7217. "Convert an org-mode file with all levels allowed to one with odd levels.
  7218. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7219. level 5 etc."
  7220. (interactive)
  7221. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7222. (let ((outline-level 'org-outline-level)
  7223. (org-odd-levels-only nil) n)
  7224. (save-excursion
  7225. (goto-char (point-min))
  7226. (while (re-search-forward "^\\*\\*+ " nil t)
  7227. (setq n (- (length (match-string 0)) 2))
  7228. (while (>= (setq n (1- n)) 0)
  7229. (org-demote))
  7230. (end-of-line 1))))))
  7231. (defun org-convert-to-oddeven-levels ()
  7232. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7233. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7234. file contains a section with an even level, conversion would
  7235. destroy the structure of the file. An error is signaled in this
  7236. case."
  7237. (interactive)
  7238. (goto-char (point-min))
  7239. ;; First check if there are no even levels
  7240. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7241. (org-show-context t)
  7242. (error "Not all levels are odd in this file. Conversion not possible"))
  7243. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7244. (let ((outline-regexp org-outline-regexp)
  7245. (outline-level 'org-outline-level)
  7246. (org-odd-levels-only nil) n)
  7247. (save-excursion
  7248. (goto-char (point-min))
  7249. (while (re-search-forward "^\\*\\*+ " nil t)
  7250. (setq n (/ (1- (length (match-string 0))) 2))
  7251. (while (>= (setq n (1- n)) 0)
  7252. (org-promote))
  7253. (end-of-line 1))))))
  7254. (defun org-tr-level (n)
  7255. "Make N odd if required."
  7256. (if org-odd-levels-only (1+ (/ n 2)) n))
  7257. ;;; Vertical tree motion, cutting and pasting of subtrees
  7258. (defun org-move-subtree-up (&optional arg)
  7259. "Move the current subtree up past ARG headlines of the same level."
  7260. (interactive "p")
  7261. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7262. (defun org-move-subtree-down (&optional arg)
  7263. "Move the current subtree down past ARG headlines of the same level."
  7264. (interactive "p")
  7265. (setq arg (prefix-numeric-value arg))
  7266. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7267. 'org-get-last-sibling))
  7268. (ins-point (make-marker))
  7269. (cnt (abs arg))
  7270. (col (current-column))
  7271. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7272. ;; Select the tree
  7273. (org-back-to-heading)
  7274. (setq beg0 (point))
  7275. (save-excursion
  7276. (setq ne-beg (org-back-over-empty-lines))
  7277. (setq beg (point)))
  7278. (save-match-data
  7279. (save-excursion (outline-end-of-heading)
  7280. (setq folded (outline-invisible-p)))
  7281. (outline-end-of-subtree))
  7282. (outline-next-heading)
  7283. (setq ne-end (org-back-over-empty-lines))
  7284. (setq end (point))
  7285. (goto-char beg0)
  7286. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7287. ;; include less whitespace
  7288. (save-excursion
  7289. (goto-char beg)
  7290. (forward-line (- ne-beg ne-end))
  7291. (setq beg (point))))
  7292. ;; Find insertion point, with error handling
  7293. (while (> cnt 0)
  7294. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7295. (progn (goto-char beg0)
  7296. (user-error "Cannot move past superior level or buffer limit")))
  7297. (setq cnt (1- cnt)))
  7298. (if (> arg 0)
  7299. ;; Moving forward - still need to move over subtree
  7300. (progn (org-end-of-subtree t t)
  7301. (save-excursion
  7302. (org-back-over-empty-lines)
  7303. (or (bolp) (newline)))))
  7304. (setq ne-ins (org-back-over-empty-lines))
  7305. (move-marker ins-point (point))
  7306. (setq txt (buffer-substring beg end))
  7307. (org-save-markers-in-region beg end)
  7308. (delete-region beg end)
  7309. (org-remove-empty-overlays-at beg)
  7310. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7311. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7312. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7313. (let ((bbb (point)))
  7314. (insert-before-markers txt)
  7315. (org-reinstall-markers-in-region bbb)
  7316. (move-marker ins-point bbb))
  7317. (or (bolp) (insert "\n"))
  7318. (setq ins-end (point))
  7319. (goto-char ins-point)
  7320. (org-skip-whitespace)
  7321. (when (and (< arg 0)
  7322. (org-first-sibling-p)
  7323. (> ne-ins ne-beg))
  7324. ;; Move whitespace back to beginning
  7325. (save-excursion
  7326. (goto-char ins-end)
  7327. (let ((kill-whole-line t))
  7328. (kill-line (- ne-ins ne-beg)) (point)))
  7329. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7330. (move-marker ins-point nil)
  7331. (if folded
  7332. (hide-subtree)
  7333. (org-show-entry)
  7334. (show-children)
  7335. (org-cycle-hide-drawers 'children))
  7336. (org-clean-visibility-after-subtree-move)
  7337. ;; move back to the initial column we were at
  7338. (move-to-column col)))
  7339. (defvar org-subtree-clip ""
  7340. "Clipboard for cut and paste of subtrees.
  7341. This is actually only a copy of the kill, because we use the normal kill
  7342. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7343. (defvar org-subtree-clip-folded nil
  7344. "Was the last copied subtree folded?
  7345. This is used to fold the tree back after pasting.")
  7346. (defun org-cut-subtree (&optional n)
  7347. "Cut the current subtree into the clipboard.
  7348. With prefix arg N, cut this many sequential subtrees.
  7349. This is a short-hand for marking the subtree and then cutting it."
  7350. (interactive "p")
  7351. (org-copy-subtree n 'cut))
  7352. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7353. "Copy the current subtree it in the clipboard.
  7354. With prefix arg N, copy this many sequential subtrees.
  7355. This is a short-hand for marking the subtree and then copying it.
  7356. If CUT is non-nil, actually cut the subtree.
  7357. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7358. of some markers in the region, even if CUT is non-nil. This is
  7359. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7360. (interactive "p")
  7361. (let (beg end folded (beg0 (point)))
  7362. (if (org-called-interactively-p 'any)
  7363. (org-back-to-heading nil) ; take what looks like a subtree
  7364. (org-back-to-heading t)) ; take what is really there
  7365. (setq beg (point))
  7366. (skip-chars-forward " \t\r\n")
  7367. (save-match-data
  7368. (if nosubtrees
  7369. (outline-next-heading)
  7370. (save-excursion (outline-end-of-heading)
  7371. (setq folded (outline-invisible-p)))
  7372. (condition-case nil
  7373. (org-forward-heading-same-level (1- n) t)
  7374. (error nil))
  7375. (org-end-of-subtree t t)))
  7376. (setq end (point))
  7377. (goto-char beg0)
  7378. (when (> end beg)
  7379. (setq org-subtree-clip-folded folded)
  7380. (when (or cut force-store-markers)
  7381. (org-save-markers-in-region beg end))
  7382. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7383. (setq org-subtree-clip (current-kill 0))
  7384. (message "%s: Subtree(s) with %d characters"
  7385. (if cut "Cut" "Copied")
  7386. (length org-subtree-clip)))))
  7387. (defun org-paste-subtree (&optional level tree for-yank)
  7388. "Paste the clipboard as a subtree, with modification of headline level.
  7389. The entire subtree is promoted or demoted in order to match a new headline
  7390. level.
  7391. If the cursor is at the beginning of a headline, the same level as
  7392. that headline is used to paste the tree.
  7393. If not, the new level is derived from the *visible* headings
  7394. before and after the insertion point, and taken to be the inferior headline
  7395. level of the two. So if the previous visible heading is level 3 and the
  7396. next is level 4 (or vice versa), level 4 will be used for insertion.
  7397. This makes sure that the subtree remains an independent subtree and does
  7398. not swallow low level entries.
  7399. You can also force a different level, either by using a numeric prefix
  7400. argument, or by inserting the heading marker by hand. For example, if the
  7401. cursor is after \"*****\", then the tree will be shifted to level 5.
  7402. If optional TREE is given, use this text instead of the kill ring.
  7403. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7404. move back over whitespace before inserting, and move point to the end of
  7405. the inserted text when done."
  7406. (interactive "P")
  7407. (setq tree (or tree (and kill-ring (current-kill 0))))
  7408. (unless (org-kill-is-subtree-p tree)
  7409. (user-error "%s"
  7410. (substitute-command-keys
  7411. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7412. (org-with-limited-levels
  7413. (let* ((visp (not (outline-invisible-p)))
  7414. (txt tree)
  7415. (^re_ "\\(\\*+\\)[ \t]*")
  7416. (old-level (if (string-match org-outline-regexp-bol txt)
  7417. (- (match-end 0) (match-beginning 0) 1)
  7418. -1))
  7419. (force-level (cond (level (prefix-numeric-value level))
  7420. ((and (looking-at "[ \t]*$")
  7421. (string-match
  7422. "^\\*+$" (buffer-substring
  7423. (point-at-bol) (point))))
  7424. (- (match-end 1) (match-beginning 1)))
  7425. ((and (bolp)
  7426. (looking-at org-outline-regexp))
  7427. (- (match-end 0) (point) 1))))
  7428. (previous-level (save-excursion
  7429. (condition-case nil
  7430. (progn
  7431. (outline-previous-visible-heading 1)
  7432. (if (looking-at ^re_)
  7433. (- (match-end 0) (match-beginning 0) 1)
  7434. 1))
  7435. (error 1))))
  7436. (next-level (save-excursion
  7437. (condition-case nil
  7438. (progn
  7439. (or (looking-at org-outline-regexp)
  7440. (outline-next-visible-heading 1))
  7441. (if (looking-at ^re_)
  7442. (- (match-end 0) (match-beginning 0) 1)
  7443. 1))
  7444. (error 1))))
  7445. (new-level (or force-level (max previous-level next-level)))
  7446. (shift (if (or (= old-level -1)
  7447. (= new-level -1)
  7448. (= old-level new-level))
  7449. 0
  7450. (- new-level old-level)))
  7451. (delta (if (> shift 0) -1 1))
  7452. (func (if (> shift 0) 'org-demote 'org-promote))
  7453. (org-odd-levels-only nil)
  7454. beg end newend)
  7455. ;; Remove the forced level indicator
  7456. (if force-level
  7457. (delete-region (point-at-bol) (point)))
  7458. ;; Paste
  7459. (beginning-of-line (if (bolp) 1 2))
  7460. (setq beg (point))
  7461. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7462. (insert-before-markers txt)
  7463. (unless (string-match "\n\\'" txt) (insert "\n"))
  7464. (setq newend (point))
  7465. (org-reinstall-markers-in-region beg)
  7466. (setq end (point))
  7467. (goto-char beg)
  7468. (skip-chars-forward " \t\n\r")
  7469. (setq beg (point))
  7470. (if (and (outline-invisible-p) visp)
  7471. (save-excursion (outline-show-heading)))
  7472. ;; Shift if necessary
  7473. (unless (= shift 0)
  7474. (save-restriction
  7475. (narrow-to-region beg end)
  7476. (while (not (= shift 0))
  7477. (org-map-region func (point-min) (point-max))
  7478. (setq shift (+ delta shift)))
  7479. (goto-char (point-min))
  7480. (setq newend (point-max))))
  7481. (when (or (org-called-interactively-p 'interactive) for-yank)
  7482. (message "Clipboard pasted as level %d subtree" new-level))
  7483. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7484. kill-ring
  7485. (eq org-subtree-clip (current-kill 0))
  7486. org-subtree-clip-folded)
  7487. ;; The tree was folded before it was killed/copied
  7488. (hide-subtree))
  7489. (and for-yank (goto-char newend)))))
  7490. (defun org-kill-is-subtree-p (&optional txt)
  7491. "Check if the current kill is an outline subtree, or a set of trees.
  7492. Returns nil if kill does not start with a headline, or if the first
  7493. headline level is not the largest headline level in the tree.
  7494. So this will actually accept several entries of equal levels as well,
  7495. which is OK for `org-paste-subtree'.
  7496. If optional TXT is given, check this string instead of the current kill."
  7497. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7498. (re (org-get-limited-outline-regexp))
  7499. (^re (concat "^" re))
  7500. (start-level (and kill
  7501. (string-match
  7502. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7503. kill)
  7504. (- (match-end 2) (match-beginning 2) 1)))
  7505. (start (1+ (or (match-beginning 2) -1))))
  7506. (if (not start-level)
  7507. (progn
  7508. nil) ;; does not even start with a heading
  7509. (catch 'exit
  7510. (while (setq start (string-match ^re kill (1+ start)))
  7511. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7512. (throw 'exit nil)))
  7513. t))))
  7514. (defvar org-markers-to-move nil
  7515. "Markers that should be moved with a cut-and-paste operation.
  7516. Those markers are stored together with their positions relative to
  7517. the start of the region.")
  7518. (defun org-save-markers-in-region (beg end)
  7519. "Check markers in region.
  7520. If these markers are between BEG and END, record their position relative
  7521. to BEG, so that after moving the block of text, we can put the markers back
  7522. into place.
  7523. This function gets called just before an entry or tree gets cut from the
  7524. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7525. called immediately, to move the markers with the entries."
  7526. (setq org-markers-to-move nil)
  7527. (when (featurep 'org-clock)
  7528. (org-clock-save-markers-for-cut-and-paste beg end))
  7529. (when (featurep 'org-agenda)
  7530. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7531. (defun org-check-and-save-marker (marker beg end)
  7532. "Check if MARKER is between BEG and END.
  7533. If yes, remember the marker and the distance to BEG."
  7534. (when (and (marker-buffer marker)
  7535. (equal (marker-buffer marker) (current-buffer)))
  7536. (if (and (>= marker beg) (< marker end))
  7537. (push (cons marker (- marker beg)) org-markers-to-move))))
  7538. (defun org-reinstall-markers-in-region (beg)
  7539. "Move all remembered markers to their position relative to BEG."
  7540. (mapc (lambda (x)
  7541. (move-marker (car x) (+ beg (cdr x))))
  7542. org-markers-to-move)
  7543. (setq org-markers-to-move nil))
  7544. (defun org-narrow-to-subtree ()
  7545. "Narrow buffer to the current subtree."
  7546. (interactive)
  7547. (save-excursion
  7548. (save-match-data
  7549. (org-with-limited-levels
  7550. (narrow-to-region
  7551. (progn (org-back-to-heading t) (point))
  7552. (progn (org-end-of-subtree t t)
  7553. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7554. (point)))))))
  7555. (defun org-narrow-to-block ()
  7556. "Narrow buffer to the current block."
  7557. (interactive)
  7558. (let* ((case-fold-search t)
  7559. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7560. "^[ \t]*#\\+end_.*")))
  7561. (if blockp
  7562. (narrow-to-region (car blockp) (cdr blockp))
  7563. (user-error "Not in a block"))))
  7564. (eval-when-compile
  7565. (defvar org-property-drawer-re))
  7566. (defvar org-property-start-re) ;; defined below
  7567. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7568. "Clone the task (subtree) at point N times.
  7569. The clones will be inserted as siblings.
  7570. In interactive use, the user will be prompted for the number of
  7571. clones to be produced. If the entry has a timestamp, the user
  7572. will also be prompted for a time shift, which may be a repeater
  7573. as used in time stamps, for example `+3d'. To disable this,
  7574. you can call the function with a universal prefix argument.
  7575. When a valid repeater is given and the entry contains any time
  7576. stamps, the clones will become a sequence in time, with time
  7577. stamps in the subtree shifted for each clone produced. If SHIFT
  7578. is nil or the empty string, time stamps will be left alone. The
  7579. ID property of the original subtree is removed.
  7580. If the original subtree did contain time stamps with a repeater,
  7581. the following will happen:
  7582. - the repeater will be removed in each clone
  7583. - an additional clone will be produced, with the current, unshifted
  7584. date(s) in the entry.
  7585. - the original entry will be placed *after* all the clones, with
  7586. repeater intact.
  7587. - the start days in the repeater in the original entry will be shifted
  7588. to past the last clone.
  7589. In this way you can spell out a number of instances of a repeating task,
  7590. and still retain the repeater to cover future instances of the task."
  7591. (interactive "nNumber of clones to produce: ")
  7592. (let ((shift
  7593. (or shift
  7594. (if (and (not (equal current-prefix-arg '(4)))
  7595. (save-excursion
  7596. (re-search-forward org-ts-regexp-both
  7597. (save-excursion
  7598. (org-end-of-subtree t)
  7599. (point)) t)))
  7600. (read-from-minibuffer
  7601. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7602. ""))) ;; No time shift
  7603. (n-no-remove -1)
  7604. (drawer-re org-drawer-regexp)
  7605. beg end template task idprop
  7606. shift-n shift-what doshift nmin nmax)
  7607. (if (not (and (integerp n) (> n 0)))
  7608. (error "Invalid number of replications %s" n))
  7609. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7610. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7611. shift)))
  7612. (error "Invalid shift specification %s" shift))
  7613. (when doshift
  7614. (setq shift-n (string-to-number (match-string 1 shift))
  7615. shift-what (cdr (assoc (match-string 2 shift)
  7616. '(("d" . day) ("w" . week)
  7617. ("m" . month) ("y" . year))))))
  7618. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7619. (setq nmin 1 nmax n)
  7620. (org-back-to-heading t)
  7621. (setq beg (point))
  7622. (setq idprop (org-entry-get nil "ID"))
  7623. (org-end-of-subtree t t)
  7624. (or (bolp) (insert "\n"))
  7625. (setq end (point))
  7626. (setq template (buffer-substring beg end))
  7627. (when (and doshift
  7628. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7629. (delete-region beg end)
  7630. (setq end beg)
  7631. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7632. (goto-char end)
  7633. (loop for n from nmin to nmax do
  7634. ;; prepare clone
  7635. (with-temp-buffer
  7636. (insert template)
  7637. (org-mode)
  7638. (goto-char (point-min))
  7639. (org-show-subtree)
  7640. (and idprop (if org-clone-delete-id
  7641. (org-entry-delete nil "ID")
  7642. (org-id-get-create t)))
  7643. (unless (= n 0)
  7644. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7645. (kill-whole-line))
  7646. (goto-char (point-min))
  7647. (while (re-search-forward drawer-re nil t)
  7648. (mapc (lambda (d)
  7649. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7650. (goto-char (point-min))
  7651. (when doshift
  7652. (while (re-search-forward org-ts-regexp-both nil t)
  7653. (org-timestamp-change (* n shift-n) shift-what))
  7654. (unless (= n n-no-remove)
  7655. (goto-char (point-min))
  7656. (while (re-search-forward org-ts-regexp nil t)
  7657. (save-excursion
  7658. (goto-char (match-beginning 0))
  7659. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7660. (delete-region (match-beginning 1) (match-end 1)))))))
  7661. (setq task (buffer-string)))
  7662. (insert task))
  7663. (goto-char beg)))
  7664. ;;; Outline Sorting
  7665. (defun org-sort (with-case)
  7666. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7667. Optional argument WITH-CASE means sort case-sensitively."
  7668. (interactive "P")
  7669. (cond
  7670. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7671. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7672. (t
  7673. (org-call-with-arg 'org-sort-entries with-case))))
  7674. (defun org-sort-remove-invisible (s)
  7675. "Remove invisible links from string S."
  7676. (remove-text-properties 0 (length s) org-rm-props s)
  7677. (while (string-match org-bracket-link-regexp s)
  7678. (setq s (replace-match (if (match-end 2)
  7679. (match-string 3 s)
  7680. (match-string 1 s)) t t s)))
  7681. (let ((st (format " %s " s)))
  7682. (while (string-match org-emph-re st)
  7683. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7684. (setq s (substring st 1 -1)))
  7685. s)
  7686. (defvar org-priority-regexp) ; defined later in the file
  7687. (defvar org-after-sorting-entries-or-items-hook nil
  7688. "Hook that is run after a bunch of entries or items have been sorted.
  7689. When children are sorted, the cursor is in the parent line when this
  7690. hook gets called. When a region or a plain list is sorted, the cursor
  7691. will be in the first entry of the sorted region/list.")
  7692. (defun org-sort-entries
  7693. (&optional with-case sorting-type getkey-func compare-func property)
  7694. "Sort entries on a certain level of an outline tree.
  7695. If there is an active region, the entries in the region are sorted.
  7696. Else, if the cursor is before the first entry, sort the top-level items.
  7697. Else, the children of the entry at point are sorted.
  7698. Sorting can be alphabetically, numerically, by date/time as given by
  7699. a time stamp, by a property, by priority order, or by a custom function.
  7700. The command prompts for the sorting type unless it has been given to the
  7701. function through the SORTING-TYPE argument, which needs to be a character,
  7702. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7703. precise meaning of each character:
  7704. n Numerically, by converting the beginning of the entry/item to a number.
  7705. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7706. o By order of TODO keywords.
  7707. t By date/time, either the first active time stamp in the entry, or, if
  7708. none exist, by the first inactive one.
  7709. s By the scheduled date/time.
  7710. d By deadline date/time.
  7711. c By creation time, which is assumed to be the first inactive time stamp
  7712. at the beginning of a line.
  7713. p By priority according to the cookie.
  7714. r By the value of a property.
  7715. Capital letters will reverse the sort order.
  7716. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7717. called with point at the beginning of the record. It must return either
  7718. a string or a number that should serve as the sorting key for that record.
  7719. Comparing entries ignores case by default. However, with an optional argument
  7720. WITH-CASE, the sorting considers case as well.
  7721. Sorting is done against the visible part of the headlines, it ignores hidden
  7722. links."
  7723. (interactive "P")
  7724. (let ((case-func (if with-case 'identity 'downcase))
  7725. (cmstr
  7726. ;; The clock marker is lost when using `sort-subr', let's
  7727. ;; store the clocking string.
  7728. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7729. (save-excursion
  7730. (goto-char org-clock-marker)
  7731. (looking-back "^.*") (match-string-no-properties 0))))
  7732. start beg end stars re re2
  7733. txt what tmp)
  7734. ;; Find beginning and end of region to sort
  7735. (cond
  7736. ((org-region-active-p)
  7737. ;; we will sort the region
  7738. (setq end (region-end)
  7739. what "region")
  7740. (goto-char (region-beginning))
  7741. (if (not (org-at-heading-p)) (outline-next-heading))
  7742. (setq start (point)))
  7743. ((or (org-at-heading-p)
  7744. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7745. ;; we will sort the children of the current headline
  7746. (org-back-to-heading)
  7747. (setq start (point)
  7748. end (progn (org-end-of-subtree t t)
  7749. (or (bolp) (insert "\n"))
  7750. (org-back-over-empty-lines)
  7751. (point))
  7752. what "children")
  7753. (goto-char start)
  7754. (show-subtree)
  7755. (outline-next-heading))
  7756. (t
  7757. ;; we will sort the top-level entries in this file
  7758. (goto-char (point-min))
  7759. (or (org-at-heading-p) (outline-next-heading))
  7760. (setq start (point))
  7761. (goto-char (point-max))
  7762. (beginning-of-line 1)
  7763. (when (looking-at ".*?\\S-")
  7764. ;; File ends in a non-white line
  7765. (end-of-line 1)
  7766. (insert "\n"))
  7767. (setq end (point-max))
  7768. (setq what "top-level")
  7769. (goto-char start)
  7770. (show-all)))
  7771. (setq beg (point))
  7772. (if (>= beg end) (user-error "Nothing to sort"))
  7773. (looking-at "\\(\\*+\\)")
  7774. (setq stars (match-string 1)
  7775. re (concat "^" (regexp-quote stars) " +")
  7776. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7777. txt (buffer-substring beg end))
  7778. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7779. (if (and (not (equal stars "*")) (string-match re2 txt))
  7780. (user-error "Region to sort contains a level above the first entry"))
  7781. (unless sorting-type
  7782. (message
  7783. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7784. [t]ime [s]cheduled [d]eadline [c]reated
  7785. A/N/P/R/O/F/T/S/D/C means reversed:"
  7786. what)
  7787. (setq sorting-type (read-char-exclusive))
  7788. (unless getkey-func
  7789. (and (= (downcase sorting-type) ?f)
  7790. (setq getkey-func
  7791. (org-icompleting-read "Sort using function: "
  7792. obarray 'fboundp t nil nil))
  7793. (setq getkey-func (intern getkey-func))))
  7794. (and (= (downcase sorting-type) ?r)
  7795. (not property)
  7796. (setq property
  7797. (org-icompleting-read "Property: "
  7798. (mapcar 'list (org-buffer-property-keys t))
  7799. nil t))))
  7800. (message "Sorting entries...")
  7801. (save-restriction
  7802. (narrow-to-region start end)
  7803. (let ((dcst (downcase sorting-type))
  7804. (case-fold-search nil)
  7805. (now (current-time)))
  7806. (sort-subr
  7807. (/= dcst sorting-type)
  7808. ;; This function moves to the beginning character of the "record" to
  7809. ;; be sorted.
  7810. (lambda nil
  7811. (if (re-search-forward re nil t)
  7812. (goto-char (match-beginning 0))
  7813. (goto-char (point-max))))
  7814. ;; This function moves to the last character of the "record" being
  7815. ;; sorted.
  7816. (lambda nil
  7817. (save-match-data
  7818. (condition-case nil
  7819. (outline-forward-same-level 1)
  7820. (error
  7821. (goto-char (point-max))))))
  7822. ;; This function returns the value that gets sorted against.
  7823. (lambda nil
  7824. (cond
  7825. ((= dcst ?n)
  7826. (if (looking-at org-complex-heading-regexp)
  7827. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7828. nil))
  7829. ((= dcst ?a)
  7830. (if (looking-at org-complex-heading-regexp)
  7831. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7832. nil))
  7833. ((= dcst ?t)
  7834. (let ((end (save-excursion (outline-next-heading) (point))))
  7835. (if (or (re-search-forward org-ts-regexp end t)
  7836. (re-search-forward org-ts-regexp-both end t))
  7837. (org-time-string-to-seconds (match-string 0))
  7838. (org-float-time now))))
  7839. ((= dcst ?c)
  7840. (let ((end (save-excursion (outline-next-heading) (point))))
  7841. (if (re-search-forward
  7842. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7843. end t)
  7844. (org-time-string-to-seconds (match-string 0))
  7845. (org-float-time now))))
  7846. ((= dcst ?s)
  7847. (let ((end (save-excursion (outline-next-heading) (point))))
  7848. (if (re-search-forward org-scheduled-time-regexp end t)
  7849. (org-time-string-to-seconds (match-string 1))
  7850. (org-float-time now))))
  7851. ((= dcst ?d)
  7852. (let ((end (save-excursion (outline-next-heading) (point))))
  7853. (if (re-search-forward org-deadline-time-regexp end t)
  7854. (org-time-string-to-seconds (match-string 1))
  7855. (org-float-time now))))
  7856. ((= dcst ?p)
  7857. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7858. (string-to-char (match-string 2))
  7859. org-default-priority))
  7860. ((= dcst ?r)
  7861. (or (org-entry-get nil property) ""))
  7862. ((= dcst ?o)
  7863. (if (looking-at org-complex-heading-regexp)
  7864. (- 9999 (length (member (match-string 2)
  7865. org-todo-keywords-1)))))
  7866. ((= dcst ?f)
  7867. (if getkey-func
  7868. (progn
  7869. (setq tmp (funcall getkey-func))
  7870. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7871. tmp)
  7872. (error "Invalid key function `%s'" getkey-func)))
  7873. (t (error "Invalid sorting type `%c'" sorting-type))))
  7874. nil
  7875. (cond
  7876. ((= dcst ?a) 'string<)
  7877. ((= dcst ?f) compare-func)
  7878. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7879. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7880. ;; Reset the clock marker if needed
  7881. (when cmstr
  7882. (save-excursion
  7883. (goto-char start)
  7884. (search-forward cmstr nil t)
  7885. (move-marker org-clock-marker (point))))
  7886. (message "Sorting entries...done")))
  7887. (defun org-do-sort (table what &optional with-case sorting-type)
  7888. "Sort TABLE of WHAT according to SORTING-TYPE.
  7889. The user will be prompted for the SORTING-TYPE if the call to this
  7890. function does not specify it. WHAT is only for the prompt, to indicate
  7891. what is being sorted. The sorting key will be extracted from
  7892. the car of the elements of the table.
  7893. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7894. (unless sorting-type
  7895. (message
  7896. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7897. what)
  7898. (setq sorting-type (read-char-exclusive)))
  7899. (let ((dcst (downcase sorting-type))
  7900. extractfun comparefun)
  7901. ;; Define the appropriate functions
  7902. (cond
  7903. ((= dcst ?n)
  7904. (setq extractfun 'string-to-number
  7905. comparefun (if (= dcst sorting-type) '< '>)))
  7906. ((= dcst ?a)
  7907. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7908. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7909. comparefun (if (= dcst sorting-type)
  7910. 'string<
  7911. (lambda (a b) (and (not (string< a b))
  7912. (not (string= a b)))))))
  7913. ((= dcst ?t)
  7914. (setq extractfun
  7915. (lambda (x)
  7916. (if (or (string-match org-ts-regexp x)
  7917. (string-match org-ts-regexp-both x))
  7918. (org-float-time
  7919. (org-time-string-to-time (match-string 0 x)))
  7920. 0))
  7921. comparefun (if (= dcst sorting-type) '< '>)))
  7922. (t (error "Invalid sorting type `%c'" sorting-type)))
  7923. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7924. table)
  7925. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7926. ;;; The orgstruct minor mode
  7927. ;; Define a minor mode which can be used in other modes in order to
  7928. ;; integrate the org-mode structure editing commands.
  7929. ;; This is really a hack, because the org-mode structure commands use
  7930. ;; keys which normally belong to the major mode. Here is how it
  7931. ;; works: The minor mode defines all the keys necessary to operate the
  7932. ;; structure commands, but wraps the commands into a function which
  7933. ;; tests if the cursor is currently at a headline or a plain list
  7934. ;; item. If that is the case, the structure command is used,
  7935. ;; temporarily setting many Org-mode variables like regular
  7936. ;; expressions for filling etc. However, when any of those keys is
  7937. ;; used at a different location, function uses `key-binding' to look
  7938. ;; up if the key has an associated command in another currently active
  7939. ;; keymap (minor modes, major mode, global), and executes that
  7940. ;; command. There might be problems if any of the keys is otherwise
  7941. ;; used as a prefix key.
  7942. (defcustom orgstruct-heading-prefix-regexp nil
  7943. "Regexp that matches the custom prefix of Org headlines in
  7944. orgstruct(++)-mode."
  7945. :group 'org
  7946. :version "24.4"
  7947. :package-version '(Org . "8.0")
  7948. :type 'string)
  7949. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7950. (defcustom orgstruct-setup-hook nil
  7951. "Hook run after orgstruct-mode-map is filled."
  7952. :group 'org
  7953. :version "24.4"
  7954. :package-version '(Org . "8.0")
  7955. :type 'hook)
  7956. (defvar orgstruct-initialized nil)
  7957. (defvar org-local-vars nil
  7958. "List of local variables, for use by `orgstruct-mode'.")
  7959. ;;;###autoload
  7960. (define-minor-mode orgstruct-mode
  7961. "Toggle the minor mode `orgstruct-mode'.
  7962. This mode is for using Org-mode structure commands in other
  7963. modes. The following keys behave as if Org-mode were active, if
  7964. the cursor is on a headline, or on a plain list item (both as
  7965. defined by Org-mode)."
  7966. nil " OrgStruct" (make-sparse-keymap)
  7967. (funcall (if orgstruct-mode
  7968. 'add-to-invisibility-spec
  7969. 'remove-from-invisibility-spec)
  7970. '(outline . t))
  7971. (when orgstruct-mode
  7972. (org-load-modules-maybe)
  7973. (unless orgstruct-initialized
  7974. (orgstruct-setup)
  7975. (setq orgstruct-initialized t))))
  7976. ;;;###autoload
  7977. (defun turn-on-orgstruct ()
  7978. "Unconditionally turn on `orgstruct-mode'."
  7979. (orgstruct-mode 1))
  7980. (defvar org-fb-vars nil)
  7981. (make-variable-buffer-local 'org-fb-vars)
  7982. (defun orgstruct++-mode (&optional arg)
  7983. "Toggle `orgstruct-mode', the enhanced version of it.
  7984. In addition to setting orgstruct-mode, this also exports all
  7985. indentation and autofilling variables from org-mode into the
  7986. buffer. It will also recognize item context in multiline items."
  7987. (interactive "P")
  7988. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7989. (if (< arg 1)
  7990. (progn (orgstruct-mode -1)
  7991. (mapc (lambda(v)
  7992. (org-set-local (car v)
  7993. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7994. org-fb-vars))
  7995. (orgstruct-mode 1)
  7996. (setq org-fb-vars nil)
  7997. (let (var val)
  7998. (mapc
  7999. (lambda (x)
  8000. (when (string-match
  8001. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8002. (symbol-name (car x)))
  8003. (setq var (car x) val (nth 1 x))
  8004. (push (list var `(quote ,(eval var))) org-fb-vars)
  8005. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8006. org-local-vars)
  8007. (org-set-local 'orgstruct-is-++ t))))
  8008. (defvar orgstruct-is-++ nil
  8009. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8010. (make-variable-buffer-local 'orgstruct-is-++)
  8011. ;;;###autoload
  8012. (defun turn-on-orgstruct++ ()
  8013. "Unconditionally turn on `orgstruct++-mode'."
  8014. (orgstruct++-mode 1))
  8015. (defun orgstruct-error ()
  8016. "Error when there is no default binding for a structure key."
  8017. (interactive)
  8018. (funcall (if (fboundp 'user-error)
  8019. 'user-error
  8020. 'error)
  8021. "This key has no function outside structure elements"))
  8022. (defun orgstruct-setup ()
  8023. "Setup orgstruct keymap."
  8024. (dolist (cell '((org-demote . t)
  8025. (org-metaleft . t)
  8026. (org-metaright . t)
  8027. (org-promote . t)
  8028. (org-shiftmetaleft . t)
  8029. (org-shiftmetaright . t)
  8030. org-backward-element
  8031. org-backward-heading-same-level
  8032. org-ctrl-c-ret
  8033. org-ctrl-c-minus
  8034. org-ctrl-c-star
  8035. org-cycle
  8036. org-forward-heading-same-level
  8037. org-insert-heading
  8038. org-insert-heading-respect-content
  8039. org-kill-note-or-show-branches
  8040. org-mark-subtree
  8041. org-meta-return
  8042. org-metadown
  8043. org-metaup
  8044. org-narrow-to-subtree
  8045. org-promote-subtree
  8046. org-reveal
  8047. org-shiftdown
  8048. org-shiftleft
  8049. org-shiftmetadown
  8050. org-shiftmetaup
  8051. org-shiftright
  8052. org-shifttab
  8053. org-shifttab
  8054. org-shiftup
  8055. org-show-subtree
  8056. org-sort
  8057. org-up-element
  8058. outline-demote
  8059. outline-next-visible-heading
  8060. outline-previous-visible-heading
  8061. outline-promote
  8062. outline-up-heading
  8063. show-children))
  8064. (let ((f (or (car-safe cell) cell))
  8065. (disable-when-heading-prefix (cdr-safe cell)))
  8066. (when (fboundp f)
  8067. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8068. (where-is-internal f outline-mode-map)))
  8069. ;; TODO use local-function-key-map
  8070. (dolist (rep '(("<tab>" . "TAB")
  8071. ("<return>" . "RET")
  8072. ("<escape>" . "ESC")
  8073. ("<delete>" . "DEL")))
  8074. (setq binding (read-kbd-macro
  8075. (let ((case-fold-search))
  8076. (replace-regexp-in-string
  8077. (regexp-quote (cdr rep))
  8078. (car rep)
  8079. (key-description binding))))))
  8080. (let ((key (lookup-key orgstruct-mode-map binding)))
  8081. (when (or (not key) (numberp key))
  8082. (condition-case nil
  8083. (org-defkey orgstruct-mode-map
  8084. binding
  8085. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8086. (error nil))))))))
  8087. (run-hooks 'orgstruct-setup-hook))
  8088. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8089. "Create a function for binding in the structure minor mode.
  8090. FUN is the command to call inside a table. KEY is the key that
  8091. should be checked in for a command to execute outside of tables.
  8092. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8093. if `orgstruct-heading-prefix-regexp' is non-nil."
  8094. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8095. (let ((nname name)
  8096. (i 0))
  8097. (while (fboundp (intern nname))
  8098. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8099. (setq name (intern nname)))
  8100. (eval
  8101. (let ((bindings '((org-heading-regexp
  8102. (concat "^"
  8103. orgstruct-heading-prefix-regexp
  8104. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8105. (org-outline-regexp
  8106. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8107. (org-outline-regexp-bol
  8108. (concat "^" org-outline-regexp))
  8109. (outline-regexp org-outline-regexp)
  8110. (outline-heading-end-regexp "\n")
  8111. (outline-level 'org-outline-level)
  8112. (outline-heading-alist))))
  8113. `(defun ,name (arg)
  8114. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8115. "Outside of structure, run the binding of `"
  8116. (key-description key) "'."
  8117. (when disable-when-heading-prefix
  8118. (concat
  8119. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8120. "back to the default binding due to limitations of Org's implementation of\n"
  8121. "`" (symbol-name fun) "'.")))
  8122. (interactive "p")
  8123. (let* ((disable
  8124. ,(when disable-when-heading-prefix
  8125. '(and orgstruct-heading-prefix-regexp
  8126. (not (string= orgstruct-heading-prefix-regexp "")))))
  8127. (fallback
  8128. (or disable
  8129. (not
  8130. (let* ,bindings
  8131. (org-context-p 'headline 'item
  8132. ,(when (memq fun '(org-insert-heading))
  8133. '(when orgstruct-is-++
  8134. 'item-body))))))))
  8135. (if fallback
  8136. (let* ((orgstruct-mode)
  8137. (binding
  8138. (loop with key = ,key
  8139. for rep in
  8140. '(nil
  8141. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8142. ("<\\([^>]*\\)return>" . "\\1RET")
  8143. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8144. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8145. do
  8146. (when rep
  8147. (setq key (read-kbd-macro
  8148. (let ((case-fold-search))
  8149. (replace-regexp-in-string
  8150. (car rep)
  8151. (cdr rep)
  8152. (key-description key))))))
  8153. thereis (key-binding key))))
  8154. (if (keymapp binding)
  8155. (set-temporary-overlay-map binding)
  8156. (let ((func (or binding
  8157. (unless disable
  8158. 'orgstruct-error))))
  8159. (when func
  8160. (call-interactively func)))))
  8161. (org-run-like-in-org-mode
  8162. (lambda ()
  8163. (interactive)
  8164. (let* ,bindings
  8165. (call-interactively ',fun)))))))))
  8166. name))
  8167. (defun org-contextualize-keys (alist contexts)
  8168. "Return valid elements in ALIST depending on CONTEXTS.
  8169. `org-agenda-custom-commands' or `org-capture-templates' are the
  8170. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8171. or `org-capture-templates-contexts' are the associated contexts
  8172. definitions."
  8173. (let ((contexts
  8174. ;; normalize contexts
  8175. (mapcar
  8176. (lambda(c) (cond ((listp (cadr c))
  8177. (list (car c) (car c) (cadr c)))
  8178. ((string= "" (cadr c))
  8179. (list (car c) (car c) (caddr c)))
  8180. (t c))) contexts))
  8181. (a alist) c r s)
  8182. ;; loop over all commands or templates
  8183. (while (setq c (pop a))
  8184. (let (vrules repl)
  8185. (cond
  8186. ((not (assoc (car c) contexts))
  8187. (push c r))
  8188. ((and (assoc (car c) contexts)
  8189. (setq vrules (org-contextualize-validate-key
  8190. (car c) contexts)))
  8191. (mapc (lambda (vr)
  8192. (when (not (equal (car vr) (cadr vr)))
  8193. (setq repl vr))) vrules)
  8194. (if (not repl) (push c r)
  8195. (push (cadr repl) s)
  8196. (push
  8197. (cons (car c)
  8198. (cdr (or (assoc (cadr repl) alist)
  8199. (error "Undefined key `%s' as contextual replacement for `%s'"
  8200. (cadr repl) (car c)))))
  8201. r))))))
  8202. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8203. (delq
  8204. nil
  8205. (delete-dups
  8206. (mapcar (lambda (x)
  8207. (let ((tpl (car x)))
  8208. (when (not (delq
  8209. nil
  8210. (mapcar (lambda(y)
  8211. (equal y tpl)) s))) x)))
  8212. (reverse r))))))
  8213. (defun org-contextualize-validate-key (key contexts)
  8214. "Check CONTEXTS for agenda or capture KEY."
  8215. (let (r rr res)
  8216. (while (setq r (pop contexts))
  8217. (mapc
  8218. (lambda (rr)
  8219. (when
  8220. (and (equal key (car r))
  8221. (if (functionp rr) (funcall rr)
  8222. (or (and (eq (car rr) 'in-file)
  8223. (buffer-file-name)
  8224. (string-match (cdr rr) (buffer-file-name)))
  8225. (and (eq (car rr) 'in-mode)
  8226. (string-match (cdr rr) (symbol-name major-mode)))
  8227. (and (eq (car rr) 'in-buffer)
  8228. (string-match (cdr rr) (buffer-name)))
  8229. (when (and (eq (car rr) 'not-in-file)
  8230. (buffer-file-name))
  8231. (not (string-match (cdr rr) (buffer-file-name))))
  8232. (when (eq (car rr) 'not-in-mode)
  8233. (not (string-match (cdr rr) (symbol-name major-mode))))
  8234. (when (eq (car rr) 'not-in-buffer)
  8235. (not (string-match (cdr rr) (buffer-name)))))))
  8236. (push r res)))
  8237. (car (last r))))
  8238. (delete-dups (delq nil res))))
  8239. (defun org-context-p (&rest contexts)
  8240. "Check if local context is any of CONTEXTS.
  8241. Possible values in the list of contexts are `table', `headline', and `item'."
  8242. (let ((pos (point)))
  8243. (goto-char (point-at-bol))
  8244. (prog1 (or (and (memq 'table contexts)
  8245. (looking-at "[ \t]*|"))
  8246. (and (memq 'headline contexts)
  8247. (looking-at org-outline-regexp))
  8248. (and (memq 'item contexts)
  8249. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8250. (and (memq 'item-body contexts)
  8251. (org-in-item-p)))
  8252. (goto-char pos))))
  8253. (defun org-get-local-variables ()
  8254. "Return a list of all local variables in an Org mode buffer."
  8255. (let (varlist)
  8256. (with-current-buffer (get-buffer-create "*Org tmp*")
  8257. (erase-buffer)
  8258. (org-mode)
  8259. (setq varlist (buffer-local-variables)))
  8260. (kill-buffer "*Org tmp*")
  8261. (delq nil
  8262. (mapcar
  8263. (lambda (x)
  8264. (setq x
  8265. (if (symbolp x)
  8266. (list x)
  8267. (list (car x) (cdr x))))
  8268. (if (and (not (get (car x) 'org-state))
  8269. (string-match
  8270. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8271. (symbol-name (car x))))
  8272. x nil))
  8273. varlist))))
  8274. (defun org-clone-local-variables (from-buffer &optional regexp)
  8275. "Clone local variables from FROM-BUFFER.
  8276. Optional argument REGEXP selects variables to clone."
  8277. (mapc
  8278. (lambda (pair)
  8279. (and (symbolp (car pair))
  8280. (or (null regexp)
  8281. (string-match regexp (symbol-name (car pair))))
  8282. (set (make-local-variable (car pair))
  8283. (cdr pair))))
  8284. (buffer-local-variables from-buffer)))
  8285. ;;;###autoload
  8286. (defun org-run-like-in-org-mode (cmd)
  8287. "Run a command, pretending that the current buffer is in Org-mode.
  8288. This will temporarily bind local variables that are typically bound in
  8289. Org-mode to the values they have in Org-mode, and then interactively
  8290. call CMD."
  8291. (org-load-modules-maybe)
  8292. (unless org-local-vars
  8293. (setq org-local-vars (org-get-local-variables)))
  8294. (let (binds)
  8295. (dolist (var org-local-vars)
  8296. (when (or (not (boundp (car var)))
  8297. (eq (symbol-value (car var))
  8298. (default-value (car var))))
  8299. (push (list (car var) `(quote ,(cadr var))) binds)))
  8300. (eval `(let ,binds
  8301. (call-interactively (quote ,cmd))))))
  8302. ;;;; Archiving
  8303. (defun org-get-category (&optional pos force-refresh)
  8304. "Get the category applying to position POS."
  8305. (save-match-data
  8306. (if force-refresh (org-refresh-category-properties))
  8307. (let ((pos (or pos (point))))
  8308. (or (get-text-property pos 'org-category)
  8309. (progn (org-refresh-category-properties)
  8310. (get-text-property pos 'org-category))))))
  8311. (defun org-refresh-category-properties ()
  8312. "Refresh category text properties in the buffer."
  8313. (let ((case-fold-search t)
  8314. (inhibit-read-only t)
  8315. (def-cat (cond
  8316. ((null org-category)
  8317. (if buffer-file-name
  8318. (file-name-sans-extension
  8319. (file-name-nondirectory buffer-file-name))
  8320. "???"))
  8321. ((symbolp org-category) (symbol-name org-category))
  8322. (t org-category)))
  8323. beg end cat pos optionp)
  8324. (org-with-silent-modifications
  8325. (save-excursion
  8326. (save-restriction
  8327. (widen)
  8328. (goto-char (point-min))
  8329. (put-text-property (point) (point-max) 'org-category def-cat)
  8330. (while (re-search-forward
  8331. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8332. (setq pos (match-end 0)
  8333. optionp (equal (char-after (match-beginning 0)) ?#)
  8334. cat (org-trim (match-string 2)))
  8335. (if optionp
  8336. (setq beg (point-at-bol) end (point-max))
  8337. (org-back-to-heading t)
  8338. (setq beg (point) end (org-end-of-subtree t t)))
  8339. (put-text-property beg end 'org-category cat)
  8340. (put-text-property beg end 'org-category-position beg)
  8341. (goto-char pos)))))))
  8342. (defun org-refresh-properties (dprop tprop)
  8343. "Refresh buffer text properties.
  8344. DPROP is the drawer property and TPROP is the corresponding text
  8345. property to set."
  8346. (let ((case-fold-search t)
  8347. (inhibit-read-only t) p)
  8348. (org-with-silent-modifications
  8349. (save-excursion
  8350. (save-restriction
  8351. (widen)
  8352. (goto-char (point-min))
  8353. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8354. (setq p (org-match-string-no-properties 1))
  8355. (save-excursion
  8356. (org-back-to-heading t)
  8357. (put-text-property
  8358. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8359. ;;;; Link Stuff
  8360. ;;; Link abbreviations
  8361. (defun org-link-expand-abbrev (link)
  8362. "Apply replacements as defined in `org-link-abbrev-alist'."
  8363. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8364. (let* ((key (match-string 1 link))
  8365. (as (or (assoc key org-link-abbrev-alist-local)
  8366. (assoc key org-link-abbrev-alist)))
  8367. (tag (and (match-end 2) (match-string 3 link)))
  8368. rpl)
  8369. (if (not as)
  8370. link
  8371. (setq rpl (cdr as))
  8372. (cond
  8373. ((symbolp rpl) (funcall rpl tag))
  8374. ((string-match "%(\\([^)]+\\))" rpl)
  8375. (replace-match
  8376. (save-match-data
  8377. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8378. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8379. ((string-match "%h" rpl)
  8380. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8381. (t (concat rpl tag)))))
  8382. link))
  8383. ;;; Storing and inserting links
  8384. (defvar org-insert-link-history nil
  8385. "Minibuffer history for links inserted with `org-insert-link'.")
  8386. (defvar org-stored-links nil
  8387. "Contains the links stored with `org-store-link'.")
  8388. (defvar org-store-link-plist nil
  8389. "Plist with info about the most recently link created with `org-store-link'.")
  8390. (defvar org-link-protocols nil
  8391. "Link protocols added to Org-mode using `org-add-link-type'.")
  8392. (defvar org-store-link-functions nil
  8393. "List of functions that are called to create and store a link.
  8394. Each function will be called in turn until one returns a non-nil
  8395. value. Each function should check if it is responsible for creating
  8396. this link (for example by looking at the major mode).
  8397. If not, it must exit and return nil.
  8398. If yes, it should return a non-nil value after a calling
  8399. `org-store-link-props' with a list of properties and values.
  8400. Special properties are:
  8401. :type The link prefix, like \"http\". This must be given.
  8402. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8403. This is obligatory as well.
  8404. :description Optional default description for the second pair
  8405. of brackets in an Org-mode link. The user can still change
  8406. this when inserting this link into an Org-mode buffer.
  8407. In addition to these, any additional properties can be specified
  8408. and then used in capture templates.")
  8409. (defun org-add-link-type (type &optional follow export)
  8410. "Add TYPE to the list of `org-link-types'.
  8411. Re-compute all regular expressions depending on `org-link-types'
  8412. FOLLOW and EXPORT are two functions.
  8413. FOLLOW should take the link path as the single argument and do whatever
  8414. is necessary to follow the link, for example find a file or display
  8415. a mail message.
  8416. EXPORT should format the link path for export to one of the export formats.
  8417. It should be a function accepting three arguments:
  8418. path the path of the link, the text after the prefix (like \"http:\")
  8419. desc the description of the link, if any, or a description added by
  8420. org-export-normalize-links if there is none
  8421. format the export format, a symbol like `html' or `latex' or `ascii'..
  8422. The function may use the FORMAT information to return different values
  8423. depending on the format. The return value will be put literally into
  8424. the exported file. If the return value is nil, this means Org should
  8425. do what it normally does with links which do not have EXPORT defined.
  8426. Org-mode has a built-in default for exporting links. If you are happy with
  8427. this default, there is no need to define an export function for the link
  8428. type. For a simple example of an export function, see `org-bbdb.el'."
  8429. (add-to-list 'org-link-types type t)
  8430. (org-make-link-regexps)
  8431. (if (assoc type org-link-protocols)
  8432. (setcdr (assoc type org-link-protocols) (list follow export))
  8433. (push (list type follow export) org-link-protocols)))
  8434. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8435. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8436. ;;;###autoload
  8437. (defun org-store-link (arg)
  8438. "\\<org-mode-map>Store an org-link to the current location.
  8439. This link is added to `org-stored-links' and can later be inserted
  8440. into an org-buffer with \\[org-insert-link].
  8441. For some link types, a prefix arg is interpreted.
  8442. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8443. For file links, arg negates `org-context-in-file-links'.
  8444. A double prefix arg force skipping storing functions that are not
  8445. part of Org's core.
  8446. A triple prefix arg force storing a link for each line in the
  8447. active region."
  8448. (interactive "P")
  8449. (org-load-modules-maybe)
  8450. (if (and (equal arg '(64)) (org-region-active-p))
  8451. (save-excursion
  8452. (let ((end (region-end)))
  8453. (goto-char (region-beginning))
  8454. (set-mark (point))
  8455. (while (< (point-at-eol) end)
  8456. (move-end-of-line 1) (activate-mark)
  8457. (let (current-prefix-arg)
  8458. (call-interactively 'org-store-link))
  8459. (move-beginning-of-line 2)
  8460. (set-mark (point)))))
  8461. (org-with-limited-levels
  8462. (setq org-store-link-plist nil)
  8463. (let (link cpltxt desc description search
  8464. txt custom-id agenda-link sfuns sfunsn)
  8465. (cond
  8466. ;; Store a link using an external link type
  8467. ((and (not (equal arg '(16)))
  8468. (setq sfuns
  8469. (delq
  8470. nil (mapcar (lambda (f)
  8471. (let (fs) (if (funcall f) (push f fs))))
  8472. org-store-link-functions))
  8473. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8474. (or (and (cdr sfuns)
  8475. (funcall (intern
  8476. (completing-read
  8477. "Which function for creating the link? "
  8478. sfunsn t (car sfunsn)))))
  8479. (funcall (caar sfuns)))
  8480. (setq link (plist-get org-store-link-plist :link)
  8481. desc (or (plist-get org-store-link-plist
  8482. :description) link))))
  8483. ;; Store a link from a source code buffer
  8484. ((org-src-edit-buffer-p)
  8485. (let (label gc)
  8486. (while (or (not label)
  8487. (save-excursion
  8488. (save-restriction
  8489. (widen)
  8490. (goto-char (point-min))
  8491. (re-search-forward
  8492. (regexp-quote (format org-coderef-label-format label))
  8493. nil t))))
  8494. (when label (message "Label exists already") (sit-for 2))
  8495. (setq label (read-string "Code line label: " label)))
  8496. (end-of-line 1)
  8497. (setq link (format org-coderef-label-format label))
  8498. (setq gc (- 79 (length link)))
  8499. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8500. (insert link)
  8501. (setq link (concat "(" label ")") desc nil)))
  8502. ;; We are in the agenda, link to referenced location
  8503. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8504. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8505. (get-text-property (point) 'org-marker))))
  8506. (when m
  8507. (org-with-point-at m
  8508. (setq agenda-link
  8509. (if (org-called-interactively-p 'any)
  8510. (call-interactively 'org-store-link)
  8511. (org-store-link nil)))))))
  8512. ((eq major-mode 'calendar-mode)
  8513. (let ((cd (calendar-cursor-to-date)))
  8514. (setq link
  8515. (format-time-string
  8516. (car org-time-stamp-formats)
  8517. (apply 'encode-time
  8518. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8519. nil nil nil))))
  8520. (org-store-link-props :type "calendar" :date cd)))
  8521. ((eq major-mode 'help-mode)
  8522. (setq link (concat "help:" (save-excursion
  8523. (goto-char (point-min))
  8524. (looking-at "^[^ ]+")
  8525. (match-string 0))))
  8526. (org-store-link-props :type "help"))
  8527. ((eq major-mode 'w3-mode)
  8528. (setq cpltxt (if (and (buffer-name)
  8529. (not (string-match "Untitled" (buffer-name))))
  8530. (buffer-name)
  8531. (url-view-url t))
  8532. link (url-view-url t))
  8533. (org-store-link-props :type "w3" :url (url-view-url t)))
  8534. ((eq major-mode 'image-mode)
  8535. (setq cpltxt (concat "file:"
  8536. (abbreviate-file-name buffer-file-name))
  8537. link cpltxt)
  8538. (org-store-link-props :type "image" :file buffer-file-name))
  8539. ;; In dired, store a link to the file of the current line
  8540. ((eq major-mode 'dired-mode)
  8541. (let ((file (dired-get-filename nil t)))
  8542. (setq file (if file
  8543. (abbreviate-file-name
  8544. (expand-file-name (dired-get-filename nil t)))
  8545. ;; otherwise, no file so use current directory.
  8546. default-directory))
  8547. (setq cpltxt (concat "file:" file)
  8548. link cpltxt)))
  8549. ((setq search (run-hook-with-args-until-success
  8550. 'org-create-file-search-functions))
  8551. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8552. "::" search))
  8553. (setq cpltxt (or description link)))
  8554. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8555. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8556. (cond
  8557. ;; Store a link using the target at point
  8558. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8559. (setq cpltxt
  8560. (concat "file:"
  8561. (abbreviate-file-name
  8562. (buffer-file-name (buffer-base-buffer)))
  8563. "::" (match-string 1))
  8564. link cpltxt))
  8565. ((and (featurep 'org-id)
  8566. (or (eq org-id-link-to-org-use-id t)
  8567. (and (org-called-interactively-p 'any)
  8568. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8569. (and (eq org-id-link-to-org-use-id
  8570. 'create-if-interactive-and-no-custom-id)
  8571. (not custom-id))))
  8572. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8573. ;; Store a link using the ID at point
  8574. (setq link (condition-case nil
  8575. (prog1 (org-id-store-link)
  8576. (setq desc (plist-get org-store-link-plist
  8577. :description)))
  8578. (error
  8579. ;; Probably before first headline, link only to file
  8580. (concat "file:"
  8581. (abbreviate-file-name
  8582. (buffer-file-name (buffer-base-buffer))))))))
  8583. (t
  8584. ;; Just link to current headline
  8585. (setq cpltxt (concat "file:"
  8586. (abbreviate-file-name
  8587. (buffer-file-name (buffer-base-buffer)))))
  8588. ;; Add a context search string
  8589. (when (org-xor org-context-in-file-links arg)
  8590. (let* ((ee (org-element-at-point))
  8591. (et (org-element-type ee))
  8592. (ev (plist-get (cadr ee) :value))
  8593. (ek (plist-get (cadr ee) :key))
  8594. (eok (and (stringp ek) (string-match "name" ek))))
  8595. (setq txt (cond
  8596. ((org-at-heading-p) nil)
  8597. ((and (eq et 'keyword) eok) ev)
  8598. ((org-region-active-p)
  8599. (buffer-substring (region-beginning) (region-end)))))
  8600. (when (or (null txt) (string-match "\\S-" txt))
  8601. (setq cpltxt
  8602. (concat cpltxt "::"
  8603. (condition-case nil
  8604. (org-make-org-heading-search-string txt)
  8605. (error "")))
  8606. desc (or (and (eq et 'keyword) eok ev)
  8607. (nth 4 (ignore-errors (org-heading-components)))
  8608. "NONE")))))
  8609. (if (string-match "::\\'" cpltxt)
  8610. (setq cpltxt (substring cpltxt 0 -2)))
  8611. (setq link cpltxt))))
  8612. ((buffer-file-name (buffer-base-buffer))
  8613. ;; Just link to this file here.
  8614. (setq cpltxt (concat "file:"
  8615. (abbreviate-file-name
  8616. (buffer-file-name (buffer-base-buffer)))))
  8617. ;; Add a context string.
  8618. (when (org-xor org-context-in-file-links arg)
  8619. (setq txt (if (org-region-active-p)
  8620. (buffer-substring (region-beginning) (region-end))
  8621. (buffer-substring (point-at-bol) (point-at-eol))))
  8622. ;; Only use search option if there is some text.
  8623. (when (string-match "\\S-" txt)
  8624. (setq cpltxt
  8625. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8626. desc "NONE")))
  8627. (setq link cpltxt))
  8628. ((org-called-interactively-p 'interactive)
  8629. (user-error "No method for storing a link from this buffer"))
  8630. (t (setq link nil)))
  8631. ;; We're done setting link and desc, clean up
  8632. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8633. (setq link (or link cpltxt)
  8634. desc (or desc cpltxt))
  8635. (cond ((equal desc "NONE") (setq desc nil))
  8636. ((string-match org-bracket-link-analytic-regexp desc)
  8637. (let ((d0 (match-string 3 desc))
  8638. (p0 (match-string 5 desc)))
  8639. (setq desc
  8640. (replace-regexp-in-string
  8641. org-bracket-link-regexp
  8642. (concat (or p0 d0)
  8643. (if (equal (length (match-string 0 desc))
  8644. (length desc)) "*" "")) desc)))))
  8645. ;; Return the link
  8646. (if (not (and (or (org-called-interactively-p 'any)
  8647. executing-kbd-macro) link))
  8648. (or agenda-link (and link (org-make-link-string link desc)))
  8649. (push (list link desc) org-stored-links)
  8650. (message "Stored: %s" (or desc link))
  8651. (when custom-id
  8652. (setq link (concat "file:" (abbreviate-file-name
  8653. (buffer-file-name)) "::#" custom-id))
  8654. (push (list link desc) org-stored-links)))))))
  8655. (defun org-store-link-props (&rest plist)
  8656. "Store link properties, extract names and addresses."
  8657. (let (x adr)
  8658. (when (setq x (plist-get plist :from))
  8659. (setq adr (mail-extract-address-components x))
  8660. (setq plist (plist-put plist :fromname (car adr)))
  8661. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8662. (when (setq x (plist-get plist :to))
  8663. (setq adr (mail-extract-address-components x))
  8664. (setq plist (plist-put plist :toname (car adr)))
  8665. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8666. (let ((from (plist-get plist :from))
  8667. (to (plist-get plist :to)))
  8668. (when (and from to org-from-is-user-regexp)
  8669. (setq plist
  8670. (plist-put plist :fromto
  8671. (if (string-match org-from-is-user-regexp from)
  8672. (concat "to %t")
  8673. (concat "from %f"))))))
  8674. (setq org-store-link-plist plist))
  8675. (defun org-add-link-props (&rest plist)
  8676. "Add these properties to the link property list."
  8677. (let (key value)
  8678. (while plist
  8679. (setq key (pop plist) value (pop plist))
  8680. (setq org-store-link-plist
  8681. (plist-put org-store-link-plist key value)))))
  8682. (defun org-email-link-description (&optional fmt)
  8683. "Return the description part of an email link.
  8684. This takes information from `org-store-link-plist' and formats it
  8685. according to FMT (default from `org-email-link-description-format')."
  8686. (setq fmt (or fmt org-email-link-description-format))
  8687. (let* ((p org-store-link-plist)
  8688. (to (plist-get p :toaddress))
  8689. (from (plist-get p :fromaddress))
  8690. (table
  8691. (list
  8692. (cons "%c" (plist-get p :fromto))
  8693. (cons "%F" (plist-get p :from))
  8694. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8695. (cons "%T" (plist-get p :to))
  8696. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8697. (cons "%s" (plist-get p :subject))
  8698. (cons "%d" (plist-get p :date))
  8699. (cons "%m" (plist-get p :message-id)))))
  8700. (when (string-match "%c" fmt)
  8701. ;; Check if the user wrote this message
  8702. (if (and org-from-is-user-regexp from to
  8703. (save-match-data (string-match org-from-is-user-regexp from)))
  8704. (setq fmt (replace-match "to %t" t t fmt))
  8705. (setq fmt (replace-match "from %f" t t fmt))))
  8706. (org-replace-escapes fmt table)))
  8707. (defun org-make-org-heading-search-string (&optional string)
  8708. "Make search string for the current headline or STRING."
  8709. (let ((s (or string
  8710. (and (derived-mode-p 'org-mode)
  8711. (save-excursion
  8712. (org-back-to-heading t)
  8713. (org-element-property :raw-value (org-element-at-point))))))
  8714. (lines org-context-in-file-links))
  8715. (or string (setq s (concat "*" s))) ; Add * for headlines
  8716. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8717. (when (and string (integerp lines) (> lines 0))
  8718. (let ((slines (org-split-string s "\n")))
  8719. (when (< lines (length slines))
  8720. (setq s (mapconcat
  8721. 'identity
  8722. (reverse (nthcdr (- (length slines) lines)
  8723. (reverse slines))) "\n")))))
  8724. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8725. (defun org-make-link-string (link &optional description)
  8726. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8727. (unless (string-match "\\S-" link)
  8728. (error "Empty link"))
  8729. (when (and description
  8730. (stringp description)
  8731. (not (string-match "\\S-" description)))
  8732. (setq description nil))
  8733. (when (stringp description)
  8734. ;; Remove brackets from the description, they are fatal.
  8735. (while (string-match "\\[" description)
  8736. (setq description (replace-match "{" t t description)))
  8737. (while (string-match "\\]" description)
  8738. (setq description (replace-match "}" t t description))))
  8739. (when (equal link description)
  8740. ;; No description needed, it is identical
  8741. (setq description nil))
  8742. (when (and (not description)
  8743. (not (string-match (org-image-file-name-regexp) link))
  8744. (not (equal link (org-link-escape link))))
  8745. (setq description (org-extract-attributes link)))
  8746. (setq link
  8747. (cond ((string-match (org-image-file-name-regexp) link) link)
  8748. ((string-match org-link-types-re link)
  8749. (concat (match-string 1 link)
  8750. (org-link-escape (substring link (match-end 1)))))
  8751. (t (org-link-escape link))))
  8752. (concat "[[" link "]"
  8753. (if description (concat "[" description "]") "")
  8754. "]"))
  8755. (defconst org-link-escape-chars
  8756. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8757. "List of characters that should be escaped in link.
  8758. This is the list that is used for internal purposes.")
  8759. (defconst org-link-escape-chars-browser
  8760. '(?\ )
  8761. "List of escapes for characters that are problematic in links.
  8762. This is the list that is used before handing over to the browser.")
  8763. (defun org-link-escape (text &optional table merge)
  8764. "Return percent escaped representation of TEXT.
  8765. TEXT is a string with the text to escape.
  8766. Optional argument TABLE is a list with characters that should be
  8767. escaped. When nil, `org-link-escape-chars' is used.
  8768. If optional argument MERGE is set, merge TABLE into
  8769. `org-link-escape-chars'."
  8770. (cond
  8771. ((and table merge)
  8772. (mapc (lambda (defchr)
  8773. (unless (member defchr table)
  8774. (setq table (cons defchr table)))) org-link-escape-chars))
  8775. ((null table)
  8776. (setq table org-link-escape-chars)))
  8777. (mapconcat
  8778. (lambda (char)
  8779. (if (or (member char table)
  8780. (and (or (< char 32) (= char 37) (> char 126))
  8781. org-url-hexify-p))
  8782. (mapconcat (lambda (sequence-element)
  8783. (format "%%%.2X" sequence-element))
  8784. (or (encode-coding-char char 'utf-8)
  8785. (error "Unable to percent escape character: %s"
  8786. (char-to-string char))) "")
  8787. (char-to-string char))) text ""))
  8788. (defun org-link-unescape (str)
  8789. "Unhex hexified Unicode strings as returned from the JavaScript function
  8790. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8791. (unless (and (null str) (string= "" str))
  8792. (let ((pos 0) (case-fold-search t) unhexed)
  8793. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8794. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8795. (setq str (replace-match unhexed t t str))
  8796. (setq pos (+ pos (length unhexed))))))
  8797. str)
  8798. (defun org-link-unescape-compound (hex)
  8799. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8800. Note: this function also decodes single byte encodings like
  8801. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8802. (save-match-data
  8803. (let* ((bytes (cdr (split-string hex "%")))
  8804. (ret "")
  8805. (eat 0)
  8806. (sum 0))
  8807. (while bytes
  8808. (let* ((val (string-to-number (pop bytes) 16))
  8809. (shift-xor
  8810. (if (= 0 eat)
  8811. (cond
  8812. ((>= val 252) (cons 6 252))
  8813. ((>= val 248) (cons 5 248))
  8814. ((>= val 240) (cons 4 240))
  8815. ((>= val 224) (cons 3 224))
  8816. ((>= val 192) (cons 2 192))
  8817. (t (cons 0 0)))
  8818. (cons 6 128))))
  8819. (if (>= val 192) (setq eat (car shift-xor)))
  8820. (setq val (logxor val (cdr shift-xor)))
  8821. (setq sum (+ (lsh sum (car shift-xor)) val))
  8822. (if (> eat 0) (setq eat (- eat 1)))
  8823. (cond
  8824. ((= 0 eat) ;multi byte
  8825. (setq ret (concat ret (org-char-to-string sum)))
  8826. (setq sum 0))
  8827. ((not bytes) ; single byte(s)
  8828. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8829. )) ;; end (while bytes
  8830. ret )))
  8831. (defun org-link-unescape-single-byte-sequence (hex)
  8832. "Unhexify hex-encoded single byte character sequences."
  8833. (mapconcat (lambda (byte)
  8834. (char-to-string (string-to-number byte 16)))
  8835. (cdr (split-string hex "%")) ""))
  8836. (defun org-xor (a b)
  8837. "Exclusive or."
  8838. (if a (not b) b))
  8839. (defun org-fixup-message-id-for-http (s)
  8840. "Replace special characters in a message id, so it can be used in an http query."
  8841. (when (string-match "%" s)
  8842. (setq s (mapconcat (lambda (c)
  8843. (if (eq c ?%)
  8844. "%25"
  8845. (char-to-string c)))
  8846. s "")))
  8847. (while (string-match "<" s)
  8848. (setq s (replace-match "%3C" t t s)))
  8849. (while (string-match ">" s)
  8850. (setq s (replace-match "%3E" t t s)))
  8851. (while (string-match "@" s)
  8852. (setq s (replace-match "%40" t t s)))
  8853. s)
  8854. (defun org-link-prettify (link)
  8855. "Return a human-readable representation of LINK.
  8856. The car of LINK must be a raw link the cdr of LINK must be either
  8857. a link description or nil."
  8858. (let ((desc (or (cadr link) "<no description>")))
  8859. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8860. "<" (car link) ">")))
  8861. ;;;###autoload
  8862. (defun org-insert-link-global ()
  8863. "Insert a link like Org-mode does.
  8864. This command can be called in any mode to insert a link in Org-mode syntax."
  8865. (interactive)
  8866. (org-load-modules-maybe)
  8867. (org-run-like-in-org-mode 'org-insert-link))
  8868. (defun org-insert-all-links (&optional keep)
  8869. "Insert all links in `org-stored-links'."
  8870. (interactive "P")
  8871. (let ((links (copy-sequence org-stored-links)) l)
  8872. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8873. (insert "- ")
  8874. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8875. (insert "\n"))))
  8876. (defun org-link-fontify-links-to-this-file ()
  8877. "Fontify links to the current file in `org-stored-links'."
  8878. (let ((f (buffer-file-name)) a b)
  8879. (setq a (mapcar (lambda(l)
  8880. (let ((ll (car l)))
  8881. (when (and (string-match "^file:\\(.+\\)::" ll)
  8882. (equal f (expand-file-name (match-string 1 ll))))
  8883. ll)))
  8884. org-stored-links))
  8885. (when (featurep 'org-id)
  8886. (setq b (mapcar (lambda(l)
  8887. (let ((ll (car l)))
  8888. (when (and (string-match "^id:\\(.+\\)$" ll)
  8889. (equal f (expand-file-name
  8890. (or (org-id-find-id-file
  8891. (match-string 1 ll)) ""))))
  8892. ll)))
  8893. org-stored-links)))
  8894. (mapcar (lambda(l)
  8895. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8896. (delq nil (append a b)))))
  8897. (defvar org-link-links-in-this-file nil)
  8898. (defun org-insert-link (&optional complete-file link-location default-description)
  8899. "Insert a link. At the prompt, enter the link.
  8900. Completion can be used to insert any of the link protocol prefixes like
  8901. http or ftp in use.
  8902. The history can be used to select a link previously stored with
  8903. `org-store-link'. When the empty string is entered (i.e. if you just
  8904. press RET at the prompt), the link defaults to the most recently
  8905. stored link. As SPC triggers completion in the minibuffer, you need to
  8906. use M-SPC or C-q SPC to force the insertion of a space character.
  8907. You will also be prompted for a description, and if one is given, it will
  8908. be displayed in the buffer instead of the link.
  8909. If there is already a link at point, this command will allow you to edit link
  8910. and description parts.
  8911. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8912. be selected using completion. The path to the file will be relative to the
  8913. current directory if the file is in the current directory or a subdirectory.
  8914. Otherwise, the link will be the absolute path as completed in the minibuffer
  8915. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8916. option `org-link-file-path-type'.
  8917. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8918. the current directory or below.
  8919. With three \\[universal-argument] prefixes, negate the meaning of
  8920. `org-keep-stored-link-after-insertion'.
  8921. If `org-make-link-description-function' is non-nil, this function will be
  8922. called with the link target, and the result will be the default
  8923. link description.
  8924. If the LINK-LOCATION parameter is non-nil, this value will be
  8925. used as the link location instead of reading one interactively.
  8926. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8927. be used as the default description."
  8928. (interactive "P")
  8929. (let* ((wcf (current-window-configuration))
  8930. (origbuf (current-buffer))
  8931. (region (if (org-region-active-p)
  8932. (buffer-substring (region-beginning) (region-end))))
  8933. (remove (and region (list (region-beginning) (region-end))))
  8934. (desc region)
  8935. tmphist ; byte-compile incorrectly complains about this
  8936. (link link-location)
  8937. (abbrevs org-link-abbrev-alist-local)
  8938. entry file all-prefixes auto-desc)
  8939. (cond
  8940. (link-location) ; specified by arg, just use it.
  8941. ((org-in-regexp org-bracket-link-regexp 1)
  8942. ;; We do have a link at point, and we are going to edit it.
  8943. (setq remove (list (match-beginning 0) (match-end 0)))
  8944. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8945. (setq link (read-string "Link: "
  8946. (org-link-unescape
  8947. (org-match-string-no-properties 1)))))
  8948. ((or (org-in-regexp org-angle-link-re)
  8949. (org-in-regexp org-plain-link-re))
  8950. ;; Convert to bracket link
  8951. (setq remove (list (match-beginning 0) (match-end 0))
  8952. link (read-string "Link: "
  8953. (org-remove-angle-brackets (match-string 0)))))
  8954. ((member complete-file '((4) (16)))
  8955. ;; Completing read for file names.
  8956. (setq link (org-file-complete-link complete-file)))
  8957. (t
  8958. ;; Read link, with completion for stored links.
  8959. (org-link-fontify-links-to-this-file)
  8960. (org-switch-to-buffer-other-window "*Org Links*")
  8961. (with-current-buffer "*Org Links*"
  8962. (erase-buffer)
  8963. (insert "Insert a link.
  8964. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8965. (when org-stored-links
  8966. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8967. (insert (mapconcat 'org-link-prettify
  8968. (reverse org-stored-links) "\n")))
  8969. (goto-char (point-min)))
  8970. (let ((cw (selected-window)))
  8971. (select-window (get-buffer-window "*Org Links*" 'visible))
  8972. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8973. (unless (pos-visible-in-window-p (point-max))
  8974. (org-fit-window-to-buffer))
  8975. (and (window-live-p cw) (select-window cw)))
  8976. ;; Fake a link history, containing the stored links.
  8977. (setq tmphist (append (mapcar 'car org-stored-links)
  8978. org-insert-link-history))
  8979. (setq all-prefixes (append (mapcar 'car abbrevs)
  8980. (mapcar 'car org-link-abbrev-alist)
  8981. org-link-types))
  8982. (unwind-protect
  8983. (progn
  8984. (setq link
  8985. (org-completing-read
  8986. "Link: "
  8987. (append
  8988. (mapcar (lambda (x) (concat x ":"))
  8989. all-prefixes)
  8990. (mapcar 'car org-stored-links))
  8991. nil nil nil
  8992. 'tmphist
  8993. (caar org-stored-links)))
  8994. (if (not (string-match "\\S-" link))
  8995. (user-error "No link selected"))
  8996. (mapc (lambda(l)
  8997. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8998. org-stored-links)
  8999. (if (or (member link all-prefixes)
  9000. (and (equal ":" (substring link -1))
  9001. (member (substring link 0 -1) all-prefixes)
  9002. (setq link (substring link 0 -1))))
  9003. (setq link (with-current-buffer origbuf
  9004. (org-link-try-special-completion link)))))
  9005. (set-window-configuration wcf)
  9006. (kill-buffer "*Org Links*"))
  9007. (setq entry (assoc link org-stored-links))
  9008. (or entry (push link org-insert-link-history))
  9009. (setq desc (or desc (nth 1 entry)))))
  9010. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9011. (not org-keep-stored-link-after-insertion))
  9012. (setq org-stored-links (delq (assoc link org-stored-links)
  9013. org-stored-links)))
  9014. (if (string-match org-plain-link-re link)
  9015. ;; URL-like link, normalize the use of angular brackets.
  9016. (setq link (org-remove-angle-brackets link)))
  9017. ;; Check if we are linking to the current file with a search
  9018. ;; option If yes, simplify the link by using only the search
  9019. ;; option.
  9020. (when (and buffer-file-name
  9021. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9022. (let* ((path (match-string 1 link))
  9023. (case-fold-search nil)
  9024. (search (match-string 2 link)))
  9025. (save-match-data
  9026. (if (equal (file-truename buffer-file-name) (file-truename path))
  9027. ;; We are linking to this same file, with a search option
  9028. (setq link search)))))
  9029. ;; Check if we can/should use a relative path. If yes, simplify the link
  9030. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9031. (let* ((type (match-string 1 link))
  9032. (path (match-string 2 link))
  9033. (origpath path)
  9034. (case-fold-search nil))
  9035. (cond
  9036. ((or (eq org-link-file-path-type 'absolute)
  9037. (equal complete-file '(16)))
  9038. (setq path (abbreviate-file-name (expand-file-name path))))
  9039. ((eq org-link-file-path-type 'noabbrev)
  9040. (setq path (expand-file-name path)))
  9041. ((eq org-link-file-path-type 'relative)
  9042. (setq path (file-relative-name path)))
  9043. (t
  9044. (save-match-data
  9045. (if (string-match (concat "^" (regexp-quote
  9046. (expand-file-name
  9047. (file-name-as-directory
  9048. default-directory))))
  9049. (expand-file-name path))
  9050. ;; We are linking a file with relative path name.
  9051. (setq path (substring (expand-file-name path)
  9052. (match-end 0)))
  9053. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9054. (setq link (concat type path))
  9055. (if (equal desc origpath)
  9056. (setq desc path))))
  9057. (if org-make-link-description-function
  9058. (setq desc
  9059. (or (condition-case nil
  9060. (funcall org-make-link-description-function link desc)
  9061. (error (progn (message "Can't get link description from `%s'"
  9062. (symbol-name org-make-link-description-function))
  9063. (sit-for 2) nil)))
  9064. (read-string "Description: " default-description)))
  9065. (if default-description (setq desc default-description)
  9066. (setq desc (or (and auto-desc desc)
  9067. (read-string "Description: " desc)))))
  9068. (unless (string-match "\\S-" desc) (setq desc nil))
  9069. (if remove (apply 'delete-region remove))
  9070. (insert (org-make-link-string link desc))))
  9071. (defun org-link-try-special-completion (type)
  9072. "If there is completion support for link type TYPE, offer it."
  9073. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9074. (if (functionp fun)
  9075. (funcall fun)
  9076. (read-string "Link (no completion support): " (concat type ":")))))
  9077. (defun org-file-complete-link (&optional arg)
  9078. "Create a file link using completion."
  9079. (let (file link)
  9080. (setq file (org-iread-file-name "File: "))
  9081. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9082. (pwd1 (file-name-as-directory (abbreviate-file-name
  9083. (expand-file-name ".")))))
  9084. (cond
  9085. ((equal arg '(16))
  9086. (setq link (concat
  9087. "file:"
  9088. (abbreviate-file-name (expand-file-name file)))))
  9089. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9090. (setq link (concat "file:" (match-string 1 file))))
  9091. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9092. (expand-file-name file))
  9093. (setq link (concat
  9094. "file:" (match-string 1 (expand-file-name file)))))
  9095. (t (setq link (concat "file:" file)))))
  9096. link))
  9097. (defun org-iread-file-name (&rest args)
  9098. "Read-file-name using `ido-mode' speedup if available.
  9099. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9100. See `read-file-name' for a description of parameters."
  9101. (org-without-partial-completion
  9102. (if (and org-completion-use-ido
  9103. (fboundp 'ido-read-file-name)
  9104. (boundp 'ido-mode) ido-mode
  9105. (listp (second args)))
  9106. (let ((ido-enter-matching-directory nil))
  9107. (apply 'ido-read-file-name args))
  9108. (apply 'read-file-name args))))
  9109. (defun org-completing-read (&rest args)
  9110. "Completing-read with SPACE being a normal character."
  9111. (let ((enable-recursive-minibuffers t)
  9112. (minibuffer-local-completion-map
  9113. (copy-keymap minibuffer-local-completion-map)))
  9114. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9115. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9116. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9117. (apply 'org-icompleting-read args)))
  9118. (defun org-completing-read-no-i (&rest args)
  9119. (let (org-completion-use-ido org-completion-use-iswitchb)
  9120. (apply 'org-completing-read args)))
  9121. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9122. "Use iswitch as a completing-read replacement to choose from choices.
  9123. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9124. from."
  9125. (let* ((iswitchb-use-virtual-buffers nil)
  9126. (iswitchb-make-buflist-hook
  9127. (lambda ()
  9128. (setq iswitchb-temp-buflist choices))))
  9129. (iswitchb-read-buffer prompt)))
  9130. (defun org-icompleting-read (&rest args)
  9131. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9132. (org-without-partial-completion
  9133. (if (and org-completion-use-ido
  9134. (fboundp 'ido-completing-read)
  9135. (boundp 'ido-mode) ido-mode
  9136. (listp (second args)))
  9137. (let ((ido-enter-matching-directory nil))
  9138. (apply 'ido-completing-read (concat (car args))
  9139. (if (consp (car (nth 1 args)))
  9140. (mapcar 'car (nth 1 args))
  9141. (nth 1 args))
  9142. (cddr args)))
  9143. (if (and org-completion-use-iswitchb
  9144. (boundp 'iswitchb-mode) iswitchb-mode
  9145. (listp (second args)))
  9146. (apply 'org-iswitchb-completing-read (concat (car args))
  9147. (if (consp (car (nth 1 args)))
  9148. (mapcar 'car (nth 1 args))
  9149. (nth 1 args))
  9150. (cddr args))
  9151. (apply 'completing-read args)))))
  9152. (defun org-extract-attributes (s)
  9153. "Extract the attributes cookie from a string and set as text property."
  9154. (let (a attr (start 0) key value)
  9155. (save-match-data
  9156. (when (string-match "{{\\([^}]+\\)}}$" s)
  9157. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9158. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9159. (setq key (match-string 1 a) value (match-string 2 a)
  9160. start (match-end 0)
  9161. attr (plist-put attr (intern key) value))))
  9162. (org-add-props s nil 'org-attr attr))
  9163. s))
  9164. ;;; Opening/following a link
  9165. (defvar org-link-search-failed nil)
  9166. (defvar org-open-link-functions nil
  9167. "Hook for functions finding a plain text link.
  9168. These functions must take a single argument, the link content.
  9169. They will be called for links that look like [[link text][description]]
  9170. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9171. like a filename (e.g. \"./blue.png\").
  9172. These functions will be called *before* Org attempts to resolve the
  9173. link by doing text searches in the current buffer - so if you want a
  9174. link \"[[target]]\" to still find \"<<target>>\", your function should
  9175. handle this as a special case.
  9176. When the function does handle the link, it must return a non-nil value.
  9177. If it decides that it is not responsible for this link, it must return
  9178. nil to indicate that that Org-mode can continue with other options
  9179. like exact and fuzzy text search.")
  9180. (defun org-next-link (&optional search-backward)
  9181. "Move forward to the next link.
  9182. If the link is in hidden text, expose it."
  9183. (interactive "P")
  9184. (when (and org-link-search-failed (eq this-command last-command))
  9185. (goto-char (point-min))
  9186. (message "Link search wrapped back to beginning of buffer"))
  9187. (setq org-link-search-failed nil)
  9188. (let* ((pos (point))
  9189. (ct (org-context))
  9190. (a (assoc :link ct))
  9191. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9192. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9193. ((looking-at org-any-link-re)
  9194. ;; Don't stay stuck at link without an org-link face
  9195. (forward-char (if search-backward -1 1))))
  9196. (if (funcall srch-fun org-any-link-re nil t)
  9197. (progn
  9198. (goto-char (match-beginning 0))
  9199. (if (outline-invisible-p) (org-show-context)))
  9200. (goto-char pos)
  9201. (setq org-link-search-failed t)
  9202. (message "No further link found"))))
  9203. (defun org-previous-link ()
  9204. "Move backward to the previous link.
  9205. If the link is in hidden text, expose it."
  9206. (interactive)
  9207. (funcall 'org-next-link t))
  9208. (defun org-translate-link (s)
  9209. "Translate a link string if a translation function has been defined."
  9210. (if (and org-link-translation-function
  9211. (fboundp org-link-translation-function)
  9212. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9213. (progn
  9214. (setq s (funcall org-link-translation-function
  9215. (match-string 1 s) (match-string 2 s)))
  9216. (concat (car s) ":" (cdr s)))
  9217. s))
  9218. (defun org-translate-link-from-planner (type path)
  9219. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9220. This is still an experimental function, your mileage may vary."
  9221. (cond
  9222. ((member type '("http" "https" "news" "ftp"))
  9223. ;; standard Internet links are the same.
  9224. nil)
  9225. ((and (equal type "irc") (string-match "^//" path))
  9226. ;; Planner has two / at the beginning of an irc link, we have 1.
  9227. ;; We should have zero, actually....
  9228. (setq path (substring path 1)))
  9229. ((and (equal type "lisp") (string-match "^/" path))
  9230. ;; Planner has a slash, we do not.
  9231. (setq type "elisp" path (substring path 1)))
  9232. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9233. ;; A typical message link. Planner has the id after the final slash,
  9234. ;; we separate it with a hash mark
  9235. (setq path (concat (match-string 1 path) "#"
  9236. (org-remove-angle-brackets (match-string 2 path))))))
  9237. (cons type path))
  9238. (defun org-find-file-at-mouse (ev)
  9239. "Open file link or URL at mouse."
  9240. (interactive "e")
  9241. (mouse-set-point ev)
  9242. (org-open-at-point 'in-emacs))
  9243. (defun org-open-at-mouse (ev)
  9244. "Open file link or URL at mouse.
  9245. See the docstring of `org-open-file' for details."
  9246. (interactive "e")
  9247. (mouse-set-point ev)
  9248. (if (eq major-mode 'org-agenda-mode)
  9249. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9250. (org-open-at-point))
  9251. (defvar org-window-config-before-follow-link nil
  9252. "The window configuration before following a link.
  9253. This is saved in case the need arises to restore it.")
  9254. (defvar org-open-link-marker (make-marker)
  9255. "Marker pointing to the location where `org-open-at-point; was called.")
  9256. ;;;###autoload
  9257. (defun org-open-at-point-global ()
  9258. "Follow a link like Org-mode does.
  9259. This command can be called in any mode to follow a link that has
  9260. Org-mode syntax."
  9261. (interactive)
  9262. (org-run-like-in-org-mode 'org-open-at-point))
  9263. ;;;###autoload
  9264. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9265. "Open a link in the string S, as if it was in Org-mode."
  9266. (interactive "sLink: \nP")
  9267. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9268. (with-temp-buffer
  9269. (let ((org-inhibit-startup (not reference-buffer)))
  9270. (org-mode)
  9271. (insert s)
  9272. (goto-char (point-min))
  9273. (when reference-buffer
  9274. (setq org-link-abbrev-alist-local
  9275. (with-current-buffer reference-buffer
  9276. org-link-abbrev-alist-local)))
  9277. (org-open-at-point arg reference-buffer)))))
  9278. (defvar org-open-at-point-functions nil
  9279. "Hook that is run when following a link at point.
  9280. Functions in this hook must return t if they identify and follow
  9281. a link at point. If they don't find anything interesting at point,
  9282. they must return nil.")
  9283. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9284. (defun org-open-at-point (&optional arg reference-buffer)
  9285. "Open link at or after point.
  9286. If there is no link at point, this function will search forward up to
  9287. the end of the current line.
  9288. Normally, files will be opened by an appropriate application. If the
  9289. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9290. With a double prefix argument, try to open outside of Emacs, in the
  9291. application the system uses for this file type."
  9292. (interactive "P")
  9293. ;; if in a code block, then open the block's results
  9294. (unless (call-interactively #'org-babel-open-src-block-result)
  9295. (org-load-modules-maybe)
  9296. (move-marker org-open-link-marker (point))
  9297. (setq org-window-config-before-follow-link (current-window-configuration))
  9298. (org-remove-occur-highlights nil nil t)
  9299. (cond
  9300. ((and (org-at-heading-p)
  9301. (not (org-at-timestamp-p t))
  9302. (not (org-in-regexp
  9303. (concat org-plain-link-re "\\|"
  9304. org-bracket-link-regexp "\\|"
  9305. org-angle-link-re "\\|"
  9306. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9307. (not (get-text-property (point) 'org-linked-text)))
  9308. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9309. (lk0 (car lkall))
  9310. (lk (if (stringp lk0) (list lk0) lk0))
  9311. (lkend (cdr lkall)))
  9312. (mapcar (lambda(l)
  9313. (search-forward l nil lkend)
  9314. (goto-char (match-beginning 0))
  9315. (org-open-at-point))
  9316. lk))
  9317. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9318. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9319. ((and (org-at-timestamp-p t)
  9320. (not (org-in-regexp org-bracket-link-regexp)))
  9321. (org-follow-timestamp-link))
  9322. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9323. (not (org-in-regexp org-any-link-re)))
  9324. (org-footnote-action))
  9325. (t
  9326. (let (type path link line search (pos (point)))
  9327. (catch 'match
  9328. (save-excursion
  9329. (skip-chars-forward "^]\n\r")
  9330. (when (org-in-regexp org-bracket-link-regexp 1)
  9331. (setq link (org-extract-attributes
  9332. (org-link-unescape (org-match-string-no-properties 1))))
  9333. (while (string-match " *\n *" link)
  9334. (setq link (replace-match " " t t link)))
  9335. (setq link (org-link-expand-abbrev link))
  9336. (cond
  9337. ((or (file-name-absolute-p link)
  9338. (string-match "^\\.\\.?/" link))
  9339. (setq type "file" path link))
  9340. ((string-match org-link-re-with-space3 link)
  9341. (setq type (match-string 1 link) path (match-string 2 link)))
  9342. ((string-match "^help:+\\(.+\\)" link)
  9343. (setq type "help" path (match-string 1 link)))
  9344. (t (setq type "thisfile" path link)))
  9345. (throw 'match t)))
  9346. (when (get-text-property (point) 'org-linked-text)
  9347. (setq type "thisfile"
  9348. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9349. (1+ (point)) (point))
  9350. path (buffer-substring
  9351. (or (previous-single-property-change pos 'org-linked-text)
  9352. (point-min))
  9353. (or (next-single-property-change pos 'org-linked-text)
  9354. (point-max)))
  9355. ;; Ensure we will search for a <<<radio>>> link, not
  9356. ;; a simple reference like <<ref>>
  9357. path (concat "<" path))
  9358. (throw 'match t))
  9359. (save-excursion
  9360. (when (or (org-in-regexp org-angle-link-re)
  9361. (let ((match (org-in-regexp org-plain-link-re)))
  9362. ;; Check a plain link is not within a bracket link
  9363. (and match
  9364. (save-excursion
  9365. (progn
  9366. (goto-char (car match))
  9367. (not (org-in-regexp org-bracket-link-regexp))))))
  9368. (let ((line_ending (save-excursion (end-of-line) (point))))
  9369. ;; We are in a line before a plain or bracket link
  9370. (or (re-search-forward org-plain-link-re line_ending t)
  9371. (re-search-forward org-bracket-link-regexp line_ending t))))
  9372. (setq type (match-string 1)
  9373. path (org-link-unescape (match-string 2)))
  9374. (throw 'match t)))
  9375. (save-excursion
  9376. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9377. (setq type "tags"
  9378. path (match-string 1))
  9379. (while (string-match ":" path)
  9380. (setq path (replace-match "+" t t path)))
  9381. (throw 'match t)))
  9382. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9383. (setq type "tree-match"
  9384. path (match-string 1))
  9385. (throw 'match t)))
  9386. (unless path
  9387. (user-error "No link found"))
  9388. ;; switch back to reference buffer
  9389. ;; needed when if called in a temporary buffer through
  9390. ;; org-open-link-from-string
  9391. (with-current-buffer (or reference-buffer (current-buffer))
  9392. ;; Remove any trailing spaces in path
  9393. (if (string-match " +\\'" path)
  9394. (setq path (replace-match "" t t path)))
  9395. (if (and org-link-translation-function
  9396. (fboundp org-link-translation-function))
  9397. ;; Check if we need to translate the link
  9398. (let ((tmp (funcall org-link-translation-function type path)))
  9399. (setq type (car tmp) path (cdr tmp))))
  9400. (cond
  9401. ((assoc type org-link-protocols)
  9402. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9403. ((equal type "help")
  9404. (let ((f-or-v (intern path)))
  9405. (cond ((fboundp f-or-v)
  9406. (describe-function f-or-v))
  9407. ((boundp f-or-v)
  9408. (describe-variable f-or-v))
  9409. (t (error "Not a known function or variable")))))
  9410. ((equal type "mailto")
  9411. (let ((cmd (car org-link-mailto-program))
  9412. (args (cdr org-link-mailto-program)) args1
  9413. (address path) (subject "") a)
  9414. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9415. (setq address (match-string 1 path)
  9416. subject (org-link-escape (match-string 2 path))))
  9417. (while args
  9418. (cond
  9419. ((not (stringp (car args))) (push (pop args) args1))
  9420. (t (setq a (pop args))
  9421. (if (string-match "%a" a)
  9422. (setq a (replace-match address t t a)))
  9423. (if (string-match "%s" a)
  9424. (setq a (replace-match subject t t a)))
  9425. (push a args1))))
  9426. (apply cmd (nreverse args1))))
  9427. ((member type '("http" "https" "ftp" "news"))
  9428. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9429. (org-link-escape
  9430. path org-link-escape-chars-browser)
  9431. path))))
  9432. ((string= type "doi")
  9433. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9434. (org-link-escape
  9435. path org-link-escape-chars-browser)
  9436. path))))
  9437. ((member type '("message"))
  9438. (browse-url (concat type ":" path)))
  9439. ((string= type "tags")
  9440. (org-tags-view arg path))
  9441. ((string= type "tree-match")
  9442. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9443. ((string= type "file")
  9444. (if (string-match "::\\([0-9]+\\)\\'" path)
  9445. (setq line (string-to-number (match-string 1 path))
  9446. path (substring path 0 (match-beginning 0)))
  9447. (if (string-match "::\\(.+\\)\\'" path)
  9448. (setq search (match-string 1 path)
  9449. path (substring path 0 (match-beginning 0)))))
  9450. (if (string-match "[*?{]" (file-name-nondirectory path))
  9451. (dired path)
  9452. (org-open-file path arg line search)))
  9453. ((string= type "shell")
  9454. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9455. (cmd path))
  9456. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9457. (string-match org-confirm-shell-link-not-regexp cmd))
  9458. (not org-confirm-shell-link-function)
  9459. (funcall org-confirm-shell-link-function
  9460. (format "Execute \"%s\" in shell? "
  9461. (org-add-props cmd nil
  9462. 'face 'org-warning))))
  9463. (progn
  9464. (message "Executing %s" cmd)
  9465. (shell-command cmd buf)
  9466. (if (featurep 'midnight)
  9467. (setq clean-buffer-list-kill-buffer-names
  9468. (cons buf clean-buffer-list-kill-buffer-names))))
  9469. (error "Abort"))))
  9470. ((string= type "elisp")
  9471. (let ((cmd path))
  9472. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9473. (string-match org-confirm-elisp-link-not-regexp cmd))
  9474. (not org-confirm-elisp-link-function)
  9475. (funcall org-confirm-elisp-link-function
  9476. (format "Execute \"%s\" as elisp? "
  9477. (org-add-props cmd nil
  9478. 'face 'org-warning))))
  9479. (message "%s => %s" cmd
  9480. (if (equal (string-to-char cmd) ?\()
  9481. (eval (read cmd))
  9482. (call-interactively (read cmd))))
  9483. (error "Abort"))))
  9484. ((and (string= type "thisfile")
  9485. (run-hook-with-args-until-success
  9486. 'org-open-link-functions path)))
  9487. ((string= type "thisfile")
  9488. (if arg
  9489. (switch-to-buffer-other-window
  9490. (org-get-buffer-for-internal-link (current-buffer)))
  9491. (org-mark-ring-push))
  9492. (let ((cmd `(org-link-search
  9493. ,path
  9494. ,(cond ((equal arg '(4)) ''occur)
  9495. ((equal arg '(16)) ''org-occur))
  9496. ,pos)))
  9497. (condition-case nil (let ((org-link-search-inhibit-query t))
  9498. (eval cmd))
  9499. (error (progn (widen) (eval cmd))))))
  9500. (t (browse-url-at-point)))))))
  9501. (move-marker org-open-link-marker nil)
  9502. (run-hook-with-args 'org-follow-link-hook)))
  9503. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9504. "Offer links in the current entry and return the selected link.
  9505. If there is only one link, return it.
  9506. If NTH is an integer, return the NTH link found.
  9507. If ZERO is a string, check also this string for a link, and if
  9508. there is one, return it."
  9509. (with-current-buffer buffer
  9510. (save-excursion
  9511. (save-restriction
  9512. (widen)
  9513. (goto-char marker)
  9514. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9515. "\\(" org-angle-link-re "\\)\\|"
  9516. "\\(" org-plain-link-re "\\)"))
  9517. (cnt ?0)
  9518. (in-emacs (if (integerp nth) nil nth))
  9519. have-zero end links link c)
  9520. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9521. (push (match-string 0 zero) links)
  9522. (setq cnt (1- cnt) have-zero t))
  9523. (save-excursion
  9524. (org-back-to-heading t)
  9525. (setq end (save-excursion (outline-next-heading) (point)))
  9526. (while (re-search-forward re end t)
  9527. (push (match-string 0) links))
  9528. (setq links (org-uniquify (reverse links))))
  9529. (cond
  9530. ((null links)
  9531. (message "No links"))
  9532. ((equal (length links) 1)
  9533. (setq link (car links)))
  9534. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9535. (setq link (nth (if have-zero nth (1- nth)) links)))
  9536. (t ; we have to select a link
  9537. (save-excursion
  9538. (save-window-excursion
  9539. (delete-other-windows)
  9540. (with-output-to-temp-buffer "*Select Link*"
  9541. (mapc (lambda (l)
  9542. (if (not (string-match org-bracket-link-regexp l))
  9543. (princ (format "[%c] %s\n" (incf cnt)
  9544. (org-remove-angle-brackets l)))
  9545. (if (match-end 3)
  9546. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9547. (match-string 3 l) (match-string 1 l)))
  9548. (princ (format "[%c] %s\n" (incf cnt)
  9549. (match-string 1 l))))))
  9550. links))
  9551. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9552. (message "Select link to open, RET to open all:")
  9553. (setq c (read-char-exclusive))
  9554. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9555. (when (equal c ?q) (error "Abort"))
  9556. (if (equal c ?\C-m)
  9557. (setq link links)
  9558. (setq nth (- c ?0))
  9559. (if have-zero (setq nth (1+ nth)))
  9560. (unless (and (integerp nth) (>= (length links) nth))
  9561. (user-error "Invalid link selection"))
  9562. (setq link (nth (1- nth) links)))))
  9563. (cons link end))))))
  9564. ;; Add special file links that specify the way of opening
  9565. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9566. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9567. (defun org-open-file-with-system (path)
  9568. "Open file at PATH using the system way of opening it."
  9569. (org-open-file path 'system))
  9570. (defun org-open-file-with-emacs (path)
  9571. "Open file at PATH in Emacs."
  9572. (org-open-file path 'emacs))
  9573. ;;; File search
  9574. (defvar org-create-file-search-functions nil
  9575. "List of functions to construct the right search string for a file link.
  9576. These functions are called in turn with point at the location to
  9577. which the link should point.
  9578. A function in the hook should first test if it would like to
  9579. handle this file type, for example by checking the `major-mode'
  9580. or the file extension. If it decides not to handle this file, it
  9581. should just return nil to give other functions a chance. If it
  9582. does handle the file, it must return the search string to be used
  9583. when following the link. The search string will be part of the
  9584. file link, given after a double colon, and `org-open-at-point'
  9585. will automatically search for it. If special measures must be
  9586. taken to make the search successful, another function should be
  9587. added to the companion hook `org-execute-file-search-functions',
  9588. which see.
  9589. A function in this hook may also use `setq' to set the variable
  9590. `description' to provide a suggestion for the descriptive text to
  9591. be used for this link when it gets inserted into an Org-mode
  9592. buffer with \\[org-insert-link].")
  9593. (defvar org-execute-file-search-functions nil
  9594. "List of functions to execute a file search triggered by a link.
  9595. Functions added to this hook must accept a single argument, the
  9596. search string that was part of the file link, the part after the
  9597. double colon. The function must first check if it would like to
  9598. handle this search, for example by checking the `major-mode' or
  9599. the file extension. If it decides not to handle this search, it
  9600. should just return nil to give other functions a chance. If it
  9601. does handle the search, it must return a non-nil value to keep
  9602. other functions from trying.
  9603. Each function can access the current prefix argument through the
  9604. variable `current-prefix-arg'. Note that a single prefix is used
  9605. to force opening a link in Emacs, so it may be good to only use a
  9606. numeric or double prefix to guide the search function.
  9607. In case this is needed, a function in this hook can also restore
  9608. the window configuration before `org-open-at-point' was called using:
  9609. (set-window-configuration org-window-config-before-follow-link)")
  9610. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9611. (defun org-link-search (s &optional type avoid-pos stealth)
  9612. "Search for a link search option.
  9613. If S is surrounded by forward slashes, it is interpreted as a
  9614. regular expression. In org-mode files, this will create an `org-occur'
  9615. sparse tree. In ordinary files, `occur' will be used to list matches.
  9616. If the current buffer is in `dired-mode', grep will be used to search
  9617. in all files. If AVOID-POS is given, ignore matches near that position.
  9618. When optional argument STEALTH is non-nil, do not modify
  9619. visibility around point, thus ignoring
  9620. `org-show-hierarchy-above', `org-show-following-heading' and
  9621. `org-show-siblings' variables."
  9622. (let ((case-fold-search t)
  9623. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9624. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9625. (append '(("") (" ") ("\t") ("\n"))
  9626. org-emphasis-alist)
  9627. "\\|") "\\)"))
  9628. (pos (point))
  9629. (pre nil) (post nil)
  9630. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9631. (cond
  9632. ;; First check if there are any special search functions
  9633. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9634. ;; Now try the builtin stuff
  9635. ((and (equal (string-to-char s0) ?#)
  9636. (> (length s0) 1)
  9637. (save-excursion
  9638. (goto-char (point-min))
  9639. (and
  9640. (re-search-forward
  9641. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9642. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9643. (setq type 'dedicated
  9644. pos (match-beginning 0))))
  9645. ;; There is an exact target for this
  9646. (goto-char pos)
  9647. (org-back-to-heading t)))
  9648. ((save-excursion
  9649. (goto-char (point-min))
  9650. (and
  9651. (re-search-forward
  9652. (concat "<<" (regexp-quote s0) ">>") nil t)
  9653. (setq type 'dedicated
  9654. pos (match-beginning 0))))
  9655. ;; There is an exact target for this
  9656. (goto-char pos))
  9657. ((save-excursion
  9658. (goto-char (point-min))
  9659. (and
  9660. (re-search-forward
  9661. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9662. (setq type 'dedicated pos (match-beginning 0))))
  9663. ;; Found an element with a matching #+name affiliated keyword.
  9664. (goto-char pos))
  9665. ((and (string-match "^(\\(.*\\))$" s0)
  9666. (save-excursion
  9667. (goto-char (point-min))
  9668. (and
  9669. (re-search-forward
  9670. (concat "[^[]" (regexp-quote
  9671. (format org-coderef-label-format
  9672. (match-string 1 s0))))
  9673. nil t)
  9674. (setq type 'dedicated
  9675. pos (1+ (match-beginning 0))))))
  9676. ;; There is a coderef target for this
  9677. (goto-char pos))
  9678. ((string-match "^/\\(.*\\)/$" s)
  9679. ;; A regular expression
  9680. (cond
  9681. ((derived-mode-p 'org-mode)
  9682. (org-occur (match-string 1 s)))
  9683. (t (org-do-occur (match-string 1 s)))))
  9684. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9685. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9686. (goto-char (point-min))
  9687. (cond
  9688. ((let (case-fold-search)
  9689. (re-search-forward (format org-complex-heading-regexp-format
  9690. (regexp-quote s))
  9691. nil t))
  9692. ;; OK, found a match
  9693. (setq type 'dedicated)
  9694. (goto-char (match-beginning 0)))
  9695. ((and (not org-link-search-inhibit-query)
  9696. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9697. (y-or-n-p "No match - create this as a new heading? "))
  9698. (goto-char (point-max))
  9699. (or (bolp) (newline))
  9700. (insert "* " s "\n")
  9701. (beginning-of-line 0))
  9702. (t
  9703. (goto-char pos)
  9704. (error "No match"))))
  9705. (t
  9706. ;; A normal search string
  9707. (when (equal (string-to-char s) ?*)
  9708. ;; Anchor on headlines, post may include tags.
  9709. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9710. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9711. s (substring s 1)))
  9712. (remove-text-properties
  9713. 0 (length s)
  9714. '(face nil mouse-face nil keymap nil fontified nil) s)
  9715. ;; Make a series of regular expressions to find a match
  9716. (setq words (org-split-string s "[ \n\r\t]+")
  9717. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9718. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9719. "\\)" markers)
  9720. re2a_ (concat "\\(" (mapconcat 'downcase words
  9721. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9722. re2a (concat "[ \t\r\n]" re2a_)
  9723. re4_ (concat "\\(" (mapconcat 'downcase words
  9724. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9725. re4 (concat "[^a-zA-Z_]" re4_)
  9726. re1 (concat pre re2 post)
  9727. re3 (concat pre (if pre re4_ re4) post)
  9728. re5 (concat pre ".*" re4)
  9729. re2 (concat pre re2)
  9730. re2a (concat pre (if pre re2a_ re2a))
  9731. re4 (concat pre (if pre re4_ re4))
  9732. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9733. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9734. re5 "\\)"))
  9735. (cond
  9736. ((eq type 'org-occur) (org-occur reall))
  9737. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9738. (t (goto-char (point-min))
  9739. (setq type 'fuzzy)
  9740. (if (or (and (org-search-not-self 1 re0 nil t)
  9741. (setq type 'dedicated))
  9742. (org-search-not-self 1 re1 nil t)
  9743. (org-search-not-self 1 re2 nil t)
  9744. (org-search-not-self 1 re2a nil t)
  9745. (org-search-not-self 1 re3 nil t)
  9746. (org-search-not-self 1 re4 nil t)
  9747. (org-search-not-self 1 re5 nil t))
  9748. (goto-char (match-beginning 1))
  9749. (goto-char pos)
  9750. (error "No match"))))))
  9751. (and (derived-mode-p 'org-mode)
  9752. (not stealth)
  9753. (org-show-context 'link-search))
  9754. type))
  9755. (defun org-search-not-self (group &rest args)
  9756. "Execute `re-search-forward', but only accept matches that do not
  9757. enclose the position of `org-open-link-marker'."
  9758. (let ((m org-open-link-marker))
  9759. (catch 'exit
  9760. (while (apply 're-search-forward args)
  9761. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9762. (goto-char (match-end group))
  9763. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9764. (> (match-beginning 0) (marker-position m))
  9765. (< (match-end 0) (marker-position m)))
  9766. (save-match-data
  9767. (or (not (org-in-regexp
  9768. org-bracket-link-analytic-regexp 1))
  9769. (not (match-end 4)) ; no description
  9770. (and (<= (match-beginning 4) (point))
  9771. (>= (match-end 4) (point))))))
  9772. (throw 'exit (point))))))))
  9773. (defun org-get-buffer-for-internal-link (buffer)
  9774. "Return a buffer to be used for displaying the link target of internal links."
  9775. (cond
  9776. ((not org-display-internal-link-with-indirect-buffer)
  9777. buffer)
  9778. ((string-match "(Clone)$" (buffer-name buffer))
  9779. (message "Buffer is already a clone, not making another one")
  9780. ;; we also do not modify visibility in this case
  9781. buffer)
  9782. (t ; make a new indirect buffer for displaying the link
  9783. (let* ((bn (buffer-name buffer))
  9784. (ibn (concat bn "(Clone)"))
  9785. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9786. (with-current-buffer ib (org-overview))
  9787. ib))))
  9788. (defun org-do-occur (regexp &optional cleanup)
  9789. "Call the Emacs command `occur'.
  9790. If CLEANUP is non-nil, remove the printout of the regular expression
  9791. in the *Occur* buffer. This is useful if the regex is long and not useful
  9792. to read."
  9793. (occur regexp)
  9794. (when cleanup
  9795. (let ((cwin (selected-window)) win beg end)
  9796. (when (setq win (get-buffer-window "*Occur*"))
  9797. (select-window win))
  9798. (goto-char (point-min))
  9799. (when (re-search-forward "match[a-z]+" nil t)
  9800. (setq beg (match-end 0))
  9801. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9802. (setq end (1- (match-beginning 0)))))
  9803. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9804. (goto-char (point-min))
  9805. (select-window cwin))))
  9806. ;;; The mark ring for links jumps
  9807. (defvar org-mark-ring nil
  9808. "Mark ring for positions before jumps in Org-mode.")
  9809. (defvar org-mark-ring-last-goto nil
  9810. "Last position in the mark ring used to go back.")
  9811. ;; Fill and close the ring
  9812. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9813. (loop for i from 1 to org-mark-ring-length do
  9814. (push (make-marker) org-mark-ring))
  9815. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9816. org-mark-ring)
  9817. (defun org-mark-ring-push (&optional pos buffer)
  9818. "Put the current position or POS into the mark ring and rotate it."
  9819. (interactive)
  9820. (setq pos (or pos (point)))
  9821. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9822. (move-marker (car org-mark-ring)
  9823. (or pos (point))
  9824. (or buffer (current-buffer)))
  9825. (message "%s"
  9826. (substitute-command-keys
  9827. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9828. (defun org-mark-ring-goto (&optional n)
  9829. "Jump to the previous position in the mark ring.
  9830. With prefix arg N, jump back that many stored positions. When
  9831. called several times in succession, walk through the entire ring.
  9832. Org-mode commands jumping to a different position in the current file,
  9833. or to another Org-mode file, automatically push the old position
  9834. onto the ring."
  9835. (interactive "p")
  9836. (let (p m)
  9837. (if (eq last-command this-command)
  9838. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9839. (setq p org-mark-ring))
  9840. (setq org-mark-ring-last-goto p)
  9841. (setq m (car p))
  9842. (org-pop-to-buffer-same-window (marker-buffer m))
  9843. (goto-char m)
  9844. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9845. (defun org-remove-angle-brackets (s)
  9846. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9847. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9848. s)
  9849. (defun org-add-angle-brackets (s)
  9850. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9851. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9852. s)
  9853. (defun org-remove-double-quotes (s)
  9854. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9855. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9856. s)
  9857. ;;; Following specific links
  9858. (defun org-follow-timestamp-link ()
  9859. "Open an agenda view for the time-stamp date/range at point."
  9860. (cond
  9861. ((org-at-date-range-p t)
  9862. (let ((org-agenda-start-on-weekday)
  9863. (t1 (match-string 1))
  9864. (t2 (match-string 2)) tt1 tt2)
  9865. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9866. tt2 (time-to-days (org-time-string-to-time t2)))
  9867. (let ((org-agenda-buffer-tmp-name
  9868. (format "*Org Agenda(a:%s)"
  9869. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9870. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9871. ((org-at-timestamp-p t)
  9872. (let ((org-agenda-buffer-tmp-name
  9873. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9874. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9875. (substring (match-string 1) 0 10)))
  9876. 1)))
  9877. (t (error "This should not happen"))))
  9878. ;;; Following file links
  9879. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9880. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9881. (declare-function mailcap-mime-info
  9882. "mailcap" (string &optional request no-decode))
  9883. (defvar org-wait nil)
  9884. (defun org-open-file (path &optional in-emacs line search)
  9885. "Open the file at PATH.
  9886. First, this expands any special file name abbreviations. Then the
  9887. configuration variable `org-file-apps' is checked if it contains an
  9888. entry for this file type, and if yes, the corresponding command is launched.
  9889. If no application is found, Emacs simply visits the file.
  9890. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9891. With a double \\[universal-argument] \\[universal-argument] \
  9892. prefix arg, Org tries to avoid opening in Emacs
  9893. and to use an external application to visit the file.
  9894. Optional LINE specifies a line to go to, optional SEARCH a string
  9895. to search for. If LINE or SEARCH is given, the file will be
  9896. opened in Emacs, unless an entry from org-file-apps that makes
  9897. use of groups in a regexp matches.
  9898. If you want to change the way frames are used when following a
  9899. link, please customize `org-link-frame-setup'.
  9900. If the file does not exist, an error is thrown."
  9901. (let* ((file (if (equal path "")
  9902. buffer-file-name
  9903. (substitute-in-file-name (expand-file-name path))))
  9904. (file-apps (append org-file-apps (org-default-apps)))
  9905. (apps (org-remove-if
  9906. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9907. (apps-dlink (org-remove-if-not
  9908. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9909. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9910. (dirp (if remp nil (file-directory-p file)))
  9911. (file (if (and dirp org-open-directory-means-index-dot-org)
  9912. (concat (file-name-as-directory file) "index.org")
  9913. file))
  9914. (a-m-a-p (assq 'auto-mode apps))
  9915. (dfile (downcase file))
  9916. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9917. (link (cond ((and (eq line nil)
  9918. (eq search nil))
  9919. file)
  9920. (line
  9921. (concat file "::" (number-to-string line)))
  9922. (search
  9923. (concat file "::" search))))
  9924. (dlink (downcase link))
  9925. (old-buffer (current-buffer))
  9926. (old-pos (point))
  9927. (old-mode major-mode)
  9928. ext cmd link-match-data)
  9929. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9930. (setq ext (match-string 1 dfile))
  9931. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9932. (setq ext (match-string 1 dfile))))
  9933. (cond
  9934. ((member in-emacs '((16) system))
  9935. (setq cmd (cdr (assoc 'system apps))))
  9936. (in-emacs (setq cmd 'emacs))
  9937. (t
  9938. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9939. (and dirp (cdr (assoc 'directory apps)))
  9940. ; first, try matching against apps-dlink
  9941. ; if we get a match here, store the match data for later
  9942. (let ((match (assoc-default dlink apps-dlink
  9943. 'string-match)))
  9944. (if match
  9945. (progn (setq link-match-data (match-data))
  9946. match)
  9947. (progn (setq in-emacs (or in-emacs line search))
  9948. nil))) ; if we have no match in apps-dlink,
  9949. ; always open the file in emacs if line or search
  9950. ; is given (for backwards compatibility)
  9951. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9952. 'string-match)
  9953. (cdr (assoc ext apps))
  9954. (cdr (assoc t apps))))))
  9955. (when (eq cmd 'system)
  9956. (setq cmd (cdr (assoc 'system apps))))
  9957. (when (eq cmd 'default)
  9958. (setq cmd (cdr (assoc t apps))))
  9959. (when (eq cmd 'mailcap)
  9960. (require 'mailcap)
  9961. (mailcap-parse-mailcaps)
  9962. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9963. (command (mailcap-mime-info mime-type)))
  9964. (if (stringp command)
  9965. (setq cmd command)
  9966. (setq cmd 'emacs))))
  9967. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9968. (not (file-exists-p file))
  9969. (not org-open-non-existing-files))
  9970. (user-error "No such file: %s" file))
  9971. (cond
  9972. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9973. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9974. (while (string-match "['\"]%s['\"]" cmd)
  9975. (setq cmd (replace-match "%s" t t cmd)))
  9976. (while (string-match "%s" cmd)
  9977. (setq cmd (replace-match
  9978. (save-match-data
  9979. (shell-quote-argument
  9980. (convert-standard-filename file)))
  9981. t t cmd)))
  9982. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9983. (save-match-data
  9984. (let ((match-index 1)
  9985. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9986. (set-match-data link-match-data)
  9987. (while (<= match-index number-of-groups)
  9988. (let ((regex (concat "%" (number-to-string match-index)))
  9989. (replace-with (match-string match-index dlink)))
  9990. (while (string-match regex cmd)
  9991. (setq cmd (replace-match replace-with t t cmd))))
  9992. (setq match-index (+ match-index 1)))))
  9993. (save-window-excursion
  9994. (message "Running %s...done" cmd)
  9995. (start-process-shell-command cmd nil cmd)
  9996. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9997. ((or (stringp cmd)
  9998. (eq cmd 'emacs))
  9999. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10000. (widen)
  10001. (if line (org-goto-line line)
  10002. (if search (org-link-search search))))
  10003. ((consp cmd)
  10004. (let ((file (convert-standard-filename file)))
  10005. (save-match-data
  10006. (set-match-data link-match-data)
  10007. (eval cmd))))
  10008. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10009. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10010. (or (not (equal old-buffer (current-buffer)))
  10011. (not (equal old-pos (point))))
  10012. (org-mark-ring-push old-pos old-buffer))))
  10013. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10014. "This function returns non-nil if `entry' uses a regular
  10015. expression which should be matched against the whole link by
  10016. org-open-file.
  10017. It assumes that is the case when the entry uses a regular
  10018. expression which has at least one grouping construct and the
  10019. action is either a lisp form or a command string containing
  10020. '%1', i.e. using at least one subexpression match as a
  10021. parameter."
  10022. (let ((selector (car entry))
  10023. (action (cdr entry)))
  10024. (if (stringp selector)
  10025. (and (> (regexp-opt-depth selector) 0)
  10026. (or (and (stringp action)
  10027. (string-match "%[0-9]" action))
  10028. (consp action)))
  10029. nil)))
  10030. (defun org-default-apps ()
  10031. "Return the default applications for this operating system."
  10032. (cond
  10033. ((eq system-type 'darwin)
  10034. org-file-apps-defaults-macosx)
  10035. ((eq system-type 'windows-nt)
  10036. org-file-apps-defaults-windowsnt)
  10037. (t org-file-apps-defaults-gnu)))
  10038. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10039. "Convert extensions to regular expressions in the cars of LIST.
  10040. Also, weed out any non-string entries, because the return value is used
  10041. only for regexp matching.
  10042. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10043. point to the symbol `emacs', indicating that the file should
  10044. be opened in Emacs."
  10045. (append
  10046. (delq nil
  10047. (mapcar (lambda (x)
  10048. (if (not (stringp (car x)))
  10049. nil
  10050. (if (string-match "\\W" (car x))
  10051. x
  10052. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10053. list))
  10054. (if add-auto-mode
  10055. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10056. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10057. (defun org-file-remote-p (file)
  10058. "Test whether FILE specifies a location on a remote system.
  10059. Return non-nil if the location is indeed remote.
  10060. For example, the filename \"/user@host:/foo\" specifies a location
  10061. on the system \"/user@host:\"."
  10062. (cond ((fboundp 'file-remote-p)
  10063. (file-remote-p file))
  10064. ((fboundp 'tramp-handle-file-remote-p)
  10065. (tramp-handle-file-remote-p file))
  10066. ((and (boundp 'ange-ftp-name-format)
  10067. (string-match (car ange-ftp-name-format) file))
  10068. t)))
  10069. ;;;; Refiling
  10070. (defun org-get-org-file ()
  10071. "Read a filename, with default directory `org-directory'."
  10072. (let ((default (or org-default-notes-file remember-data-file)))
  10073. (read-file-name (format "File name [%s]: " default)
  10074. (file-name-as-directory org-directory)
  10075. default)))
  10076. (defun org-notes-order-reversed-p ()
  10077. "Check if the current file should receive notes in reversed order."
  10078. (cond
  10079. ((not org-reverse-note-order) nil)
  10080. ((eq t org-reverse-note-order) t)
  10081. ((not (listp org-reverse-note-order)) nil)
  10082. (t (catch 'exit
  10083. (let ((all org-reverse-note-order)
  10084. entry)
  10085. (while (setq entry (pop all))
  10086. (if (string-match (car entry) buffer-file-name)
  10087. (throw 'exit (cdr entry))))
  10088. nil)))))
  10089. (defvar org-refile-target-table nil
  10090. "The list of refile targets, created by `org-refile'.")
  10091. (defvar org-agenda-new-buffers nil
  10092. "Buffers created to visit agenda files.")
  10093. (defvar org-refile-cache nil
  10094. "Cache for refile targets.")
  10095. (defvar org-refile-markers nil
  10096. "All the markers used for caching refile locations.")
  10097. (defun org-refile-marker (pos)
  10098. "Get a new refile marker, but only if caching is in use."
  10099. (if (not org-refile-use-cache)
  10100. pos
  10101. (let ((m (make-marker)))
  10102. (move-marker m pos)
  10103. (push m org-refile-markers)
  10104. m)))
  10105. (defun org-refile-cache-clear ()
  10106. "Clear the refile cache and disable all the markers."
  10107. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10108. (setq org-refile-markers nil)
  10109. (setq org-refile-cache nil)
  10110. (message "Refile cache has been cleared"))
  10111. (defun org-refile-cache-check-set (set)
  10112. "Check if all the markers in the cache still have live buffers."
  10113. (let (marker)
  10114. (catch 'exit
  10115. (while (and set (setq marker (nth 3 (pop set))))
  10116. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10117. (when (and marker (null (marker-buffer marker)))
  10118. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10119. (sit-for 3)
  10120. (throw 'exit nil)))
  10121. t)))
  10122. (defun org-refile-cache-put (set &rest identifiers)
  10123. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10124. (let* ((key (sha1 (prin1-to-string identifiers)))
  10125. (entry (assoc key org-refile-cache)))
  10126. (if entry
  10127. (setcdr entry set)
  10128. (push (cons key set) org-refile-cache))))
  10129. (defun org-refile-cache-get (&rest identifiers)
  10130. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10131. (cond
  10132. ((not org-refile-cache) nil)
  10133. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10134. (t
  10135. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10136. org-refile-cache))))
  10137. (and set (org-refile-cache-check-set set) set)))))
  10138. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10139. "Produce a table with refile targets."
  10140. (let ((case-fold-search nil)
  10141. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10142. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10143. targets tgs txt re files f desc descre fast-path-p level pos0)
  10144. (message "Getting targets...")
  10145. (with-current-buffer (or default-buffer (current-buffer))
  10146. (while (setq entry (pop entries))
  10147. (setq files (car entry) desc (cdr entry))
  10148. (setq fast-path-p nil)
  10149. (cond
  10150. ((null files) (setq files (list (current-buffer))))
  10151. ((eq files 'org-agenda-files)
  10152. (setq files (org-agenda-files 'unrestricted)))
  10153. ((and (symbolp files) (fboundp files))
  10154. (setq files (funcall files)))
  10155. ((and (symbolp files) (boundp files))
  10156. (setq files (symbol-value files))))
  10157. (if (stringp files) (setq files (list files)))
  10158. (cond
  10159. ((eq (car desc) :tag)
  10160. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10161. ((eq (car desc) :todo)
  10162. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10163. ((eq (car desc) :regexp)
  10164. (setq descre (cdr desc)))
  10165. ((eq (car desc) :level)
  10166. (setq descre (concat "^\\*\\{" (number-to-string
  10167. (if org-odd-levels-only
  10168. (1- (* 2 (cdr desc)))
  10169. (cdr desc)))
  10170. "\\}[ \t]")))
  10171. ((eq (car desc) :maxlevel)
  10172. (setq fast-path-p t)
  10173. (setq descre (concat "^\\*\\{1," (number-to-string
  10174. (if org-odd-levels-only
  10175. (1- (* 2 (cdr desc)))
  10176. (cdr desc)))
  10177. "\\}[ \t]")))
  10178. (t (error "Bad refiling target description %s" desc)))
  10179. (while (setq f (pop files))
  10180. (with-current-buffer
  10181. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10182. (or
  10183. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10184. (progn
  10185. (if (bufferp f) (setq f (buffer-file-name
  10186. (buffer-base-buffer f))))
  10187. (setq f (and f (expand-file-name f)))
  10188. (if (eq org-refile-use-outline-path 'file)
  10189. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10190. (save-excursion
  10191. (save-restriction
  10192. (widen)
  10193. (goto-char (point-min))
  10194. (while (re-search-forward descre nil t)
  10195. (goto-char (setq pos0 (point-at-bol)))
  10196. (catch 'next
  10197. (when org-refile-target-verify-function
  10198. (save-match-data
  10199. (or (funcall org-refile-target-verify-function)
  10200. (throw 'next t))))
  10201. (when (and (looking-at org-complex-heading-regexp)
  10202. (not (member (match-string 4) excluded-entries))
  10203. (match-string 4))
  10204. (setq level (org-reduced-level
  10205. (- (match-end 1) (match-beginning 1)))
  10206. txt (org-link-display-format (match-string 4))
  10207. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10208. re (format org-complex-heading-regexp-format
  10209. (regexp-quote (match-string 4))))
  10210. (when org-refile-use-outline-path
  10211. (setq txt (mapconcat
  10212. 'org-protect-slash
  10213. (append
  10214. (if (eq org-refile-use-outline-path
  10215. 'file)
  10216. (list (file-name-nondirectory
  10217. (buffer-file-name
  10218. (buffer-base-buffer))))
  10219. (if (eq org-refile-use-outline-path
  10220. 'full-file-path)
  10221. (list (buffer-file-name
  10222. (buffer-base-buffer)))))
  10223. (org-get-outline-path fast-path-p
  10224. level txt)
  10225. (list txt))
  10226. "/")))
  10227. (push (list txt f re (org-refile-marker (point)))
  10228. tgs)))
  10229. (when (= (point) pos0)
  10230. ;; verification function has not moved point
  10231. (goto-char (point-at-eol))))))))
  10232. (when org-refile-use-cache
  10233. (org-refile-cache-put tgs (buffer-file-name) descre))
  10234. (setq targets (append tgs targets))))))
  10235. (message "Getting targets...done")
  10236. (nreverse targets)))
  10237. (defun org-protect-slash (s)
  10238. (while (string-match "/" s)
  10239. (setq s (replace-match "\\" t t s)))
  10240. s)
  10241. (defvar org-olpa (make-vector 20 nil))
  10242. (defun org-get-outline-path (&optional fastp level heading)
  10243. "Return the outline path to the current entry, as a list.
  10244. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10245. routine which makes outline path derivations for an entire file,
  10246. avoiding backtracing. Refile target collection makes use of that."
  10247. (if fastp
  10248. (progn
  10249. (if (> level 19)
  10250. (error "Outline path failure, more than 19 levels"))
  10251. (loop for i from level upto 19 do
  10252. (aset org-olpa i nil))
  10253. (prog1
  10254. (delq nil (append org-olpa nil))
  10255. (aset org-olpa level heading)))
  10256. (let (rtn case-fold-search)
  10257. (save-excursion
  10258. (save-restriction
  10259. (widen)
  10260. (while (org-up-heading-safe)
  10261. (when (looking-at org-complex-heading-regexp)
  10262. (push (org-trim
  10263. (replace-regexp-in-string
  10264. ;; Remove statistical/checkboxes cookies
  10265. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10266. (org-match-string-no-properties 4)))
  10267. rtn)))
  10268. rtn)))))
  10269. (defun org-format-outline-path (path &optional width prefix separator)
  10270. "Format the outline path PATH for display.
  10271. WIDTH is the maximum number of characters that is available.
  10272. PREFIX is a prefix to be included in the returned string,
  10273. such as the file name.
  10274. SEPARATOR is inserted between the different parts of the path,
  10275. the default is \"/\"."
  10276. (setq width (or width 79))
  10277. (if prefix (setq width (- width (length prefix))))
  10278. (if (not path)
  10279. (or prefix "")
  10280. (let* ((nsteps (length path))
  10281. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10282. (maxwidth (if (<= total-width width)
  10283. 10000 ;; everything fits
  10284. ;; we need to shorten the level headings
  10285. (/ (- width nsteps) nsteps)))
  10286. (org-odd-levels-only nil)
  10287. (n 0)
  10288. (total (1+ (length prefix))))
  10289. (setq maxwidth (max maxwidth 10))
  10290. (concat prefix
  10291. (if prefix (or separator "/"))
  10292. (mapconcat
  10293. (lambda (h)
  10294. (setq n (1+ n))
  10295. (if (and (= n nsteps) (< maxwidth 10000))
  10296. (setq maxwidth (- total-width total)))
  10297. (if (< (length h) maxwidth)
  10298. (progn (setq total (+ total (length h) 1)) h)
  10299. (setq h (substring h 0 (- maxwidth 2))
  10300. total (+ total maxwidth 1))
  10301. (if (string-match "[ \t]+\\'" h)
  10302. (setq h (substring h 0 (match-beginning 0))))
  10303. (setq h (concat h "..")))
  10304. (org-add-props h nil 'face
  10305. (nth (% (1- n) org-n-level-faces)
  10306. org-level-faces))
  10307. h)
  10308. path (or separator "/"))))))
  10309. (defun org-display-outline-path (&optional file current separator just-return-string)
  10310. "Display the current outline path in the echo area.
  10311. If FILE is non-nil, prepend the output with the file name.
  10312. If CURRENT is non-nil, append the current heading to the output.
  10313. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10314. the different parts of the path and defaults to \"/\".
  10315. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10316. (interactive "P")
  10317. (let* (case-fold-search
  10318. message-log-max ; Don't populate the *Messages* buffer
  10319. (bfn (buffer-file-name (buffer-base-buffer)))
  10320. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10321. res)
  10322. (if current (setq path (append path
  10323. (save-excursion
  10324. (org-back-to-heading t)
  10325. (if (looking-at org-complex-heading-regexp)
  10326. (list (match-string 4)))))))
  10327. (setq res
  10328. (org-format-outline-path
  10329. path
  10330. (1- (frame-width))
  10331. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10332. separator))
  10333. (if just-return-string
  10334. (org-no-properties res)
  10335. (message "%s" res))))
  10336. (defvar org-refile-history nil
  10337. "History for refiling operations.")
  10338. (defvar org-after-refile-insert-hook nil
  10339. "Hook run after `org-refile' has inserted its stuff at the new location.
  10340. Note that this is still *before* the stuff will be removed from
  10341. the *old* location.")
  10342. (defvar org-capture-last-stored-marker)
  10343. (defvar org-refile-keep nil
  10344. "Non-nil means `org-refile' will copy instead of refile.")
  10345. (defun org-copy ()
  10346. "Like `org-refile', but copy."
  10347. (interactive)
  10348. (let ((org-refile-keep t))
  10349. (funcall 'org-refile nil nil nil "Copy")))
  10350. (defun org-refile (&optional goto default-buffer rfloc msg)
  10351. "Move the entry or entries at point to another heading.
  10352. The list of target headings is compiled using the information in
  10353. `org-refile-targets', which see.
  10354. At the target location, the entry is filed as a subitem of the target
  10355. heading. Depending on `org-reverse-note-order', the new subitem will
  10356. either be the first or the last subitem.
  10357. If there is an active region, all entries in that region will be moved.
  10358. However, the region must fulfill the requirement that the first heading
  10359. is the first one sets the top-level of the moved text - at most siblings
  10360. below it are allowed.
  10361. With prefix arg GOTO, the command will only visit the target location
  10362. and not actually move anything.
  10363. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10364. go to the location where the last refiling operation has put the subtree.
  10365. With a prefix argument of `2', refile to the running clock.
  10366. RFLOC can be a refile location obtained in a different way.
  10367. MSG is a string to replace \"Refile\" in the default prompt with
  10368. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10369. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10370. If you are using target caching (see `org-refile-use-cache'),
  10371. you have to clear the target cache in order to find new targets.
  10372. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10373. prefix argument (`C-u C-u C-u C-c C-w')."
  10374. (interactive "P")
  10375. (if (member goto '(0 (64)))
  10376. (org-refile-cache-clear)
  10377. (let* ((actionmsg (or msg "Refile"))
  10378. (cbuf (current-buffer))
  10379. (regionp (org-region-active-p))
  10380. (region-start (and regionp (region-beginning)))
  10381. (region-end (and regionp (region-end)))
  10382. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10383. pos it nbuf file re level reversed)
  10384. (setq last-command nil)
  10385. (when regionp
  10386. (goto-char region-start)
  10387. (or (bolp) (goto-char (point-at-bol)))
  10388. (setq region-start (point))
  10389. (unless (or (org-kill-is-subtree-p
  10390. (buffer-substring region-start region-end))
  10391. (prog1 org-refile-active-region-within-subtree
  10392. (let ((s (point-at-eol)))
  10393. (org-toggle-heading)
  10394. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10395. (user-error "The region is not a (sequence of) subtree(s)")))
  10396. (if (equal goto '(16))
  10397. (org-refile-goto-last-stored)
  10398. (when (or
  10399. (and (equal goto 2)
  10400. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10401. (prog1
  10402. (setq it (list (or org-clock-heading "running clock")
  10403. (buffer-file-name
  10404. (marker-buffer org-clock-hd-marker))
  10405. ""
  10406. (marker-position org-clock-hd-marker)))
  10407. (setq goto nil)))
  10408. (setq it (or rfloc
  10409. (let (heading-text)
  10410. (save-excursion
  10411. (unless goto
  10412. (org-back-to-heading t)
  10413. (setq heading-text
  10414. (nth 4 (org-heading-components))))
  10415. (org-refile-get-location
  10416. (cond (goto "Goto")
  10417. (regionp (concat actionmsg " region to"))
  10418. (t (concat actionmsg " subtree \""
  10419. heading-text "\" to")))
  10420. default-buffer
  10421. (and (not (equal '(4) goto))
  10422. org-refile-allow-creating-parent-nodes)
  10423. goto))))))
  10424. (setq file (nth 1 it)
  10425. re (nth 2 it)
  10426. pos (nth 3 it))
  10427. (if (and (not goto)
  10428. pos
  10429. (equal (buffer-file-name) file)
  10430. (if regionp
  10431. (and (>= pos region-start)
  10432. (<= pos region-end))
  10433. (and (>= pos (point))
  10434. (< pos (save-excursion
  10435. (org-end-of-subtree t t))))))
  10436. (error "Cannot refile to position inside the tree or region"))
  10437. (setq nbuf (or (find-buffer-visiting file)
  10438. (find-file-noselect file)))
  10439. (if goto
  10440. (progn
  10441. (org-pop-to-buffer-same-window nbuf)
  10442. (goto-char pos)
  10443. (org-show-context 'org-goto))
  10444. (if regionp
  10445. (progn
  10446. (org-kill-new (buffer-substring region-start region-end))
  10447. (org-save-markers-in-region region-start region-end))
  10448. (org-copy-subtree 1 nil t))
  10449. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10450. (find-file-noselect file)))
  10451. (setq reversed (org-notes-order-reversed-p))
  10452. (save-excursion
  10453. (save-restriction
  10454. (widen)
  10455. (if pos
  10456. (progn
  10457. (goto-char pos)
  10458. (looking-at org-outline-regexp)
  10459. (setq level (org-get-valid-level (funcall outline-level) 1))
  10460. (goto-char
  10461. (if reversed
  10462. (or (outline-next-heading) (point-max))
  10463. (or (save-excursion (org-get-next-sibling))
  10464. (org-end-of-subtree t t)
  10465. (point-max)))))
  10466. (setq level 1)
  10467. (if (not reversed)
  10468. (goto-char (point-max))
  10469. (goto-char (point-min))
  10470. (or (outline-next-heading) (goto-char (point-max)))))
  10471. (if (not (bolp)) (newline))
  10472. (org-paste-subtree level)
  10473. (when org-log-refile
  10474. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10475. (unless (eq org-log-refile 'note)
  10476. (save-excursion (org-add-log-note))))
  10477. (and org-auto-align-tags
  10478. (let ((org-loop-over-headlines-in-active-region nil))
  10479. (org-set-tags nil t)))
  10480. (with-demoted-errors
  10481. (bookmark-set "org-refile-last-stored"))
  10482. ;; If we are refiling for capture, make sure that the
  10483. ;; last-capture pointers point here
  10484. (when (org-bound-and-true-p org-refile-for-capture)
  10485. (with-demoted-errors
  10486. (bookmark-set "org-capture-last-stored-marker"))
  10487. (move-marker org-capture-last-stored-marker (point)))
  10488. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10489. (run-hooks 'org-after-refile-insert-hook))))
  10490. (unless org-refile-keep
  10491. (if regionp
  10492. (delete-region (point) (+ (point) (- region-end region-start)))
  10493. (delete-region
  10494. (and (org-back-to-heading t) (point))
  10495. (min (buffer-size) (org-end-of-subtree t t) (point)))))
  10496. (when (featurep 'org-inlinetask)
  10497. (org-inlinetask-remove-END-maybe))
  10498. (setq org-markers-to-move nil)
  10499. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10500. (defun org-refile-goto-last-stored ()
  10501. "Go to the location where the last refile was stored."
  10502. (interactive)
  10503. (bookmark-jump "org-refile-last-stored")
  10504. (message "This is the location of the last refile"))
  10505. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10506. no-exclude)
  10507. "Prompt the user for a refile location, using PROMPT.
  10508. PROMPT should not be suffixed with a colon and a space, because
  10509. this function appends the default value from
  10510. `org-refile-history' automatically, if that is not empty.
  10511. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10512. this is used for the GOTO interface."
  10513. (let ((org-refile-targets org-refile-targets)
  10514. (org-refile-use-outline-path org-refile-use-outline-path)
  10515. excluded-entries)
  10516. (when (and (derived-mode-p 'org-mode)
  10517. (not org-refile-use-cache)
  10518. (not no-exclude))
  10519. (org-map-tree
  10520. (lambda()
  10521. (setq excluded-entries
  10522. (append excluded-entries (list (org-get-heading t t)))))))
  10523. (setq org-refile-target-table
  10524. (org-refile-get-targets default-buffer excluded-entries)))
  10525. (unless org-refile-target-table
  10526. (user-error "No refile targets"))
  10527. (let* ((cbuf (current-buffer))
  10528. (partial-completion-mode nil)
  10529. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10530. (cfunc (if (and org-refile-use-outline-path
  10531. org-outline-path-complete-in-steps)
  10532. 'org-olpath-completing-read
  10533. 'org-icompleting-read))
  10534. (extra (if org-refile-use-outline-path "/" ""))
  10535. (cbnex (concat (buffer-name) extra))
  10536. (filename (and cfn (expand-file-name cfn)))
  10537. (tbl (mapcar
  10538. (lambda (x)
  10539. (if (and (not (member org-refile-use-outline-path
  10540. '(file full-file-path)))
  10541. (not (equal filename (nth 1 x))))
  10542. (cons (concat (car x) extra " ("
  10543. (file-name-nondirectory (nth 1 x)) ")")
  10544. (cdr x))
  10545. (cons (concat (car x) extra) (cdr x))))
  10546. org-refile-target-table))
  10547. (completion-ignore-case t)
  10548. cdef
  10549. (prompt (concat prompt
  10550. (or (and (car org-refile-history)
  10551. (concat " (default " (car org-refile-history) ")"))
  10552. (and (assoc cbnex tbl) (setq cdef cbnex)
  10553. (concat " (default " cbnex ")"))) ": "))
  10554. pa answ parent-target child parent old-hist)
  10555. (setq old-hist org-refile-history)
  10556. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10557. nil 'org-refile-history (or cdef (car org-refile-history))))
  10558. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10559. (org-refile-check-position pa)
  10560. (if pa
  10561. (progn
  10562. (when (or (not org-refile-history)
  10563. (not (eq old-hist org-refile-history))
  10564. (not (equal (car pa) (car org-refile-history))))
  10565. (setq org-refile-history
  10566. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10567. org-refile-history
  10568. (cdr org-refile-history))))
  10569. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10570. (pop org-refile-history)))
  10571. pa)
  10572. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10573. (progn
  10574. (setq parent (match-string 1 answ)
  10575. child (match-string 2 answ))
  10576. (setq parent-target (or (assoc parent tbl)
  10577. (assoc (concat parent "/") tbl)))
  10578. (when (and parent-target
  10579. (or (eq new-nodes t)
  10580. (and (eq new-nodes 'confirm)
  10581. (y-or-n-p (format "Create new node \"%s\"? "
  10582. child)))))
  10583. (org-refile-new-child parent-target child)))
  10584. (user-error "Invalid target location")))))
  10585. (declare-function org-string-nw-p "org-macs" (s))
  10586. (defun org-refile-check-position (refile-pointer)
  10587. "Check if the refile pointer matches the headline to which it points."
  10588. (let* ((file (nth 1 refile-pointer))
  10589. (re (nth 2 refile-pointer))
  10590. (pos (nth 3 refile-pointer))
  10591. buffer)
  10592. (if (and (not (markerp pos)) (not file))
  10593. (user-error "Please save the buffer to a file before refiling")
  10594. (when (org-string-nw-p re)
  10595. (setq buffer (if (markerp pos)
  10596. (marker-buffer pos)
  10597. (or (find-buffer-visiting file)
  10598. (find-file-noselect file))))
  10599. (with-current-buffer buffer
  10600. (save-excursion
  10601. (save-restriction
  10602. (widen)
  10603. (goto-char pos)
  10604. (beginning-of-line 1)
  10605. (unless (org-looking-at-p re)
  10606. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10607. (defun org-refile-new-child (parent-target child)
  10608. "Use refile target PARENT-TARGET to add new CHILD below it."
  10609. (unless parent-target
  10610. (error "Cannot find parent for new node"))
  10611. (let ((file (nth 1 parent-target))
  10612. (pos (nth 3 parent-target))
  10613. level)
  10614. (with-current-buffer (or (find-buffer-visiting file)
  10615. (find-file-noselect file))
  10616. (save-excursion
  10617. (save-restriction
  10618. (widen)
  10619. (if pos
  10620. (goto-char pos)
  10621. (goto-char (point-max))
  10622. (if (not (bolp)) (newline)))
  10623. (when (looking-at org-outline-regexp)
  10624. (setq level (funcall outline-level))
  10625. (org-end-of-subtree t t))
  10626. (org-back-over-empty-lines)
  10627. (insert "\n" (make-string
  10628. (if pos (org-get-valid-level level 1) 1) ?*)
  10629. " " child "\n")
  10630. (beginning-of-line 0)
  10631. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10632. (defun org-olpath-completing-read (prompt collection &rest args)
  10633. "Read an outline path like a file name."
  10634. (let ((thetable collection)
  10635. (org-completion-use-ido nil) ; does not work with ido.
  10636. (org-completion-use-iswitchb nil)) ; or iswitchb
  10637. (apply
  10638. 'org-icompleting-read prompt
  10639. (lambda (string predicate &optional flag)
  10640. (let (rtn r f (l (length string)))
  10641. (cond
  10642. ((eq flag nil)
  10643. ;; try completion
  10644. (try-completion string thetable))
  10645. ((eq flag t)
  10646. ;; all-completions
  10647. (setq rtn (all-completions string thetable predicate))
  10648. (mapcar
  10649. (lambda (x)
  10650. (setq r (substring x l))
  10651. (if (string-match " ([^)]*)$" x)
  10652. (setq f (match-string 0 x))
  10653. (setq f ""))
  10654. (if (string-match "/" r)
  10655. (concat string (substring r 0 (match-end 0)) f)
  10656. x))
  10657. rtn))
  10658. ((eq flag 'lambda)
  10659. ;; exact match?
  10660. (assoc string thetable)))))
  10661. args)))
  10662. ;;;; Dynamic blocks
  10663. (defun org-find-dblock (name)
  10664. "Find the first dynamic block with name NAME in the buffer.
  10665. If not found, stay at current position and return nil."
  10666. (let ((case-fold-search t) pos)
  10667. (save-excursion
  10668. (goto-char (point-min))
  10669. (setq pos (and (re-search-forward
  10670. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10671. (match-beginning 0))))
  10672. (if pos (goto-char pos))
  10673. pos))
  10674. (defconst org-dblock-start-re
  10675. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10676. "Matches the start line of a dynamic block, with parameters.")
  10677. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10678. "Matches the end of a dynamic block.")
  10679. (defun org-create-dblock (plist)
  10680. "Create a dynamic block section, with parameters taken from PLIST.
  10681. PLIST must contain a :name entry which is used as name of the block."
  10682. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10683. (end-of-line 1)
  10684. (newline))
  10685. (let ((col (current-column))
  10686. (name (plist-get plist :name)))
  10687. (insert "#+BEGIN: " name)
  10688. (while plist
  10689. (if (eq (car plist) :name)
  10690. (setq plist (cddr plist))
  10691. (insert " " (prin1-to-string (pop plist)))))
  10692. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10693. (beginning-of-line -2)))
  10694. (defun org-prepare-dblock ()
  10695. "Prepare dynamic block for refresh.
  10696. This empties the block, puts the cursor at the insert position and returns
  10697. the property list including an extra property :name with the block name."
  10698. (unless (looking-at org-dblock-start-re)
  10699. (user-error "Not at a dynamic block"))
  10700. (let* ((begdel (1+ (match-end 0)))
  10701. (name (org-no-properties (match-string 1)))
  10702. (params (append (list :name name)
  10703. (read (concat "(" (match-string 3) ")")))))
  10704. (save-excursion
  10705. (beginning-of-line 1)
  10706. (skip-chars-forward " \t")
  10707. (setq params (plist-put params :indentation-column (current-column))))
  10708. (unless (re-search-forward org-dblock-end-re nil t)
  10709. (error "Dynamic block not terminated"))
  10710. (setq params
  10711. (append params
  10712. (list :content (buffer-substring
  10713. begdel (match-beginning 0)))))
  10714. (delete-region begdel (match-beginning 0))
  10715. (goto-char begdel)
  10716. (open-line 1)
  10717. params))
  10718. (defun org-map-dblocks (&optional command)
  10719. "Apply COMMAND to all dynamic blocks in the current buffer.
  10720. If COMMAND is not given, use `org-update-dblock'."
  10721. (let ((cmd (or command 'org-update-dblock)))
  10722. (save-excursion
  10723. (goto-char (point-min))
  10724. (while (re-search-forward org-dblock-start-re nil t)
  10725. (goto-char (match-beginning 0))
  10726. (save-excursion
  10727. (condition-case nil
  10728. (funcall cmd)
  10729. (error (message "Error during update of dynamic block"))))
  10730. (unless (re-search-forward org-dblock-end-re nil t)
  10731. (error "Dynamic block not terminated"))))))
  10732. (defun org-dblock-update (&optional arg)
  10733. "User command for updating dynamic blocks.
  10734. Update the dynamic block at point. With prefix ARG, update all dynamic
  10735. blocks in the buffer."
  10736. (interactive "P")
  10737. (if arg
  10738. (org-update-all-dblocks)
  10739. (or (looking-at org-dblock-start-re)
  10740. (org-beginning-of-dblock))
  10741. (org-update-dblock)))
  10742. (defun org-update-dblock ()
  10743. "Update the dynamic block at point.
  10744. This means to empty the block, parse for parameters and then call
  10745. the correct writing function."
  10746. (interactive)
  10747. (save-window-excursion
  10748. (let* ((pos (point))
  10749. (line (org-current-line))
  10750. (params (org-prepare-dblock))
  10751. (name (plist-get params :name))
  10752. (indent (plist-get params :indentation-column))
  10753. (cmd (intern (concat "org-dblock-write:" name))))
  10754. (message "Updating dynamic block `%s' at line %d..." name line)
  10755. (funcall cmd params)
  10756. (message "Updating dynamic block `%s' at line %d...done" name line)
  10757. (goto-char pos)
  10758. (when (and indent (> indent 0))
  10759. (setq indent (make-string indent ?\ ))
  10760. (save-excursion
  10761. (org-beginning-of-dblock)
  10762. (forward-line 1)
  10763. (while (not (looking-at org-dblock-end-re))
  10764. (insert indent)
  10765. (beginning-of-line 2))
  10766. (when (looking-at org-dblock-end-re)
  10767. (and (looking-at "[ \t]+")
  10768. (replace-match ""))
  10769. (insert indent)))))))
  10770. (defun org-beginning-of-dblock ()
  10771. "Find the beginning of the dynamic block at point.
  10772. Error if there is no such block at point."
  10773. (let ((pos (point))
  10774. beg)
  10775. (end-of-line 1)
  10776. (if (and (re-search-backward org-dblock-start-re nil t)
  10777. (setq beg (match-beginning 0))
  10778. (re-search-forward org-dblock-end-re nil t)
  10779. (> (match-end 0) pos))
  10780. (goto-char beg)
  10781. (goto-char pos)
  10782. (error "Not in a dynamic block"))))
  10783. (defun org-update-all-dblocks ()
  10784. "Update all dynamic blocks in the buffer.
  10785. This function can be used in a hook."
  10786. (interactive)
  10787. (when (derived-mode-p 'org-mode)
  10788. (org-map-dblocks 'org-update-dblock)))
  10789. ;;;; Completion
  10790. (defun org-get-export-keywords ()
  10791. "Return a list of all currently understood export keywords.
  10792. Export keywords include options, block names, attributes and
  10793. keywords relative to each registered export back-end."
  10794. (delq nil
  10795. (let (keywords)
  10796. (mapc
  10797. (lambda (back-end)
  10798. (let ((props (cdr back-end)))
  10799. ;; Back-end name (for keywords, like #+LATEX:)
  10800. (push (upcase (symbol-name (car back-end))) keywords)
  10801. ;; Back-end options.
  10802. (mapc (lambda (option) (push (cadr option) keywords))
  10803. (plist-get (cdr back-end) :options-alist))))
  10804. (org-bound-and-true-p org-export-registered-backends))
  10805. keywords)))
  10806. (defconst org-options-keywords
  10807. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10808. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10809. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10810. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10811. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10812. (defcustom org-structure-template-alist
  10813. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10814. "<src lang=\"?\">\n\n</src>")
  10815. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10816. "<example>\n?\n</example>")
  10817. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10818. "<quote>\n?\n</quote>")
  10819. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10820. "<verse>\n?\n</verse>")
  10821. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10822. "<verbatim>\n?\n</verbatim>")
  10823. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10824. "<center>\n?\n</center>")
  10825. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10826. "<literal style=\"latex\">\n?\n</literal>")
  10827. ("L" "#+LaTeX: "
  10828. "<literal style=\"latex\">?</literal>")
  10829. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10830. "<literal style=\"html\">\n?\n</literal>")
  10831. ("H" "#+HTML: "
  10832. "<literal style=\"html\">?</literal>")
  10833. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10834. ("A" "#+ASCII: ")
  10835. ("i" "#+INDEX: ?"
  10836. "#+INDEX: ?")
  10837. ("I" "#+INCLUDE: %file ?"
  10838. "<include file=%file markup=\"?\">"))
  10839. "Structure completion elements.
  10840. This is a list of abbreviation keys and values. The value gets inserted
  10841. if you type `<' followed by the key and then press the completion key,
  10842. usually `M-TAB'. %file will be replaced by a file name after prompting
  10843. for the file using completion. The cursor will be placed at the position
  10844. of the `?` in the template.
  10845. There are two templates for each key, the first uses the original Org syntax,
  10846. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10847. the default when the /org-mtags.el/ module has been loaded. See also the
  10848. variable `org-mtags-prefer-muse-templates'."
  10849. :group 'org-completion
  10850. :type '(repeat
  10851. (string :tag "Key")
  10852. (string :tag "Template")
  10853. (string :tag "Muse Template")))
  10854. (defun org-try-structure-completion ()
  10855. "Try to complete a structure template before point.
  10856. This looks for strings like \"<e\" on an otherwise empty line and
  10857. expands them."
  10858. (let ((l (buffer-substring (point-at-bol) (point)))
  10859. a)
  10860. (when (and (looking-at "[ \t]*$")
  10861. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10862. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10863. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10864. (match-beginning 1)) a)
  10865. t)))
  10866. (defun org-complete-expand-structure-template (start cell)
  10867. "Expand a structure template."
  10868. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10869. (rpl (nth (if musep 2 1) cell))
  10870. (ind ""))
  10871. (delete-region start (point))
  10872. (when (string-match "\\`#\\+" rpl)
  10873. (cond
  10874. ((bolp))
  10875. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10876. (setq ind (buffer-substring (point-at-bol) (point))))
  10877. (t (newline))))
  10878. (setq start (point))
  10879. (if (string-match "%file" rpl)
  10880. (setq rpl (replace-match
  10881. (concat
  10882. "\""
  10883. (save-match-data
  10884. (abbreviate-file-name (read-file-name "Include file: ")))
  10885. "\"")
  10886. t t rpl)))
  10887. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10888. (concat "\n" ind)))
  10889. (insert rpl)
  10890. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10891. ;;;; TODO, DEADLINE, Comments
  10892. (defun org-toggle-comment ()
  10893. "Change the COMMENT state of an entry."
  10894. (interactive)
  10895. (save-excursion
  10896. (org-back-to-heading)
  10897. (let (case-fold-search)
  10898. (cond
  10899. ((looking-at (format org-heading-keyword-regexp-format
  10900. org-comment-string))
  10901. (goto-char (match-end 1))
  10902. (looking-at (concat " +" org-comment-string))
  10903. (replace-match "" t t)
  10904. (when (eolp) (insert " ")))
  10905. ((looking-at org-outline-regexp)
  10906. (goto-char (match-end 0))
  10907. (insert org-comment-string " "))))))
  10908. (defvar org-last-todo-state-is-todo nil
  10909. "This is non-nil when the last TODO state change led to a TODO state.
  10910. If the last change removed the TODO tag or switched to DONE, then
  10911. this is nil.")
  10912. (defvar org-setting-tags nil) ; dynamically skipped
  10913. (defvar org-todo-setup-filter-hook nil
  10914. "Hook for functions that pre-filter todo specs.
  10915. Each function takes a todo spec and returns either nil or the spec
  10916. transformed into canonical form." )
  10917. (defvar org-todo-get-default-hook nil
  10918. "Hook for functions that get a default item for todo.
  10919. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10920. nil or a string to be used for the todo mark." )
  10921. (defvar org-agenda-headline-snapshot-before-repeat)
  10922. (defun org-current-effective-time ()
  10923. "Return current time adjusted for `org-extend-today-until' variable."
  10924. (let* ((ct (org-current-time))
  10925. (dct (decode-time ct))
  10926. (ct1
  10927. (cond
  10928. (org-use-last-clock-out-time-as-effective-time
  10929. (or (org-clock-get-last-clock-out-time) ct))
  10930. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10931. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10932. (t ct))))
  10933. ct1))
  10934. (defun org-todo-yesterday (&optional arg)
  10935. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10936. (interactive "P")
  10937. (if (eq major-mode 'org-agenda-mode)
  10938. (apply 'org-agenda-todo-yesterday arg)
  10939. (let* ((hour (third (decode-time
  10940. (org-current-time))))
  10941. (org-extend-today-until (1+ hour)))
  10942. (org-todo arg))))
  10943. (defvar org-block-entry-blocking ""
  10944. "First entry preventing the TODO state change.")
  10945. (defun org-todo (&optional arg)
  10946. "Change the TODO state of an item.
  10947. The state of an item is given by a keyword at the start of the heading,
  10948. like
  10949. *** TODO Write paper
  10950. *** DONE Call mom
  10951. The different keywords are specified in the variable `org-todo-keywords'.
  10952. By default the available states are \"TODO\" and \"DONE\".
  10953. So for this example: when the item starts with TODO, it is changed to DONE.
  10954. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10955. DONE are present, add TODO at the beginning of the heading.
  10956. With \\[universal-argument] prefix arg, use completion to determine the new \
  10957. state.
  10958. With numeric prefix arg, switch to that state.
  10959. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10960. keywords (nextset).
  10961. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10962. With a numeric prefix arg of 0, inhibit note taking for the change.
  10963. For calling through lisp, arg is also interpreted in the following way:
  10964. 'none -> empty state
  10965. \"\"(empty string) -> switch to empty state
  10966. 'done -> switch to DONE
  10967. 'nextset -> switch to the next set of keywords
  10968. 'previousset -> switch to the previous set of keywords
  10969. \"WAITING\" -> switch to the specified keyword, but only if it
  10970. really is a member of `org-todo-keywords'."
  10971. (interactive "P")
  10972. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10973. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10974. 'region-start-level 'region))
  10975. org-loop-over-headlines-in-active-region)
  10976. (org-map-entries
  10977. `(org-todo ,arg)
  10978. org-loop-over-headlines-in-active-region
  10979. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10980. (if (equal arg '(16)) (setq arg 'nextset))
  10981. (let ((org-blocker-hook org-blocker-hook)
  10982. commentp
  10983. case-fold-search)
  10984. (when (equal arg '(64))
  10985. (setq arg nil org-blocker-hook nil))
  10986. (when (and org-blocker-hook
  10987. (or org-inhibit-blocking
  10988. (org-entry-get nil "NOBLOCKING")))
  10989. (setq org-blocker-hook nil))
  10990. (save-excursion
  10991. (catch 'exit
  10992. (org-back-to-heading t)
  10993. (when (looking-at (concat "^\\*+ " org-comment-string))
  10994. (org-toggle-comment)
  10995. (setq commentp t))
  10996. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10997. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10998. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10999. (let* ((match-data (match-data))
  11000. (startpos (point-at-bol))
  11001. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11002. (org-log-done org-log-done)
  11003. (org-log-repeat org-log-repeat)
  11004. (org-todo-log-states org-todo-log-states)
  11005. (org-inhibit-logging
  11006. (if (equal arg 0)
  11007. (progn (setq arg nil) 'note) org-inhibit-logging))
  11008. (this (match-string 1))
  11009. (hl-pos (match-beginning 0))
  11010. (head (org-get-todo-sequence-head this))
  11011. (ass (assoc head org-todo-kwd-alist))
  11012. (interpret (nth 1 ass))
  11013. (done-word (nth 3 ass))
  11014. (final-done-word (nth 4 ass))
  11015. (org-last-state (or this ""))
  11016. (completion-ignore-case t)
  11017. (member (member this org-todo-keywords-1))
  11018. (tail (cdr member))
  11019. (org-state (cond
  11020. ((and org-todo-key-trigger
  11021. (or (and (equal arg '(4))
  11022. (eq org-use-fast-todo-selection 'prefix))
  11023. (and (not arg) org-use-fast-todo-selection
  11024. (not (eq org-use-fast-todo-selection
  11025. 'prefix)))))
  11026. ;; Use fast selection
  11027. (org-fast-todo-selection))
  11028. ((and (equal arg '(4))
  11029. (or (not org-use-fast-todo-selection)
  11030. (not org-todo-key-trigger)))
  11031. ;; Read a state with completion
  11032. (org-icompleting-read
  11033. "State: " (mapcar 'list org-todo-keywords-1)
  11034. nil t))
  11035. ((eq arg 'right)
  11036. (if this
  11037. (if tail (car tail) nil)
  11038. (car org-todo-keywords-1)))
  11039. ((eq arg 'left)
  11040. (if (equal member org-todo-keywords-1)
  11041. nil
  11042. (if this
  11043. (nth (- (length org-todo-keywords-1)
  11044. (length tail) 2)
  11045. org-todo-keywords-1)
  11046. (org-last org-todo-keywords-1))))
  11047. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11048. (setq arg nil))) ; hack to fall back to cycling
  11049. (arg
  11050. ;; user or caller requests a specific state
  11051. (cond
  11052. ((equal arg "") nil)
  11053. ((eq arg 'none) nil)
  11054. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11055. ((eq arg 'nextset)
  11056. (or (car (cdr (member head org-todo-heads)))
  11057. (car org-todo-heads)))
  11058. ((eq arg 'previousset)
  11059. (let ((org-todo-heads (reverse org-todo-heads)))
  11060. (or (car (cdr (member head org-todo-heads)))
  11061. (car org-todo-heads))))
  11062. ((car (member arg org-todo-keywords-1)))
  11063. ((stringp arg)
  11064. (user-error "State `%s' not valid in this file" arg))
  11065. ((nth (1- (prefix-numeric-value arg))
  11066. org-todo-keywords-1))))
  11067. ((null member) (or head (car org-todo-keywords-1)))
  11068. ((equal this final-done-word) nil) ;; -> make empty
  11069. ((null tail) nil) ;; -> first entry
  11070. ((memq interpret '(type priority))
  11071. (if (eq this-command last-command)
  11072. (car tail)
  11073. (if (> (length tail) 0)
  11074. (or done-word (car org-done-keywords))
  11075. nil)))
  11076. (t
  11077. (car tail))))
  11078. (org-state (or
  11079. (run-hook-with-args-until-success
  11080. 'org-todo-get-default-hook org-state org-last-state)
  11081. org-state))
  11082. (next (if org-state (concat " " org-state " ") " "))
  11083. (change-plist (list :type 'todo-state-change :from this :to org-state
  11084. :position startpos))
  11085. dolog now-done-p)
  11086. (when org-blocker-hook
  11087. (setq org-last-todo-state-is-todo
  11088. (not (member this org-done-keywords)))
  11089. (unless (save-excursion
  11090. (save-match-data
  11091. (org-with-wide-buffer
  11092. (run-hook-with-args-until-failure
  11093. 'org-blocker-hook change-plist))))
  11094. (if (org-called-interactively-p 'interactive)
  11095. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11096. this org-state org-block-entry-blocking)
  11097. ;; fail silently
  11098. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11099. this org-state org-block-entry-blocking)
  11100. (throw 'exit nil))))
  11101. (store-match-data match-data)
  11102. (replace-match next t t)
  11103. (unless (pos-visible-in-window-p hl-pos)
  11104. (message "TODO state changed to %s" (org-trim next)))
  11105. (unless head
  11106. (setq head (org-get-todo-sequence-head org-state)
  11107. ass (assoc head org-todo-kwd-alist)
  11108. interpret (nth 1 ass)
  11109. done-word (nth 3 ass)
  11110. final-done-word (nth 4 ass)))
  11111. (when (memq arg '(nextset previousset))
  11112. (message "Keyword-Set %d/%d: %s"
  11113. (- (length org-todo-sets) -1
  11114. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11115. (length org-todo-sets)
  11116. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11117. (setq org-last-todo-state-is-todo
  11118. (not (member org-state org-done-keywords)))
  11119. (setq now-done-p (and (member org-state org-done-keywords)
  11120. (not (member this org-done-keywords))))
  11121. (and logging (org-local-logging logging))
  11122. (when (and (or org-todo-log-states org-log-done)
  11123. (not (eq org-inhibit-logging t))
  11124. (not (memq arg '(nextset previousset))))
  11125. ;; we need to look at recording a time and note
  11126. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11127. (nth 2 (assoc this org-todo-log-states))))
  11128. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11129. (setq dolog 'time))
  11130. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11131. (and org-state
  11132. (member org-state org-not-done-keywords)
  11133. (not (member this org-not-done-keywords))))
  11134. ;; This is now a todo state and was not one before
  11135. ;; If there was a CLOSED time stamp, get rid of it.
  11136. (org-add-planning-info nil nil 'closed))
  11137. (when (and now-done-p org-log-done)
  11138. ;; It is now done, and it was not done before
  11139. (org-add-planning-info 'closed (org-current-effective-time))
  11140. (if (and (not dolog) (eq 'note org-log-done))
  11141. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11142. (when (and org-state dolog)
  11143. ;; This is a non-nil state, and we need to log it
  11144. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11145. ;; Fixup tag positioning
  11146. (org-todo-trigger-tag-changes org-state)
  11147. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11148. (when org-provide-todo-statistics
  11149. (org-update-parent-todo-statistics))
  11150. (run-hooks 'org-after-todo-state-change-hook)
  11151. (if (and arg (not (member org-state org-done-keywords)))
  11152. (setq head (org-get-todo-sequence-head org-state)))
  11153. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11154. ;; Do we need to trigger a repeat?
  11155. (when now-done-p
  11156. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11157. ;; This is for the agenda, take a snapshot of the headline.
  11158. (save-match-data
  11159. (setq org-agenda-headline-snapshot-before-repeat
  11160. (org-get-heading))))
  11161. (org-auto-repeat-maybe org-state))
  11162. ;; Fixup cursor location if close to the keyword
  11163. (if (and (outline-on-heading-p)
  11164. (not (bolp))
  11165. (save-excursion (beginning-of-line 1)
  11166. (looking-at org-todo-line-regexp))
  11167. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11168. (progn
  11169. (goto-char (or (match-end 2) (match-end 1)))
  11170. (and (looking-at " ") (just-one-space))))
  11171. (when org-trigger-hook
  11172. (save-excursion
  11173. (run-hook-with-args 'org-trigger-hook change-plist)))
  11174. (when commentp (org-toggle-comment))))))))
  11175. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11176. "Block turning an entry into a TODO, using the hierarchy.
  11177. This checks whether the current task should be blocked from state
  11178. changes. Such blocking occurs when:
  11179. 1. The task has children which are not all in a completed state.
  11180. 2. A task has a parent with the property :ORDERED:, and there
  11181. are siblings prior to the current task with incomplete
  11182. status.
  11183. 3. The parent of the task is blocked because it has siblings that should
  11184. be done first, or is child of a block grandparent TODO entry."
  11185. (if (not org-enforce-todo-dependencies)
  11186. t ; if locally turned off don't block
  11187. (catch 'dont-block
  11188. ;; If this is not a todo state change, or if this entry is already DONE,
  11189. ;; do not block
  11190. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11191. (member (plist-get change-plist :from)
  11192. (cons 'done org-done-keywords))
  11193. (member (plist-get change-plist :to)
  11194. (cons 'todo org-not-done-keywords))
  11195. (not (plist-get change-plist :to)))
  11196. (throw 'dont-block t))
  11197. ;; If this task has children, and any are undone, it's blocked
  11198. (save-excursion
  11199. (org-back-to-heading t)
  11200. (let ((this-level (funcall outline-level)))
  11201. (outline-next-heading)
  11202. (let ((child-level (funcall outline-level)))
  11203. (while (and (not (eobp))
  11204. (> child-level this-level))
  11205. ;; this todo has children, check whether they are all
  11206. ;; completed
  11207. (if (and (not (org-entry-is-done-p))
  11208. (org-entry-is-todo-p))
  11209. (progn (setq org-block-entry-blocking (org-get-heading))
  11210. (throw 'dont-block nil)))
  11211. (outline-next-heading)
  11212. (setq child-level (funcall outline-level))))))
  11213. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11214. ;; any previous siblings are undone, it's blocked
  11215. (save-excursion
  11216. (org-back-to-heading t)
  11217. (let* ((pos (point))
  11218. (parent-pos (and (org-up-heading-safe) (point))))
  11219. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11220. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11221. (forward-line 1)
  11222. (re-search-forward org-not-done-heading-regexp pos t))
  11223. (setq org-block-entry-blocking (match-string 0))
  11224. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11225. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11226. (while t
  11227. (goto-char parent-pos)
  11228. (if (not (looking-at org-not-done-heading-regexp))
  11229. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11230. (setq pos (point))
  11231. (setq parent-pos (and (org-up-heading-safe) (point)))
  11232. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11233. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11234. (forward-line 1)
  11235. (re-search-forward org-not-done-heading-regexp pos t)
  11236. (setq org-block-entry-blocking (org-get-heading)))
  11237. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11238. (defcustom org-track-ordered-property-with-tag nil
  11239. "Should the ORDERED property also be shown as a tag?
  11240. The ORDERED property decides if an entry should require subtasks to be
  11241. completed in sequence. Since a property is not very visible, setting
  11242. this option means that toggling the ORDERED property with the command
  11243. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11244. not relevant for the behavior, but it makes things more visible.
  11245. Note that toggling the tag with tags commands will not change the property
  11246. and therefore not influence behavior!
  11247. This can be t, meaning the tag ORDERED should be used, It can also be a
  11248. string to select a different tag for this task."
  11249. :group 'org-todo
  11250. :type '(choice
  11251. (const :tag "No tracking" nil)
  11252. (const :tag "Track with ORDERED tag" t)
  11253. (string :tag "Use other tag")))
  11254. (defun org-toggle-ordered-property ()
  11255. "Toggle the ORDERED property of the current entry.
  11256. For better visibility, you can track the value of this property with a tag.
  11257. See variable `org-track-ordered-property-with-tag'."
  11258. (interactive)
  11259. (let* ((t1 org-track-ordered-property-with-tag)
  11260. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11261. (save-excursion
  11262. (org-back-to-heading)
  11263. (if (org-entry-get nil "ORDERED")
  11264. (progn
  11265. (org-delete-property "ORDERED" "PROPERTIES")
  11266. (and tag (org-toggle-tag tag 'off))
  11267. (message "Subtasks can be completed in arbitrary order"))
  11268. (org-entry-put nil "ORDERED" "t")
  11269. (and tag (org-toggle-tag tag 'on))
  11270. (message "Subtasks must be completed in sequence")))))
  11271. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11272. (defun org-block-todo-from-checkboxes (change-plist)
  11273. "Block turning an entry into a TODO, using checkboxes.
  11274. This checks whether the current task should be blocked from state
  11275. changes because there are unchecked boxes in this entry."
  11276. (if (not org-enforce-todo-checkbox-dependencies)
  11277. t ; if locally turned off don't block
  11278. (catch 'dont-block
  11279. ;; If this is not a todo state change, or if this entry is already DONE,
  11280. ;; do not block
  11281. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11282. (member (plist-get change-plist :from)
  11283. (cons 'done org-done-keywords))
  11284. (member (plist-get change-plist :to)
  11285. (cons 'todo org-not-done-keywords))
  11286. (not (plist-get change-plist :to)))
  11287. (throw 'dont-block t))
  11288. ;; If this task has checkboxes that are not checked, it's blocked
  11289. (save-excursion
  11290. (org-back-to-heading t)
  11291. (let ((beg (point)) end)
  11292. (outline-next-heading)
  11293. (setq end (point))
  11294. (goto-char beg)
  11295. (if (org-list-search-forward
  11296. (concat (org-item-beginning-re)
  11297. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11298. "\\[[- ]\\]")
  11299. end t)
  11300. (progn
  11301. (if (boundp 'org-blocked-by-checkboxes)
  11302. (setq org-blocked-by-checkboxes t))
  11303. (throw 'dont-block nil)))))
  11304. t))) ; do not block
  11305. (defun org-entry-blocked-p ()
  11306. "Is the current entry blocked?"
  11307. (org-with-silent-modifications
  11308. (if (org-entry-get nil "NOBLOCKING")
  11309. nil ;; Never block this entry
  11310. (not (run-hook-with-args-until-failure
  11311. 'org-blocker-hook
  11312. (list :type 'todo-state-change
  11313. :position (point)
  11314. :from 'todo
  11315. :to 'done))))))
  11316. (defun org-update-statistics-cookies (all)
  11317. "Update the statistics cookie, either from TODO or from checkboxes.
  11318. This should be called with the cursor in a line with a statistics cookie."
  11319. (interactive "P")
  11320. (if all
  11321. (progn
  11322. (org-update-checkbox-count 'all)
  11323. (org-map-entries 'org-update-parent-todo-statistics))
  11324. (if (not (org-at-heading-p))
  11325. (org-update-checkbox-count)
  11326. (let ((pos (point-marker))
  11327. end l1 l2)
  11328. (ignore-errors (org-back-to-heading t))
  11329. (if (not (org-at-heading-p))
  11330. (org-update-checkbox-count)
  11331. (setq l1 (org-outline-level))
  11332. (setq end (save-excursion
  11333. (outline-next-heading)
  11334. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11335. (point)))
  11336. (if (and (save-excursion
  11337. (re-search-forward
  11338. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11339. (not (save-excursion (re-search-forward
  11340. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11341. (org-update-checkbox-count)
  11342. (if (and l2 (> l2 l1))
  11343. (progn
  11344. (goto-char end)
  11345. (org-update-parent-todo-statistics))
  11346. (goto-char pos)
  11347. (beginning-of-line 1)
  11348. (while (re-search-forward
  11349. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11350. (point-at-eol) t)
  11351. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11352. (goto-char pos)
  11353. (move-marker pos nil)))))
  11354. (defvar org-entry-property-inherited-from) ;; defined below
  11355. (defun org-update-parent-todo-statistics ()
  11356. "Update any statistics cookie in the parent of the current headline.
  11357. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11358. the entire subtree and this will travel up the hierarchy and update
  11359. statistics everywhere."
  11360. (let* ((prop (save-excursion (org-up-heading-safe)
  11361. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11362. (recursive (or (not org-hierarchical-todo-statistics)
  11363. (and prop (string-match "\\<recursive\\>" prop))))
  11364. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11365. 0))
  11366. (first t)
  11367. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11368. level ltoggle l1 new ndel
  11369. (cnt-all 0) (cnt-done 0) is-percent kwd
  11370. checkbox-beg ov ovs ove cookie-present)
  11371. (catch 'exit
  11372. (save-excursion
  11373. (beginning-of-line 1)
  11374. (setq ltoggle (funcall outline-level))
  11375. ;; Three situations are to consider:
  11376. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11377. ;; to the top-level ancestor on the headline;
  11378. ;; 2. If parent has "recursive" property, repeat up to the
  11379. ;; headline setting that property, taking inheritance into
  11380. ;; account;
  11381. ;; 3. Else, move up to direct parent and proceed only once.
  11382. (while (and (setq level (org-up-heading-safe))
  11383. (or recursive first)
  11384. (>= (point) lim))
  11385. (setq first nil cookie-present nil)
  11386. (unless (and level
  11387. (not (string-match
  11388. "\\<checkbox\\>"
  11389. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11390. "")))))
  11391. (throw 'exit nil))
  11392. (while (re-search-forward box-re (point-at-eol) t)
  11393. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11394. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11395. (save-match-data
  11396. (unless (outline-next-heading) (throw 'exit nil))
  11397. (while (and (looking-at org-complex-heading-regexp)
  11398. (> (setq l1 (length (match-string 1))) level))
  11399. (setq kwd (and (or recursive (= l1 ltoggle))
  11400. (match-string 2)))
  11401. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11402. (and (listp org-provide-todo-statistics)
  11403. (or (member kwd org-provide-todo-statistics)
  11404. (member kwd org-done-keywords))))
  11405. (setq cnt-all (1+ cnt-all))
  11406. (if (eq org-provide-todo-statistics t)
  11407. (and kwd (setq cnt-all (1+ cnt-all)))))
  11408. (and (member kwd org-done-keywords)
  11409. (setq cnt-done (1+ cnt-done)))
  11410. (outline-next-heading)))
  11411. (setq new
  11412. (if is-percent
  11413. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11414. (format "[%d/%d]" cnt-done cnt-all))
  11415. ndel (- (match-end 0) checkbox-beg))
  11416. ;; handle overlays when updating cookie from column view
  11417. (when (setq ov (car (overlays-at checkbox-beg)))
  11418. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11419. (delete-overlay ov))
  11420. (goto-char checkbox-beg)
  11421. (insert new)
  11422. (delete-region (point) (+ (point) ndel))
  11423. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11424. (when ov (move-overlay ov ovs ove)))
  11425. (when cookie-present
  11426. (run-hook-with-args 'org-after-todo-statistics-hook
  11427. cnt-done (- cnt-all cnt-done))))))
  11428. (run-hooks 'org-todo-statistics-hook)))
  11429. (defvar org-after-todo-statistics-hook nil
  11430. "Hook that is called after a TODO statistics cookie has been updated.
  11431. Each function is called with two arguments: the number of not-done entries
  11432. and the number of done entries.
  11433. For example, the following function, when added to this hook, will switch
  11434. an entry to DONE when all children are done, and back to TODO when new
  11435. entries are set to a TODO status. Note that this hook is only called
  11436. when there is a statistics cookie in the headline!
  11437. (defun org-summary-todo (n-done n-not-done)
  11438. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11439. (let (org-log-done org-log-states) ; turn off logging
  11440. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11441. ")
  11442. (defvar org-todo-statistics-hook nil
  11443. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11444. This hook runs even if there is no statistics cookie present, in which case
  11445. `org-after-todo-statistics-hook' would not run.")
  11446. (defun org-todo-trigger-tag-changes (state)
  11447. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11448. (let ((l org-todo-state-tags-triggers)
  11449. changes)
  11450. (when (or (not state) (equal state ""))
  11451. (setq changes (append changes (cdr (assoc "" l)))))
  11452. (when (and (stringp state) (> (length state) 0))
  11453. (setq changes (append changes (cdr (assoc state l)))))
  11454. (when (member state org-not-done-keywords)
  11455. (setq changes (append changes (cdr (assoc 'todo l)))))
  11456. (when (member state org-done-keywords)
  11457. (setq changes (append changes (cdr (assoc 'done l)))))
  11458. (dolist (c changes)
  11459. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11460. (defun org-local-logging (value)
  11461. "Get logging settings from a property VALUE."
  11462. (let* (words w a)
  11463. ;; directly set the variables, they are already local.
  11464. (setq org-log-done nil
  11465. org-log-repeat nil
  11466. org-todo-log-states nil)
  11467. (setq words (org-split-string value))
  11468. (while (setq w (pop words))
  11469. (cond
  11470. ((setq a (assoc w org-startup-options))
  11471. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11472. (set (nth 1 a) (nth 2 a))))
  11473. ((setq a (org-extract-log-state-settings w))
  11474. (and (member (car a) org-todo-keywords-1)
  11475. (push a org-todo-log-states)))))))
  11476. (defun org-get-todo-sequence-head (kwd)
  11477. "Return the head of the TODO sequence to which KWD belongs.
  11478. If KWD is not set, check if there is a text property remembering the
  11479. right sequence."
  11480. (let (p)
  11481. (cond
  11482. ((not kwd)
  11483. (or (get-text-property (point-at-bol) 'org-todo-head)
  11484. (progn
  11485. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11486. nil (point-at-eol)))
  11487. (get-text-property p 'org-todo-head))))
  11488. ((not (member kwd org-todo-keywords-1))
  11489. (car org-todo-keywords-1))
  11490. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11491. (defun org-fast-todo-selection ()
  11492. "Fast TODO keyword selection with single keys.
  11493. Returns the new TODO keyword, or nil if no state change should occur."
  11494. (let* ((fulltable org-todo-key-alist)
  11495. (done-keywords org-done-keywords) ;; needed for the faces.
  11496. (maxlen (apply 'max (mapcar
  11497. (lambda (x)
  11498. (if (stringp (car x)) (string-width (car x)) 0))
  11499. fulltable)))
  11500. (expert nil)
  11501. (fwidth (+ maxlen 3 1 3))
  11502. (ncol (/ (- (window-width) 4) fwidth))
  11503. tg cnt e c tbl
  11504. groups ingroup)
  11505. (save-excursion
  11506. (save-window-excursion
  11507. (if expert
  11508. (set-buffer (get-buffer-create " *Org todo*"))
  11509. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11510. (erase-buffer)
  11511. (org-set-local 'org-done-keywords done-keywords)
  11512. (setq tbl fulltable cnt 0)
  11513. (while (setq e (pop tbl))
  11514. (cond
  11515. ((equal e '(:startgroup))
  11516. (push '() groups) (setq ingroup t)
  11517. (when (not (= cnt 0))
  11518. (setq cnt 0)
  11519. (insert "\n"))
  11520. (insert "{ "))
  11521. ((equal e '(:endgroup))
  11522. (setq ingroup nil cnt 0)
  11523. (insert "}\n"))
  11524. ((equal e '(:newline))
  11525. (when (not (= cnt 0))
  11526. (setq cnt 0)
  11527. (insert "\n")
  11528. (setq e (car tbl))
  11529. (while (equal (car tbl) '(:newline))
  11530. (insert "\n")
  11531. (setq tbl (cdr tbl)))))
  11532. (t
  11533. (setq tg (car e) c (cdr e))
  11534. (if ingroup (push tg (car groups)))
  11535. (setq tg (org-add-props tg nil 'face
  11536. (org-get-todo-face tg)))
  11537. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11538. (insert "[" c "] " tg (make-string
  11539. (- fwidth 4 (length tg)) ?\ ))
  11540. (when (= (setq cnt (1+ cnt)) ncol)
  11541. (insert "\n")
  11542. (if ingroup (insert " "))
  11543. (setq cnt 0)))))
  11544. (insert "\n")
  11545. (goto-char (point-min))
  11546. (if (not expert) (org-fit-window-to-buffer))
  11547. (message "[a-z..]:Set [SPC]:clear")
  11548. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11549. (cond
  11550. ((or (= c ?\C-g)
  11551. (and (= c ?q) (not (rassoc c fulltable))))
  11552. (setq quit-flag t))
  11553. ((= c ?\ ) nil)
  11554. ((setq e (rassoc c fulltable) tg (car e))
  11555. tg)
  11556. (t (setq quit-flag t)))))))
  11557. (defun org-entry-is-todo-p ()
  11558. (member (org-get-todo-state) org-not-done-keywords))
  11559. (defun org-entry-is-done-p ()
  11560. (member (org-get-todo-state) org-done-keywords))
  11561. (defun org-get-todo-state ()
  11562. "Return the TODO keyword of the current subtree."
  11563. (save-excursion
  11564. (org-back-to-heading t)
  11565. (and (looking-at org-todo-line-regexp)
  11566. (match-end 2)
  11567. (match-string 2))))
  11568. (defun org-at-date-range-p (&optional inactive-ok)
  11569. "Is the cursor inside a date range?"
  11570. (interactive)
  11571. (save-excursion
  11572. (catch 'exit
  11573. (let ((pos (point)))
  11574. (skip-chars-backward "^[<\r\n")
  11575. (skip-chars-backward "<[")
  11576. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11577. (>= (match-end 0) pos)
  11578. (throw 'exit t))
  11579. (skip-chars-backward "^<[\r\n")
  11580. (skip-chars-backward "<[")
  11581. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11582. (>= (match-end 0) pos)
  11583. (throw 'exit t)))
  11584. nil)))
  11585. (defun org-get-repeat (&optional tagline)
  11586. "Check if there is a deadline/schedule with repeater in this entry."
  11587. (save-match-data
  11588. (save-excursion
  11589. (org-back-to-heading t)
  11590. (and (re-search-forward (if tagline
  11591. (concat tagline "\\s-*" org-repeat-re)
  11592. org-repeat-re)
  11593. (org-entry-end-position) t)
  11594. (match-string-no-properties 1)))))
  11595. (defvar org-last-changed-timestamp)
  11596. (defvar org-last-inserted-timestamp)
  11597. (defvar org-log-post-message)
  11598. (defvar org-log-note-purpose)
  11599. (defvar org-log-note-how)
  11600. (defvar org-log-note-extra)
  11601. (defun org-auto-repeat-maybe (done-word)
  11602. "Check if the current headline contains a repeated deadline/schedule.
  11603. If yes, set TODO state back to what it was and change the base date
  11604. of repeating deadline/scheduled time stamps to new date.
  11605. This function is run automatically after each state change to a DONE state."
  11606. ;; last-state is dynamically scoped into this function
  11607. (let* ((repeat (org-get-repeat))
  11608. (aa (assoc org-last-state org-todo-kwd-alist))
  11609. (interpret (nth 1 aa))
  11610. (head (nth 2 aa))
  11611. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11612. (msg "Entry repeats: ")
  11613. (org-log-done nil)
  11614. (org-todo-log-states nil)
  11615. re type n what ts time to-state)
  11616. (when repeat
  11617. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11618. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11619. org-todo-repeat-to-state))
  11620. (unless (and to-state (member to-state org-todo-keywords-1))
  11621. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11622. (org-todo to-state)
  11623. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11624. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11625. (org-time-stamp-format t t))))
  11626. (when org-log-repeat
  11627. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11628. (memq 'org-add-log-note post-command-hook))
  11629. ;; OK, we are already setup for some record
  11630. (if (eq org-log-repeat 'note)
  11631. ;; make sure we take a note, not only a time stamp
  11632. (setq org-log-note-how 'note))
  11633. ;; Set up for taking a record
  11634. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11635. org-last-state
  11636. 'findpos org-log-repeat)))
  11637. (org-back-to-heading t)
  11638. (org-add-planning-info nil nil 'closed)
  11639. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11640. org-deadline-time-regexp "\\)\\|\\("
  11641. org-ts-regexp "\\)"))
  11642. (while (re-search-forward
  11643. re (save-excursion (outline-next-heading) (point)) t)
  11644. (setq type (if (match-end 1) org-scheduled-string
  11645. (if (match-end 3) org-deadline-string "Plain:"))
  11646. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11647. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11648. (setq n (string-to-number (match-string 2 ts))
  11649. what (match-string 3 ts))
  11650. (if (equal what "w") (setq n (* n 7) what "d"))
  11651. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11652. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11653. ;; Preparation, see if we need to modify the start date for the change
  11654. (when (match-end 1)
  11655. (setq time (save-match-data (org-time-string-to-time ts)))
  11656. (cond
  11657. ((equal (match-string 1 ts) ".")
  11658. ;; Shift starting date to today
  11659. (org-timestamp-change
  11660. (- (org-today) (time-to-days time))
  11661. 'day))
  11662. ((equal (match-string 1 ts) "+")
  11663. (let ((nshiftmax 10) (nshift 0))
  11664. (while (or (= nshift 0)
  11665. (<= (time-to-days time)
  11666. (time-to-days (current-time))))
  11667. (when (= (incf nshift) nshiftmax)
  11668. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11669. (error "Abort")))
  11670. (org-timestamp-change n (cdr (assoc what whata)))
  11671. (org-at-timestamp-p t)
  11672. (setq ts (match-string 1))
  11673. (setq time (save-match-data (org-time-string-to-time ts)))))
  11674. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11675. ;; rematch, so that we have everything in place for the real shift
  11676. (org-at-timestamp-p t)
  11677. (setq ts (match-string 1))
  11678. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11679. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11680. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11681. (setq org-log-post-message msg)
  11682. (message "%s" msg))))
  11683. (defun org-show-todo-tree (arg)
  11684. "Make a compact tree which shows all headlines marked with TODO.
  11685. The tree will show the lines where the regexp matches, and all higher
  11686. headlines above the match.
  11687. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11688. With a numeric prefix N, construct a sparse tree for the Nth element
  11689. of `org-todo-keywords-1'."
  11690. (interactive "P")
  11691. (let ((case-fold-search nil)
  11692. (kwd-re
  11693. (cond ((null arg) org-not-done-regexp)
  11694. ((equal arg '(4))
  11695. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11696. (mapcar 'list org-todo-keywords-1))))
  11697. (concat "\\("
  11698. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11699. "\\)\\>")))
  11700. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11701. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11702. org-todo-keywords-1)))
  11703. (t (user-error "Invalid prefix argument: %s" arg)))))
  11704. (message "%d TODO entries found"
  11705. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11706. (defun org-deadline (arg &optional time)
  11707. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11708. With one universal prefix argument, remove any deadline from the item.
  11709. With two universal prefix arguments, prompt for a warning delay.
  11710. With argument TIME, set the deadline at the corresponding date. TIME
  11711. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11712. (interactive "P")
  11713. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11714. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11715. 'region-start-level 'region))
  11716. org-loop-over-headlines-in-active-region)
  11717. (org-map-entries
  11718. `(org-deadline ',arg ,time)
  11719. org-loop-over-headlines-in-active-region
  11720. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11721. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11722. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11723. (repeater (and old-date
  11724. (string-match
  11725. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11726. old-date)
  11727. (match-string 1 old-date))))
  11728. (cond
  11729. ((equal arg '(4))
  11730. (when (and old-date org-log-redeadline)
  11731. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11732. org-log-redeadline))
  11733. (org-remove-timestamp-with-keyword org-deadline-string)
  11734. (message "Item no longer has a deadline."))
  11735. ((equal arg '(16))
  11736. (save-excursion
  11737. (if (re-search-forward
  11738. org-deadline-time-regexp
  11739. (save-excursion (outline-next-heading) (point)) t)
  11740. (let* ((rpl0 (match-string 1))
  11741. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11742. (replace-match
  11743. (concat org-deadline-string
  11744. " <" rpl
  11745. (format " -%dd"
  11746. (abs
  11747. (- (time-to-days
  11748. (save-match-data
  11749. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11750. (time-to-days old-date-time))))
  11751. ">") t t))
  11752. (user-error "No deadline information to update"))))
  11753. (t
  11754. (org-add-planning-info 'deadline time 'closed)
  11755. (when (and old-date org-log-redeadline
  11756. (not (equal old-date
  11757. (substring org-last-inserted-timestamp 1 -1))))
  11758. (org-add-log-setup 'redeadline nil old-date 'findpos
  11759. org-log-redeadline))
  11760. (when repeater
  11761. (save-excursion
  11762. (org-back-to-heading t)
  11763. (when (re-search-forward (concat org-deadline-string " "
  11764. org-last-inserted-timestamp)
  11765. (save-excursion
  11766. (outline-next-heading) (point)) t)
  11767. (goto-char (1- (match-end 0)))
  11768. (insert " " repeater)
  11769. (setq org-last-inserted-timestamp
  11770. (concat (substring org-last-inserted-timestamp 0 -1)
  11771. " " repeater
  11772. (substring org-last-inserted-timestamp -1))))))
  11773. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11774. (defun org-schedule (arg &optional time)
  11775. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11776. With one universal prefix argument, remove any scheduling date from the item.
  11777. With two universal prefix arguments, prompt for a delay cookie.
  11778. With argument TIME, scheduled at the corresponding date. TIME can
  11779. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11780. (interactive "P")
  11781. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11782. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11783. 'region-start-level 'region))
  11784. org-loop-over-headlines-in-active-region)
  11785. (org-map-entries
  11786. `(org-schedule ',arg ,time)
  11787. org-loop-over-headlines-in-active-region
  11788. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11789. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11790. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11791. (repeater (and old-date
  11792. (string-match
  11793. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11794. old-date)
  11795. (match-string 1 old-date))))
  11796. (cond
  11797. ((equal arg '(4))
  11798. (progn
  11799. (when (and old-date org-log-reschedule)
  11800. (org-add-log-setup 'delschedule nil old-date 'findpos
  11801. org-log-reschedule))
  11802. (org-remove-timestamp-with-keyword org-scheduled-string)
  11803. (message "Item is no longer scheduled.")))
  11804. ((equal arg '(16))
  11805. (save-excursion
  11806. (if (re-search-forward
  11807. org-scheduled-time-regexp
  11808. (save-excursion (outline-next-heading) (point)) t)
  11809. (let* ((rpl0 (match-string 1))
  11810. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11811. (replace-match
  11812. (concat org-scheduled-string
  11813. " <" rpl
  11814. (format " -%dd"
  11815. (abs
  11816. (- (time-to-days
  11817. (save-match-data
  11818. (org-read-date nil t nil "Delay until" old-date-time)))
  11819. (time-to-days old-date-time))))
  11820. ">") t t))
  11821. (user-error "No scheduled information to update"))))
  11822. (t
  11823. (org-add-planning-info 'scheduled time 'closed)
  11824. (when (and old-date org-log-reschedule
  11825. (not (equal old-date
  11826. (substring org-last-inserted-timestamp 1 -1))))
  11827. (org-add-log-setup 'reschedule nil old-date 'findpos
  11828. org-log-reschedule))
  11829. (when repeater
  11830. (save-excursion
  11831. (org-back-to-heading t)
  11832. (when (re-search-forward (concat org-scheduled-string " "
  11833. org-last-inserted-timestamp)
  11834. (save-excursion
  11835. (outline-next-heading) (point)) t)
  11836. (goto-char (1- (match-end 0)))
  11837. (insert " " repeater)
  11838. (setq org-last-inserted-timestamp
  11839. (concat (substring org-last-inserted-timestamp 0 -1)
  11840. " " repeater
  11841. (substring org-last-inserted-timestamp -1))))))
  11842. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11843. (defun org-get-scheduled-time (pom &optional inherit)
  11844. "Get the scheduled time as a time tuple, of a format suitable
  11845. for calling org-schedule with, or if there is no scheduling,
  11846. returns nil."
  11847. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11848. (when time
  11849. (apply 'encode-time (org-parse-time-string time)))))
  11850. (defun org-get-deadline-time (pom &optional inherit)
  11851. "Get the deadline as a time tuple, of a format suitable for
  11852. calling org-deadline with, or if there is no scheduling, returns
  11853. nil."
  11854. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11855. (when time
  11856. (apply 'encode-time (org-parse-time-string time)))))
  11857. (defun org-remove-timestamp-with-keyword (keyword)
  11858. "Remove all time stamps with KEYWORD in the current entry."
  11859. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11860. beg)
  11861. (save-excursion
  11862. (org-back-to-heading t)
  11863. (setq beg (point))
  11864. (outline-next-heading)
  11865. (while (re-search-backward re beg t)
  11866. (replace-match "")
  11867. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11868. (equal (char-before) ?\ ))
  11869. (backward-delete-char 1)
  11870. (if (string-match "^[ \t]*$" (buffer-substring
  11871. (point-at-bol) (point-at-eol)))
  11872. (delete-region (point-at-bol)
  11873. (min (point-max) (1+ (point-at-eol))))))))))
  11874. (defun org-add-planning-info (what &optional time &rest remove)
  11875. "Insert new timestamp with keyword in the line directly after the headline.
  11876. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11877. If non is given, the user is prompted for a date.
  11878. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11879. be removed."
  11880. (interactive)
  11881. (let (org-time-was-given org-end-time-was-given ts
  11882. end default-time default-input)
  11883. (catch 'exit
  11884. (when (and (memq what '(scheduled deadline))
  11885. (or (not time)
  11886. (and (stringp time)
  11887. (string-match "^[-+]+[0-9]" time))))
  11888. ;; Try to get a default date/time from existing timestamp
  11889. (save-excursion
  11890. (org-back-to-heading t)
  11891. (setq end (save-excursion (outline-next-heading) (point)))
  11892. (when (re-search-forward (if (eq what 'scheduled)
  11893. org-scheduled-time-regexp
  11894. org-deadline-time-regexp)
  11895. end t)
  11896. (setq ts (match-string 1)
  11897. default-time
  11898. (apply 'encode-time (org-parse-time-string ts))
  11899. default-input (and ts (org-get-compact-tod ts))))))
  11900. (when what
  11901. (setq time
  11902. (if (stringp time)
  11903. ;; This is a string (relative or absolute), set proper date
  11904. (apply 'encode-time
  11905. (org-read-date-analyze
  11906. time default-time (decode-time default-time)))
  11907. ;; If necessary, get the time from the user
  11908. (or time (org-read-date nil 'to-time nil nil
  11909. default-time default-input)))))
  11910. (when (and org-insert-labeled-timestamps-at-point
  11911. (member what '(scheduled deadline)))
  11912. (insert
  11913. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11914. (org-insert-time-stamp time org-time-was-given
  11915. nil nil nil (list org-end-time-was-given))
  11916. (setq what nil))
  11917. (save-excursion
  11918. (save-restriction
  11919. (let (col list elt ts buffer-invisibility-spec)
  11920. (org-back-to-heading t)
  11921. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11922. (goto-char (match-end 1))
  11923. (setq col (current-column))
  11924. (goto-char (match-end 0))
  11925. (if (eobp) (insert "\n") (forward-char 1))
  11926. (when (and (not what)
  11927. (not (looking-at
  11928. (concat "[ \t]*"
  11929. org-keyword-time-not-clock-regexp))))
  11930. ;; Nothing to add, nothing to remove...... :-)
  11931. (throw 'exit nil))
  11932. (if (and (not (looking-at org-outline-regexp))
  11933. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11934. "[^\r\n]*"))
  11935. (not (equal (match-string 1) org-clock-string)))
  11936. (narrow-to-region (match-beginning 0) (match-end 0))
  11937. (insert-before-markers "\n")
  11938. (backward-char 1)
  11939. (narrow-to-region (point) (point))
  11940. (and org-adapt-indentation (org-indent-to-column col)))
  11941. ;; Check if we have to remove something.
  11942. (setq list (cons what remove))
  11943. (while list
  11944. (setq elt (pop list))
  11945. (when (or (and (eq elt 'scheduled)
  11946. (re-search-forward org-scheduled-time-regexp nil t))
  11947. (and (eq elt 'deadline)
  11948. (re-search-forward org-deadline-time-regexp nil t))
  11949. (and (eq elt 'closed)
  11950. (re-search-forward org-closed-time-regexp nil t)))
  11951. (replace-match "")
  11952. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11953. (and (looking-at "[ \t]+") (replace-match ""))
  11954. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11955. (when what
  11956. (insert
  11957. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11958. (cond ((eq what 'scheduled) org-scheduled-string)
  11959. ((eq what 'deadline) org-deadline-string)
  11960. ((eq what 'closed) org-closed-string))
  11961. " ")
  11962. (setq ts (org-insert-time-stamp
  11963. time
  11964. (or org-time-was-given
  11965. (and (eq what 'closed) org-log-done-with-time))
  11966. (eq what 'closed)
  11967. nil nil (list org-end-time-was-given)))
  11968. (insert
  11969. (if (not (or (bolp) (eq (char-before) ?\ )
  11970. (memq (char-after) '(32 10))
  11971. (eobp))) " " ""))
  11972. (end-of-line 1))
  11973. (goto-char (point-min))
  11974. (widen)
  11975. (if (and (looking-at "[ \t]*\n")
  11976. (equal (char-before) ?\n))
  11977. (delete-region (1- (point)) (point-at-eol)))
  11978. ts))))))
  11979. (defvar org-log-note-marker (make-marker))
  11980. (defvar org-log-note-purpose nil)
  11981. (defvar org-log-note-state nil)
  11982. (defvar org-log-note-previous-state nil)
  11983. (defvar org-log-note-how nil)
  11984. (defvar org-log-note-extra nil)
  11985. (defvar org-log-note-window-configuration nil)
  11986. (defvar org-log-note-return-to (make-marker))
  11987. (defvar org-log-note-effective-time nil
  11988. "Remembered current time so that dynamically scoped
  11989. `org-extend-today-until' affects tha timestamps in state change
  11990. log")
  11991. (defvar org-log-post-message nil
  11992. "Message to be displayed after a log note has been stored.
  11993. The auto-repeater uses this.")
  11994. (defun org-add-note ()
  11995. "Add a note to the current entry.
  11996. This is done in the same way as adding a state change note."
  11997. (interactive)
  11998. (org-add-log-setup 'note nil nil 'findpos nil))
  11999. (defvar org-property-end-re)
  12000. (defun org-add-log-setup (&optional purpose state prev-state
  12001. findpos how extra)
  12002. "Set up the post command hook to take a note.
  12003. If this is about to TODO state change, the new state is expected in STATE.
  12004. When FINDPOS is non-nil, find the correct position for the note in
  12005. the current entry. If not, assume that it can be inserted at point.
  12006. HOW is an indicator what kind of note should be created.
  12007. EXTRA is additional text that will be inserted into the notes buffer."
  12008. (let* ((org-log-into-drawer (org-log-into-drawer))
  12009. (drawer (cond ((stringp org-log-into-drawer)
  12010. org-log-into-drawer)
  12011. (org-log-into-drawer "LOGBOOK"))))
  12012. (save-restriction
  12013. (save-excursion
  12014. (when findpos
  12015. (org-back-to-heading t)
  12016. (narrow-to-region (point) (save-excursion
  12017. (outline-next-heading) (point)))
  12018. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12019. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12020. "[^\r\n]*\\)?"))
  12021. (goto-char (match-end 0))
  12022. (cond
  12023. (drawer
  12024. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12025. nil t)
  12026. (progn
  12027. (goto-char (match-end 0))
  12028. (or org-log-states-order-reversed
  12029. (and (re-search-forward org-property-end-re nil t)
  12030. (goto-char (1- (match-beginning 0))))))
  12031. (insert "\n:" drawer ":\n:END:")
  12032. (beginning-of-line 0)
  12033. (org-indent-line)
  12034. (beginning-of-line 2)
  12035. (org-indent-line)
  12036. (end-of-line 0)))
  12037. ((and org-log-state-notes-insert-after-drawers
  12038. (save-excursion
  12039. (forward-line) (looking-at org-drawer-regexp)))
  12040. (forward-line)
  12041. (while (looking-at org-drawer-regexp)
  12042. (goto-char (match-end 0))
  12043. (re-search-forward org-property-end-re (point-max) t)
  12044. (forward-line))
  12045. (forward-line -1)))
  12046. (unless org-log-states-order-reversed
  12047. (and (= (char-after) ?\n) (forward-char 1))
  12048. (org-skip-over-state-notes)
  12049. (skip-chars-backward " \t\n\r")))
  12050. (move-marker org-log-note-marker (point))
  12051. (setq org-log-note-purpose purpose
  12052. org-log-note-state state
  12053. org-log-note-previous-state prev-state
  12054. org-log-note-how how
  12055. org-log-note-extra extra
  12056. org-log-note-effective-time (org-current-effective-time))
  12057. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12058. (defun org-skip-over-state-notes ()
  12059. "Skip past the list of State notes in an entry."
  12060. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12061. (when (ignore-errors (goto-char (org-in-item-p)))
  12062. (let* ((struct (org-list-struct))
  12063. (prevs (org-list-prevs-alist struct)))
  12064. (while (looking-at "[ \t]*- State")
  12065. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12066. (org-list-get-item-end (point) struct)))))))
  12067. (defun org-add-log-note (&optional purpose)
  12068. "Pop up a window for taking a note, and add this note later at point."
  12069. (remove-hook 'post-command-hook 'org-add-log-note)
  12070. (setq org-log-note-window-configuration (current-window-configuration))
  12071. (delete-other-windows)
  12072. (move-marker org-log-note-return-to (point))
  12073. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12074. (goto-char org-log-note-marker)
  12075. (org-switch-to-buffer-other-window "*Org Note*")
  12076. (erase-buffer)
  12077. (if (memq org-log-note-how '(time state))
  12078. (let (current-prefix-arg) (org-store-log-note))
  12079. (let ((org-inhibit-startup t)) (org-mode))
  12080. (insert (format "# Insert note for %s.
  12081. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12082. (cond
  12083. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12084. ((eq org-log-note-purpose 'done) "closed todo item")
  12085. ((eq org-log-note-purpose 'state)
  12086. (format "state change from \"%s\" to \"%s\""
  12087. (or org-log-note-previous-state "")
  12088. (or org-log-note-state "")))
  12089. ((eq org-log-note-purpose 'reschedule)
  12090. "rescheduling")
  12091. ((eq org-log-note-purpose 'delschedule)
  12092. "no longer scheduled")
  12093. ((eq org-log-note-purpose 'redeadline)
  12094. "changing deadline")
  12095. ((eq org-log-note-purpose 'deldeadline)
  12096. "removing deadline")
  12097. ((eq org-log-note-purpose 'refile)
  12098. "refiling")
  12099. ((eq org-log-note-purpose 'note)
  12100. "this entry")
  12101. (t (error "This should not happen")))))
  12102. (if org-log-note-extra (insert org-log-note-extra))
  12103. (org-set-local 'org-finish-function 'org-store-log-note)
  12104. (run-hooks 'org-log-buffer-setup-hook)))
  12105. (defvar org-note-abort nil) ; dynamically scoped
  12106. (defun org-store-log-note ()
  12107. "Finish taking a log note, and insert it to where it belongs."
  12108. (let ((txt (buffer-string)))
  12109. (kill-buffer (current-buffer))
  12110. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12111. lines ind bul)
  12112. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12113. (setq txt (replace-match "" t t txt)))
  12114. (if (string-match "\\s-+\\'" txt)
  12115. (setq txt (replace-match "" t t txt)))
  12116. (setq lines (org-split-string txt "\n"))
  12117. (when (and note (string-match "\\S-" note))
  12118. (setq note
  12119. (org-replace-escapes
  12120. note
  12121. (list (cons "%u" (user-login-name))
  12122. (cons "%U" user-full-name)
  12123. (cons "%t" (format-time-string
  12124. (org-time-stamp-format 'long 'inactive)
  12125. org-log-note-effective-time))
  12126. (cons "%T" (format-time-string
  12127. (org-time-stamp-format 'long nil)
  12128. org-log-note-effective-time))
  12129. (cons "%d" (format-time-string
  12130. (org-time-stamp-format nil 'inactive)
  12131. org-log-note-effective-time))
  12132. (cons "%D" (format-time-string
  12133. (org-time-stamp-format nil nil)
  12134. org-log-note-effective-time))
  12135. (cons "%s" (if org-log-note-state
  12136. (concat "\"" org-log-note-state "\"")
  12137. ""))
  12138. (cons "%S" (if org-log-note-previous-state
  12139. (concat "\"" org-log-note-previous-state "\"")
  12140. "\"\"")))))
  12141. (if lines (setq note (concat note " \\\\")))
  12142. (push note lines))
  12143. (when (or current-prefix-arg org-note-abort)
  12144. (when org-log-into-drawer
  12145. (org-remove-empty-drawer-at
  12146. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12147. org-log-note-marker))
  12148. (setq lines nil))
  12149. (when lines
  12150. (with-current-buffer (marker-buffer org-log-note-marker)
  12151. (save-excursion
  12152. (goto-char org-log-note-marker)
  12153. (move-marker org-log-note-marker nil)
  12154. (end-of-line 1)
  12155. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12156. (setq ind (save-excursion
  12157. (if (ignore-errors (goto-char (org-in-item-p)))
  12158. (let ((struct (org-list-struct)))
  12159. (org-list-get-ind
  12160. (org-list-get-top-point struct) struct))
  12161. (skip-chars-backward " \r\t\n")
  12162. (cond
  12163. ((and (org-at-heading-p)
  12164. org-adapt-indentation)
  12165. (1+ (org-current-level)))
  12166. ((org-at-heading-p) 0)
  12167. (t (org-get-indentation))))))
  12168. (setq bul (org-list-bullet-string "-"))
  12169. (org-indent-line-to ind)
  12170. (insert bul (pop lines))
  12171. (let ((ind-body (+ (length bul) ind)))
  12172. (while lines
  12173. (insert "\n")
  12174. (org-indent-line-to ind-body)
  12175. (insert (pop lines))))
  12176. (message "Note stored")
  12177. (org-back-to-heading t)
  12178. (org-cycle-hide-drawers 'children))
  12179. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12180. ;; from within `org-add-log-note' because `buffer-undo-list'
  12181. ;; is then modified outside of `org-with-remote-undo'.
  12182. (when (eq this-command 'org-agenda-todo)
  12183. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12184. ;; Don't add undo information when called from `org-agenda-todo'
  12185. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12186. (set-window-configuration org-log-note-window-configuration)
  12187. (with-current-buffer (marker-buffer org-log-note-return-to)
  12188. (goto-char org-log-note-return-to))
  12189. (move-marker org-log-note-return-to nil)
  12190. (and org-log-post-message (message "%s" org-log-post-message))))
  12191. (defun org-remove-empty-drawer-at (drawer pos)
  12192. "Remove an empty drawer DRAWER at position POS.
  12193. POS may also be a marker."
  12194. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12195. (save-excursion
  12196. (save-restriction
  12197. (widen)
  12198. (goto-char pos)
  12199. (if (org-in-regexp
  12200. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12201. (replace-match ""))))))
  12202. (defvar org-ts-type nil)
  12203. (defun org-sparse-tree (&optional arg type)
  12204. "Create a sparse tree, prompt for the details.
  12205. This command can create sparse trees. You first need to select the type
  12206. of match used to create the tree:
  12207. t Show all TODO entries.
  12208. T Show entries with a specific TODO keyword.
  12209. m Show entries selected by a tags/property match.
  12210. p Enter a property name and its value (both with completion on existing
  12211. names/values) and show entries with that property.
  12212. r Show entries matching a regular expression (`/' can be used as well).
  12213. b Show deadlines and scheduled items before a date.
  12214. a Show deadlines and scheduled items after a date.
  12215. d Show deadlines due within `org-deadline-warning-days'.
  12216. D Show deadlines and scheduled items between a date range."
  12217. (interactive "P")
  12218. (let (ans kwd value ts-type)
  12219. (setq type (or type org-sparse-tree-default-date-type))
  12220. (setq org-ts-type type)
  12221. (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"
  12222. (cond ((eq type 'all) "all timestamps")
  12223. ((eq type 'scheduled) "only scheduled")
  12224. ((eq type 'deadline) "only deadline")
  12225. ((eq type 'active) "only active timestamps")
  12226. ((eq type 'inactive) "only inactive timestamps")
  12227. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12228. ((eq type 'closed) "with a closed time-stamp")
  12229. (t "scheduled/deadline")))
  12230. (setq ans (read-char-exclusive))
  12231. (cond
  12232. ((equal ans ?c)
  12233. (org-sparse-tree
  12234. arg (cadr (member type '(scheduled-or-deadline
  12235. all scheduled deadline active inactive closed)))))
  12236. ((equal ans ?d)
  12237. (call-interactively 'org-check-deadlines))
  12238. ((equal ans ?b)
  12239. (call-interactively 'org-check-before-date))
  12240. ((equal ans ?a)
  12241. (call-interactively 'org-check-after-date))
  12242. ((equal ans ?D)
  12243. (call-interactively 'org-check-dates-range))
  12244. ((equal ans ?t)
  12245. (call-interactively 'org-show-todo-tree))
  12246. ((equal ans ?T)
  12247. (org-show-todo-tree '(4)))
  12248. ((member ans '(?T ?m))
  12249. (call-interactively 'org-match-sparse-tree))
  12250. ((member ans '(?p ?P))
  12251. (setq kwd (org-icompleting-read "Property: "
  12252. (mapcar 'list (org-buffer-property-keys))))
  12253. (setq value (org-icompleting-read "Value: "
  12254. (mapcar 'list (org-property-values kwd))))
  12255. (unless (string-match "\\`{.*}\\'" value)
  12256. (setq value (concat "\"" value "\"")))
  12257. (org-match-sparse-tree arg (concat kwd "=" value)))
  12258. ((member ans '(?r ?R ?/))
  12259. (call-interactively 'org-occur))
  12260. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12261. (defvar org-occur-highlights nil
  12262. "List of overlays used for occur matches.")
  12263. (make-variable-buffer-local 'org-occur-highlights)
  12264. (defvar org-occur-parameters nil
  12265. "Parameters of the active org-occur calls.
  12266. This is a list, each call to org-occur pushes as cons cell,
  12267. containing the regular expression and the callback, onto the list.
  12268. The list can contain several entries if `org-occur' has been called
  12269. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12270. will only contain one set of parameters. When the highlights are
  12271. removed (for example with `C-c C-c', or with the next edit (depending
  12272. on `org-remove-highlights-with-change'), this variable is emptied
  12273. as well.")
  12274. (make-variable-buffer-local 'org-occur-parameters)
  12275. (defun org-occur (regexp &optional keep-previous callback)
  12276. "Make a compact tree which shows all matches of REGEXP.
  12277. The tree will show the lines where the regexp matches, and all higher
  12278. headlines above the match. It will also show the heading after the match,
  12279. to make sure editing the matching entry is easy.
  12280. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12281. call to `org-occur' will be kept, to allow stacking of calls to this
  12282. command.
  12283. If CALLBACK is non-nil, it is a function which is called to confirm
  12284. that the match should indeed be shown."
  12285. (interactive "sRegexp: \nP")
  12286. (when (equal regexp "")
  12287. (user-error "Regexp cannot be empty"))
  12288. (unless keep-previous
  12289. (org-remove-occur-highlights nil nil t))
  12290. (push (cons regexp callback) org-occur-parameters)
  12291. (let ((cnt 0))
  12292. (save-excursion
  12293. (goto-char (point-min))
  12294. (if (or (not keep-previous) ; do not want to keep
  12295. (not org-occur-highlights)) ; no previous matches
  12296. ;; hide everything
  12297. (org-overview))
  12298. (while (re-search-forward regexp nil t)
  12299. (when (or (not callback)
  12300. (save-match-data (funcall callback)))
  12301. (setq cnt (1+ cnt))
  12302. (when org-highlight-sparse-tree-matches
  12303. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12304. (org-show-context 'occur-tree))))
  12305. (when org-remove-highlights-with-change
  12306. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12307. nil 'local))
  12308. (unless org-sparse-tree-open-archived-trees
  12309. (org-hide-archived-subtrees (point-min) (point-max)))
  12310. (run-hooks 'org-occur-hook)
  12311. (if (org-called-interactively-p 'interactive)
  12312. (message "%d match(es) for regexp %s" cnt regexp))
  12313. cnt))
  12314. (defun org-occur-next-match (&optional n reset)
  12315. "Function for `next-error-function' to find sparse tree matches.
  12316. N is the number of matches to move, when negative move backwards.
  12317. RESET is entirely ignored - this function always goes back to the
  12318. starting point when no match is found."
  12319. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12320. (search-func (if (< n 0)
  12321. 'previous-single-char-property-change
  12322. 'next-single-char-property-change))
  12323. (n (abs n))
  12324. (pos (point))
  12325. p1)
  12326. (catch 'exit
  12327. (while (setq p1 (funcall search-func (point) 'org-type))
  12328. (when (equal p1 limit)
  12329. (goto-char pos)
  12330. (error "No more matches"))
  12331. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12332. (setq n (1- n))
  12333. (when (= n 0)
  12334. (goto-char p1)
  12335. (throw 'exit (point))))
  12336. (goto-char p1))
  12337. (goto-char p1)
  12338. (error "No more matches"))))
  12339. (defun org-show-context (&optional key)
  12340. "Make sure point and context are visible.
  12341. How much context is shown depends upon the variables
  12342. `org-show-hierarchy-above', `org-show-following-heading',
  12343. `org-show-entry-below' and `org-show-siblings'."
  12344. (let ((heading-p (org-at-heading-p t))
  12345. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12346. (following-p (org-get-alist-option org-show-following-heading key))
  12347. (entry-p (org-get-alist-option org-show-entry-below key))
  12348. (siblings-p (org-get-alist-option org-show-siblings key)))
  12349. ;; Show heading or entry text
  12350. (if (and heading-p (not entry-p))
  12351. (org-flag-heading nil) ; only show the heading
  12352. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12353. (org-show-hidden-entry))) ; show entire entry
  12354. (when following-p
  12355. ;; Show next sibling, or heading below text
  12356. (save-excursion
  12357. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12358. (org-flag-heading nil))))
  12359. (when siblings-p (org-show-siblings))
  12360. (when hierarchy-p
  12361. ;; show all higher headings, possibly with siblings
  12362. (save-excursion
  12363. (while (and (condition-case nil
  12364. (progn (org-up-heading-all 1) t)
  12365. (error nil))
  12366. (not (bobp)))
  12367. (org-flag-heading nil)
  12368. (when siblings-p (org-show-siblings)))))
  12369. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12370. (defvar org-reveal-start-hook nil
  12371. "Hook run before revealing a location.")
  12372. (defun org-reveal (&optional siblings)
  12373. "Show current entry, hierarchy above it, and the following headline.
  12374. This can be used to show a consistent set of context around locations
  12375. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12376. not t for the search context.
  12377. With optional argument SIBLINGS, on each level of the hierarchy all
  12378. siblings are shown. This repairs the tree structure to what it would
  12379. look like when opened with hierarchical calls to `org-cycle'.
  12380. With double optional argument \\[universal-argument] \\[universal-argument], \
  12381. go to the parent and show the
  12382. entire tree."
  12383. (interactive "P")
  12384. (run-hooks 'org-reveal-start-hook)
  12385. (let ((org-show-hierarchy-above t)
  12386. (org-show-following-heading t)
  12387. (org-show-siblings (if siblings t org-show-siblings)))
  12388. (org-show-context nil))
  12389. (when (equal siblings '(16))
  12390. (save-excursion
  12391. (when (org-up-heading-safe)
  12392. (org-show-subtree)
  12393. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12394. (defun org-highlight-new-match (beg end)
  12395. "Highlight from BEG to END and mark the highlight is an occur headline."
  12396. (let ((ov (make-overlay beg end)))
  12397. (overlay-put ov 'face 'secondary-selection)
  12398. (overlay-put ov 'org-type 'org-occur)
  12399. (push ov org-occur-highlights)))
  12400. (defun org-remove-occur-highlights (&optional beg end noremove)
  12401. "Remove the occur highlights from the buffer.
  12402. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12403. from the `before-change-functions' in the current buffer."
  12404. (interactive)
  12405. (unless org-inhibit-highlight-removal
  12406. (mapc 'delete-overlay org-occur-highlights)
  12407. (setq org-occur-highlights nil)
  12408. (setq org-occur-parameters nil)
  12409. (unless noremove
  12410. (remove-hook 'before-change-functions
  12411. 'org-remove-occur-highlights 'local))))
  12412. ;;;; Priorities
  12413. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12414. "Regular expression matching the priority indicator.")
  12415. (defvar org-remove-priority-next-time nil)
  12416. (defun org-priority-up ()
  12417. "Increase the priority of the current item."
  12418. (interactive)
  12419. (org-priority 'up))
  12420. (defun org-priority-down ()
  12421. "Decrease the priority of the current item."
  12422. (interactive)
  12423. (org-priority 'down))
  12424. (defun org-priority (&optional action show)
  12425. "Change the priority of an item.
  12426. ACTION can be `set', `up', `down', or a character."
  12427. (interactive "P")
  12428. (if (equal action '(4))
  12429. (org-show-priority)
  12430. (unless org-enable-priority-commands
  12431. (user-error "Priority commands are disabled"))
  12432. (setq action (or action 'set))
  12433. (let (current new news have remove)
  12434. (save-excursion
  12435. (org-back-to-heading t)
  12436. (if (looking-at org-priority-regexp)
  12437. (setq current (string-to-char (match-string 2))
  12438. have t))
  12439. (cond
  12440. ((eq action 'remove)
  12441. (setq remove t new ?\ ))
  12442. ((or (eq action 'set)
  12443. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12444. (if (not (eq action 'set))
  12445. (setq new action)
  12446. (message "Priority %c-%c, SPC to remove: "
  12447. org-highest-priority org-lowest-priority)
  12448. (save-match-data
  12449. (setq new (read-char-exclusive))))
  12450. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12451. (= (upcase org-lowest-priority) org-lowest-priority))
  12452. (setq new (upcase new)))
  12453. (cond ((equal new ?\ ) (setq remove t))
  12454. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12455. (user-error "Priority must be between `%c' and `%c'"
  12456. org-highest-priority org-lowest-priority))))
  12457. ((eq action 'up)
  12458. (setq new (if have
  12459. (1- current) ; normal cycling
  12460. ;; last priority was empty
  12461. (if (eq last-command this-command)
  12462. org-lowest-priority ; wrap around empty to lowest
  12463. ;; default
  12464. (if org-priority-start-cycle-with-default
  12465. org-default-priority
  12466. (1- org-default-priority))))))
  12467. ((eq action 'down)
  12468. (setq new (if have
  12469. (1+ current) ; normal cycling
  12470. ;; last priority was empty
  12471. (if (eq last-command this-command)
  12472. org-highest-priority ; wrap around empty to highest
  12473. ;; default
  12474. (if org-priority-start-cycle-with-default
  12475. org-default-priority
  12476. (1+ org-default-priority))))))
  12477. (t (user-error "Invalid action")))
  12478. (if (or (< (upcase new) org-highest-priority)
  12479. (> (upcase new) org-lowest-priority))
  12480. (if (and (memq action '(up down))
  12481. (not have) (not (eq last-command this-command)))
  12482. ;; `new' is from default priority
  12483. (error
  12484. "The default can not be set, see `org-default-priority' why")
  12485. ;; normal cycling: `new' is beyond highest/lowest priority
  12486. ;; and is wrapped around to the empty priority
  12487. (setq remove t)))
  12488. (setq news (format "%c" new))
  12489. (if have
  12490. (if remove
  12491. (replace-match "" t t nil 1)
  12492. (replace-match news t t nil 2))
  12493. (if remove
  12494. (user-error "No priority cookie found in line")
  12495. (let ((case-fold-search nil))
  12496. (looking-at org-todo-line-regexp))
  12497. (if (match-end 2)
  12498. (progn
  12499. (goto-char (match-end 2))
  12500. (insert " [#" news "]"))
  12501. (goto-char (match-beginning 3))
  12502. (insert "[#" news "] "))))
  12503. (org-preserve-lc (org-set-tags nil 'align)))
  12504. (if remove
  12505. (message "Priority removed")
  12506. (message "Priority of current item set to %s" news)))))
  12507. (defun org-show-priority ()
  12508. "Show the priority of the current item.
  12509. This priority is composed of the main priority given with the [#A] cookies,
  12510. and by additional input from the age of a schedules or deadline entry."
  12511. (interactive)
  12512. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12513. (org-get-at-bol 'priority)
  12514. (save-excursion
  12515. (save-match-data
  12516. (beginning-of-line)
  12517. (and (looking-at org-heading-regexp)
  12518. (org-get-priority (match-string 0))))))))
  12519. (message "Priority is %d" (if pri pri -1000))))
  12520. (defun org-get-priority (s)
  12521. "Find priority cookie and return priority."
  12522. (save-match-data
  12523. (if (functionp org-get-priority-function)
  12524. (funcall org-get-priority-function)
  12525. (if (not (string-match org-priority-regexp s))
  12526. (* 1000 (- org-lowest-priority org-default-priority))
  12527. (* 1000 (- org-lowest-priority
  12528. (string-to-char (match-string 2 s))))))))
  12529. ;;;; Tags
  12530. (defvar org-agenda-archives-mode)
  12531. (defvar org-map-continue-from nil
  12532. "Position from where mapping should continue.
  12533. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12534. (defvar org-scanner-tags nil
  12535. "The current tag list while the tags scanner is running.")
  12536. (defvar org-trust-scanner-tags nil
  12537. "Should `org-get-tags-at' use the tags for the scanner.
  12538. This is for internal dynamical scoping only.
  12539. When this is non-nil, the function `org-get-tags-at' will return the value
  12540. of `org-scanner-tags' instead of building the list by itself. This
  12541. can lead to large speed-ups when the tags scanner is used in a file with
  12542. many entries, and when the list of tags is retrieved, for example to
  12543. obtain a list of properties. Building the tags list for each entry in such
  12544. a file becomes an N^2 operation - but with this variable set, it scales
  12545. as N.")
  12546. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12547. "Sca headline tags with inheritance and produce output ACTION.
  12548. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12549. or `agenda' to produce an entry list for an agenda view. It can also be
  12550. a Lisp form or a function that should be called at each matched headline, in
  12551. this case the return value is a list of all return values from these calls.
  12552. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12553. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12554. only lines with a not-done TODO keyword are included in the output.
  12555. This should be the same variable that was scoped into
  12556. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12557. START-LEVEL can be a string with asterisks, reducing the scope to
  12558. headlines matching this string."
  12559. (require 'org-agenda)
  12560. (let* ((re (concat "^"
  12561. (if start-level
  12562. ;; Get the correct level to match
  12563. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12564. org-outline-regexp)
  12565. " *\\(\\<\\("
  12566. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12567. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12568. (props (list 'face 'default
  12569. 'done-face 'org-agenda-done
  12570. 'undone-face 'default
  12571. 'mouse-face 'highlight
  12572. 'org-not-done-regexp org-not-done-regexp
  12573. 'org-todo-regexp org-todo-regexp
  12574. 'org-complex-heading-regexp org-complex-heading-regexp
  12575. 'help-echo
  12576. (format "mouse-2 or RET jump to org file %s"
  12577. (abbreviate-file-name
  12578. (or (buffer-file-name (buffer-base-buffer))
  12579. (buffer-name (buffer-base-buffer)))))))
  12580. (org-map-continue-from nil)
  12581. lspos tags tags-list
  12582. (tags-alist (list (cons 0 org-file-tags)))
  12583. (llast 0) rtn rtn1 level category i txt
  12584. todo marker entry priority)
  12585. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12586. (setq action (list 'lambda nil action)))
  12587. (save-excursion
  12588. (goto-char (point-min))
  12589. (when (eq action 'sparse-tree)
  12590. (org-overview)
  12591. (org-remove-occur-highlights))
  12592. (while (let (case-fold-search)
  12593. (re-search-forward re nil t))
  12594. (setq org-map-continue-from nil)
  12595. (catch :skip
  12596. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12597. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12598. (goto-char (setq lspos (match-beginning 0)))
  12599. (setq level (org-reduced-level (org-outline-level))
  12600. category (org-get-category))
  12601. (setq i llast llast level)
  12602. ;; remove tag lists from same and sublevels
  12603. (while (>= i level)
  12604. (when (setq entry (assoc i tags-alist))
  12605. (setq tags-alist (delete entry tags-alist)))
  12606. (setq i (1- i)))
  12607. ;; add the next tags
  12608. (when tags
  12609. (setq tags (org-split-string tags ":")
  12610. tags-alist
  12611. (cons (cons level tags) tags-alist)))
  12612. ;; compile tags for current headline
  12613. (setq tags-list
  12614. (if org-use-tag-inheritance
  12615. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12616. tags)
  12617. org-scanner-tags tags-list)
  12618. (when org-use-tag-inheritance
  12619. (setcdr (car tags-alist)
  12620. (mapcar (lambda (x)
  12621. (setq x (copy-sequence x))
  12622. (org-add-prop-inherited x))
  12623. (cdar tags-alist))))
  12624. (when (and tags org-use-tag-inheritance
  12625. (or (not (eq t org-use-tag-inheritance))
  12626. org-tags-exclude-from-inheritance))
  12627. ;; selective inheritance, remove uninherited ones
  12628. (setcdr (car tags-alist)
  12629. (org-remove-uninherited-tags (cdar tags-alist))))
  12630. (when (and
  12631. ;; eval matcher only when the todo condition is OK
  12632. (and (or (not todo-only) (member todo org-not-done-keywords))
  12633. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12634. (eval matcher)))
  12635. ;; Call the skipper, but return t if it does not skip,
  12636. ;; so that the `and' form continues evaluating
  12637. (progn
  12638. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12639. t)
  12640. ;; Check if timestamps are deselecting this entry
  12641. (or (not todo-only)
  12642. (and (member todo org-not-done-keywords)
  12643. (or (not org-agenda-tags-todo-honor-ignore-options)
  12644. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12645. ;; select this headline
  12646. (cond
  12647. ((eq action 'sparse-tree)
  12648. (and org-highlight-sparse-tree-matches
  12649. (org-get-heading) (match-end 0)
  12650. (org-highlight-new-match
  12651. (match-beginning 1) (match-end 1)))
  12652. (org-show-context 'tags-tree))
  12653. ((eq action 'agenda)
  12654. (setq txt (org-agenda-format-item
  12655. ""
  12656. (concat
  12657. (if (eq org-tags-match-list-sublevels 'indented)
  12658. (make-string (1- level) ?.) "")
  12659. (org-get-heading))
  12660. level category
  12661. tags-list)
  12662. priority (org-get-priority txt))
  12663. (goto-char lspos)
  12664. (setq marker (org-agenda-new-marker))
  12665. (org-add-props txt props
  12666. 'org-marker marker 'org-hd-marker marker 'org-category category
  12667. 'todo-state todo
  12668. 'priority priority 'type "tagsmatch")
  12669. (push txt rtn))
  12670. ((functionp action)
  12671. (setq org-map-continue-from nil)
  12672. (save-excursion
  12673. (setq rtn1 (funcall action))
  12674. (push rtn1 rtn)))
  12675. (t (user-error "Invalid action")))
  12676. ;; if we are to skip sublevels, jump to end of subtree
  12677. (unless org-tags-match-list-sublevels
  12678. (org-end-of-subtree t)
  12679. (backward-char 1))))
  12680. ;; Get the correct position from where to continue
  12681. (if org-map-continue-from
  12682. (goto-char org-map-continue-from)
  12683. (and (= (point) lspos) (end-of-line 1)))))
  12684. (when (and (eq action 'sparse-tree)
  12685. (not org-sparse-tree-open-archived-trees))
  12686. (org-hide-archived-subtrees (point-min) (point-max)))
  12687. (nreverse rtn)))
  12688. (defun org-remove-uninherited-tags (tags)
  12689. "Remove all tags that are not inherited from the list TAGS."
  12690. (cond
  12691. ((eq org-use-tag-inheritance t)
  12692. (if org-tags-exclude-from-inheritance
  12693. (org-delete-all org-tags-exclude-from-inheritance tags)
  12694. tags))
  12695. ((not org-use-tag-inheritance) nil)
  12696. ((stringp org-use-tag-inheritance)
  12697. (delq nil (mapcar
  12698. (lambda (x)
  12699. (if (and (string-match org-use-tag-inheritance x)
  12700. (not (member x org-tags-exclude-from-inheritance)))
  12701. x nil))
  12702. tags)))
  12703. ((listp org-use-tag-inheritance)
  12704. (delq nil (mapcar
  12705. (lambda (x)
  12706. (if (member x org-use-tag-inheritance) x nil))
  12707. tags)))))
  12708. (defun org-match-sparse-tree (&optional todo-only match)
  12709. "Create a sparse tree according to tags string MATCH.
  12710. MATCH can contain positive and negative selection of tags, like
  12711. \"+WORK+URGENT-WITHBOSS\".
  12712. If optional argument TODO-ONLY is non-nil, only select lines that are
  12713. also TODO lines."
  12714. (interactive "P")
  12715. (org-agenda-prepare-buffers (list (current-buffer)))
  12716. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12717. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12718. (defvar org-cached-props nil)
  12719. (defun org-cached-entry-get (pom property)
  12720. (if (or (eq t org-use-property-inheritance)
  12721. (and (stringp org-use-property-inheritance)
  12722. (string-match org-use-property-inheritance property))
  12723. (and (listp org-use-property-inheritance)
  12724. (member property org-use-property-inheritance)))
  12725. ;; Caching is not possible, check it directly
  12726. (org-entry-get pom property 'inherit)
  12727. ;; Get all properties, so that we can do complicated checks easily
  12728. (cdr (assoc property (or org-cached-props
  12729. (setq org-cached-props
  12730. (org-entry-properties pom)))))))
  12731. (defun org-global-tags-completion-table (&optional files)
  12732. "Return the list of all tags in all agenda buffer/files.
  12733. Optional FILES argument is a list of files which can be used
  12734. instead of the agenda files."
  12735. (save-excursion
  12736. (org-uniquify
  12737. (delq nil
  12738. (apply 'append
  12739. (mapcar
  12740. (lambda (file)
  12741. (set-buffer (find-file-noselect file))
  12742. (append (org-get-buffer-tags)
  12743. (mapcar (lambda (x) (if (stringp (car-safe x))
  12744. (list (car-safe x)) nil))
  12745. org-tag-alist)))
  12746. (if (and files (car files))
  12747. files
  12748. (org-agenda-files))))))))
  12749. (defun org-make-tags-matcher (match)
  12750. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12751. The variable `todo-only' is scoped dynamically into this function.
  12752. It will be set to t if the matcher restricts matching to TODO entries,
  12753. otherwise will not be touched.
  12754. Returns a cons of the selection string MATCH and the constructed
  12755. lisp form implementing the matcher. The matcher is to be evaluated
  12756. at an Org entry, with point on the headline, and returns t if the
  12757. entry matches the selection string MATCH. The returned lisp form
  12758. references two variables with information about the entry, which
  12759. must be bound around the form's evaluation: todo, the TODO keyword
  12760. at the entry (or nil of none); and tags-list, the list of all tags
  12761. at the entry including inherited ones. Additionally, the category
  12762. of the entry (if any) must be specified as the text property
  12763. 'org-category on the headline.
  12764. See also `org-scan-tags'.
  12765. "
  12766. (declare (special todo-only))
  12767. (unless (boundp 'todo-only)
  12768. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12769. (unless match
  12770. ;; Get a new match request, with completion against the global
  12771. ;; tags table and the local tags in current buffer
  12772. (let ((org-last-tags-completion-table
  12773. (org-uniquify
  12774. (delq nil (append (org-get-buffer-tags)
  12775. (org-global-tags-completion-table))))))
  12776. (setq match (org-completing-read-no-i
  12777. "Match: " 'org-tags-completion-function nil nil nil
  12778. 'org-tags-history))))
  12779. ;; Parse the string and create a lisp form
  12780. (let ((match0 match)
  12781. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12782. minus tag mm
  12783. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12784. orterms term orlist re-p str-p level-p level-op time-p
  12785. prop-p pn pv po gv rest)
  12786. ;; Expand group tags
  12787. (setq match (org-tags-expand match))
  12788. (if (string-match "/+" match)
  12789. ;; match contains also a todo-matching request
  12790. (progn
  12791. (setq tagsmatch (substring match 0 (match-beginning 0))
  12792. todomatch (substring match (match-end 0)))
  12793. (if (string-match "^!" todomatch)
  12794. (setq todo-only t todomatch (substring todomatch 1)))
  12795. (if (string-match "^\\s-*$" todomatch)
  12796. (setq todomatch nil)))
  12797. ;; only matching tags
  12798. (setq tagsmatch match todomatch nil))
  12799. ;; Make the tags matcher
  12800. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12801. (setq tagsmatcher t)
  12802. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12803. (while (setq term (pop orterms))
  12804. (while (and (equal (substring term -1) "\\") orterms)
  12805. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12806. (while (string-match re term)
  12807. (setq rest (substring term (match-end 0))
  12808. minus (and (match-end 1)
  12809. (equal (match-string 1 term) "-"))
  12810. tag (save-match-data (replace-regexp-in-string
  12811. "\\\\-" "-"
  12812. (match-string 2 term)))
  12813. re-p (equal (string-to-char tag) ?{)
  12814. level-p (match-end 4)
  12815. prop-p (match-end 5)
  12816. mm (cond
  12817. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12818. (level-p
  12819. (setq level-op (org-op-to-function (match-string 3 term)))
  12820. `(,level-op level ,(string-to-number
  12821. (match-string 4 term))))
  12822. (prop-p
  12823. (setq pn (match-string 5 term)
  12824. po (match-string 6 term)
  12825. pv (match-string 7 term)
  12826. re-p (equal (string-to-char pv) ?{)
  12827. str-p (equal (string-to-char pv) ?\")
  12828. time-p (save-match-data
  12829. (string-match "^\"[[<].*[]>]\"$" pv))
  12830. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12831. (if time-p (setq pv (org-matcher-time pv)))
  12832. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12833. (cond
  12834. ((equal pn "CATEGORY")
  12835. (setq gv '(get-text-property (point) 'org-category)))
  12836. ((equal pn "TODO")
  12837. (setq gv 'todo))
  12838. (t
  12839. (setq gv `(org-cached-entry-get nil ,pn))))
  12840. (if re-p
  12841. (if (eq po 'org<>)
  12842. `(not (string-match ,pv (or ,gv "")))
  12843. `(string-match ,pv (or ,gv "")))
  12844. (if str-p
  12845. `(,po (or ,gv "") ,pv)
  12846. `(,po (string-to-number (or ,gv ""))
  12847. ,(string-to-number pv) ))))
  12848. (t `(member ,tag tags-list)))
  12849. mm (if minus (list 'not mm) mm)
  12850. term rest)
  12851. (push mm tagsmatcher))
  12852. (push (if (> (length tagsmatcher) 1)
  12853. (cons 'and tagsmatcher)
  12854. (car tagsmatcher))
  12855. orlist)
  12856. (setq tagsmatcher nil))
  12857. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12858. (setq tagsmatcher
  12859. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12860. ;; Make the todo matcher
  12861. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12862. (setq todomatcher t)
  12863. (setq orterms (org-split-string todomatch "|") orlist nil)
  12864. (while (setq term (pop orterms))
  12865. (while (string-match re term)
  12866. (setq minus (and (match-end 1)
  12867. (equal (match-string 1 term) "-"))
  12868. kwd (match-string 2 term)
  12869. re-p (equal (string-to-char kwd) ?{)
  12870. term (substring term (match-end 0))
  12871. mm (if re-p
  12872. `(string-match ,(substring kwd 1 -1) todo)
  12873. (list 'equal 'todo kwd))
  12874. mm (if minus (list 'not mm) mm))
  12875. (push mm todomatcher))
  12876. (push (if (> (length todomatcher) 1)
  12877. (cons 'and todomatcher)
  12878. (car todomatcher))
  12879. orlist)
  12880. (setq todomatcher nil))
  12881. (setq todomatcher (if (> (length orlist) 1)
  12882. (cons 'or orlist) (car orlist))))
  12883. ;; Return the string and lisp forms of the matcher
  12884. (setq matcher (if todomatcher
  12885. (list 'and tagsmatcher todomatcher)
  12886. tagsmatcher))
  12887. (when todo-only
  12888. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12889. matcher)))
  12890. (cons match0 matcher)))
  12891. (defun org-tags-expand (match &optional single-as-list downcased)
  12892. "Expand group tags in MATCH.
  12893. This replaces every group tag in MATCH with a regexp tag search.
  12894. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12895. will be replaced like this:
  12896. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12897. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12898. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12899. Replacing by a regexp preserves the structure of the match.
  12900. E.g., this expansion
  12901. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12902. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12903. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12904. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12905. assumed to be a single group tag, and the function will return
  12906. the list of tags in this group.
  12907. When DOWNCASE is non-nil, expand downcased TAGS."
  12908. (if org-group-tags
  12909. (let* ((case-fold-search t)
  12910. (stable org-mode-syntax-table)
  12911. (tal (or org-tag-groups-alist-for-agenda
  12912. org-tag-groups-alist))
  12913. (tal (if downcased
  12914. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12915. (tml (mapcar 'car tal))
  12916. (rtnmatch match) rpl)
  12917. ;; @ and _ are allowed as word-components in tags
  12918. (modify-syntax-entry ?@ "w" stable)
  12919. (modify-syntax-entry ?_ "w" stable)
  12920. (while (and tml
  12921. (with-syntax-table stable
  12922. (string-match
  12923. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12924. (regexp-opt tml) "\\>\\)") rtnmatch)))
  12925. (let* ((dir (match-string 1 rtnmatch))
  12926. (tag (match-string 2 rtnmatch))
  12927. (tag (if downcased (downcase tag) tag)))
  12928. (setq tml (delete tag tml))
  12929. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12930. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12931. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12932. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12933. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12934. (if single-as-list
  12935. (or (reverse rpl) (list rtnmatch))
  12936. rtnmatch))
  12937. (if single-as-list (list (if downcased (downcase match) match))
  12938. match)))
  12939. (defun org-op-to-function (op &optional stringp)
  12940. "Turn an operator into the appropriate function."
  12941. (setq op
  12942. (cond
  12943. ((equal op "<" ) '(< string< org-time<))
  12944. ((equal op ">" ) '(> org-string> org-time>))
  12945. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12946. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12947. ((member op '("=" "==")) '(= string= org-time=))
  12948. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12949. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12950. (defun org<> (a b) (not (= a b)))
  12951. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12952. (defun org-string>= (a b) (not (string< a b)))
  12953. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12954. (defun org-string<> (a b) (not (string= a b)))
  12955. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12956. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12957. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12958. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12959. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12960. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12961. (defun org-2ft (s)
  12962. "Convert S to a floating point time.
  12963. If S is already a number, just return it. If it is a string, parse
  12964. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12965. (cond
  12966. ((numberp s) s)
  12967. ((stringp s)
  12968. (condition-case nil
  12969. (float-time (apply 'encode-time (org-parse-time-string s)))
  12970. (error 0.)))
  12971. (t 0.)))
  12972. (defun org-time-today ()
  12973. "Time in seconds today at 0:00.
  12974. Returns the float number of seconds since the beginning of the
  12975. epoch to the beginning of today (00:00)."
  12976. (float-time (apply 'encode-time
  12977. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12978. (defun org-matcher-time (s)
  12979. "Interpret a time comparison value."
  12980. (save-match-data
  12981. (cond
  12982. ((string= s "<now>") (float-time))
  12983. ((string= s "<today>") (org-time-today))
  12984. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12985. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12986. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12987. (+ (org-time-today)
  12988. (* (string-to-number (match-string 1 s))
  12989. (cdr (assoc (match-string 2 s)
  12990. '(("d" . 86400.0) ("w" . 604800.0)
  12991. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12992. (t (org-2ft s)))))
  12993. (defun org-match-any-p (re list)
  12994. "Does re match any element of list?"
  12995. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12996. (delq nil list))
  12997. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12998. (defvar org-tags-overlay (make-overlay 1 1))
  12999. (org-detach-overlay org-tags-overlay)
  13000. (defun org-get-local-tags-at (&optional pos)
  13001. "Get a list of tags defined in the current headline."
  13002. (org-get-tags-at pos 'local))
  13003. (defun org-get-local-tags ()
  13004. "Get a list of tags defined in the current headline."
  13005. (org-get-tags-at nil 'local))
  13006. (defun org-get-tags-at (&optional pos local)
  13007. "Get a list of all headline tags applicable at POS.
  13008. POS defaults to point. If tags are inherited, the list contains
  13009. the targets in the same sequence as the headlines appear, i.e.
  13010. the tags of the current headline come last.
  13011. When LOCAL is non-nil, only return tags from the current headline,
  13012. ignore inherited ones."
  13013. (interactive)
  13014. (if (and org-trust-scanner-tags
  13015. (or (not pos) (equal pos (point)))
  13016. (not local))
  13017. org-scanner-tags
  13018. (let (tags ltags lastpos parent)
  13019. (save-excursion
  13020. (save-restriction
  13021. (widen)
  13022. (goto-char (or pos (point)))
  13023. (save-match-data
  13024. (catch 'done
  13025. (condition-case nil
  13026. (progn
  13027. (org-back-to-heading t)
  13028. (while (not (equal lastpos (point)))
  13029. (setq lastpos (point))
  13030. (when (looking-at
  13031. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13032. (setq ltags (org-split-string
  13033. (org-match-string-no-properties 1) ":"))
  13034. (when parent
  13035. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13036. (setq tags (append
  13037. (if parent
  13038. (org-remove-uninherited-tags ltags)
  13039. ltags)
  13040. tags)))
  13041. (or org-use-tag-inheritance (throw 'done t))
  13042. (if local (throw 'done t))
  13043. (or (org-up-heading-safe) (error nil))
  13044. (setq parent t)))
  13045. (error nil)))))
  13046. (if local
  13047. tags
  13048. (reverse (delete-dups
  13049. (reverse (append
  13050. (org-remove-uninherited-tags
  13051. org-file-tags) tags)))))))))
  13052. (defun org-add-prop-inherited (s)
  13053. (add-text-properties 0 (length s) '(inherited t) s)
  13054. s)
  13055. (defun org-toggle-tag (tag &optional onoff)
  13056. "Toggle the tag TAG for the current line.
  13057. If ONOFF is `on' or `off', don't toggle but set to this state."
  13058. (let (res current)
  13059. (save-excursion
  13060. (org-back-to-heading t)
  13061. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13062. (point-at-eol) t)
  13063. (progn
  13064. (setq current (match-string 1))
  13065. (replace-match ""))
  13066. (setq current ""))
  13067. (setq current (nreverse (org-split-string current ":")))
  13068. (cond
  13069. ((eq onoff 'on)
  13070. (setq res t)
  13071. (or (member tag current) (push tag current)))
  13072. ((eq onoff 'off)
  13073. (or (not (member tag current)) (setq current (delete tag current))))
  13074. (t (if (member tag current)
  13075. (setq current (delete tag current))
  13076. (setq res t)
  13077. (push tag current))))
  13078. (end-of-line 1)
  13079. (if current
  13080. (progn
  13081. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13082. (org-set-tags nil t))
  13083. (delete-horizontal-space))
  13084. (run-hooks 'org-after-tags-change-hook))
  13085. res))
  13086. (defun org-align-tags-here (to-col)
  13087. ;; Assumes that this is a headline
  13088. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13089. (beginning-of-line 1)
  13090. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13091. (< pos (match-beginning 2)))
  13092. (progn
  13093. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13094. (goto-char (match-beginning 1))
  13095. (insert " ")
  13096. (delete-region (point) (1+ (match-beginning 2)))
  13097. (setq ncol (max (current-column)
  13098. (1+ col)
  13099. (if (> to-col 0)
  13100. to-col
  13101. (- (abs to-col) tags-l))))
  13102. (setq p (point))
  13103. (insert (make-string (- ncol (current-column)) ?\ ))
  13104. (setq ncol (current-column))
  13105. (when indent-tabs-mode (tabify p (point-at-eol)))
  13106. (org-move-to-column (min ncol col) t))
  13107. (goto-char pos))))
  13108. (defun org-set-tags-command (&optional arg just-align)
  13109. "Call the set-tags command for the current entry."
  13110. (interactive "P")
  13111. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13112. (org-set-tags arg just-align)
  13113. (save-excursion
  13114. (unless (and (org-region-active-p)
  13115. org-loop-over-headlines-in-active-region)
  13116. (org-back-to-heading t))
  13117. (org-set-tags arg just-align))))
  13118. (defun org-set-tags-to (data)
  13119. "Set the tags of the current entry to DATA, replacing the current tags.
  13120. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13121. If DATA is nil or the empty string, any tags will be removed."
  13122. (interactive "sTags: ")
  13123. (setq data
  13124. (cond
  13125. ((eq data nil) "")
  13126. ((equal data "") "")
  13127. ((stringp data)
  13128. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13129. ":"))
  13130. ((listp data)
  13131. (concat ":" (mapconcat 'identity data ":") ":"))))
  13132. (when data
  13133. (save-excursion
  13134. (org-back-to-heading t)
  13135. (when (looking-at org-complex-heading-regexp)
  13136. (if (match-end 5)
  13137. (progn
  13138. (goto-char (match-beginning 5))
  13139. (insert data)
  13140. (delete-region (point) (point-at-eol))
  13141. (org-set-tags nil 'align))
  13142. (goto-char (point-at-eol))
  13143. (insert " " data)
  13144. (org-set-tags nil 'align)))
  13145. (beginning-of-line 1)
  13146. (if (looking-at ".*?\\([ \t]+\\)$")
  13147. (delete-region (match-beginning 1) (match-end 1))))))
  13148. (defun org-align-all-tags ()
  13149. "Align the tags i all headings."
  13150. (interactive)
  13151. (save-excursion
  13152. (or (ignore-errors (org-back-to-heading t))
  13153. (outline-next-heading))
  13154. (if (org-at-heading-p)
  13155. (org-set-tags t)
  13156. (message "No headings"))))
  13157. (defvar org-indent-indentation-per-level)
  13158. (defun org-set-tags (&optional arg just-align)
  13159. "Set the tags for the current headline.
  13160. With prefix ARG, realign all tags in headings in the current buffer."
  13161. (interactive "P")
  13162. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13163. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13164. 'region-start-level 'region))
  13165. org-loop-over-headlines-in-active-region)
  13166. (org-map-entries
  13167. ;; We don't use ARG and JUST-ALIGN here these args are not
  13168. ;; useful when looping over headlines
  13169. `(org-set-tags)
  13170. org-loop-over-headlines-in-active-region
  13171. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13172. (let* ((re org-outline-regexp-bol)
  13173. (current (unless arg (org-get-tags-string)))
  13174. (col (current-column))
  13175. (org-setting-tags t)
  13176. table current-tags inherited-tags ; computed below when needed
  13177. tags p0 c0 c1 rpl di tc level)
  13178. (if arg
  13179. (save-excursion
  13180. (goto-char (point-min))
  13181. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13182. (while (re-search-forward re nil t)
  13183. (org-set-tags nil t)
  13184. (end-of-line 1)))
  13185. (message "All tags realigned to column %d" org-tags-column))
  13186. (if just-align
  13187. (setq tags current)
  13188. ;; Get a new set of tags from the user
  13189. (save-excursion
  13190. (setq table (append org-tag-persistent-alist
  13191. (or org-tag-alist (org-get-buffer-tags))
  13192. (and
  13193. org-complete-tags-always-offer-all-agenda-tags
  13194. (org-global-tags-completion-table
  13195. (org-agenda-files))))
  13196. org-last-tags-completion-table table
  13197. current-tags (org-split-string current ":")
  13198. inherited-tags (nreverse
  13199. (nthcdr (length current-tags)
  13200. (nreverse (org-get-tags-at))))
  13201. tags
  13202. (if (or (eq t org-use-fast-tag-selection)
  13203. (and org-use-fast-tag-selection
  13204. (delq nil (mapcar 'cdr table))))
  13205. (org-fast-tag-selection
  13206. current-tags inherited-tags table
  13207. (if org-fast-tag-selection-include-todo
  13208. org-todo-key-alist))
  13209. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13210. (org-trim
  13211. (org-icompleting-read "Tags: "
  13212. 'org-tags-completion-function
  13213. nil nil current 'org-tags-history))))))
  13214. (while (string-match "[-+&]+" tags)
  13215. ;; No boolean logic, just a list
  13216. (setq tags (replace-match ":" t t tags))))
  13217. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13218. (if org-tags-sort-function
  13219. (setq tags (mapconcat 'identity
  13220. (sort (org-split-string
  13221. tags (org-re "[^[:alnum:]_@#%]+"))
  13222. org-tags-sort-function) ":")))
  13223. (if (string-match "\\`[\t ]*\\'" tags)
  13224. (setq tags "")
  13225. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13226. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13227. ;; Insert new tags at the correct column
  13228. (beginning-of-line 1)
  13229. (setq level (or (and (looking-at org-outline-regexp)
  13230. (- (match-end 0) (point) 1))
  13231. 1))
  13232. (cond
  13233. ((and (equal current "") (equal tags "")))
  13234. ((re-search-forward
  13235. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13236. (point-at-eol) t)
  13237. (if (equal tags "")
  13238. (setq rpl "")
  13239. (goto-char (match-beginning 0))
  13240. (setq c0 (current-column)
  13241. ;; compute offset for the case of org-indent-mode active
  13242. di (if org-indent-mode
  13243. (* (1- org-indent-indentation-per-level) (1- level))
  13244. 0)
  13245. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13246. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13247. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13248. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13249. (replace-match rpl t t)
  13250. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13251. tags)
  13252. (t (error "Tags alignment failed")))
  13253. (org-move-to-column col)
  13254. (unless just-align
  13255. (run-hooks 'org-after-tags-change-hook))))))
  13256. (defun org-change-tag-in-region (beg end tag off)
  13257. "Add or remove TAG for each entry in the region.
  13258. This works in the agenda, and also in an org-mode buffer."
  13259. (interactive
  13260. (list (region-beginning) (region-end)
  13261. (let ((org-last-tags-completion-table
  13262. (if (derived-mode-p 'org-mode)
  13263. (org-uniquify
  13264. (delq nil (append (org-get-buffer-tags)
  13265. (org-global-tags-completion-table))))
  13266. (org-global-tags-completion-table))))
  13267. (org-icompleting-read
  13268. "Tag: " 'org-tags-completion-function nil nil nil
  13269. 'org-tags-history))
  13270. (progn
  13271. (message "[s]et or [r]emove? ")
  13272. (equal (read-char-exclusive) ?r))))
  13273. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13274. (let ((agendap (equal major-mode 'org-agenda-mode))
  13275. l1 l2 m buf pos newhead (cnt 0))
  13276. (goto-char end)
  13277. (setq l2 (1- (org-current-line)))
  13278. (goto-char beg)
  13279. (setq l1 (org-current-line))
  13280. (loop for l from l1 to l2 do
  13281. (org-goto-line l)
  13282. (setq m (get-text-property (point) 'org-hd-marker))
  13283. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13284. (and agendap m))
  13285. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13286. pos (if agendap m (point)))
  13287. (with-current-buffer buf
  13288. (save-excursion
  13289. (save-restriction
  13290. (goto-char pos)
  13291. (setq cnt (1+ cnt))
  13292. (org-toggle-tag tag (if off 'off 'on))
  13293. (setq newhead (org-get-heading)))))
  13294. (and agendap (org-agenda-change-all-lines newhead m))))
  13295. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13296. (defun org-tags-completion-function (string predicate &optional flag)
  13297. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13298. (confirm (lambda (x) (stringp (car x)))))
  13299. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13300. (setq s1 (match-string 1 string)
  13301. s2 (match-string 2 string))
  13302. (setq s1 "" s2 string))
  13303. (cond
  13304. ((eq flag nil)
  13305. ;; try completion
  13306. (setq rtn (try-completion s2 ctable confirm))
  13307. (if (stringp rtn)
  13308. (setq rtn
  13309. (concat s1 s2 (substring rtn (length s2))
  13310. (if (and org-add-colon-after-tag-completion
  13311. (assoc rtn ctable))
  13312. ":" ""))))
  13313. rtn)
  13314. ((eq flag t)
  13315. ;; all-completions
  13316. (all-completions s2 ctable confirm))
  13317. ((eq flag 'lambda)
  13318. ;; exact match?
  13319. (assoc s2 ctable)))))
  13320. (defun org-fast-tag-insert (kwd tags face &optional end)
  13321. "Insert KDW, and the TAGS, the latter with face FACE.
  13322. Also insert END."
  13323. (insert (format "%-12s" (concat kwd ":"))
  13324. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13325. (or end "")))
  13326. (defun org-fast-tag-show-exit (flag)
  13327. (save-excursion
  13328. (org-goto-line 3)
  13329. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13330. (replace-match ""))
  13331. (when flag
  13332. (end-of-line 1)
  13333. (org-move-to-column (- (window-width) 19) t)
  13334. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13335. (defun org-set-current-tags-overlay (current prefix)
  13336. "Add an overlay to CURRENT tag with PREFIX."
  13337. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13338. (if (featurep 'xemacs)
  13339. (org-overlay-display org-tags-overlay (concat prefix s)
  13340. 'secondary-selection)
  13341. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13342. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13343. (defvar org-last-tag-selection-key nil)
  13344. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13345. "Fast tag selection with single keys.
  13346. CURRENT is the current list of tags in the headline, INHERITED is the
  13347. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13348. possibly with grouping information. TODO-TABLE is a similar table with
  13349. TODO keywords, should these have keys assigned to them.
  13350. If the keys are nil, a-z are automatically assigned.
  13351. Returns the new tags string, or nil to not change the current settings."
  13352. (let* ((fulltable (append table todo-table))
  13353. (maxlen (apply 'max (mapcar
  13354. (lambda (x)
  13355. (if (stringp (car x)) (string-width (car x)) 0))
  13356. fulltable)))
  13357. (buf (current-buffer))
  13358. (expert (eq org-fast-tag-selection-single-key 'expert))
  13359. (buffer-tags nil)
  13360. (fwidth (+ maxlen 3 1 3))
  13361. (ncol (/ (- (window-width) 4) fwidth))
  13362. (i-face 'org-done)
  13363. (c-face 'org-todo)
  13364. tg cnt e c char c1 c2 ntable tbl rtn
  13365. ov-start ov-end ov-prefix
  13366. (exit-after-next org-fast-tag-selection-single-key)
  13367. (done-keywords org-done-keywords)
  13368. groups ingroup)
  13369. (save-excursion
  13370. (beginning-of-line 1)
  13371. (if (looking-at
  13372. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13373. (setq ov-start (match-beginning 1)
  13374. ov-end (match-end 1)
  13375. ov-prefix "")
  13376. (setq ov-start (1- (point-at-eol))
  13377. ov-end (1+ ov-start))
  13378. (skip-chars-forward "^\n\r")
  13379. (setq ov-prefix
  13380. (concat
  13381. (buffer-substring (1- (point)) (point))
  13382. (if (> (current-column) org-tags-column)
  13383. " "
  13384. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13385. (move-overlay org-tags-overlay ov-start ov-end)
  13386. (save-window-excursion
  13387. (if expert
  13388. (set-buffer (get-buffer-create " *Org tags*"))
  13389. (delete-other-windows)
  13390. (split-window-vertically)
  13391. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13392. (erase-buffer)
  13393. (org-set-local 'org-done-keywords done-keywords)
  13394. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13395. (org-fast-tag-insert "Current" current c-face "\n\n")
  13396. (org-fast-tag-show-exit exit-after-next)
  13397. (org-set-current-tags-overlay current ov-prefix)
  13398. (setq tbl fulltable char ?a cnt 0)
  13399. (while (setq e (pop tbl))
  13400. (cond
  13401. ((equal (car e) :startgroup)
  13402. (push '() groups) (setq ingroup t)
  13403. (when (not (= cnt 0))
  13404. (setq cnt 0)
  13405. (insert "\n"))
  13406. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13407. ((equal (car e) :endgroup)
  13408. (setq ingroup nil cnt 0)
  13409. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13410. ((equal e '(:newline))
  13411. (when (not (= cnt 0))
  13412. (setq cnt 0)
  13413. (insert "\n")
  13414. (setq e (car tbl))
  13415. (while (equal (car tbl) '(:newline))
  13416. (insert "\n")
  13417. (setq tbl (cdr tbl)))))
  13418. ((equal e '(:grouptags)) nil)
  13419. (t
  13420. (setq tg (copy-sequence (car e)) c2 nil)
  13421. (if (cdr e)
  13422. (setq c (cdr e))
  13423. ;; automatically assign a character.
  13424. (setq c1 (string-to-char
  13425. (downcase (substring
  13426. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13427. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13428. (while (or (rassoc char ntable) (rassoc char table))
  13429. (setq char (1+ char)))
  13430. (setq c2 c1))
  13431. (setq c (or c2 char)))
  13432. (if ingroup (push tg (car groups)))
  13433. (setq tg (org-add-props tg nil 'face
  13434. (cond
  13435. ((not (assoc tg table))
  13436. (org-get-todo-face tg))
  13437. ((member tg current) c-face)
  13438. ((member tg inherited) i-face))))
  13439. (if (equal (caar tbl) :grouptags)
  13440. (org-add-props tg nil 'face 'org-tag-group))
  13441. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13442. (insert "[" c "] " tg (make-string
  13443. (- fwidth 4 (length tg)) ?\ ))
  13444. (push (cons tg c) ntable)
  13445. (when (= (setq cnt (1+ cnt)) ncol)
  13446. (insert "\n")
  13447. (if ingroup (insert " "))
  13448. (setq cnt 0)))))
  13449. (setq ntable (nreverse ntable))
  13450. (insert "\n")
  13451. (goto-char (point-min))
  13452. (if (not expert) (org-fit-window-to-buffer))
  13453. (setq rtn
  13454. (catch 'exit
  13455. (while t
  13456. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13457. (if (not groups) "no " "")
  13458. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13459. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13460. (setq org-last-tag-selection-key c)
  13461. (cond
  13462. ((= c ?\r) (throw 'exit t))
  13463. ((= c ?!)
  13464. (setq groups (not groups))
  13465. (goto-char (point-min))
  13466. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13467. ((= c ?\C-c)
  13468. (if (not expert)
  13469. (org-fast-tag-show-exit
  13470. (setq exit-after-next (not exit-after-next)))
  13471. (setq expert nil)
  13472. (delete-other-windows)
  13473. (set-window-buffer (split-window-vertically) " *Org tags*")
  13474. (org-switch-to-buffer-other-window " *Org tags*")
  13475. (org-fit-window-to-buffer)))
  13476. ((or (= c ?\C-g)
  13477. (and (= c ?q) (not (rassoc c ntable))))
  13478. (org-detach-overlay org-tags-overlay)
  13479. (setq quit-flag t))
  13480. ((= c ?\ )
  13481. (setq current nil)
  13482. (if exit-after-next (setq exit-after-next 'now)))
  13483. ((= c ?\t)
  13484. (condition-case nil
  13485. (setq tg (org-icompleting-read
  13486. "Tag: "
  13487. (or buffer-tags
  13488. (with-current-buffer buf
  13489. (org-get-buffer-tags)))))
  13490. (quit (setq tg "")))
  13491. (when (string-match "\\S-" tg)
  13492. (add-to-list 'buffer-tags (list tg))
  13493. (if (member tg current)
  13494. (setq current (delete tg current))
  13495. (push tg current)))
  13496. (if exit-after-next (setq exit-after-next 'now)))
  13497. ((setq e (rassoc c todo-table) tg (car e))
  13498. (with-current-buffer buf
  13499. (save-excursion (org-todo tg)))
  13500. (if exit-after-next (setq exit-after-next 'now)))
  13501. ((setq e (rassoc c ntable) tg (car e))
  13502. (if (member tg current)
  13503. (setq current (delete tg current))
  13504. (loop for g in groups do
  13505. (if (member tg g)
  13506. (mapc (lambda (x)
  13507. (setq current (delete x current)))
  13508. g)))
  13509. (push tg current))
  13510. (if exit-after-next (setq exit-after-next 'now))))
  13511. ;; Create a sorted list
  13512. (setq current
  13513. (sort current
  13514. (lambda (a b)
  13515. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13516. (if (eq exit-after-next 'now) (throw 'exit t))
  13517. (goto-char (point-min))
  13518. (beginning-of-line 2)
  13519. (delete-region (point) (point-at-eol))
  13520. (org-fast-tag-insert "Current" current c-face)
  13521. (org-set-current-tags-overlay current ov-prefix)
  13522. (while (re-search-forward
  13523. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13524. (setq tg (match-string 1))
  13525. (add-text-properties
  13526. (match-beginning 1) (match-end 1)
  13527. (list 'face
  13528. (cond
  13529. ((member tg current) c-face)
  13530. ((member tg inherited) i-face)
  13531. (t (get-text-property (match-beginning 1) 'face))))))
  13532. (goto-char (point-min)))))
  13533. (org-detach-overlay org-tags-overlay)
  13534. (if rtn
  13535. (mapconcat 'identity current ":")
  13536. nil))))
  13537. (defun org-get-tags-string ()
  13538. "Get the TAGS string in the current headline."
  13539. (unless (org-at-heading-p t)
  13540. (user-error "Not on a heading"))
  13541. (save-excursion
  13542. (beginning-of-line 1)
  13543. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13544. (org-match-string-no-properties 1)
  13545. "")))
  13546. (defun org-get-tags ()
  13547. "Get the list of tags specified in the current headline."
  13548. (org-split-string (org-get-tags-string) ":"))
  13549. (defun org-get-buffer-tags ()
  13550. "Get a table of all tags used in the buffer, for completion."
  13551. (let (tags)
  13552. (save-excursion
  13553. (goto-char (point-min))
  13554. (while (re-search-forward
  13555. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13556. (when (equal (char-after (point-at-bol 0)) ?*)
  13557. (mapc (lambda (x) (add-to-list 'tags x))
  13558. (org-split-string (org-match-string-no-properties 1) ":")))))
  13559. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13560. (mapcar 'list tags)))
  13561. ;;;; The mapping API
  13562. (defun org-map-entries (func &optional match scope &rest skip)
  13563. "Call FUNC at each headline selected by MATCH in SCOPE.
  13564. FUNC is a function or a lisp form. The function will be called without
  13565. arguments, with the cursor positioned at the beginning of the headline.
  13566. The return values of all calls to the function will be collected and
  13567. returned as a list.
  13568. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13569. does not need to preserve point. After evaluation, the cursor will be
  13570. moved to the end of the line (presumably of the headline of the
  13571. processed entry) and search continues from there. Under some
  13572. circumstances, this may not produce the wanted results. For example,
  13573. if you have removed (e.g. archived) the current (sub)tree it could
  13574. mean that the next entry will be skipped entirely. In such cases, you
  13575. can specify the position from where search should continue by making
  13576. FUNC set the variable `org-map-continue-from' to the desired buffer
  13577. position.
  13578. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13579. Only headlines that are matched by this query will be considered during
  13580. the iteration. When MATCH is nil or t, all headlines will be
  13581. visited by the iteration.
  13582. SCOPE determines the scope of this command. It can be any of:
  13583. nil The current buffer, respecting the restriction if any
  13584. tree The subtree started with the entry at point
  13585. region The entries within the active region, if any
  13586. region-start-level
  13587. The entries within the active region, but only those at
  13588. the same level than the first one.
  13589. file The current buffer, without restriction
  13590. file-with-archives
  13591. The current buffer, and any archives associated with it
  13592. agenda All agenda files
  13593. agenda-with-archives
  13594. All agenda files with any archive files associated with them
  13595. \(file1 file2 ...)
  13596. If this is a list, all files in the list will be scanned
  13597. The remaining args are treated as settings for the skipping facilities of
  13598. the scanner. The following items can be given here:
  13599. archive skip trees with the archive tag
  13600. comment skip trees with the COMMENT keyword
  13601. function or Emacs Lisp form:
  13602. will be used as value for `org-agenda-skip-function', so
  13603. whenever the function returns a position, FUNC will not be
  13604. called for that entry and search will continue from the
  13605. position returned
  13606. If your function needs to retrieve the tags including inherited tags
  13607. at the *current* entry, you can use the value of the variable
  13608. `org-scanner-tags' which will be much faster than getting the value
  13609. with `org-get-tags-at'. If your function gets properties with
  13610. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13611. to t around the call to `org-entry-properties' to get the same speedup.
  13612. Note that if your function moves around to retrieve tags and properties at
  13613. a *different* entry, you cannot use these techniques."
  13614. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13615. (not (org-region-active-p)))
  13616. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13617. (org-agenda-skip-archived-trees (memq 'archive skip))
  13618. (org-agenda-skip-comment-trees (memq 'comment skip))
  13619. (org-agenda-skip-function
  13620. (car (org-delete-all '(comment archive) skip)))
  13621. (org-tags-match-list-sublevels t)
  13622. (start-level (eq scope 'region-start-level))
  13623. matcher file res
  13624. org-todo-keywords-for-agenda
  13625. org-done-keywords-for-agenda
  13626. org-todo-keyword-alist-for-agenda
  13627. org-drawers-for-agenda
  13628. org-tag-alist-for-agenda
  13629. todo-only)
  13630. (cond
  13631. ((eq match t) (setq matcher t))
  13632. ((eq match nil) (setq matcher t))
  13633. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13634. (save-window-excursion
  13635. (save-restriction
  13636. (cond ((eq scope 'tree)
  13637. (org-back-to-heading t)
  13638. (org-narrow-to-subtree)
  13639. (setq scope nil))
  13640. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13641. (org-region-active-p))
  13642. ;; If needed, set start-level to a string like "2"
  13643. (when start-level
  13644. (save-excursion
  13645. (goto-char (region-beginning))
  13646. (unless (org-at-heading-p) (outline-next-heading))
  13647. (setq start-level (org-current-level))))
  13648. (narrow-to-region (region-beginning)
  13649. (save-excursion
  13650. (goto-char (region-end))
  13651. (unless (and (bolp) (org-at-heading-p))
  13652. (outline-next-heading))
  13653. (point)))
  13654. (setq scope nil)))
  13655. (if (not scope)
  13656. (progn
  13657. (org-agenda-prepare-buffers
  13658. (list (buffer-file-name (current-buffer))))
  13659. (setq res (org-scan-tags func matcher todo-only start-level)))
  13660. ;; Get the right scope
  13661. (cond
  13662. ((and scope (listp scope) (symbolp (car scope)))
  13663. (setq scope (eval scope)))
  13664. ((eq scope 'agenda)
  13665. (setq scope (org-agenda-files t)))
  13666. ((eq scope 'agenda-with-archives)
  13667. (setq scope (org-agenda-files t))
  13668. (setq scope (org-add-archive-files scope)))
  13669. ((eq scope 'file)
  13670. (setq scope (list (buffer-file-name))))
  13671. ((eq scope 'file-with-archives)
  13672. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13673. (org-agenda-prepare-buffers scope)
  13674. (while (setq file (pop scope))
  13675. (with-current-buffer (org-find-base-buffer-visiting file)
  13676. (save-excursion
  13677. (save-restriction
  13678. (widen)
  13679. (goto-char (point-min))
  13680. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13681. res)))
  13682. ;;;; Properties
  13683. ;;; Setting and retrieving properties
  13684. (defconst org-special-properties
  13685. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13686. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13687. "The special properties valid in Org-mode.
  13688. These are properties that are not defined in the property drawer,
  13689. but in some other way.")
  13690. (defconst org-default-properties
  13691. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13692. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13693. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13694. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13695. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13696. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13697. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13698. "Some properties that are used by Org-mode for various purposes.
  13699. Being in this list makes sure that they are offered for completion.")
  13700. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13701. "Regular expression matching the first line of a property drawer.")
  13702. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13703. "Regular expression matching the last line of a property drawer.")
  13704. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13705. "Regular expression matching the first line of a property drawer.")
  13706. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13707. "Regular expression matching the first line of a property drawer.")
  13708. (defconst org-property-drawer-re
  13709. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13710. org-property-end-re "\\)\n?")
  13711. "Matches an entire property drawer.")
  13712. (defconst org-clock-drawer-re
  13713. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13714. org-property-end-re "\\)\n?")
  13715. "Matches an entire clock drawer.")
  13716. (defsubst org-re-property (property)
  13717. "Return a regexp matching a PROPERTY line.
  13718. Match group 1 will be set to the value."
  13719. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13720. (defsubst org-re-property-keyword (property)
  13721. "Return a regexp matching a PROPERTY line, possibly with no
  13722. value for the property."
  13723. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13724. (defun org-property-action ()
  13725. "Do an action on properties."
  13726. (interactive)
  13727. (let (c)
  13728. (org-at-property-p)
  13729. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13730. (setq c (read-char-exclusive))
  13731. (cond
  13732. ((equal c ?s)
  13733. (call-interactively 'org-set-property))
  13734. ((equal c ?d)
  13735. (call-interactively 'org-delete-property))
  13736. ((equal c ?D)
  13737. (call-interactively 'org-delete-property-globally))
  13738. ((equal c ?c)
  13739. (call-interactively 'org-compute-property-at-point))
  13740. (t (user-error "No such property action %c" c)))))
  13741. (defun org-inc-effort ()
  13742. "Increment the value of the effort property in the current entry."
  13743. (interactive)
  13744. (org-set-effort nil t))
  13745. (defvar org-clock-effort) ;; Defined in org-clock.el
  13746. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13747. (defun org-set-effort (&optional value increment)
  13748. "Set the effort property of the current entry.
  13749. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13750. 10th allowed value.
  13751. When INCREMENT is non-nil, set the property to the next allowed value."
  13752. (interactive "P")
  13753. (if (equal value 0) (setq value 10))
  13754. (let* ((completion-ignore-case t)
  13755. (prop org-effort-property)
  13756. (cur (org-entry-get nil prop))
  13757. (allowed (org-property-get-allowed-values nil prop 'table))
  13758. (existing (mapcar 'list (org-property-values prop)))
  13759. (heading (nth 4 (org-heading-components)))
  13760. rpl
  13761. (val (cond
  13762. ((stringp value) value)
  13763. ((and allowed (integerp value))
  13764. (or (car (nth (1- value) allowed))
  13765. (car (org-last allowed))))
  13766. ((and allowed increment)
  13767. (or (caadr (member (list cur) allowed))
  13768. (user-error "Allowed effort values are not set")))
  13769. (allowed
  13770. (message "Select 1-9,0, [RET%s]: %s"
  13771. (if cur (concat "=" cur) "")
  13772. (mapconcat 'car allowed " "))
  13773. (setq rpl (read-char-exclusive))
  13774. (if (equal rpl ?\r)
  13775. cur
  13776. (setq rpl (- rpl ?0))
  13777. (if (equal rpl 0) (setq rpl 10))
  13778. (if (and (> rpl 0) (<= rpl (length allowed)))
  13779. (car (nth (1- rpl) allowed))
  13780. (org-completing-read "Effort: " allowed nil))))
  13781. (t
  13782. (let (org-completion-use-ido org-completion-use-iswitchb)
  13783. (org-completing-read
  13784. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13785. (concat "[" cur "]") "")
  13786. ": ")
  13787. existing nil nil "" nil cur))))))
  13788. (unless (equal (org-entry-get nil prop) val)
  13789. (org-entry-put nil prop val))
  13790. (save-excursion
  13791. (org-back-to-heading t)
  13792. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13793. (when (string= heading org-clock-current-task)
  13794. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13795. (org-clock-update-mode-line))
  13796. (message "%s is now %s" prop val)))
  13797. (defun org-at-property-p ()
  13798. "Is cursor inside a property drawer?"
  13799. (save-excursion
  13800. (beginning-of-line 1)
  13801. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13802. (save-match-data ;; Used by calling procedures
  13803. (let ((p (point))
  13804. (range (unless (org-before-first-heading-p)
  13805. (org-get-property-block))))
  13806. (and range (<= (car range) p) (< p (cdr range))))))))
  13807. (defun org-get-property-block (&optional beg end force)
  13808. "Return the (beg . end) range of the body of the property drawer.
  13809. BEG and END are the beginning and end of the current subtree, or of
  13810. the part before the first headline. If they are not given, they will
  13811. be found. If the drawer does not exist and FORCE is non-nil, create
  13812. the drawer."
  13813. (catch 'exit
  13814. (save-excursion
  13815. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13816. (progn (org-back-to-heading t) (point))))
  13817. (end (or end (and (not (outline-next-heading)) (point-max))
  13818. (point))))
  13819. (goto-char beg)
  13820. (if (re-search-forward org-property-start-re end t)
  13821. (setq beg (1+ (match-end 0)))
  13822. (if force
  13823. (save-excursion
  13824. (org-insert-property-drawer)
  13825. (setq end (progn (outline-next-heading) (point))))
  13826. (throw 'exit nil))
  13827. (goto-char beg)
  13828. (if (re-search-forward org-property-start-re end t)
  13829. (setq beg (1+ (match-end 0)))))
  13830. (if (re-search-forward org-property-end-re end t)
  13831. (setq end (match-beginning 0))
  13832. (or force (throw 'exit nil))
  13833. (goto-char beg)
  13834. (setq end beg)
  13835. (org-indent-line)
  13836. (insert ":END:\n"))
  13837. (cons beg end)))))
  13838. (defun org-entry-properties (&optional pom which specific)
  13839. "Get all properties of the entry at point-or-marker POM.
  13840. This includes the TODO keyword, the tags, time strings for deadline,
  13841. scheduled, and clocking, and any additional properties defined in the
  13842. entry. The return value is an alist, keys may occur multiple times
  13843. if the property key was used several times.
  13844. POM may also be nil, in which case the current entry is used.
  13845. If WHICH is nil or `all', get all properties. If WHICH is
  13846. `special' or `standard', only get that subclass. If WHICH
  13847. is a string only get exactly this property. SPECIFIC can be a string, the
  13848. specific property we are interested in. Specifying it can speed
  13849. things up because then unnecessary parsing is avoided."
  13850. (setq which (or which 'all))
  13851. (org-with-point-at pom
  13852. (let ((clockstr (substring org-clock-string 0 -1))
  13853. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13854. (case-fold-search nil)
  13855. beg end range props sum-props key key1 value string clocksum clocksumt)
  13856. (save-excursion
  13857. (when (condition-case nil
  13858. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13859. (error nil))
  13860. (setq beg (point))
  13861. (setq sum-props (get-text-property (point) 'org-summaries))
  13862. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13863. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13864. (outline-next-heading)
  13865. (setq end (point))
  13866. (when (memq which '(all special))
  13867. ;; Get the special properties, like TODO and tags
  13868. (goto-char beg)
  13869. (when (and (or (not specific) (string= specific "TODO"))
  13870. (looking-at org-todo-line-regexp) (match-end 2))
  13871. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13872. (when (and (or (not specific) (string= specific "PRIORITY"))
  13873. (looking-at org-priority-regexp))
  13874. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13875. (when (or (not specific) (string= specific "FILE"))
  13876. (push (cons "FILE" buffer-file-name) props))
  13877. (when (and (or (not specific) (string= specific "TAGS"))
  13878. (setq value (org-get-tags-string))
  13879. (string-match "\\S-" value))
  13880. (push (cons "TAGS" value) props))
  13881. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13882. (setq value (org-get-tags-at)))
  13883. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13884. ":"))
  13885. props))
  13886. (when (or (not specific) (string= specific "BLOCKED"))
  13887. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13888. (when (or (not specific)
  13889. (member specific
  13890. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13891. "TIMESTAMP" "TIMESTAMP_IA")))
  13892. (catch 'match
  13893. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13894. (setq key (if (match-end 1)
  13895. (substring (org-match-string-no-properties 1)
  13896. 0 -1))
  13897. string (if (equal key clockstr)
  13898. (org-trim
  13899. (buffer-substring-no-properties
  13900. (match-beginning 3) (goto-char
  13901. (point-at-eol))))
  13902. (substring (org-match-string-no-properties 3)
  13903. 1 -1)))
  13904. ;; Get the correct property name from the key. This is
  13905. ;; necessary if the user has configured time keywords.
  13906. (setq key1 (concat key ":"))
  13907. (cond
  13908. ((not key)
  13909. (setq key
  13910. (if (= (char-after (match-beginning 3)) ?\[)
  13911. "TIMESTAMP_IA" "TIMESTAMP")))
  13912. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13913. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13914. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13915. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13916. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13917. (progn
  13918. (push (cons key string) props)
  13919. ;; no need to search further if match is found
  13920. (throw 'match t))
  13921. (when (or (equal key "CLOCK") (not (assoc key props)))
  13922. (push (cons key string) props)))))))
  13923. (when (memq which '(all standard))
  13924. ;; Get the standard properties, like :PROP: ...
  13925. (setq range (org-get-property-block beg end))
  13926. (when range
  13927. (goto-char (car range))
  13928. (while (re-search-forward
  13929. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13930. (cdr range) t)
  13931. (setq key (org-match-string-no-properties 1)
  13932. value (org-trim (or (org-match-string-no-properties 2) "")))
  13933. (unless (member key excluded)
  13934. (push (cons key (or value "")) props)))))
  13935. (if clocksum
  13936. (push (cons "CLOCKSUM"
  13937. (org-columns-number-to-string (/ (float clocksum) 60.)
  13938. 'add_times))
  13939. props))
  13940. (if clocksumt
  13941. (push (cons "CLOCKSUM_T"
  13942. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13943. 'add_times))
  13944. props))
  13945. (unless (assoc "CATEGORY" props)
  13946. (push (cons "CATEGORY" (org-get-category)) props))
  13947. (append sum-props (nreverse props)))))))
  13948. (defun org-entry-get (pom property &optional inherit literal-nil)
  13949. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13950. If INHERIT is non-nil and the entry does not have the property,
  13951. then also check higher levels of the hierarchy.
  13952. If INHERIT is the symbol `selective', use inheritance only if the setting
  13953. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13954. If the property is present but empty, the return value is the empty string.
  13955. If the property is not present at all, nil is returned.
  13956. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13957. do not interpret it as the list atom nil. This is used for inheritance
  13958. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13959. (org-with-point-at pom
  13960. (if (and inherit (if (eq inherit 'selective)
  13961. (org-property-inherit-p property)
  13962. t))
  13963. (org-entry-get-with-inheritance property literal-nil)
  13964. (if (member property org-special-properties)
  13965. ;; We need a special property. Use `org-entry-properties' to
  13966. ;; retrieve it, but specify the wanted property
  13967. (cdr (assoc property (org-entry-properties nil 'special property)))
  13968. (let ((range (org-get-property-block)))
  13969. (when (and range (not (eq (car range) (cdr range))))
  13970. (let* ((props (list (or (assoc property org-file-properties)
  13971. (assoc property org-global-properties)
  13972. (assoc property org-global-properties-fixed))))
  13973. (ap (lambda (key)
  13974. (when (re-search-forward
  13975. (org-re-property key) (cdr range) t)
  13976. (setq props
  13977. (org-update-property-plist
  13978. key
  13979. (if (match-end 1)
  13980. (org-match-string-no-properties 1) "")
  13981. props)))))
  13982. val)
  13983. (goto-char (car range))
  13984. (funcall ap property)
  13985. (goto-char (car range))
  13986. (while (funcall ap (concat property "+")))
  13987. (setq val (cdr (assoc property props)))
  13988. (when val (if literal-nil val (org-not-nil val))))))))))
  13989. (defun org-property-or-variable-value (var &optional inherit)
  13990. "Check if there is a property fixing the value of VAR.
  13991. If yes, return this value. If not, return the current value of the variable."
  13992. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13993. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13994. (read prop)
  13995. (symbol-value var))))
  13996. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13997. "Delete the property PROPERTY from entry at point-or-marker POM.
  13998. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13999. an empty drawer to delete."
  14000. (org-with-point-at pom
  14001. (if (member property org-special-properties)
  14002. nil ; cannot delete these properties.
  14003. (let ((range (org-get-property-block)))
  14004. (if (and range
  14005. (goto-char (car range))
  14006. (re-search-forward
  14007. (org-re-property property)
  14008. (cdr range) t))
  14009. (progn
  14010. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14011. (and delete-empty-drawer
  14012. (org-remove-empty-drawer-at
  14013. delete-empty-drawer (car range)))
  14014. t)
  14015. nil)))))
  14016. ;; Multi-values properties are properties that contain multiple values
  14017. ;; These values are assumed to be single words, separated by whitespace.
  14018. (defun org-entry-add-to-multivalued-property (pom property value)
  14019. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14020. (let* ((old (org-entry-get pom property))
  14021. (values (and old (org-split-string old "[ \t]"))))
  14022. (setq value (org-entry-protect-space value))
  14023. (unless (member value values)
  14024. (setq values (append values (list value)))
  14025. (org-entry-put pom property
  14026. (mapconcat 'identity values " ")))))
  14027. (defun org-entry-remove-from-multivalued-property (pom property value)
  14028. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14029. (let* ((old (org-entry-get pom property))
  14030. (values (and old (org-split-string old "[ \t]"))))
  14031. (setq value (org-entry-protect-space value))
  14032. (when (member value values)
  14033. (setq values (delete value values))
  14034. (org-entry-put pom property
  14035. (mapconcat 'identity values " ")))))
  14036. (defun org-entry-member-in-multivalued-property (pom property value)
  14037. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14038. (let* ((old (org-entry-get pom property))
  14039. (values (and old (org-split-string old "[ \t]"))))
  14040. (setq value (org-entry-protect-space value))
  14041. (member value values)))
  14042. (defun org-entry-get-multivalued-property (pom property)
  14043. "Return a list of values in a multivalued property."
  14044. (let* ((value (org-entry-get pom property))
  14045. (values (and value (org-split-string value "[ \t]"))))
  14046. (mapcar 'org-entry-restore-space values)))
  14047. (defun org-entry-put-multivalued-property (pom property &rest values)
  14048. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14049. VALUES should be a list of strings. Spaces will be protected."
  14050. (org-entry-put pom property
  14051. (mapconcat 'org-entry-protect-space values " "))
  14052. (let* ((value (org-entry-get pom property))
  14053. (values (and value (org-split-string value "[ \t]"))))
  14054. (mapcar 'org-entry-restore-space values)))
  14055. (defun org-entry-protect-space (s)
  14056. "Protect spaces and newline in string S."
  14057. (while (string-match " " s)
  14058. (setq s (replace-match "%20" t t s)))
  14059. (while (string-match "\n" s)
  14060. (setq s (replace-match "%0A" t t s)))
  14061. s)
  14062. (defun org-entry-restore-space (s)
  14063. "Restore spaces and newline in string S."
  14064. (while (string-match "%20" s)
  14065. (setq s (replace-match " " t t s)))
  14066. (while (string-match "%0A" s)
  14067. (setq s (replace-match "\n" t t s)))
  14068. s)
  14069. (defvar org-entry-property-inherited-from (make-marker)
  14070. "Marker pointing to the entry from where a property was inherited.
  14071. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14072. location of the entry where the inheritance search matched. If there was
  14073. no match, the marker will point nowhere.
  14074. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14075. is set.")
  14076. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14077. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14078. The search will stop at the first ancestor which has the property defined.
  14079. If the value found is \"nil\", return nil to show that the property
  14080. should be considered as undefined (this is the meaning of nil here).
  14081. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14082. (move-marker org-entry-property-inherited-from nil)
  14083. (let (tmp)
  14084. (save-excursion
  14085. (save-restriction
  14086. (widen)
  14087. (catch 'ex
  14088. (while t
  14089. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14090. (or (ignore-errors (org-back-to-heading t))
  14091. (goto-char (point-min)))
  14092. (move-marker org-entry-property-inherited-from (point))
  14093. (throw 'ex tmp))
  14094. (or (ignore-errors (org-up-heading-safe))
  14095. (throw 'ex nil))))))
  14096. (setq tmp (or tmp
  14097. (cdr (assoc property org-file-properties))
  14098. (cdr (assoc property org-global-properties))
  14099. (cdr (assoc property org-global-properties-fixed))))
  14100. (if literal-nil tmp (org-not-nil tmp))))
  14101. (defvar org-property-changed-functions nil
  14102. "Hook called when the value of a property has changed.
  14103. Each hook function should accept two arguments, the name of the property
  14104. and the new value.")
  14105. (defun org-entry-put (pom property value)
  14106. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14107. (org-with-point-at pom
  14108. (org-back-to-heading t)
  14109. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14110. range)
  14111. (cond
  14112. ((equal property "TODO")
  14113. (when (and (stringp value) (string-match "\\S-" value)
  14114. (not (member value org-todo-keywords-1)))
  14115. (user-error "\"%s\" is not a valid TODO state" value))
  14116. (if (or (not value)
  14117. (not (string-match "\\S-" value)))
  14118. (setq value 'none))
  14119. (org-todo value)
  14120. (org-set-tags nil 'align))
  14121. ((equal property "PRIORITY")
  14122. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14123. (string-to-char value) ?\ ))
  14124. (org-set-tags nil 'align))
  14125. ((equal property "CLOCKSUM")
  14126. (if (not (re-search-forward
  14127. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14128. (error "Cannot find a clock log")
  14129. (goto-char (- (match-end 1) 2))
  14130. (cond
  14131. ((eq value 'earlier) (org-timestamp-down))
  14132. ((eq value 'later) (org-timestamp-up)))
  14133. (org-clock-sum-current-item)))
  14134. ((equal property "SCHEDULED")
  14135. (if (re-search-forward org-scheduled-time-regexp end t)
  14136. (cond
  14137. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14138. ((eq value 'later) (org-timestamp-change 1 'day))
  14139. (t (call-interactively 'org-schedule)))
  14140. (call-interactively 'org-schedule)))
  14141. ((equal property "DEADLINE")
  14142. (if (re-search-forward org-deadline-time-regexp end t)
  14143. (cond
  14144. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14145. ((eq value 'later) (org-timestamp-change 1 'day))
  14146. (t (call-interactively 'org-deadline)))
  14147. (call-interactively 'org-deadline)))
  14148. ((member property org-special-properties)
  14149. (error "The %s property can not yet be set with `org-entry-put'"
  14150. property))
  14151. (t ; a non-special property
  14152. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14153. (setq range (org-get-property-block beg end 'force))
  14154. (goto-char (car range))
  14155. (if (re-search-forward
  14156. (org-re-property-keyword property) (cdr range) t)
  14157. (progn
  14158. (delete-region (match-beginning 0) (match-end 0))
  14159. (goto-char (match-beginning 0)))
  14160. (goto-char (cdr range))
  14161. (insert "\n")
  14162. (backward-char 1)
  14163. (org-indent-line))
  14164. (insert ":" property ":")
  14165. (and value (insert " " value))
  14166. (org-indent-line)))))
  14167. (run-hook-with-args 'org-property-changed-functions property value)))
  14168. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14169. "Get all property keys in the current buffer.
  14170. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14171. like tags and TODO state.
  14172. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14173. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14174. and others.
  14175. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14176. formats in the current buffer."
  14177. (let (rtn range cfmt s p)
  14178. (save-excursion
  14179. (save-restriction
  14180. (widen)
  14181. (goto-char (point-min))
  14182. (while (re-search-forward org-property-start-re nil t)
  14183. (setq range (org-get-property-block))
  14184. (goto-char (car range))
  14185. (while (re-search-forward
  14186. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14187. (cdr range) t)
  14188. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14189. (outline-next-heading))))
  14190. (when include-specials
  14191. (setq rtn (append org-special-properties rtn)))
  14192. (when include-defaults
  14193. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14194. (add-to-list 'rtn org-effort-property))
  14195. (when include-columns
  14196. (save-excursion
  14197. (save-restriction
  14198. (widen)
  14199. (goto-char (point-min))
  14200. (while (re-search-forward
  14201. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14202. nil t)
  14203. (setq cfmt (match-string 2) s 0)
  14204. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14205. cfmt s)
  14206. (setq s (match-end 0)
  14207. p (match-string 1 cfmt))
  14208. (unless (or (equal p "ITEM")
  14209. (member p org-special-properties))
  14210. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14211. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14212. (defun org-property-values (key)
  14213. "Return a list of all values of property KEY in the current buffer."
  14214. (save-excursion
  14215. (save-restriction
  14216. (widen)
  14217. (goto-char (point-min))
  14218. (let ((re (org-re-property key))
  14219. values)
  14220. (while (re-search-forward re nil t)
  14221. (add-to-list 'values (org-trim (match-string 1))))
  14222. (delete "" values)))))
  14223. (defun org-insert-property-drawer ()
  14224. "Insert a property drawer into the current entry."
  14225. (org-back-to-heading t)
  14226. (looking-at org-outline-regexp)
  14227. (let ((indent (if org-adapt-indentation
  14228. (- (match-end 0) (match-beginning 0))
  14229. 0))
  14230. (beg (point))
  14231. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14232. end hiddenp)
  14233. (outline-next-heading)
  14234. (setq end (point))
  14235. (goto-char beg)
  14236. (while (re-search-forward re end t))
  14237. (setq hiddenp (outline-invisible-p))
  14238. (end-of-line 1)
  14239. (and (equal (char-after) ?\n) (forward-char 1))
  14240. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14241. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14242. ;; just skip this line
  14243. (beginning-of-line 2)
  14244. ;; Drawer start, find the end
  14245. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14246. (beginning-of-line 1)))
  14247. (org-skip-over-state-notes)
  14248. (skip-chars-backward " \t\n\r")
  14249. (if (eq (char-before) ?*) (forward-char 1))
  14250. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14251. (beginning-of-line 0)
  14252. (org-indent-to-column indent)
  14253. (beginning-of-line 2)
  14254. (org-indent-to-column indent)
  14255. (beginning-of-line 0)
  14256. (if hiddenp
  14257. (save-excursion
  14258. (org-back-to-heading t)
  14259. (hide-entry))
  14260. (org-flag-drawer t))))
  14261. (defun org-insert-drawer (&optional arg drawer)
  14262. "Insert a drawer at point.
  14263. Optional argument DRAWER, when non-nil, is a string representing
  14264. drawer's name. Otherwise, the user is prompted for a name.
  14265. If a region is active, insert the drawer around that region
  14266. instead.
  14267. Point is left between drawer's boundaries."
  14268. (interactive "P")
  14269. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14270. "LOGBOOK"))
  14271. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14272. ;; internally by Org. They are meant to be inserted
  14273. ;; automatically.
  14274. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14275. ;; Remove system drawers from list. Note: For some reason,
  14276. ;; `org-completing-read' ignores the predicate while
  14277. ;; `completing-read' handles it fine.
  14278. (drawer (if arg "PROPERTIES"
  14279. (or drawer
  14280. (completing-read
  14281. "Drawer: " org-drawers
  14282. (lambda (d) (not (member d system-drawers))))))))
  14283. (cond
  14284. ;; With C-u, fall back on `org-insert-property-drawer'
  14285. (arg (org-insert-property-drawer))
  14286. ;; With an active region, insert a drawer at point.
  14287. ((not (org-region-active-p))
  14288. (progn
  14289. (unless (bolp) (insert "\n"))
  14290. (insert (format ":%s:\n\n:END:\n" drawer))
  14291. (forward-line -2)))
  14292. ;; Otherwise, insert the drawer at point
  14293. (t
  14294. (let ((rbeg (region-beginning))
  14295. (rend (copy-marker (region-end))))
  14296. (unwind-protect
  14297. (progn
  14298. (goto-char rbeg)
  14299. (beginning-of-line)
  14300. (when (save-excursion
  14301. (re-search-forward org-outline-regexp-bol rend t))
  14302. (user-error "Drawers cannot contain headlines"))
  14303. ;; Position point at the beginning of the first
  14304. ;; non-blank line in region. Insert drawer's opening
  14305. ;; there, then indent it.
  14306. (org-skip-whitespace)
  14307. (beginning-of-line)
  14308. (insert ":" drawer ":\n")
  14309. (forward-line -1)
  14310. (indent-for-tab-command)
  14311. ;; Move point to the beginning of the first blank line
  14312. ;; after the last non-blank line in region. Insert
  14313. ;; drawer's closing, then indent it.
  14314. (goto-char rend)
  14315. (skip-chars-backward " \r\t\n")
  14316. (insert "\n:END:")
  14317. (deactivate-mark t)
  14318. (indent-for-tab-command)
  14319. (unless (eolp) (insert "\n")))
  14320. ;; Clear marker, whatever the outcome of insertion is.
  14321. (set-marker rend nil)))))))
  14322. (defvar org-property-set-functions-alist nil
  14323. "Property set function alist.
  14324. Each entry should have the following format:
  14325. (PROPERTY . READ-FUNCTION)
  14326. The read function will be called with the same argument as
  14327. `org-completing-read'.")
  14328. (defun org-set-property-function (property)
  14329. "Get the function that should be used to set PROPERTY.
  14330. This is computed according to `org-property-set-functions-alist'."
  14331. (or (cdr (assoc property org-property-set-functions-alist))
  14332. 'org-completing-read))
  14333. (defun org-read-property-value (property)
  14334. "Read PROPERTY value from user."
  14335. (let* ((completion-ignore-case t)
  14336. (allowed (org-property-get-allowed-values nil property 'table))
  14337. (cur (org-entry-get nil property))
  14338. (prompt (concat property " value"
  14339. (if (and cur (string-match "\\S-" cur))
  14340. (concat " [" cur "]") "") ": "))
  14341. (set-function (org-set-property-function property))
  14342. (val (if allowed
  14343. (funcall set-function prompt allowed nil
  14344. (not (get-text-property 0 'org-unrestricted
  14345. (caar allowed))))
  14346. (let (org-completion-use-ido org-completion-use-iswitchb)
  14347. (funcall set-function prompt
  14348. (mapcar 'list (org-property-values property))
  14349. nil nil "" nil cur)))))
  14350. (if (equal val "")
  14351. cur
  14352. val)))
  14353. (defvar org-last-set-property nil)
  14354. (defvar org-last-set-property-value nil)
  14355. (defun org-read-property-name ()
  14356. "Read a property name."
  14357. (let* ((completion-ignore-case t)
  14358. (keys (org-buffer-property-keys nil t t))
  14359. (default-prop (or (save-excursion
  14360. (save-match-data
  14361. (beginning-of-line)
  14362. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14363. (null (string= (match-string 1) "END"))
  14364. (match-string 1))))
  14365. org-last-set-property))
  14366. (property (org-icompleting-read
  14367. (concat "Property"
  14368. (if default-prop (concat " [" default-prop "]") "")
  14369. ": ")
  14370. (mapcar 'list keys)
  14371. nil nil nil nil
  14372. default-prop)))
  14373. (if (member property keys)
  14374. property
  14375. (or (cdr (assoc (downcase property)
  14376. (mapcar (lambda (x) (cons (downcase x) x))
  14377. keys)))
  14378. property))))
  14379. (defun org-set-property-and-value (use-last)
  14380. "Allow to set [PROPERTY]: [value] direction from prompt.
  14381. When use-default, don't even ask, just use the last
  14382. \"[PROPERTY]: [value]\" string from the history."
  14383. (interactive "P")
  14384. (let* ((completion-ignore-case t)
  14385. (pv (or (and use-last org-last-set-property-value)
  14386. (org-completing-read
  14387. "Enter a \"[Property]: [value]\" pair: "
  14388. nil nil nil nil nil
  14389. org-last-set-property-value)))
  14390. prop val)
  14391. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14392. (setq prop (match-string 1 pv)
  14393. val (match-string 2 pv))
  14394. (org-set-property prop val))))
  14395. (defun org-set-property (property value)
  14396. "In the current entry, set PROPERTY to VALUE.
  14397. When called interactively, this will prompt for a property name, offering
  14398. completion on existing and default properties. And then it will prompt
  14399. for a value, offering completion either on allowed values (via an inherited
  14400. xxx_ALL property) or on existing values in other instances of this property
  14401. in the current file."
  14402. (interactive (list nil nil))
  14403. (let* ((property (or property (org-read-property-name)))
  14404. (value (or value (org-read-property-value property)))
  14405. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14406. (setq org-last-set-property property)
  14407. (setq org-last-set-property-value (concat property ": " value))
  14408. ;; Possibly postprocess the inserted value:
  14409. (when fn (setq value (funcall fn value)))
  14410. (unless (equal (org-entry-get nil property) value)
  14411. (org-entry-put nil property value))))
  14412. (defun org-delete-property (property &optional delete-empty-drawer)
  14413. "In the current entry, delete PROPERTY.
  14414. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14415. an empty drawer to delete."
  14416. (interactive
  14417. (let* ((completion-ignore-case t)
  14418. (prop (org-icompleting-read "Property: "
  14419. (org-entry-properties nil 'standard))))
  14420. (list prop)))
  14421. (message "Property %s %s" property
  14422. (if (org-entry-delete nil property delete-empty-drawer)
  14423. "deleted"
  14424. "was not present in the entry")))
  14425. (defun org-delete-property-globally (property)
  14426. "Remove PROPERTY globally, from all entries."
  14427. (interactive
  14428. (let* ((completion-ignore-case t)
  14429. (prop (org-icompleting-read
  14430. "Globally remove property: "
  14431. (mapcar 'list (org-buffer-property-keys)))))
  14432. (list prop)))
  14433. (save-excursion
  14434. (save-restriction
  14435. (widen)
  14436. (goto-char (point-min))
  14437. (let ((cnt 0))
  14438. (while (re-search-forward
  14439. (org-re-property property)
  14440. nil t)
  14441. (setq cnt (1+ cnt))
  14442. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14443. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14444. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14445. (defun org-compute-property-at-point ()
  14446. "Compute the property at point.
  14447. This looks for an enclosing column format, extracts the operator and
  14448. then applies it to the property in the column format's scope."
  14449. (interactive)
  14450. (unless (org-at-property-p)
  14451. (user-error "Not at a property"))
  14452. (let ((prop (org-match-string-no-properties 2)))
  14453. (org-columns-get-format-and-top-level)
  14454. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14455. (user-error "No operator defined for property %s" prop))
  14456. (org-columns-compute prop)))
  14457. (defvar org-property-allowed-value-functions nil
  14458. "Hook for functions supplying allowed values for a specific property.
  14459. The functions must take a single argument, the name of the property, and
  14460. return a flat list of allowed values. If \":ETC\" is one of
  14461. the values, this means that these values are intended as defaults for
  14462. completion, but that other values should be allowed too.
  14463. The functions must return nil if they are not responsible for this
  14464. property.")
  14465. (defun org-property-get-allowed-values (pom property &optional table)
  14466. "Get allowed values for the property PROPERTY.
  14467. When TABLE is non-nil, return an alist that can directly be used for
  14468. completion."
  14469. (let (vals)
  14470. (cond
  14471. ((equal property "TODO")
  14472. (setq vals (org-with-point-at pom
  14473. (append org-todo-keywords-1 '("")))))
  14474. ((equal property "PRIORITY")
  14475. (let ((n org-lowest-priority))
  14476. (while (>= n org-highest-priority)
  14477. (push (char-to-string n) vals)
  14478. (setq n (1- n)))))
  14479. ((member property org-special-properties))
  14480. ((setq vals (run-hook-with-args-until-success
  14481. 'org-property-allowed-value-functions property)))
  14482. (t
  14483. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14484. (when (and vals (string-match "\\S-" vals))
  14485. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14486. (setq vals (mapcar (lambda (x)
  14487. (cond ((stringp x) x)
  14488. ((numberp x) (number-to-string x))
  14489. ((symbolp x) (symbol-name x))
  14490. (t "???")))
  14491. vals)))))
  14492. (when (member ":ETC" vals)
  14493. (setq vals (remove ":ETC" vals))
  14494. (org-add-props (car vals) '(org-unrestricted t)))
  14495. (if table (mapcar 'list vals) vals)))
  14496. (defun org-property-previous-allowed-value (&optional previous)
  14497. "Switch to the next allowed value for this property."
  14498. (interactive)
  14499. (org-property-next-allowed-value t))
  14500. (defun org-property-next-allowed-value (&optional previous)
  14501. "Switch to the next allowed value for this property."
  14502. (interactive)
  14503. (unless (org-at-property-p)
  14504. (user-error "Not at a property"))
  14505. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14506. (key (match-string 2))
  14507. (value (match-string 3))
  14508. (allowed (or (org-property-get-allowed-values (point) key)
  14509. (and (member value '("[ ]" "[-]" "[X]"))
  14510. '("[ ]" "[X]"))))
  14511. (heading (save-match-data (nth 4 (org-heading-components))))
  14512. nval)
  14513. (unless allowed
  14514. (user-error "Allowed values for this property have not been defined"))
  14515. (if previous (setq allowed (reverse allowed)))
  14516. (if (member value allowed)
  14517. (setq nval (car (cdr (member value allowed)))))
  14518. (setq nval (or nval (car allowed)))
  14519. (if (equal nval value)
  14520. (user-error "Only one allowed value for this property"))
  14521. (org-at-property-p)
  14522. (replace-match (concat " :" key ": " nval) t t)
  14523. (org-indent-line)
  14524. (beginning-of-line 1)
  14525. (skip-chars-forward " \t")
  14526. (when (equal prop org-effort-property)
  14527. (save-excursion
  14528. (org-back-to-heading t)
  14529. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14530. (when (string= org-clock-current-task heading)
  14531. (setq org-clock-effort nval)
  14532. (org-clock-update-mode-line)))
  14533. (run-hook-with-args 'org-property-changed-functions key nval)))
  14534. (defun org-find-olp (path &optional this-buffer)
  14535. "Return a marker pointing to the entry at outline path OLP.
  14536. If anything goes wrong, throw an error.
  14537. You can wrap this call to catch the error like this:
  14538. (condition-case msg
  14539. (org-mobile-locate-entry (match-string 4))
  14540. (error (nth 1 msg)))
  14541. The return value will then be either a string with the error message,
  14542. or a marker if everything is OK.
  14543. If THIS-BUFFER is set, the outline path does not contain a file,
  14544. only headings."
  14545. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14546. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14547. (level 1)
  14548. (lmin 1)
  14549. (lmax 1)
  14550. limit re end found pos heading cnt flevel)
  14551. (unless buffer (error "File not found :%s" file))
  14552. (with-current-buffer buffer
  14553. (save-excursion
  14554. (save-restriction
  14555. (widen)
  14556. (setq limit (point-max))
  14557. (goto-char (point-min))
  14558. (while (setq heading (pop path))
  14559. (setq re (format org-complex-heading-regexp-format
  14560. (regexp-quote heading)))
  14561. (setq cnt 0 pos (point))
  14562. (while (re-search-forward re end t)
  14563. (setq level (- (match-end 1) (match-beginning 1)))
  14564. (if (and (>= level lmin) (<= level lmax))
  14565. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14566. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14567. lmax heading))
  14568. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14569. lmax heading))
  14570. (goto-char found)
  14571. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14572. (setq end (save-excursion (org-end-of-subtree t t))))
  14573. (when (org-at-heading-p)
  14574. (point-marker)))))))
  14575. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14576. "Find node HEADING in BUFFER.
  14577. Return a marker to the heading if it was found, or nil if not.
  14578. If POS-ONLY is set, return just the position instead of a marker.
  14579. The heading text must match exact, but it may have a TODO keyword,
  14580. a priority cookie and tags in the standard locations."
  14581. (with-current-buffer (or buffer (current-buffer))
  14582. (save-excursion
  14583. (save-restriction
  14584. (widen)
  14585. (goto-char (point-min))
  14586. (let (case-fold-search)
  14587. (if (re-search-forward
  14588. (format org-complex-heading-regexp-format
  14589. (regexp-quote heading)) nil t)
  14590. (if pos-only
  14591. (match-beginning 0)
  14592. (move-marker (make-marker) (match-beginning 0)))))))))
  14593. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14594. "Find Org node headline HEADING in all .org files in directory DIR.
  14595. When the target headline is found, return a marker to this location."
  14596. (let ((files (directory-files (or dir default-directory)
  14597. nil "\\`[^.#].*\\.org\\'"))
  14598. file visiting m buffer)
  14599. (catch 'found
  14600. (while (setq file (pop files))
  14601. (message "trying %s" file)
  14602. (setq visiting (org-find-base-buffer-visiting file))
  14603. (setq buffer (or visiting (find-file-noselect file)))
  14604. (setq m (org-find-exact-headline-in-buffer
  14605. heading buffer))
  14606. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14607. (and m (throw 'found m))))))
  14608. (defun org-find-entry-with-id (ident)
  14609. "Locate the entry that contains the ID property with exact value IDENT.
  14610. IDENT can be a string, a symbol or a number, this function will search for
  14611. the string representation of it.
  14612. Return the position where this entry starts, or nil if there is no such entry."
  14613. (interactive "sID: ")
  14614. (let ((id (cond
  14615. ((stringp ident) ident)
  14616. ((symbol-name ident) (symbol-name ident))
  14617. ((numberp ident) (number-to-string ident))
  14618. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14619. (case-fold-search nil))
  14620. (save-excursion
  14621. (save-restriction
  14622. (widen)
  14623. (goto-char (point-min))
  14624. (when (re-search-forward
  14625. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14626. nil t)
  14627. (org-back-to-heading t)
  14628. (point))))))
  14629. ;;;; Timestamps
  14630. (defvar org-last-changed-timestamp nil)
  14631. (defvar org-last-inserted-timestamp nil
  14632. "The last time stamp inserted with `org-insert-time-stamp'.")
  14633. (defvar org-time-was-given) ; dynamically scoped parameter
  14634. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14635. (defvar org-ts-what) ; dynamically scoped parameter
  14636. (defun org-time-stamp (arg &optional inactive)
  14637. "Prompt for a date/time and insert a time stamp.
  14638. If the user specifies a time like HH:MM or if this command is
  14639. called with at least one prefix argument, the time stamp contains
  14640. the date and the time. Otherwise, only the date is be included.
  14641. All parts of a date not specified by the user is filled in from
  14642. the current date/time. So if you just press return without
  14643. typing anything, the time stamp will represent the current
  14644. date/time.
  14645. If there is already a timestamp at the cursor, it will be
  14646. modified.
  14647. With two universal prefix arguments, insert an active timestamp
  14648. with the current time without prompting the user."
  14649. (interactive "P")
  14650. (let* ((ts nil)
  14651. (default-time
  14652. ;; Default time is either today, or, when entering a range,
  14653. ;; the range start.
  14654. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14655. (save-excursion
  14656. (re-search-backward
  14657. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14658. (- (point) 20) t)))
  14659. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14660. (current-time)))
  14661. (default-input (and ts (org-get-compact-tod ts)))
  14662. (repeater (save-excursion
  14663. (save-match-data
  14664. (beginning-of-line)
  14665. (when (re-search-forward
  14666. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14667. (save-excursion (progn (end-of-line) (point))) t)
  14668. (match-string 0)))))
  14669. org-time-was-given org-end-time-was-given time)
  14670. (cond
  14671. ((and (org-at-timestamp-p t)
  14672. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14673. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14674. (insert "--")
  14675. (setq time (let ((this-command this-command))
  14676. (org-read-date arg 'totime nil nil
  14677. default-time default-input inactive)))
  14678. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14679. ((org-at-timestamp-p t)
  14680. (setq time (let ((this-command this-command))
  14681. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14682. (when (org-at-timestamp-p t) ; just to get the match data
  14683. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14684. (replace-match "")
  14685. (setq org-last-changed-timestamp
  14686. (org-insert-time-stamp
  14687. time (or org-time-was-given arg)
  14688. inactive nil nil (list org-end-time-was-given)))
  14689. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14690. (setq org-last-changed-timestamp
  14691. (concat (substring org-last-inserted-timestamp 0 -1)
  14692. " " repeater ">"))))
  14693. (message "Timestamp updated"))
  14694. ((equal arg '(16))
  14695. (org-insert-time-stamp (current-time) t))
  14696. (t
  14697. (setq time (let ((this-command this-command))
  14698. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14699. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14700. nil nil (list org-end-time-was-given))))))
  14701. ;; FIXME: can we use this for something else, like computing time differences?
  14702. (defun org-get-compact-tod (s)
  14703. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14704. (let* ((t1 (match-string 1 s))
  14705. (h1 (string-to-number (match-string 2 s)))
  14706. (m1 (string-to-number (match-string 3 s)))
  14707. (t2 (and (match-end 4) (match-string 5 s)))
  14708. (h2 (and t2 (string-to-number (match-string 6 s))))
  14709. (m2 (and t2 (string-to-number (match-string 7 s))))
  14710. dh dm)
  14711. (if (not t2)
  14712. t1
  14713. (setq dh (- h2 h1) dm (- m2 m1))
  14714. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14715. (concat t1 "+" (number-to-string dh)
  14716. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14717. (defun org-time-stamp-inactive (&optional arg)
  14718. "Insert an inactive time stamp.
  14719. An inactive time stamp is enclosed in square brackets instead of angle
  14720. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14721. does not link to the calendar and cannot be changed with the S-cursor keys.
  14722. So these are more for recording a certain time/date."
  14723. (interactive "P")
  14724. (org-time-stamp arg 'inactive))
  14725. (defvar org-date-ovl (make-overlay 1 1))
  14726. (overlay-put org-date-ovl 'face 'org-date-selected)
  14727. (org-detach-overlay org-date-ovl)
  14728. (defvar org-ans1) ; dynamically scoped parameter
  14729. (defvar org-ans2) ; dynamically scoped parameter
  14730. (defvar org-plain-time-of-day-regexp) ; defined below
  14731. (defvar org-overriding-default-time nil) ; dynamically scoped
  14732. (defvar org-read-date-overlay nil)
  14733. (defvar org-dcst nil) ; dynamically scoped
  14734. (defvar org-read-date-history nil)
  14735. (defvar org-read-date-final-answer nil)
  14736. (defvar org-read-date-analyze-futurep nil)
  14737. (defvar org-read-date-analyze-forced-year nil)
  14738. (defvar org-read-date-inactive)
  14739. (defvar org-read-date-minibuffer-local-map
  14740. (let ((map (make-sparse-keymap)))
  14741. (set-keymap-parent map minibuffer-local-map)
  14742. (org-defkey map (kbd ".")
  14743. (lambda () (interactive)
  14744. ;; Are we at the beginning of the prompt?
  14745. (if (looking-back "^[^:]+: ")
  14746. (org-eval-in-calendar '(calendar-goto-today))
  14747. (insert "."))))
  14748. (org-defkey map (kbd "C-.")
  14749. (lambda () (interactive)
  14750. (org-eval-in-calendar '(calendar-goto-today))))
  14751. (org-defkey map [(meta shift left)]
  14752. (lambda () (interactive)
  14753. (org-eval-in-calendar '(calendar-backward-month 1))))
  14754. (org-defkey map [(meta shift right)]
  14755. (lambda () (interactive)
  14756. (org-eval-in-calendar '(calendar-forward-month 1))))
  14757. (org-defkey map [(meta shift up)]
  14758. (lambda () (interactive)
  14759. (org-eval-in-calendar '(calendar-backward-year 1))))
  14760. (org-defkey map [(meta shift down)]
  14761. (lambda () (interactive)
  14762. (org-eval-in-calendar '(calendar-forward-year 1))))
  14763. (org-defkey map [?\e (shift left)]
  14764. (lambda () (interactive)
  14765. (org-eval-in-calendar '(calendar-backward-month 1))))
  14766. (org-defkey map [?\e (shift right)]
  14767. (lambda () (interactive)
  14768. (org-eval-in-calendar '(calendar-forward-month 1))))
  14769. (org-defkey map [?\e (shift up)]
  14770. (lambda () (interactive)
  14771. (org-eval-in-calendar '(calendar-backward-year 1))))
  14772. (org-defkey map [?\e (shift down)]
  14773. (lambda () (interactive)
  14774. (org-eval-in-calendar '(calendar-forward-year 1))))
  14775. (org-defkey map [(shift up)]
  14776. (lambda () (interactive)
  14777. (org-eval-in-calendar '(calendar-backward-week 1))))
  14778. (org-defkey map [(shift down)]
  14779. (lambda () (interactive)
  14780. (org-eval-in-calendar '(calendar-forward-week 1))))
  14781. (org-defkey map [(shift left)]
  14782. (lambda () (interactive)
  14783. (org-eval-in-calendar '(calendar-backward-day 1))))
  14784. (org-defkey map [(shift right)]
  14785. (lambda () (interactive)
  14786. (org-eval-in-calendar '(calendar-forward-day 1))))
  14787. (org-defkey map "!"
  14788. (lambda () (interactive)
  14789. (org-eval-in-calendar '(diary-view-entries))
  14790. (message "")))
  14791. (org-defkey map ">"
  14792. (lambda () (interactive)
  14793. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14794. (org-defkey map "<"
  14795. (lambda () (interactive)
  14796. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14797. (org-defkey map "\C-v"
  14798. (lambda () (interactive)
  14799. (org-eval-in-calendar
  14800. '(calendar-scroll-left-three-months 1))))
  14801. (org-defkey map "\M-v"
  14802. (lambda () (interactive)
  14803. (org-eval-in-calendar
  14804. '(calendar-scroll-right-three-months 1))))
  14805. map)
  14806. "Keymap for minibuffer commands when using `org-read-date'.")
  14807. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14808. default-time default-input inactive)
  14809. "Read a date, possibly a time, and make things smooth for the user.
  14810. The prompt will suggest to enter an ISO date, but you can also enter anything
  14811. which will at least partially be understood by `parse-time-string'.
  14812. Unrecognized parts of the date will default to the current day, month, year,
  14813. hour and minute. If this command is called to replace a timestamp at point,
  14814. or to enter the second timestamp of a range, the default time is taken
  14815. from the existing stamp. Furthermore, the command prefers the future,
  14816. so if you are giving a date where the year is not given, and the day-month
  14817. combination is already past in the current year, it will assume you
  14818. mean next year. For details, see the manual. A few examples:
  14819. 3-2-5 --> 2003-02-05
  14820. feb 15 --> currentyear-02-15
  14821. 2/15 --> currentyear-02-15
  14822. sep 12 9 --> 2009-09-12
  14823. 12:45 --> today 12:45
  14824. 22 sept 0:34 --> currentyear-09-22 0:34
  14825. 12 --> currentyear-currentmonth-12
  14826. Fri --> nearest Friday after today
  14827. -Tue --> last Tuesday
  14828. etc.
  14829. Furthermore you can specify a relative date by giving, as the *first* thing
  14830. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14831. change in days weeks, months, years.
  14832. With a single plus or minus, the date is relative to today. With a double
  14833. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14834. +4d --> four days from today
  14835. +4 --> same as above
  14836. +2w --> two weeks from today
  14837. ++5 --> five days from default date
  14838. The function understands only English month and weekday abbreviations.
  14839. While prompting, a calendar is popped up - you can also select the
  14840. date with the mouse (button 1). The calendar shows a period of three
  14841. months. To scroll it to other months, use the keys `>' and `<'.
  14842. If you don't like the calendar, turn it off with
  14843. \(setq org-read-date-popup-calendar nil)
  14844. With optional argument TO-TIME, the date will immediately be converted
  14845. to an internal time.
  14846. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14847. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14848. still enter a time, and this function will inform the calling routine
  14849. about this change. The calling routine may then choose to change the
  14850. format used to insert the time stamp into the buffer to include the time.
  14851. With optional argument FROM-STRING, read from this string instead from
  14852. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14853. the time/date that is used for everything that is not specified by the
  14854. user."
  14855. (require 'parse-time)
  14856. (let* ((org-time-stamp-rounding-minutes
  14857. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14858. (org-dcst org-display-custom-times)
  14859. (ct (org-current-time))
  14860. (org-def (or org-overriding-default-time default-time ct))
  14861. (org-defdecode (decode-time org-def))
  14862. (dummy (progn
  14863. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14864. (setcar (nthcdr 2 org-defdecode) -1)
  14865. (setcar (nthcdr 1 org-defdecode) 59)
  14866. (setq org-def (apply 'encode-time org-defdecode)
  14867. org-defdecode (decode-time org-def)))))
  14868. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14869. (calendar-frame-setup nil)
  14870. (calendar-setup nil)
  14871. (calendar-move-hook nil)
  14872. (calendar-view-diary-initially-flag nil)
  14873. (calendar-view-holidays-initially-flag nil)
  14874. (timestr (format-time-string
  14875. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14876. (prompt (concat (if prompt (concat prompt " ") "")
  14877. (format "Date+time [%s]: " timestr)))
  14878. ans (org-ans0 "") org-ans1 org-ans2 final)
  14879. (cond
  14880. (from-string (setq ans from-string))
  14881. (org-read-date-popup-calendar
  14882. (save-excursion
  14883. (save-window-excursion
  14884. (calendar)
  14885. (org-eval-in-calendar '(setq cursor-type nil) t)
  14886. (unwind-protect
  14887. (progn
  14888. (calendar-forward-day (- (time-to-days org-def)
  14889. (calendar-absolute-from-gregorian
  14890. (calendar-current-date))))
  14891. (org-eval-in-calendar nil t)
  14892. (let* ((old-map (current-local-map))
  14893. (map (copy-keymap calendar-mode-map))
  14894. (minibuffer-local-map
  14895. (copy-keymap org-read-date-minibuffer-local-map)))
  14896. (org-defkey map (kbd "RET") 'org-calendar-select)
  14897. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14898. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14899. (unwind-protect
  14900. (progn
  14901. (use-local-map map)
  14902. (setq org-read-date-inactive inactive)
  14903. (add-hook 'post-command-hook 'org-read-date-display)
  14904. (setq org-ans0 (read-string prompt default-input
  14905. 'org-read-date-history nil))
  14906. ;; org-ans0: from prompt
  14907. ;; org-ans1: from mouse click
  14908. ;; org-ans2: from calendar motion
  14909. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14910. (remove-hook 'post-command-hook 'org-read-date-display)
  14911. (use-local-map old-map)
  14912. (when org-read-date-overlay
  14913. (delete-overlay org-read-date-overlay)
  14914. (setq org-read-date-overlay nil)))))
  14915. (bury-buffer "*Calendar*")))))
  14916. (t ; Naked prompt only
  14917. (unwind-protect
  14918. (setq ans (read-string prompt default-input
  14919. 'org-read-date-history timestr))
  14920. (when org-read-date-overlay
  14921. (delete-overlay org-read-date-overlay)
  14922. (setq org-read-date-overlay nil)))))
  14923. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14924. (when org-read-date-analyze-forced-year
  14925. (message "Year was forced into %s"
  14926. (if org-read-date-force-compatible-dates
  14927. "compatible range (1970-2037)"
  14928. "range representable on this machine"))
  14929. (ding))
  14930. ;; One round trip to get rid of 34th of August and stuff like that....
  14931. (setq final (decode-time (apply 'encode-time final)))
  14932. (setq org-read-date-final-answer ans)
  14933. (if to-time
  14934. (apply 'encode-time final)
  14935. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14936. (format "%04d-%02d-%02d %02d:%02d"
  14937. (nth 5 final) (nth 4 final) (nth 3 final)
  14938. (nth 2 final) (nth 1 final))
  14939. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14940. (defvar org-def)
  14941. (defvar org-defdecode)
  14942. (defvar org-with-time)
  14943. (defun org-read-date-display ()
  14944. "Display the current date prompt interpretation in the minibuffer."
  14945. (when org-read-date-display-live
  14946. (when org-read-date-overlay
  14947. (delete-overlay org-read-date-overlay))
  14948. (when (minibufferp (current-buffer))
  14949. (save-excursion
  14950. (end-of-line 1)
  14951. (while (not (equal (buffer-substring
  14952. (max (point-min) (- (point) 4)) (point))
  14953. " "))
  14954. (insert " ")))
  14955. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14956. " " (or org-ans1 org-ans2)))
  14957. (org-end-time-was-given nil)
  14958. (f (org-read-date-analyze ans org-def org-defdecode))
  14959. (fmts (if org-dcst
  14960. org-time-stamp-custom-formats
  14961. org-time-stamp-formats))
  14962. (fmt (if (or org-with-time
  14963. (and (boundp 'org-time-was-given) org-time-was-given))
  14964. (cdr fmts)
  14965. (car fmts)))
  14966. (txt (format-time-string fmt (apply 'encode-time f)))
  14967. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14968. (txt (concat "=> " txt)))
  14969. (when (and org-end-time-was-given
  14970. (string-match org-plain-time-of-day-regexp txt))
  14971. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14972. org-end-time-was-given
  14973. (substring txt (match-end 0)))))
  14974. (when org-read-date-analyze-futurep
  14975. (setq txt (concat txt " (=>F)")))
  14976. (setq org-read-date-overlay
  14977. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14978. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14979. (defun org-read-date-analyze (ans org-def org-defdecode)
  14980. "Analyze the combined answer of the date prompt."
  14981. ;; FIXME: cleanup and comment
  14982. (let ((nowdecode (decode-time (current-time)))
  14983. delta deltan deltaw deltadef year month day
  14984. hour minute second wday pm h2 m2 tl wday1
  14985. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14986. (setq org-read-date-analyze-futurep nil
  14987. org-read-date-analyze-forced-year nil)
  14988. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14989. (setq ans "+0"))
  14990. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14991. (setq ans (replace-match "" t t ans)
  14992. deltan (car delta)
  14993. deltaw (nth 1 delta)
  14994. deltadef (nth 2 delta)))
  14995. ;; Check if there is an iso week date in there. If yes, store the
  14996. ;; info and postpone interpreting it until the rest of the parsing
  14997. ;; is done.
  14998. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14999. (setq iso-year (if (match-end 1)
  15000. (org-small-year-to-year
  15001. (string-to-number (match-string 1 ans))))
  15002. iso-weekday (if (match-end 3)
  15003. (string-to-number (match-string 3 ans)))
  15004. iso-week (string-to-number (match-string 2 ans)))
  15005. (setq ans (replace-match "" t t ans)))
  15006. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15007. (when (string-match
  15008. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15009. (setq year (if (match-end 2)
  15010. (string-to-number (match-string 2 ans))
  15011. (progn (setq kill-year t)
  15012. (string-to-number (format-time-string "%Y"))))
  15013. month (string-to-number (match-string 3 ans))
  15014. day (string-to-number (match-string 4 ans)))
  15015. (if (< year 100) (setq year (+ 2000 year)))
  15016. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15017. t nil ans)))
  15018. ;; Help matching dotted european dates
  15019. (when (string-match
  15020. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15021. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15022. (setq kill-year t)
  15023. (string-to-number (format-time-string "%Y")))
  15024. day (string-to-number (match-string 1 ans))
  15025. month (string-to-number (match-string 2 ans))
  15026. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15027. t nil ans)))
  15028. ;; Help matching american dates, like 5/30 or 5/30/7
  15029. (when (string-match
  15030. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15031. (setq year (if (match-end 4)
  15032. (string-to-number (match-string 4 ans))
  15033. (progn (setq kill-year t)
  15034. (string-to-number (format-time-string "%Y"))))
  15035. month (string-to-number (match-string 1 ans))
  15036. day (string-to-number (match-string 2 ans)))
  15037. (if (< year 100) (setq year (+ 2000 year)))
  15038. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15039. t nil ans)))
  15040. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15041. ;; If there is a time with am/pm, and *no* time without it, we convert
  15042. ;; so that matching will be successful.
  15043. (loop for i from 1 to 2 do ; twice, for end time as well
  15044. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15045. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15046. (setq hour (string-to-number (match-string 1 ans))
  15047. minute (if (match-end 3)
  15048. (string-to-number (match-string 3 ans))
  15049. 0)
  15050. pm (equal ?p
  15051. (string-to-char (downcase (match-string 4 ans)))))
  15052. (if (and (= hour 12) (not pm))
  15053. (setq hour 0)
  15054. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15055. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15056. t t ans))))
  15057. ;; Check if a time range is given as a duration
  15058. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15059. (setq hour (string-to-number (match-string 1 ans))
  15060. h2 (+ hour (string-to-number (match-string 3 ans)))
  15061. minute (string-to-number (match-string 2 ans))
  15062. m2 (+ minute (if (match-end 5) (string-to-number
  15063. (match-string 5 ans))0)))
  15064. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15065. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15066. t t ans)))
  15067. ;; Check if there is a time range
  15068. (when (boundp 'org-end-time-was-given)
  15069. (setq org-time-was-given nil)
  15070. (when (and (string-match org-plain-time-of-day-regexp ans)
  15071. (match-end 8))
  15072. (setq org-end-time-was-given (match-string 8 ans))
  15073. (setq ans (concat (substring ans 0 (match-beginning 7))
  15074. (substring ans (match-end 7))))))
  15075. (setq tl (parse-time-string ans)
  15076. day (or (nth 3 tl) (nth 3 org-defdecode))
  15077. month (or (nth 4 tl)
  15078. (if (and org-read-date-prefer-future
  15079. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15080. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15081. (nth 4 org-defdecode)))
  15082. year (or (and (not kill-year) (nth 5 tl))
  15083. (if (and org-read-date-prefer-future
  15084. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15085. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15086. (nth 5 org-defdecode)))
  15087. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15088. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15089. second (or (nth 0 tl) 0)
  15090. wday (nth 6 tl))
  15091. (when (and (eq org-read-date-prefer-future 'time)
  15092. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15093. (equal day (nth 3 nowdecode))
  15094. (equal month (nth 4 nowdecode))
  15095. (equal year (nth 5 nowdecode))
  15096. (nth 2 tl)
  15097. (or (< (nth 2 tl) (nth 2 nowdecode))
  15098. (and (= (nth 2 tl) (nth 2 nowdecode))
  15099. (nth 1 tl)
  15100. (< (nth 1 tl) (nth 1 nowdecode)))))
  15101. (setq day (1+ day)
  15102. futurep t))
  15103. ;; Special date definitions below
  15104. (cond
  15105. (iso-week
  15106. ;; There was an iso week
  15107. (require 'cal-iso)
  15108. (setq futurep nil)
  15109. (setq year (or iso-year year)
  15110. day (or iso-weekday wday 1)
  15111. wday nil ; to make sure that the trigger below does not match
  15112. iso-date (calendar-gregorian-from-absolute
  15113. (calendar-absolute-from-iso
  15114. (list iso-week day year))))
  15115. ; FIXME: Should we also push ISO weeks into the future?
  15116. ; (when (and org-read-date-prefer-future
  15117. ; (not iso-year)
  15118. ; (< (calendar-absolute-from-gregorian iso-date)
  15119. ; (time-to-days (current-time))))
  15120. ; (setq year (1+ year)
  15121. ; iso-date (calendar-gregorian-from-absolute
  15122. ; (calendar-absolute-from-iso
  15123. ; (list iso-week day year)))))
  15124. (setq month (car iso-date)
  15125. year (nth 2 iso-date)
  15126. day (nth 1 iso-date)))
  15127. (deltan
  15128. (setq futurep nil)
  15129. (unless deltadef
  15130. (let ((now (decode-time (current-time))))
  15131. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15132. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15133. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15134. ((equal deltaw "m") (setq month (+ month deltan)))
  15135. ((equal deltaw "y") (setq year (+ year deltan)))))
  15136. ((and wday (not (nth 3 tl)))
  15137. ;; Weekday was given, but no day, so pick that day in the week
  15138. ;; on or after the derived date.
  15139. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15140. (unless (equal wday wday1)
  15141. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15142. (if (and (boundp 'org-time-was-given)
  15143. (nth 2 tl))
  15144. (setq org-time-was-given t))
  15145. (if (< year 100) (setq year (+ 2000 year)))
  15146. ;; Check of the date is representable
  15147. (if org-read-date-force-compatible-dates
  15148. (progn
  15149. (if (< year 1970)
  15150. (setq year 1970 org-read-date-analyze-forced-year t))
  15151. (if (> year 2037)
  15152. (setq year 2037 org-read-date-analyze-forced-year t)))
  15153. (condition-case nil
  15154. (ignore (encode-time second minute hour day month year))
  15155. (error
  15156. (setq year (nth 5 org-defdecode))
  15157. (setq org-read-date-analyze-forced-year t))))
  15158. (setq org-read-date-analyze-futurep futurep)
  15159. (list second minute hour day month year)))
  15160. (defvar parse-time-weekdays)
  15161. (defun org-read-date-get-relative (s today default)
  15162. "Check string S for special relative date string.
  15163. TODAY and DEFAULT are internal times, for today and for a default.
  15164. Return shift list (N what def-flag)
  15165. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15166. N is the number of WHATs to shift.
  15167. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15168. the DEFAULT date rather than TODAY."
  15169. (require 'parse-time)
  15170. (when (and
  15171. (string-match
  15172. (concat
  15173. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15174. "\\([0-9]+\\)?"
  15175. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15176. "\\([ \t]\\|$\\)") s)
  15177. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15178. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15179. (string-to-char (substring (match-string 1 s) -1))
  15180. ?+))
  15181. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15182. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15183. (what (if (match-end 3) (match-string 3 s) "d"))
  15184. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15185. (date (if rel default today))
  15186. (wday (nth 6 (decode-time date)))
  15187. delta)
  15188. (if wday1
  15189. (progn
  15190. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15191. (if (= delta 0) (setq delta 7))
  15192. (if (= dir ?-)
  15193. (progn
  15194. (setq delta (- delta 7))
  15195. (if (= delta 0) (setq delta -7))))
  15196. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15197. (list delta "d" rel))
  15198. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15199. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15200. "Turn a user-specified date into the internal representation.
  15201. The internal representation needed by the calendar is (month day year).
  15202. This is a wrapper to handle the brain-dead convention in calendar that
  15203. user function argument order change dependent on argument order."
  15204. (if (boundp 'calendar-date-style)
  15205. (cond
  15206. ((eq calendar-date-style 'american)
  15207. (list arg1 arg2 arg3))
  15208. ((eq calendar-date-style 'european)
  15209. (list arg2 arg1 arg3))
  15210. ((eq calendar-date-style 'iso)
  15211. (list arg2 arg3 arg1)))
  15212. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15213. (if (org-bound-and-true-p european-calendar-style)
  15214. (list arg2 arg1 arg3)
  15215. (list arg1 arg2 arg3)))))
  15216. (defun org-eval-in-calendar (form &optional keepdate)
  15217. "Eval FORM in the calendar window and return to current window.
  15218. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15219. otherwise stick to the current value of `org-ans2'."
  15220. (let ((sf (selected-frame))
  15221. (sw (selected-window)))
  15222. (select-window (get-buffer-window "*Calendar*" t))
  15223. (eval form)
  15224. (when (and (not keepdate) (calendar-cursor-to-date))
  15225. (let* ((date (calendar-cursor-to-date))
  15226. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15227. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15228. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15229. (select-window sw)
  15230. (org-select-frame-set-input-focus sf)))
  15231. (defun org-calendar-select ()
  15232. "Return to `org-read-date' with the date currently selected.
  15233. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15234. (interactive)
  15235. (when (calendar-cursor-to-date)
  15236. (let* ((date (calendar-cursor-to-date))
  15237. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15238. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15239. (if (active-minibuffer-window) (exit-minibuffer))))
  15240. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15241. "Insert a date stamp for the date given by the internal TIME.
  15242. WITH-HM means use the stamp format that includes the time of the day.
  15243. INACTIVE means use square brackets instead of angular ones, so that the
  15244. stamp will not contribute to the agenda.
  15245. PRE and POST are optional strings to be inserted before and after the
  15246. stamp.
  15247. The command returns the inserted time stamp."
  15248. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15249. stamp)
  15250. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15251. (insert-before-markers (or pre ""))
  15252. (when (listp extra)
  15253. (setq extra (car extra))
  15254. (if (and (stringp extra)
  15255. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15256. (setq extra (format "-%02d:%02d"
  15257. (string-to-number (match-string 1 extra))
  15258. (string-to-number (match-string 2 extra))))
  15259. (setq extra nil)))
  15260. (when extra
  15261. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15262. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15263. (insert-before-markers (or post ""))
  15264. (setq org-last-inserted-timestamp stamp)))
  15265. (defun org-toggle-time-stamp-overlays ()
  15266. "Toggle the use of custom time stamp formats."
  15267. (interactive)
  15268. (setq org-display-custom-times (not org-display-custom-times))
  15269. (unless org-display-custom-times
  15270. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15271. (while (setq p (next-single-property-change p 'display))
  15272. (if (and (get-text-property p 'display)
  15273. (eq (get-text-property p 'face) 'org-date))
  15274. (remove-text-properties
  15275. p (setq p (next-single-property-change p 'display))
  15276. '(display t))))
  15277. (set-buffer-modified-p bmp)))
  15278. (if (featurep 'xemacs)
  15279. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15280. (org-restart-font-lock)
  15281. (setq org-table-may-need-update t)
  15282. (if org-display-custom-times
  15283. (message "Time stamps are overlaid with custom format")
  15284. (message "Time stamp overlays removed")))
  15285. (defun org-display-custom-time (beg end)
  15286. "Overlay modified time stamp format over timestamp between BEG and END."
  15287. (let* ((ts (buffer-substring beg end))
  15288. t1 w1 with-hm tf time str w2 (off 0))
  15289. (save-match-data
  15290. (setq t1 (org-parse-time-string ts t))
  15291. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15292. (setq off (- (match-end 0) (match-beginning 0)))))
  15293. (setq end (- end off))
  15294. (setq w1 (- end beg)
  15295. with-hm (and (nth 1 t1) (nth 2 t1))
  15296. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15297. time (org-fix-decoded-time t1)
  15298. str (org-add-props
  15299. (format-time-string
  15300. (substring tf 1 -1) (apply 'encode-time time))
  15301. nil 'mouse-face 'highlight)
  15302. w2 (length str))
  15303. (if (not (= w2 w1))
  15304. (add-text-properties (1+ beg) (+ 2 beg)
  15305. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15306. (if (featurep 'xemacs)
  15307. (progn
  15308. (put-text-property beg end 'invisible t)
  15309. (put-text-property beg end 'end-glyph (make-glyph str)))
  15310. (put-text-property beg end 'display str))))
  15311. (defun org-translate-time (string)
  15312. "Translate all timestamps in STRING to custom format.
  15313. But do this only if the variable `org-display-custom-times' is set."
  15314. (when org-display-custom-times
  15315. (save-match-data
  15316. (let* ((start 0)
  15317. (re org-ts-regexp-both)
  15318. t1 with-hm inactive tf time str beg end)
  15319. (while (setq start (string-match re string start))
  15320. (setq beg (match-beginning 0)
  15321. end (match-end 0)
  15322. t1 (save-match-data
  15323. (org-parse-time-string (substring string beg end) t))
  15324. with-hm (and (nth 1 t1) (nth 2 t1))
  15325. inactive (equal (substring string beg (1+ beg)) "[")
  15326. tf (funcall (if with-hm 'cdr 'car)
  15327. org-time-stamp-custom-formats)
  15328. time (org-fix-decoded-time t1)
  15329. str (format-time-string
  15330. (concat
  15331. (if inactive "[" "<") (substring tf 1 -1)
  15332. (if inactive "]" ">"))
  15333. (apply 'encode-time time))
  15334. string (replace-match str t t string)
  15335. start (+ start (length str)))))))
  15336. string)
  15337. (defun org-fix-decoded-time (time)
  15338. "Set 0 instead of nil for the first 6 elements of time.
  15339. Don't touch the rest."
  15340. (let ((n 0))
  15341. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15342. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15343. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15344. "Difference between TIMESTAMP-STRING and now in days.
  15345. If SECONDS is non-nil, return the difference in seconds."
  15346. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15347. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15348. (funcall fdiff (current-time)))))
  15349. (defun org-deadline-close (timestamp-string &optional ndays)
  15350. "Is the time in TIMESTAMP-STRING close to the current date?"
  15351. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15352. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15353. (not (org-entry-is-done-p))))
  15354. (defun org-get-wdays (ts &optional delay zero-delay)
  15355. "Get the deadline lead time appropriate for timestring TS.
  15356. When DELAY is non-nil, get the delay time for scheduled items
  15357. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15358. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15359. don't try to find the delay cookie in the scheduled timestamp."
  15360. (let ((tv (if delay org-scheduled-delay-days
  15361. org-deadline-warning-days)))
  15362. (cond
  15363. ((or (and delay (< tv 0))
  15364. (and delay zero-delay (<= tv 0))
  15365. (and (not delay) (<= tv 0)))
  15366. ;; Enforce this value no matter what
  15367. (- tv))
  15368. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15369. ;; lead time is specified.
  15370. (floor (* (string-to-number (match-string 1 ts))
  15371. (cdr (assoc (match-string 2 ts)
  15372. '(("d" . 1) ("w" . 7)
  15373. ("m" . 30.4) ("y" . 365.25)
  15374. ("h" . 0.041667)))))))
  15375. ;; go for the default.
  15376. (t tv))))
  15377. (defun org-calendar-select-mouse (ev)
  15378. "Return to `org-read-date' with the date currently selected.
  15379. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15380. (interactive "e")
  15381. (mouse-set-point ev)
  15382. (when (calendar-cursor-to-date)
  15383. (let* ((date (calendar-cursor-to-date))
  15384. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15385. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15386. (if (active-minibuffer-window) (exit-minibuffer))))
  15387. (defun org-check-deadlines (ndays)
  15388. "Check if there are any deadlines due or past due.
  15389. A deadline is considered due if it happens within `org-deadline-warning-days'
  15390. days from today's date. If the deadline appears in an entry marked DONE,
  15391. it is not shown. The prefix arg NDAYS can be used to test that many
  15392. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15393. (interactive "P")
  15394. (let* ((org-warn-days
  15395. (cond
  15396. ((equal ndays '(4)) 100000)
  15397. (ndays (prefix-numeric-value ndays))
  15398. (t (abs org-deadline-warning-days))))
  15399. (case-fold-search nil)
  15400. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15401. (callback
  15402. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15403. (message "%d deadlines past-due or due within %d days"
  15404. (org-occur regexp nil callback)
  15405. org-warn-days)))
  15406. (defsubst org-re-timestamp (type)
  15407. "Return a regexp for timestamp TYPE.
  15408. Allowed values for TYPE are:
  15409. all: all timestamps
  15410. active: only active timestamps (<...>)
  15411. inactive: only inactive timestamps ([...])
  15412. scheduled: only scheduled timestamps
  15413. deadline: only deadline timestamps
  15414. closed: only closed time-stamps
  15415. When TYPE is nil, fall back on returning a regexp that matches
  15416. both scheduled and deadline timestamps."
  15417. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15418. ((eq type 'active) org-ts-regexp)
  15419. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15420. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15421. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15422. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15423. ((eq type 'scheduled-or-deadline)
  15424. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15425. (defun org-check-before-date (date)
  15426. "Check if there are deadlines or scheduled entries before DATE."
  15427. (interactive (list (org-read-date)))
  15428. (let ((case-fold-search nil)
  15429. (regexp (org-re-timestamp org-ts-type))
  15430. (callback
  15431. (lambda () (time-less-p
  15432. (org-time-string-to-time (match-string 1))
  15433. (org-time-string-to-time date)))))
  15434. (message "%d entries before %s"
  15435. (org-occur regexp nil callback) date)))
  15436. (defun org-check-after-date (date)
  15437. "Check if there are deadlines or scheduled entries after DATE."
  15438. (interactive (list (org-read-date)))
  15439. (let ((case-fold-search nil)
  15440. (regexp (org-re-timestamp org-ts-type))
  15441. (callback
  15442. (lambda () (not
  15443. (time-less-p
  15444. (org-time-string-to-time (match-string 1))
  15445. (org-time-string-to-time date))))))
  15446. (message "%d entries after %s"
  15447. (org-occur regexp nil callback) date)))
  15448. (defun org-check-dates-range (start-date end-date)
  15449. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15450. (interactive (list (org-read-date nil nil nil "Range starts")
  15451. (org-read-date nil nil nil "Range end")))
  15452. (let ((case-fold-search nil)
  15453. (regexp (org-re-timestamp org-ts-type))
  15454. (callback
  15455. (lambda ()
  15456. (let ((match (match-string 1)))
  15457. (and
  15458. (not (time-less-p
  15459. (org-time-string-to-time match)
  15460. (org-time-string-to-time start-date)))
  15461. (time-less-p
  15462. (org-time-string-to-time match)
  15463. (org-time-string-to-time end-date)))))))
  15464. (message "%d entries between %s and %s"
  15465. (org-occur regexp nil callback) start-date end-date)))
  15466. (defun org-evaluate-time-range (&optional to-buffer)
  15467. "Evaluate a time range by computing the difference between start and end.
  15468. Normally the result is just printed in the echo area, but with prefix arg
  15469. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15470. If the time range is actually in a table, the result is inserted into the
  15471. next column.
  15472. For time difference computation, a year is assumed to be exactly 365
  15473. days in order to avoid rounding problems."
  15474. (interactive "P")
  15475. (or
  15476. (org-clock-update-time-maybe)
  15477. (save-excursion
  15478. (unless (org-at-date-range-p t)
  15479. (goto-char (point-at-bol))
  15480. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15481. (if (not (org-at-date-range-p t))
  15482. (user-error "Not at a time-stamp range, and none found in current line")))
  15483. (let* ((ts1 (match-string 1))
  15484. (ts2 (match-string 2))
  15485. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15486. (match-end (match-end 0))
  15487. (time1 (org-time-string-to-time ts1))
  15488. (time2 (org-time-string-to-time ts2))
  15489. (t1 (org-float-time time1))
  15490. (t2 (org-float-time time2))
  15491. (diff (abs (- t2 t1)))
  15492. (negative (< (- t2 t1) 0))
  15493. ;; (ys (floor (* 365 24 60 60)))
  15494. (ds (* 24 60 60))
  15495. (hs (* 60 60))
  15496. (fy "%dy %dd %02d:%02d")
  15497. (fy1 "%dy %dd")
  15498. (fd "%dd %02d:%02d")
  15499. (fd1 "%dd")
  15500. (fh "%02d:%02d")
  15501. y d h m align)
  15502. (if havetime
  15503. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15504. y 0
  15505. d (floor (/ diff ds)) diff (mod diff ds)
  15506. h (floor (/ diff hs)) diff (mod diff hs)
  15507. m (floor (/ diff 60)))
  15508. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15509. y 0
  15510. d (floor (+ (/ diff ds) 0.5))
  15511. h 0 m 0))
  15512. (if (not to-buffer)
  15513. (message "%s" (org-make-tdiff-string y d h m))
  15514. (if (org-at-table-p)
  15515. (progn
  15516. (goto-char match-end)
  15517. (setq align t)
  15518. (and (looking-at " *|") (goto-char (match-end 0))))
  15519. (goto-char match-end))
  15520. (if (looking-at
  15521. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15522. (replace-match ""))
  15523. (if negative (insert " -"))
  15524. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15525. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15526. (insert " " (format fh h m))))
  15527. (if align (org-table-align))
  15528. (message "Time difference inserted")))))
  15529. (defun org-make-tdiff-string (y d h m)
  15530. (let ((fmt "")
  15531. (l nil))
  15532. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15533. l (push y l)))
  15534. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15535. l (push d l)))
  15536. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15537. l (push h l)))
  15538. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15539. l (push m l)))
  15540. (apply 'format fmt (nreverse l))))
  15541. (defun org-time-string-to-time (s &optional buffer pos)
  15542. "Convert a timestamp string into internal time."
  15543. (condition-case errdata
  15544. (apply 'encode-time (org-parse-time-string s))
  15545. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15546. s (if (not (and buffer pos))
  15547. ""
  15548. (format " at %d in buffer `%s'" pos buffer))
  15549. (cdr errdata)))))
  15550. (defun org-time-string-to-seconds (s)
  15551. "Convert a timestamp string to a number of seconds."
  15552. (org-float-time (org-time-string-to-time s)))
  15553. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15554. "Convert a time stamp to an absolute day number.
  15555. If there is a specifier for a cyclic time stamp, get the closest
  15556. date to DAYNR.
  15557. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15558. The variable `date' is bound by the calendar when this is called."
  15559. (cond
  15560. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15561. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15562. daynr
  15563. (+ daynr 1000)))
  15564. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15565. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15566. (time-to-days (current-time))) (match-string 0 s)
  15567. prefer show-all))
  15568. (t (time-to-days
  15569. (condition-case errdata
  15570. (apply 'encode-time (org-parse-time-string s))
  15571. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15572. s (if (not (and buffer pos))
  15573. ""
  15574. (format " at %d in buffer `%s'" pos buffer))
  15575. (cdr errdata))))))))
  15576. (defun org-days-to-iso-week (days)
  15577. "Return the iso week number."
  15578. (require 'cal-iso)
  15579. (car (calendar-iso-from-absolute days)))
  15580. (defun org-small-year-to-year (year)
  15581. "Convert 2-digit years into 4-digit years.
  15582. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15583. The year 2000 cannot be abbreviated. Any year larger than 99
  15584. is returned unchanged."
  15585. (if (< year 38)
  15586. (setq year (+ 2000 year))
  15587. (if (< year 100)
  15588. (setq year (+ 1900 year))))
  15589. year)
  15590. (defun org-time-from-absolute (d)
  15591. "Return the time corresponding to date D.
  15592. D may be an absolute day number, or a calendar-type list (month day year)."
  15593. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15594. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15595. (defun org-calendar-holiday ()
  15596. "List of holidays, for Diary display in Org-mode."
  15597. (require 'holidays)
  15598. (let ((hl (funcall
  15599. (if (fboundp 'calendar-check-holidays)
  15600. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15601. (if hl (mapconcat 'identity hl "; "))))
  15602. (defun org-diary-sexp-entry (sexp entry date)
  15603. "Process a SEXP diary ENTRY for DATE."
  15604. (require 'diary-lib)
  15605. (let ((result (if calendar-debug-sexp
  15606. (let ((stack-trace-on-error t))
  15607. (eval (car (read-from-string sexp))))
  15608. (condition-case nil
  15609. (eval (car (read-from-string sexp)))
  15610. (error
  15611. (beep)
  15612. (message "Bad sexp at line %d in %s: %s"
  15613. (org-current-line)
  15614. (buffer-file-name) sexp)
  15615. (sleep-for 2))))))
  15616. (cond ((stringp result) (split-string result "; "))
  15617. ((and (consp result)
  15618. (not (consp (cdr result)))
  15619. (stringp (cdr result))) (cdr result))
  15620. ((and (consp result)
  15621. (stringp (car result))) result)
  15622. (result entry))))
  15623. (defun org-diary-to-ical-string (frombuf)
  15624. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15625. This uses the icalendar.el library."
  15626. (let* ((tmpdir (if (featurep 'xemacs)
  15627. (temp-directory)
  15628. temporary-file-directory))
  15629. (tmpfile (make-temp-name
  15630. (expand-file-name "orgics" tmpdir)))
  15631. buf rtn b e)
  15632. (with-current-buffer frombuf
  15633. (icalendar-export-region (point-min) (point-max) tmpfile)
  15634. (setq buf (find-buffer-visiting tmpfile))
  15635. (set-buffer buf)
  15636. (goto-char (point-min))
  15637. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15638. (setq b (match-beginning 0)))
  15639. (goto-char (point-max))
  15640. (if (re-search-backward "^END:VEVENT" nil t)
  15641. (setq e (match-end 0)))
  15642. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15643. (kill-buffer buf)
  15644. (delete-file tmpfile)
  15645. rtn))
  15646. (defun org-closest-date (start current change prefer show-all)
  15647. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15648. When PREFER is `past', return a date that is either CURRENT or past.
  15649. When PREFER is `future', return a date that is either CURRENT or future.
  15650. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15651. ;; Make the proper lists from the dates
  15652. (catch 'exit
  15653. (let ((a1 '(("h" . hour)
  15654. ("d" . day)
  15655. ("w" . week)
  15656. ("m" . month)
  15657. ("y" . year)))
  15658. (shour (nth 2 (org-parse-time-string start)))
  15659. dn dw sday cday n1 n2 n0
  15660. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15661. (setq start (org-date-to-gregorian start)
  15662. current (org-date-to-gregorian
  15663. (if show-all
  15664. current
  15665. (time-to-days (current-time))))
  15666. sday (calendar-absolute-from-gregorian start)
  15667. cday (calendar-absolute-from-gregorian current))
  15668. (if (<= cday sday) (throw 'exit sday))
  15669. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15670. (setq dn (string-to-number (match-string 1 change))
  15671. dw (cdr (assoc (match-string 2 change) a1)))
  15672. (user-error "Invalid change specifier: %s" change))
  15673. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15674. (cond
  15675. ((eq dw 'hour)
  15676. (let ((missing-hours
  15677. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15678. dn)))
  15679. (setq n1 (if (zerop missing-hours) cday
  15680. (- cday (1+ (floor (/ missing-hours 24)))))
  15681. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15682. ((eq dw 'day)
  15683. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15684. n2 (+ n1 dn)))
  15685. ((eq dw 'year)
  15686. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15687. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15688. (setq date1 (list m d y1)
  15689. n1 (calendar-absolute-from-gregorian date1)
  15690. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15691. n2 (calendar-absolute-from-gregorian date2)))
  15692. ((eq dw 'month)
  15693. ;; approx number of month between the two dates
  15694. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15695. ;; How often does dn fit in there?
  15696. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15697. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15698. m (+ m nm)
  15699. ny (floor (/ m 12))
  15700. y (+ y ny)
  15701. m (- m (* ny 12)))
  15702. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15703. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15704. (setq m2 (+ m dn) y2 y)
  15705. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15706. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15707. (while (<= n2 cday)
  15708. (setq n1 n2 m m2 y y2)
  15709. (setq m2 (+ m dn) y2 y)
  15710. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15711. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15712. ;; Make sure n1 is the earlier date
  15713. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15714. (if show-all
  15715. (cond
  15716. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15717. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15718. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15719. (cond
  15720. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15721. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15722. (t (if (= cday n1) n1 n2)))))))
  15723. (defun org-date-to-gregorian (date)
  15724. "Turn any specification of DATE into a Gregorian date for the calendar."
  15725. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15726. ((and (listp date) (= (length date) 3)) date)
  15727. ((stringp date)
  15728. (setq date (org-parse-time-string date))
  15729. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15730. ((listp date)
  15731. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15732. (defun org-parse-time-string (s &optional nodefault)
  15733. "Parse the standard Org-mode time string.
  15734. This should be a lot faster than the normal `parse-time-string'.
  15735. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15736. hour and minute fields will be nil if not given."
  15737. (cond ((string-match org-ts-regexp0 s)
  15738. (list 0
  15739. (if (or (match-beginning 8) (not nodefault))
  15740. (string-to-number (or (match-string 8 s) "0")))
  15741. (if (or (match-beginning 7) (not nodefault))
  15742. (string-to-number (or (match-string 7 s) "0")))
  15743. (string-to-number (match-string 4 s))
  15744. (string-to-number (match-string 3 s))
  15745. (string-to-number (match-string 2 s))
  15746. nil nil nil))
  15747. ((string-match "^<[^>]+>$" s)
  15748. (decode-time (seconds-to-time (org-matcher-time s))))
  15749. (t (error "Not a standard Org-mode time string: %s" s))))
  15750. (defun org-timestamp-up (&optional arg)
  15751. "Increase the date item at the cursor by one.
  15752. If the cursor is on the year, change the year. If it is on the month,
  15753. the day or the time, change that.
  15754. With prefix ARG, change by that many units."
  15755. (interactive "p")
  15756. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15757. (defun org-timestamp-down (&optional arg)
  15758. "Decrease the date item at the cursor by one.
  15759. If the cursor is on the year, change the year. If it is on the month,
  15760. the day or the time, change that.
  15761. With prefix ARG, change by that many units."
  15762. (interactive "p")
  15763. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15764. (defun org-timestamp-up-day (&optional arg)
  15765. "Increase the date in the time stamp by one day.
  15766. With prefix ARG, change that many days."
  15767. (interactive "p")
  15768. (if (and (not (org-at-timestamp-p t))
  15769. (org-at-heading-p))
  15770. (org-todo 'up)
  15771. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15772. (defun org-timestamp-down-day (&optional arg)
  15773. "Decrease the date in the time stamp by one day.
  15774. With prefix ARG, change that many days."
  15775. (interactive "p")
  15776. (if (and (not (org-at-timestamp-p t))
  15777. (org-at-heading-p))
  15778. (org-todo 'down)
  15779. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15780. (defun org-at-timestamp-p (&optional inactive-ok)
  15781. "Determine if the cursor is in or at a timestamp."
  15782. (interactive)
  15783. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15784. (pos (point))
  15785. (ans (or (looking-at tsr)
  15786. (save-excursion
  15787. (skip-chars-backward "^[<\n\r\t")
  15788. (if (> (point) (point-min)) (backward-char 1))
  15789. (and (looking-at tsr)
  15790. (> (- (match-end 0) pos) -1))))))
  15791. (and ans
  15792. (boundp 'org-ts-what)
  15793. (setq org-ts-what
  15794. (cond
  15795. ((= pos (match-beginning 0)) 'bracket)
  15796. ;; Point is considered to be "on the bracket" whether
  15797. ;; it's really on it or right after it.
  15798. ((= pos (1- (match-end 0))) 'bracket)
  15799. ((= pos (match-end 0)) 'after)
  15800. ((org-pos-in-match-range pos 2) 'year)
  15801. ((org-pos-in-match-range pos 3) 'month)
  15802. ((org-pos-in-match-range pos 7) 'hour)
  15803. ((org-pos-in-match-range pos 8) 'minute)
  15804. ((or (org-pos-in-match-range pos 4)
  15805. (org-pos-in-match-range pos 5)) 'day)
  15806. ((and (> pos (or (match-end 8) (match-end 5)))
  15807. (< pos (match-end 0)))
  15808. (- pos (or (match-end 8) (match-end 5))))
  15809. (t 'day))))
  15810. ans))
  15811. (defun org-toggle-timestamp-type ()
  15812. "Toggle the type (<active> or [inactive]) of a time stamp."
  15813. (interactive)
  15814. (when (org-at-timestamp-p t)
  15815. (let ((beg (match-beginning 0)) (end (match-end 0))
  15816. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15817. (save-excursion
  15818. (goto-char beg)
  15819. (while (re-search-forward "[][<>]" end t)
  15820. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15821. t t)))
  15822. (message "Timestamp is now %sactive"
  15823. (if (equal (char-after beg) ?<) "" "in")))))
  15824. (defun org-at-clock-log-p nil
  15825. "Is the cursor on the clock log line?"
  15826. (save-excursion
  15827. (move-beginning-of-line 1)
  15828. (looking-at "^[ \t]*CLOCK:")))
  15829. (defvar org-clock-history) ; defined in org-clock.el
  15830. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15831. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15832. "Change the date in the time stamp at point.
  15833. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15834. `year', `minute', `second'. If WHAT is not given, the cursor position
  15835. in the timestamp determines what will be changed.
  15836. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15837. (let ((origin (point)) origin-cat
  15838. with-hm inactive
  15839. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15840. org-ts-what
  15841. extra rem
  15842. ts time time0 fixnext clrgx)
  15843. (if (not (org-at-timestamp-p t))
  15844. (user-error "Not at a timestamp"))
  15845. (if (and (not what) (eq org-ts-what 'bracket))
  15846. (org-toggle-timestamp-type)
  15847. ;; Point isn't on brackets. Remember the part of the time-stamp
  15848. ;; the point was in. Indeed, size of time-stamps may change,
  15849. ;; but point must be kept in the same category nonetheless.
  15850. (setq origin-cat org-ts-what)
  15851. (if (and (not what) (not (eq org-ts-what 'day))
  15852. org-display-custom-times
  15853. (get-text-property (point) 'display)
  15854. (not (get-text-property (1- (point)) 'display)))
  15855. (setq org-ts-what 'day))
  15856. (setq org-ts-what (or what org-ts-what)
  15857. inactive (= (char-after (match-beginning 0)) ?\[)
  15858. ts (match-string 0))
  15859. (replace-match "")
  15860. (when (string-match
  15861. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15862. ts)
  15863. (setq extra (match-string 1 ts))
  15864. (if suppress-tmp-delay
  15865. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15866. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15867. (setq with-hm t))
  15868. (setq time0 (org-parse-time-string ts))
  15869. (when (and updown
  15870. (eq org-ts-what 'minute)
  15871. (not current-prefix-arg))
  15872. ;; This looks like s-up and s-down. Change by one rounding step.
  15873. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15874. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15875. (setcar (cdr time0) (+ (nth 1 time0)
  15876. (if (> n 0) (- rem) (- dm rem))))))
  15877. (setq time
  15878. (encode-time (or (car time0) 0)
  15879. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15880. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15881. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15882. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15883. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15884. (nthcdr 6 time0)))
  15885. (when (and (member org-ts-what '(hour minute))
  15886. extra
  15887. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15888. (setq extra (org-modify-ts-extra
  15889. extra
  15890. (if (eq org-ts-what 'hour) 2 5)
  15891. n dm)))
  15892. (when (integerp org-ts-what)
  15893. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15894. (if (eq what 'calendar)
  15895. (let ((cal-date (org-get-date-from-calendar)))
  15896. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15897. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15898. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15899. (setcar time0 (or (car time0) 0))
  15900. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15901. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15902. (setq time (apply 'encode-time time0))))
  15903. ;; Insert the new time-stamp, and ensure point stays in the same
  15904. ;; category as before (i.e. not after the last position in that
  15905. ;; category).
  15906. (let ((pos (point)))
  15907. ;; Stay before inserted string. `save-excursion' is of no use.
  15908. (setq org-last-changed-timestamp
  15909. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15910. (goto-char pos))
  15911. (save-match-data
  15912. (looking-at org-ts-regexp3)
  15913. (goto-char (cond
  15914. ;; `day' category ends before `hour' if any, or at
  15915. ;; the end of the day name.
  15916. ((eq origin-cat 'day)
  15917. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15918. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15919. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15920. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15921. ;; `year' and `month' have both fixed size: point
  15922. ;; couldn't have moved into another part.
  15923. (t origin))))
  15924. ;; Update clock if on a CLOCK line.
  15925. (org-clock-update-time-maybe)
  15926. ;; Maybe adjust the closest clock in `org-clock-history'
  15927. (when org-clock-adjust-closest
  15928. (if (not (and (org-at-clock-log-p)
  15929. (< 1 (length (delq nil (mapcar 'marker-position
  15930. org-clock-history))))))
  15931. (message "No clock to adjust")
  15932. (cond ((save-excursion ; fix previous clock?
  15933. (re-search-backward org-ts-regexp0 nil t)
  15934. (org-looking-back (concat org-clock-string " \\[")))
  15935. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15936. ((save-excursion ; fix next clock?
  15937. (re-search-backward org-ts-regexp0 nil t)
  15938. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15939. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15940. (save-window-excursion
  15941. ;; Find closest clock to point, adjust the previous/next one in history
  15942. (let* ((p (save-excursion (org-back-to-heading t)))
  15943. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15944. (clfixnth
  15945. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15946. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15947. (if (not clfixpos)
  15948. (message "No clock to adjust")
  15949. (save-excursion
  15950. (org-goto-marker-or-bmk clfixpos)
  15951. (org-show-subtree)
  15952. (when (re-search-forward clrgx nil t)
  15953. (goto-char (match-beginning 1))
  15954. (let (org-clock-adjust-closest)
  15955. (org-timestamp-change n org-ts-what updown))
  15956. (message "Clock adjusted in %s for heading: %s"
  15957. (file-name-nondirectory (buffer-file-name))
  15958. (org-get-heading t t)))))))))
  15959. ;; Try to recenter the calendar window, if any.
  15960. (if (and org-calendar-follow-timestamp-change
  15961. (get-buffer-window "*Calendar*" t)
  15962. (memq org-ts-what '(day month year)))
  15963. (org-recenter-calendar (time-to-days time))))))
  15964. (defun org-modify-ts-extra (s pos n dm)
  15965. "Change the different parts of the lead-time and repeat fields in timestamp."
  15966. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15967. ng h m new rem)
  15968. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15969. (cond
  15970. ((or (org-pos-in-match-range pos 2)
  15971. (org-pos-in-match-range pos 3))
  15972. (setq m (string-to-number (match-string 3 s))
  15973. h (string-to-number (match-string 2 s)))
  15974. (if (org-pos-in-match-range pos 2)
  15975. (setq h (+ h n))
  15976. (setq n (* dm (org-no-warnings (signum n))))
  15977. (when (not (= 0 (setq rem (% m dm))))
  15978. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15979. (setq m (+ m n)))
  15980. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15981. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15982. (setq h (min 24 (max 0 h)))
  15983. (setq ng 1 new (format "-%02d:%02d" h m)))
  15984. ((org-pos-in-match-range pos 6)
  15985. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15986. ((org-pos-in-match-range pos 5)
  15987. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15988. ((org-pos-in-match-range pos 9)
  15989. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15990. ((org-pos-in-match-range pos 8)
  15991. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15992. (when ng
  15993. (setq s (concat
  15994. (substring s 0 (match-beginning ng))
  15995. new
  15996. (substring s (match-end ng))))))
  15997. s))
  15998. (defun org-recenter-calendar (date)
  15999. "If the calendar is visible, recenter it to DATE."
  16000. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16001. (when cwin
  16002. (let ((calendar-move-hook nil))
  16003. (with-selected-window cwin
  16004. (calendar-goto-date (if (listp date) date
  16005. (calendar-gregorian-from-absolute date))))))))
  16006. (defun org-goto-calendar (&optional arg)
  16007. "Go to the Emacs calendar at the current date.
  16008. If there is a time stamp in the current line, go to that date.
  16009. A prefix ARG can be used to force the current date."
  16010. (interactive "P")
  16011. (let ((tsr org-ts-regexp) diff
  16012. (calendar-move-hook nil)
  16013. (calendar-view-holidays-initially-flag nil)
  16014. (calendar-view-diary-initially-flag nil))
  16015. (if (or (org-at-timestamp-p)
  16016. (save-excursion
  16017. (beginning-of-line 1)
  16018. (looking-at (concat ".*" tsr))))
  16019. (let ((d1 (time-to-days (current-time)))
  16020. (d2 (time-to-days
  16021. (org-time-string-to-time (match-string 1)))))
  16022. (setq diff (- d2 d1))))
  16023. (calendar)
  16024. (calendar-goto-today)
  16025. (if (and diff (not arg)) (calendar-forward-day diff))))
  16026. (defun org-get-date-from-calendar ()
  16027. "Return a list (month day year) of date at point in calendar."
  16028. (with-current-buffer "*Calendar*"
  16029. (save-match-data
  16030. (calendar-cursor-to-date))))
  16031. (defun org-date-from-calendar ()
  16032. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16033. If there is already a time stamp at the cursor position, update it."
  16034. (interactive)
  16035. (if (org-at-timestamp-p t)
  16036. (org-timestamp-change 0 'calendar)
  16037. (let ((cal-date (org-get-date-from-calendar)))
  16038. (org-insert-time-stamp
  16039. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16040. (defcustom org-effort-durations
  16041. `(("h" . 60)
  16042. ("d" . ,(* 60 8))
  16043. ("w" . ,(* 60 8 5))
  16044. ("m" . ,(* 60 8 5 4))
  16045. ("y" . ,(* 60 8 5 40)))
  16046. "Conversion factor to minutes for an effort modifier.
  16047. Each entry has the form (MODIFIER . MINUTES).
  16048. In an effort string, a number followed by MODIFIER is multiplied
  16049. by the specified number of MINUTES to obtain an effort in
  16050. minutes.
  16051. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16052. effort string \"2hours\" is equivalent to 120 minutes."
  16053. :group 'org-agenda
  16054. :version "24.1"
  16055. :type '(alist :key-type (string :tag "Modifier")
  16056. :value-type (number :tag "Minutes")))
  16057. (defun org-minutes-to-clocksum-string (m)
  16058. "Format number of minutes as a clocksum string.
  16059. The format is determined by `org-time-clocksum-format',
  16060. `org-time-clocksum-use-fractional' and
  16061. `org-time-clocksum-fractional-format' and
  16062. `org-time-clocksum-use-effort-durations'."
  16063. (let ((clocksum "")
  16064. (m (round m)) ; Don't allow fractions of minutes
  16065. h d w mo y fmt n)
  16066. (setq h (if org-time-clocksum-use-effort-durations
  16067. (cdr (assoc "h" org-effort-durations)) 60)
  16068. d (if org-time-clocksum-use-effort-durations
  16069. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16070. w (if org-time-clocksum-use-effort-durations
  16071. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16072. mo (if org-time-clocksum-use-effort-durations
  16073. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16074. y (if org-time-clocksum-use-effort-durations
  16075. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16076. ;; fractional format
  16077. (if org-time-clocksum-use-fractional
  16078. (cond
  16079. ;; single format string
  16080. ((stringp org-time-clocksum-fractional-format)
  16081. (format org-time-clocksum-fractional-format (/ m (float h))))
  16082. ;; choice of fractional formats for different time units
  16083. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16084. (> (/ (truncate m) (* y d h)) 0))
  16085. (format fmt (/ m (* y d (float h)))))
  16086. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16087. (> (/ (truncate m) (* mo d h)) 0))
  16088. (format fmt (/ m (* mo d (float h)))))
  16089. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16090. (> (/ (truncate m) (* w d h)) 0))
  16091. (format fmt (/ m (* w d (float h)))))
  16092. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16093. (> (/ (truncate m) (* d h)) 0))
  16094. (format fmt (/ m (* d (float h)))))
  16095. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16096. (> (/ (truncate m) h) 0))
  16097. (format fmt (/ m (float h))))
  16098. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16099. (format fmt m))
  16100. ;; fall back to smallest time unit with a format
  16101. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16102. (format fmt (/ m (float h))))
  16103. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16104. (format fmt (/ m (* d (float h)))))
  16105. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16106. (format fmt (/ m (* w d (float h)))))
  16107. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16108. (format fmt (/ m (* mo d (float h)))))
  16109. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16110. (format fmt (/ m (* y d (float h))))))
  16111. ;; standard (non-fractional) format, with single format string
  16112. (if (stringp org-time-clocksum-format)
  16113. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16114. ;; separate formats components
  16115. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16116. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16117. (plist-get org-time-clocksum-format :require-years))
  16118. (setq clocksum (concat clocksum (format fmt n))
  16119. m (- m (* n y d h))))
  16120. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16121. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16122. (plist-get org-time-clocksum-format :require-months))
  16123. (setq clocksum (concat clocksum (format fmt n))
  16124. m (- m (* n mo d h))))
  16125. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16126. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16127. (plist-get org-time-clocksum-format :require-weeks))
  16128. (setq clocksum (concat clocksum (format fmt n))
  16129. m (- m (* n w d h))))
  16130. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16131. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16132. (plist-get org-time-clocksum-format :require-days))
  16133. (setq clocksum (concat clocksum (format fmt n))
  16134. m (- m (* n d h))))
  16135. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16136. (or (> (setq n (/ (truncate m) h)) 0)
  16137. (plist-get org-time-clocksum-format :require-hours))
  16138. (setq clocksum (concat clocksum (format fmt n))
  16139. m (- m (* n h))))
  16140. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16141. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16142. (setq clocksum (concat clocksum (format fmt m))))
  16143. ;; return formatted time duration
  16144. clocksum))))
  16145. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16146. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16147. "Org mode version 8.0")
  16148. (defun org-hours-to-clocksum-string (n)
  16149. (org-minutes-to-clocksum-string (* n 60)))
  16150. (defun org-hh:mm-string-to-minutes (s)
  16151. "Convert a string H:MM to a number of minutes.
  16152. If the string is just a number, interpret it as minutes.
  16153. In fact, the first hh:mm or number in the string will be taken,
  16154. there can be extra stuff in the string.
  16155. If no number is found, the return value is 0."
  16156. (cond
  16157. ((integerp s) s)
  16158. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16159. (+ (* (string-to-number (match-string 1 s)) 60)
  16160. (string-to-number (match-string 2 s))))
  16161. ((string-match "\\([0-9]+\\)" s)
  16162. (string-to-number (match-string 1 s)))
  16163. (t 0)))
  16164. (defcustom org-image-actual-width t
  16165. "Should we use the actual width of images when inlining them?
  16166. When set to `t', always use the image width.
  16167. When set to a number, use imagemagick (when available) to set
  16168. the image's width to this value.
  16169. When set to a number in a list, try to get the width from any
  16170. #+ATTR.* keyword if it matches a width specification like
  16171. #+ATTR_HTML: :width 300px
  16172. and fall back on that number if none is found.
  16173. When set to nil, try to get the width from an #+ATTR.* keyword
  16174. and fall back on the original width if none is found.
  16175. This requires Emacs >= 24.1, build with imagemagick support."
  16176. :group 'org-appearance
  16177. :version "24.4"
  16178. :package-version '(Org . "8.0")
  16179. :type '(choice
  16180. (const :tag "Use the image width" t)
  16181. (integer :tag "Use a number of pixels")
  16182. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16183. (const :tag "Use #+ATTR* or don't resize" nil)))
  16184. (defcustom org-agenda-inhibit-startup nil
  16185. "Inhibit startup when preparing agenda buffers.
  16186. When this variable is `t' (the default), the initialization of
  16187. the Org agenda buffers is inhibited: e.g. the visibility state
  16188. is not set, the tables are not re-aligned, etc."
  16189. :type 'boolean
  16190. :version "24.3"
  16191. :group 'org-agenda)
  16192. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16193. "Convert a duration string S to minutes.
  16194. A bare number is interpreted as minutes, modifiers can be set by
  16195. customizing `org-effort-durations' (which see).
  16196. Entries containing a colon are interpreted as H:MM by
  16197. `org-hh:mm-string-to-minutes'."
  16198. (let ((result 0)
  16199. (re (concat "\\([0-9.]+\\) *\\("
  16200. (regexp-opt (mapcar 'car org-effort-durations))
  16201. "\\)")))
  16202. (while (string-match re s)
  16203. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16204. (string-to-number (match-string 1 s))))
  16205. (setq s (replace-match "" nil t s)))
  16206. (setq result (floor result))
  16207. (incf result (org-hh:mm-string-to-minutes s))
  16208. (if output-to-string (number-to-string result) result)))
  16209. ;;;; Files
  16210. (defun org-save-all-org-buffers ()
  16211. "Save all Org-mode buffers without user confirmation."
  16212. (interactive)
  16213. (message "Saving all Org-mode buffers...")
  16214. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16215. (when (featurep 'org-id) (org-id-locations-save))
  16216. (message "Saving all Org-mode buffers... done"))
  16217. (defun org-revert-all-org-buffers ()
  16218. "Revert all Org-mode buffers.
  16219. Prompt for confirmation when there are unsaved changes.
  16220. Be sure you know what you are doing before letting this function
  16221. overwrite your changes.
  16222. This function is useful in a setup where one tracks org files
  16223. with a version control system, to revert on one machine after pulling
  16224. changes from another. I believe the procedure must be like this:
  16225. 1. M-x org-save-all-org-buffers
  16226. 2. Pull changes from the other machine, resolve conflicts
  16227. 3. M-x org-revert-all-org-buffers"
  16228. (interactive)
  16229. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16230. (user-error "Abort"))
  16231. (save-excursion
  16232. (save-window-excursion
  16233. (mapc
  16234. (lambda (b)
  16235. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16236. (with-current-buffer b buffer-file-name))
  16237. (org-pop-to-buffer-same-window b)
  16238. (revert-buffer t 'no-confirm)))
  16239. (buffer-list))
  16240. (when (and (featurep 'org-id) org-id-track-globally)
  16241. (org-id-locations-load)))))
  16242. ;;;; Agenda files
  16243. ;;;###autoload
  16244. (defun org-switchb (&optional arg)
  16245. "Switch between Org buffers.
  16246. With one prefix argument, restrict available buffers to files.
  16247. With two prefix arguments, restrict available buffers to agenda files.
  16248. Defaults to `iswitchb' for buffer name completion.
  16249. Set `org-completion-use-ido' to make it use ido instead."
  16250. (interactive "P")
  16251. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16252. ((equal arg '(16)) (org-buffer-list 'agenda))
  16253. (t (org-buffer-list))))
  16254. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16255. (org-completion-use-ido org-completion-use-ido))
  16256. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16257. (setq org-completion-use-iswitchb t))
  16258. (org-pop-to-buffer-same-window
  16259. (org-icompleting-read "Org buffer: "
  16260. (mapcar 'list (mapcar 'buffer-name blist))
  16261. nil t))))
  16262. ;;; Define some older names previously used for this functionality
  16263. ;;;###autoload
  16264. (defalias 'org-ido-switchb 'org-switchb)
  16265. ;;;###autoload
  16266. (defalias 'org-iswitchb 'org-switchb)
  16267. (defun org-buffer-list (&optional predicate exclude-tmp)
  16268. "Return a list of Org buffers.
  16269. PREDICATE can be `export', `files' or `agenda'.
  16270. export restrict the list to Export buffers.
  16271. files restrict the list to buffers visiting Org files.
  16272. agenda restrict the list to buffers visiting agenda files.
  16273. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16274. (let* ((bfn nil)
  16275. (agenda-files (and (eq predicate 'agenda)
  16276. (mapcar 'file-truename (org-agenda-files t))))
  16277. (filter
  16278. (cond
  16279. ((eq predicate 'files)
  16280. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16281. ((eq predicate 'export)
  16282. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16283. ((eq predicate 'agenda)
  16284. (lambda (b)
  16285. (with-current-buffer b
  16286. (and (derived-mode-p 'org-mode)
  16287. (setq bfn (buffer-file-name b))
  16288. (member (file-truename bfn) agenda-files)))))
  16289. (t (lambda (b) (with-current-buffer b
  16290. (or (derived-mode-p 'org-mode)
  16291. (string-match "\*Org .*Export"
  16292. (buffer-name b)))))))))
  16293. (delq nil
  16294. (mapcar
  16295. (lambda(b)
  16296. (if (and (funcall filter b)
  16297. (or (not exclude-tmp)
  16298. (not (string-match "tmp" (buffer-name b)))))
  16299. b
  16300. nil))
  16301. (buffer-list)))))
  16302. (defun org-agenda-files (&optional unrestricted archives)
  16303. "Get the list of agenda files.
  16304. Optional UNRESTRICTED means return the full list even if a restriction
  16305. is currently in place.
  16306. When ARCHIVES is t, include all archive files that are really being
  16307. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16308. `org-agenda-archives-mode' is t."
  16309. (let ((files
  16310. (cond
  16311. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16312. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16313. ((listp org-agenda-files) org-agenda-files)
  16314. (t (error "Invalid value of `org-agenda-files'")))))
  16315. (setq files (apply 'append
  16316. (mapcar (lambda (f)
  16317. (if (file-directory-p f)
  16318. (directory-files
  16319. f t org-agenda-file-regexp)
  16320. (list f)))
  16321. files)))
  16322. (when org-agenda-skip-unavailable-files
  16323. (setq files (delq nil
  16324. (mapcar (function
  16325. (lambda (file)
  16326. (and (file-readable-p file) file)))
  16327. files))))
  16328. (when (or (eq archives t)
  16329. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16330. (setq files (org-add-archive-files files)))
  16331. files))
  16332. (defun org-agenda-file-p (&optional file)
  16333. "Return non-nil, if FILE is an agenda file.
  16334. If FILE is omitted, use the file associated with the current
  16335. buffer."
  16336. (member (or file (buffer-file-name))
  16337. (org-agenda-files t)))
  16338. (defun org-edit-agenda-file-list ()
  16339. "Edit the list of agenda files.
  16340. Depending on setup, this either uses customize to edit the variable
  16341. `org-agenda-files', or it visits the file that is holding the list. In the
  16342. latter case, the buffer is set up in a way that saving it automatically kills
  16343. the buffer and restores the previous window configuration."
  16344. (interactive)
  16345. (if (stringp org-agenda-files)
  16346. (let ((cw (current-window-configuration)))
  16347. (find-file org-agenda-files)
  16348. (org-set-local 'org-window-configuration cw)
  16349. (org-add-hook 'after-save-hook
  16350. (lambda ()
  16351. (set-window-configuration
  16352. (prog1 org-window-configuration
  16353. (kill-buffer (current-buffer))))
  16354. (org-install-agenda-files-menu)
  16355. (message "New agenda file list installed"))
  16356. nil 'local)
  16357. (message "%s" (substitute-command-keys
  16358. "Edit list and finish with \\[save-buffer]")))
  16359. (customize-variable 'org-agenda-files)))
  16360. (defun org-store-new-agenda-file-list (list)
  16361. "Set new value for the agenda file list and save it correctly."
  16362. (if (stringp org-agenda-files)
  16363. (let ((fe (org-read-agenda-file-list t)) b u)
  16364. (while (setq b (find-buffer-visiting org-agenda-files))
  16365. (kill-buffer b))
  16366. (with-temp-file org-agenda-files
  16367. (insert
  16368. (mapconcat
  16369. (lambda (f) ;; Keep un-expanded entries.
  16370. (if (setq u (assoc f fe))
  16371. (cdr u)
  16372. f))
  16373. list "\n")
  16374. "\n")))
  16375. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16376. (org-insert-mode-line-in-empty-file nil))
  16377. (setq org-agenda-files list)
  16378. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16379. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16380. "Read the list of agenda files from a file.
  16381. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16382. filenames, used by `org-store-new-agenda-file-list' to write back
  16383. un-expanded file names."
  16384. (when (file-directory-p org-agenda-files)
  16385. (error "`org-agenda-files' cannot be a single directory"))
  16386. (when (stringp org-agenda-files)
  16387. (with-temp-buffer
  16388. (insert-file-contents org-agenda-files)
  16389. (mapcar
  16390. (lambda (f)
  16391. (let ((e (expand-file-name (substitute-in-file-name f)
  16392. org-directory)))
  16393. (if pair-with-expansion
  16394. (cons e f)
  16395. e)))
  16396. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16397. ;;;###autoload
  16398. (defun org-cycle-agenda-files ()
  16399. "Cycle through the files in `org-agenda-files'.
  16400. If the current buffer visits an agenda file, find the next one in the list.
  16401. If the current buffer does not, find the first agenda file."
  16402. (interactive)
  16403. (let* ((fs (org-agenda-files t))
  16404. (files (append fs (list (car fs))))
  16405. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16406. file)
  16407. (unless files (user-error "No agenda files"))
  16408. (catch 'exit
  16409. (while (setq file (pop files))
  16410. (if (equal (file-truename file) tcf)
  16411. (when (car files)
  16412. (find-file (car files))
  16413. (throw 'exit t))))
  16414. (find-file (car fs)))
  16415. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16416. (defun org-agenda-file-to-front (&optional to-end)
  16417. "Move/add the current file to the top of the agenda file list.
  16418. If the file is not present in the list, it is added to the front. If it is
  16419. present, it is moved there. With optional argument TO-END, add/move to the
  16420. end of the list."
  16421. (interactive "P")
  16422. (let ((org-agenda-skip-unavailable-files nil)
  16423. (file-alist (mapcar (lambda (x)
  16424. (cons (file-truename x) x))
  16425. (org-agenda-files t)))
  16426. (ctf (file-truename
  16427. (or buffer-file-name
  16428. (user-error "Please save the current buffer to a file"))))
  16429. x had)
  16430. (setq x (assoc ctf file-alist) had x)
  16431. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16432. (if to-end
  16433. (setq file-alist (append (delq x file-alist) (list x)))
  16434. (setq file-alist (cons x (delq x file-alist))))
  16435. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16436. (org-install-agenda-files-menu)
  16437. (message "File %s to %s of agenda file list"
  16438. (if had "moved" "added") (if to-end "end" "front"))))
  16439. (defun org-remove-file (&optional file)
  16440. "Remove current file from the list of files in variable `org-agenda-files'.
  16441. These are the files which are being checked for agenda entries.
  16442. Optional argument FILE means use this file instead of the current."
  16443. (interactive)
  16444. (let* ((org-agenda-skip-unavailable-files nil)
  16445. (file (or file buffer-file-name
  16446. (user-error "Current buffer does not visit a file")))
  16447. (true-file (file-truename file))
  16448. (afile (abbreviate-file-name file))
  16449. (files (delq nil (mapcar
  16450. (lambda (x)
  16451. (if (equal true-file
  16452. (file-truename x))
  16453. nil x))
  16454. (org-agenda-files t)))))
  16455. (if (not (= (length files) (length (org-agenda-files t))))
  16456. (progn
  16457. (org-store-new-agenda-file-list files)
  16458. (org-install-agenda-files-menu)
  16459. (message "Removed file: %s" afile))
  16460. (message "File was not in list: %s (not removed)" afile))))
  16461. (defun org-file-menu-entry (file)
  16462. (vector file (list 'find-file file) t))
  16463. (defun org-check-agenda-file (file)
  16464. "Make sure FILE exists. If not, ask user what to do."
  16465. (when (not (file-exists-p file))
  16466. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16467. (abbreviate-file-name file))
  16468. (let ((r (downcase (read-char-exclusive))))
  16469. (cond
  16470. ((equal r ?r)
  16471. (org-remove-file file)
  16472. (throw 'nextfile t))
  16473. (t (error "Abort"))))))
  16474. (defun org-get-agenda-file-buffer (file)
  16475. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16476. it to the list of buffers which might be released later."
  16477. (let ((buf (org-find-base-buffer-visiting file)))
  16478. (if buf
  16479. buf ; just return it
  16480. ;; Make a new buffer and remember it
  16481. (setq buf (find-file-noselect file))
  16482. (if buf (push buf org-agenda-new-buffers))
  16483. buf)))
  16484. (defun org-release-buffers (blist)
  16485. "Release all buffers in list, asking the user for confirmation when needed.
  16486. When a buffer is unmodified, it is just killed. When modified, it is saved
  16487. \(if the user agrees) and then killed."
  16488. (let (buf file)
  16489. (while (setq buf (pop blist))
  16490. (setq file (buffer-file-name buf))
  16491. (when (and (buffer-modified-p buf)
  16492. file
  16493. (y-or-n-p (format "Save file %s? " file)))
  16494. (with-current-buffer buf (save-buffer)))
  16495. (kill-buffer buf))))
  16496. (defun org-agenda-prepare-buffers (files)
  16497. "Create buffers for all agenda files, protect archived trees and comments."
  16498. (interactive)
  16499. (let ((pa '(:org-archived t))
  16500. (pc '(:org-comment t))
  16501. (pall '(:org-archived t :org-comment t))
  16502. (inhibit-read-only t)
  16503. (org-inhibit-startup org-agenda-inhibit-startup)
  16504. (rea (concat ":" org-archive-tag ":"))
  16505. file re)
  16506. (setq org-tag-alist-for-agenda nil
  16507. org-tag-groups-alist-for-agenda nil)
  16508. (save-excursion
  16509. (save-restriction
  16510. (while (setq file (pop files))
  16511. (catch 'nextfile
  16512. (if (bufferp file)
  16513. (set-buffer file)
  16514. (org-check-agenda-file file)
  16515. (set-buffer (org-get-agenda-file-buffer file)))
  16516. (widen)
  16517. (org-set-regexps-and-options-for-tags)
  16518. (goto-char (point-min))
  16519. (let ((case-fold-search t))
  16520. (when (search-forward "#+setupfile" nil t)
  16521. ;; Don't set all regexps and options systematically as
  16522. ;; this is only run for setting agenda tags from setup
  16523. ;; file
  16524. (org-set-regexps-and-options)))
  16525. (org-refresh-category-properties)
  16526. (org-refresh-properties org-effort-property 'org-effort)
  16527. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16528. (setq org-todo-keywords-for-agenda
  16529. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16530. (setq org-done-keywords-for-agenda
  16531. (append org-done-keywords-for-agenda org-done-keywords))
  16532. (setq org-todo-keyword-alist-for-agenda
  16533. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16534. (setq org-drawers-for-agenda
  16535. (append org-drawers-for-agenda org-drawers))
  16536. (setq org-tag-alist-for-agenda
  16537. (org-uniquify
  16538. (append org-tag-alist-for-agenda
  16539. org-tag-alist
  16540. org-tag-persistent-alist)))
  16541. (if org-group-tags
  16542. (setq org-tag-groups-alist-for-agenda
  16543. (org-uniquify-alist
  16544. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16545. (org-with-silent-modifications
  16546. (save-excursion
  16547. (remove-text-properties (point-min) (point-max) pall)
  16548. (when org-agenda-skip-archived-trees
  16549. (goto-char (point-min))
  16550. (while (re-search-forward rea nil t)
  16551. (if (org-at-heading-p t)
  16552. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16553. (goto-char (point-min))
  16554. (setq re (format org-heading-keyword-regexp-format
  16555. org-comment-string))
  16556. (while (re-search-forward re nil t)
  16557. (add-text-properties
  16558. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16559. (setq org-todo-keywords-for-agenda
  16560. (org-uniquify org-todo-keywords-for-agenda))
  16561. (setq org-todo-keyword-alist-for-agenda
  16562. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16563. ;;;; CDLaTeX minor mode
  16564. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16565. "Keymap for the minor `org-cdlatex-mode'.")
  16566. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16567. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16568. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16569. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16570. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16571. (defvar org-cdlatex-texmathp-advice-is-done nil
  16572. "Flag remembering if we have applied the advice to texmathp already.")
  16573. (define-minor-mode org-cdlatex-mode
  16574. "Toggle the minor `org-cdlatex-mode'.
  16575. This mode supports entering LaTeX environment and math in LaTeX fragments
  16576. in Org-mode.
  16577. \\{org-cdlatex-mode-map}"
  16578. nil " OCDL" nil
  16579. (when org-cdlatex-mode
  16580. (require 'cdlatex)
  16581. (run-hooks 'cdlatex-mode-hook)
  16582. (cdlatex-compute-tables))
  16583. (unless org-cdlatex-texmathp-advice-is-done
  16584. (setq org-cdlatex-texmathp-advice-is-done t)
  16585. (defadvice texmathp (around org-math-always-on activate)
  16586. "Always return t in org-mode buffers.
  16587. This is because we want to insert math symbols without dollars even outside
  16588. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16589. an embedded LaTeX fragment, let texmathp do its job.
  16590. \\[org-cdlatex-mode-map]"
  16591. (interactive)
  16592. (let (p)
  16593. (cond
  16594. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16595. ((eq this-command 'cdlatex-math-symbol)
  16596. (setq ad-return-value t
  16597. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16598. (t
  16599. (let ((p (org-inside-LaTeX-fragment-p)))
  16600. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16601. (setq ad-return-value t
  16602. texmathp-why '("Org-mode embedded math" . 0))
  16603. (if p ad-do-it)))))))))
  16604. (defun turn-on-org-cdlatex ()
  16605. "Unconditionally turn on `org-cdlatex-mode'."
  16606. (org-cdlatex-mode 1))
  16607. (defun org-try-cdlatex-tab ()
  16608. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16609. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16610. - inside a LaTeX fragment, or
  16611. - after the first word in a line, where an abbreviation expansion could
  16612. insert a LaTeX environment."
  16613. (when org-cdlatex-mode
  16614. (cond
  16615. ;; Before any word on the line: No expansion possible.
  16616. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16617. ;; Just after first word on the line: Expand it. Make sure it
  16618. ;; cannot happen on headlines, though.
  16619. ((save-excursion
  16620. (skip-chars-backward "a-zA-Z0-9*")
  16621. (skip-chars-backward " \t")
  16622. (and (bolp) (not (org-at-heading-p))))
  16623. (cdlatex-tab) t)
  16624. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16625. (defun org-cdlatex-underscore-caret (&optional arg)
  16626. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16627. Revert to the normal definition outside of these fragments."
  16628. (interactive "P")
  16629. (if (org-inside-LaTeX-fragment-p)
  16630. (call-interactively 'cdlatex-sub-superscript)
  16631. (let (org-cdlatex-mode)
  16632. (call-interactively (key-binding (vector last-input-event))))))
  16633. (defun org-cdlatex-math-modify (&optional arg)
  16634. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16635. Revert to the normal definition outside of these fragments."
  16636. (interactive "P")
  16637. (if (org-inside-LaTeX-fragment-p)
  16638. (call-interactively 'cdlatex-math-modify)
  16639. (let (org-cdlatex-mode)
  16640. (call-interactively (key-binding (vector last-input-event))))))
  16641. ;;;; LaTeX fragments
  16642. (defvar org-latex-regexps
  16643. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16644. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16645. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16646. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16647. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16648. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16649. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16650. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16651. "Regular expressions for matching embedded LaTeX.")
  16652. (defun org-inside-LaTeX-fragment-p ()
  16653. "Test if point is inside a LaTeX fragment.
  16654. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16655. sequence appearing also before point.
  16656. Even though the matchers for math are configurable, this function assumes
  16657. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16658. delimiters are skipped when they have been removed by customization.
  16659. The return value is nil, or a cons cell with the delimiter and the
  16660. position of this delimiter.
  16661. This function does a reasonably good job, but can locally be fooled by
  16662. for example currency specifications. For example it will assume being in
  16663. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16664. fragments that are properly closed, but during editing, we have to live
  16665. with the uncertainty caused by missing closing delimiters. This function
  16666. looks only before point, not after."
  16667. (catch 'exit
  16668. (let ((pos (point))
  16669. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16670. (lim (progn
  16671. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16672. (point)))
  16673. dd-on str (start 0) m re)
  16674. (goto-char pos)
  16675. (when dodollar
  16676. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16677. re (nth 1 (assoc "$" org-latex-regexps)))
  16678. (while (string-match re str start)
  16679. (cond
  16680. ((= (match-end 0) (length str))
  16681. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16682. ((= (match-end 0) (- (length str) 5))
  16683. (throw 'exit nil))
  16684. (t (setq start (match-end 0))))))
  16685. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16686. (goto-char pos)
  16687. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16688. (and (match-beginning 2) (throw 'exit nil))
  16689. ;; count $$
  16690. (while (re-search-backward "\\$\\$" lim t)
  16691. (setq dd-on (not dd-on)))
  16692. (goto-char pos)
  16693. (if dd-on (cons "$$" m))))))
  16694. (defun org-inside-latex-macro-p ()
  16695. "Is point inside a LaTeX macro or its arguments?"
  16696. (save-match-data
  16697. (org-in-regexp
  16698. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16699. (defvar org-latex-fragment-image-overlays nil
  16700. "List of overlays carrying the images of latex fragments.")
  16701. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16702. (defun org-remove-latex-fragment-image-overlays ()
  16703. "Remove all overlays with LaTeX fragment images in current buffer."
  16704. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16705. (setq org-latex-fragment-image-overlays nil))
  16706. (defun org-preview-latex-fragment (&optional subtree)
  16707. "Preview the LaTeX fragment at point, or all locally or globally.
  16708. If the cursor is in a LaTeX fragment, create the image and overlay
  16709. it over the source code. If there is no fragment at point, display
  16710. all fragments in the current text, from one headline to the next. With
  16711. prefix SUBTREE, display all fragments in the current subtree. With a
  16712. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16713. the cursor is before the first headline,
  16714. display all fragments in the buffer.
  16715. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16716. (interactive "P")
  16717. (unless buffer-file-name
  16718. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16719. (org-remove-latex-fragment-image-overlays)
  16720. (save-excursion
  16721. (save-restriction
  16722. (let (beg end at msg)
  16723. (cond
  16724. ((or (equal subtree '(16))
  16725. (not (save-excursion
  16726. (re-search-backward org-outline-regexp-bol nil t))))
  16727. (setq beg (point-min) end (point-max)
  16728. msg "Creating images for buffer...%s"))
  16729. ((equal subtree '(4))
  16730. (org-back-to-heading)
  16731. (setq beg (point) end (org-end-of-subtree t)
  16732. msg "Creating images for subtree...%s"))
  16733. (t
  16734. (if (setq at (org-inside-LaTeX-fragment-p))
  16735. (goto-char (max (point-min) (- (cdr at) 2)))
  16736. (org-back-to-heading))
  16737. (setq beg (point) end (progn (outline-next-heading) (point))
  16738. msg (if at "Creating image...%s"
  16739. "Creating images for entry...%s"))))
  16740. (message msg "")
  16741. (narrow-to-region beg end)
  16742. (goto-char beg)
  16743. (org-format-latex
  16744. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16745. (file-name-nondirectory
  16746. buffer-file-name)))
  16747. default-directory 'overlays msg at 'forbuffer
  16748. org-latex-create-formula-image-program)
  16749. (message msg "done. Use `C-c C-c' to remove images.")))))
  16750. (defun org-format-latex (prefix &optional dir overlays msg at
  16751. forbuffer processing-type)
  16752. "Replace LaTeX fragments with links to an image, and produce images.
  16753. Some of the options can be changed using the variable
  16754. `org-format-latex-options'."
  16755. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16756. (let* ((prefixnodir (file-name-nondirectory prefix))
  16757. (absprefix (expand-file-name prefix dir))
  16758. (todir (file-name-directory absprefix))
  16759. (opt org-format-latex-options)
  16760. (optnew org-format-latex-options)
  16761. (matchers (plist-get opt :matchers))
  16762. (re-list org-latex-regexps)
  16763. (cnt 0) txt hash link beg end re e checkdir
  16764. string
  16765. m n block-type block linkfile movefile ov)
  16766. ;; Check the different regular expressions
  16767. (while (setq e (pop re-list))
  16768. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16769. block (if block-type "\n\n" ""))
  16770. (when (member m matchers)
  16771. (goto-char (point-min))
  16772. (while (re-search-forward re nil t)
  16773. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16774. (or (not overlays)
  16775. (not (eq (get-char-property (match-beginning n)
  16776. 'org-overlay-type)
  16777. 'org-latex-overlay))))
  16778. (cond
  16779. ((eq processing-type 'verbatim))
  16780. ((eq processing-type 'mathjax)
  16781. ;; Prepare for MathJax processing.
  16782. (setq string (match-string n))
  16783. (when (member m '("$" "$1"))
  16784. (save-excursion
  16785. (delete-region (match-beginning n) (match-end n))
  16786. (goto-char (match-beginning n))
  16787. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16788. ((or (eq processing-type 'dvipng)
  16789. (eq processing-type 'imagemagick))
  16790. ;; Process to an image.
  16791. (setq txt (match-string n)
  16792. beg (match-beginning n) end (match-end n)
  16793. cnt (1+ cnt))
  16794. (let ((face (face-at-point))
  16795. (fg (plist-get opt :foreground))
  16796. (bg (plist-get opt :background))
  16797. ;; Ensure full list is printed.
  16798. print-length print-level)
  16799. (when forbuffer
  16800. ;; Get the colors from the face at point.
  16801. (goto-char beg)
  16802. (when (eq fg 'auto)
  16803. (setq fg (face-attribute face :foreground nil 'default)))
  16804. (when (eq bg 'auto)
  16805. (setq bg (face-attribute face :background nil 'default)))
  16806. (setq optnew (copy-sequence opt))
  16807. (plist-put optnew :foreground fg)
  16808. (plist-put optnew :background bg))
  16809. (setq hash (sha1 (prin1-to-string
  16810. (list org-format-latex-header
  16811. org-latex-default-packages-alist
  16812. org-latex-packages-alist
  16813. org-format-latex-options
  16814. forbuffer txt fg bg)))
  16815. linkfile (format "%s_%s.png" prefix hash)
  16816. movefile (format "%s_%s.png" absprefix hash)))
  16817. (setq link (concat block "[[file:" linkfile "]]" block))
  16818. (if msg (message msg cnt))
  16819. (goto-char beg)
  16820. (unless checkdir ; Ensure the directory exists.
  16821. (setq checkdir t)
  16822. (or (file-directory-p todir) (make-directory todir t)))
  16823. (unless (file-exists-p movefile)
  16824. (org-create-formula-image
  16825. txt movefile optnew forbuffer processing-type))
  16826. (if overlays
  16827. (progn
  16828. (mapc (lambda (o)
  16829. (if (eq (overlay-get o 'org-overlay-type)
  16830. 'org-latex-overlay)
  16831. (delete-overlay o)))
  16832. (overlays-in beg end))
  16833. (setq ov (make-overlay beg end))
  16834. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16835. (if (featurep 'xemacs)
  16836. (progn
  16837. (overlay-put ov 'invisible t)
  16838. (overlay-put
  16839. ov 'end-glyph
  16840. (make-glyph (vector 'png :file movefile))))
  16841. (overlay-put
  16842. ov 'display
  16843. (list 'image :type 'png :file movefile :ascent 'center)))
  16844. (push ov org-latex-fragment-image-overlays)
  16845. (goto-char end))
  16846. (delete-region beg end)
  16847. (insert (org-add-props link
  16848. (list 'org-latex-src
  16849. (replace-regexp-in-string
  16850. "\"" "" txt)
  16851. 'org-latex-src-embed-type
  16852. (if block-type 'paragraph 'character))))))
  16853. ((eq processing-type 'mathml)
  16854. ;; Process to MathML
  16855. (unless (save-match-data (org-format-latex-mathml-available-p))
  16856. (user-error "LaTeX to MathML converter not configured"))
  16857. (setq txt (match-string n)
  16858. beg (match-beginning n) end (match-end n)
  16859. cnt (1+ cnt))
  16860. (if msg (message msg cnt))
  16861. (goto-char beg)
  16862. (delete-region beg end)
  16863. (insert (org-format-latex-as-mathml
  16864. txt block-type prefix dir)))
  16865. (t
  16866. (error "Unknown conversion type %s for LaTeX fragments"
  16867. processing-type)))))))))
  16868. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16869. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16870. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16871. sucessful, return the portion between \"<math...> </math>\"
  16872. elements otherwise return nil. When MATHML-FILE is specified,
  16873. write the results in to that file. When invoked as an
  16874. interactive command, prompt for LATEX-FRAG, with initial value
  16875. set to the current active region and echo the results for user
  16876. inspection."
  16877. (interactive (list (let ((frag (when (org-region-active-p)
  16878. (buffer-substring-no-properties
  16879. (region-beginning) (region-end)))))
  16880. (read-string "LaTeX Fragment: " frag nil frag))))
  16881. (unless latex-frag (error "Invalid LaTeX fragment"))
  16882. (let* ((tmp-in-file (file-relative-name
  16883. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16884. (ignore (write-region latex-frag nil tmp-in-file))
  16885. (tmp-out-file (file-relative-name
  16886. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16887. (cmd (format-spec
  16888. org-latex-to-mathml-convert-command
  16889. `((?j . ,(shell-quote-argument
  16890. (expand-file-name org-latex-to-mathml-jar-file)))
  16891. (?I . ,(shell-quote-argument tmp-in-file))
  16892. (?o . ,(shell-quote-argument tmp-out-file)))))
  16893. mathml shell-command-output)
  16894. (when (org-called-interactively-p 'any)
  16895. (unless (org-format-latex-mathml-available-p)
  16896. (user-error "LaTeX to MathML converter not configured")))
  16897. (message "Running %s" cmd)
  16898. (setq shell-command-output (shell-command-to-string cmd))
  16899. (setq mathml
  16900. (when (file-readable-p tmp-out-file)
  16901. (with-current-buffer (find-file-noselect tmp-out-file t)
  16902. (goto-char (point-min))
  16903. (when (re-search-forward
  16904. (concat
  16905. (regexp-quote
  16906. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16907. "\\(.\\|\n\\)*"
  16908. (regexp-quote "</math>")) nil t)
  16909. (prog1 (match-string 0) (kill-buffer))))))
  16910. (cond
  16911. (mathml
  16912. (setq mathml
  16913. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16914. (when mathml-file
  16915. (write-region mathml nil mathml-file))
  16916. (when (org-called-interactively-p 'any)
  16917. (message mathml)))
  16918. ((message "LaTeX to MathML conversion failed")
  16919. (message shell-command-output)))
  16920. (delete-file tmp-in-file)
  16921. (when (file-exists-p tmp-out-file)
  16922. (delete-file tmp-out-file))
  16923. mathml))
  16924. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16925. prefix &optional dir)
  16926. "Use `org-create-math-formula' but check local cache first."
  16927. (let* ((absprefix (expand-file-name prefix dir))
  16928. (print-length nil) (print-level nil)
  16929. (formula-id (concat
  16930. "formula-"
  16931. (sha1
  16932. (prin1-to-string
  16933. (list latex-frag
  16934. org-latex-to-mathml-convert-command)))))
  16935. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16936. (formula-cache-dir (file-name-directory formula-cache)))
  16937. (unless (file-directory-p formula-cache-dir)
  16938. (make-directory formula-cache-dir t))
  16939. (unless (file-exists-p formula-cache)
  16940. (org-create-math-formula latex-frag formula-cache))
  16941. (if (file-exists-p formula-cache)
  16942. ;; Successful conversion. Return the link to MathML file.
  16943. (org-add-props
  16944. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16945. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16946. 'org-latex-src-embed-type (if latex-frag-type
  16947. 'paragraph 'character)))
  16948. ;; Failed conversion. Return the LaTeX fragment verbatim
  16949. latex-frag)))
  16950. (defun org-create-formula-image (string tofile options buffer &optional type)
  16951. "Create an image from LaTeX source using dvipng or convert.
  16952. This function calls either `org-create-formula-image-with-dvipng'
  16953. or `org-create-formula-image-with-imagemagick' depending on the
  16954. value of `org-latex-create-formula-image-program' or on the value
  16955. of the optional TYPE variable.
  16956. Note: ultimately these two function should be combined as they
  16957. share a good deal of logic."
  16958. (org-check-external-command
  16959. "latex" "needed to convert LaTeX fragments to images")
  16960. (funcall
  16961. (case (or type org-latex-create-formula-image-program)
  16962. ('dvipng
  16963. (org-check-external-command
  16964. "dvipng" "needed to convert LaTeX fragments to images")
  16965. #'org-create-formula-image-with-dvipng)
  16966. ('imagemagick
  16967. (org-check-external-command
  16968. "convert" "you need to install imagemagick")
  16969. #'org-create-formula-image-with-imagemagick)
  16970. (t (error
  16971. "Invalid value of `org-latex-create-formula-image-program'")))
  16972. string tofile options buffer))
  16973. (declare-function org-export--get-global-options "ox" (&optional backend))
  16974. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16975. (defun org-create-formula--latex-header ()
  16976. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16977. (org-latex-guess-inputenc
  16978. (org-splice-latex-header
  16979. org-format-latex-header
  16980. org-latex-default-packages-alist
  16981. org-latex-packages-alist t
  16982. (plist-get
  16983. (org-combine-plists
  16984. (org-export--get-global-options 'latex)
  16985. (org-export--get-inbuffer-options 'latex))
  16986. :latex-header))))
  16987. ;; This function borrows from Ganesh Swami's latex2png.el
  16988. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16989. "This calls dvipng."
  16990. (require 'ox-latex)
  16991. (let* ((tmpdir (if (featurep 'xemacs)
  16992. (temp-directory)
  16993. temporary-file-directory))
  16994. (texfilebase (make-temp-name
  16995. (expand-file-name "orgtex" tmpdir)))
  16996. (texfile (concat texfilebase ".tex"))
  16997. (dvifile (concat texfilebase ".dvi"))
  16998. (pngfile (concat texfilebase ".png"))
  16999. (fnh (if (featurep 'xemacs)
  17000. (font-height (face-font 'default))
  17001. (face-attribute 'default :height nil)))
  17002. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17003. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17004. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17005. "Black"))
  17006. (bg (or (plist-get options (if buffer :background :html-background))
  17007. "Transparent")))
  17008. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17009. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17010. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17011. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17012. (let ((latex-header (org-create-formula--latex-header)))
  17013. (with-temp-file texfile
  17014. (insert latex-header)
  17015. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17016. (let ((dir default-directory))
  17017. (condition-case nil
  17018. (progn
  17019. (cd tmpdir)
  17020. (call-process "latex" nil nil nil texfile))
  17021. (error nil))
  17022. (cd dir))
  17023. (if (not (file-exists-p dvifile))
  17024. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17025. (condition-case nil
  17026. (if (featurep 'xemacs)
  17027. (call-process "dvipng" nil nil nil
  17028. "-fg" fg "-bg" bg
  17029. "-T" "tight"
  17030. "-o" pngfile
  17031. dvifile)
  17032. (call-process "dvipng" nil nil nil
  17033. "-fg" fg "-bg" bg
  17034. "-D" dpi
  17035. ;;"-x" scale "-y" scale
  17036. "-T" "tight"
  17037. "-o" pngfile
  17038. dvifile))
  17039. (error nil))
  17040. (if (not (file-exists-p pngfile))
  17041. (if org-format-latex-signal-error
  17042. (error "Failed to create png file from %s" texfile)
  17043. (message "Failed to create png file from %s" texfile)
  17044. nil)
  17045. ;; Use the requested file name and clean up
  17046. (copy-file pngfile tofile 'replace)
  17047. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17048. (if (file-exists-p (concat texfilebase e))
  17049. (delete-file (concat texfilebase e))))
  17050. pngfile))))
  17051. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17052. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17053. "This calls convert, which is included into imagemagick."
  17054. (require 'ox-latex)
  17055. (let* ((tmpdir (if (featurep 'xemacs)
  17056. (temp-directory)
  17057. temporary-file-directory))
  17058. (texfilebase (make-temp-name
  17059. (expand-file-name "orgtex" tmpdir)))
  17060. (texfile (concat texfilebase ".tex"))
  17061. (pdffile (concat texfilebase ".pdf"))
  17062. (pngfile (concat texfilebase ".png"))
  17063. (fnh (if (featurep 'xemacs)
  17064. (font-height (face-font 'default))
  17065. (face-attribute 'default :height nil)))
  17066. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17067. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17068. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17069. "black"))
  17070. (bg (or (plist-get options (if buffer :background :html-background))
  17071. "white")))
  17072. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17073. (setq fg (org-latex-color-format fg)))
  17074. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17075. (setq bg (org-latex-color-format
  17076. (if (string= bg "Transparent") "white" bg))))
  17077. (let ((latex-header (org-create-formula--latex-header)))
  17078. (with-temp-file texfile
  17079. (insert latex-header)
  17080. (insert "\n\\begin{document}\n"
  17081. "\\definecolor{fg}{rgb}{" fg "}\n"
  17082. "\\definecolor{bg}{rgb}{" bg "}\n"
  17083. "\n\\pagecolor{bg}\n"
  17084. "\n{\\color{fg}\n"
  17085. string
  17086. "\n}\n"
  17087. "\n\\end{document}\n")))
  17088. (org-latex-compile texfile t)
  17089. (if (not (file-exists-p pdffile))
  17090. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17091. (condition-case nil
  17092. (if (featurep 'xemacs)
  17093. (call-process "convert" nil nil nil
  17094. "-density" "96"
  17095. "-trim"
  17096. "-antialias"
  17097. pdffile
  17098. "-quality" "100"
  17099. ;; "-sharpen" "0x1.0"
  17100. pngfile)
  17101. (call-process "convert" nil nil nil
  17102. "-density" dpi
  17103. "-trim"
  17104. "-antialias"
  17105. pdffile
  17106. "-quality" "100"
  17107. ;; "-sharpen" "0x1.0"
  17108. pngfile))
  17109. (error nil))
  17110. (if (not (file-exists-p pngfile))
  17111. (if org-format-latex-signal-error
  17112. (error "Failed to create png file from %s" texfile)
  17113. (message "Failed to create png file from %s" texfile)
  17114. nil)
  17115. ;; Use the requested file name and clean up
  17116. (copy-file pngfile tofile 'replace)
  17117. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17118. (if (file-exists-p (concat texfilebase e))
  17119. (delete-file (concat texfilebase e))))
  17120. pngfile))))
  17121. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17122. "Fill a LaTeX header template TPL.
  17123. In the template, the following place holders will be recognized:
  17124. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17125. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17126. [PACKAGES] \\usepackage statements for PKG
  17127. [NO-PACKAGES] do not include PKG
  17128. [EXTRA] the string EXTRA
  17129. [NO-EXTRA] do not include EXTRA
  17130. For backward compatibility, if both the positive and the negative place
  17131. holder is missing, the positive one (without the \"NO-\") will be
  17132. assumed to be present at the end of the template.
  17133. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17134. EXTRA is a string.
  17135. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17136. (let (rpl (end ""))
  17137. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17138. (setq rpl (if (or (match-end 1) (not def-pkg))
  17139. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17140. tpl (replace-match rpl t t tpl))
  17141. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17142. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17143. (setq rpl (if (or (match-end 1) (not pkg))
  17144. "" (org-latex-packages-to-string pkg snippets-p t))
  17145. tpl (replace-match rpl t t tpl))
  17146. (if pkg (setq end
  17147. (concat end "\n"
  17148. (org-latex-packages-to-string pkg snippets-p)))))
  17149. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17150. (setq rpl (if (or (match-end 1) (not extra))
  17151. "" (concat extra "\n"))
  17152. tpl (replace-match rpl t t tpl))
  17153. (if (and extra (string-match "\\S-" extra))
  17154. (setq end (concat end "\n" extra))))
  17155. (if (string-match "\\S-" end)
  17156. (concat tpl "\n" end)
  17157. tpl)))
  17158. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17159. "Turn an alist of packages into a string with the \\usepackage macros."
  17160. (setq pkg (mapconcat (lambda(p)
  17161. (cond
  17162. ((stringp p) p)
  17163. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17164. (format "%% Package %s omitted" (cadr p)))
  17165. ((equal "" (car p))
  17166. (format "\\usepackage{%s}" (cadr p)))
  17167. (t
  17168. (format "\\usepackage[%s]{%s}"
  17169. (car p) (cadr p)))))
  17170. pkg
  17171. "\n"))
  17172. (if newline (concat pkg "\n") pkg))
  17173. (defun org-dvipng-color (attr)
  17174. "Return a RGB color specification for dvipng."
  17175. (apply 'format "rgb %s %s %s"
  17176. (mapcar 'org-normalize-color
  17177. (if (featurep 'xemacs)
  17178. (color-rgb-components
  17179. (face-property 'default
  17180. (cond ((eq attr :foreground) 'foreground)
  17181. ((eq attr :background) 'background))))
  17182. (color-values (face-attribute 'default attr nil))))))
  17183. (defun org-dvipng-color-format (color-name)
  17184. "Convert COLOR-NAME to a RGB color value for dvipng."
  17185. (apply 'format "rgb %s %s %s"
  17186. (mapcar 'org-normalize-color
  17187. (color-values color-name))))
  17188. (defun org-latex-color (attr)
  17189. "Return a RGB color for the LaTeX color package."
  17190. (apply 'format "%s,%s,%s"
  17191. (mapcar 'org-normalize-color
  17192. (if (featurep 'xemacs)
  17193. (color-rgb-components
  17194. (face-property 'default
  17195. (cond ((eq attr :foreground) 'foreground)
  17196. ((eq attr :background) 'background))))
  17197. (color-values (face-attribute 'default attr nil))))))
  17198. (defun org-latex-color-format (color-name)
  17199. "Convert COLOR-NAME to a RGB color value."
  17200. (apply 'format "%s,%s,%s"
  17201. (mapcar 'org-normalize-color
  17202. (color-values color-name))))
  17203. (defun org-normalize-color (value)
  17204. "Return string to be used as color value for an RGB component."
  17205. (format "%g" (/ value 65535.0)))
  17206. ;; Image display
  17207. (defvar org-inline-image-overlays nil)
  17208. (make-variable-buffer-local 'org-inline-image-overlays)
  17209. (defun org-toggle-inline-images (&optional include-linked)
  17210. "Toggle the display of inline images.
  17211. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17212. (interactive "P")
  17213. (if org-inline-image-overlays
  17214. (progn
  17215. (org-remove-inline-images)
  17216. (message "Inline image display turned off"))
  17217. (org-display-inline-images include-linked)
  17218. (if (and (org-called-interactively-p)
  17219. org-inline-image-overlays)
  17220. (message "%d images displayed inline"
  17221. (length org-inline-image-overlays))
  17222. (message "No images to display inline"))))
  17223. (defun org-redisplay-inline-images ()
  17224. "Refresh the display of inline images."
  17225. (interactive)
  17226. (if (not org-inline-image-overlays)
  17227. (org-toggle-inline-images)
  17228. (org-toggle-inline-images)
  17229. (org-toggle-inline-images)))
  17230. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17231. "Display inline images.
  17232. Normally only links without a description part are inlined, because this
  17233. is how it will work for export. When INCLUDE-LINKED is set, also links
  17234. with a description part will be inlined. This can be nice for a quick
  17235. look at those images, but it does not reflect what exported files will look
  17236. like.
  17237. When REFRESH is set, refresh existing images between BEG and END.
  17238. This will create new image displays only if necessary.
  17239. BEG and END default to the buffer boundaries."
  17240. (interactive "P")
  17241. (unless refresh
  17242. (org-remove-inline-images)
  17243. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17244. (save-excursion
  17245. (save-restriction
  17246. (widen)
  17247. (setq beg (or beg (point-min)) end (or end (point-max)))
  17248. (goto-char beg)
  17249. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17250. (substring (org-image-file-name-regexp) 0 -2)
  17251. "\\)\\]" (if include-linked "" "\\]")))
  17252. (case-fold-search t)
  17253. old file ov img type attrwidth width)
  17254. (while (re-search-forward re end t)
  17255. (setq old (get-char-property-and-overlay (match-beginning 1)
  17256. 'org-image-overlay)
  17257. file (expand-file-name
  17258. (concat (or (match-string 3) "") (match-string 4))))
  17259. (when (image-type-available-p 'imagemagick)
  17260. (setq attrwidth (if (or (listp org-image-actual-width)
  17261. (null org-image-actual-width))
  17262. (save-excursion
  17263. (save-match-data
  17264. (when (re-search-backward
  17265. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17266. (save-excursion
  17267. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17268. (string-to-number (match-string 1))))))
  17269. width (cond ((eq org-image-actual-width t) nil)
  17270. ((null org-image-actual-width) attrwidth)
  17271. ((numberp org-image-actual-width)
  17272. org-image-actual-width)
  17273. ((listp org-image-actual-width)
  17274. (or attrwidth (car org-image-actual-width))))
  17275. type (if width 'imagemagick)))
  17276. (when (file-exists-p file)
  17277. (if (and (car-safe old) refresh)
  17278. (image-refresh (overlay-get (cdr old) 'display))
  17279. (setq img (save-match-data (create-image file type nil :width width)))
  17280. (when img
  17281. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17282. (overlay-put ov 'display img)
  17283. (overlay-put ov 'face 'default)
  17284. (overlay-put ov 'org-image-overlay t)
  17285. (overlay-put ov 'modification-hooks
  17286. (list 'org-display-inline-remove-overlay))
  17287. (push ov org-inline-image-overlays)))))))))
  17288. (define-obsolete-function-alias
  17289. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17290. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17291. "Remove inline-display overlay if a corresponding region is modified."
  17292. (let ((inhibit-modification-hooks t))
  17293. (when (and ov after)
  17294. (delete ov org-inline-image-overlays)
  17295. (delete-overlay ov))))
  17296. (defun org-remove-inline-images ()
  17297. "Remove inline display of images."
  17298. (interactive)
  17299. (mapc 'delete-overlay org-inline-image-overlays)
  17300. (setq org-inline-image-overlays nil))
  17301. ;;;; Key bindings
  17302. ;; Outline functions from `outline-mode-prefix-map'
  17303. ;; that can be remapped in Org:
  17304. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17305. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17306. (define-key org-mode-map [remap outline-forward-same-level]
  17307. 'org-forward-heading-same-level)
  17308. (define-key org-mode-map [remap outline-backward-same-level]
  17309. 'org-backward-heading-same-level)
  17310. (define-key org-mode-map [remap show-branches]
  17311. 'org-kill-note-or-show-branches)
  17312. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17313. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17314. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17315. ;; Outline functions from `outline-mode-prefix-map' that can not
  17316. ;; be remapped in Org:
  17317. ;;
  17318. ;; - the column "key binding" shows whether the Outline function is still
  17319. ;; available in Org mode on the same key that it has been bound to in
  17320. ;; Outline mode:
  17321. ;; - "overridden": key used for a different functionality in Org mode
  17322. ;; - else: key still bound to the same Outline function in Org mode
  17323. ;;
  17324. ;; | Outline function | key binding | Org replacement |
  17325. ;; |------------------------------------+-------------+-----------------------|
  17326. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17327. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17328. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17329. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17330. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17331. ;; | `show-entry' | overridden | no replacement |
  17332. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17333. ;; | `show-branches' | `C-c C-k' | still same function |
  17334. ;; | `show-subtree' | overridden | visibility cycling |
  17335. ;; | `show-all' | overridden | no replacement |
  17336. ;; | `hide-subtree' | overridden | visibility cycling |
  17337. ;; | `hide-body' | overridden | no replacement |
  17338. ;; | `hide-entry' | overridden | visibility cycling |
  17339. ;; | `hide-leaves' | overridden | no replacement |
  17340. ;; | `hide-sublevels' | overridden | no replacement |
  17341. ;; | `hide-other' | overridden | no replacement |
  17342. ;; Make `C-c C-x' a prefix key
  17343. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17344. ;; TAB key with modifiers
  17345. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17346. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17347. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17348. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17349. ;; The following line is necessary under Suse GNU/Linux
  17350. (unless (featurep 'xemacs)
  17351. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17352. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17353. (define-key org-mode-map [backtab] 'org-shifttab)
  17354. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17355. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17356. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17357. ;; Cursor keys with modifiers
  17358. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17359. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17360. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17361. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17362. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17363. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17364. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17365. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17366. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17367. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17368. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17369. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17370. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17371. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17372. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17373. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17374. ;; Babel keys
  17375. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17376. (mapc (lambda (pair)
  17377. (define-key org-babel-map (car pair) (cdr pair)))
  17378. org-babel-key-bindings)
  17379. ;;; Extra keys for tty access.
  17380. ;; We only set them when really needed because otherwise the
  17381. ;; menus don't show the simple keys
  17382. (when (or org-use-extra-keys
  17383. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17384. (not window-system))
  17385. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17386. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17387. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17388. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17389. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17390. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17391. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17392. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17393. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17394. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17395. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17396. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17397. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17398. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17399. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17400. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17401. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17402. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17403. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17404. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17405. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17406. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17407. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17408. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17409. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17410. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17411. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17412. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17413. ;; All the other keys
  17414. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17415. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17416. (if (boundp 'narrow-map)
  17417. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17418. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17419. (if (boundp 'narrow-map)
  17420. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17421. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17422. (if (boundp 'narrow-map)
  17423. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17424. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17425. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17426. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17427. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17428. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17429. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17430. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17431. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17432. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17433. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17434. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17435. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17436. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17437. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17438. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17439. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17440. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17441. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17442. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17443. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17444. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17445. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17446. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17447. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17448. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17449. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17450. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17451. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17452. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17453. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17454. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17455. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17456. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17457. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17458. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17459. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17460. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17461. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17462. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17463. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17464. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17465. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17466. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17467. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17468. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17469. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17470. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17471. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17472. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17473. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17474. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17475. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17476. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17477. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17478. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17479. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17480. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17481. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17482. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17483. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17484. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17485. (org-defkey org-mode-map "\C-m" 'org-return)
  17486. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17487. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17488. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17489. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17490. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17491. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17492. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17493. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17494. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17495. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17496. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17497. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17498. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17499. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17500. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17501. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17502. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17503. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17504. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17505. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17506. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17507. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17508. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17509. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17510. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17511. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17512. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17513. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17514. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17515. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17516. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17517. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17518. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17519. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17520. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17521. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17522. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17523. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17524. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17525. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17526. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17527. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17528. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17529. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17530. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17531. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17532. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17533. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17534. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17535. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17536. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17537. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17538. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17539. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17540. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17541. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17542. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17543. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17544. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17545. (when (featurep 'xemacs)
  17546. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17547. (defconst org-speed-commands-default
  17548. '(
  17549. ("Outline Navigation")
  17550. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17551. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17552. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17553. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17554. ("F" . org-next-block)
  17555. ("B" . org-previous-block)
  17556. ("u" . (org-speed-move-safe 'outline-up-heading))
  17557. ("j" . org-goto)
  17558. ("g" . (org-refile t))
  17559. ("Outline Visibility")
  17560. ("c" . org-cycle)
  17561. ("C" . org-shifttab)
  17562. (" " . org-display-outline-path)
  17563. ("s" . org-narrow-to-subtree)
  17564. ("=" . org-columns)
  17565. ("Outline Structure Editing")
  17566. ("U" . org-shiftmetaup)
  17567. ("D" . org-shiftmetadown)
  17568. ("r" . org-metaright)
  17569. ("l" . org-metaleft)
  17570. ("R" . org-shiftmetaright)
  17571. ("L" . org-shiftmetaleft)
  17572. ("i" . (progn (forward-char 1) (call-interactively
  17573. 'org-insert-heading-respect-content)))
  17574. ("^" . org-sort)
  17575. ("w" . org-refile)
  17576. ("a" . org-archive-subtree-default-with-confirmation)
  17577. ("@" . org-mark-subtree)
  17578. ("#" . org-toggle-comment)
  17579. ("Clock Commands")
  17580. ("I" . org-clock-in)
  17581. ("O" . org-clock-out)
  17582. ("Meta Data Editing")
  17583. ("t" . org-todo)
  17584. ("," . (org-priority))
  17585. ("0" . (org-priority ?\ ))
  17586. ("1" . (org-priority ?A))
  17587. ("2" . (org-priority ?B))
  17588. ("3" . (org-priority ?C))
  17589. (":" . org-set-tags-command)
  17590. ("e" . org-set-effort)
  17591. ("E" . org-inc-effort)
  17592. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17593. (org-entry-put (point) "APPT_WARNTIME" m)))
  17594. ("Agenda Views etc")
  17595. ("v" . org-agenda)
  17596. ("/" . org-sparse-tree)
  17597. ("Misc")
  17598. ("o" . org-open-at-point)
  17599. ("?" . org-speed-command-help)
  17600. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17601. (">" . (org-agenda-remove-restriction-lock))
  17602. )
  17603. "The default speed commands.")
  17604. (defun org-print-speed-command (e)
  17605. (if (> (length (car e)) 1)
  17606. (progn
  17607. (princ "\n")
  17608. (princ (car e))
  17609. (princ "\n")
  17610. (princ (make-string (length (car e)) ?-))
  17611. (princ "\n"))
  17612. (princ (car e))
  17613. (princ " ")
  17614. (if (symbolp (cdr e))
  17615. (princ (symbol-name (cdr e)))
  17616. (prin1 (cdr e)))
  17617. (princ "\n")))
  17618. (defun org-speed-command-help ()
  17619. "Show the available speed commands."
  17620. (interactive)
  17621. (if (not org-use-speed-commands)
  17622. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17623. (with-output-to-temp-buffer "*Help*"
  17624. (princ "User-defined Speed commands\n===========================\n")
  17625. (mapc 'org-print-speed-command org-speed-commands-user)
  17626. (princ "\n")
  17627. (princ "Built-in Speed commands\n=======================\n")
  17628. (mapc 'org-print-speed-command org-speed-commands-default))
  17629. (with-current-buffer "*Help*"
  17630. (setq truncate-lines t))))
  17631. (defun org-speed-move-safe (cmd)
  17632. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17633. If not, return to the original position and throw an error."
  17634. (interactive)
  17635. (let ((pos (point)))
  17636. (call-interactively cmd)
  17637. (unless (and (bolp) (org-at-heading-p))
  17638. (goto-char pos)
  17639. (error "Boundary reached while executing %s" cmd))))
  17640. (defvar org-self-insert-command-undo-counter 0)
  17641. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17642. (defvar org-speed-command nil)
  17643. (define-obsolete-function-alias
  17644. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17645. (defun org-speed-command-activate (keys)
  17646. "Hook for activating single-letter speed commands.
  17647. `org-speed-commands-default' specifies a minimal command set.
  17648. Use `org-speed-commands-user' for further customization."
  17649. (when (or (and (bolp) (looking-at org-outline-regexp))
  17650. (and (functionp org-use-speed-commands)
  17651. (funcall org-use-speed-commands)))
  17652. (cdr (assoc keys (append org-speed-commands-user
  17653. org-speed-commands-default)))))
  17654. (define-obsolete-function-alias
  17655. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17656. (defun org-babel-speed-command-activate (keys)
  17657. "Hook for activating single-letter code block commands."
  17658. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17659. (cdr (assoc keys org-babel-key-bindings))))
  17660. (defcustom org-speed-command-hook
  17661. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17662. "Hook for activating speed commands at strategic locations.
  17663. Hook functions are called in sequence until a valid handler is
  17664. found.
  17665. Each hook takes a single argument, a user-pressed command key
  17666. which is also a `self-insert-command' from the global map.
  17667. Within the hook, examine the cursor position and the command key
  17668. and return nil or a valid handler as appropriate. Handler could
  17669. be one of an interactive command, a function, or a form.
  17670. Set `org-use-speed-commands' to non-nil value to enable this
  17671. hook. The default setting is `org-speed-command-activate'."
  17672. :group 'org-structure
  17673. :version "24.1"
  17674. :type 'hook)
  17675. (defun org-self-insert-command (N)
  17676. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17677. If the cursor is in a table looking at whitespace, the whitespace is
  17678. overwritten, and the table is not marked as requiring realignment."
  17679. (interactive "p")
  17680. (org-check-before-invisible-edit 'insert)
  17681. (cond
  17682. ((and org-use-speed-commands
  17683. (setq org-speed-command
  17684. (run-hook-with-args-until-success
  17685. 'org-speed-command-hook (this-command-keys))))
  17686. (cond
  17687. ((commandp org-speed-command)
  17688. (setq this-command org-speed-command)
  17689. (call-interactively org-speed-command))
  17690. ((functionp org-speed-command)
  17691. (funcall org-speed-command))
  17692. ((and org-speed-command (listp org-speed-command))
  17693. (eval org-speed-command))
  17694. (t (let (org-use-speed-commands)
  17695. (call-interactively 'org-self-insert-command)))))
  17696. ((and
  17697. (org-table-p)
  17698. (progn
  17699. ;; check if we blank the field, and if that triggers align
  17700. (and (featurep 'org-table) org-table-auto-blank-field
  17701. (member last-command
  17702. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17703. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17704. ;; got extra space, this field does not determine column width
  17705. (let (org-table-may-need-update) (org-table-blank-field))
  17706. ;; no extra space, this field may determine column width
  17707. (org-table-blank-field)))
  17708. t)
  17709. (eq N 1)
  17710. (looking-at "[^|\n]* |"))
  17711. (let (org-table-may-need-update)
  17712. (goto-char (1- (match-end 0)))
  17713. (backward-delete-char 1)
  17714. (goto-char (match-beginning 0))
  17715. (self-insert-command N)))
  17716. (t
  17717. (setq org-table-may-need-update t)
  17718. (self-insert-command N)
  17719. (org-fix-tags-on-the-fly)
  17720. (if org-self-insert-cluster-for-undo
  17721. (if (not (eq last-command 'org-self-insert-command))
  17722. (setq org-self-insert-command-undo-counter 1)
  17723. (if (>= org-self-insert-command-undo-counter 20)
  17724. (setq org-self-insert-command-undo-counter 1)
  17725. (and (> org-self-insert-command-undo-counter 0)
  17726. buffer-undo-list (listp buffer-undo-list)
  17727. (not (cadr buffer-undo-list)) ; remove nil entry
  17728. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17729. (setq org-self-insert-command-undo-counter
  17730. (1+ org-self-insert-command-undo-counter))))))))
  17731. (defun org-check-before-invisible-edit (kind)
  17732. "Check is editing if kind KIND would be dangerous with invisible text around.
  17733. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17734. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17735. (if (and org-catch-invisible-edits
  17736. (or (not (boundp 'visible-mode)) (not visible-mode))
  17737. (or (get-char-property (point) 'invisible)
  17738. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17739. ;; OK, we need to take a closer look
  17740. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17741. (invisible-before-point (if (bobp) nil (get-char-property
  17742. (1- (point)) 'invisible)))
  17743. (border-and-ok-direction
  17744. (or
  17745. ;; Check if we are acting predictably before invisible text
  17746. (and invisible-at-point (not invisible-before-point)
  17747. (memq kind '(insert delete-backward)))
  17748. ;; Check if we are acting predictably after invisible text
  17749. ;; This works not well, and I have turned it off. It seems
  17750. ;; better to always show and stop after invisible text.
  17751. ;; (and (not invisible-at-point) invisible-before-point
  17752. ;; (memq kind '(insert delete)))
  17753. )))
  17754. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17755. (memq invisible-before-point '(outline org-hide-block t)))
  17756. (if (eq org-catch-invisible-edits 'error)
  17757. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17758. (if (and org-custom-properties-overlays
  17759. (y-or-n-p "Display invisible properties in this buffer? "))
  17760. (org-toggle-custom-properties-visibility)
  17761. ;; Make the area visible
  17762. (save-excursion
  17763. (if invisible-before-point
  17764. (goto-char (previous-single-char-property-change
  17765. (point) 'invisible)))
  17766. (org-cycle))
  17767. (cond
  17768. ((eq org-catch-invisible-edits 'show)
  17769. ;; That's it, we do the edit after showing
  17770. (message
  17771. "Unfolding invisible region around point before editing")
  17772. (sit-for 1))
  17773. ((and (eq org-catch-invisible-edits 'smart)
  17774. border-and-ok-direction)
  17775. (message "Unfolding invisible region around point before editing"))
  17776. (t
  17777. ;; Don't do the edit, make the user repeat it in full visibility
  17778. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17779. (defun org-fix-tags-on-the-fly ()
  17780. (when (and (equal (char-after (point-at-bol)) ?*)
  17781. (org-at-heading-p))
  17782. (org-align-tags-here org-tags-column)))
  17783. (defun org-delete-backward-char (N)
  17784. "Like `delete-backward-char', insert whitespace at field end in tables.
  17785. When deleting backwards, in tables this function will insert whitespace in
  17786. front of the next \"|\" separator, to keep the table aligned. The table will
  17787. still be marked for re-alignment if the field did fill the entire column,
  17788. because, in this case the deletion might narrow the column."
  17789. (interactive "p")
  17790. (save-match-data
  17791. (org-check-before-invisible-edit 'delete-backward)
  17792. (if (and (org-table-p)
  17793. (eq N 1)
  17794. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17795. (looking-at ".*?|"))
  17796. (let ((pos (point))
  17797. (noalign (looking-at "[^|\n\r]* |"))
  17798. (c org-table-may-need-update))
  17799. (backward-delete-char N)
  17800. (if (not overwrite-mode)
  17801. (progn
  17802. (skip-chars-forward "^|")
  17803. (insert " ")
  17804. (goto-char (1- pos))))
  17805. ;; noalign: if there were two spaces at the end, this field
  17806. ;; does not determine the width of the column.
  17807. (if noalign (setq org-table-may-need-update c)))
  17808. (backward-delete-char N)
  17809. (org-fix-tags-on-the-fly))))
  17810. (defun org-delete-char (N)
  17811. "Like `delete-char', but insert whitespace at field end in tables.
  17812. When deleting characters, in tables this function will insert whitespace in
  17813. front of the next \"|\" separator, to keep the table aligned. The table will
  17814. still be marked for re-alignment if the field did fill the entire column,
  17815. because, in this case the deletion might narrow the column."
  17816. (interactive "p")
  17817. (save-match-data
  17818. (org-check-before-invisible-edit 'delete)
  17819. (if (and (org-table-p)
  17820. (not (bolp))
  17821. (not (= (char-after) ?|))
  17822. (eq N 1))
  17823. (if (looking-at ".*?|")
  17824. (let ((pos (point))
  17825. (noalign (looking-at "[^|\n\r]* |"))
  17826. (c org-table-may-need-update))
  17827. (replace-match
  17828. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17829. (goto-char pos)
  17830. ;; noalign: if there were two spaces at the end, this field
  17831. ;; does not determine the width of the column.
  17832. (if noalign (setq org-table-may-need-update c)))
  17833. (delete-char N))
  17834. (delete-char N)
  17835. (org-fix-tags-on-the-fly))))
  17836. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17837. (put 'org-self-insert-command 'delete-selection t)
  17838. (put 'orgtbl-self-insert-command 'delete-selection t)
  17839. (put 'org-delete-char 'delete-selection 'supersede)
  17840. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17841. (put 'org-yank 'delete-selection 'yank)
  17842. ;; Make `flyspell-mode' delay after some commands
  17843. (put 'org-self-insert-command 'flyspell-delayed t)
  17844. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17845. (put 'org-delete-char 'flyspell-delayed t)
  17846. (put 'org-delete-backward-char 'flyspell-delayed t)
  17847. ;; Make pabbrev-mode expand after org-mode commands
  17848. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17849. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17850. ;; How to do this: Measure non-white length of current string
  17851. ;; If equal to column width, we should realign.
  17852. (defun org-remap (map &rest commands)
  17853. "In MAP, remap the functions given in COMMANDS.
  17854. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17855. (let (new old)
  17856. (while commands
  17857. (setq old (pop commands) new (pop commands))
  17858. (if (fboundp 'command-remapping)
  17859. (org-defkey map (vector 'remap old) new)
  17860. (substitute-key-definition old new map global-map)))))
  17861. (defun org-transpose-words ()
  17862. "Transpose words for Org.
  17863. This uses the `org-mode-transpose-word-syntax-table' syntax
  17864. table, which interprets characters in `org-emphasis-alist' as
  17865. word constituants."
  17866. (interactive)
  17867. (with-syntax-table org-mode-transpose-word-syntax-table
  17868. (call-interactively 'transpose-words)))
  17869. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17870. (when (eq org-enable-table-editor 'optimized)
  17871. ;; If the user wants maximum table support, we need to hijack
  17872. ;; some standard editing functions
  17873. (org-remap org-mode-map
  17874. 'self-insert-command 'org-self-insert-command
  17875. 'delete-char 'org-delete-char
  17876. 'delete-backward-char 'org-delete-backward-char)
  17877. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17878. (defvar org-ctrl-c-ctrl-c-hook nil
  17879. "Hook for functions attaching themselves to `C-c C-c'.
  17880. This can be used to add additional functionality to the C-c C-c
  17881. key which executes context-dependent commands. This hook is run
  17882. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17883. run after the last test.
  17884. Each function will be called with no arguments. The function
  17885. must check if the context is appropriate for it to act. If yes,
  17886. it should do its thing and then return a non-nil value. If the
  17887. context is wrong, just do nothing and return nil.")
  17888. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17889. "Hook for functions attaching themselves to `C-c C-c'.
  17890. This can be used to add additional functionality to the C-c C-c
  17891. key which executes context-dependent commands. This hook is run
  17892. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17893. before the first test.
  17894. Each function will be called with no arguments. The function
  17895. must check if the context is appropriate for it to act. If yes,
  17896. it should do its thing and then return a non-nil value. If the
  17897. context is wrong, just do nothing and return nil.")
  17898. (defvar org-tab-first-hook nil
  17899. "Hook for functions to attach themselves to TAB.
  17900. See `org-ctrl-c-ctrl-c-hook' for more information.
  17901. This hook runs as the first action when TAB is pressed, even before
  17902. `org-cycle' messes around with the `outline-regexp' to cater for
  17903. inline tasks and plain list item folding.
  17904. If any function in this hook returns t, any other actions that
  17905. would have been caused by TAB (such as table field motion or visibility
  17906. cycling) will not occur.")
  17907. (defvar org-tab-after-check-for-table-hook nil
  17908. "Hook for functions to attach themselves to TAB.
  17909. See `org-ctrl-c-ctrl-c-hook' for more information.
  17910. This hook runs after it has been established that the cursor is not in a
  17911. table, but before checking if the cursor is in a headline or if global cycling
  17912. should be done.
  17913. If any function in this hook returns t, not other actions like visibility
  17914. cycling will be done.")
  17915. (defvar org-tab-after-check-for-cycling-hook nil
  17916. "Hook for functions to attach themselves to TAB.
  17917. See `org-ctrl-c-ctrl-c-hook' for more information.
  17918. This hook runs after it has been established that not table field motion and
  17919. not visibility should be done because of current context. This is probably
  17920. the place where a package like yasnippets can hook in.")
  17921. (defvar org-tab-before-tab-emulation-hook nil
  17922. "Hook for functions to attach themselves to TAB.
  17923. See `org-ctrl-c-ctrl-c-hook' for more information.
  17924. This hook runs after every other options for TAB have been exhausted, but
  17925. before indentation and \t insertion takes place.")
  17926. (defvar org-metaleft-hook nil
  17927. "Hook for functions attaching themselves to `M-left'.
  17928. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17929. (defvar org-metaright-hook nil
  17930. "Hook for functions attaching themselves to `M-right'.
  17931. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17932. (defvar org-metaup-hook nil
  17933. "Hook for functions attaching themselves to `M-up'.
  17934. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17935. (defvar org-metadown-hook nil
  17936. "Hook for functions attaching themselves to `M-down'.
  17937. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17938. (defvar org-shiftmetaleft-hook nil
  17939. "Hook for functions attaching themselves to `M-S-left'.
  17940. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17941. (defvar org-shiftmetaright-hook nil
  17942. "Hook for functions attaching themselves to `M-S-right'.
  17943. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17944. (defvar org-shiftmetaup-hook nil
  17945. "Hook for functions attaching themselves to `M-S-up'.
  17946. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17947. (defvar org-shiftmetadown-hook nil
  17948. "Hook for functions attaching themselves to `M-S-down'.
  17949. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17950. (defvar org-metareturn-hook nil
  17951. "Hook for functions attaching themselves to `M-RET'.
  17952. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17953. (defvar org-shiftup-hook nil
  17954. "Hook for functions attaching themselves to `S-up'.
  17955. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17956. (defvar org-shiftup-final-hook nil
  17957. "Hook for functions attaching themselves to `S-up'.
  17958. This one runs after all other options except shift-select have been excluded.
  17959. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17960. (defvar org-shiftdown-hook nil
  17961. "Hook for functions attaching themselves to `S-down'.
  17962. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17963. (defvar org-shiftdown-final-hook nil
  17964. "Hook for functions attaching themselves to `S-down'.
  17965. This one runs after all other options except shift-select have been excluded.
  17966. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17967. (defvar org-shiftleft-hook nil
  17968. "Hook for functions attaching themselves to `S-left'.
  17969. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17970. (defvar org-shiftleft-final-hook nil
  17971. "Hook for functions attaching themselves to `S-left'.
  17972. This one runs after all other options except shift-select have been excluded.
  17973. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17974. (defvar org-shiftright-hook nil
  17975. "Hook for functions attaching themselves to `S-right'.
  17976. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17977. (defvar org-shiftright-final-hook nil
  17978. "Hook for functions attaching themselves to `S-right'.
  17979. This one runs after all other options except shift-select have been excluded.
  17980. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17981. (defun org-modifier-cursor-error ()
  17982. "Throw an error, a modified cursor command was applied in wrong context."
  17983. (user-error "This command is active in special context like tables, headlines or items"))
  17984. (defun org-shiftselect-error ()
  17985. "Throw an error because Shift-Cursor command was applied in wrong context."
  17986. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17987. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17988. (user-error "This command works only in special context like headlines or timestamps")))
  17989. (defun org-call-for-shift-select (cmd)
  17990. (let ((this-command-keys-shift-translated t))
  17991. (call-interactively cmd)))
  17992. (defun org-shifttab (&optional arg)
  17993. "Global visibility cycling or move to previous table field.
  17994. Call `org-table-previous-field' within a table.
  17995. When ARG is nil, cycle globally through visibility states.
  17996. When ARG is a numeric prefix, show contents of this level."
  17997. (interactive "P")
  17998. (cond
  17999. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18000. ((integerp arg)
  18001. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18002. (message "Content view to level: %d" arg)
  18003. (org-content (prefix-numeric-value arg2))
  18004. (org-cycle-show-empty-lines t)
  18005. (setq org-cycle-global-status 'overview)))
  18006. (t (call-interactively 'org-global-cycle))))
  18007. (defun org-shiftmetaleft ()
  18008. "Promote subtree or delete table column.
  18009. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18010. `org-table-delete-column', depending on context. See the
  18011. individual commands for more information."
  18012. (interactive)
  18013. (cond
  18014. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18015. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18016. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18017. ((if (not (org-region-active-p)) (org-at-item-p)
  18018. (save-excursion (goto-char (region-beginning))
  18019. (org-at-item-p)))
  18020. (call-interactively 'org-outdent-item-tree))
  18021. (t (org-modifier-cursor-error))))
  18022. (defun org-shiftmetaright ()
  18023. "Demote subtree or insert table column.
  18024. Calls `org-demote-subtree', `org-indent-item-tree', or
  18025. `org-table-insert-column', depending on context. See the
  18026. individual commands for more information."
  18027. (interactive)
  18028. (cond
  18029. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18030. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18031. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18032. ((if (not (org-region-active-p)) (org-at-item-p)
  18033. (save-excursion (goto-char (region-beginning))
  18034. (org-at-item-p)))
  18035. (call-interactively 'org-indent-item-tree))
  18036. (t (org-modifier-cursor-error))))
  18037. (defun org-shiftmetaup (&optional arg)
  18038. "Move subtree up or kill table row.
  18039. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18040. `org-move-item-up' or `org-timestamp-up', depending on context.
  18041. See the individual commands for more information."
  18042. (interactive "P")
  18043. (cond
  18044. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18045. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18046. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18047. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18048. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18049. (call-interactively 'org-timestamp-up)))
  18050. (t (call-interactively 'org-drag-line-backward))))
  18051. (defun org-shiftmetadown (&optional arg)
  18052. "Move subtree down or insert table row.
  18053. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18054. `org-move-item-down' or `org-timestamp-up', depending on context.
  18055. See the individual commands for more information."
  18056. (interactive "P")
  18057. (cond
  18058. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18059. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18060. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18061. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18062. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18063. (call-interactively 'org-timestamp-down)))
  18064. (t (call-interactively 'org-drag-line-forward))))
  18065. (defsubst org-hidden-tree-error ()
  18066. (user-error
  18067. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18068. (defun org-metaleft (&optional arg)
  18069. "Promote heading or move table column to left.
  18070. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18071. With no specific context, calls the Emacs default `backward-word'.
  18072. See the individual commands for more information."
  18073. (interactive "P")
  18074. (cond
  18075. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18076. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18077. ((org-with-limited-levels
  18078. (or (org-at-heading-p)
  18079. (and (org-region-active-p)
  18080. (save-excursion
  18081. (goto-char (region-beginning))
  18082. (org-at-heading-p)))))
  18083. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18084. (call-interactively 'org-do-promote))
  18085. ;; At an inline task.
  18086. ((org-at-heading-p)
  18087. (call-interactively 'org-inlinetask-promote))
  18088. ((or (org-at-item-p)
  18089. (and (org-region-active-p)
  18090. (save-excursion
  18091. (goto-char (region-beginning))
  18092. (org-at-item-p))))
  18093. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18094. (call-interactively 'org-outdent-item))
  18095. (t (call-interactively 'backward-word))))
  18096. (defun org-metaright (&optional arg)
  18097. "Demote a subtree, a list item or move table column to right.
  18098. In front of a drawer or a block keyword, indent it correctly.
  18099. With no specific context, calls the Emacs default `forward-word'.
  18100. See the individual commands for more information."
  18101. (interactive "P")
  18102. (cond
  18103. ((run-hook-with-args-until-success 'org-metaright-hook))
  18104. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18105. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18106. ((org-at-block-p) (call-interactively 'org-indent-block))
  18107. ((org-with-limited-levels
  18108. (or (org-at-heading-p)
  18109. (and (org-region-active-p)
  18110. (save-excursion
  18111. (goto-char (region-beginning))
  18112. (org-at-heading-p)))))
  18113. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18114. (call-interactively 'org-do-demote))
  18115. ;; At an inline task.
  18116. ((org-at-heading-p)
  18117. (call-interactively 'org-inlinetask-demote))
  18118. ((or (org-at-item-p)
  18119. (and (org-region-active-p)
  18120. (save-excursion
  18121. (goto-char (region-beginning))
  18122. (org-at-item-p))))
  18123. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18124. (call-interactively 'org-indent-item))
  18125. (t (call-interactively 'forward-word))))
  18126. (defun org-check-for-hidden (what)
  18127. "Check if there are hidden headlines/items in the current visual line.
  18128. WHAT can be either `headlines' or `items'. If the current line is
  18129. an outline or item heading and it has a folded subtree below it,
  18130. this function returns t, nil otherwise."
  18131. (let ((re (cond
  18132. ((eq what 'headlines) org-outline-regexp-bol)
  18133. ((eq what 'items) (org-item-beginning-re))
  18134. (t (error "This should not happen"))))
  18135. beg end)
  18136. (save-excursion
  18137. (catch 'exit
  18138. (unless (org-region-active-p)
  18139. (setq beg (point-at-bol))
  18140. (beginning-of-line 2)
  18141. (while (and (not (eobp)) ;; this is like `next-line'
  18142. (get-char-property (1- (point)) 'invisible))
  18143. (beginning-of-line 2))
  18144. (setq end (point))
  18145. (goto-char beg)
  18146. (goto-char (point-at-eol))
  18147. (setq end (max end (point)))
  18148. (while (re-search-forward re end t)
  18149. (if (get-char-property (match-beginning 0) 'invisible)
  18150. (throw 'exit t))))
  18151. nil))))
  18152. (defun org-metaup (&optional arg)
  18153. "Move subtree up or move table row up.
  18154. Calls `org-move-subtree-up' or `org-table-move-row' or
  18155. `org-move-item-up', depending on context. See the individual commands
  18156. for more information."
  18157. (interactive "P")
  18158. (cond
  18159. ((run-hook-with-args-until-success 'org-metaup-hook))
  18160. ((org-region-active-p)
  18161. (let* ((a (min (region-beginning) (region-end)))
  18162. (b (1- (max (region-beginning) (region-end))))
  18163. (c (save-excursion (goto-char a)
  18164. (move-beginning-of-line 0)))
  18165. (d (save-excursion (goto-char a)
  18166. (move-end-of-line 0) (point))))
  18167. (transpose-regions a b c d)
  18168. (goto-char c)))
  18169. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18170. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18171. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18172. (t (org-drag-element-backward))))
  18173. (defun org-metadown (&optional arg)
  18174. "Move subtree down or move table row down.
  18175. Calls `org-move-subtree-down' or `org-table-move-row' or
  18176. `org-move-item-down', depending on context. See the individual
  18177. commands for more information."
  18178. (interactive "P")
  18179. (cond
  18180. ((run-hook-with-args-until-success 'org-metadown-hook))
  18181. ((org-region-active-p)
  18182. (let* ((a (min (region-beginning) (region-end)))
  18183. (b (max (region-beginning) (region-end)))
  18184. (c (save-excursion (goto-char b)
  18185. (move-beginning-of-line 1)))
  18186. (d (save-excursion (goto-char b)
  18187. (move-end-of-line 1) (1+ (point)))))
  18188. (transpose-regions a b c d)
  18189. (goto-char d)))
  18190. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18191. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18192. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18193. (t (org-drag-element-forward))))
  18194. (defun org-shiftup (&optional arg)
  18195. "Increase item in timestamp or increase priority of current headline.
  18196. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18197. depending on context. See the individual commands for more information."
  18198. (interactive "P")
  18199. (cond
  18200. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18201. ((and org-support-shift-select (org-region-active-p))
  18202. (org-call-for-shift-select 'previous-line))
  18203. ((org-at-timestamp-p t)
  18204. (call-interactively (if org-edit-timestamp-down-means-later
  18205. 'org-timestamp-down 'org-timestamp-up)))
  18206. ((and (not (eq org-support-shift-select 'always))
  18207. org-enable-priority-commands
  18208. (org-at-heading-p))
  18209. (call-interactively 'org-priority-up))
  18210. ((and (not org-support-shift-select) (org-at-item-p))
  18211. (call-interactively 'org-previous-item))
  18212. ((org-clocktable-try-shift 'up arg))
  18213. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18214. (org-support-shift-select
  18215. (org-call-for-shift-select 'previous-line))
  18216. (t (org-shiftselect-error))))
  18217. (defun org-shiftdown (&optional arg)
  18218. "Decrease item in timestamp or decrease priority of current headline.
  18219. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18220. depending on context. See the individual commands for more information."
  18221. (interactive "P")
  18222. (cond
  18223. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18224. ((and org-support-shift-select (org-region-active-p))
  18225. (org-call-for-shift-select 'next-line))
  18226. ((org-at-timestamp-p t)
  18227. (call-interactively (if org-edit-timestamp-down-means-later
  18228. 'org-timestamp-up 'org-timestamp-down)))
  18229. ((and (not (eq org-support-shift-select 'always))
  18230. org-enable-priority-commands
  18231. (org-at-heading-p))
  18232. (call-interactively 'org-priority-down))
  18233. ((and (not org-support-shift-select) (org-at-item-p))
  18234. (call-interactively 'org-next-item))
  18235. ((org-clocktable-try-shift 'down arg))
  18236. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18237. (org-support-shift-select
  18238. (org-call-for-shift-select 'next-line))
  18239. (t (org-shiftselect-error))))
  18240. (defun org-shiftright (&optional arg)
  18241. "Cycle the thing at point or in the current line, depending on context.
  18242. Depending on context, this does one of the following:
  18243. - switch a timestamp at point one day into the future
  18244. - on a headline, switch to the next TODO keyword.
  18245. - on an item, switch entire list to the next bullet type
  18246. - on a property line, switch to the next allowed value
  18247. - on a clocktable definition line, move time block into the future"
  18248. (interactive "P")
  18249. (cond
  18250. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18251. ((and org-support-shift-select (org-region-active-p))
  18252. (org-call-for-shift-select 'forward-char))
  18253. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18254. ((and (not (eq org-support-shift-select 'always))
  18255. (org-at-heading-p))
  18256. (let ((org-inhibit-logging
  18257. (not org-treat-S-cursor-todo-selection-as-state-change))
  18258. (org-inhibit-blocking
  18259. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18260. (org-call-with-arg 'org-todo 'right)))
  18261. ((or (and org-support-shift-select
  18262. (not (eq org-support-shift-select 'always))
  18263. (org-at-item-bullet-p))
  18264. (and (not org-support-shift-select) (org-at-item-p)))
  18265. (org-call-with-arg 'org-cycle-list-bullet nil))
  18266. ((and (not (eq org-support-shift-select 'always))
  18267. (org-at-property-p))
  18268. (call-interactively 'org-property-next-allowed-value))
  18269. ((org-clocktable-try-shift 'right arg))
  18270. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18271. (org-support-shift-select
  18272. (org-call-for-shift-select 'forward-char))
  18273. (t (org-shiftselect-error))))
  18274. (defun org-shiftleft (&optional arg)
  18275. "Cycle the thing at point or in the current line, depending on context.
  18276. Depending on context, this does one of the following:
  18277. - switch a timestamp at point one day into the past
  18278. - on a headline, switch to the previous TODO keyword.
  18279. - on an item, switch entire list to the previous bullet type
  18280. - on a property line, switch to the previous allowed value
  18281. - on a clocktable definition line, move time block into the past"
  18282. (interactive "P")
  18283. (cond
  18284. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18285. ((and org-support-shift-select (org-region-active-p))
  18286. (org-call-for-shift-select 'backward-char))
  18287. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18288. ((and (not (eq org-support-shift-select 'always))
  18289. (org-at-heading-p))
  18290. (let ((org-inhibit-logging
  18291. (not org-treat-S-cursor-todo-selection-as-state-change))
  18292. (org-inhibit-blocking
  18293. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18294. (org-call-with-arg 'org-todo 'left)))
  18295. ((or (and org-support-shift-select
  18296. (not (eq org-support-shift-select 'always))
  18297. (org-at-item-bullet-p))
  18298. (and (not org-support-shift-select) (org-at-item-p)))
  18299. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18300. ((and (not (eq org-support-shift-select 'always))
  18301. (org-at-property-p))
  18302. (call-interactively 'org-property-previous-allowed-value))
  18303. ((org-clocktable-try-shift 'left arg))
  18304. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18305. (org-support-shift-select
  18306. (org-call-for-shift-select 'backward-char))
  18307. (t (org-shiftselect-error))))
  18308. (defun org-shiftcontrolright ()
  18309. "Switch to next TODO set."
  18310. (interactive)
  18311. (cond
  18312. ((and org-support-shift-select (org-region-active-p))
  18313. (org-call-for-shift-select 'forward-word))
  18314. ((and (not (eq org-support-shift-select 'always))
  18315. (org-at-heading-p))
  18316. (org-call-with-arg 'org-todo 'nextset))
  18317. (org-support-shift-select
  18318. (org-call-for-shift-select 'forward-word))
  18319. (t (org-shiftselect-error))))
  18320. (defun org-shiftcontrolleft ()
  18321. "Switch to previous TODO set."
  18322. (interactive)
  18323. (cond
  18324. ((and org-support-shift-select (org-region-active-p))
  18325. (org-call-for-shift-select 'backward-word))
  18326. ((and (not (eq org-support-shift-select 'always))
  18327. (org-at-heading-p))
  18328. (org-call-with-arg 'org-todo 'previousset))
  18329. (org-support-shift-select
  18330. (org-call-for-shift-select 'backward-word))
  18331. (t (org-shiftselect-error))))
  18332. (defun org-shiftcontrolup (&optional n)
  18333. "Change timestamps synchronously up in CLOCK log lines.
  18334. Optional argument N tells to change by that many units."
  18335. (interactive "P")
  18336. (cond ((and (not org-support-shift-select)
  18337. (org-at-clock-log-p)
  18338. (org-at-timestamp-p t))
  18339. (org-clock-timestamps-up n))
  18340. (t (org-shiftselect-error))))
  18341. (defun org-shiftcontroldown (&optional n)
  18342. "Change timestamps synchronously down in CLOCK log lines.
  18343. Optional argument N tells to change by that many units."
  18344. (interactive "P")
  18345. (cond ((and (not org-support-shift-select)
  18346. (org-at-clock-log-p)
  18347. (org-at-timestamp-p t))
  18348. (org-clock-timestamps-down n))
  18349. (t (org-shiftselect-error))))
  18350. (defun org-ctrl-c-ret ()
  18351. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18352. (interactive)
  18353. (cond
  18354. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18355. (t (call-interactively 'org-insert-heading))))
  18356. (defun org-find-visible ()
  18357. (let ((s (point)))
  18358. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18359. (get-char-property s 'invisible)))
  18360. s))
  18361. (defun org-find-invisible ()
  18362. (let ((s (point)))
  18363. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18364. (not (get-char-property s 'invisible))))
  18365. s))
  18366. (defun org-copy-visible (beg end)
  18367. "Copy the visible parts of the region."
  18368. (interactive "r")
  18369. (let (snippets s)
  18370. (save-excursion
  18371. (save-restriction
  18372. (narrow-to-region beg end)
  18373. (setq s (goto-char (point-min)))
  18374. (while (not (= (point) (point-max)))
  18375. (goto-char (org-find-invisible))
  18376. (push (buffer-substring s (point)) snippets)
  18377. (setq s (goto-char (org-find-visible))))))
  18378. (kill-new (apply 'concat (nreverse snippets)))))
  18379. (defun org-copy-special ()
  18380. "Copy region in table or copy current subtree.
  18381. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18382. See the individual commands for more information."
  18383. (interactive)
  18384. (call-interactively
  18385. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18386. (defun org-cut-special ()
  18387. "Cut region in table or cut current subtree.
  18388. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18389. See the individual commands for more information."
  18390. (interactive)
  18391. (call-interactively
  18392. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18393. (defun org-paste-special (arg)
  18394. "Paste rectangular region into table, or past subtree relative to level.
  18395. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18396. See the individual commands for more information."
  18397. (interactive "P")
  18398. (if (org-at-table-p)
  18399. (org-table-paste-rectangle)
  18400. (org-paste-subtree arg)))
  18401. (defsubst org-in-fixed-width-region-p ()
  18402. "Is point in a fixed-width region?"
  18403. (save-match-data
  18404. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18405. (defun org-edit-special (&optional arg)
  18406. "Call a special editor for the element at point.
  18407. When at a table, call the formula editor with `org-table-edit-formulas'.
  18408. When in a source code block, call `org-edit-src-code'.
  18409. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18410. When at an #+INCLUDE keyword, visit the included file.
  18411. On a link, call `ffap' to visit the link at point.
  18412. Otherwise, return a user error."
  18413. (interactive "P")
  18414. (let ((element (org-element-at-point)))
  18415. (assert (not buffer-read-only) nil
  18416. "Buffer is read-only: %s" (buffer-name))
  18417. (case (org-element-type element)
  18418. (src-block
  18419. (if (not arg) (org-edit-src-code)
  18420. (let* ((info (org-babel-get-src-block-info))
  18421. (lang (nth 0 info))
  18422. (params (nth 2 info))
  18423. (session (cdr (assq :session params))))
  18424. (if (not session) (org-edit-src-code)
  18425. ;; At a src-block with a session and function called with
  18426. ;; an ARG: switch to the buffer related to the inferior
  18427. ;; process.
  18428. (switch-to-buffer
  18429. (funcall (intern (concat "org-babel-prep-session:" lang))
  18430. session params))))))
  18431. (keyword
  18432. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18433. (find-file
  18434. (org-remove-double-quotes
  18435. (car (org-split-string (org-element-property :value element)))))
  18436. (user-error "No special environment to edit here")))
  18437. (table
  18438. (if (eq (org-element-property :type element) 'table.el)
  18439. (org-edit-src-code)
  18440. (call-interactively 'org-table-edit-formulas)))
  18441. ;; Only Org tables contain `table-row' type elements.
  18442. (table-row (call-interactively 'org-table-edit-formulas))
  18443. ((example-block export-block) (org-edit-src-code))
  18444. (fixed-width (org-edit-fixed-width-region))
  18445. (otherwise
  18446. ;; No notable element at point. Though, we may be at a link,
  18447. ;; which is an object. Thus, scan deeper.
  18448. (if (eq (org-element-type (org-element-context element)) 'link)
  18449. (call-interactively 'ffap)
  18450. (user-error "No special environment to edit here"))))))
  18451. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18452. (defun org-ctrl-c-ctrl-c (&optional arg)
  18453. "Set tags in headline, or update according to changed information at point.
  18454. This command does many different things, depending on context:
  18455. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18456. this is what we do.
  18457. - If the cursor is on a statistics cookie, update it.
  18458. - If the cursor is in a headline, prompt for tags and insert them
  18459. into the current line, aligned to `org-tags-column'. When called
  18460. with prefix arg, realign all tags in the current buffer.
  18461. - If the cursor is in one of the special #+KEYWORD lines, this
  18462. triggers scanning the buffer for these lines and updating the
  18463. information.
  18464. - If the cursor is inside a table, realign the table. This command
  18465. works even if the automatic table editor has been turned off.
  18466. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18467. the entire table.
  18468. - If the cursor is at a footnote reference or definition, jump to
  18469. the corresponding definition or references, respectively.
  18470. - If the cursor is a the beginning of a dynamic block, update it.
  18471. - If the current buffer is a capture buffer, close note and file it.
  18472. - If the cursor is on a <<<target>>>, update radio targets and
  18473. corresponding links in this buffer.
  18474. - If the cursor is on a numbered item in a plain list, renumber the
  18475. ordered list.
  18476. - If the cursor is on a checkbox, toggle it.
  18477. - If the cursor is on a code block, evaluate it. The variable
  18478. `org-confirm-babel-evaluate' can be used to control prompting
  18479. before code block evaluation, by default every code block
  18480. evaluation requires confirmation. Code block evaluation can be
  18481. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18482. (interactive "P")
  18483. (cond
  18484. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18485. org-occur-highlights
  18486. org-latex-fragment-image-overlays)
  18487. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18488. (org-remove-occur-highlights)
  18489. (org-remove-latex-fragment-image-overlays)
  18490. (message "Temporary highlights/overlays removed from current buffer"))
  18491. ((and (local-variable-p 'org-finish-function (current-buffer))
  18492. (fboundp org-finish-function))
  18493. (funcall org-finish-function))
  18494. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18495. (t
  18496. (let* ((context (org-element-context)) (type (org-element-type context)))
  18497. ;; Test if point is within a blank line.
  18498. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18499. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18500. (user-error "C-c C-c can do nothing useful at this location"))
  18501. ;; For convenience: at the first line of a paragraph on the
  18502. ;; same line as an item, apply function on that item instead.
  18503. (when (eq type 'paragraph)
  18504. (let ((parent (org-element-property :parent context)))
  18505. (when (and (eq (org-element-type parent) 'item)
  18506. (= (point-at-bol) (org-element-property :begin parent)))
  18507. (setq context parent type 'item))))
  18508. ;; Act according to type of element or object at point.
  18509. (case type
  18510. (clock (org-clock-update-time-maybe))
  18511. (dynamic-block
  18512. (save-excursion
  18513. (goto-char (org-element-property :post-affiliated context))
  18514. (org-update-dblock)))
  18515. (footnote-definition
  18516. (goto-char (org-element-property :post-affiliated context))
  18517. (call-interactively 'org-footnote-action))
  18518. (footnote-reference (call-interactively 'org-footnote-action))
  18519. ((headline inlinetask)
  18520. (save-excursion (goto-char (org-element-property :begin context))
  18521. (call-interactively 'org-set-tags)))
  18522. (item
  18523. ;; At an item: a double C-u set checkbox to "[-]"
  18524. ;; unconditionally, whereas a single one will toggle its
  18525. ;; presence. Without an universal argument, if the item
  18526. ;; has a checkbox, toggle it. Otherwise repair the list.
  18527. (let* ((box (org-element-property :checkbox context))
  18528. (struct (org-element-property :structure context))
  18529. (old-struct (copy-tree struct))
  18530. (parents (org-list-parents-alist struct))
  18531. (prevs (org-list-prevs-alist struct))
  18532. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18533. (org-list-set-checkbox
  18534. (org-element-property :begin context) struct
  18535. (cond ((equal arg '(16)) "[-]")
  18536. ((and (not box) (equal arg '(4))) "[ ]")
  18537. ((or (not box) (equal arg '(4))) nil)
  18538. ((eq box 'on) "[ ]")
  18539. (t "[X]")))
  18540. ;; Mimic `org-list-write-struct' but with grabbing
  18541. ;; a return value from `org-list-struct-fix-box'.
  18542. (org-list-struct-fix-ind struct parents 2)
  18543. (org-list-struct-fix-item-end struct)
  18544. (org-list-struct-fix-bul struct prevs)
  18545. (org-list-struct-fix-ind struct parents)
  18546. (let ((block-item
  18547. (org-list-struct-fix-box struct parents prevs orderedp)))
  18548. (if (and box (equal struct old-struct))
  18549. (if (equal arg '(16))
  18550. (message "Checkboxes already reset")
  18551. (user-error "Cannot toggle this checkbox: %s"
  18552. (if (eq box 'on)
  18553. "all subitems checked"
  18554. "unchecked subitems")))
  18555. (org-list-struct-apply-struct struct old-struct)
  18556. (org-update-checkbox-count-maybe))
  18557. (when block-item
  18558. (message "Checkboxes were removed due to empty box at line %d"
  18559. (org-current-line block-item))))))
  18560. (keyword
  18561. (let ((org-inhibit-startup-visibility-stuff t)
  18562. (org-startup-align-all-tables nil))
  18563. (when (boundp 'org-table-coordinate-overlays)
  18564. (mapc 'delete-overlay org-table-coordinate-overlays)
  18565. (setq org-table-coordinate-overlays nil))
  18566. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18567. (message "Local setup has been refreshed"))
  18568. (plain-list
  18569. ;; At a plain list, with a double C-u argument, set
  18570. ;; checkboxes of each item to "[-]", whereas a single one
  18571. ;; will toggle their presence according to the state of the
  18572. ;; first item in the list. Without an argument, repair the
  18573. ;; list.
  18574. (let* ((begin (org-element-property :contents-begin context))
  18575. (beginm (move-marker (make-marker) begin))
  18576. (struct (org-element-property :structure context))
  18577. (old-struct (copy-tree struct))
  18578. (first-box (save-excursion
  18579. (goto-char begin)
  18580. (looking-at org-list-full-item-re)
  18581. (match-string-no-properties 3)))
  18582. (new-box (cond ((equal arg '(16)) "[-]")
  18583. ((equal arg '(4)) (unless first-box "[ ]"))
  18584. ((equal first-box "[X]") "[ ]")
  18585. (t "[X]"))))
  18586. (cond
  18587. (arg
  18588. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18589. (org-list-get-all-items
  18590. begin struct (org-list-prevs-alist struct))))
  18591. ((and first-box (eq (point) begin))
  18592. ;; For convenience, when point is at bol on the first
  18593. ;; item of the list and no argument is provided, simply
  18594. ;; toggle checkbox of that item, if any.
  18595. (org-list-set-checkbox begin struct new-box)))
  18596. (org-list-write-struct
  18597. struct (org-list-parents-alist struct) old-struct)
  18598. (org-update-checkbox-count-maybe)
  18599. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18600. ((property-drawer node-property)
  18601. (call-interactively 'org-property-action))
  18602. ((radio-target target)
  18603. (call-interactively 'org-update-radio-target-regexp))
  18604. (statistics-cookie
  18605. (call-interactively 'org-update-statistics-cookies))
  18606. ((table table-cell table-row)
  18607. ;; At a table, recalculate every field and align it. Also
  18608. ;; send the table if necessary. If the table has
  18609. ;; a `table.el' type, just give up. At a table row or
  18610. ;; cell, maybe recalculate line but always align table.
  18611. (if (eq (org-element-property :type context) 'table.el)
  18612. (message "Use C-c ' to edit table.el tables")
  18613. (let ((org-enable-table-editor t))
  18614. (if (or (eq type 'table)
  18615. ;; Check if point is at a TBLFM line.
  18616. (and (eq type 'table-row)
  18617. (= (point) (org-element-property :end context))))
  18618. (save-excursion
  18619. (if (org-at-TBLFM-p)
  18620. (progn (require 'org-table)
  18621. (org-table-calc-current-TBLFM))
  18622. (goto-char (org-element-property :contents-begin context))
  18623. (org-call-with-arg 'org-table-recalculate (or arg t))
  18624. (orgtbl-send-table 'maybe)))
  18625. (org-table-maybe-eval-formula)
  18626. (cond (arg (call-interactively 'org-table-recalculate))
  18627. ((org-table-maybe-recalculate-line))
  18628. (t (org-table-align)))))))
  18629. (timestamp (org-timestamp-change 0 'day))
  18630. (otherwise
  18631. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18632. (user-error
  18633. "C-c C-c can do nothing useful at this location")))))))))
  18634. (defun org-mode-restart ()
  18635. "Restart Org-mode, to scan again for special lines.
  18636. Also updates the keyword regular expressions."
  18637. (interactive)
  18638. (org-mode)
  18639. (message "Org-mode restarted"))
  18640. (defun org-kill-note-or-show-branches ()
  18641. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18642. (interactive)
  18643. (if (not org-finish-function)
  18644. (progn
  18645. (hide-subtree)
  18646. (call-interactively 'show-branches))
  18647. (let ((org-note-abort t))
  18648. (funcall org-finish-function))))
  18649. (defun org-open-line (n)
  18650. "Insert a new row in tables, call `open-line' elsewhere."
  18651. (interactive "*p")
  18652. (if (org-at-table-p)
  18653. (org-table-insert-row)
  18654. (open-line n)))
  18655. (defun org-return (&optional indent)
  18656. "Goto next table row or insert a newline.
  18657. Calls `org-table-next-row' or `newline', depending on context.
  18658. See the individual commands for more information."
  18659. (interactive)
  18660. (let (org-ts-what)
  18661. (cond
  18662. ((or (bobp) (org-in-src-block-p))
  18663. (if indent (newline-and-indent) (newline)))
  18664. ((org-at-table-p)
  18665. (org-table-justify-field-maybe)
  18666. (call-interactively 'org-table-next-row))
  18667. ;; when `newline-and-indent' is called within a list, make sure
  18668. ;; text moved stays inside the item.
  18669. ((and (org-in-item-p) indent)
  18670. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18671. (progn
  18672. (save-match-data (newline))
  18673. (org-indent-line-to (length (match-string 0))))
  18674. (let ((ind (org-get-indentation)))
  18675. (newline)
  18676. (if (org-looking-back org-list-end-re)
  18677. (org-indent-line)
  18678. (org-indent-line-to ind)))))
  18679. ((and org-return-follows-link
  18680. (org-at-timestamp-p t)
  18681. (not (eq org-ts-what 'after)))
  18682. (org-follow-timestamp-link))
  18683. ((and org-return-follows-link
  18684. (let ((tprop (get-text-property (point) 'face)))
  18685. (or (eq tprop 'org-link)
  18686. (and (listp tprop) (memq 'org-link tprop)))))
  18687. (call-interactively 'org-open-at-point))
  18688. ((and (org-at-heading-p)
  18689. (looking-at
  18690. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18691. (org-show-entry)
  18692. (end-of-line 1)
  18693. (newline))
  18694. (t (if indent (newline-and-indent) (newline))))))
  18695. (defun org-return-indent ()
  18696. "Goto next table row or insert a newline and indent.
  18697. Calls `org-table-next-row' or `newline-and-indent', depending on
  18698. context. See the individual commands for more information."
  18699. (interactive)
  18700. (org-return t))
  18701. (defun org-ctrl-c-star ()
  18702. "Compute table, or change heading status of lines.
  18703. Calls `org-table-recalculate' or `org-toggle-heading',
  18704. depending on context."
  18705. (interactive)
  18706. (cond
  18707. ((org-at-table-p)
  18708. (call-interactively 'org-table-recalculate))
  18709. (t
  18710. ;; Convert all lines in region to list items
  18711. (call-interactively 'org-toggle-heading))))
  18712. (defun org-ctrl-c-minus ()
  18713. "Insert separator line in table or modify bullet status of line.
  18714. Also turns a plain line or a region of lines into list items.
  18715. Calls `org-table-insert-hline', `org-toggle-item', or
  18716. `org-cycle-list-bullet', depending on context."
  18717. (interactive)
  18718. (cond
  18719. ((org-at-table-p)
  18720. (call-interactively 'org-table-insert-hline))
  18721. ((org-region-active-p)
  18722. (call-interactively 'org-toggle-item))
  18723. ((org-in-item-p)
  18724. (call-interactively 'org-cycle-list-bullet))
  18725. (t
  18726. (call-interactively 'org-toggle-item))))
  18727. (defun org-toggle-item (arg)
  18728. "Convert headings or normal lines to items, items to normal lines.
  18729. If there is no active region, only the current line is considered.
  18730. If the first non blank line in the region is a headline, convert
  18731. all headlines to items, shifting text accordingly.
  18732. If it is an item, convert all items to normal lines.
  18733. If it is normal text, change region into a list of items.
  18734. With a prefix argument ARG, change the region in a single item."
  18735. (interactive "P")
  18736. (let ((shift-text
  18737. (function
  18738. ;; Shift text in current section to IND, from point to END.
  18739. ;; The function leaves point to END line.
  18740. (lambda (ind end)
  18741. (let ((min-i 1000) (end (copy-marker end)))
  18742. ;; First determine the minimum indentation (MIN-I) of
  18743. ;; the text.
  18744. (save-excursion
  18745. (catch 'exit
  18746. (while (< (point) end)
  18747. (let ((i (org-get-indentation)))
  18748. (cond
  18749. ;; Skip blank lines and inline tasks.
  18750. ((looking-at "^[ \t]*$"))
  18751. ((looking-at org-outline-regexp-bol))
  18752. ;; We can't find less than 0 indentation.
  18753. ((zerop i) (throw 'exit (setq min-i 0)))
  18754. ((< i min-i) (setq min-i i))))
  18755. (forward-line))))
  18756. ;; Then indent each line so that a line indented to
  18757. ;; MIN-I becomes indented to IND. Ignore blank lines
  18758. ;; and inline tasks in the process.
  18759. (let ((delta (- ind min-i)))
  18760. (while (< (point) end)
  18761. (unless (or (looking-at "^[ \t]*$")
  18762. (looking-at org-outline-regexp-bol))
  18763. (org-indent-line-to (+ (org-get-indentation) delta)))
  18764. (forward-line)))))))
  18765. (skip-blanks
  18766. (function
  18767. ;; Return beginning of first non-blank line, starting from
  18768. ;; line at POS.
  18769. (lambda (pos)
  18770. (save-excursion
  18771. (goto-char pos)
  18772. (skip-chars-forward " \r\t\n")
  18773. (point-at-bol)))))
  18774. beg end)
  18775. ;; Determine boundaries of changes.
  18776. (if (org-region-active-p)
  18777. (setq beg (funcall skip-blanks (region-beginning))
  18778. end (copy-marker (region-end)))
  18779. (setq beg (funcall skip-blanks (point-at-bol))
  18780. end (copy-marker (point-at-eol))))
  18781. ;; Depending on the starting line, choose an action on the text
  18782. ;; between BEG and END.
  18783. (org-with-limited-levels
  18784. (save-excursion
  18785. (goto-char beg)
  18786. (cond
  18787. ;; Case 1. Start at an item: de-itemize. Note that it only
  18788. ;; happens when a region is active: `org-ctrl-c-minus'
  18789. ;; would call `org-cycle-list-bullet' otherwise.
  18790. ((org-at-item-p)
  18791. (while (< (point) end)
  18792. (when (org-at-item-p)
  18793. (skip-chars-forward " \t")
  18794. (delete-region (point) (match-end 0)))
  18795. (forward-line)))
  18796. ;; Case 2. Start at an heading: convert to items.
  18797. ((org-at-heading-p)
  18798. (let* ((bul (org-list-bullet-string "-"))
  18799. (bul-len (length bul))
  18800. ;; Indentation of the first heading. It should be
  18801. ;; relative to the indentation of its parent, if any.
  18802. (start-ind (save-excursion
  18803. (cond
  18804. ((not org-adapt-indentation) 0)
  18805. ((not (outline-previous-heading)) 0)
  18806. (t (length (match-string 0))))))
  18807. ;; Level of first heading. Further headings will be
  18808. ;; compared to it to determine hierarchy in the list.
  18809. (ref-level (org-reduced-level (org-outline-level))))
  18810. (while (< (point) end)
  18811. (let* ((level (org-reduced-level (org-outline-level)))
  18812. (delta (max 0 (- level ref-level))))
  18813. ;; If current headline is less indented than the first
  18814. ;; one, set it as reference, in order to preserve
  18815. ;; subtrees.
  18816. (when (< level ref-level) (setq ref-level level))
  18817. (replace-match bul t t)
  18818. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18819. ;; Ensure all text down to END (or SECTION-END) belongs
  18820. ;; to the newly created item.
  18821. (let ((section-end (save-excursion
  18822. (or (outline-next-heading) (point)))))
  18823. (forward-line)
  18824. (funcall shift-text
  18825. (+ start-ind (* (1+ delta) bul-len))
  18826. (min end section-end)))))))
  18827. ;; Case 3. Normal line with ARG: make the first line of region
  18828. ;; an item, and shift indentation of others lines to
  18829. ;; set them as item's body.
  18830. (arg (let* ((bul (org-list-bullet-string "-"))
  18831. (bul-len (length bul))
  18832. (ref-ind (org-get-indentation)))
  18833. (skip-chars-forward " \t")
  18834. (insert bul)
  18835. (forward-line)
  18836. (while (< (point) end)
  18837. ;; Ensure that lines less indented than first one
  18838. ;; still get included in item body.
  18839. (funcall shift-text
  18840. (+ ref-ind bul-len)
  18841. (min end (save-excursion (or (outline-next-heading)
  18842. (point)))))
  18843. (forward-line))))
  18844. ;; Case 4. Normal line without ARG: turn each non-item line
  18845. ;; into an item.
  18846. (t
  18847. (while (< (point) end)
  18848. (unless (or (org-at-heading-p) (org-at-item-p))
  18849. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18850. (replace-match
  18851. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18852. (forward-line))))))))
  18853. (defun org-toggle-heading (&optional nstars)
  18854. "Convert headings to normal text, or items or text to headings.
  18855. If there is no active region, only convert the current line.
  18856. With a \\[universal-argument] prefix, convert the whole list at
  18857. point into heading.
  18858. In a region:
  18859. - If the first non blank line is a headline, remove the stars
  18860. from all headlines in the region.
  18861. - If it is a normal line, turn each and every normal line (i.e.,
  18862. not an heading or an item) in the region into headings. If you
  18863. want to convert only the first line of this region, use one
  18864. universal prefix argument.
  18865. - If it is a plain list item, turn all plain list items into headings.
  18866. When converting a line into a heading, the number of stars is chosen
  18867. such that the lines become children of the current entry. However,
  18868. when a numeric prefix argument is given, its value determines the
  18869. number of stars to add."
  18870. (interactive "P")
  18871. (let ((skip-blanks
  18872. (function
  18873. ;; Return beginning of first non-blank line, starting from
  18874. ;; line at POS.
  18875. (lambda (pos)
  18876. (save-excursion
  18877. (goto-char pos)
  18878. (while (org-at-comment-p) (forward-line))
  18879. (skip-chars-forward " \r\t\n")
  18880. (point-at-bol)))))
  18881. beg end toggled)
  18882. ;; Determine boundaries of changes. If a universal prefix has
  18883. ;; been given, put the list in a region. If region ends at a bol,
  18884. ;; do not consider the last line to be in the region.
  18885. (when (and current-prefix-arg (org-at-item-p))
  18886. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18887. (org-mark-element))
  18888. (if (org-region-active-p)
  18889. (setq beg (funcall skip-blanks (region-beginning))
  18890. end (copy-marker (save-excursion
  18891. (goto-char (region-end))
  18892. (if (bolp) (point) (point-at-eol)))))
  18893. (setq beg (funcall skip-blanks (point-at-bol))
  18894. end (copy-marker (point-at-eol))))
  18895. ;; Ensure inline tasks don't count as headings.
  18896. (org-with-limited-levels
  18897. (save-excursion
  18898. (goto-char beg)
  18899. (cond
  18900. ;; Case 1. Started at an heading: de-star headings.
  18901. ((org-at-heading-p)
  18902. (while (< (point) end)
  18903. (when (org-at-heading-p t)
  18904. (looking-at org-outline-regexp) (replace-match "")
  18905. (setq toggled t))
  18906. (forward-line)))
  18907. ;; Case 2. Started at an item: change items into headlines.
  18908. ;; One star will be added by `org-list-to-subtree'.
  18909. ((org-at-item-p)
  18910. (let* ((stars (make-string
  18911. ;; subtract the star that will be added again by
  18912. ;; `org-list-to-subtree'
  18913. (if (numberp nstars) (1- nstars)
  18914. (or (org-current-level) 0))
  18915. ?*))
  18916. (add-stars
  18917. (cond (nstars "") ; stars from prefix only
  18918. ((equal stars "") "") ; before first heading
  18919. (org-odd-levels-only "*") ; inside heading, odd
  18920. (t "")))) ; inside heading, oddeven
  18921. (while (< (point) end)
  18922. (when (org-at-item-p)
  18923. ;; Pay attention to cases when region ends before list.
  18924. (let* ((struct (org-list-struct))
  18925. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18926. (save-restriction
  18927. (narrow-to-region (point) list-end)
  18928. (insert
  18929. (org-list-to-subtree
  18930. (org-list-parse-list t)
  18931. '(:istart (concat stars add-stars (funcall get-stars depth))
  18932. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18933. (setq toggled t))
  18934. (forward-line))))
  18935. ;; Case 3. Started at normal text: make every line an heading,
  18936. ;; skipping headlines and items.
  18937. (t (let* ((stars
  18938. (make-string
  18939. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18940. (add-stars
  18941. (cond (nstars "") ; stars from prefix only
  18942. ((equal stars "") "*") ; before first heading
  18943. (org-odd-levels-only "**") ; inside heading, odd
  18944. (t "*"))) ; inside heading, oddeven
  18945. (rpl (concat stars add-stars " "))
  18946. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18947. (while (< (point) (if (equal nstars '(4)) lend end))
  18948. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18949. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18950. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18951. (forward-line)))))))
  18952. (unless toggled (message "Cannot toggle heading from here"))))
  18953. (defun org-meta-return (&optional arg)
  18954. "Insert a new heading or wrap a region in a table.
  18955. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18956. See the individual commands for more information."
  18957. (interactive "P")
  18958. (org-check-before-invisible-edit 'insert)
  18959. (cond
  18960. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18961. ((or (org-at-drawer-p) (org-in-drawer-p) (org-at-property-p))
  18962. (newline-and-indent))
  18963. ((org-at-table-p)
  18964. (call-interactively 'org-table-wrap-region))
  18965. (t (call-interactively 'org-insert-heading))))
  18966. ;;; Menu entries
  18967. (defsubst org-in-subtree-not-table-p ()
  18968. "Are we in a subtree and not in a table?"
  18969. (and (not (org-before-first-heading-p))
  18970. (not (org-at-table-p))))
  18971. ;; Define the Org-mode menus
  18972. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18973. '("Tbl"
  18974. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18975. ["Next Field" org-cycle (org-at-table-p)]
  18976. ["Previous Field" org-shifttab (org-at-table-p)]
  18977. ["Next Row" org-return (org-at-table-p)]
  18978. "--"
  18979. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18980. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18981. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18982. "--"
  18983. ("Column"
  18984. ["Move Column Left" org-metaleft (org-at-table-p)]
  18985. ["Move Column Right" org-metaright (org-at-table-p)]
  18986. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18987. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18988. ("Row"
  18989. ["Move Row Up" org-metaup (org-at-table-p)]
  18990. ["Move Row Down" org-metadown (org-at-table-p)]
  18991. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18992. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18993. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18994. "--"
  18995. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18996. ("Rectangle"
  18997. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18998. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18999. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19000. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19001. "--"
  19002. ("Calculate"
  19003. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19004. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19005. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19006. "--"
  19007. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19008. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19009. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19010. "--"
  19011. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19012. "--"
  19013. ["Sum Column/Rectangle" org-table-sum
  19014. (or (org-at-table-p) (org-region-active-p))]
  19015. ["Which Column?" org-table-current-column (org-at-table-p)])
  19016. ["Debug Formulas"
  19017. org-table-toggle-formula-debugger
  19018. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19019. ["Show Col/Row Numbers"
  19020. org-table-toggle-coordinate-overlays
  19021. :style toggle
  19022. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19023. "--"
  19024. ["Create" org-table-create (and (not (org-at-table-p))
  19025. org-enable-table-editor)]
  19026. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19027. ["Import from File" org-table-import (not (org-at-table-p))]
  19028. ["Export to File" org-table-export (org-at-table-p)]
  19029. "--"
  19030. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19031. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19032. '("Org"
  19033. ("Show/Hide"
  19034. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19035. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19036. ["Sparse Tree..." org-sparse-tree t]
  19037. ["Reveal Context" org-reveal t]
  19038. ["Show All" show-all t]
  19039. "--"
  19040. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19041. "--"
  19042. ["New Heading" org-insert-heading t]
  19043. ("Navigate Headings"
  19044. ["Up" outline-up-heading t]
  19045. ["Next" outline-next-visible-heading t]
  19046. ["Previous" outline-previous-visible-heading t]
  19047. ["Next Same Level" outline-forward-same-level t]
  19048. ["Previous Same Level" outline-backward-same-level t]
  19049. "--"
  19050. ["Jump" org-goto t])
  19051. ("Edit Structure"
  19052. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19053. "--"
  19054. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19055. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19056. "--"
  19057. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19058. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19059. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19060. "--"
  19061. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19062. "--"
  19063. ["Copy visible text" org-copy-visible t]
  19064. "--"
  19065. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19066. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19067. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19068. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19069. "--"
  19070. ["Sort Region/Children" org-sort t]
  19071. "--"
  19072. ["Convert to odd levels" org-convert-to-odd-levels t]
  19073. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19074. ("Editing"
  19075. ["Emphasis..." org-emphasize t]
  19076. ["Edit Source Example" org-edit-special t]
  19077. "--"
  19078. ["Footnote new/jump" org-footnote-action t]
  19079. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19080. ("Archive"
  19081. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19082. "--"
  19083. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19084. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19085. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19086. )
  19087. "--"
  19088. ("Hyperlinks"
  19089. ["Store Link (Global)" org-store-link t]
  19090. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19091. ["Insert Link" org-insert-link t]
  19092. ["Follow Link" org-open-at-point t]
  19093. "--"
  19094. ["Next link" org-next-link t]
  19095. ["Previous link" org-previous-link t]
  19096. "--"
  19097. ["Descriptive Links"
  19098. org-toggle-link-display
  19099. :style radio
  19100. :selected org-descriptive-links
  19101. ]
  19102. ["Literal Links"
  19103. org-toggle-link-display
  19104. :style radio
  19105. :selected (not org-descriptive-links)])
  19106. "--"
  19107. ("TODO Lists"
  19108. ["TODO/DONE/-" org-todo t]
  19109. ("Select keyword"
  19110. ["Next keyword" org-shiftright (org-at-heading-p)]
  19111. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19112. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19113. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19114. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19115. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19116. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19117. "--"
  19118. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19119. :selected org-enforce-todo-dependencies :style toggle :active t]
  19120. "Settings for tree at point"
  19121. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19122. :selected (org-entry-get nil "ORDERED")
  19123. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19124. ["Do Children parallel" org-toggle-ordered-property :style radio
  19125. :selected (not (org-entry-get nil "ORDERED"))
  19126. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19127. "--"
  19128. ["Set Priority" org-priority t]
  19129. ["Priority Up" org-shiftup t]
  19130. ["Priority Down" org-shiftdown t]
  19131. "--"
  19132. ["Get news from all feeds" org-feed-update-all t]
  19133. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19134. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19135. ("TAGS and Properties"
  19136. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19137. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19138. "--"
  19139. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19140. ["Column view of properties" org-columns t]
  19141. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19142. ("Dates and Scheduling"
  19143. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19144. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19145. ("Change Date"
  19146. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19147. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19148. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19149. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19150. ["Compute Time Range" org-evaluate-time-range t]
  19151. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19152. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19153. "--"
  19154. ["Custom time format" org-toggle-time-stamp-overlays
  19155. :style radio :selected org-display-custom-times]
  19156. "--"
  19157. ["Goto Calendar" org-goto-calendar t]
  19158. ["Date from Calendar" org-date-from-calendar t]
  19159. "--"
  19160. ["Start/Restart Timer" org-timer-start t]
  19161. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19162. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19163. ["Insert Timer String" org-timer t]
  19164. ["Insert Timer Item" org-timer-item t])
  19165. ("Logging work"
  19166. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19167. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19168. ["Clock out" org-clock-out t]
  19169. ["Clock cancel" org-clock-cancel t]
  19170. "--"
  19171. ["Mark as default task" org-clock-mark-default-task t]
  19172. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19173. ["Goto running clock" org-clock-goto t]
  19174. "--"
  19175. ["Display times" org-clock-display t]
  19176. ["Create clock table" org-clock-report t]
  19177. "--"
  19178. ["Record DONE time"
  19179. (progn (setq org-log-done (not org-log-done))
  19180. (message "Switching to %s will %s record a timestamp"
  19181. (car org-done-keywords)
  19182. (if org-log-done "automatically" "not")))
  19183. :style toggle :selected org-log-done])
  19184. "--"
  19185. ["Agenda Command..." org-agenda t]
  19186. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19187. ("File List for Agenda")
  19188. ("Special views current file"
  19189. ["TODO Tree" org-show-todo-tree t]
  19190. ["Check Deadlines" org-check-deadlines t]
  19191. ["Timeline" org-timeline t]
  19192. ["Tags/Property tree" org-match-sparse-tree t])
  19193. "--"
  19194. ["Export/Publish..." org-export-dispatch t]
  19195. ("LaTeX"
  19196. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19197. :selected org-cdlatex-mode]
  19198. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19199. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19200. ["Modify math symbol" org-cdlatex-math-modify
  19201. (org-inside-LaTeX-fragment-p)]
  19202. ["Insert citation" org-reftex-citation t]
  19203. "--"
  19204. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19205. "--"
  19206. ("MobileOrg"
  19207. ["Push Files and Views" org-mobile-push t]
  19208. ["Get Captured and Flagged" org-mobile-pull t]
  19209. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19210. "--"
  19211. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19212. "--"
  19213. ("Documentation"
  19214. ["Show Version" org-version t]
  19215. ["Info Documentation" org-info t])
  19216. ("Customize"
  19217. ["Browse Org Group" org-customize t]
  19218. "--"
  19219. ["Expand This Menu" org-create-customize-menu
  19220. (fboundp 'customize-menu-create)])
  19221. ["Send bug report" org-submit-bug-report t]
  19222. "--"
  19223. ("Refresh/Reload"
  19224. ["Refresh setup current buffer" org-mode-restart t]
  19225. ["Reload Org (after update)" org-reload t]
  19226. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19227. ))
  19228. (defun org-info (&optional node)
  19229. "Read documentation for Org-mode in the info system.
  19230. With optional NODE, go directly to that node."
  19231. (interactive)
  19232. (info (format "(org)%s" (or node ""))))
  19233. ;;;###autoload
  19234. (defun org-submit-bug-report ()
  19235. "Submit a bug report on Org-mode via mail.
  19236. Don't hesitate to report any problems or inaccurate documentation.
  19237. If you don't have setup sending mail from (X)Emacs, please copy the
  19238. output buffer into your mail program, as it gives us important
  19239. information about your Org-mode version and configuration."
  19240. (interactive)
  19241. (require 'reporter)
  19242. (org-load-modules-maybe)
  19243. (org-require-autoloaded-modules)
  19244. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19245. (reporter-submit-bug-report
  19246. "emacs-orgmode@gnu.org"
  19247. (org-version nil 'full)
  19248. (let (list)
  19249. (save-window-excursion
  19250. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19251. (delete-other-windows)
  19252. (erase-buffer)
  19253. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19254. We would like to add your full Org-mode and Outline configuration to the
  19255. bug report. This greatly simplifies the work of the maintainer and
  19256. other experts on the mailing list.
  19257. HOWEVER, some variables you have customized may contain private
  19258. information. The names of customers, colleagues, or friends, might
  19259. appear in the form of file names, tags, todo states, or search strings.
  19260. If you answer yes to the prompt, you might want to check and remove
  19261. such private information before sending the email.")
  19262. (add-text-properties (point-min) (point-max) '(face org-warning))
  19263. (when (yes-or-no-p "Include your Org-mode configuration ")
  19264. (mapatoms
  19265. (lambda (v)
  19266. (and (boundp v)
  19267. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19268. (or (and (symbol-value v)
  19269. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19270. (and
  19271. (get v 'custom-type) (get v 'standard-value)
  19272. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19273. (push v list)))))
  19274. (kill-buffer (get-buffer "*Warn about privacy*"))
  19275. list))
  19276. nil nil
  19277. "Remember to cover the basics, that is, what you expected to happen and
  19278. what in fact did happen. You don't know how to make a good report? See
  19279. http://orgmode.org/manual/Feedback.html#Feedback
  19280. Your bug report will be posted to the Org-mode mailing list.
  19281. ------------------------------------------------------------------------")
  19282. (save-excursion
  19283. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19284. (replace-match "\\1Bug: \\3 [\\2]")))))
  19285. (defun org-install-agenda-files-menu ()
  19286. (let ((bl (buffer-list)))
  19287. (save-excursion
  19288. (while bl
  19289. (set-buffer (pop bl))
  19290. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19291. (when (derived-mode-p 'org-mode)
  19292. (easy-menu-change
  19293. '("Org") "File List for Agenda"
  19294. (append
  19295. (list
  19296. ["Edit File List" (org-edit-agenda-file-list) t]
  19297. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19298. ["Remove Current File from List" org-remove-file t]
  19299. ["Cycle through agenda files" org-cycle-agenda-files t]
  19300. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19301. "--")
  19302. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19303. ;;;; Documentation
  19304. (defun org-require-autoloaded-modules ()
  19305. (interactive)
  19306. (mapc 'require
  19307. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19308. org-remember org-table org-timer)))
  19309. ;;;###autoload
  19310. (defun org-reload (&optional uncompiled)
  19311. "Reload all org lisp files.
  19312. With prefix arg UNCOMPILED, load the uncompiled versions."
  19313. (interactive "P")
  19314. (require 'loadhist)
  19315. (let* ((org-dir (org-find-library-dir "org"))
  19316. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19317. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19318. (remove-re (mapconcat 'identity
  19319. (mapcar (lambda (f) (concat "^" f "$"))
  19320. (list (if (featurep 'xemacs)
  19321. "org-colview"
  19322. "org-colview-xemacs")
  19323. "org" "org-loaddefs" "org-version"))
  19324. "\\|"))
  19325. (feats (delete-dups
  19326. (mapcar 'file-name-sans-extension
  19327. (mapcar 'file-name-nondirectory
  19328. (delq nil
  19329. (mapcar 'feature-file
  19330. features))))))
  19331. (lfeat (append
  19332. (sort
  19333. (setq feats
  19334. (delq nil (mapcar
  19335. (lambda (f)
  19336. (if (and (string-match feature-re f)
  19337. (not (string-match remove-re f)))
  19338. f nil))
  19339. feats)))
  19340. 'string-lessp)
  19341. (list "org-version" "org")))
  19342. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19343. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19344. load-uncore load-misses)
  19345. (setq load-misses
  19346. (delq 't
  19347. (mapcar (lambda (f)
  19348. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19349. (and (string= org-dir contrib-dir)
  19350. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19351. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19352. (add-to-list 'load-uncore f 'append)
  19353. 't)
  19354. f))
  19355. lfeat)))
  19356. (if load-uncore
  19357. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19358. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19359. (if load-misses
  19360. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19361. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19362. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19363. ;;;###autoload
  19364. (defun org-customize ()
  19365. "Call the customize function with org as argument."
  19366. (interactive)
  19367. (org-load-modules-maybe)
  19368. (org-require-autoloaded-modules)
  19369. (customize-browse 'org))
  19370. (defun org-create-customize-menu ()
  19371. "Create a full customization menu for Org-mode, insert it into the menu."
  19372. (interactive)
  19373. (org-load-modules-maybe)
  19374. (org-require-autoloaded-modules)
  19375. (if (fboundp 'customize-menu-create)
  19376. (progn
  19377. (easy-menu-change
  19378. '("Org") "Customize"
  19379. `(["Browse Org group" org-customize t]
  19380. "--"
  19381. ,(customize-menu-create 'org)
  19382. ["Set" Custom-set t]
  19383. ["Save" Custom-save t]
  19384. ["Reset to Current" Custom-reset-current t]
  19385. ["Reset to Saved" Custom-reset-saved t]
  19386. ["Reset to Standard Settings" Custom-reset-standard t]))
  19387. (message "\"Org\"-menu now contains full customization menu"))
  19388. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19389. ;;;; Miscellaneous stuff
  19390. ;;; Generally useful functions
  19391. (defun org-get-at-bol (property)
  19392. "Get text property PROPERTY at beginning of line."
  19393. (get-text-property (point-at-bol) property))
  19394. (defun org-find-text-property-in-string (prop s)
  19395. "Return the first non-nil value of property PROP in string S."
  19396. (or (get-text-property 0 prop s)
  19397. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19398. prop s)))
  19399. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19400. "Display the given MESSAGE as a warning."
  19401. (if (fboundp 'display-warning)
  19402. (display-warning 'org message
  19403. (if (featurep 'xemacs) 'warning :warning))
  19404. (let ((buf (get-buffer-create "*Org warnings*")))
  19405. (with-current-buffer buf
  19406. (goto-char (point-max))
  19407. (insert "Warning (Org): " message)
  19408. (unless (bolp)
  19409. (newline)))
  19410. (display-buffer buf)
  19411. (sit-for 0))))
  19412. (defun org-eval (form)
  19413. "Eval FORM and return result."
  19414. (condition-case error
  19415. (eval form)
  19416. (error (format "%%![Error: %s]" error))))
  19417. (defun org-in-clocktable-p ()
  19418. "Check if the cursor is in a clocktable."
  19419. (let ((pos (point)) start)
  19420. (save-excursion
  19421. (end-of-line 1)
  19422. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19423. (setq start (match-beginning 0))
  19424. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19425. (>= (match-end 0) pos)
  19426. start))))
  19427. (defun org-in-commented-line ()
  19428. "Is point in a line starting with `#'?"
  19429. (equal (char-after (point-at-bol)) ?#))
  19430. (defun org-in-indented-comment-line ()
  19431. "Is point in a line starting with `#' after some white space?"
  19432. (save-excursion
  19433. (save-match-data
  19434. (goto-char (point-at-bol))
  19435. (looking-at "[ \t]*#"))))
  19436. (defun org-in-verbatim-emphasis ()
  19437. (save-match-data
  19438. (and (org-in-regexp org-emph-re 2)
  19439. (>= (point) (match-beginning 3))
  19440. (<= (point) (match-end 4))
  19441. (member (match-string 3) '("=" "~")))))
  19442. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19443. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19444. (if (and marker (marker-buffer marker)
  19445. (buffer-live-p (marker-buffer marker)))
  19446. (progn
  19447. (org-pop-to-buffer-same-window (marker-buffer marker))
  19448. (if (or (> marker (point-max)) (< marker (point-min)))
  19449. (widen))
  19450. (goto-char marker)
  19451. (org-show-context 'org-goto))
  19452. (if bookmark
  19453. (bookmark-jump bookmark)
  19454. (error "Cannot find location"))))
  19455. (defun org-quote-csv-field (s)
  19456. "Quote field for inclusion in CSV material."
  19457. (if (string-match "[\",]" s)
  19458. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19459. s))
  19460. (defun org-force-self-insert (N)
  19461. "Needed to enforce self-insert under remapping."
  19462. (interactive "p")
  19463. (self-insert-command N))
  19464. (defun org-string-width (s)
  19465. "Compute width of string, ignoring invisible characters.
  19466. This ignores character with invisibility property `org-link', and also
  19467. characters with property `org-cwidth', because these will become invisible
  19468. upon the next fontification round."
  19469. (let (b l)
  19470. (when (or (eq t buffer-invisibility-spec)
  19471. (assq 'org-link buffer-invisibility-spec))
  19472. (while (setq b (text-property-any 0 (length s)
  19473. 'invisible 'org-link s))
  19474. (setq s (concat (substring s 0 b)
  19475. (substring s (or (next-single-property-change
  19476. b 'invisible s) (length s)))))))
  19477. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19478. (setq s (concat (substring s 0 b)
  19479. (substring s (or (next-single-property-change
  19480. b 'org-cwidth s) (length s))))))
  19481. (setq l (string-width s) b -1)
  19482. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19483. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19484. l))
  19485. (defun org-shorten-string (s maxlength)
  19486. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19487. If the string is shorter or has length MAXLENGTH, just return the
  19488. original string. If it is longer, the functions finds a space in the
  19489. string, breaks this string off at that locations and adds three dots
  19490. as ellipsis. Including the ellipsis, the string will not be longer
  19491. than MAXLENGTH. If finding a good breaking point in the string does
  19492. not work, the string is just chopped off in the middle of a word
  19493. if necessary."
  19494. (if (<= (length s) maxlength)
  19495. s
  19496. (let* ((n (max (- maxlength 4) 1))
  19497. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19498. (if (string-match re s)
  19499. (concat (match-string 1 s) "...")
  19500. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19501. (defun org-get-indentation (&optional line)
  19502. "Get the indentation of the current line, interpreting tabs.
  19503. When LINE is given, assume it represents a line and compute its indentation."
  19504. (if line
  19505. (if (string-match "^ *" (org-remove-tabs line))
  19506. (match-end 0))
  19507. (save-excursion
  19508. (beginning-of-line 1)
  19509. (skip-chars-forward " \t")
  19510. (current-column))))
  19511. (defun org-get-string-indentation (s)
  19512. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19513. (let ((n -1) (i 0) (w tab-width) c)
  19514. (catch 'exit
  19515. (while (< (setq n (1+ n)) (length s))
  19516. (setq c (aref s n))
  19517. (cond ((= c ?\ ) (setq i (1+ i)))
  19518. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19519. (t (throw 'exit t)))))
  19520. i))
  19521. (defun org-remove-tabs (s &optional width)
  19522. "Replace tabulators in S with spaces.
  19523. Assumes that s is a single line, starting in column 0."
  19524. (setq width (or width tab-width))
  19525. (while (string-match "\t" s)
  19526. (setq s (replace-match
  19527. (make-string
  19528. (- (* width (/ (+ (match-beginning 0) width) width))
  19529. (match-beginning 0)) ?\ )
  19530. t t s)))
  19531. s)
  19532. (defun org-fix-indentation (line ind)
  19533. "Fix indentation in LINE.
  19534. IND is a cons cell with target and minimum indentation.
  19535. If the current indentation in LINE is smaller than the minimum,
  19536. leave it alone. If it is larger than ind, set it to the target."
  19537. (let* ((l (org-remove-tabs line))
  19538. (i (org-get-indentation l))
  19539. (i1 (car ind)) (i2 (cdr ind)))
  19540. (if (>= i i2) (setq l (substring line i2)))
  19541. (if (> i1 0)
  19542. (concat (make-string i1 ?\ ) l)
  19543. l)))
  19544. (defun org-remove-indentation (code &optional n)
  19545. "Remove the maximum common indentation from the lines in CODE.
  19546. N may optionally be the number of spaces to remove."
  19547. (with-temp-buffer
  19548. (insert code)
  19549. (org-do-remove-indentation n)
  19550. (buffer-string)))
  19551. (defun org-do-remove-indentation (&optional n)
  19552. "Remove the maximum common indentation from the buffer."
  19553. (untabify (point-min) (point-max))
  19554. (let ((min 10000) re)
  19555. (if n
  19556. (setq min n)
  19557. (goto-char (point-min))
  19558. (while (re-search-forward "^ *[^ \n]" nil t)
  19559. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19560. (unless (or (= min 0) (= min 10000))
  19561. (setq re (format "^ \\{%d\\}" min))
  19562. (goto-char (point-min))
  19563. (while (re-search-forward re nil t)
  19564. (replace-match "")
  19565. (end-of-line 1))
  19566. min)))
  19567. (defun org-fill-template (template alist)
  19568. "Find each %key of ALIST in TEMPLATE and replace it."
  19569. (let ((case-fold-search nil)
  19570. entry key value)
  19571. (setq alist (sort (copy-sequence alist)
  19572. (lambda (a b) (< (length (car a)) (length (car b))))))
  19573. (while (setq entry (pop alist))
  19574. (setq template
  19575. (replace-regexp-in-string
  19576. (concat "%" (regexp-quote (car entry)))
  19577. (or (cdr entry) "") template t t)))
  19578. template))
  19579. (defun org-base-buffer (buffer)
  19580. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19581. (if (not buffer)
  19582. buffer
  19583. (or (buffer-base-buffer buffer)
  19584. buffer)))
  19585. (defun org-trim (s)
  19586. "Remove whitespace at beginning and end of string."
  19587. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19588. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19589. s)
  19590. (defun org-wrap (string &optional width lines)
  19591. "Wrap string to either a number of lines, or a width in characters.
  19592. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19593. that costs. If there is a word longer than WIDTH, the text is actually
  19594. wrapped to the length of that word.
  19595. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19596. many lines, whatever width that takes.
  19597. The return value is a list of lines, without newlines at the end."
  19598. (let* ((words (org-split-string string "[ \t\n]+"))
  19599. (maxword (apply 'max (mapcar 'org-string-width words)))
  19600. w ll)
  19601. (cond (width
  19602. (org-do-wrap words (max maxword width)))
  19603. (lines
  19604. (setq w maxword)
  19605. (setq ll (org-do-wrap words maxword))
  19606. (if (<= (length ll) lines)
  19607. ll
  19608. (setq ll words)
  19609. (while (> (length ll) lines)
  19610. (setq w (1+ w))
  19611. (setq ll (org-do-wrap words w)))
  19612. ll))
  19613. (t (error "Cannot wrap this")))))
  19614. (defun org-do-wrap (words width)
  19615. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19616. (let (lines line)
  19617. (while words
  19618. (setq line (pop words))
  19619. (while (and words (< (+ (length line) (length (car words))) width))
  19620. (setq line (concat line " " (pop words))))
  19621. (setq lines (push line lines)))
  19622. (nreverse lines)))
  19623. (defun org-split-string (string &optional separators)
  19624. "Splits STRING into substrings at SEPARATORS.
  19625. No empty strings are returned if there are matches at the beginning
  19626. and end of string."
  19627. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19628. (start 0)
  19629. notfirst
  19630. (list nil))
  19631. (while (and (string-match rexp string
  19632. (if (and notfirst
  19633. (= start (match-beginning 0))
  19634. (< start (length string)))
  19635. (1+ start) start))
  19636. (< (match-beginning 0) (length string)))
  19637. (setq notfirst t)
  19638. (or (eq (match-beginning 0) 0)
  19639. (and (eq (match-beginning 0) (match-end 0))
  19640. (eq (match-beginning 0) start))
  19641. (setq list
  19642. (cons (substring string start (match-beginning 0))
  19643. list)))
  19644. (setq start (match-end 0)))
  19645. (or (eq start (length string))
  19646. (setq list
  19647. (cons (substring string start)
  19648. list)))
  19649. (nreverse list)))
  19650. (defun org-quote-vert (s)
  19651. "Replace \"|\" with \"\\vert\"."
  19652. (while (string-match "|" s)
  19653. (setq s (replace-match "\\vert" t t s)))
  19654. s)
  19655. (defun org-uuidgen-p (s)
  19656. "Is S an ID created by UUIDGEN?"
  19657. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19658. (defun org-in-src-block-p (&optional inside)
  19659. "Whether point is in a code source block.
  19660. When INSIDE is non-nil, don't consider we are within a src block
  19661. when point is at #+BEGIN_SRC or #+END_SRC."
  19662. (let ((case-fold-search t) ov)
  19663. (or (and (setq ov (overlays-at (point)))
  19664. (memq 'org-block-background
  19665. (overlay-properties (car ov))))
  19666. (and (not inside)
  19667. (save-match-data
  19668. (save-excursion
  19669. (beginning-of-line)
  19670. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19671. (defun org-context ()
  19672. "Return a list of contexts of the current cursor position.
  19673. If several contexts apply, all are returned.
  19674. Each context entry is a list with a symbol naming the context, and
  19675. two positions indicating start and end of the context. Possible
  19676. contexts are:
  19677. :headline anywhere in a headline
  19678. :headline-stars on the leading stars in a headline
  19679. :todo-keyword on a TODO keyword (including DONE) in a headline
  19680. :tags on the TAGS in a headline
  19681. :priority on the priority cookie in a headline
  19682. :item on the first line of a plain list item
  19683. :item-bullet on the bullet/number of a plain list item
  19684. :checkbox on the checkbox in a plain list item
  19685. :table in an org-mode table
  19686. :table-special on a special filed in a table
  19687. :table-table in a table.el table
  19688. :clocktable in a clocktable
  19689. :src-block in a source block
  19690. :link on a hyperlink
  19691. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19692. :target on a <<target>>
  19693. :radio-target on a <<<radio-target>>>
  19694. :latex-fragment on a LaTeX fragment
  19695. :latex-preview on a LaTeX fragment with overlaid preview image
  19696. This function expects the position to be visible because it uses font-lock
  19697. faces as a help to recognize the following contexts: :table-special, :link,
  19698. and :keyword."
  19699. (let* ((f (get-text-property (point) 'face))
  19700. (faces (if (listp f) f (list f)))
  19701. (case-fold-search t)
  19702. (p (point)) clist o)
  19703. ;; First the large context
  19704. (cond
  19705. ((org-at-heading-p t)
  19706. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19707. (when (progn
  19708. (beginning-of-line 1)
  19709. (looking-at org-todo-line-tags-regexp))
  19710. (push (org-point-in-group p 1 :headline-stars) clist)
  19711. (push (org-point-in-group p 2 :todo-keyword) clist)
  19712. (push (org-point-in-group p 4 :tags) clist))
  19713. (goto-char p)
  19714. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19715. (if (looking-at "\\[#[A-Z0-9]\\]")
  19716. (push (org-point-in-group p 0 :priority) clist)))
  19717. ((org-at-item-p)
  19718. (push (org-point-in-group p 2 :item-bullet) clist)
  19719. (push (list :item (point-at-bol)
  19720. (save-excursion (org-end-of-item) (point)))
  19721. clist)
  19722. (and (org-at-item-checkbox-p)
  19723. (push (org-point-in-group p 0 :checkbox) clist)))
  19724. ((org-at-table-p)
  19725. (push (list :table (org-table-begin) (org-table-end)) clist)
  19726. (if (memq 'org-formula faces)
  19727. (push (list :table-special
  19728. (previous-single-property-change p 'face)
  19729. (next-single-property-change p 'face)) clist)))
  19730. ((org-at-table-p 'any)
  19731. (push (list :table-table) clist)))
  19732. (goto-char p)
  19733. (let ((case-fold-search t))
  19734. ;; New the "medium" contexts: clocktables, source blocks
  19735. (cond ((org-in-clocktable-p)
  19736. (push (list :clocktable
  19737. (and (or (looking-at "#\\+BEGIN: clocktable")
  19738. (search-backward "#+BEGIN: clocktable" nil t))
  19739. (match-beginning 0))
  19740. (and (re-search-forward "#\\+END:?" nil t)
  19741. (match-end 0))) clist))
  19742. ((org-in-src-block-p)
  19743. (push (list :src-block
  19744. (and (or (looking-at "#\\+BEGIN_SRC")
  19745. (search-backward "#+BEGIN_SRC" nil t))
  19746. (match-beginning 0))
  19747. (and (search-forward "#+END_SRC" nil t)
  19748. (match-beginning 0))) clist))))
  19749. (goto-char p)
  19750. ;; Now the small context
  19751. (cond
  19752. ((org-at-timestamp-p)
  19753. (push (org-point-in-group p 0 :timestamp) clist))
  19754. ((memq 'org-link faces)
  19755. (push (list :link
  19756. (previous-single-property-change p 'face)
  19757. (next-single-property-change p 'face)) clist))
  19758. ((memq 'org-special-keyword faces)
  19759. (push (list :keyword
  19760. (previous-single-property-change p 'face)
  19761. (next-single-property-change p 'face)) clist))
  19762. ((org-at-target-p)
  19763. (push (org-point-in-group p 0 :target) clist)
  19764. (goto-char (1- (match-beginning 0)))
  19765. (if (looking-at org-radio-target-regexp)
  19766. (push (org-point-in-group p 0 :radio-target) clist))
  19767. (goto-char p))
  19768. ((setq o (car (delq nil
  19769. (mapcar
  19770. (lambda (x)
  19771. (if (memq x org-latex-fragment-image-overlays) x))
  19772. (overlays-at (point))))))
  19773. (push (list :latex-fragment
  19774. (overlay-start o) (overlay-end o)) clist)
  19775. (push (list :latex-preview
  19776. (overlay-start o) (overlay-end o)) clist))
  19777. ((org-inside-LaTeX-fragment-p)
  19778. ;; FIXME: positions wrong.
  19779. (push (list :latex-fragment (point) (point)) clist)))
  19780. (setq clist (nreverse (delq nil clist)))
  19781. clist))
  19782. ;; FIXME: Compare with at-regexp-p Do we need both?
  19783. (defun org-in-regexp (re &optional nlines visually)
  19784. "Check if point is inside a match of regexp.
  19785. Normally only the current line is checked, but you can include NLINES extra
  19786. lines both before and after point into the search.
  19787. If VISUALLY is set, require that the cursor is not after the match but
  19788. really on, so that the block visually is on the match."
  19789. (catch 'exit
  19790. (let ((pos (point))
  19791. (eol (point-at-eol (+ 1 (or nlines 0))))
  19792. (inc (if visually 1 0)))
  19793. (save-excursion
  19794. (beginning-of-line (- 1 (or nlines 0)))
  19795. (while (re-search-forward re eol t)
  19796. (if (and (<= (match-beginning 0) pos)
  19797. (>= (+ inc (match-end 0)) pos))
  19798. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19799. (defun org-at-regexp-p (regexp)
  19800. "Is point inside a match of REGEXP in the current line?"
  19801. (catch 'exit
  19802. (save-excursion
  19803. (let ((pos (point)) (end (point-at-eol)))
  19804. (beginning-of-line 1)
  19805. (while (re-search-forward regexp end t)
  19806. (if (and (<= (match-beginning 0) pos)
  19807. (>= (match-end 0) pos))
  19808. (throw 'exit t)))
  19809. nil))))
  19810. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19811. "Non-nil when point is between matches of START-RE and END-RE.
  19812. Also return a non-nil value when point is on one of the matches.
  19813. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19814. buffer positions. Default values are the positions of headlines
  19815. surrounding the point.
  19816. The functions returns a cons cell whose car (resp. cdr) is the
  19817. position before START-RE (resp. after END-RE)."
  19818. (save-match-data
  19819. (let ((pos (point))
  19820. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19821. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19822. beg end)
  19823. (save-excursion
  19824. ;; Point is on a block when on START-RE or if START-RE can be
  19825. ;; found before it...
  19826. (and (or (org-at-regexp-p start-re)
  19827. (re-search-backward start-re limit-up t))
  19828. (setq beg (match-beginning 0))
  19829. ;; ... and END-RE after it...
  19830. (goto-char (match-end 0))
  19831. (re-search-forward end-re limit-down t)
  19832. (> (setq end (match-end 0)) pos)
  19833. ;; ... without another START-RE in-between.
  19834. (goto-char (match-beginning 0))
  19835. (not (re-search-backward start-re (1+ beg) t))
  19836. ;; Return value.
  19837. (cons beg end))))))
  19838. (defun org-in-block-p (names)
  19839. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19840. NAMES is a list of strings containing names of blocks.
  19841. Return first block name matched, or nil. Beware that in case of
  19842. nested blocks, the returned name may not belong to the closest
  19843. block from point."
  19844. (save-match-data
  19845. (catch 'exit
  19846. (let ((case-fold-search t)
  19847. (lim-up (save-excursion (outline-previous-heading)))
  19848. (lim-down (save-excursion (outline-next-heading))))
  19849. (mapc (lambda (name)
  19850. (let ((n (regexp-quote name)))
  19851. (when (org-between-regexps-p
  19852. (concat "^[ \t]*#\\+begin_" n)
  19853. (concat "^[ \t]*#\\+end_" n)
  19854. lim-up lim-down)
  19855. (throw 'exit n))))
  19856. names))
  19857. nil)))
  19858. (defun org-in-drawer-p ()
  19859. "Is point within a drawer?"
  19860. (save-match-data
  19861. (let ((case-fold-search t)
  19862. (lim-up (save-excursion (outline-previous-heading)))
  19863. (lim-down (save-excursion (outline-next-heading))))
  19864. (org-between-regexps-p
  19865. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  19866. "^[ \t]*:end:.*$"
  19867. lim-up lim-down))))
  19868. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19869. "Call `multi-occur' with buffers for all agenda files."
  19870. (interactive "sOrg-files matching: \np")
  19871. (let* ((files (org-agenda-files))
  19872. (tnames (mapcar 'file-truename files))
  19873. (extra org-agenda-text-search-extra-files)
  19874. f)
  19875. (when (eq (car extra) 'agenda-archives)
  19876. (setq extra (cdr extra))
  19877. (setq files (org-add-archive-files files)))
  19878. (while (setq f (pop extra))
  19879. (unless (member (file-truename f) tnames)
  19880. (add-to-list 'files f 'append)
  19881. (add-to-list 'tnames (file-truename f) 'append)))
  19882. (multi-occur
  19883. (mapcar (lambda (x)
  19884. (with-current-buffer
  19885. (or (get-file-buffer x) (find-file-noselect x))
  19886. (widen)
  19887. (current-buffer)))
  19888. files)
  19889. regexp)))
  19890. (if (boundp 'occur-mode-find-occurrence-hook)
  19891. ;; Emacs 23
  19892. (add-hook 'occur-mode-find-occurrence-hook
  19893. (lambda ()
  19894. (when (derived-mode-p 'org-mode)
  19895. (org-reveal))))
  19896. ;; Emacs 22
  19897. (defadvice occur-mode-goto-occurrence
  19898. (after org-occur-reveal activate)
  19899. (and (derived-mode-p 'org-mode) (org-reveal)))
  19900. (defadvice occur-mode-goto-occurrence-other-window
  19901. (after org-occur-reveal activate)
  19902. (and (derived-mode-p 'org-mode) (org-reveal)))
  19903. (defadvice occur-mode-display-occurrence
  19904. (after org-occur-reveal activate)
  19905. (when (derived-mode-p 'org-mode)
  19906. (let ((pos (occur-mode-find-occurrence)))
  19907. (with-current-buffer (marker-buffer pos)
  19908. (save-excursion
  19909. (goto-char pos)
  19910. (org-reveal)))))))
  19911. (defun org-occur-link-in-agenda-files ()
  19912. "Create a link and search for it in the agendas.
  19913. The link is not stored in `org-stored-links', it is just created
  19914. for the search purpose."
  19915. (interactive)
  19916. (let ((link (condition-case nil
  19917. (org-store-link nil)
  19918. (error "Unable to create a link to here"))))
  19919. (org-occur-in-agenda-files (regexp-quote link))))
  19920. (defun org-reverse-string (string)
  19921. "Return the reverse of STRING."
  19922. (apply 'string (reverse (string-to-list string))))
  19923. (defsubst org-uniquify (list)
  19924. "Non-destructively remove duplicate elements from LIST."
  19925. (let ((res (copy-sequence list))) (delete-dups res)))
  19926. (defun org-uniquify-alist (alist)
  19927. "Merge elements of ALIST with the same key.
  19928. For example, in this alist:
  19929. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19930. => '((a 1 3) (b 2))
  19931. merge (a 1) and (a 3) into (a 1 3).
  19932. The function returns the new ALIST."
  19933. (let (rtn)
  19934. (mapc
  19935. (lambda (e)
  19936. (let (n)
  19937. (if (not (assoc (car e) rtn))
  19938. (push e rtn)
  19939. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19940. (setq rtn (assq-delete-all (car e) rtn))
  19941. (push n rtn))))
  19942. alist)
  19943. rtn))
  19944. (defun org-delete-all (elts list)
  19945. "Remove all elements in ELTS from LIST."
  19946. (while elts
  19947. (setq list (delete (pop elts) list)))
  19948. list)
  19949. (defun org-count (cl-item cl-seq)
  19950. "Count the number of occurrences of ITEM in SEQ.
  19951. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19952. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19953. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19954. (while (< cl-start cl-end)
  19955. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19956. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19957. (setq cl-start (1+ cl-start)))
  19958. cl-count))
  19959. (defun org-remove-if (predicate seq)
  19960. "Remove everything from SEQ that fulfills PREDICATE."
  19961. (let (res e)
  19962. (while seq
  19963. (setq e (pop seq))
  19964. (if (not (funcall predicate e)) (push e res)))
  19965. (nreverse res)))
  19966. (defun org-remove-if-not (predicate seq)
  19967. "Remove everything from SEQ that does not fulfill PREDICATE."
  19968. (let (res e)
  19969. (while seq
  19970. (setq e (pop seq))
  19971. (if (funcall predicate e) (push e res)))
  19972. (nreverse res)))
  19973. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19974. "Reduce two-argument FUNCTION across SEQ.
  19975. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19976. \":initial-value\" removed."
  19977. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19978. (cadr (memq :initial-value cl-keys)))
  19979. (cl-seq (pop cl-seq))
  19980. (t (funcall cl-func)))))
  19981. (while cl-seq
  19982. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19983. cl-accum))
  19984. (defun org-back-over-empty-lines ()
  19985. "Move backwards over whitespace, to the beginning of the first empty line.
  19986. Returns the number of empty lines passed."
  19987. (let ((pos (point)))
  19988. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19989. (skip-chars-backward " \t\n\r")
  19990. (unless (eobp)
  19991. (forward-line -1)))
  19992. (beginning-of-line 2)
  19993. (goto-char (min (point) pos))
  19994. (count-lines (point) pos)))
  19995. (defun org-skip-whitespace ()
  19996. (skip-chars-forward " \t\n\r"))
  19997. (defun org-point-in-group (point group &optional context)
  19998. "Check if POINT is in match-group GROUP.
  19999. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20000. match. If the match group does not exist or point is not inside it,
  20001. return nil."
  20002. (and (match-beginning group)
  20003. (>= point (match-beginning group))
  20004. (<= point (match-end group))
  20005. (if context
  20006. (list context (match-beginning group) (match-end group))
  20007. t)))
  20008. (defun org-switch-to-buffer-other-window (&rest args)
  20009. "Switch to buffer in a second window on the current frame.
  20010. In particular, do not allow pop-up frames.
  20011. Returns the newly created buffer."
  20012. (org-no-popups
  20013. (apply 'switch-to-buffer-other-window args)))
  20014. (defun org-combine-plists (&rest plists)
  20015. "Create a single property list from all plists in PLISTS.
  20016. The process starts by copying the first list, and then setting properties
  20017. from the other lists. Settings in the last list are the most significant
  20018. ones and overrule settings in the other lists."
  20019. (let ((rtn (copy-sequence (pop plists)))
  20020. p v ls)
  20021. (while plists
  20022. (setq ls (pop plists))
  20023. (while ls
  20024. (setq p (pop ls) v (pop ls))
  20025. (setq rtn (plist-put rtn p v))))
  20026. rtn))
  20027. (defun org-replace-escapes (string table)
  20028. "Replace %-escapes in STRING with values in TABLE.
  20029. TABLE is an association list with keys like \"%a\" and string values.
  20030. The sequences in STRING may contain normal field width and padding information,
  20031. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20032. so values can contain further %-escapes if they are define later in TABLE."
  20033. (let ((tbl (copy-alist table))
  20034. (case-fold-search nil)
  20035. (pchg 0)
  20036. e re rpl)
  20037. (while (setq e (pop tbl))
  20038. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20039. (when (and (cdr e) (string-match re (cdr e)))
  20040. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20041. (safe "SREF"))
  20042. (add-text-properties 0 3 (list 'sref sref) safe)
  20043. (setcdr e (replace-match safe t t (cdr e)))))
  20044. (while (string-match re string)
  20045. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20046. (cdr e)))
  20047. (setq string (replace-match rpl t t string))))
  20048. (while (setq pchg (next-property-change pchg string))
  20049. (let ((sref (get-text-property pchg 'sref string)))
  20050. (when (and sref (string-match "SREF" string pchg))
  20051. (setq string (replace-match sref t t string)))))
  20052. string))
  20053. (defun org-sublist (list start end)
  20054. "Return a section of LIST, from START to END.
  20055. Counting starts at 1."
  20056. (let (rtn (c start))
  20057. (setq list (nthcdr (1- start) list))
  20058. (while (and list (<= c end))
  20059. (push (pop list) rtn)
  20060. (setq c (1+ c)))
  20061. (nreverse rtn)))
  20062. (defun org-find-base-buffer-visiting (file)
  20063. "Like `find-buffer-visiting' but always return the base buffer and
  20064. not an indirect buffer."
  20065. (let ((buf (or (get-file-buffer file)
  20066. (find-buffer-visiting file))))
  20067. (if buf
  20068. (or (buffer-base-buffer buf) buf)
  20069. nil)))
  20070. (defun org-image-file-name-regexp (&optional extensions)
  20071. "Return regexp matching the file names of images.
  20072. If EXTENSIONS is given, only match these."
  20073. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20074. (image-file-name-regexp)
  20075. (let ((image-file-name-extensions
  20076. (or extensions
  20077. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20078. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20079. (concat "\\."
  20080. (regexp-opt (nconc (mapcar 'upcase
  20081. image-file-name-extensions)
  20082. image-file-name-extensions)
  20083. t)
  20084. "\\'"))))
  20085. (defun org-file-image-p (file &optional extensions)
  20086. "Return non-nil if FILE is an image."
  20087. (save-match-data
  20088. (string-match (org-image-file-name-regexp extensions) file)))
  20089. (defun org-get-cursor-date (&optional with-time)
  20090. "Return the date at cursor in as a time.
  20091. This works in the calendar and in the agenda, anywhere else it just
  20092. returns the current time.
  20093. If WITH-TIME is non-nil, returns the time of the event at point (in
  20094. the agenda) or the current time of the day."
  20095. (let (date day defd tp tm hod mod)
  20096. (when with-time
  20097. (setq tp (get-text-property (point) 'time))
  20098. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20099. (setq hod (string-to-number (match-string 1 tp))
  20100. mod (string-to-number (match-string 2 tp))))
  20101. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20102. mod (nth 1 (decode-time (current-time))))))
  20103. (cond
  20104. ((eq major-mode 'calendar-mode)
  20105. (setq date (calendar-cursor-to-date)
  20106. defd (encode-time 0 (or mod 0) (or hod 0)
  20107. (nth 1 date) (nth 0 date) (nth 2 date))))
  20108. ((eq major-mode 'org-agenda-mode)
  20109. (setq day (get-text-property (point) 'day))
  20110. (if day
  20111. (setq date (calendar-gregorian-from-absolute day)
  20112. defd (encode-time 0 (or mod 0) (or hod 0)
  20113. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20114. (or defd (current-time))))
  20115. (defun org-mark-subtree (&optional up)
  20116. "Mark the current subtree.
  20117. This puts point at the start of the current subtree, and mark at
  20118. the end. If a numeric prefix UP is given, move up into the
  20119. hierarchy of headlines by UP levels before marking the subtree."
  20120. (interactive "P")
  20121. (org-with-limited-levels
  20122. (cond ((org-at-heading-p) (beginning-of-line))
  20123. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20124. (t (outline-previous-visible-heading 1))))
  20125. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20126. (if (org-called-interactively-p 'any)
  20127. (call-interactively 'org-mark-element)
  20128. (org-mark-element)))
  20129. ;;; Indentation
  20130. (defun org-indent-line ()
  20131. "Indent line depending on context."
  20132. (interactive)
  20133. (let* ((pos (point))
  20134. (itemp (org-at-item-p))
  20135. (case-fold-search t)
  20136. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20137. (inline-task-p (and (featurep 'org-inlinetask)
  20138. (org-inlinetask-in-task-p)))
  20139. (inline-re (and inline-task-p
  20140. (org-inlinetask-outline-regexp)))
  20141. column)
  20142. (if (and orgstruct-is-++ (eq pos (point)))
  20143. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20144. (indent-according-to-mode))
  20145. (beginning-of-line 1)
  20146. (cond
  20147. ;; Headings
  20148. ((looking-at org-outline-regexp) (setq column 0))
  20149. ;; Footnote definition
  20150. ((looking-at org-footnote-definition-re) (setq column 0))
  20151. ;; Literal examples
  20152. ((looking-at "[ \t]*:\\( \\|$\\)")
  20153. (setq column (org-get-indentation))) ; do nothing
  20154. ;; Lists
  20155. ((ignore-errors (goto-char (org-in-item-p)))
  20156. (setq column (if itemp
  20157. (org-get-indentation)
  20158. (org-list-item-body-column (point))))
  20159. (goto-char pos))
  20160. ;; Drawers
  20161. ((and (looking-at "[ \t]*:END:")
  20162. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20163. (save-excursion
  20164. (goto-char (1- (match-beginning 1)))
  20165. (setq column (current-column))))
  20166. ;; Special blocks
  20167. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20168. (save-excursion
  20169. (re-search-backward
  20170. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20171. (setq column (org-get-indentation (match-string 0))))
  20172. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20173. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20174. (save-excursion
  20175. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20176. (setq column
  20177. (cond ((equal (downcase (match-string 1)) "src")
  20178. ;; src blocks: let `org-edit-src-exit' handle them
  20179. (org-get-indentation))
  20180. ((equal (downcase (match-string 1)) "example")
  20181. (max (org-get-indentation)
  20182. (org-get-indentation (match-string 0))))
  20183. (t
  20184. (org-get-indentation (match-string 0))))))
  20185. ;; This line has nothing special, look at the previous relevant
  20186. ;; line to compute indentation
  20187. (t
  20188. (beginning-of-line 0)
  20189. (while (and (not (bobp))
  20190. (not (looking-at org-table-line-regexp))
  20191. (not (looking-at org-drawer-regexp))
  20192. ;; When point started in an inline task, do not move
  20193. ;; above task starting line.
  20194. (not (and inline-task-p (looking-at inline-re)))
  20195. ;; Skip drawers, blocks, empty lines, verbatim,
  20196. ;; comments, tables, footnotes definitions, lists,
  20197. ;; inline tasks.
  20198. (or (and (looking-at "[ \t]*:END:")
  20199. (re-search-backward org-drawer-regexp nil t))
  20200. (and (looking-at "[ \t]*#\\+end_")
  20201. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20202. (looking-at "[ \t]*[\n:#|]")
  20203. (looking-at org-footnote-definition-re)
  20204. (and (not inline-task-p)
  20205. (featurep 'org-inlinetask)
  20206. (org-inlinetask-in-task-p)
  20207. (or (org-inlinetask-goto-beginning) t))))
  20208. (beginning-of-line 0))
  20209. (cond
  20210. ;; There was a list item above.
  20211. ((save-excursion
  20212. (and (ignore-errors (goto-char (org-in-item-p)))
  20213. (goto-char
  20214. (org-list-get-top-point (org-list-struct)))))
  20215. (looking-at org-list-full-item-re)
  20216. (setq column (length (match-string 0))))
  20217. ;; There was an heading above.
  20218. ((looking-at "\\*+[ \t]+")
  20219. (if (not org-adapt-indentation)
  20220. (setq column 0)
  20221. (goto-char (match-end 0))
  20222. (setq column (current-column))))
  20223. ;; A drawer had started and is unfinished
  20224. ((looking-at org-drawer-regexp)
  20225. (goto-char (1- (match-beginning 1)))
  20226. (setq column (current-column)))
  20227. ;; Else, nothing noticeable found: get indentation and go on.
  20228. (t (setq column (org-get-indentation))))))
  20229. ;; Now apply indentation and move cursor accordingly
  20230. (goto-char pos)
  20231. (if (<= (current-column) (current-indentation))
  20232. (org-indent-line-to column)
  20233. (save-excursion (org-indent-line-to column)))
  20234. ;; Special polishing for properties, see `org-property-format'
  20235. (setq column (current-column))
  20236. (beginning-of-line 1)
  20237. (if (looking-at
  20238. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20239. (replace-match (concat (match-string 1)
  20240. (format org-property-format
  20241. (match-string 2) (match-string 3)))
  20242. t t))
  20243. (org-move-to-column column))))
  20244. (defun org-indent-drawer ()
  20245. "Indent the drawer at point."
  20246. (interactive)
  20247. (let ((p (point))
  20248. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20249. (match-end 0)))
  20250. (folded
  20251. (save-excursion
  20252. (end-of-line)
  20253. (when (overlays-at (point))
  20254. (member 'invisible (overlay-properties
  20255. (car (overlays-at (point)))))))))
  20256. (when folded (org-cycle))
  20257. (indent-for-tab-command)
  20258. (while (and (move-beginning-of-line 2) (< (point) e))
  20259. (indent-for-tab-command))
  20260. (goto-char p)
  20261. (when folded (org-cycle)))
  20262. (message "Drawer at point indented"))
  20263. (defun org-indent-block ()
  20264. "Indent the block at point."
  20265. (interactive)
  20266. (let ((p (point))
  20267. (case-fold-search t)
  20268. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20269. (match-end 0)))
  20270. (folded
  20271. (save-excursion
  20272. (end-of-line)
  20273. (when (overlays-at (point))
  20274. (member 'invisible (overlay-properties
  20275. (car (overlays-at (point)))))))))
  20276. (when folded (org-cycle))
  20277. (indent-for-tab-command)
  20278. (while (and (move-beginning-of-line 2) (< (point) e))
  20279. (indent-for-tab-command))
  20280. (goto-char p)
  20281. (when folded (org-cycle)))
  20282. (message "Block at point indented"))
  20283. (defun org-indent-region (start end)
  20284. "Indent region."
  20285. (interactive "r")
  20286. (save-excursion
  20287. (let ((line-end (org-current-line end)))
  20288. (goto-char start)
  20289. (while (< (org-current-line) line-end)
  20290. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20291. (t (call-interactively 'org-indent-line)))
  20292. (move-beginning-of-line 2)))))
  20293. ;;; Filling
  20294. ;; We use our own fill-paragraph and auto-fill functions.
  20295. ;; `org-fill-paragraph' relies on adaptive filling and context
  20296. ;; checking. Appropriate `fill-prefix' is computed with
  20297. ;; `org-adaptive-fill-function'.
  20298. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20299. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20300. ;; `org-adaptive-fill-function' value. Internally,
  20301. ;; `org-comment-line-break-function' breaks the line.
  20302. ;; `org-setup-filling' installs filling and auto-filling related
  20303. ;; variables during `org-mode' initialization.
  20304. (defun org-setup-filling ()
  20305. (interactive)
  20306. ;; Prevent auto-fill from inserting unwanted new items.
  20307. (when (boundp 'fill-nobreak-predicate)
  20308. (org-set-local
  20309. 'fill-nobreak-predicate
  20310. (org-uniquify
  20311. (append fill-nobreak-predicate
  20312. '(org-fill-paragraph-separate-nobreak-p
  20313. org-fill-line-break-nobreak-p
  20314. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20315. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20316. (org-set-local 'auto-fill-inhibit-regexp nil)
  20317. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20318. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20319. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20320. (defvar org-element-paragraph-separate) ; org-element.el
  20321. (defun org-fill-paragraph-separate-nobreak-p ()
  20322. "Non-nil when a new line at point would end current paragraph."
  20323. (looking-at (substring org-element-paragraph-separate 1)))
  20324. (defun org-fill-line-break-nobreak-p ()
  20325. "Non-nil when a new line at point would create an Org line break."
  20326. (save-excursion
  20327. (skip-chars-backward "[ \t]")
  20328. (skip-chars-backward "\\\\")
  20329. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20330. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20331. "Non-nil when a new line at point would split a timestamp."
  20332. (and (org-at-timestamp-p t)
  20333. (not (looking-at org-ts-regexp-both))))
  20334. (declare-function message-in-body-p "message" ())
  20335. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20336. (defun org-adaptive-fill-function ()
  20337. "Compute a fill prefix for the current line.
  20338. Return fill prefix, as a string, or nil if current line isn't
  20339. meant to be filled."
  20340. (let (prefix)
  20341. (catch 'exit
  20342. (when (derived-mode-p 'message-mode)
  20343. (save-excursion
  20344. (beginning-of-line)
  20345. (cond ((or (not (message-in-body-p))
  20346. (looking-at orgtbl-line-start-regexp))
  20347. (throw 'exit nil))
  20348. ((looking-at message-cite-prefix-regexp)
  20349. (throw 'exit (match-string-no-properties 0)))
  20350. ((looking-at org-outline-regexp)
  20351. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20352. (org-with-wide-buffer
  20353. (let* ((p (line-beginning-position))
  20354. (element (save-excursion
  20355. (beginning-of-line)
  20356. (or (ignore-errors (org-element-at-point))
  20357. (user-error "An element cannot be parsed line %d"
  20358. (line-number-at-pos (point))))))
  20359. (type (org-element-type element))
  20360. (post-affiliated (org-element-property :post-affiliated element)))
  20361. (unless (and post-affiliated (< p post-affiliated))
  20362. (case type
  20363. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20364. (footnote-definition "")
  20365. ((item plain-list)
  20366. (make-string (org-list-item-body-column
  20367. (or post-affiliated
  20368. (org-element-property :begin element)))
  20369. ? ))
  20370. (paragraph
  20371. ;; Fill prefix is usually the same as the current line,
  20372. ;; except if the paragraph is at the beginning of an item.
  20373. (let ((parent (org-element-property :parent element)))
  20374. (cond ((eq (org-element-type parent) 'item)
  20375. (make-string (org-list-item-body-column
  20376. (org-element-property :begin parent))
  20377. ? ))
  20378. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20379. (match-string 0))
  20380. (t ""))))
  20381. (comment-block
  20382. ;; Only fill contents if P is within block boundaries.
  20383. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20384. (forward-line)
  20385. (point)))
  20386. (cend (save-excursion
  20387. (goto-char (org-element-property :end element))
  20388. (skip-chars-backward " \r\t\n")
  20389. (line-beginning-position))))
  20390. (when (and (>= p cbeg) (< p cend))
  20391. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20392. (match-string 0)
  20393. "")))))))))))
  20394. (declare-function message-goto-body "message" ())
  20395. (defvar message-cite-prefix-regexp) ; From message.el
  20396. (defun org-fill-paragraph (&optional justify)
  20397. "Fill element at point, when applicable.
  20398. This function only applies to comment blocks, comments, example
  20399. blocks and paragraphs. Also, as a special case, re-align table
  20400. when point is at one.
  20401. If JUSTIFY is non-nil (interactively, with prefix argument),
  20402. justify as well. If `sentence-end-double-space' is non-nil, then
  20403. period followed by one space does not end a sentence, so don't
  20404. break a line there. The variable `fill-column' controls the
  20405. width for filling.
  20406. For convenience, when point is at a plain list, an item or
  20407. a footnote definition, try to fill the first paragraph within."
  20408. (interactive)
  20409. (if (and (derived-mode-p 'message-mode)
  20410. (or (not (message-in-body-p))
  20411. (save-excursion (move-beginning-of-line 1)
  20412. (looking-at message-cite-prefix-regexp))))
  20413. ;; First ensure filling is correct in message-mode.
  20414. (let ((fill-paragraph-function
  20415. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20416. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20417. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20418. (paragraph-separate
  20419. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20420. (fill-paragraph nil))
  20421. (with-syntax-table org-mode-transpose-word-syntax-table
  20422. ;; Move to end of line in order to get the first paragraph
  20423. ;; within a plain list or a footnote definition.
  20424. (let ((element (save-excursion
  20425. (end-of-line)
  20426. (or (ignore-errors (org-element-at-point))
  20427. (user-error "An element cannot be parsed line %d"
  20428. (line-number-at-pos (point)))))))
  20429. ;; First check if point is in a blank line at the beginning of
  20430. ;; the buffer. In that case, ignore filling.
  20431. (case (org-element-type element)
  20432. ;; Use major mode filling function is src blocks.
  20433. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20434. ;; Align Org tables, leave table.el tables as-is.
  20435. (table-row (org-table-align) t)
  20436. (table
  20437. (when (eq (org-element-property :type element) 'org)
  20438. (save-excursion
  20439. (goto-char (org-element-property :post-affiliated element))
  20440. (org-table-align)))
  20441. t)
  20442. (paragraph
  20443. ;; Paragraphs may contain `line-break' type objects.
  20444. (let ((beg (max (point-min)
  20445. (org-element-property :contents-begin element)))
  20446. (end (min (point-max)
  20447. (org-element-property :contents-end element))))
  20448. ;; Do nothing if point is at an affiliated keyword.
  20449. (if (< (line-end-position) beg) t
  20450. (when (derived-mode-p 'message-mode)
  20451. ;; In `message-mode', do not fill following citation
  20452. ;; in current paragraph nor text before message body.
  20453. (let ((body-start (save-excursion (message-goto-body))))
  20454. (when body-start (setq beg (max body-start beg))))
  20455. (when (save-excursion
  20456. (re-search-forward
  20457. (concat "^" message-cite-prefix-regexp) end t))
  20458. (setq end (match-beginning 0))))
  20459. ;; Fill paragraph, taking line breaks into account.
  20460. ;; For that, slice the paragraph using line breaks as
  20461. ;; separators, and fill the parts in reverse order to
  20462. ;; avoid messing with markers.
  20463. (save-excursion
  20464. (goto-char end)
  20465. (mapc
  20466. (lambda (pos)
  20467. (fill-region-as-paragraph pos (point) justify)
  20468. (goto-char pos))
  20469. ;; Find the list of ending positions for line breaks
  20470. ;; in the current paragraph. Add paragraph
  20471. ;; beginning to include first slice.
  20472. (nreverse
  20473. (cons beg
  20474. (org-element-map
  20475. (org-element--parse-objects
  20476. beg end nil (org-element-restriction 'paragraph))
  20477. 'line-break
  20478. (lambda (lb) (org-element-property :end lb)))))))
  20479. t)))
  20480. ;; Contents of `comment-block' type elements should be
  20481. ;; filled as plain text, but only if point is within block
  20482. ;; markers.
  20483. (comment-block
  20484. (let* ((case-fold-search t)
  20485. (beg (save-excursion
  20486. (goto-char (org-element-property :begin element))
  20487. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20488. (forward-line)
  20489. (point)))
  20490. (end (save-excursion
  20491. (goto-char (org-element-property :end element))
  20492. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20493. (line-beginning-position))))
  20494. (when (and (>= (point) beg) (< (point) end))
  20495. (fill-region-as-paragraph
  20496. (save-excursion
  20497. (end-of-line)
  20498. (re-search-backward "^[ \t]*$" beg 'move)
  20499. (line-beginning-position))
  20500. (save-excursion
  20501. (beginning-of-line)
  20502. (re-search-forward "^[ \t]*$" end 'move)
  20503. (line-beginning-position))
  20504. justify)))
  20505. t)
  20506. ;; Fill comments.
  20507. (comment (fill-comment-paragraph justify))
  20508. ;; Ignore every other element.
  20509. (otherwise t))))))
  20510. (defun org-auto-fill-function ()
  20511. "Auto-fill function."
  20512. ;; Check if auto-filling is meaningful.
  20513. (let ((fc (current-fill-column)))
  20514. (when (and fc (> (current-column) fc))
  20515. (let* ((fill-prefix (org-adaptive-fill-function))
  20516. ;; Enforce empty fill prefix, if required. Otherwise, it
  20517. ;; will be computed again.
  20518. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20519. (when fill-prefix (do-auto-fill))))))
  20520. (defun org-comment-line-break-function (&optional soft)
  20521. "Break line at point and indent, continuing comment if within one.
  20522. The inserted newline is marked hard if variable
  20523. `use-hard-newlines' is true, unless optional argument SOFT is
  20524. non-nil."
  20525. (if soft (insert-and-inherit ?\n) (newline 1))
  20526. (save-excursion (forward-char -1) (delete-horizontal-space))
  20527. (delete-horizontal-space)
  20528. (indent-to-left-margin)
  20529. (insert-before-markers-and-inherit fill-prefix))
  20530. ;;; Comments
  20531. ;; Org comments syntax is quite complex. It requires the entire line
  20532. ;; to be just a comment. Also, even with the right syntax at the
  20533. ;; beginning of line, some some elements (i.e. verse-block or
  20534. ;; example-block) don't accept comments. Usual Emacs comment commands
  20535. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20536. ;; Org still relies on `comment-dwim', but cannot trust
  20537. ;; `comment-only-p'. So, `comment-region-function' and
  20538. ;; `uncomment-region-function' both point
  20539. ;; to`org-comment-or-uncomment-region'. Eventually,
  20540. ;; `org-insert-comment' takes care of insertion of comments at the
  20541. ;; beginning of line.
  20542. ;; `org-setup-comments-handling' install comments related variables
  20543. ;; during `org-mode' initialization.
  20544. (defun org-setup-comments-handling ()
  20545. (interactive)
  20546. (org-set-local 'comment-use-syntax nil)
  20547. (org-set-local 'comment-start "# ")
  20548. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20549. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20550. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20551. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20552. (defun org-insert-comment ()
  20553. "Insert an empty comment above current line.
  20554. If the line is empty, insert comment at its beginning."
  20555. (beginning-of-line)
  20556. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20557. (org-indent-line)
  20558. (insert "# "))
  20559. (defvar comment-empty-lines) ; From newcomment.el.
  20560. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20561. "Comment or uncomment each non-blank line in the region.
  20562. Uncomment each non-blank line between BEG and END if it only
  20563. contains commented lines. Otherwise, comment them."
  20564. (save-restriction
  20565. ;; Restrict region
  20566. (narrow-to-region (save-excursion (goto-char beg)
  20567. (skip-chars-forward " \r\t\n" end)
  20568. (line-beginning-position))
  20569. (save-excursion (goto-char end)
  20570. (skip-chars-backward " \r\t\n" beg)
  20571. (line-end-position)))
  20572. (let ((uncommentp
  20573. ;; UNCOMMENTP is non-nil when every non blank line between
  20574. ;; BEG and END is a comment.
  20575. (save-excursion
  20576. (goto-char (point-min))
  20577. (while (and (not (eobp))
  20578. (let ((element (org-element-at-point)))
  20579. (and (eq (org-element-type element) 'comment)
  20580. (goto-char (min (point-max)
  20581. (org-element-property
  20582. :end element)))))))
  20583. (eobp))))
  20584. (if uncommentp
  20585. ;; Only blank lines and comments in region: uncomment it.
  20586. (save-excursion
  20587. (goto-char (point-min))
  20588. (while (not (eobp))
  20589. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20590. (replace-match "" nil nil nil 1))
  20591. (forward-line)))
  20592. ;; Comment each line in region.
  20593. (let ((min-indent (point-max)))
  20594. ;; First find the minimum indentation across all lines.
  20595. (save-excursion
  20596. (goto-char (point-min))
  20597. (while (and (not (eobp)) (not (zerop min-indent)))
  20598. (unless (looking-at "[ \t]*$")
  20599. (setq min-indent (min min-indent (current-indentation))))
  20600. (forward-line)))
  20601. ;; Then loop over all lines.
  20602. (save-excursion
  20603. (goto-char (point-min))
  20604. (while (not (eobp))
  20605. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20606. ;; Don't get fooled by invisible text (e.g. link path)
  20607. ;; when moving to column MIN-INDENT.
  20608. (let ((buffer-invisibility-spec nil))
  20609. (org-move-to-column min-indent t))
  20610. (insert comment-start))
  20611. (forward-line))))))))
  20612. ;;; Planning
  20613. ;; This section contains tools to operate on timestamp objects, as
  20614. ;; returned by, e.g. `org-element-context'.
  20615. (defun org-timestamp-has-time-p (timestamp)
  20616. "Non-nil when TIMESTAMP has a time specified."
  20617. (org-element-property :hour-start timestamp))
  20618. (defun org-timestamp-format (timestamp format &optional end utc)
  20619. "Format a TIMESTAMP element into a string.
  20620. FORMAT is a format specifier to be passed to
  20621. `format-time-string'.
  20622. When optional argument END is non-nil, use end of date-range or
  20623. time-range, if possible.
  20624. When optional argument UTC is non-nil, time will be expressed as
  20625. Universal Time."
  20626. (format-time-string
  20627. format
  20628. (apply 'encode-time
  20629. (cons 0
  20630. (mapcar
  20631. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20632. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20633. '(:minute-start :hour-start :day-start :month-start
  20634. :year-start)))))
  20635. utc))
  20636. (defun org-timestamp-split-range (timestamp &optional end)
  20637. "Extract a timestamp object from a date or time range.
  20638. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20639. the end of the range. Otherwise, extract its start.
  20640. Return a new timestamp object sharing the same parent as
  20641. TIMESTAMP."
  20642. (let ((type (org-element-property :type timestamp)))
  20643. (if (memq type '(active inactive diary)) timestamp
  20644. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20645. ;; Set new type.
  20646. (org-element-put-property
  20647. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20648. ;; Copy start properties over end properties if END is
  20649. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20650. (let ((p-alist '((:minute-start . :minute-end)
  20651. (:hour-start . :hour-end)
  20652. (:day-start . :day-end)
  20653. (:month-start . :month-end)
  20654. (:year-start . :year-end))))
  20655. (dolist (p-cell p-alist)
  20656. (org-element-put-property
  20657. split-ts
  20658. (funcall (if end 'car 'cdr) p-cell)
  20659. (org-element-property
  20660. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20661. ;; Eventually refresh `:raw-value'.
  20662. (org-element-put-property split-ts :raw-value nil)
  20663. (org-element-put-property
  20664. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20665. (defun org-timestamp-translate (timestamp &optional boundary)
  20666. "Apply `org-translate-time' on a TIMESTAMP object.
  20667. When optional argument BOUNDARY is non-nil, it is either the
  20668. symbol `start' or `end'. In this case, only translate the
  20669. starting or ending part of TIMESTAMP if it is a date or time
  20670. range. Otherwise, translate both parts."
  20671. (if (and (not boundary)
  20672. (memq (org-element-property :type timestamp)
  20673. '(active-range inactive-range)))
  20674. (concat
  20675. (org-translate-time
  20676. (org-element-property :raw-value
  20677. (org-timestamp-split-range timestamp)))
  20678. "--"
  20679. (org-translate-time
  20680. (org-element-property :raw-value
  20681. (org-timestamp-split-range timestamp t))))
  20682. (org-translate-time
  20683. (org-element-property
  20684. :raw-value
  20685. (if (not boundary) timestamp
  20686. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20687. ;;; Other stuff.
  20688. (defun org-toggle-fixed-width-section (arg)
  20689. "Toggle the fixed-width export.
  20690. If there is no active region, the QUOTE keyword at the current headline is
  20691. inserted or removed. When present, it causes the text between this headline
  20692. and the next to be exported as fixed-width text, and unmodified.
  20693. If there is an active region, this command adds or removes a colon as the
  20694. first character of this line. If the first character of a line is a colon,
  20695. this line is also exported in fixed-width font."
  20696. (interactive "P")
  20697. (let* ((cc 0)
  20698. (regionp (org-region-active-p))
  20699. (beg (if regionp (region-beginning) (point)))
  20700. (end (if regionp (region-end)))
  20701. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20702. (case-fold-search nil)
  20703. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20704. off)
  20705. (if regionp
  20706. (save-excursion
  20707. (goto-char beg)
  20708. (setq cc (current-column))
  20709. (beginning-of-line 1)
  20710. (setq off (looking-at re))
  20711. (while (> nlines 0)
  20712. (setq nlines (1- nlines))
  20713. (beginning-of-line 1)
  20714. (cond
  20715. (arg
  20716. (org-move-to-column cc t)
  20717. (insert ": \n")
  20718. (forward-line -1))
  20719. ((and off (looking-at re))
  20720. (replace-match "" t t nil 1))
  20721. ((not off) (org-move-to-column cc t) (insert ": ")))
  20722. (forward-line 1)))
  20723. (save-excursion
  20724. (org-back-to-heading)
  20725. (cond
  20726. ((looking-at (format org-heading-keyword-regexp-format
  20727. org-quote-string))
  20728. (goto-char (match-end 1))
  20729. (looking-at (concat " +" org-quote-string))
  20730. (replace-match "" t t)
  20731. (when (eolp) (insert " ")))
  20732. ((looking-at org-outline-regexp)
  20733. (goto-char (match-end 0))
  20734. (insert org-quote-string " ")))))))
  20735. (defun org-reftex-citation ()
  20736. "Use reftex-citation to insert a citation into the buffer.
  20737. This looks for a line like
  20738. #+BIBLIOGRAPHY: foo plain option:-d
  20739. and derives from it that foo.bib is the bibliography file relevant
  20740. for this document. It then installs the necessary environment for RefTeX
  20741. to work in this buffer and calls `reftex-citation' to insert a citation
  20742. into the buffer.
  20743. Export of such citations to both LaTeX and HTML is handled by the contributed
  20744. package org-exp-bibtex by Taru Karttunen."
  20745. (interactive)
  20746. (let ((reftex-docstruct-symbol 'rds)
  20747. (reftex-cite-format "\\cite{%l}")
  20748. rds bib)
  20749. (save-excursion
  20750. (save-restriction
  20751. (widen)
  20752. (let ((case-fold-search t)
  20753. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20754. (if (not (save-excursion
  20755. (or (re-search-forward re nil t)
  20756. (re-search-backward re nil t))))
  20757. (error "No bibliography defined in file")
  20758. (setq bib (concat (match-string 1) ".bib")
  20759. rds (list (list 'bib bib)))))))
  20760. (call-interactively 'reftex-citation)))
  20761. ;;;; Functions extending outline functionality
  20762. (defun org-beginning-of-line (&optional arg)
  20763. "Go to the beginning of the current line. If that is invisible, continue
  20764. to a visible line beginning. This makes the function of C-a more intuitive.
  20765. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20766. first attempt, and only move to after the tags when the cursor is already
  20767. beyond the end of the headline."
  20768. (interactive "P")
  20769. (let ((pos (point))
  20770. (special (if (consp org-special-ctrl-a/e)
  20771. (car org-special-ctrl-a/e)
  20772. org-special-ctrl-a/e))
  20773. refpos)
  20774. (if (org-bound-and-true-p visual-line-mode)
  20775. (beginning-of-visual-line 1)
  20776. (beginning-of-line 1))
  20777. (if (and arg (fboundp 'move-beginning-of-line))
  20778. (call-interactively 'move-beginning-of-line)
  20779. (if (bobp)
  20780. nil
  20781. (backward-char 1)
  20782. (if (org-truely-invisible-p)
  20783. (while (and (not (bobp)) (org-truely-invisible-p))
  20784. (backward-char 1)
  20785. (beginning-of-line 1))
  20786. (forward-char 1))))
  20787. (when special
  20788. (cond
  20789. ((and (looking-at org-complex-heading-regexp)
  20790. (= (char-after (match-end 1)) ?\ ))
  20791. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20792. (point-at-eol)))
  20793. (goto-char
  20794. (if (eq special t)
  20795. (cond ((> pos refpos) refpos)
  20796. ((= pos (point)) refpos)
  20797. (t (point)))
  20798. (cond ((> pos (point)) (point))
  20799. ((not (eq last-command this-command)) (point))
  20800. (t refpos)))))
  20801. ((org-at-item-p)
  20802. ;; Being at an item and not looking at an the item means point
  20803. ;; was previously moved to beginning of a visual line, which
  20804. ;; doesn't contain the item. Therefore, do nothing special,
  20805. ;; just stay here.
  20806. (when (looking-at org-list-full-item-re)
  20807. ;; Set special position at first white space character after
  20808. ;; bullet, and check-box, if any.
  20809. (let ((after-bullet
  20810. (let ((box (match-end 3)))
  20811. (if (not box) (match-end 1)
  20812. (let ((after (char-after box)))
  20813. (if (and after (= after ? )) (1+ box) box))))))
  20814. ;; Special case: Move point to special position when
  20815. ;; currently after it or at beginning of line.
  20816. (if (eq special t)
  20817. (when (or (> pos after-bullet) (= (point) pos))
  20818. (goto-char after-bullet))
  20819. ;; Reversed case: Move point to special position when
  20820. ;; point was already at beginning of line and command is
  20821. ;; repeated.
  20822. (when (and (= (point) pos) (eq last-command this-command))
  20823. (goto-char after-bullet))))))))
  20824. (org-no-warnings
  20825. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20826. (defun org-end-of-line (&optional arg)
  20827. "Go to the end of the line.
  20828. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20829. tags on the first attempt, and only move to after the tags when
  20830. the cursor is already beyond the end of the headline."
  20831. (interactive "P")
  20832. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20833. org-special-ctrl-a/e))
  20834. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20835. 'end-of-visual-line)
  20836. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20837. (t 'end-of-line))))
  20838. (if (or (not special) arg) (call-interactively move-fun)
  20839. (let* ((element (save-excursion (beginning-of-line)
  20840. (org-element-at-point)))
  20841. (type (org-element-type element)))
  20842. (cond
  20843. ((memq type '(headline inlinetask))
  20844. (let ((pos (point)))
  20845. (beginning-of-line 1)
  20846. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20847. (if (eq special t)
  20848. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20849. (goto-char (match-beginning 1))
  20850. (goto-char (match-end 0)))
  20851. (if (or (< pos (match-end 0))
  20852. (not (eq this-command last-command)))
  20853. (goto-char (match-end 0))
  20854. (goto-char (match-beginning 1))))
  20855. (call-interactively move-fun))))
  20856. ((org-element-property :hiddenp element)
  20857. ;; If element is hidden, `move-end-of-line' would put point
  20858. ;; after it. Use `end-of-line' to stay on current line.
  20859. (call-interactively 'end-of-line))
  20860. (t (call-interactively move-fun)))))
  20861. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20862. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20863. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20864. (defun org-backward-sentence (&optional arg)
  20865. "Go to beginning of sentence, or beginning of table field.
  20866. This will call `backward-sentence' or `org-table-beginning-of-field',
  20867. depending on context."
  20868. (interactive "P")
  20869. (cond
  20870. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20871. (t (call-interactively 'backward-sentence))))
  20872. (defun org-forward-sentence (&optional arg)
  20873. "Go to end of sentence, or end of table field.
  20874. This will call `forward-sentence' or `org-table-end-of-field',
  20875. depending on context."
  20876. (interactive "P")
  20877. (cond
  20878. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20879. (t (call-interactively 'forward-sentence))))
  20880. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20881. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20882. (defun org-kill-line (&optional arg)
  20883. "Kill line, to tags or end of line."
  20884. (interactive "P")
  20885. (cond
  20886. ((or (not org-special-ctrl-k)
  20887. (bolp)
  20888. (not (org-at-heading-p)))
  20889. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20890. org-ctrl-k-protect-subtree)
  20891. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20892. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20893. (user-error "C-k aborted as it would kill a hidden subtree")))
  20894. (call-interactively
  20895. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20896. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20897. (kill-region (point) (match-beginning 1))
  20898. (org-set-tags nil t))
  20899. (t (kill-region (point) (point-at-eol)))))
  20900. (define-key org-mode-map "\C-k" 'org-kill-line)
  20901. (defun org-yank (&optional arg)
  20902. "Yank. If the kill is a subtree, treat it specially.
  20903. This command will look at the current kill and check if is a single
  20904. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20905. cursor is at the beginning of a line or after the stars of a currently
  20906. empty headline, then the yank is handled specially. How exactly depends
  20907. on the value of the following variables, both set by default.
  20908. org-yank-folded-subtrees
  20909. When set, the subtree(s) will be folded after insertion, but only
  20910. if doing so would now swallow text after the yanked text.
  20911. org-yank-adjusted-subtrees
  20912. When set, the subtree will be promoted or demoted in order to
  20913. fit into the local outline tree structure, which means that the level
  20914. will be adjusted so that it becomes the smaller one of the two
  20915. *visible* surrounding headings.
  20916. Any prefix to this command will cause `yank' to be called directly with
  20917. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20918. will just
  20919. plainly yank the text as it is.
  20920. \[1] The test checks if the first non-white line is a heading
  20921. and if there are no other headings with fewer stars."
  20922. (interactive "P")
  20923. (org-yank-generic 'yank arg))
  20924. (defun org-yank-generic (command arg)
  20925. "Perform some yank-like command.
  20926. This function implements the behavior described in the `org-yank'
  20927. documentation. However, it has been generalized to work for any
  20928. interactive command with similar behavior."
  20929. ;; pretend to be command COMMAND
  20930. (setq this-command command)
  20931. (if arg
  20932. (call-interactively command)
  20933. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20934. (and (org-kill-is-subtree-p)
  20935. (or (bolp)
  20936. (and (looking-at "[ \t]*$")
  20937. (string-match
  20938. "\\`\\*+\\'"
  20939. (buffer-substring (point-at-bol) (point)))))))
  20940. swallowp)
  20941. (cond
  20942. ((and subtreep org-yank-folded-subtrees)
  20943. (let ((beg (point))
  20944. end)
  20945. (if (and subtreep org-yank-adjusted-subtrees)
  20946. (org-paste-subtree nil nil 'for-yank)
  20947. (call-interactively command))
  20948. (setq end (point))
  20949. (goto-char beg)
  20950. (when (and (bolp) subtreep
  20951. (not (setq swallowp
  20952. (org-yank-folding-would-swallow-text beg end))))
  20953. (org-with-limited-levels
  20954. (or (looking-at org-outline-regexp)
  20955. (re-search-forward org-outline-regexp-bol end t))
  20956. (while (and (< (point) end) (looking-at org-outline-regexp))
  20957. (hide-subtree)
  20958. (org-cycle-show-empty-lines 'folded)
  20959. (condition-case nil
  20960. (outline-forward-same-level 1)
  20961. (error (goto-char end))))))
  20962. (when swallowp
  20963. (message
  20964. "Inserted text not folded because that would swallow text"))
  20965. (goto-char end)
  20966. (skip-chars-forward " \t\n\r")
  20967. (beginning-of-line 1)
  20968. (push-mark beg 'nomsg)))
  20969. ((and subtreep org-yank-adjusted-subtrees)
  20970. (let ((beg (point-at-bol)))
  20971. (org-paste-subtree nil nil 'for-yank)
  20972. (push-mark beg 'nomsg)))
  20973. (t
  20974. (call-interactively command))))))
  20975. (defun org-yank-folding-would-swallow-text (beg end)
  20976. "Would hide-subtree at BEG swallow any text after END?"
  20977. (let (level)
  20978. (org-with-limited-levels
  20979. (save-excursion
  20980. (goto-char beg)
  20981. (when (or (looking-at org-outline-regexp)
  20982. (re-search-forward org-outline-regexp-bol end t))
  20983. (setq level (org-outline-level)))
  20984. (goto-char end)
  20985. (skip-chars-forward " \t\r\n\v\f")
  20986. (if (or (eobp)
  20987. (and (bolp) (looking-at org-outline-regexp)
  20988. (<= (org-outline-level) level)))
  20989. nil ; Nothing would be swallowed
  20990. t))))) ; something would swallow
  20991. (define-key org-mode-map "\C-y" 'org-yank)
  20992. (defun org-truely-invisible-p ()
  20993. "Check if point is at a character currently not visible.
  20994. This version does not only check the character property, but also
  20995. `visible-mode'."
  20996. ;; Early versions of noutline don't have `outline-invisible-p'.
  20997. (if (org-bound-and-true-p visible-mode)
  20998. nil
  20999. (outline-invisible-p)))
  21000. (defun org-invisible-p2 ()
  21001. "Check if point is at a character currently not visible."
  21002. (save-excursion
  21003. (if (and (eolp) (not (bobp))) (backward-char 1))
  21004. ;; Early versions of noutline don't have `outline-invisible-p'.
  21005. (outline-invisible-p)))
  21006. (defun org-back-to-heading (&optional invisible-ok)
  21007. "Call `outline-back-to-heading', but provide a better error message."
  21008. (condition-case nil
  21009. (outline-back-to-heading invisible-ok)
  21010. (error (error "Before first headline at position %d in buffer %s"
  21011. (point) (current-buffer)))))
  21012. (defun org-before-first-heading-p ()
  21013. "Before first heading?"
  21014. (save-excursion
  21015. (end-of-line)
  21016. (null (re-search-backward org-outline-regexp-bol nil t))))
  21017. (defun org-at-heading-p (&optional ignored)
  21018. (outline-on-heading-p t))
  21019. ;; Compatibility alias with Org versions < 7.8.03
  21020. (defalias 'org-on-heading-p 'org-at-heading-p)
  21021. (defun org-at-comment-p nil
  21022. "Is cursor in a line starting with a # character?"
  21023. (save-excursion
  21024. (beginning-of-line)
  21025. (looking-at "^#")))
  21026. (defun org-at-drawer-p nil
  21027. "Is cursor at a drawer keyword?"
  21028. (save-excursion
  21029. (move-beginning-of-line 1)
  21030. (looking-at org-drawer-regexp)))
  21031. (defun org-at-block-p nil
  21032. "Is cursor at a block keyword?"
  21033. (save-excursion
  21034. (move-beginning-of-line 1)
  21035. (looking-at org-block-regexp)))
  21036. (defun org-point-at-end-of-empty-headline ()
  21037. "If point is at the end of an empty headline, return t, else nil.
  21038. If the heading only contains a TODO keyword, it is still still considered
  21039. empty."
  21040. (and (looking-at "[ \t]*$")
  21041. (when org-todo-line-regexp
  21042. (save-excursion
  21043. (beginning-of-line 1)
  21044. (let ((case-fold-search nil))
  21045. (looking-at org-todo-line-regexp)
  21046. (string= (match-string 3) ""))))))
  21047. (defun org-at-heading-or-item-p ()
  21048. (or (org-at-heading-p) (org-at-item-p)))
  21049. (defun org-at-target-p ()
  21050. (or (org-in-regexp org-radio-target-regexp)
  21051. (org-in-regexp org-target-regexp)))
  21052. ;; Compatibility alias with Org versions < 7.8.03
  21053. (defalias 'org-on-target-p 'org-at-target-p)
  21054. (defun org-up-heading-all (arg)
  21055. "Move to the heading line of which the present line is a subheading.
  21056. This function considers both visible and invisible heading lines.
  21057. With argument, move up ARG levels."
  21058. (if (fboundp 'outline-up-heading-all)
  21059. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21060. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21061. (defun org-up-heading-safe ()
  21062. "Move to the heading line of which the present line is a subheading.
  21063. This version will not throw an error. It will return the level of the
  21064. headline found, or nil if no higher level is found.
  21065. Also, this function will be a lot faster than `outline-up-heading',
  21066. because it relies on stars being the outline starters. This can really
  21067. make a significant difference in outlines with very many siblings."
  21068. (let (start-level re)
  21069. (org-back-to-heading t)
  21070. (setq start-level (funcall outline-level))
  21071. (if (equal start-level 1)
  21072. nil
  21073. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21074. (if (re-search-backward re nil t)
  21075. (funcall outline-level)))))
  21076. (defun org-first-sibling-p ()
  21077. "Is this heading the first child of its parents?"
  21078. (interactive)
  21079. (let ((re org-outline-regexp-bol)
  21080. level l)
  21081. (unless (org-at-heading-p t)
  21082. (user-error "Not at a heading"))
  21083. (setq level (funcall outline-level))
  21084. (save-excursion
  21085. (if (not (re-search-backward re nil t))
  21086. t
  21087. (setq l (funcall outline-level))
  21088. (< l level)))))
  21089. (defun org-goto-sibling (&optional previous)
  21090. "Goto the next sibling, even if it is invisible.
  21091. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21092. when a sibling was found. When none is found, return nil and don't
  21093. move point."
  21094. (let ((fun (if previous 're-search-backward 're-search-forward))
  21095. (pos (point))
  21096. (re org-outline-regexp-bol)
  21097. level l)
  21098. (when (condition-case nil (org-back-to-heading t) (error nil))
  21099. (setq level (funcall outline-level))
  21100. (catch 'exit
  21101. (or previous (forward-char 1))
  21102. (while (funcall fun re nil t)
  21103. (setq l (funcall outline-level))
  21104. (when (< l level) (goto-char pos) (throw 'exit nil))
  21105. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21106. (goto-char pos)
  21107. nil))))
  21108. (defun org-show-siblings ()
  21109. "Show all siblings of the current headline."
  21110. (save-excursion
  21111. (while (org-goto-sibling) (org-flag-heading nil)))
  21112. (save-excursion
  21113. (while (org-goto-sibling 'previous)
  21114. (org-flag-heading nil))))
  21115. (defun org-goto-first-child ()
  21116. "Goto the first child, even if it is invisible.
  21117. Return t when a child was found. Otherwise don't move point and
  21118. return nil."
  21119. (let (level (pos (point)) (re org-outline-regexp-bol))
  21120. (when (condition-case nil (org-back-to-heading t) (error nil))
  21121. (setq level (outline-level))
  21122. (forward-char 1)
  21123. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21124. (progn (goto-char (match-beginning 0)) t)
  21125. (goto-char pos) nil))))
  21126. (defun org-show-hidden-entry ()
  21127. "Show an entry where even the heading is hidden."
  21128. (save-excursion
  21129. (org-show-entry)))
  21130. (defun org-flag-heading (flag &optional entry)
  21131. "Flag the current heading. FLAG non-nil means make invisible.
  21132. When ENTRY is non-nil, show the entire entry."
  21133. (save-excursion
  21134. (org-back-to-heading t)
  21135. ;; Check if we should show the entire entry
  21136. (if entry
  21137. (progn
  21138. (org-show-entry)
  21139. (save-excursion
  21140. (and (outline-next-heading)
  21141. (org-flag-heading nil))))
  21142. (outline-flag-region (max (point-min) (1- (point)))
  21143. (save-excursion (outline-end-of-heading) (point))
  21144. flag))))
  21145. (defun org-get-next-sibling ()
  21146. "Move to next heading of the same level, and return point.
  21147. If there is no such heading, return nil.
  21148. This is like outline-next-sibling, but invisible headings are ok."
  21149. (let ((level (funcall outline-level)))
  21150. (outline-next-heading)
  21151. (while (and (not (eobp)) (> (funcall outline-level) level))
  21152. (outline-next-heading))
  21153. (if (or (eobp) (< (funcall outline-level) level))
  21154. nil
  21155. (point))))
  21156. (defun org-get-last-sibling ()
  21157. "Move to previous heading of the same level, and return point.
  21158. If there is no such heading, return nil."
  21159. (let ((opoint (point))
  21160. (level (funcall outline-level)))
  21161. (outline-previous-heading)
  21162. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21163. (while (and (> (funcall outline-level) level)
  21164. (not (bobp)))
  21165. (outline-previous-heading))
  21166. (if (< (funcall outline-level) level)
  21167. nil
  21168. (point)))))
  21169. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21170. "Goto to the end of a subtree."
  21171. ;; This contains an exact copy of the original function, but it uses
  21172. ;; `org-back-to-heading', to make it work also in invisible
  21173. ;; trees. And is uses an invisible-ok argument.
  21174. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21175. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21176. ;; with many children and grandchildren etc, this can be much faster
  21177. ;; than the outline version.
  21178. (org-back-to-heading invisible-ok)
  21179. (let ((first t)
  21180. (level (funcall outline-level)))
  21181. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21182. ;; A true heading (not a plain list item), in Org-mode
  21183. ;; This means we can easily find the end by looking
  21184. ;; only for the right number of stars. Using a regexp to do
  21185. ;; this is so much faster than using a Lisp loop.
  21186. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21187. (forward-char 1)
  21188. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21189. ;; something else, do it the slow way
  21190. (while (and (not (eobp))
  21191. (or first (> (funcall outline-level) level)))
  21192. (setq first nil)
  21193. (outline-next-heading)))
  21194. (unless to-heading
  21195. (if (memq (preceding-char) '(?\n ?\^M))
  21196. (progn
  21197. ;; Go to end of line before heading
  21198. (forward-char -1)
  21199. (if (memq (preceding-char) '(?\n ?\^M))
  21200. ;; leave blank line before heading
  21201. (forward-char -1))))))
  21202. (point))
  21203. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21204. "Use Org version in org-mode, for dramatic speed-up."
  21205. (if (derived-mode-p 'org-mode)
  21206. (progn
  21207. (org-end-of-subtree nil t)
  21208. (unless (eobp) (backward-char 1)))
  21209. ad-do-it))
  21210. (defun org-end-of-meta-data-and-drawers ()
  21211. "Jump to the first text after meta data and drawers in the current entry.
  21212. This will move over empty lines, lines with planning time stamps,
  21213. clocking lines, and drawers."
  21214. (org-back-to-heading t)
  21215. (let ((end (save-excursion (outline-next-heading) (point)))
  21216. (re (concat "\\(" org-drawer-regexp "\\)"
  21217. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21218. (forward-line 1)
  21219. (while (re-search-forward re end t)
  21220. (if (not (match-end 1))
  21221. ;; empty or planning line
  21222. (forward-line 1)
  21223. ;; a drawer, find the end
  21224. (re-search-forward "^[ \t]*:END:" end 'move)
  21225. (forward-line 1)))
  21226. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21227. (point)))
  21228. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21229. "Move forward to the ARG'th subheading at same level as this one.
  21230. Stop at the first and last subheadings of a superior heading.
  21231. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21232. non-nil it will also look at invisible ones."
  21233. (interactive "p")
  21234. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21235. (if (and arg (< arg 0))
  21236. (goto-char (point-min))
  21237. (outline-next-heading))
  21238. (org-at-heading-p)
  21239. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21240. (f (if (and arg (< arg 0))
  21241. 're-search-backward
  21242. 're-search-forward))
  21243. (count (if arg (abs arg) 1))
  21244. (result (point)))
  21245. (while (and (prog1 (> count 0)
  21246. (forward-char (if (and arg (< arg 0)) -1 1)))
  21247. (funcall f org-outline-regexp-bol nil 'move))
  21248. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21249. (cond ((< l level) (setq count 0))
  21250. ((and (= l level)
  21251. (or invisible-ok
  21252. (progn
  21253. (goto-char (line-beginning-position))
  21254. (not (outline-invisible-p)))))
  21255. (setq count (1- count))
  21256. (when (eq l level)
  21257. (setq result (point)))))))
  21258. (goto-char result))
  21259. (beginning-of-line 1)))
  21260. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21261. "Move backward to the ARG'th subheading at same level as this one.
  21262. Stop at the first and last subheadings of a superior heading."
  21263. (interactive "p")
  21264. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21265. (defun org-next-block (arg &optional backward block-regexp)
  21266. "Jump to the next block.
  21267. With a prefix argument ARG, jump forward ARG many source blocks.
  21268. When BACKWARD is non-nil, jump to the previous block.
  21269. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21270. (interactive "p")
  21271. (let ((re (or block-regexp org-block-regexp))
  21272. (re-search-fn (or (and backward 're-search-backward)
  21273. 're-search-forward)))
  21274. (if (looking-at re) (forward-char 1))
  21275. (condition-case nil
  21276. (funcall re-search-fn re nil nil arg)
  21277. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21278. (goto-char (match-beginning 0)) (org-show-context)))
  21279. (defun org-previous-block (arg &optional block-regexp)
  21280. "Jump to the previous block.
  21281. With a prefix argument ARG, jump backward ARG many source blocks.
  21282. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21283. (interactive "p")
  21284. (org-next-block arg t block-regexp))
  21285. (defun org-forward-element ()
  21286. "Move forward by one element.
  21287. Move to the next element at the same level, when possible."
  21288. (interactive)
  21289. (cond ((eobp) (user-error "Cannot move further down"))
  21290. ((org-with-limited-levels (org-at-heading-p))
  21291. (let ((origin (point)))
  21292. (goto-char (org-end-of-subtree nil t))
  21293. (unless (org-with-limited-levels (org-at-heading-p))
  21294. (goto-char origin)
  21295. (user-error "Cannot move further down"))))
  21296. (t
  21297. (let* ((elem (org-element-at-point))
  21298. (end (org-element-property :end elem))
  21299. (parent (org-element-property :parent elem)))
  21300. (if (and parent (= (org-element-property :contents-end parent) end))
  21301. (goto-char (org-element-property :end parent))
  21302. (goto-char end))))))
  21303. (defun org-backward-element ()
  21304. "Move backward by one element.
  21305. Move to the previous element at the same level, when possible."
  21306. (interactive)
  21307. (cond ((bobp) (user-error "Cannot move further up"))
  21308. ((org-with-limited-levels (org-at-heading-p))
  21309. ;; At a headline, move to the previous one, if any, or stay
  21310. ;; here.
  21311. (let ((origin (point)))
  21312. (org-with-limited-levels (org-backward-heading-same-level 1))
  21313. ;; When current headline has no sibling above, move to its
  21314. ;; parent.
  21315. (when (= (point) origin)
  21316. (or (org-with-limited-levels (org-up-heading-safe))
  21317. (progn (goto-char origin)
  21318. (user-error "Cannot move further up"))))))
  21319. (t
  21320. (let* ((trail (org-element-at-point 'keep-trail))
  21321. (elem (car trail))
  21322. (prev-elem (nth 1 trail))
  21323. (beg (org-element-property :begin elem)))
  21324. (cond
  21325. ;; Move to beginning of current element if point isn't
  21326. ;; there already.
  21327. ((/= (point) beg) (goto-char beg))
  21328. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21329. ((org-before-first-heading-p) (goto-char (point-min)))
  21330. (t (org-back-to-heading)))))))
  21331. (defun org-up-element ()
  21332. "Move to upper element."
  21333. (interactive)
  21334. (if (org-with-limited-levels (org-at-heading-p))
  21335. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21336. (let* ((elem (org-element-at-point))
  21337. (parent (org-element-property :parent elem)))
  21338. (if parent (goto-char (org-element-property :begin parent))
  21339. (if (org-with-limited-levels (org-before-first-heading-p))
  21340. (user-error "No surrounding element")
  21341. (org-with-limited-levels (org-back-to-heading)))))))
  21342. (defvar org-element-greater-elements)
  21343. (defun org-down-element ()
  21344. "Move to inner element."
  21345. (interactive)
  21346. (let ((element (org-element-at-point)))
  21347. (cond
  21348. ((memq (org-element-type element) '(plain-list table))
  21349. (goto-char (org-element-property :contents-begin element))
  21350. (forward-char))
  21351. ((memq (org-element-type element) org-element-greater-elements)
  21352. ;; If contents are hidden, first disclose them.
  21353. (when (org-element-property :hiddenp element) (org-cycle))
  21354. (goto-char (or (org-element-property :contents-begin element)
  21355. (user-error "No content for this element"))))
  21356. (t (user-error "No inner element")))))
  21357. (defun org-drag-element-backward ()
  21358. "Move backward element at point."
  21359. (interactive)
  21360. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21361. (let* ((trail (org-element-at-point 'keep-trail))
  21362. (elem (car trail))
  21363. (prev-elem (nth 1 trail)))
  21364. ;; Error out if no previous element or previous element is
  21365. ;; a parent of the current one.
  21366. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21367. (user-error "Cannot drag element backward")
  21368. (let ((pos (point)))
  21369. (org-element-swap-A-B prev-elem elem)
  21370. (goto-char (+ (org-element-property :begin prev-elem)
  21371. (- pos (org-element-property :begin elem)))))))))
  21372. (defun org-drag-element-forward ()
  21373. "Move forward element at point."
  21374. (interactive)
  21375. (let* ((pos (point))
  21376. (elem (org-element-at-point)))
  21377. (when (= (point-max) (org-element-property :end elem))
  21378. (user-error "Cannot drag element forward"))
  21379. (goto-char (org-element-property :end elem))
  21380. (let ((next-elem (org-element-at-point)))
  21381. (when (or (org-element-nested-p elem next-elem)
  21382. (and (eq (org-element-type next-elem) 'headline)
  21383. (not (eq (org-element-type elem) 'headline))))
  21384. (goto-char pos)
  21385. (user-error "Cannot drag element forward"))
  21386. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21387. ;; body's length (without final blanks) and by the length of
  21388. ;; blanks between ELEM and NEXT-ELEM.
  21389. (let ((size-next (- (save-excursion
  21390. (goto-char (org-element-property :end next-elem))
  21391. (skip-chars-backward " \r\t\n")
  21392. (forward-line)
  21393. ;; Small correction if buffer doesn't end
  21394. ;; with a newline character.
  21395. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21396. (org-element-property :begin next-elem)))
  21397. (size-blank (- (org-element-property :end elem)
  21398. (save-excursion
  21399. (goto-char (org-element-property :end elem))
  21400. (skip-chars-backward " \r\t\n")
  21401. (forward-line)
  21402. (point)))))
  21403. (org-element-swap-A-B elem next-elem)
  21404. (goto-char (+ pos size-next size-blank))))))
  21405. (defun org-drag-line-forward (arg)
  21406. "Drag the line at point ARG lines forward."
  21407. (interactive "p")
  21408. (dotimes (n (abs arg))
  21409. (let ((c (current-column)))
  21410. (if (< 0 arg)
  21411. (progn
  21412. (beginning-of-line 2)
  21413. (transpose-lines 1)
  21414. (beginning-of-line 0))
  21415. (transpose-lines 1)
  21416. (beginning-of-line -1))
  21417. (org-move-to-column c))))
  21418. (defun org-drag-line-backward (arg)
  21419. "Drag the line at point ARG lines backward."
  21420. (interactive "p")
  21421. (org-drag-line-forward (- arg)))
  21422. (defun org-mark-element ()
  21423. "Put point at beginning of this element, mark at end.
  21424. Interactively, if this command is repeated or (in Transient Mark
  21425. mode) if the mark is active, it marks the next element after the
  21426. ones already marked."
  21427. (interactive)
  21428. (let (deactivate-mark)
  21429. (if (and (org-called-interactively-p 'any)
  21430. (or (and (eq last-command this-command) (mark t))
  21431. (and transient-mark-mode mark-active)))
  21432. (set-mark
  21433. (save-excursion
  21434. (goto-char (mark))
  21435. (goto-char (org-element-property :end (org-element-at-point)))))
  21436. (let ((element (org-element-at-point)))
  21437. (end-of-line)
  21438. (push-mark (org-element-property :end element) t t)
  21439. (goto-char (org-element-property :begin element))))))
  21440. (defun org-narrow-to-element ()
  21441. "Narrow buffer to current element."
  21442. (interactive)
  21443. (let ((elem (org-element-at-point)))
  21444. (cond
  21445. ((eq (car elem) 'headline)
  21446. (narrow-to-region
  21447. (org-element-property :begin elem)
  21448. (org-element-property :end elem)))
  21449. ((memq (car elem) org-element-greater-elements)
  21450. (narrow-to-region
  21451. (org-element-property :contents-begin elem)
  21452. (org-element-property :contents-end elem)))
  21453. (t
  21454. (narrow-to-region
  21455. (org-element-property :begin elem)
  21456. (org-element-property :end elem))))))
  21457. (defun org-transpose-element ()
  21458. "Transpose current and previous elements, keeping blank lines between.
  21459. Point is moved after both elements."
  21460. (interactive)
  21461. (org-skip-whitespace)
  21462. (let ((end (org-element-property :end (org-element-at-point))))
  21463. (org-drag-element-backward)
  21464. (goto-char end)))
  21465. (defun org-unindent-buffer ()
  21466. "Un-indent the visible part of the buffer.
  21467. Relative indentation (between items, inside blocks, etc.) isn't
  21468. modified."
  21469. (interactive)
  21470. (unless (eq major-mode 'org-mode)
  21471. (user-error "Cannot un-indent a buffer not in Org mode"))
  21472. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21473. unindent-tree ; For byte-compiler.
  21474. (unindent-tree
  21475. (function
  21476. (lambda (contents)
  21477. (mapc
  21478. (lambda (element)
  21479. (if (memq (org-element-type element) '(headline section))
  21480. (funcall unindent-tree (org-element-contents element))
  21481. (save-excursion
  21482. (save-restriction
  21483. (narrow-to-region
  21484. (org-element-property :begin element)
  21485. (org-element-property :end element))
  21486. (org-do-remove-indentation)))))
  21487. (reverse contents))))))
  21488. (funcall unindent-tree (org-element-contents parse-tree))))
  21489. (defun org-show-subtree ()
  21490. "Show everything after this heading at deeper levels."
  21491. (interactive)
  21492. (outline-flag-region
  21493. (point)
  21494. (save-excursion
  21495. (org-end-of-subtree t t))
  21496. nil))
  21497. (defun org-show-entry ()
  21498. "Show the body directly following this heading.
  21499. Show the heading too, if it is currently invisible."
  21500. (interactive)
  21501. (save-excursion
  21502. (condition-case nil
  21503. (progn
  21504. (org-back-to-heading t)
  21505. (outline-flag-region
  21506. (max (point-min) (1- (point)))
  21507. (save-excursion
  21508. (if (re-search-forward
  21509. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21510. (match-beginning 1)
  21511. (point-max)))
  21512. nil)
  21513. (org-cycle-hide-drawers 'children))
  21514. (error nil))))
  21515. (defun org-make-options-regexp (kwds &optional extra)
  21516. "Make a regular expression for keyword lines."
  21517. (concat
  21518. "^#\\+\\("
  21519. (mapconcat 'regexp-quote kwds "\\|")
  21520. (if extra (concat "\\|" extra))
  21521. "\\):[ \t]*\\(.*\\)"))
  21522. ;; Make isearch reveal the necessary context
  21523. (defun org-isearch-end ()
  21524. "Reveal context after isearch exits."
  21525. (when isearch-success ; only if search was successful
  21526. (if (featurep 'xemacs)
  21527. ;; Under XEmacs, the hook is run in the correct place,
  21528. ;; we directly show the context.
  21529. (org-show-context 'isearch)
  21530. ;; In Emacs the hook runs *before* restoring the overlays.
  21531. ;; So we have to use a one-time post-command-hook to do this.
  21532. ;; (Emacs 22 has a special variable, see function `org-mode')
  21533. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21534. isearch-mode-end-hook-quit)
  21535. ;; Only when the isearch was not quitted.
  21536. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21537. 'append 'local)))
  21538. (org-fix-ellipsis-at-bol)))
  21539. (defun org-isearch-post-command ()
  21540. "Remove self from hook, and show context."
  21541. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21542. (org-show-context 'isearch))
  21543. ;;;; Integration with and fixes for other packages
  21544. ;;; Imenu support
  21545. (defvar org-imenu-markers nil
  21546. "All markers currently used by Imenu.")
  21547. (make-variable-buffer-local 'org-imenu-markers)
  21548. (defun org-imenu-new-marker (&optional pos)
  21549. "Return a new marker for use by Imenu, and remember the marker."
  21550. (let ((m (make-marker)))
  21551. (move-marker m (or pos (point)))
  21552. (push m org-imenu-markers)
  21553. m))
  21554. (defun org-imenu-get-tree ()
  21555. "Produce the index for Imenu."
  21556. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21557. (setq org-imenu-markers nil)
  21558. (let* ((n org-imenu-depth)
  21559. (re (concat "^" (org-get-limited-outline-regexp)))
  21560. (subs (make-vector (1+ n) nil))
  21561. (last-level 0)
  21562. m level head0 head)
  21563. (save-excursion
  21564. (save-restriction
  21565. (widen)
  21566. (goto-char (point-max))
  21567. (while (re-search-backward re nil t)
  21568. (setq level (org-reduced-level (funcall outline-level)))
  21569. (when (and (<= level n)
  21570. (looking-at org-complex-heading-regexp)
  21571. (setq head0 (org-match-string-no-properties 4)))
  21572. (setq head (org-link-display-format head0)
  21573. m (org-imenu-new-marker))
  21574. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21575. (if (>= level last-level)
  21576. (push (cons head m) (aref subs level))
  21577. (push (cons head (aref subs (1+ level))) (aref subs level))
  21578. (loop for i from (1+ level) to n do (aset subs i nil)))
  21579. (setq last-level level)))))
  21580. (aref subs 1)))
  21581. (eval-after-load "imenu"
  21582. '(progn
  21583. (add-hook 'imenu-after-jump-hook
  21584. (lambda ()
  21585. (if (derived-mode-p 'org-mode)
  21586. (org-show-context 'org-goto))))))
  21587. (defun org-link-display-format (link)
  21588. "Replace a link with its the description.
  21589. If there is no description, use the link target."
  21590. (save-match-data
  21591. (if (string-match org-bracket-link-analytic-regexp link)
  21592. (replace-match (if (match-end 5)
  21593. (match-string 5 link)
  21594. (concat (match-string 1 link)
  21595. (match-string 3 link)))
  21596. nil t link)
  21597. link)))
  21598. (defun org-toggle-link-display ()
  21599. "Toggle the literal or descriptive display of links."
  21600. (interactive)
  21601. (if org-descriptive-links
  21602. (progn (org-remove-from-invisibility-spec '(org-link))
  21603. (org-restart-font-lock)
  21604. (setq org-descriptive-links nil))
  21605. (progn (add-to-invisibility-spec '(org-link))
  21606. (org-restart-font-lock)
  21607. (setq org-descriptive-links t))))
  21608. ;; Speedbar support
  21609. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21610. "Overlay marking the agenda restriction line in speedbar.")
  21611. (overlay-put org-speedbar-restriction-lock-overlay
  21612. 'face 'org-agenda-restriction-lock)
  21613. (overlay-put org-speedbar-restriction-lock-overlay
  21614. 'help-echo "Agendas are currently limited to this item.")
  21615. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21616. (defun org-speedbar-set-agenda-restriction ()
  21617. "Restrict future agenda commands to the location at point in speedbar.
  21618. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21619. (interactive)
  21620. (require 'org-agenda)
  21621. (let (p m tp np dir txt)
  21622. (cond
  21623. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21624. 'org-imenu t))
  21625. (setq m (get-text-property p 'org-imenu-marker))
  21626. (with-current-buffer (marker-buffer m)
  21627. (goto-char m)
  21628. (org-agenda-set-restriction-lock 'subtree)))
  21629. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21630. 'speedbar-function 'speedbar-find-file))
  21631. (setq tp (previous-single-property-change
  21632. (1+ p) 'speedbar-function)
  21633. np (next-single-property-change
  21634. tp 'speedbar-function)
  21635. dir (speedbar-line-directory)
  21636. txt (buffer-substring-no-properties (or tp (point-min))
  21637. (or np (point-max))))
  21638. (with-current-buffer (find-file-noselect
  21639. (let ((default-directory dir))
  21640. (expand-file-name txt)))
  21641. (unless (derived-mode-p 'org-mode)
  21642. (user-error "Cannot restrict to non-Org-mode file"))
  21643. (org-agenda-set-restriction-lock 'file)))
  21644. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21645. (move-overlay org-speedbar-restriction-lock-overlay
  21646. (point-at-bol) (point-at-eol))
  21647. (setq current-prefix-arg nil)
  21648. (org-agenda-maybe-redo)))
  21649. (defvar speedbar-file-key-map)
  21650. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  21651. (eval-after-load "speedbar"
  21652. '(progn
  21653. (speedbar-add-supported-extension ".org")
  21654. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21655. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21656. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21657. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21658. (add-hook 'speedbar-visiting-tag-hook
  21659. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21660. ;;; Fixes and Hacks for problems with other packages
  21661. ;; Make flyspell not check words in links, to not mess up our keymap
  21662. (defvar org-element-affiliated-keywords) ; From org-element.el
  21663. (defvar org-element-block-name-alist) ; From org-element.el
  21664. (defun org-mode-flyspell-verify ()
  21665. "Don't let flyspell put overlays at active buttons, or on
  21666. {todo,all-time,additional-option-like}-keywords."
  21667. (require 'org-element) ; For `org-element-affiliated-keywords'
  21668. (let ((pos (max (1- (point)) (point-min)))
  21669. (word (thing-at-point 'word)))
  21670. (and (not (get-text-property pos 'keymap))
  21671. (not (get-text-property pos 'org-no-flyspell))
  21672. (not (member word org-todo-keywords-1))
  21673. (not (member word org-all-time-keywords))
  21674. (not (member word org-options-keywords))
  21675. (not (member word (mapcar 'car org-startup-options)))
  21676. (not (member-ignore-case word org-element-affiliated-keywords))
  21677. (not (member-ignore-case word (org-get-export-keywords)))
  21678. (not (member-ignore-case
  21679. word (mapcar 'car org-element-block-name-alist)))
  21680. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21681. (defun org-remove-flyspell-overlays-in (beg end)
  21682. "Remove flyspell overlays in region."
  21683. (and (org-bound-and-true-p flyspell-mode)
  21684. (fboundp 'flyspell-delete-region-overlays)
  21685. (flyspell-delete-region-overlays beg end))
  21686. (add-text-properties beg end '(org-no-flyspell t)))
  21687. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21688. (eval-after-load "bookmark"
  21689. '(if (boundp 'bookmark-after-jump-hook)
  21690. ;; We can use the hook
  21691. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21692. ;; Hook not available, use advice
  21693. (defadvice bookmark-jump (after org-make-visible activate)
  21694. "Make the position visible."
  21695. (org-bookmark-jump-unhide))))
  21696. ;; Make sure saveplace shows the location if it was hidden
  21697. (eval-after-load "saveplace"
  21698. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21699. "Make the position visible."
  21700. (org-bookmark-jump-unhide)))
  21701. ;; Make sure ecb shows the location if it was hidden
  21702. (eval-after-load "ecb"
  21703. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21704. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21705. (if (derived-mode-p 'org-mode)
  21706. (org-show-context))))
  21707. (defun org-bookmark-jump-unhide ()
  21708. "Unhide the current position, to show the bookmark location."
  21709. (and (derived-mode-p 'org-mode)
  21710. (or (outline-invisible-p)
  21711. (save-excursion (goto-char (max (point-min) (1- (point))))
  21712. (outline-invisible-p)))
  21713. (org-show-context 'bookmark-jump)))
  21714. ;; Make session.el ignore our circular variable
  21715. (defvar session-globals-exclude)
  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