org.el 893 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618
  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. ("" "soul" 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. - graphicx: for including images
  3343. - float, wrapfig: for figure placement
  3344. - longtable: for long tables
  3345. - hyperref: for cross references
  3346. Therefore you should not modify this variable unless you know
  3347. what you are doing. The one reason to change it anyway is that
  3348. you might be loading some other package that conflicts with one
  3349. of the default packages. Each cell is of the format
  3350. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3351. the package also needs to be included when compiling LaTeX
  3352. snippets into images for inclusion into non-LaTeX output."
  3353. :group 'org-latex
  3354. :group 'org-export-latex
  3355. :set 'org-set-packages-alist
  3356. :get 'org-get-packages-alist
  3357. :version "24.1"
  3358. :type '(repeat
  3359. (choice
  3360. (list :tag "options/package pair"
  3361. (string :tag "options")
  3362. (string :tag "package")
  3363. (boolean :tag "Snippet"))
  3364. (string :tag "A line of LaTeX"))))
  3365. (defcustom org-latex-packages-alist nil
  3366. "Alist of packages to be inserted in every LaTeX header.
  3367. These will be inserted after `org-latex-default-packages-alist'.
  3368. Each cell is of the format:
  3369. \(\"options\" \"package\" snippet-flag)
  3370. SNIPPET-FLAG, when t, indicates that this package is also needed
  3371. when turning LaTeX snippets into images for inclusion into
  3372. non-LaTeX output.
  3373. Make sure that you only list packages here which:
  3374. - you want in every file
  3375. - do not conflict with the setup in `org-format-latex-header'.
  3376. - do not conflict with the default packages in
  3377. `org-latex-default-packages-alist'."
  3378. :group 'org-latex
  3379. :group 'org-export-latex
  3380. :set 'org-set-packages-alist
  3381. :get 'org-get-packages-alist
  3382. :type '(repeat
  3383. (choice
  3384. (list :tag "options/package pair"
  3385. (string :tag "options")
  3386. (string :tag "package")
  3387. (boolean :tag "Snippet"))
  3388. (string :tag "A line of LaTeX"))))
  3389. (defgroup org-appearance nil
  3390. "Settings for Org-mode appearance."
  3391. :tag "Org Appearance"
  3392. :group 'org)
  3393. (defcustom org-level-color-stars-only nil
  3394. "Non-nil means fontify only the stars in each headline.
  3395. When nil, the entire headline is fontified.
  3396. Changing it requires restart of `font-lock-mode' to become effective
  3397. also in regions already fontified."
  3398. :group 'org-appearance
  3399. :type 'boolean)
  3400. (defcustom org-hide-leading-stars nil
  3401. "Non-nil means hide the first N-1 stars in a headline.
  3402. This works by using the face `org-hide' for these stars. This
  3403. face is white for a light background, and black for a dark
  3404. background. You may have to customize the face `org-hide' to
  3405. make this work.
  3406. Changing it requires restart of `font-lock-mode' to become effective
  3407. also in regions already fontified.
  3408. You may also set this on a per-file basis by adding one of the following
  3409. lines to the buffer:
  3410. #+STARTUP: hidestars
  3411. #+STARTUP: showstars"
  3412. :group 'org-appearance
  3413. :type 'boolean)
  3414. (defcustom org-hidden-keywords nil
  3415. "List of symbols corresponding to keywords to be hidden the org buffer.
  3416. For example, a value '(title) for this list will make the document's title
  3417. appear in the buffer without the initial #+TITLE: keyword."
  3418. :group 'org-appearance
  3419. :version "24.1"
  3420. :type '(set (const :tag "#+AUTHOR" author)
  3421. (const :tag "#+DATE" date)
  3422. (const :tag "#+EMAIL" email)
  3423. (const :tag "#+TITLE" title)))
  3424. (defcustom org-custom-properties nil
  3425. "List of properties (as strings) with a special meaning.
  3426. The default use of these custom properties is to let the user
  3427. hide them with `org-toggle-custom-properties-visibility'."
  3428. :group 'org-properties
  3429. :group 'org-appearance
  3430. :version "24.3"
  3431. :type '(repeat (string :tag "Property Name")))
  3432. (defcustom org-fontify-done-headline nil
  3433. "Non-nil means change the face of a headline if it is marked DONE.
  3434. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3435. When this is non-nil, the headline after the keyword is set to the
  3436. `org-headline-done' as an additional indication."
  3437. :group 'org-appearance
  3438. :type 'boolean)
  3439. (defcustom org-fontify-emphasized-text t
  3440. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3441. Changing this variable requires a restart of Emacs to take effect."
  3442. :group 'org-appearance
  3443. :type 'boolean)
  3444. (defcustom org-fontify-whole-heading-line nil
  3445. "Non-nil means fontify the whole line for headings.
  3446. This is useful when setting a background color for the
  3447. org-level-* faces."
  3448. :group 'org-appearance
  3449. :type 'boolean)
  3450. (defcustom org-highlight-latex-and-related nil
  3451. "Non-nil means highlight LaTeX related syntax in the buffer.
  3452. When non nil, the value should be a list containing any of the
  3453. following symbols:
  3454. `latex' Highlight LaTeX snippets and environments.
  3455. `script' Highlight subscript and superscript.
  3456. `entities' Highlight entities."
  3457. :group 'org-appearance
  3458. :version "24.4"
  3459. :package-version '(Org . "8.0")
  3460. :type '(choice
  3461. (const :tag "No highlighting" nil)
  3462. (set :greedy t :tag "Highlight"
  3463. (const :tag "LaTeX snippets and environments" latex)
  3464. (const :tag "Subscript and superscript" script)
  3465. (const :tag "Entities" entities))))
  3466. (defcustom org-hide-emphasis-markers nil
  3467. "Non-nil mean font-lock should hide the emphasis marker characters."
  3468. :group 'org-appearance
  3469. :type 'boolean)
  3470. (defcustom org-pretty-entities nil
  3471. "Non-nil means show entities as UTF8 characters.
  3472. When nil, the \\name form remains in the buffer."
  3473. :group 'org-appearance
  3474. :version "24.1"
  3475. :type 'boolean)
  3476. (defcustom org-pretty-entities-include-sub-superscripts t
  3477. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3478. :group 'org-appearance
  3479. :version "24.1"
  3480. :type 'boolean)
  3481. (defvar org-emph-re nil
  3482. "Regular expression for matching emphasis.
  3483. After a match, the match groups contain these elements:
  3484. 0 The match of the full regular expression, including the characters
  3485. before and after the proper match
  3486. 1 The character before the proper match, or empty at beginning of line
  3487. 2 The proper match, including the leading and trailing markers
  3488. 3 The leading marker like * or /, indicating the type of highlighting
  3489. 4 The text between the emphasis markers, not including the markers
  3490. 5 The character after the match, empty at the end of a line")
  3491. (defvar org-verbatim-re nil
  3492. "Regular expression for matching verbatim text.")
  3493. (defvar org-emphasis-regexp-components) ; defined just below
  3494. (defvar org-emphasis-alist) ; defined just below
  3495. (defun org-set-emph-re (var val)
  3496. "Set variable and compute the emphasis regular expression."
  3497. (set var val)
  3498. (when (and (boundp 'org-emphasis-alist)
  3499. (boundp 'org-emphasis-regexp-components)
  3500. org-emphasis-alist org-emphasis-regexp-components)
  3501. (let* ((e org-emphasis-regexp-components)
  3502. (pre (car e))
  3503. (post (nth 1 e))
  3504. (border (nth 2 e))
  3505. (body (nth 3 e))
  3506. (nl (nth 4 e))
  3507. (body1 (concat body "*?"))
  3508. (markers (mapconcat 'car org-emphasis-alist ""))
  3509. (vmarkers (mapconcat
  3510. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3511. org-emphasis-alist "")))
  3512. ;; make sure special characters appear at the right position in the class
  3513. (if (string-match "\\^" markers)
  3514. (setq markers (concat (replace-match "" t t markers) "^")))
  3515. (if (string-match "-" markers)
  3516. (setq markers (concat (replace-match "" t t markers) "-")))
  3517. (if (string-match "\\^" vmarkers)
  3518. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3519. (if (string-match "-" vmarkers)
  3520. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3521. (if (> nl 0)
  3522. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3523. (int-to-string nl) "\\}")))
  3524. ;; Make the regexp
  3525. (setq org-emph-re
  3526. (concat "\\([" pre "]\\|^\\)"
  3527. "\\("
  3528. "\\([" markers "]\\)"
  3529. "\\("
  3530. "[^" border "]\\|"
  3531. "[^" border "]"
  3532. body1
  3533. "[^" border "]"
  3534. "\\)"
  3535. "\\3\\)"
  3536. "\\([" post "]\\|$\\)"))
  3537. (setq org-verbatim-re
  3538. (concat "\\([" pre "]\\|^\\)"
  3539. "\\("
  3540. "\\([" vmarkers "]\\)"
  3541. "\\("
  3542. "[^" border "]\\|"
  3543. "[^" border "]"
  3544. body1
  3545. "[^" border "]"
  3546. "\\)"
  3547. "\\3\\)"
  3548. "\\([" post "]\\|$\\)")))))
  3549. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3550. ;; set this option proved cumbersome. See this message/thread:
  3551. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3552. (defvar org-emphasis-regexp-components
  3553. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3554. "Components used to build the regular expression for emphasis.
  3555. This is a list with five entries. Terminology: In an emphasis string
  3556. like \" *strong word* \", we call the initial space PREMATCH, the final
  3557. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3558. and \"trong wor\" is the body. The different components in this variable
  3559. specify what is allowed/forbidden in each part:
  3560. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3561. post Chars allowed as postmatch. End of line will be allowed too.
  3562. border The chars *forbidden* as border characters.
  3563. body-regexp A regexp like \".\" to match a body character. Don't use
  3564. non-shy groups here, and don't allow newline here.
  3565. newline The maximum number of newlines allowed in an emphasis exp.
  3566. You need to reload Org or to restart Emacs after customizing this.")
  3567. (defcustom org-emphasis-alist
  3568. `(("*" bold)
  3569. ("/" italic)
  3570. ("_" underline)
  3571. ("=" org-code verbatim)
  3572. ("~" org-verbatim verbatim)
  3573. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3574. "Alist of characters and faces to emphasize text.
  3575. Text starting and ending with a special character will be emphasized,
  3576. for example *bold*, _underlined_ and /italic/. This variable sets the
  3577. marker characters and the face to be used by font-lock for highlighting
  3578. in Org-mode Emacs buffers.
  3579. You need to reload Org or to restart Emacs after customizing this."
  3580. :group 'org-appearance
  3581. :set 'org-set-emph-re
  3582. :version "24.4"
  3583. :package-version '(Org . "8.0")
  3584. :type '(repeat
  3585. (list
  3586. (string :tag "Marker character")
  3587. (choice
  3588. (face :tag "Font-lock-face")
  3589. (plist :tag "Face property list"))
  3590. (option (const verbatim)))))
  3591. (defvar org-protecting-blocks
  3592. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3593. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3594. This is needed for font-lock setup.")
  3595. ;;; Miscellaneous options
  3596. (defgroup org-completion nil
  3597. "Completion in Org-mode."
  3598. :tag "Org Completion"
  3599. :group 'org)
  3600. (defcustom org-completion-use-ido nil
  3601. "Non-nil means use ido completion wherever possible.
  3602. Note that `ido-mode' must be active for this variable to be relevant.
  3603. If you decide to turn this variable on, you might well want to turn off
  3604. `org-outline-path-complete-in-steps'.
  3605. See also `org-completion-use-iswitchb'."
  3606. :group 'org-completion
  3607. :type 'boolean)
  3608. (defcustom org-completion-use-iswitchb nil
  3609. "Non-nil means use iswitchb completion wherever possible.
  3610. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3611. If you decide to turn this variable on, you might well want to turn off
  3612. `org-outline-path-complete-in-steps'.
  3613. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3614. :group 'org-completion
  3615. :type 'boolean)
  3616. (defcustom org-completion-fallback-command 'hippie-expand
  3617. "The expansion command called by \\[pcomplete] in normal context.
  3618. Normal means, no org-mode-specific context."
  3619. :group 'org-completion
  3620. :type 'function)
  3621. ;;; Functions and variables from their packages
  3622. ;; Declared here to avoid compiler warnings
  3623. ;; XEmacs only
  3624. (defvar outline-mode-menu-heading)
  3625. (defvar outline-mode-menu-show)
  3626. (defvar outline-mode-menu-hide)
  3627. (defvar zmacs-regions) ; XEmacs regions
  3628. ;; Emacs only
  3629. (defvar mark-active)
  3630. ;; Various packages
  3631. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3632. (declare-function calendar-forward-day "cal-move" (arg))
  3633. (declare-function calendar-goto-date "cal-move" (date))
  3634. (declare-function calendar-goto-today "cal-move" ())
  3635. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3636. (defvar calc-embedded-close-formula)
  3637. (defvar calc-embedded-open-formula)
  3638. (declare-function cdlatex-tab "ext:cdlatex" ())
  3639. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3640. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3641. (defvar font-lock-unfontify-region-function)
  3642. (declare-function iswitchb-read-buffer "iswitchb"
  3643. (prompt &optional default require-match start matches-set))
  3644. (defvar iswitchb-temp-buflist)
  3645. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3646. (defvar org-agenda-tags-todo-honor-ignore-options)
  3647. (declare-function org-agenda-skip "org-agenda" ())
  3648. (declare-function
  3649. org-agenda-format-item "org-agenda"
  3650. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3651. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3652. (declare-function org-agenda-change-all-lines "org-agenda"
  3653. (newhead hdmarker &optional fixface just-this))
  3654. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3655. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3656. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3657. (beg end))
  3658. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3659. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3660. "org-agenda" (&optional end))
  3661. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3662. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3663. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3664. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3665. (declare-function org-indent-mode "org-indent" (&optional arg))
  3666. (declare-function parse-time-string "parse-time" (string))
  3667. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3668. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3669. (defvar remember-data-file)
  3670. (defvar texmathp-why)
  3671. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3672. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3673. (defvar org-latex-regexps)
  3674. ;;; Autoload and prepare some org modules
  3675. ;; Some table stuff that needs to be defined here, because it is used
  3676. ;; by the functions setting up org-mode or checking for table context.
  3677. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3678. "Detect an org-type or table-type table.")
  3679. (defconst org-table-line-regexp "^[ \t]*|"
  3680. "Detect an org-type table line.")
  3681. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3682. "Detect an org-type table line.")
  3683. (defconst org-table-hline-regexp "^[ \t]*|-"
  3684. "Detect an org-type table hline.")
  3685. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3686. "Detect a table-type table hline.")
  3687. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3688. "Detect the first line outside a table when searching from within it.
  3689. This works for both table types.")
  3690. ;; Autoload the functions in org-table.el that are needed by functions here.
  3691. (eval-and-compile
  3692. (org-autoload "org-table"
  3693. '(org-table-begin org-table-blank-field org-table-end)))
  3694. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3695. "Detect a #+TBLFM line.")
  3696. ;;;###autoload
  3697. (defun turn-on-orgtbl ()
  3698. "Unconditionally turn on `orgtbl-mode'."
  3699. (require 'org-table)
  3700. (orgtbl-mode 1))
  3701. (defun org-at-table-p (&optional table-type)
  3702. "Return t if the cursor is inside an org-type table.
  3703. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3704. (if org-enable-table-editor
  3705. (save-excursion
  3706. (beginning-of-line 1)
  3707. (looking-at (if table-type org-table-any-line-regexp
  3708. org-table-line-regexp)))
  3709. nil))
  3710. (defsubst org-table-p () (org-at-table-p))
  3711. (defun org-at-table.el-p ()
  3712. "Return t if and only if we are at a table.el table."
  3713. (and (org-at-table-p 'any)
  3714. (save-excursion
  3715. (goto-char (org-table-begin 'any))
  3716. (looking-at org-table1-hline-regexp))))
  3717. (defun org-table-recognize-table.el ()
  3718. "If there is a table.el table nearby, recognize it and move into it."
  3719. (if org-table-tab-recognizes-table.el
  3720. (if (org-at-table.el-p)
  3721. (progn
  3722. (beginning-of-line 1)
  3723. (if (looking-at org-table-dataline-regexp)
  3724. nil
  3725. (if (looking-at org-table1-hline-regexp)
  3726. (progn
  3727. (beginning-of-line 2)
  3728. (if (looking-at org-table-any-border-regexp)
  3729. (beginning-of-line -1)))))
  3730. (if (re-search-forward "|" (org-table-end t) t)
  3731. (progn
  3732. (require 'table)
  3733. (if (table--at-cell-p (point))
  3734. t
  3735. (message "recognizing table.el table...")
  3736. (table-recognize-table)
  3737. (message "recognizing table.el table...done")))
  3738. (error "This should not happen"))
  3739. t)
  3740. nil)
  3741. nil))
  3742. (defun org-at-table-hline-p ()
  3743. "Return t if the cursor is inside a hline in a table."
  3744. (if org-enable-table-editor
  3745. (save-excursion
  3746. (beginning-of-line 1)
  3747. (looking-at org-table-hline-regexp))
  3748. nil))
  3749. (defvar org-table-clean-did-remove-column nil)
  3750. (defun org-table-map-tables (function &optional quietly)
  3751. "Apply FUNCTION to the start of all tables in the buffer."
  3752. (save-excursion
  3753. (save-restriction
  3754. (widen)
  3755. (goto-char (point-min))
  3756. (while (re-search-forward org-table-any-line-regexp nil t)
  3757. (unless quietly
  3758. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3759. (beginning-of-line 1)
  3760. (when (and (looking-at org-table-line-regexp)
  3761. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3762. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3763. (save-excursion (funcall function))
  3764. (or (looking-at org-table-line-regexp)
  3765. (forward-char 1)))
  3766. (re-search-forward org-table-any-border-regexp nil 1))))
  3767. (unless quietly (message "Mapping tables: done")))
  3768. ;; Declare and autoload functions from ox.el and al.
  3769. (declare-function org-export-get-environment "ox"
  3770. (&optional backend subtreep ext-plist))
  3771. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3772. ;; Declare and autoload functions from org-agenda.el
  3773. (eval-and-compile
  3774. (org-autoload "org-agenda"
  3775. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3776. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3777. (declare-function org-clock-update-mode-line "org-clock" ())
  3778. (declare-function org-resolve-clocks "org-clock"
  3779. (&optional also-non-dangling-p prompt last-valid))
  3780. (defun org-at-TBLFM-p (&optional pos)
  3781. "Return t when point (or POS) is in #+TBLFM line."
  3782. (save-excursion
  3783. (let ((pos pos)))
  3784. (goto-char (or pos (point)))
  3785. (beginning-of-line 1)
  3786. (looking-at org-TBLFM-regexp)))
  3787. (defvar org-clock-start-time)
  3788. (defvar org-clock-marker (make-marker)
  3789. "Marker recording the last clock-in.")
  3790. (defvar org-clock-hd-marker (make-marker)
  3791. "Marker recording the last clock-in, but the headline position.")
  3792. (defvar org-clock-heading ""
  3793. "The heading of the current clock entry.")
  3794. (defun org-clock-is-active ()
  3795. "Return non-nil if clock is currently running.
  3796. The return value is actually the clock marker."
  3797. (marker-buffer org-clock-marker))
  3798. (eval-and-compile
  3799. (org-autoload "org-clock" '(org-clock-remove-overlays
  3800. org-clock-update-time-maybe
  3801. org-clocktable-shift)))
  3802. (defun org-check-running-clock ()
  3803. "Check if the current buffer contains the running clock.
  3804. If yes, offer to stop it and to save the buffer with the changes."
  3805. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3806. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3807. (buffer-name))))
  3808. (org-clock-out)
  3809. (when (y-or-n-p "Save changed buffer?")
  3810. (save-buffer))))
  3811. (defun org-clocktable-try-shift (dir n)
  3812. "Check if this line starts a clock table, if yes, shift the time block."
  3813. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3814. (org-clocktable-shift dir n)))
  3815. ;;;###autoload
  3816. (defun org-clock-persistence-insinuate ()
  3817. "Set up hooks for clock persistence."
  3818. (require 'org-clock)
  3819. (add-hook 'org-mode-hook 'org-clock-load)
  3820. (add-hook 'kill-emacs-hook 'org-clock-save))
  3821. ;; Define the variable already here, to make sure we have it.
  3822. (defvar org-indent-mode nil
  3823. "Non-nil if Org-Indent mode is enabled.
  3824. Use the command `org-indent-mode' to change this variable.")
  3825. ;; Autoload archiving code
  3826. ;; The stuff that is needed for cycling and tags has to be defined here.
  3827. (defgroup org-archive nil
  3828. "Options concerning archiving in Org-mode."
  3829. :tag "Org Archive"
  3830. :group 'org-structure)
  3831. (defcustom org-archive-location "%s_archive::"
  3832. "The location where subtrees should be archived.
  3833. The value of this variable is a string, consisting of two parts,
  3834. separated by a double-colon. The first part is a filename and
  3835. the second part is a headline.
  3836. When the filename is omitted, archiving happens in the same file.
  3837. %s in the filename will be replaced by the current file
  3838. name (without the directory part). Archiving to a different file
  3839. is useful to keep archived entries from contributing to the
  3840. Org-mode Agenda.
  3841. The archived entries will be filed as subtrees of the specified
  3842. headline. When the headline is omitted, the subtrees are simply
  3843. filed away at the end of the file, as top-level entries. Also in
  3844. the heading you can use %s to represent the file name, this can be
  3845. useful when using the same archive for a number of different files.
  3846. Here are a few examples:
  3847. \"%s_archive::\"
  3848. If the current file is Projects.org, archive in file
  3849. Projects.org_archive, as top-level trees. This is the default.
  3850. \"::* Archived Tasks\"
  3851. Archive in the current file, under the top-level headline
  3852. \"* Archived Tasks\".
  3853. \"~/org/archive.org::\"
  3854. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3855. \"~/org/archive.org::* From %s\"
  3856. Archive in file ~/org/archive.org (absolute path), under headlines
  3857. \"From FILENAME\" where file name is the current file name.
  3858. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3859. The \"datetree/\" string is special, signifying to archive
  3860. items to the datetree. Items are placed in either the CLOSED
  3861. date of the item, or the current date if there is no CLOSED date.
  3862. The heading will be a subentry to the current date. There doesn't
  3863. need to be a heading, but there always needs to be a slash after
  3864. datetree. For example, to store archived items directly in the
  3865. datetree, use \"~/org/datetree.org::datetree/\".
  3866. \"basement::** Finished Tasks\"
  3867. Archive in file ./basement (relative path), as level 3 trees
  3868. below the level 2 heading \"** Finished Tasks\".
  3869. You may set this option on a per-file basis by adding to the buffer a
  3870. line like
  3871. #+ARCHIVE: basement::** Finished Tasks
  3872. You may also define it locally for a subtree by setting an ARCHIVE property
  3873. in the entry. If such a property is found in an entry, or anywhere up
  3874. the hierarchy, it will be used."
  3875. :group 'org-archive
  3876. :type 'string)
  3877. (defcustom org-archive-tag "ARCHIVE"
  3878. "The tag that marks a subtree as archived.
  3879. An archived subtree does not open during visibility cycling, and does
  3880. not contribute to the agenda listings.
  3881. After changing this, font-lock must be restarted in the relevant buffers to
  3882. get the proper fontification."
  3883. :group 'org-archive
  3884. :group 'org-keywords
  3885. :type 'string)
  3886. (defcustom org-agenda-skip-archived-trees t
  3887. "Non-nil means the agenda will skip any items located in archived trees.
  3888. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3889. variable is no longer recommended, you should leave it at the value t.
  3890. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3891. :group 'org-archive
  3892. :group 'org-agenda-skip
  3893. :type 'boolean)
  3894. (defcustom org-columns-skip-archived-trees t
  3895. "Non-nil means ignore archived trees when creating column view."
  3896. :group 'org-archive
  3897. :group 'org-properties
  3898. :type 'boolean)
  3899. (defcustom org-cycle-open-archived-trees nil
  3900. "Non-nil means `org-cycle' will open archived trees.
  3901. An archived tree is a tree marked with the tag ARCHIVE.
  3902. When nil, archived trees will stay folded. You can still open them with
  3903. normal outline commands like `show-all', but not with the cycling commands."
  3904. :group 'org-archive
  3905. :group 'org-cycle
  3906. :type 'boolean)
  3907. (defcustom org-sparse-tree-open-archived-trees nil
  3908. "Non-nil means sparse tree construction shows matches in archived trees.
  3909. When nil, matches in these trees are highlighted, but the trees are kept in
  3910. collapsed state."
  3911. :group 'org-archive
  3912. :group 'org-sparse-trees
  3913. :type 'boolean)
  3914. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3915. "The default date type when building a sparse tree.
  3916. When this is nil, a date is a scheduled or a deadline timestamp.
  3917. Otherwise, these types are allowed:
  3918. all: all timestamps
  3919. active: only active timestamps (<...>)
  3920. inactive: only inactive timestamps (<...)
  3921. scheduled: only scheduled timestamps
  3922. deadline: only deadline timestamps"
  3923. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3924. (const :tag "All timestamps" all)
  3925. (const :tag "Only active timestamps" active)
  3926. (const :tag "Only inactive timestamps" inactive)
  3927. (const :tag "Only scheduled timestamps" scheduled)
  3928. (const :tag "Only deadline timestamps" deadline)
  3929. (const :tag "Only closed timestamps" closed))
  3930. :version "24.3"
  3931. :group 'org-sparse-trees)
  3932. (defun org-cycle-hide-archived-subtrees (state)
  3933. "Re-hide all archived subtrees after a visibility state change."
  3934. (when (and (not org-cycle-open-archived-trees)
  3935. (not (memq state '(overview folded))))
  3936. (save-excursion
  3937. (let* ((globalp (memq state '(contents all)))
  3938. (beg (if globalp (point-min) (point)))
  3939. (end (if globalp (point-max) (org-end-of-subtree t))))
  3940. (org-hide-archived-subtrees beg end)
  3941. (goto-char beg)
  3942. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3943. (message "%s" (substitute-command-keys
  3944. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3945. (defun org-force-cycle-archived ()
  3946. "Cycle subtree even if it is archived."
  3947. (interactive)
  3948. (setq this-command 'org-cycle)
  3949. (let ((org-cycle-open-archived-trees t))
  3950. (call-interactively 'org-cycle)))
  3951. (defun org-hide-archived-subtrees (beg end)
  3952. "Re-hide all archived subtrees after a visibility state change."
  3953. (save-excursion
  3954. (let* ((re (concat ":" org-archive-tag ":")))
  3955. (goto-char beg)
  3956. (while (re-search-forward re end t)
  3957. (when (org-at-heading-p)
  3958. (org-flag-subtree t)
  3959. (org-end-of-subtree t))))))
  3960. (declare-function outline-end-of-heading "outline" ())
  3961. (declare-function outline-flag-region "outline" (from to flag))
  3962. (defun org-flag-subtree (flag)
  3963. (save-excursion
  3964. (org-back-to-heading t)
  3965. (outline-end-of-heading)
  3966. (outline-flag-region (point)
  3967. (progn (org-end-of-subtree t) (point))
  3968. flag)))
  3969. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3970. (eval-and-compile
  3971. (org-autoload "org-archive"
  3972. '(org-add-archive-files)))
  3973. ;; Autoload Column View Code
  3974. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3975. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3976. (declare-function org-columns-compute "org-colview" (property))
  3977. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3978. '(org-columns-number-to-string
  3979. org-columns-get-format-and-top-level
  3980. org-columns-compute
  3981. org-columns-remove-overlays))
  3982. ;; Autoload ID code
  3983. (declare-function org-id-store-link "org-id")
  3984. (declare-function org-id-locations-load "org-id")
  3985. (declare-function org-id-locations-save "org-id")
  3986. (defvar org-id-track-globally)
  3987. (org-autoload "org-id"
  3988. '(org-id-new
  3989. org-id-copy
  3990. org-id-get-with-outline-path-completion
  3991. org-id-get-with-outline-drilling))
  3992. ;;; Variables for pre-computed regular expressions, all buffer local
  3993. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3994. "Matches first line of a hidden block.")
  3995. (make-variable-buffer-local 'org-drawer-regexp)
  3996. (defvar org-todo-regexp nil
  3997. "Matches any of the TODO state keywords.")
  3998. (make-variable-buffer-local 'org-todo-regexp)
  3999. (defvar org-not-done-regexp nil
  4000. "Matches any of the TODO state keywords except the last one.")
  4001. (make-variable-buffer-local 'org-not-done-regexp)
  4002. (defvar org-not-done-heading-regexp nil
  4003. "Matches a TODO headline that is not done.")
  4004. (make-variable-buffer-local 'org-not-done-regexp)
  4005. (defvar org-todo-line-regexp nil
  4006. "Matches a headline and puts TODO state into group 2 if present.")
  4007. (make-variable-buffer-local 'org-todo-line-regexp)
  4008. (defvar org-complex-heading-regexp nil
  4009. "Matches a headline and puts everything into groups:
  4010. group 1: the stars
  4011. group 2: The todo keyword, maybe
  4012. group 3: Priority cookie
  4013. group 4: True headline
  4014. group 5: Tags")
  4015. (make-variable-buffer-local 'org-complex-heading-regexp)
  4016. (defvar org-complex-heading-regexp-format nil
  4017. "Printf format to make regexp to match an exact headline.
  4018. This regexp will match the headline of any node which has the
  4019. exact headline text that is put into the format, but may have any
  4020. TODO state, priority and tags.")
  4021. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4022. (defvar org-todo-line-tags-regexp nil
  4023. "Matches a headline and puts TODO state into group 2 if present.
  4024. Also put tags into group 4 if tags are present.")
  4025. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4026. (defvar org-ds-keyword-length 12
  4027. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4028. (make-variable-buffer-local 'org-ds-keyword-length)
  4029. (defvar org-deadline-regexp nil
  4030. "Matches the DEADLINE keyword.")
  4031. (make-variable-buffer-local 'org-deadline-regexp)
  4032. (defvar org-deadline-time-regexp nil
  4033. "Matches the DEADLINE keyword together with a time stamp.")
  4034. (make-variable-buffer-local 'org-deadline-time-regexp)
  4035. (defvar org-deadline-time-hour-regexp nil
  4036. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4037. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4038. (defvar org-deadline-line-regexp nil
  4039. "Matches the DEADLINE keyword and the rest of the line.")
  4040. (make-variable-buffer-local 'org-deadline-line-regexp)
  4041. (defvar org-scheduled-regexp nil
  4042. "Matches the SCHEDULED keyword.")
  4043. (make-variable-buffer-local 'org-scheduled-regexp)
  4044. (defvar org-scheduled-time-regexp nil
  4045. "Matches the SCHEDULED keyword together with a time stamp.")
  4046. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4047. (defvar org-scheduled-time-hour-regexp nil
  4048. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4049. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4050. (defvar org-closed-time-regexp nil
  4051. "Matches the CLOSED keyword together with a time stamp.")
  4052. (make-variable-buffer-local 'org-closed-time-regexp)
  4053. (defvar org-keyword-time-regexp nil
  4054. "Matches any of the 4 keywords, together with the time stamp.")
  4055. (make-variable-buffer-local 'org-keyword-time-regexp)
  4056. (defvar org-keyword-time-not-clock-regexp nil
  4057. "Matches any of the 3 keywords, together with the time stamp.")
  4058. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4059. (defvar org-maybe-keyword-time-regexp nil
  4060. "Matches a timestamp, possibly preceded by a keyword.")
  4061. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4062. (defvar org-all-time-keywords nil
  4063. "List of time keywords.")
  4064. (make-variable-buffer-local 'org-all-time-keywords)
  4065. (defconst org-plain-time-of-day-regexp
  4066. (concat
  4067. "\\(\\<[012]?[0-9]"
  4068. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4069. "\\(--?"
  4070. "\\(\\<[012]?[0-9]"
  4071. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4072. "\\)?")
  4073. "Regular expression to match a plain time or time range.
  4074. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4075. groups carry important information:
  4076. 0 the full match
  4077. 1 the first time, range or not
  4078. 8 the second time, if it is a range.")
  4079. (defconst org-plain-time-extension-regexp
  4080. (concat
  4081. "\\(\\<[012]?[0-9]"
  4082. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4083. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4084. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4085. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4086. groups carry important information:
  4087. 0 the full match
  4088. 7 hours of duration
  4089. 9 minutes of duration")
  4090. (defconst org-stamp-time-of-day-regexp
  4091. (concat
  4092. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4093. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4094. "\\(--?"
  4095. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4096. "Regular expression to match a timestamp time or time range.
  4097. After a match, the following groups carry important information:
  4098. 0 the full match
  4099. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4100. 2 the first time, range or not
  4101. 4 the second time, if it is a range.")
  4102. (defconst org-startup-options
  4103. '(("fold" org-startup-folded t)
  4104. ("overview" org-startup-folded t)
  4105. ("nofold" org-startup-folded nil)
  4106. ("showall" org-startup-folded nil)
  4107. ("showeverything" org-startup-folded showeverything)
  4108. ("content" org-startup-folded content)
  4109. ("indent" org-startup-indented t)
  4110. ("noindent" org-startup-indented nil)
  4111. ("hidestars" org-hide-leading-stars t)
  4112. ("showstars" org-hide-leading-stars nil)
  4113. ("odd" org-odd-levels-only t)
  4114. ("oddeven" org-odd-levels-only nil)
  4115. ("align" org-startup-align-all-tables t)
  4116. ("noalign" org-startup-align-all-tables nil)
  4117. ("inlineimages" org-startup-with-inline-images t)
  4118. ("noinlineimages" org-startup-with-inline-images nil)
  4119. ("latexpreview" org-startup-with-latex-preview t)
  4120. ("nolatexpreview" org-startup-with-latex-preview nil)
  4121. ("customtime" org-display-custom-times t)
  4122. ("logdone" org-log-done time)
  4123. ("lognotedone" org-log-done note)
  4124. ("nologdone" org-log-done nil)
  4125. ("lognoteclock-out" org-log-note-clock-out t)
  4126. ("nolognoteclock-out" org-log-note-clock-out nil)
  4127. ("logrepeat" org-log-repeat state)
  4128. ("lognoterepeat" org-log-repeat note)
  4129. ("logdrawer" org-log-into-drawer t)
  4130. ("nologdrawer" org-log-into-drawer nil)
  4131. ("logstatesreversed" org-log-states-order-reversed t)
  4132. ("nologstatesreversed" org-log-states-order-reversed nil)
  4133. ("nologrepeat" org-log-repeat nil)
  4134. ("logreschedule" org-log-reschedule time)
  4135. ("lognotereschedule" org-log-reschedule note)
  4136. ("nologreschedule" org-log-reschedule nil)
  4137. ("logredeadline" org-log-redeadline time)
  4138. ("lognoteredeadline" org-log-redeadline note)
  4139. ("nologredeadline" org-log-redeadline nil)
  4140. ("logrefile" org-log-refile time)
  4141. ("lognoterefile" org-log-refile note)
  4142. ("nologrefile" org-log-refile nil)
  4143. ("fninline" org-footnote-define-inline t)
  4144. ("nofninline" org-footnote-define-inline nil)
  4145. ("fnlocal" org-footnote-section nil)
  4146. ("fnauto" org-footnote-auto-label t)
  4147. ("fnprompt" org-footnote-auto-label nil)
  4148. ("fnconfirm" org-footnote-auto-label confirm)
  4149. ("fnplain" org-footnote-auto-label plain)
  4150. ("fnadjust" org-footnote-auto-adjust t)
  4151. ("nofnadjust" org-footnote-auto-adjust nil)
  4152. ("constcgs" constants-unit-system cgs)
  4153. ("constSI" constants-unit-system SI)
  4154. ("noptag" org-tag-persistent-alist nil)
  4155. ("hideblocks" org-hide-block-startup t)
  4156. ("nohideblocks" org-hide-block-startup nil)
  4157. ("beamer" org-startup-with-beamer-mode t)
  4158. ("entitiespretty" org-pretty-entities t)
  4159. ("entitiesplain" org-pretty-entities nil))
  4160. "Variable associated with STARTUP options for org-mode.
  4161. Each element is a list of three items: the startup options (as written
  4162. in the #+STARTUP line), the corresponding variable, and the value to set
  4163. this variable to if the option is found. An optional forth element PUSH
  4164. means to push this value onto the list in the variable.")
  4165. (defun org-update-property-plist (key val props)
  4166. "Update PROPS with KEY and VAL."
  4167. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4168. (key (if appending (substring key 0 (- (length key) 1)) key))
  4169. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4170. (previous (cdr (assoc key props))))
  4171. (if appending
  4172. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4173. (cons (cons key val) remainder))))
  4174. (defconst org-block-regexp
  4175. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4176. "Regular expression for hiding blocks.")
  4177. (defconst org-heading-keyword-regexp-format
  4178. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4179. "Printf format for a regexp matching a headline with some keyword.
  4180. This regexp will match the headline of any node which has the
  4181. exact keyword that is put into the format. The keyword isn't in
  4182. any group by default, but the stars and the body are.")
  4183. (defconst org-heading-keyword-maybe-regexp-format
  4184. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4185. "Printf format for a regexp matching a headline, possibly with some keyword.
  4186. This regexp can match any headline with the specified keyword, or
  4187. without a keyword. The keyword isn't in any group by default,
  4188. but the stars and the body are.")
  4189. (defcustom org-group-tags t
  4190. "When non-nil (the default), use group tags.
  4191. This can be turned on/off through `org-toggle-tags-groups'."
  4192. :group 'org-tags
  4193. :group 'org-startup
  4194. :type 'boolean)
  4195. (defun org-toggle-tags-groups ()
  4196. "Toggle support for group tags.
  4197. Support for group tags is controlled by the option
  4198. `org-group-tags', which is non-nil by default."
  4199. (interactive)
  4200. (setq org-group-tags (not org-group-tags))
  4201. (cond ((and (derived-mode-p 'org-agenda-mode)
  4202. org-group-tags)
  4203. (org-agenda-redo))
  4204. ((derived-mode-p 'org-mode)
  4205. (let ((org-inhibit-startup t)) (org-mode))))
  4206. (message "Groups tags support has been turned %s"
  4207. (if org-group-tags "on" "off")))
  4208. (defun org-set-regexps-and-options-for-tags ()
  4209. "Precompute regular expressions used for tags in the current buffer."
  4210. (when (derived-mode-p 'org-mode)
  4211. (org-set-local 'org-file-tags nil)
  4212. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4213. (splitre "[ \t]+")
  4214. (start 0)
  4215. tags ftags key value)
  4216. (save-excursion
  4217. (save-restriction
  4218. (widen)
  4219. (goto-char (point-min))
  4220. (while (re-search-forward re nil t)
  4221. (setq key (upcase (org-match-string-no-properties 1))
  4222. value (org-match-string-no-properties 2))
  4223. (if (stringp value) (setq value (org-trim value)))
  4224. (cond
  4225. ((equal key "TAGS")
  4226. (setq tags (append tags (if tags '("\\n") nil)
  4227. (org-split-string value splitre))))
  4228. ((equal key "FILETAGS")
  4229. (when (string-match "\\S-" value)
  4230. (setq ftags
  4231. (append
  4232. ftags
  4233. (apply 'append
  4234. (mapcar (lambda (x) (org-split-string x ":"))
  4235. (org-split-string value)))))))))))
  4236. ;; Process the file tags.
  4237. (and ftags (org-set-local 'org-file-tags
  4238. (mapcar 'org-add-prop-inherited ftags)))
  4239. (org-set-local 'org-tag-groups-alist nil)
  4240. ;; Process the tags.
  4241. ;; FIXME
  4242. (when tags
  4243. (let (e tgs g)
  4244. (while (setq e (pop tags))
  4245. (cond
  4246. ((equal e "{")
  4247. (progn (push '(:startgroup) tgs)
  4248. (when (equal (nth 1 tags) ":")
  4249. (push (list (replace-regexp-in-string
  4250. "(.+)$" "" (nth 0 tags)))
  4251. org-tag-groups-alist)
  4252. (setq g 0))))
  4253. ((equal e ":") (push '(:grouptags) tgs))
  4254. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4255. ((equal e "\\n") (push '(:newline) tgs))
  4256. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4257. (push (cons (match-string 1 e)
  4258. (string-to-char (match-string 2 e))) tgs)
  4259. (if (and g (> g 0))
  4260. (setcar org-tag-groups-alist
  4261. (append (car org-tag-groups-alist)
  4262. (list (match-string 1 e)))))
  4263. (if g (setq g (1+ g))))
  4264. (t (push (list e) tgs)
  4265. (if (and g (> g 0))
  4266. (setcar org-tag-groups-alist
  4267. (append (car org-tag-groups-alist) (list e))))
  4268. (if g (setq g (1+ g))))))
  4269. (org-set-local 'org-tag-alist nil)
  4270. (while (setq e (pop tgs))
  4271. (or (and (stringp (car e))
  4272. (assoc (car e) org-tag-alist))
  4273. (push e org-tag-alist)))
  4274. ;; Return a list with tag variables
  4275. (list org-file-tags org-tag-alist org-tag-groups-alist))))))
  4276. (defun org-set-regexps-and-options ()
  4277. "Precompute regular expressions used in the current buffer."
  4278. (when (derived-mode-p 'org-mode)
  4279. (org-set-local 'org-todo-kwd-alist nil)
  4280. (org-set-local 'org-todo-key-alist nil)
  4281. (org-set-local 'org-todo-key-trigger nil)
  4282. (org-set-local 'org-todo-keywords-1 nil)
  4283. (org-set-local 'org-done-keywords nil)
  4284. (org-set-local 'org-todo-heads nil)
  4285. (org-set-local 'org-todo-sets nil)
  4286. (org-set-local 'org-todo-log-states nil)
  4287. (org-set-local 'org-file-properties nil)
  4288. (let ((re (org-make-options-regexp
  4289. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4290. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4291. "SETUPFILE" "OPTIONS")
  4292. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4293. (splitre "[ \t]+")
  4294. (scripts org-use-sub-superscripts)
  4295. kwds kws0 kwsa key log value cat arch const links hw dws
  4296. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4297. (start 0))
  4298. (save-excursion
  4299. (save-restriction
  4300. (widen)
  4301. (goto-char (point-min))
  4302. (while
  4303. (or (and
  4304. ext-setup-or-nil
  4305. (let (ret)
  4306. (with-temp-buffer
  4307. (insert ext-setup-or-nil)
  4308. (let ((major-mode 'org-mode))
  4309. (setq ret (save-match-data
  4310. (org-set-regexps-and-options-for-tags)))))
  4311. ;; Append setupfile tags to existing tags
  4312. (setq org-file-tags
  4313. (delq nil (append org-file-tags (nth 0 ret)))
  4314. org-tag-alist
  4315. (delq nil (append org-tag-alist (nth 1 ret)))
  4316. org-tag-groups-alist
  4317. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4318. (and ext-setup-or-nil
  4319. (string-match re ext-setup-or-nil start)
  4320. (setq start (match-end 0)))
  4321. (and (setq ext-setup-or-nil nil start 0)
  4322. (re-search-forward re nil t)))
  4323. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4324. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4325. (if (stringp value) (setq value (org-trim value)))
  4326. (cond
  4327. ((equal key "CATEGORY")
  4328. (setq cat value))
  4329. ((member key '("SEQ_TODO" "TODO"))
  4330. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4331. ((equal key "TYP_TODO")
  4332. (push (cons 'type (org-split-string value splitre)) kwds))
  4333. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4334. ;; general TODO-like setup
  4335. (push (cons (intern (downcase (match-string 1 key)))
  4336. (org-split-string value splitre)) kwds))
  4337. ((equal key "COLUMNS")
  4338. (org-set-local 'org-columns-default-format value))
  4339. ((equal key "LINK")
  4340. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4341. (push (cons (match-string 1 value)
  4342. (org-trim (match-string 2 value)))
  4343. links)))
  4344. ((equal key "PRIORITIES")
  4345. (setq prio (org-split-string value " +")))
  4346. ((equal key "PROPERTY")
  4347. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4348. (setq props (org-update-property-plist (match-string 1 value)
  4349. (match-string 2 value)
  4350. props))))
  4351. ((equal key "DRAWERS")
  4352. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4353. ((equal key "CONSTANTS")
  4354. (org-table-set-constants))
  4355. ((equal key "STARTUP")
  4356. (let ((opts (org-split-string value splitre))
  4357. l var val)
  4358. (while (setq l (pop opts))
  4359. (when (setq l (assoc l org-startup-options))
  4360. (setq var (nth 1 l) val (nth 2 l))
  4361. (if (not (nth 3 l))
  4362. (set (make-local-variable var) val)
  4363. (if (not (listp (symbol-value var)))
  4364. (set (make-local-variable var) nil))
  4365. (set (make-local-variable var) (symbol-value var))
  4366. (add-to-list var val))))))
  4367. ((equal key "ARCHIVE")
  4368. (setq arch value)
  4369. (remove-text-properties 0 (length arch)
  4370. '(face t fontified t) arch))
  4371. ((equal key "OPTIONS")
  4372. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4373. (setq scripts (read (match-string 2 value)))))
  4374. ((and (equal key "SETUPFILE")
  4375. ;; Prevent checking in Gnus messages
  4376. (not buffer-read-only))
  4377. (setq setup-contents (org-file-contents
  4378. (expand-file-name
  4379. (org-remove-double-quotes value))
  4380. 'noerror))
  4381. (if (not ext-setup-or-nil)
  4382. (setq ext-setup-or-nil setup-contents start 0)
  4383. (setq ext-setup-or-nil
  4384. (concat (substring ext-setup-or-nil 0 start)
  4385. "\n" setup-contents "\n"
  4386. (substring ext-setup-or-nil start)))))))
  4387. ;; search for property blocks
  4388. (goto-char (point-min))
  4389. (while (re-search-forward org-block-regexp nil t)
  4390. (when (equal "PROPERTY" (upcase (match-string 1)))
  4391. (setq value (replace-regexp-in-string
  4392. "[\n\r]" " " (match-string 4)))
  4393. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4394. (setq props (org-update-property-plist (match-string 1 value)
  4395. (match-string 2 value)
  4396. props)))))))
  4397. (org-set-local 'org-use-sub-superscripts scripts)
  4398. (when cat
  4399. (org-set-local 'org-category (intern cat))
  4400. (push (cons "CATEGORY" cat) props))
  4401. (when prio
  4402. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4403. (setq prio (mapcar 'string-to-char prio))
  4404. (org-set-local 'org-highest-priority (nth 0 prio))
  4405. (org-set-local 'org-lowest-priority (nth 1 prio))
  4406. (org-set-local 'org-default-priority (nth 2 prio)))
  4407. (and props (org-set-local 'org-file-properties (nreverse props)))
  4408. (and drawers (org-set-local 'org-drawers drawers))
  4409. (and arch (org-set-local 'org-archive-location arch))
  4410. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4411. ;; Process the TODO keywords
  4412. (unless kwds
  4413. ;; Use the global values as if they had been given locally.
  4414. (setq kwds (default-value 'org-todo-keywords))
  4415. (if (stringp (car kwds))
  4416. (setq kwds (list (cons org-todo-interpretation
  4417. (default-value 'org-todo-keywords)))))
  4418. (setq kwds (reverse kwds)))
  4419. (setq kwds (nreverse kwds))
  4420. (let (inter kws kw)
  4421. (while (setq kws (pop kwds))
  4422. (let ((kws (or
  4423. (run-hook-with-args-until-success
  4424. 'org-todo-setup-filter-hook kws)
  4425. kws)))
  4426. (setq inter (pop kws) sep (member "|" kws)
  4427. kws0 (delete "|" (copy-sequence kws))
  4428. kwsa nil
  4429. kws1 (mapcar
  4430. (lambda (x)
  4431. ;; 1 2
  4432. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4433. (progn
  4434. (setq kw (match-string 1 x)
  4435. key (and (match-end 2) (match-string 2 x))
  4436. log (org-extract-log-state-settings x))
  4437. (push (cons kw (and key (string-to-char key))) kwsa)
  4438. (and log (push log org-todo-log-states))
  4439. kw)
  4440. (error "Invalid TODO keyword %s" x)))
  4441. kws0)
  4442. kwsa (if kwsa (append '((:startgroup))
  4443. (nreverse kwsa)
  4444. '((:endgroup))))
  4445. hw (car kws1)
  4446. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4447. tail (list inter hw (car dws) (org-last dws))))
  4448. (add-to-list 'org-todo-heads hw 'append)
  4449. (push kws1 org-todo-sets)
  4450. (setq org-done-keywords (append org-done-keywords dws nil))
  4451. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4452. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4453. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4454. (setq org-todo-sets (nreverse org-todo-sets)
  4455. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4456. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4457. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4458. ;; Compute the regular expressions and other local variables.
  4459. ;; Using `org-outline-regexp-bol' would complicate them much,
  4460. ;; because of the fixed white space at the end of that string.
  4461. (if (not org-done-keywords)
  4462. (setq org-done-keywords (and org-todo-keywords-1
  4463. (list (org-last org-todo-keywords-1)))))
  4464. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4465. (length org-scheduled-string)
  4466. (length org-clock-string)
  4467. (length org-closed-string)))
  4468. org-drawer-regexp
  4469. (concat "^[ \t]*:\\("
  4470. (mapconcat 'regexp-quote org-drawers "\\|")
  4471. "\\):[ \t]*$")
  4472. org-not-done-keywords
  4473. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4474. org-todo-regexp
  4475. (concat "\\("
  4476. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4477. "\\)")
  4478. org-not-done-regexp
  4479. (concat "\\("
  4480. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4481. "\\)")
  4482. org-not-done-heading-regexp
  4483. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4484. org-todo-line-regexp
  4485. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4486. org-complex-heading-regexp
  4487. (concat "^\\(\\*+\\)"
  4488. "\\(?: +" org-todo-regexp "\\)?"
  4489. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4490. "\\(?: +\\(.*?\\)\\)??"
  4491. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4492. "[ \t]*$")
  4493. org-complex-heading-regexp-format
  4494. (concat "^\\(\\*+\\)"
  4495. "\\(?: +" org-todo-regexp "\\)?"
  4496. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4497. "\\(?: +"
  4498. ;; Stats cookies can be stuck to body.
  4499. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4500. "\\(%s\\)"
  4501. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4502. "\\)"
  4503. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4504. "[ \t]*$")
  4505. org-todo-line-tags-regexp
  4506. (concat "^\\(\\*+\\)"
  4507. "\\(?: +" org-todo-regexp "\\)?"
  4508. "\\(?: +\\(.*?\\)\\)??"
  4509. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4510. "[ \t]*$")
  4511. org-deadline-regexp (concat "\\<" org-deadline-string)
  4512. org-deadline-time-regexp
  4513. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4514. org-deadline-time-hour-regexp
  4515. (concat "\\<" org-deadline-string
  4516. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4517. org-deadline-line-regexp
  4518. (concat "\\<\\(" org-deadline-string "\\).*")
  4519. org-scheduled-regexp
  4520. (concat "\\<" org-scheduled-string)
  4521. org-scheduled-time-regexp
  4522. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4523. org-scheduled-time-hour-regexp
  4524. (concat "\\<" org-scheduled-string
  4525. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4526. org-closed-time-regexp
  4527. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4528. org-keyword-time-regexp
  4529. (concat "\\<\\(" org-scheduled-string
  4530. "\\|" org-deadline-string
  4531. "\\|" org-closed-string
  4532. "\\|" org-clock-string "\\)"
  4533. " *[[<]\\([^]>]+\\)[]>]")
  4534. org-keyword-time-not-clock-regexp
  4535. (concat "\\<\\(" org-scheduled-string
  4536. "\\|" org-deadline-string
  4537. "\\|" org-closed-string
  4538. "\\)"
  4539. " *[[<]\\([^]>]+\\)[]>]")
  4540. org-maybe-keyword-time-regexp
  4541. (concat "\\(\\<\\(" org-scheduled-string
  4542. "\\|" org-deadline-string
  4543. "\\|" org-closed-string
  4544. "\\|" org-clock-string "\\)\\)?"
  4545. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4546. org-all-time-keywords
  4547. (mapcar (lambda (w) (substring w 0 -1))
  4548. (list org-scheduled-string org-deadline-string
  4549. org-clock-string org-closed-string)))
  4550. (org-compute-latex-and-related-regexp)
  4551. (org-set-font-lock-defaults))))
  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. (when (and org-tag-faces (not org-tags-special-faces-re))
  4716. ;; tag faces set outside customize.... force initialization.
  4717. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4718. ;; Calc embedded
  4719. (org-set-local 'calc-embedded-open-mode "# ")
  4720. ;; Modify a few syntax entries
  4721. (modify-syntax-entry ?@ "w")
  4722. (modify-syntax-entry ?\" "\"")
  4723. (if org-startup-truncated (setq truncate-lines t))
  4724. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4725. (org-set-local 'font-lock-unfontify-region-function
  4726. 'org-unfontify-region)
  4727. ;; Activate before-change-function
  4728. (org-set-local 'org-table-may-need-update t)
  4729. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4730. 'local)
  4731. ;; Check for running clock before killing a buffer
  4732. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4733. ;; Initialize macros templates.
  4734. (org-macro-initialize-templates)
  4735. ;; Initialize radio targets.
  4736. (org-update-radio-target-regexp)
  4737. ;; Indentation.
  4738. (org-set-local 'indent-line-function 'org-indent-line)
  4739. (org-set-local 'indent-region-function 'org-indent-region)
  4740. ;; Filling and auto-filling.
  4741. (org-setup-filling)
  4742. ;; Comments.
  4743. (org-setup-comments-handling)
  4744. ;; Beginning/end of defun
  4745. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4746. (org-set-local 'end-of-defun-function 'org-forward-element)
  4747. ;; Next error for sparse trees
  4748. (org-set-local 'next-error-function 'org-occur-next-match)
  4749. ;; Make sure dependence stuff works reliably, even for users who set it
  4750. ;; too late :-(
  4751. (if org-enforce-todo-dependencies
  4752. (add-hook 'org-blocker-hook
  4753. 'org-block-todo-from-children-or-siblings-or-parent)
  4754. (remove-hook 'org-blocker-hook
  4755. 'org-block-todo-from-children-or-siblings-or-parent))
  4756. (if org-enforce-todo-checkbox-dependencies
  4757. (add-hook 'org-blocker-hook
  4758. 'org-block-todo-from-checkboxes)
  4759. (remove-hook 'org-blocker-hook
  4760. 'org-block-todo-from-checkboxes))
  4761. ;; Align options lines
  4762. (org-set-local
  4763. 'align-mode-rules-list
  4764. '((org-in-buffer-settings
  4765. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4766. (modes . '(org-mode)))))
  4767. ;; Imenu
  4768. (org-set-local 'imenu-create-index-function
  4769. 'org-imenu-get-tree)
  4770. ;; Make isearch reveal context
  4771. (if (or (featurep 'xemacs)
  4772. (not (boundp 'outline-isearch-open-invisible-function)))
  4773. ;; Emacs 21 and XEmacs make use of the hook
  4774. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4775. ;; Emacs 22 deals with this through a special variable
  4776. (org-set-local 'outline-isearch-open-invisible-function
  4777. (lambda (&rest ignore) (org-show-context 'isearch)))
  4778. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4779. ;; Setup the pcomplete hooks
  4780. (set (make-local-variable 'pcomplete-command-completion-function)
  4781. 'org-pcomplete-initial)
  4782. (set (make-local-variable 'pcomplete-command-name-function)
  4783. 'org-command-at-point)
  4784. (set (make-local-variable 'pcomplete-default-completion-function)
  4785. 'ignore)
  4786. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4787. 'org-parse-arguments)
  4788. (set (make-local-variable 'pcomplete-termination-string) "")
  4789. (when (>= emacs-major-version 23)
  4790. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4791. ;; If empty file that did not turn on org-mode automatically, make it to.
  4792. (if (and org-insert-mode-line-in-empty-file
  4793. (org-called-interactively-p 'any)
  4794. (= (point-min) (point-max)))
  4795. (insert "# -*- mode: org -*-\n\n"))
  4796. (unless org-inhibit-startup
  4797. (org-unmodified
  4798. (and org-startup-with-beamer-mode (org-beamer-mode))
  4799. (when org-startup-align-all-tables
  4800. (org-table-map-tables 'org-table-align 'quietly))
  4801. (when org-startup-with-inline-images
  4802. (org-display-inline-images))
  4803. (when org-startup-with-latex-preview
  4804. (org-preview-latex-fragment))
  4805. (unless org-inhibit-startup-visibility-stuff
  4806. (org-set-startup-visibility))))
  4807. ;; Try to set org-hide correctly
  4808. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4809. ;; Update `customize-package-emacs-version-alist'
  4810. (add-to-list 'customize-package-emacs-version-alist
  4811. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4812. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4813. ("8.0" . "24.4")))
  4814. (defvar org-mode-transpose-word-syntax-table
  4815. (let ((st (make-syntax-table)))
  4816. (mapc (lambda(c) (modify-syntax-entry
  4817. (string-to-char (car c)) "w p" st))
  4818. org-emphasis-alist)
  4819. st))
  4820. (when (fboundp 'abbrev-table-put)
  4821. (abbrev-table-put org-mode-abbrev-table
  4822. :parents (list text-mode-abbrev-table)))
  4823. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4824. (defsubst org-fix-ellipsis-at-bol ()
  4825. (save-excursion (goto-char (window-start)) (recenter 0)))
  4826. (defun org-find-invisible-foreground ()
  4827. (let ((candidates (remove
  4828. "unspecified-bg"
  4829. (nconc
  4830. (list (face-background 'default)
  4831. (face-background 'org-default))
  4832. (mapcar
  4833. (lambda (alist)
  4834. (when (boundp alist)
  4835. (cdr (assoc 'background-color (symbol-value alist)))))
  4836. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4837. (list (face-foreground 'org-hide))))))
  4838. (car (remove nil candidates))))
  4839. (defun org-current-time (&optional rounding-minutes past)
  4840. "Current time, possibly rounded to ROUNDING-MINUTES.
  4841. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4842. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4843. the rounding returns a past time."
  4844. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4845. (car org-time-stamp-rounding-minutes)))
  4846. (time (decode-time)) res)
  4847. (if (< r 1)
  4848. (current-time)
  4849. (setq res
  4850. (apply 'encode-time
  4851. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4852. (nthcdr 2 time))))
  4853. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4854. (seconds-to-time (- (org-float-time res) (* r 60)))
  4855. res))))
  4856. (defun org-today ()
  4857. "Return today date, considering `org-extend-today-until'."
  4858. (time-to-days
  4859. (time-subtract (current-time)
  4860. (list 0 (* 3600 org-extend-today-until) 0))))
  4861. ;;;; Font-Lock stuff, including the activators
  4862. (defvar org-mouse-map (make-sparse-keymap))
  4863. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4864. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4865. (when org-mouse-1-follows-link
  4866. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4867. (when org-tab-follows-link
  4868. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4869. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4870. (require 'font-lock)
  4871. (defconst org-non-link-chars "]\t\n\r<>")
  4872. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4873. "shell" "elisp" "doi" "message"))
  4874. (defvar org-link-types-re nil
  4875. "Matches a link that has a url-like prefix like \"http:\"")
  4876. (defvar org-link-re-with-space nil
  4877. "Matches a link with spaces, optional angular brackets around it.")
  4878. (defvar org-link-re-with-space2 nil
  4879. "Matches a link with spaces, optional angular brackets around it.")
  4880. (defvar org-link-re-with-space3 nil
  4881. "Matches a link with spaces, only for internal part in bracket links.")
  4882. (defvar org-angle-link-re nil
  4883. "Matches link with angular brackets, spaces are allowed.")
  4884. (defvar org-plain-link-re nil
  4885. "Matches plain link, without spaces.")
  4886. (defvar org-bracket-link-regexp nil
  4887. "Matches a link in double brackets.")
  4888. (defvar org-bracket-link-analytic-regexp nil
  4889. "Regular expression used to analyze links.
  4890. Here is what the match groups contain after a match:
  4891. 1: http:
  4892. 2: http
  4893. 3: path
  4894. 4: [desc]
  4895. 5: desc")
  4896. (defvar org-bracket-link-analytic-regexp++ nil
  4897. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4898. (defvar org-any-link-re nil
  4899. "Regular expression matching any link.")
  4900. (defconst org-match-sexp-depth 3
  4901. "Number of stacked braces for sub/superscript matching.")
  4902. (defun org-create-multibrace-regexp (left right n)
  4903. "Create a regular expression which will match a balanced sexp.
  4904. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4905. as single character strings.
  4906. The regexp returned will match the entire expression including the
  4907. delimiters. It will also define a single group which contains the
  4908. match except for the outermost delimiters. The maximum depth of
  4909. stacked delimiters is N. Escaping delimiters is not possible."
  4910. (let* ((nothing (concat "[^" left right "]*?"))
  4911. (or "\\|")
  4912. (re nothing)
  4913. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4914. (while (> n 1)
  4915. (setq n (1- n)
  4916. re (concat re or next)
  4917. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4918. (concat left "\\(" re "\\)" right)))
  4919. (defvar org-match-substring-regexp
  4920. (concat
  4921. "\\(\\S-\\)\\([_^]\\)\\("
  4922. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4923. "\\|"
  4924. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4925. "\\|"
  4926. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4927. "The regular expression matching a sub- or superscript.")
  4928. (defvar org-match-substring-with-braces-regexp
  4929. (concat
  4930. "\\(\\S-\\)\\([_^]\\)\\("
  4931. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4932. "\\)")
  4933. "The regular expression matching a sub- or superscript, forcing braces.")
  4934. (defun org-make-link-regexps ()
  4935. "Update the link regular expressions.
  4936. This should be called after the variable `org-link-types' has changed."
  4937. (setq org-link-types-re
  4938. (concat
  4939. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4940. org-link-re-with-space
  4941. (concat
  4942. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4943. "\\([^" org-non-link-chars " ]"
  4944. "[^" org-non-link-chars "]*"
  4945. "[^" org-non-link-chars " ]\\)>?")
  4946. org-link-re-with-space2
  4947. (concat
  4948. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4949. "\\([^" org-non-link-chars " ]"
  4950. "[^\t\n\r]*"
  4951. "[^" org-non-link-chars " ]\\)>?")
  4952. org-link-re-with-space3
  4953. (concat
  4954. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4955. "\\([^" org-non-link-chars " ]"
  4956. "[^\t\n\r]*\\)")
  4957. org-angle-link-re
  4958. (concat
  4959. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4960. "\\([^" org-non-link-chars " ]"
  4961. "[^" org-non-link-chars "]*"
  4962. "\\)>")
  4963. org-plain-link-re
  4964. (concat
  4965. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4966. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4967. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4968. org-bracket-link-regexp
  4969. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4970. org-bracket-link-analytic-regexp
  4971. (concat
  4972. "\\[\\["
  4973. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4974. "\\([^]]+\\)"
  4975. "\\]"
  4976. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4977. "\\]")
  4978. org-bracket-link-analytic-regexp++
  4979. (concat
  4980. "\\[\\["
  4981. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4982. "\\([^]]+\\)"
  4983. "\\]"
  4984. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4985. "\\]")
  4986. org-any-link-re
  4987. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4988. org-angle-link-re "\\)\\|\\("
  4989. org-plain-link-re "\\)")))
  4990. (org-make-link-regexps)
  4991. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4992. "Regular expression for fast time stamp matching.")
  4993. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4994. "Regular expression for fast time stamp matching.")
  4995. (defconst org-ts-regexp0
  4996. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4997. "Regular expression matching time strings for analysis.
  4998. This one does not require the space after the date, so it can be used
  4999. on a string that terminates immediately after the date.")
  5000. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  5001. "Regular expression matching time strings for analysis.")
  5002. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  5003. "Regular expression matching time stamps, with groups.")
  5004. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  5005. "Regular expression matching time stamps (also [..]), with groups.")
  5006. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  5007. "Regular expression matching a time stamp range.")
  5008. (defconst org-tr-regexp-both
  5009. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5010. "Regular expression matching a time stamp range.")
  5011. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5012. org-ts-regexp "\\)?")
  5013. "Regular expression matching a time stamp or time stamp range.")
  5014. (defconst org-tsr-regexp-both
  5015. (concat org-ts-regexp-both "\\(--?-?"
  5016. org-ts-regexp-both "\\)?")
  5017. "Regular expression matching a time stamp or time stamp range.
  5018. The time stamps may be either active or inactive.")
  5019. (defvar org-emph-face nil)
  5020. (defun org-do-emphasis-faces (limit)
  5021. "Run through the buffer and add overlays to emphasized strings."
  5022. (let (rtn a)
  5023. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5024. (if (not (= (char-after (match-beginning 3))
  5025. (char-after (match-beginning 4))))
  5026. (progn
  5027. (setq rtn t)
  5028. (setq a (assoc (match-string 3) org-emphasis-alist))
  5029. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5030. 'face
  5031. (nth 1 a))
  5032. (and (nth 4 a)
  5033. (org-remove-flyspell-overlays-in
  5034. (match-beginning 0) (match-end 0)))
  5035. (add-text-properties (match-beginning 2) (match-end 2)
  5036. '(font-lock-multiline t org-emphasis t))
  5037. (when org-hide-emphasis-markers
  5038. (add-text-properties (match-end 4) (match-beginning 5)
  5039. '(invisible org-link))
  5040. (add-text-properties (match-beginning 3) (match-end 3)
  5041. '(invisible org-link)))))
  5042. (backward-char 1))
  5043. rtn))
  5044. (defun org-emphasize (&optional char)
  5045. "Insert or change an emphasis, i.e. a font like bold or italic.
  5046. If there is an active region, change that region to a new emphasis.
  5047. If there is no region, just insert the marker characters and position
  5048. the cursor between them.
  5049. CHAR should be the marker character. If it is a space, it means to
  5050. remove the emphasis of the selected region.
  5051. If CHAR is not given (for example in an interactive call) it will be
  5052. prompted for."
  5053. (interactive)
  5054. (let ((erc org-emphasis-regexp-components)
  5055. (prompt "")
  5056. (string "") beg end move c s)
  5057. (if (org-region-active-p)
  5058. (setq beg (region-beginning) end (region-end)
  5059. string (buffer-substring beg end))
  5060. (setq move t))
  5061. (unless char
  5062. (message "Emphasis marker or tag: [%s]"
  5063. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5064. (setq char (read-char-exclusive)))
  5065. (if (equal char ?\ )
  5066. (setq s "" move nil)
  5067. (unless (assoc (char-to-string char) org-emphasis-alist)
  5068. (user-error "No such emphasis marker: \"%c\"" char))
  5069. (setq s (char-to-string char)))
  5070. (while (and (> (length string) 1)
  5071. (equal (substring string 0 1) (substring string -1))
  5072. (assoc (substring string 0 1) org-emphasis-alist))
  5073. (setq string (substring string 1 -1)))
  5074. (setq string (concat s string s))
  5075. (if beg (delete-region beg end))
  5076. (unless (or (bolp)
  5077. (string-match (concat "[" (nth 0 erc) "\n]")
  5078. (char-to-string (char-before (point)))))
  5079. (insert " "))
  5080. (unless (or (eobp)
  5081. (string-match (concat "[" (nth 1 erc) "\n]")
  5082. (char-to-string (char-after (point)))))
  5083. (insert " ") (backward-char 1))
  5084. (insert string)
  5085. (and move (backward-char 1))))
  5086. (defconst org-nonsticky-props
  5087. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5088. (defsubst org-rear-nonsticky-at (pos)
  5089. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5090. (defun org-activate-plain-links (limit)
  5091. "Run through the buffer and add overlays to links."
  5092. (let (f hl)
  5093. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5094. (not (org-in-src-block-p)))
  5095. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5096. (setq f (get-text-property (match-beginning 0) 'face))
  5097. (setq hl (org-match-string-no-properties 0))
  5098. (if (or (eq f 'org-tag)
  5099. (and (listp f) (memq 'org-tag f)))
  5100. nil
  5101. (add-text-properties (match-beginning 0) (match-end 0)
  5102. (list 'mouse-face 'highlight
  5103. 'face 'org-link
  5104. 'htmlize-link `(:uri ,hl)
  5105. 'keymap org-mouse-map))
  5106. (org-rear-nonsticky-at (match-end 0)))
  5107. t)))
  5108. (defun org-activate-code (limit)
  5109. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5110. (progn
  5111. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5112. (remove-text-properties (match-beginning 0) (match-end 0)
  5113. '(display t invisible t intangible t))
  5114. t)))
  5115. (defcustom org-src-fontify-natively nil
  5116. "When non-nil, fontify code in code blocks."
  5117. :type 'boolean
  5118. :version "24.1"
  5119. :group 'org-appearance
  5120. :group 'org-babel)
  5121. (defcustom org-allow-promoting-top-level-subtree nil
  5122. "When non-nil, allow promoting a top level subtree.
  5123. The leading star of the top level headline will be replaced
  5124. by a #."
  5125. :type 'boolean
  5126. :version "24.1"
  5127. :group 'org-appearance)
  5128. (defun org-fontify-meta-lines-and-blocks (limit)
  5129. (condition-case nil
  5130. (org-fontify-meta-lines-and-blocks-1 limit)
  5131. (error (message "org-mode fontification error"))))
  5132. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5133. "Fontify #+ lines and blocks."
  5134. (let ((case-fold-search t))
  5135. (if (re-search-forward
  5136. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5137. limit t)
  5138. (let ((beg (match-beginning 0))
  5139. (block-start (match-end 0))
  5140. (block-end nil)
  5141. (lang (match-string 7))
  5142. (beg1 (line-beginning-position 2))
  5143. (dc1 (downcase (match-string 2)))
  5144. (dc3 (downcase (match-string 3)))
  5145. end end1 quoting block-type ovl)
  5146. (cond
  5147. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5148. ;; a single line of backend-specific content
  5149. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5150. (remove-text-properties (match-beginning 0) (match-end 0)
  5151. '(display t invisible t intangible t))
  5152. (add-text-properties (match-beginning 1) (match-end 3)
  5153. '(font-lock-fontified t face org-meta-line))
  5154. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5155. '(font-lock-fontified t face org-block))
  5156. ; for backend-specific code
  5157. t)
  5158. ((and (match-end 4) (equal dc3 "+begin"))
  5159. ;; Truly a block
  5160. (setq block-type (downcase (match-string 5))
  5161. quoting (member block-type org-protecting-blocks))
  5162. (when (re-search-forward
  5163. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5164. nil t) ;; on purpose, we look further than LIMIT
  5165. (setq end (min (point-max) (match-end 0))
  5166. end1 (min (point-max) (1- (match-beginning 0))))
  5167. (setq block-end (match-beginning 0))
  5168. (when quoting
  5169. (remove-text-properties beg end
  5170. '(display t invisible t intangible t)))
  5171. (add-text-properties
  5172. beg end
  5173. '(font-lock-fontified t font-lock-multiline t))
  5174. (add-text-properties beg beg1 '(face org-meta-line))
  5175. (add-text-properties end1 (min (point-max) (1+ end))
  5176. '(face org-meta-line)) ; for end_src
  5177. (cond
  5178. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5179. (org-src-font-lock-fontify-block lang block-start block-end)
  5180. ;; remove old background overlays
  5181. (mapc (lambda (ov)
  5182. (if (eq (overlay-get ov 'face) 'org-block-background)
  5183. (delete-overlay ov)))
  5184. (overlays-at (/ (+ beg1 block-end) 2)))
  5185. ;; add a background overlay
  5186. (setq ovl (make-overlay beg1 block-end))
  5187. (overlay-put ovl 'face 'org-block-background)
  5188. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5189. (quoting
  5190. (add-text-properties beg1 (min (point-max) (1+ end1))
  5191. '(face org-block))) ; end of source block
  5192. ((not org-fontify-quote-and-verse-blocks))
  5193. ((string= block-type "quote")
  5194. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5195. ((string= block-type "verse")
  5196. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5197. (add-text-properties beg beg1 '(face org-block-begin-line))
  5198. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5199. '(face org-block-end-line))
  5200. t))
  5201. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5202. (add-text-properties
  5203. beg (match-end 3)
  5204. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5205. '(font-lock-fontified t invisible t)
  5206. '(font-lock-fontified t face org-document-info-keyword)))
  5207. (add-text-properties
  5208. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5209. (if (string-equal dc1 "+title:")
  5210. '(font-lock-fontified t face org-document-title)
  5211. '(font-lock-fontified t face org-document-info))))
  5212. ((or (equal dc1 "+results")
  5213. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5214. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5215. "+call:" "+header:" "+headers:" "+name:"))
  5216. (and (match-end 4) (equal dc3 "+attr")))
  5217. (add-text-properties
  5218. beg (match-end 0)
  5219. '(font-lock-fontified t face org-meta-line))
  5220. t)
  5221. ((member dc3 '(" " ""))
  5222. (add-text-properties
  5223. beg (match-end 0)
  5224. '(font-lock-fontified t face font-lock-comment-face)))
  5225. ((not (member (char-after beg) '(?\ ?\t)))
  5226. ;; just any other in-buffer setting, but not indented
  5227. (add-text-properties
  5228. beg (match-end 0)
  5229. '(font-lock-fontified t face org-meta-line))
  5230. t)
  5231. (t nil))))))
  5232. (defun org-activate-angle-links (limit)
  5233. "Run through the buffer and add overlays to links."
  5234. (if (and (re-search-forward org-angle-link-re limit t)
  5235. (not (org-in-src-block-p)))
  5236. (progn
  5237. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5238. (add-text-properties (match-beginning 0) (match-end 0)
  5239. (list 'mouse-face 'highlight
  5240. 'keymap org-mouse-map))
  5241. (org-rear-nonsticky-at (match-end 0))
  5242. t)))
  5243. (defun org-activate-footnote-links (limit)
  5244. "Run through the buffer and add overlays to footnotes."
  5245. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5246. (when fn
  5247. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5248. (org-remove-flyspell-overlays-in beg end)
  5249. (add-text-properties beg end
  5250. (list 'mouse-face 'highlight
  5251. 'keymap org-mouse-map
  5252. 'help-echo
  5253. (if (= (point-at-bol) beg)
  5254. "Footnote definition"
  5255. "Footnote reference")
  5256. 'font-lock-fontified t
  5257. 'font-lock-multiline t
  5258. 'face 'org-footnote))))))
  5259. (defun org-activate-bracket-links (limit)
  5260. "Run through the buffer and add overlays to bracketed links."
  5261. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5262. (not (org-in-src-block-p)))
  5263. (let* ((hl (org-match-string-no-properties 1))
  5264. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5265. (ip (org-maybe-intangible
  5266. (list 'invisible 'org-link
  5267. 'keymap org-mouse-map 'mouse-face 'highlight
  5268. 'font-lock-multiline t 'help-echo help
  5269. 'htmlize-link `(:uri ,hl))))
  5270. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5271. 'font-lock-multiline t 'help-echo help
  5272. 'htmlize-link `(:uri ,hl))))
  5273. ;; We need to remove the invisible property here. Table narrowing
  5274. ;; may have made some of this invisible.
  5275. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5276. (remove-text-properties (match-beginning 0) (match-end 0)
  5277. '(invisible nil))
  5278. (if (match-end 3)
  5279. (progn
  5280. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5281. (org-rear-nonsticky-at (match-beginning 3))
  5282. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5283. (org-rear-nonsticky-at (match-end 3))
  5284. (add-text-properties (match-end 3) (match-end 0) ip)
  5285. (org-rear-nonsticky-at (match-end 0)))
  5286. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5287. (org-rear-nonsticky-at (match-beginning 1))
  5288. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5289. (org-rear-nonsticky-at (match-end 1))
  5290. (add-text-properties (match-end 1) (match-end 0) ip)
  5291. (org-rear-nonsticky-at (match-end 0)))
  5292. t)))
  5293. (defun org-activate-dates (limit)
  5294. "Run through the buffer and add overlays to dates."
  5295. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5296. (not (equal (char-before (match-beginning 0)) 91)))
  5297. (progn
  5298. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5299. (add-text-properties (match-beginning 0) (match-end 0)
  5300. (list 'mouse-face 'highlight
  5301. 'keymap org-mouse-map))
  5302. (org-rear-nonsticky-at (match-end 0))
  5303. (when org-display-custom-times
  5304. (if (match-end 3)
  5305. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5306. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5307. t)))
  5308. (defvar org-target-link-regexp nil
  5309. "Regular expression matching radio targets in plain text.")
  5310. (make-variable-buffer-local 'org-target-link-regexp)
  5311. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5312. "Regular expression matching a link target.")
  5313. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5314. "Regular expression matching a radio target.")
  5315. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5316. "Regular expression matching any target.")
  5317. (defun org-activate-target-links (limit)
  5318. "Run through the buffer and add overlays to target matches."
  5319. (when org-target-link-regexp
  5320. (let ((case-fold-search t))
  5321. (if (re-search-forward org-target-link-regexp limit t)
  5322. (progn
  5323. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5324. (add-text-properties (match-beginning 0) (match-end 0)
  5325. (list 'mouse-face 'highlight
  5326. 'keymap org-mouse-map
  5327. 'help-echo "Radio target link"
  5328. 'org-linked-text t))
  5329. (org-rear-nonsticky-at (match-end 0))
  5330. t)))))
  5331. (defun org-update-radio-target-regexp ()
  5332. "Find all radio targets in this file and update the regular expression."
  5333. (interactive)
  5334. (when (memq 'radio org-activate-links)
  5335. (setq org-target-link-regexp
  5336. (org-make-target-link-regexp (org-all-targets 'radio)))
  5337. (org-restart-font-lock)))
  5338. (defun org-hide-wide-columns (limit)
  5339. (let (s e)
  5340. (setq s (text-property-any (point) (or limit (point-max))
  5341. 'org-cwidth t))
  5342. (when s
  5343. (setq e (next-single-property-change s 'org-cwidth))
  5344. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5345. (goto-char e)
  5346. t)))
  5347. (defvar org-latex-and-related-regexp nil
  5348. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5349. (defvar org-match-substring-regexp)
  5350. (defvar org-match-substring-with-braces-regexp)
  5351. (defun org-compute-latex-and-related-regexp ()
  5352. "Compute regular expression for LaTeX, entities and sub/superscript.
  5353. Result depends on variable `org-highlight-latex-and-related'."
  5354. (org-set-local
  5355. 'org-latex-and-related-regexp
  5356. (let* ((re-sub
  5357. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5358. ((eq org-use-sub-superscripts '{})
  5359. (list org-match-substring-with-braces-regexp))
  5360. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5361. (re-latex
  5362. (when (memq 'latex org-highlight-latex-and-related)
  5363. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5364. (delq nil
  5365. (mapcar (lambda (x)
  5366. (and (member (car x) matchers) (nth 1 x)))
  5367. org-latex-regexps)))))
  5368. (re-entities
  5369. (when (memq 'entities org-highlight-latex-and-related)
  5370. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5371. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5372. (defun org-do-latex-and-related (limit)
  5373. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5374. LIMIT bounds the search for syntax to highlight. Stop at first
  5375. highlighted object, if any. Return t if some highlighting was
  5376. done, nil otherwise."
  5377. (when (org-string-nw-p org-latex-and-related-regexp)
  5378. (catch 'found
  5379. (while (re-search-forward org-latex-and-related-regexp limit t)
  5380. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5381. 'face))
  5382. '(org-code org-verbatim underline))
  5383. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5384. '(?_ ?^))
  5385. 1
  5386. 0)))
  5387. (font-lock-prepend-text-property
  5388. (+ offset (match-beginning 0)) (match-end 0)
  5389. 'face 'org-latex-and-related)
  5390. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5391. '(font-lock-multiline t)))
  5392. (throw 'found t)))
  5393. nil)))
  5394. (defun org-restart-font-lock ()
  5395. "Restart `font-lock-mode', to force refontification."
  5396. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5397. (font-lock-mode -1)
  5398. (font-lock-mode 1)))
  5399. (defun org-all-targets (&optional radio)
  5400. "Return a list of all targets in this file.
  5401. When optional argument RADIO is non-nil, only find radio
  5402. targets."
  5403. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5404. (save-excursion
  5405. (goto-char (point-min))
  5406. (while (re-search-forward re nil t)
  5407. ;; Make sure point is really within the object.
  5408. (backward-char)
  5409. (let ((obj (org-element-context)))
  5410. (when (memq (org-element-type obj) '(radio-target target))
  5411. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5412. rtn)))
  5413. (defun org-make-target-link-regexp (targets)
  5414. "Make regular expression matching all strings in TARGETS.
  5415. The regular expression finds the targets also if there is a line break
  5416. between words."
  5417. (and targets
  5418. (concat
  5419. "\\<\\("
  5420. (mapconcat
  5421. (lambda (x)
  5422. (setq x (regexp-quote x))
  5423. (while (string-match " +" x)
  5424. (setq x (replace-match "\\s-+" t t x)))
  5425. x)
  5426. targets
  5427. "\\|")
  5428. "\\)\\>")))
  5429. (defun org-activate-tags (limit)
  5430. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5431. (progn
  5432. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5433. (add-text-properties (match-beginning 1) (match-end 1)
  5434. (list 'mouse-face 'highlight
  5435. 'keymap org-mouse-map))
  5436. (org-rear-nonsticky-at (match-end 1))
  5437. t)))
  5438. (defun org-outline-level ()
  5439. "Compute the outline level of the heading at point.
  5440. If this is called at a normal headline, the level is the number of stars.
  5441. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5442. (save-excursion
  5443. (if (not (condition-case nil
  5444. (org-back-to-heading t)
  5445. (error nil)))
  5446. 0
  5447. (looking-at org-outline-regexp)
  5448. (1- (- (match-end 0) (match-beginning 0))))))
  5449. (defvar org-font-lock-keywords nil)
  5450. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5451. "Regular expression matching a property line.")
  5452. (defvar org-font-lock-hook nil
  5453. "Functions to be called for special font lock stuff.")
  5454. (defvar org-font-lock-set-keywords-hook nil
  5455. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5456. This is called after `org-font-lock-extra-keywords' is defined, but before
  5457. it is installed to be used by font lock. This can be useful if something
  5458. needs to be inserted at a specific position in the font-lock sequence.")
  5459. (defun org-font-lock-hook (limit)
  5460. "Run `org-font-lock-hook' within LIMIT."
  5461. (run-hook-with-args 'org-font-lock-hook limit))
  5462. (defun org-set-font-lock-defaults ()
  5463. "Set font lock defaults for the current buffer."
  5464. (let* ((em org-fontify-emphasized-text)
  5465. (lk org-activate-links)
  5466. (org-font-lock-extra-keywords
  5467. (list
  5468. ;; Call the hook
  5469. '(org-font-lock-hook)
  5470. ;; Headlines
  5471. `(,(if org-fontify-whole-heading-line
  5472. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5473. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5474. (1 (org-get-level-face 1))
  5475. (2 (org-get-level-face 2))
  5476. (3 (org-get-level-face 3)))
  5477. ;; Table lines
  5478. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5479. (1 'org-table t))
  5480. ;; Table internals
  5481. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5482. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5483. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5484. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5485. ;; Drawers
  5486. (list org-drawer-regexp '(0 'org-special-keyword t))
  5487. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5488. ;; Properties
  5489. (list org-property-re
  5490. '(1 'org-special-keyword t)
  5491. '(3 'org-property-value t))
  5492. ;; Links
  5493. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5494. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5495. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5496. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5497. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5498. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5499. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5500. ;; Targets.
  5501. (list org-any-target-regexp '(0 'org-target t))
  5502. ;; Diary sexps.
  5503. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5504. ;; Macro
  5505. '("{{{.+}}}" (0 'org-macro t))
  5506. '(org-hide-wide-columns (0 nil append))
  5507. ;; TODO keyword
  5508. (list (format org-heading-keyword-regexp-format
  5509. org-todo-regexp)
  5510. '(2 (org-get-todo-face 2) t))
  5511. ;; DONE
  5512. (if org-fontify-done-headline
  5513. (list (format org-heading-keyword-regexp-format
  5514. (concat
  5515. "\\(?:"
  5516. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5517. "\\)"))
  5518. '(2 'org-headline-done t))
  5519. nil)
  5520. ;; Priorities
  5521. '(org-font-lock-add-priority-faces)
  5522. ;; Tags
  5523. '(org-font-lock-add-tag-faces)
  5524. ;; Tags groups
  5525. (if (and org-group-tags org-tag-groups-alist)
  5526. (list (concat org-outline-regexp-bol ".+\\(:"
  5527. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5528. ":\\).*$")
  5529. '(1 'org-tag-group prepend)))
  5530. ;; Special keywords
  5531. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5532. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5533. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5534. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5535. ;; Emphasis
  5536. (if em
  5537. (if (featurep 'xemacs)
  5538. '(org-do-emphasis-faces (0 nil append))
  5539. '(org-do-emphasis-faces)))
  5540. ;; Checkboxes
  5541. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5542. 1 'org-checkbox prepend)
  5543. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5544. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5545. (0 (org-get-checkbox-statistics-face) t)))
  5546. ;; Description list items
  5547. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5548. 1 'org-list-dt prepend)
  5549. ;; ARCHIVEd headings
  5550. (list (concat
  5551. org-outline-regexp-bol
  5552. "\\(.*:" org-archive-tag ":.*\\)")
  5553. '(1 'org-archived prepend))
  5554. ;; Specials
  5555. '(org-do-latex-and-related)
  5556. '(org-fontify-entities)
  5557. '(org-raise-scripts)
  5558. ;; Code
  5559. '(org-activate-code (1 'org-code t))
  5560. ;; COMMENT
  5561. (list (format org-heading-keyword-regexp-format
  5562. (concat "\\("
  5563. org-comment-string "\\|" org-quote-string
  5564. "\\)"))
  5565. '(2 'org-special-keyword t))
  5566. ;; Blocks and meta lines
  5567. '(org-fontify-meta-lines-and-blocks))))
  5568. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5569. (run-hooks 'org-font-lock-set-keywords-hook)
  5570. ;; Now set the full font-lock-keywords
  5571. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5572. (org-set-local 'font-lock-defaults
  5573. '(org-font-lock-keywords t nil nil backward-paragraph))
  5574. (kill-local-variable 'font-lock-keywords) nil))
  5575. (defun org-toggle-pretty-entities ()
  5576. "Toggle the composition display of entities as UTF8 characters."
  5577. (interactive)
  5578. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5579. (org-restart-font-lock)
  5580. (if org-pretty-entities
  5581. (message "Entities are now displayed as UTF8 characters")
  5582. (save-restriction
  5583. (widen)
  5584. (org-decompose-region (point-min) (point-max))
  5585. (message "Entities are now displayed as plain text"))))
  5586. (defvar org-custom-properties-overlays nil
  5587. "List of overlays used for custom properties.")
  5588. (make-variable-buffer-local 'org-custom-properties-overlays)
  5589. (defun org-toggle-custom-properties-visibility ()
  5590. "Display or hide properties in `org-custom-properties'."
  5591. (interactive)
  5592. (if org-custom-properties-overlays
  5593. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5594. (setq org-custom-properties-overlays nil))
  5595. (unless (not org-custom-properties)
  5596. (save-excursion
  5597. (save-restriction
  5598. (widen)
  5599. (goto-char (point-min))
  5600. (while (re-search-forward org-property-re nil t)
  5601. (mapc (lambda(p)
  5602. (when (equal p (substring (match-string 1) 1 -1))
  5603. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5604. (overlay-put o 'invisible t)
  5605. (overlay-put o 'org-custom-property t)
  5606. (push o org-custom-properties-overlays))))
  5607. org-custom-properties)))))))
  5608. (defun org-fontify-entities (limit)
  5609. "Find an entity to fontify."
  5610. (let (ee)
  5611. (when org-pretty-entities
  5612. (catch 'match
  5613. (while (re-search-forward
  5614. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5615. limit t)
  5616. (if (and (not (org-in-indented-comment-line))
  5617. (setq ee (org-entity-get (match-string 1)))
  5618. (= (length (nth 6 ee)) 1))
  5619. (let*
  5620. ((end (if (equal (match-string 2) "{}")
  5621. (match-end 2)
  5622. (match-end 1))))
  5623. (add-text-properties
  5624. (match-beginning 0) end
  5625. (list 'font-lock-fontified t))
  5626. (compose-region (match-beginning 0) end
  5627. (nth 6 ee) nil)
  5628. (backward-char 1)
  5629. (throw 'match t))))
  5630. nil))))
  5631. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5632. "Fontify string S like in Org-mode."
  5633. (with-temp-buffer
  5634. (insert s)
  5635. (let ((org-odd-levels-only odd-levels))
  5636. (org-mode)
  5637. (font-lock-fontify-buffer)
  5638. (buffer-string))))
  5639. (defvar org-m nil)
  5640. (defvar org-l nil)
  5641. (defvar org-f nil)
  5642. (defun org-get-level-face (n)
  5643. "Get the right face for match N in font-lock matching of headlines."
  5644. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5645. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5646. (if org-cycle-level-faces
  5647. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5648. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5649. (cond
  5650. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5651. ((eq n 2) org-f)
  5652. (t (if org-level-color-stars-only nil org-f))))
  5653. (defun org-get-todo-face (kwd)
  5654. "Get the right face for a TODO keyword KWD.
  5655. If KWD is a number, get the corresponding match group."
  5656. (if (numberp kwd) (setq kwd (match-string kwd)))
  5657. (or (org-face-from-face-or-color
  5658. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5659. (and (member kwd org-done-keywords) 'org-done)
  5660. 'org-todo))
  5661. (defun org-face-from-face-or-color (context inherit face-or-color)
  5662. "Create a face list that inherits INHERIT, but sets the foreground color.
  5663. When FACE-OR-COLOR is not a string, just return it."
  5664. (if (stringp face-or-color)
  5665. (list :inherit inherit
  5666. (cdr (assoc context org-faces-easy-properties))
  5667. face-or-color)
  5668. face-or-color))
  5669. (defun org-font-lock-add-tag-faces (limit)
  5670. "Add the special tag faces."
  5671. (when (and org-tag-faces org-tags-special-faces-re)
  5672. (while (re-search-forward org-tags-special-faces-re limit t)
  5673. (add-text-properties (match-beginning 1) (match-end 1)
  5674. (list 'face (org-get-tag-face 1)
  5675. 'font-lock-fontified t))
  5676. (backward-char 1))))
  5677. (defun org-font-lock-add-priority-faces (limit)
  5678. "Add the special priority faces."
  5679. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5680. (when (save-match-data (org-at-heading-p))
  5681. (add-text-properties
  5682. (match-beginning 0) (match-end 0)
  5683. (list 'face (or (org-face-from-face-or-color
  5684. 'priority 'org-priority
  5685. (cdr (assoc (char-after (match-beginning 1))
  5686. org-priority-faces)))
  5687. 'org-priority)
  5688. 'font-lock-fontified t)))))
  5689. (defun org-get-tag-face (kwd)
  5690. "Get the right face for a TODO keyword KWD.
  5691. If KWD is a number, get the corresponding match group."
  5692. (if (numberp kwd) (setq kwd (match-string kwd)))
  5693. (or (org-face-from-face-or-color
  5694. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5695. 'org-tag))
  5696. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5697. "Remove fontification and activation overlays from links."
  5698. (font-lock-default-unfontify-region beg end)
  5699. (let* ((buffer-undo-list t)
  5700. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5701. (inhibit-modification-hooks t)
  5702. deactivate-mark buffer-file-name buffer-file-truename)
  5703. (org-decompose-region beg end)
  5704. (remove-text-properties beg end
  5705. '(mouse-face t keymap t org-linked-text t
  5706. invisible t intangible t
  5707. org-no-flyspell t org-emphasis t))
  5708. (org-remove-font-lock-display-properties beg end)))
  5709. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5710. ((raise 0.3) (height 0.7))
  5711. ((raise -0.5))
  5712. ((raise 0.5)))
  5713. "Display properties for showing superscripts and subscripts.")
  5714. (defun org-remove-font-lock-display-properties (beg end)
  5715. "Remove specific display properties that have been added by font lock.
  5716. The will remove the raise properties that are used to show superscripts
  5717. and subscripts."
  5718. (let (next prop)
  5719. (while (< beg end)
  5720. (setq next (next-single-property-change beg 'display nil end)
  5721. prop (get-text-property beg 'display))
  5722. (if (member prop org-script-display)
  5723. (put-text-property beg next 'display nil))
  5724. (setq beg next))))
  5725. (defun org-raise-scripts (limit)
  5726. "Add raise properties to sub/superscripts."
  5727. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5728. (if (re-search-forward
  5729. (if (eq org-use-sub-superscripts t)
  5730. org-match-substring-regexp
  5731. org-match-substring-with-braces-regexp)
  5732. limit t)
  5733. (let* ((pos (point)) table-p comment-p
  5734. (mpos (match-beginning 3))
  5735. (emph-p (get-text-property mpos 'org-emphasis))
  5736. (link-p (get-text-property mpos 'mouse-face))
  5737. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5738. (goto-char (point-at-bol))
  5739. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5740. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5741. (goto-char pos)
  5742. ;; Handle a_b^c
  5743. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5744. (if (or comment-p emph-p link-p keyw-p)
  5745. t
  5746. (put-text-property (match-beginning 3) (match-end 0)
  5747. 'display
  5748. (if (equal (char-after (match-beginning 2)) ?^)
  5749. (nth (if table-p 3 1) org-script-display)
  5750. (nth (if table-p 2 0) org-script-display)))
  5751. (add-text-properties (match-beginning 2) (match-end 2)
  5752. (list 'invisible t
  5753. 'org-dwidth t 'org-dwidth-n 1))
  5754. (if (and (eq (char-after (match-beginning 3)) ?{)
  5755. (eq (char-before (match-end 3)) ?}))
  5756. (progn
  5757. (add-text-properties
  5758. (match-beginning 3) (1+ (match-beginning 3))
  5759. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5760. (add-text-properties
  5761. (1- (match-end 3)) (match-end 3)
  5762. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5763. t)))))
  5764. ;;;; Visibility cycling, including org-goto and indirect buffer
  5765. ;;; Cycling
  5766. (defvar org-cycle-global-status nil)
  5767. (make-variable-buffer-local 'org-cycle-global-status)
  5768. (put 'org-cycle-global-status 'org-state t)
  5769. (defvar org-cycle-subtree-status nil)
  5770. (make-variable-buffer-local 'org-cycle-subtree-status)
  5771. (put 'org-cycle-subtree-status 'org-state t)
  5772. (defvar org-inlinetask-min-level)
  5773. ;;;###autoload
  5774. (defun org-cycle (&optional arg)
  5775. "TAB-action and visibility cycling for Org-mode.
  5776. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5777. is outline visibility cycling, but it also invokes other actions
  5778. in special contexts.
  5779. - When this function is called with a prefix argument, rotate the entire
  5780. buffer through 3 states (global cycling)
  5781. 1. OVERVIEW: Show only top-level headlines.
  5782. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5783. 3. SHOW ALL: Show everything.
  5784. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5785. determined by the variable `org-startup-folded', and by any VISIBILITY
  5786. properties in the buffer.
  5787. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5788. including any drawers.
  5789. - When inside a table, re-align the table and move to the next field.
  5790. - When point is at the beginning of a headline, rotate the subtree started
  5791. by this line through 3 different states (local cycling)
  5792. 1. FOLDED: Only the main headline is shown.
  5793. 2. CHILDREN: The main headline and the direct children are shown.
  5794. From this state, you can move to one of the children
  5795. and zoom in further.
  5796. 3. SUBTREE: Show the entire subtree, including body text.
  5797. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5798. - When point is at the beginning of an empty headline and the variable
  5799. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5800. of the headline by demoting and promoting it to likely levels. This
  5801. speeds up creation document structure by pressing TAB once or several
  5802. times right after creating a new headline.
  5803. - When there is a numeric prefix, go up to a heading with level ARG, do
  5804. a `show-subtree' and return to the previous cursor position. If ARG
  5805. is negative, go up that many levels.
  5806. - When point is not at the beginning of a headline, execute the global
  5807. binding for TAB, which is re-indenting the line. See the option
  5808. `org-cycle-emulate-tab' for details.
  5809. - Special case: if point is at the beginning of the buffer and there is
  5810. no headline in line 1, this function will act as if called with prefix arg
  5811. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5812. But only if also the variable `org-cycle-global-at-bob' is t."
  5813. (interactive "P")
  5814. (org-load-modules-maybe)
  5815. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5816. (and org-cycle-level-after-item/entry-creation
  5817. (or (org-cycle-level)
  5818. (org-cycle-item-indentation))))
  5819. (let* (message-log-max ; Don't populate the *Messages* buffer
  5820. (limit-level
  5821. (or org-cycle-max-level
  5822. (and (boundp 'org-inlinetask-min-level)
  5823. org-inlinetask-min-level
  5824. (1- org-inlinetask-min-level))))
  5825. (nstars (and limit-level
  5826. (if org-odd-levels-only
  5827. (and limit-level (1- (* limit-level 2)))
  5828. limit-level)))
  5829. (org-outline-regexp
  5830. (if (not (derived-mode-p 'org-mode))
  5831. outline-regexp
  5832. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5833. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5834. (not (looking-at org-outline-regexp))))
  5835. (org-cycle-hook
  5836. (if bob-special
  5837. (delq 'org-optimize-window-after-visibility-change
  5838. (copy-sequence org-cycle-hook))
  5839. org-cycle-hook))
  5840. (pos (point)))
  5841. (if (or bob-special (equal arg '(4)))
  5842. ;; special case: use global cycling
  5843. (setq arg t))
  5844. (cond
  5845. ((equal arg '(16))
  5846. (setq last-command 'dummy)
  5847. (org-set-startup-visibility)
  5848. (message "Startup visibility, plus VISIBILITY properties"))
  5849. ((equal arg '(64))
  5850. (show-all)
  5851. (message "Entire buffer visible, including drawers"))
  5852. ;; Table: enter it or move to the next field.
  5853. ((org-at-table-p 'any)
  5854. (if (org-at-table.el-p)
  5855. (message "Use C-c ' to edit table.el tables")
  5856. (if arg (org-table-edit-field t)
  5857. (org-table-justify-field-maybe)
  5858. (call-interactively 'org-table-next-field))))
  5859. ((run-hook-with-args-until-success
  5860. 'org-tab-after-check-for-table-hook))
  5861. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5862. ((eq arg t) (org-cycle-internal-global))
  5863. ;; Drawers: delegate to `org-flag-drawer'.
  5864. ((and org-drawers org-drawer-regexp
  5865. (save-excursion
  5866. (beginning-of-line 1)
  5867. (looking-at org-drawer-regexp)))
  5868. (org-flag-drawer ; toggle block visibility
  5869. (not (get-char-property (match-end 0) 'invisible))))
  5870. ;; Show-subtree, ARG levels up from here.
  5871. ((integerp arg)
  5872. (save-excursion
  5873. (org-back-to-heading)
  5874. (outline-up-heading (if (< arg 0) (- arg)
  5875. (- (funcall outline-level) arg)))
  5876. (org-show-subtree)))
  5877. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5878. ((and (featurep 'org-inlinetask)
  5879. (org-inlinetask-at-task-p)
  5880. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5881. (org-inlinetask-toggle-visibility))
  5882. ((org-try-cdlatex-tab))
  5883. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5884. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5885. (save-excursion (beginning-of-line 1)
  5886. (looking-at org-outline-regexp)))
  5887. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5888. (org-cycle-internal-local))
  5889. ;; From there: TAB emulation and template completion.
  5890. (buffer-read-only (org-back-to-heading))
  5891. ((run-hook-with-args-until-success
  5892. 'org-tab-after-check-for-cycling-hook))
  5893. ((org-try-structure-completion))
  5894. ((run-hook-with-args-until-success
  5895. 'org-tab-before-tab-emulation-hook))
  5896. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5897. (or (not (bolp))
  5898. (not (looking-at org-outline-regexp))))
  5899. (call-interactively (global-key-binding "\t")))
  5900. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5901. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5902. (or (and (eq org-cycle-emulate-tab 'white)
  5903. (= (match-end 0) (point-at-eol)))
  5904. (and (eq org-cycle-emulate-tab 'whitestart)
  5905. (>= (match-end 0) pos))))
  5906. t
  5907. (eq org-cycle-emulate-tab t))
  5908. (call-interactively (global-key-binding "\t")))
  5909. (t (save-excursion
  5910. (org-back-to-heading)
  5911. (org-cycle)))))))
  5912. (defun org-cycle-internal-global ()
  5913. "Do the global cycling action."
  5914. ;; Hack to avoid display of messages for .org attachments in Gnus
  5915. (let (message-log-max ; Don't populate the *Messages* buffer
  5916. (ga (string-match "\\*fontification" (buffer-name))))
  5917. (cond
  5918. ((and (eq last-command this-command)
  5919. (eq org-cycle-global-status 'overview))
  5920. ;; We just created the overview - now do table of contents
  5921. ;; This can be slow in very large buffers, so indicate action
  5922. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5923. (unless ga (message "CONTENTS..."))
  5924. (org-content)
  5925. (unless ga (message "CONTENTS...done"))
  5926. (setq org-cycle-global-status 'contents)
  5927. (run-hook-with-args 'org-cycle-hook 'contents))
  5928. ((and (eq last-command this-command)
  5929. (eq org-cycle-global-status 'contents))
  5930. ;; We just showed the table of contents - now show everything
  5931. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5932. (show-all)
  5933. (unless ga (message "SHOW ALL"))
  5934. (setq org-cycle-global-status 'all)
  5935. (run-hook-with-args 'org-cycle-hook 'all))
  5936. (t
  5937. ;; Default action: go to overview
  5938. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5939. (org-overview)
  5940. (unless ga (message "OVERVIEW"))
  5941. (setq org-cycle-global-status 'overview)
  5942. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5943. (defun org-cycle-internal-local ()
  5944. "Do the local cycling action."
  5945. (let (message-log-max ; Don't populate the *Messages* buffer
  5946. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5947. ;; First, determine end of headline (EOH), end of subtree or item
  5948. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5949. (save-excursion
  5950. (if (org-at-item-p)
  5951. (progn
  5952. (beginning-of-line)
  5953. (setq struct (org-list-struct))
  5954. (setq eoh (point-at-eol))
  5955. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5956. (setq has-children (org-list-has-child-p (point) struct)))
  5957. (org-back-to-heading)
  5958. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5959. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5960. (setq has-children
  5961. (or (save-excursion
  5962. (let ((level (funcall outline-level)))
  5963. (outline-next-heading)
  5964. (and (org-at-heading-p t)
  5965. (> (funcall outline-level) level))))
  5966. (save-excursion
  5967. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5968. ;; Determine end invisible part of buffer (EOL)
  5969. (beginning-of-line 2)
  5970. ;; XEmacs doesn't have `next-single-char-property-change'
  5971. (if (featurep 'xemacs)
  5972. (while (and (not (eobp)) ;; this is like `next-line'
  5973. (get-char-property (1- (point)) 'invisible))
  5974. (beginning-of-line 2))
  5975. (while (and (not (eobp)) ;; this is like `next-line'
  5976. (get-char-property (1- (point)) 'invisible))
  5977. (goto-char (next-single-char-property-change (point) 'invisible))
  5978. (and (eolp) (beginning-of-line 2))))
  5979. (setq eol (point)))
  5980. ;; Find out what to do next and set `this-command'
  5981. (cond
  5982. ((= eos eoh)
  5983. ;; Nothing is hidden behind this heading
  5984. (unless (org-before-first-heading-p)
  5985. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5986. (message "EMPTY ENTRY")
  5987. (setq org-cycle-subtree-status nil)
  5988. (save-excursion
  5989. (goto-char eos)
  5990. (outline-next-heading)
  5991. (if (outline-invisible-p) (org-flag-heading nil))))
  5992. ((and (or (>= eol eos)
  5993. (not (string-match "\\S-" (buffer-substring eol eos))))
  5994. (or has-children
  5995. (not (setq children-skipped
  5996. org-cycle-skip-children-state-if-no-children))))
  5997. ;; Entire subtree is hidden in one line: children view
  5998. (unless (org-before-first-heading-p)
  5999. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6000. (if (org-at-item-p)
  6001. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6002. (org-show-entry)
  6003. (org-with-limited-levels (show-children))
  6004. ;; FIXME: This slows down the func way too much.
  6005. ;; How keep drawers hidden in subtree anyway?
  6006. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6007. ;; (org-cycle-hide-drawers 'subtree))
  6008. ;; Fold every list in subtree to top-level items.
  6009. (when (eq org-cycle-include-plain-lists 'integrate)
  6010. (save-excursion
  6011. (org-back-to-heading)
  6012. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6013. (beginning-of-line 1)
  6014. (let* ((struct (org-list-struct))
  6015. (prevs (org-list-prevs-alist struct))
  6016. (end (org-list-get-bottom-point struct)))
  6017. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6018. (org-list-get-all-items (point) struct prevs))
  6019. (goto-char (if (< end eos) end eos)))))))
  6020. (message "CHILDREN")
  6021. (save-excursion
  6022. (goto-char eos)
  6023. (outline-next-heading)
  6024. (if (outline-invisible-p) (org-flag-heading nil)))
  6025. (setq org-cycle-subtree-status 'children)
  6026. (unless (org-before-first-heading-p)
  6027. (run-hook-with-args 'org-cycle-hook 'children)))
  6028. ((or children-skipped
  6029. (and (eq last-command this-command)
  6030. (eq org-cycle-subtree-status 'children)))
  6031. ;; We just showed the children, or no children are there,
  6032. ;; now show everything.
  6033. (unless (org-before-first-heading-p)
  6034. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6035. (outline-flag-region eoh eos nil)
  6036. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6037. (setq org-cycle-subtree-status 'subtree)
  6038. (unless (org-before-first-heading-p)
  6039. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6040. (t
  6041. ;; Default action: hide the subtree.
  6042. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6043. (outline-flag-region eoh eos t)
  6044. (message "FOLDED")
  6045. (setq org-cycle-subtree-status 'folded)
  6046. (unless (org-before-first-heading-p)
  6047. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6048. ;;;###autoload
  6049. (defun org-global-cycle (&optional arg)
  6050. "Cycle the global visibility. For details see `org-cycle'.
  6051. With \\[universal-argument] prefix arg, switch to startup visibility.
  6052. With a numeric prefix, show all headlines up to that level."
  6053. (interactive "P")
  6054. (let ((org-cycle-include-plain-lists
  6055. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6056. (cond
  6057. ((integerp arg)
  6058. (show-all)
  6059. (hide-sublevels arg)
  6060. (setq org-cycle-global-status 'contents))
  6061. ((equal arg '(4))
  6062. (org-set-startup-visibility)
  6063. (message "Startup visibility, plus VISIBILITY properties."))
  6064. (t
  6065. (org-cycle '(4))))))
  6066. (defun org-set-startup-visibility ()
  6067. "Set the visibility required by startup options and properties."
  6068. (cond
  6069. ((eq org-startup-folded t)
  6070. (org-cycle '(4)))
  6071. ((eq org-startup-folded 'content)
  6072. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6073. (org-cycle '(4)) (org-cycle '(4)))))
  6074. (unless (eq org-startup-folded 'showeverything)
  6075. (if org-hide-block-startup (org-hide-block-all))
  6076. (org-set-visibility-according-to-property 'no-cleanup)
  6077. (org-cycle-hide-archived-subtrees 'all)
  6078. (org-cycle-hide-drawers 'all)
  6079. (org-cycle-show-empty-lines t)))
  6080. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6081. "Switch subtree visibilities according to :VISIBILITY: property."
  6082. (interactive)
  6083. (let (org-show-entry-below state)
  6084. (save-excursion
  6085. (goto-char (point-min))
  6086. (while (re-search-forward
  6087. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6088. nil t)
  6089. (setq state (match-string 1))
  6090. (save-excursion
  6091. (org-back-to-heading t)
  6092. (hide-subtree)
  6093. (org-reveal)
  6094. (cond
  6095. ((equal state '("fold" "folded"))
  6096. (hide-subtree))
  6097. ((equal state "children")
  6098. (org-show-hidden-entry)
  6099. (show-children))
  6100. ((equal state "content")
  6101. (save-excursion
  6102. (save-restriction
  6103. (org-narrow-to-subtree)
  6104. (org-content))))
  6105. ((member state '("all" "showall"))
  6106. (show-subtree)))))
  6107. (unless no-cleanup
  6108. (org-cycle-hide-archived-subtrees 'all)
  6109. (org-cycle-hide-drawers 'all)
  6110. (org-cycle-show-empty-lines 'all)))))
  6111. ;; This function uses outline-regexp instead of the more fundamental
  6112. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6113. ;; buffers, where outline-regexp is needed.
  6114. (defun org-overview ()
  6115. "Switch to overview mode, showing only top-level headlines.
  6116. Really, this shows all headlines with level equal or greater than the level
  6117. of the first headline in the buffer. This is important, because if the
  6118. first headline is not level one, then (hide-sublevels 1) gives confusing
  6119. results."
  6120. (interactive)
  6121. (let ((l (org-current-line))
  6122. (level (save-excursion
  6123. (goto-char (point-min))
  6124. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6125. (progn
  6126. (goto-char (match-beginning 0))
  6127. (funcall outline-level))))))
  6128. (and level (hide-sublevels level))
  6129. (recenter '(4))
  6130. (org-goto-line l)))
  6131. (defun org-content (&optional arg)
  6132. "Show all headlines in the buffer, like a table of contents.
  6133. With numerical argument N, show content up to level N."
  6134. (interactive "P")
  6135. (save-excursion
  6136. ;; Visit all headings and show their offspring
  6137. (and (integerp arg) (org-overview))
  6138. (goto-char (point-max))
  6139. (catch 'exit
  6140. (while (and (progn (condition-case nil
  6141. (outline-previous-visible-heading 1)
  6142. (error (goto-char (point-min))))
  6143. t)
  6144. (looking-at org-outline-regexp))
  6145. (if (integerp arg)
  6146. (show-children (1- arg))
  6147. (show-branches))
  6148. (if (bobp) (throw 'exit nil))))))
  6149. (defun org-optimize-window-after-visibility-change (state)
  6150. "Adjust the window after a change in outline visibility.
  6151. This function is the default value of the hook `org-cycle-hook'."
  6152. (when (get-buffer-window (current-buffer))
  6153. (cond
  6154. ((eq state 'content) nil)
  6155. ((eq state 'all) nil)
  6156. ((eq state 'folded) nil)
  6157. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6158. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6159. (defun org-remove-empty-overlays-at (pos)
  6160. "Remove outline overlays that do not contain non-white stuff."
  6161. (mapc
  6162. (lambda (o)
  6163. (and (eq 'outline (overlay-get o 'invisible))
  6164. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6165. (overlay-end o))))
  6166. (delete-overlay o)))
  6167. (overlays-at pos)))
  6168. (defun org-clean-visibility-after-subtree-move ()
  6169. "Fix visibility issues after moving a subtree."
  6170. ;; First, find a reasonable region to look at:
  6171. ;; Start two siblings above, end three below
  6172. (let* ((beg (save-excursion
  6173. (and (org-get-last-sibling)
  6174. (org-get-last-sibling))
  6175. (point)))
  6176. (end (save-excursion
  6177. (and (org-get-next-sibling)
  6178. (org-get-next-sibling)
  6179. (org-get-next-sibling))
  6180. (if (org-at-heading-p)
  6181. (point-at-eol)
  6182. (point))))
  6183. (level (looking-at "\\*+"))
  6184. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6185. (save-excursion
  6186. (save-restriction
  6187. (narrow-to-region beg end)
  6188. (when re
  6189. ;; Properly fold already folded siblings
  6190. (goto-char (point-min))
  6191. (while (re-search-forward re nil t)
  6192. (if (and (not (outline-invisible-p))
  6193. (save-excursion
  6194. (goto-char (point-at-eol)) (outline-invisible-p)))
  6195. (hide-entry))))
  6196. (org-cycle-show-empty-lines 'overview)
  6197. (org-cycle-hide-drawers 'overview)))))
  6198. (defun org-cycle-show-empty-lines (state)
  6199. "Show empty lines above all visible headlines.
  6200. The region to be covered depends on STATE when called through
  6201. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6202. entire buffer covered. Note that an empty line is only shown if there
  6203. are at least `org-cycle-separator-lines' empty lines before the headline."
  6204. (when (not (= org-cycle-separator-lines 0))
  6205. (save-excursion
  6206. (let* ((n (abs org-cycle-separator-lines))
  6207. (re (cond
  6208. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6209. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6210. (t (let ((ns (number-to-string (- n 2))))
  6211. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6212. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6213. beg end b e)
  6214. (cond
  6215. ((memq state '(overview contents t))
  6216. (setq beg (point-min) end (point-max)))
  6217. ((memq state '(children folded))
  6218. (setq beg (point) end (progn (org-end-of-subtree t t)
  6219. (beginning-of-line 2)
  6220. (point)))))
  6221. (when beg
  6222. (goto-char beg)
  6223. (while (re-search-forward re end t)
  6224. (unless (get-char-property (match-end 1) 'invisible)
  6225. (setq e (match-end 1))
  6226. (if (< org-cycle-separator-lines 0)
  6227. (setq b (save-excursion
  6228. (goto-char (match-beginning 0))
  6229. (org-back-over-empty-lines)
  6230. (if (save-excursion
  6231. (goto-char (max (point-min) (1- (point))))
  6232. (org-at-heading-p))
  6233. (1- (point))
  6234. (point))))
  6235. (setq b (match-beginning 1)))
  6236. (outline-flag-region b e nil)))))))
  6237. ;; Never hide empty lines at the end of the file.
  6238. (save-excursion
  6239. (goto-char (point-max))
  6240. (outline-previous-heading)
  6241. (outline-end-of-heading)
  6242. (if (and (looking-at "[ \t\n]+")
  6243. (= (match-end 0) (point-max)))
  6244. (outline-flag-region (point) (match-end 0) nil))))
  6245. (defun org-show-empty-lines-in-parent ()
  6246. "Move to the parent and re-show empty lines before visible headlines."
  6247. (save-excursion
  6248. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6249. (org-cycle-show-empty-lines context))))
  6250. (defun org-files-list ()
  6251. "Return `org-agenda-files' list, plus all open org-mode files.
  6252. This is useful for operations that need to scan all of a user's
  6253. open and agenda-wise Org files."
  6254. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6255. (dolist (buf (buffer-list))
  6256. (with-current-buffer buf
  6257. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6258. (let ((file (expand-file-name (buffer-file-name))))
  6259. (unless (member file files)
  6260. (push file files))))))
  6261. files))
  6262. (defsubst org-entry-beginning-position ()
  6263. "Return the beginning position of the current entry."
  6264. (save-excursion (outline-back-to-heading t) (point)))
  6265. (defsubst org-entry-end-position ()
  6266. "Return the end position of the current entry."
  6267. (save-excursion (outline-next-heading) (point)))
  6268. (defun org-cycle-hide-drawers (state)
  6269. "Re-hide all drawers after a visibility state change."
  6270. (when (and (derived-mode-p 'org-mode)
  6271. (not (memq state '(overview folded contents))))
  6272. (save-excursion
  6273. (let* ((globalp (memq state '(contents all)))
  6274. (beg (if globalp (point-min) (point)))
  6275. (end (if globalp (point-max)
  6276. (if (eq state 'children)
  6277. (save-excursion (outline-next-heading) (point))
  6278. (org-end-of-subtree t)))))
  6279. (goto-char beg)
  6280. (while (re-search-forward org-drawer-regexp end t)
  6281. (org-flag-drawer t))))))
  6282. (defun org-cycle-hide-inline-tasks (state)
  6283. "Re-hide inline task when switching to 'contents visibility state."
  6284. (when (and (eq state 'contents)
  6285. (boundp 'org-inlinetask-min-level)
  6286. org-inlinetask-min-level)
  6287. (hide-sublevels (1- org-inlinetask-min-level))))
  6288. (defun org-flag-drawer (flag)
  6289. "When FLAG is non-nil, hide the drawer we are within.
  6290. Otherwise make it visible."
  6291. (save-excursion
  6292. (beginning-of-line 1)
  6293. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6294. (let ((b (match-end 0)))
  6295. (if (re-search-forward
  6296. "^[ \t]*:END:"
  6297. (save-excursion (outline-next-heading) (point)) t)
  6298. (outline-flag-region b (point-at-eol) flag)
  6299. (user-error ":END: line missing at position %s" b))))))
  6300. (defun org-subtree-end-visible-p ()
  6301. "Is the end of the current subtree visible?"
  6302. (pos-visible-in-window-p
  6303. (save-excursion (org-end-of-subtree t) (point))))
  6304. (defun org-first-headline-recenter (&optional N)
  6305. "Move cursor to the first headline and recenter the headline.
  6306. Optional argument N means put the headline into the Nth line of the window."
  6307. (goto-char (point-min))
  6308. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6309. (beginning-of-line)
  6310. (recenter (prefix-numeric-value N))))
  6311. ;;; Saving and restoring visibility
  6312. (defun org-outline-overlay-data (&optional use-markers)
  6313. "Return a list of the locations of all outline overlays.
  6314. These are overlays with the `invisible' property value `outline'.
  6315. The return value is a list of cons cells, with start and stop
  6316. positions for each overlay.
  6317. If USE-MARKERS is set, return the positions as markers."
  6318. (let (beg end)
  6319. (save-excursion
  6320. (save-restriction
  6321. (widen)
  6322. (delq nil
  6323. (mapcar (lambda (o)
  6324. (when (eq (overlay-get o 'invisible) 'outline)
  6325. (setq beg (overlay-start o)
  6326. end (overlay-end o))
  6327. (and beg end (> end beg)
  6328. (if use-markers
  6329. (cons (move-marker (make-marker) beg)
  6330. (move-marker (make-marker) end))
  6331. (cons beg end)))))
  6332. (overlays-in (point-min) (point-max))))))))
  6333. (defun org-set-outline-overlay-data (data)
  6334. "Create visibility overlays for all positions in DATA.
  6335. DATA should have been made by `org-outline-overlay-data'."
  6336. (let (o)
  6337. (save-excursion
  6338. (save-restriction
  6339. (widen)
  6340. (show-all)
  6341. (mapc (lambda (c)
  6342. (outline-flag-region (car c) (cdr c) t))
  6343. data)))))
  6344. ;;; Folding of blocks
  6345. (defvar org-hide-block-overlays nil
  6346. "Overlays hiding blocks.")
  6347. (make-variable-buffer-local 'org-hide-block-overlays)
  6348. (defun org-block-map (function &optional start end)
  6349. "Call FUNCTION at the head of all source blocks in the current buffer.
  6350. Optional arguments START and END can be used to limit the range."
  6351. (let ((start (or start (point-min)))
  6352. (end (or end (point-max))))
  6353. (save-excursion
  6354. (goto-char start)
  6355. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6356. (save-excursion
  6357. (save-match-data
  6358. (goto-char (match-beginning 0))
  6359. (funcall function)))))))
  6360. (defun org-hide-block-toggle-all ()
  6361. "Toggle the visibility of all blocks in the current buffer."
  6362. (org-block-map #'org-hide-block-toggle))
  6363. (defun org-hide-block-all ()
  6364. "Fold all blocks in the current buffer."
  6365. (interactive)
  6366. (org-show-block-all)
  6367. (org-block-map #'org-hide-block-toggle-maybe))
  6368. (defun org-show-block-all ()
  6369. "Unfold all blocks in the current buffer."
  6370. (interactive)
  6371. (mapc 'delete-overlay org-hide-block-overlays)
  6372. (setq org-hide-block-overlays nil))
  6373. (defun org-hide-block-toggle-maybe ()
  6374. "Toggle visibility of block at point."
  6375. (interactive)
  6376. (let ((case-fold-search t))
  6377. (if (save-excursion
  6378. (beginning-of-line 1)
  6379. (looking-at org-block-regexp))
  6380. (progn (org-hide-block-toggle)
  6381. t) ;; to signal that we took action
  6382. nil))) ;; to signal that we did not
  6383. (defun org-hide-block-toggle (&optional force)
  6384. "Toggle the visibility of the current block."
  6385. (interactive)
  6386. (save-excursion
  6387. (beginning-of-line)
  6388. (if (re-search-forward org-block-regexp nil t)
  6389. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6390. (end (match-end 0)) ;; end of entire body
  6391. ov)
  6392. (if (memq t (mapcar (lambda (overlay)
  6393. (eq (overlay-get overlay 'invisible)
  6394. 'org-hide-block))
  6395. (overlays-at start)))
  6396. (if (or (not force) (eq force 'off))
  6397. (mapc (lambda (ov)
  6398. (when (member ov org-hide-block-overlays)
  6399. (setq org-hide-block-overlays
  6400. (delq ov org-hide-block-overlays)))
  6401. (when (eq (overlay-get ov 'invisible)
  6402. 'org-hide-block)
  6403. (delete-overlay ov)))
  6404. (overlays-at start)))
  6405. (setq ov (make-overlay start end))
  6406. (overlay-put ov 'invisible 'org-hide-block)
  6407. ;; make the block accessible to isearch
  6408. (overlay-put
  6409. ov 'isearch-open-invisible
  6410. (lambda (ov)
  6411. (when (member ov org-hide-block-overlays)
  6412. (setq org-hide-block-overlays
  6413. (delq ov org-hide-block-overlays)))
  6414. (when (eq (overlay-get ov 'invisible)
  6415. 'org-hide-block)
  6416. (delete-overlay ov))))
  6417. (push ov org-hide-block-overlays)))
  6418. (user-error "Not looking at a source block"))))
  6419. ;; org-tab-after-check-for-cycling-hook
  6420. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6421. ;; Remove overlays when changing major mode
  6422. (add-hook 'org-mode-hook
  6423. (lambda () (org-add-hook 'change-major-mode-hook
  6424. 'org-show-block-all 'append 'local)))
  6425. ;;; Org-goto
  6426. (defvar org-goto-window-configuration nil)
  6427. (defvar org-goto-marker nil)
  6428. (defvar org-goto-map)
  6429. (defun org-goto-map ()
  6430. "Set the keymap `org-goto'."
  6431. (setq org-goto-map
  6432. (let ((map (make-sparse-keymap)))
  6433. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6434. mouse-drag-region universal-argument org-occur))
  6435. cmd)
  6436. (while (setq cmd (pop cmds))
  6437. (substitute-key-definition cmd cmd map global-map)))
  6438. (suppress-keymap map)
  6439. (org-defkey map "\C-m" 'org-goto-ret)
  6440. (org-defkey map [(return)] 'org-goto-ret)
  6441. (org-defkey map [(left)] 'org-goto-left)
  6442. (org-defkey map [(right)] 'org-goto-right)
  6443. (org-defkey map [(control ?g)] 'org-goto-quit)
  6444. (org-defkey map "\C-i" 'org-cycle)
  6445. (org-defkey map [(tab)] 'org-cycle)
  6446. (org-defkey map [(down)] 'outline-next-visible-heading)
  6447. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6448. (if org-goto-auto-isearch
  6449. (if (fboundp 'define-key-after)
  6450. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6451. nil)
  6452. (org-defkey map "q" 'org-goto-quit)
  6453. (org-defkey map "n" 'outline-next-visible-heading)
  6454. (org-defkey map "p" 'outline-previous-visible-heading)
  6455. (org-defkey map "f" 'outline-forward-same-level)
  6456. (org-defkey map "b" 'outline-backward-same-level)
  6457. (org-defkey map "u" 'outline-up-heading))
  6458. (org-defkey map "/" 'org-occur)
  6459. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6460. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6461. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6462. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6463. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6464. map)))
  6465. (defconst org-goto-help
  6466. "Browse buffer copy, to find location or copy text.%s
  6467. RET=jump to location C-g=quit and return to previous location
  6468. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6469. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6470. (defun org-goto (&optional alternative-interface)
  6471. "Look up a different location in the current file, keeping current visibility.
  6472. When you want look-up or go to a different location in a
  6473. document, the fastest way is often to fold the entire buffer and
  6474. then dive into the tree. This method has the disadvantage, that
  6475. the previous location will be folded, which may not be what you
  6476. want.
  6477. This command works around this by showing a copy of the current
  6478. buffer in an indirect buffer, in overview mode. You can dive
  6479. into the tree in that copy, use org-occur and incremental search
  6480. to find a location. When pressing RET or `Q', the command
  6481. returns to the original buffer in which the visibility is still
  6482. unchanged. After RET it will also jump to the location selected
  6483. in the indirect buffer and expose the headline hierarchy above.
  6484. With a prefix argument, use the alternative interface: e.g. if
  6485. `org-goto-interface' is 'outline use 'outline-path-completion."
  6486. (interactive "P")
  6487. (org-goto-map)
  6488. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6489. (org-refile-use-outline-path t)
  6490. (org-refile-target-verify-function nil)
  6491. (interface
  6492. (if (not alternative-interface)
  6493. org-goto-interface
  6494. (if (eq org-goto-interface 'outline)
  6495. 'outline-path-completion
  6496. 'outline)))
  6497. (org-goto-start-pos (point))
  6498. (selected-point
  6499. (if (eq interface 'outline)
  6500. (car (org-get-location (current-buffer) org-goto-help))
  6501. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6502. (org-refile-check-position pa)
  6503. (nth 3 pa)))))
  6504. (if selected-point
  6505. (progn
  6506. (org-mark-ring-push org-goto-start-pos)
  6507. (goto-char selected-point)
  6508. (if (or (outline-invisible-p) (org-invisible-p2))
  6509. (org-show-context 'org-goto)))
  6510. (message "Quit"))))
  6511. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6512. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6513. (defvar org-goto-local-auto-isearch-map) ; defined below
  6514. (defun org-get-location (buf help)
  6515. "Let the user select a location in the Org-mode buffer BUF.
  6516. This function uses a recursive edit. It returns the selected position
  6517. or nil."
  6518. (org-no-popups
  6519. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6520. (isearch-hide-immediately nil)
  6521. (isearch-search-fun-function
  6522. (lambda () 'org-goto-local-search-headings))
  6523. (org-goto-selected-point org-goto-exit-command))
  6524. (save-excursion
  6525. (save-window-excursion
  6526. (delete-other-windows)
  6527. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6528. (org-pop-to-buffer-same-window
  6529. (condition-case nil
  6530. (make-indirect-buffer (current-buffer) "*org-goto*")
  6531. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6532. (with-output-to-temp-buffer "*Org Help*"
  6533. (princ (format help (if org-goto-auto-isearch
  6534. " Just type for auto-isearch."
  6535. " n/p/f/b/u to navigate, q to quit."))))
  6536. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6537. (setq buffer-read-only nil)
  6538. (let ((org-startup-truncated t)
  6539. (org-startup-folded nil)
  6540. (org-startup-align-all-tables nil))
  6541. (org-mode)
  6542. (org-overview))
  6543. (setq buffer-read-only t)
  6544. (if (and (boundp 'org-goto-start-pos)
  6545. (integer-or-marker-p org-goto-start-pos))
  6546. (let ((org-show-hierarchy-above t)
  6547. (org-show-siblings t)
  6548. (org-show-following-heading t))
  6549. (goto-char org-goto-start-pos)
  6550. (and (outline-invisible-p) (org-show-context)))
  6551. (goto-char (point-min)))
  6552. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6553. (message "Select location and press RET")
  6554. (use-local-map org-goto-map)
  6555. (recursive-edit)))
  6556. (kill-buffer "*org-goto*")
  6557. (cons org-goto-selected-point org-goto-exit-command))))
  6558. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6559. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6560. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6561. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6562. (defun org-goto-local-search-headings (string bound noerror)
  6563. "Search and make sure that any matches are in headlines."
  6564. (catch 'return
  6565. (while (if isearch-forward
  6566. (search-forward string bound noerror)
  6567. (search-backward string bound noerror))
  6568. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6569. (and (member :headline context)
  6570. (not (member :tags context))))
  6571. (throw 'return (point))))))
  6572. (defun org-goto-local-auto-isearch ()
  6573. "Start isearch."
  6574. (interactive)
  6575. (goto-char (point-min))
  6576. (let ((keys (this-command-keys)))
  6577. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6578. (isearch-mode t)
  6579. (isearch-process-search-char (string-to-char keys)))))
  6580. (defun org-goto-ret (&optional arg)
  6581. "Finish `org-goto' by going to the new location."
  6582. (interactive "P")
  6583. (setq org-goto-selected-point (point)
  6584. org-goto-exit-command 'return)
  6585. (throw 'exit nil))
  6586. (defun org-goto-left ()
  6587. "Finish `org-goto' by going to the new location."
  6588. (interactive)
  6589. (if (org-at-heading-p)
  6590. (progn
  6591. (beginning-of-line 1)
  6592. (setq org-goto-selected-point (point)
  6593. org-goto-exit-command 'left)
  6594. (throw 'exit nil))
  6595. (user-error "Not on a heading")))
  6596. (defun org-goto-right ()
  6597. "Finish `org-goto' by going to the new location."
  6598. (interactive)
  6599. (if (org-at-heading-p)
  6600. (progn
  6601. (setq org-goto-selected-point (point)
  6602. org-goto-exit-command 'right)
  6603. (throw 'exit nil))
  6604. (user-error "Not on a heading")))
  6605. (defun org-goto-quit ()
  6606. "Finish `org-goto' without cursor motion."
  6607. (interactive)
  6608. (setq org-goto-selected-point nil)
  6609. (setq org-goto-exit-command 'quit)
  6610. (throw 'exit nil))
  6611. ;;; Indirect buffer display of subtrees
  6612. (defvar org-indirect-dedicated-frame nil
  6613. "This is the frame being used for indirect tree display.")
  6614. (defvar org-last-indirect-buffer nil)
  6615. (defun org-tree-to-indirect-buffer (&optional arg)
  6616. "Create indirect buffer and narrow it to current subtree.
  6617. With a numerical prefix ARG, go up to this level and then take that tree.
  6618. If ARG is negative, go up that many levels.
  6619. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6620. indirect buffer previously made with this command, to avoid proliferation of
  6621. indirect buffers. However, when you call the command with a \
  6622. \\[universal-argument] prefix, or
  6623. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6624. is kept so that you can work with several indirect buffers at the same time.
  6625. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6626. \\[universal-argument] prefix also
  6627. requests that a new frame be made for the new buffer, so that the dedicated
  6628. frame is not changed."
  6629. (interactive "P")
  6630. (let ((cbuf (current-buffer))
  6631. (cwin (selected-window))
  6632. (pos (point))
  6633. beg end level heading ibuf)
  6634. (save-excursion
  6635. (org-back-to-heading t)
  6636. (when (numberp arg)
  6637. (setq level (org-outline-level))
  6638. (if (< arg 0) (setq arg (+ level arg)))
  6639. (while (> (setq level (org-outline-level)) arg)
  6640. (org-up-heading-safe)))
  6641. (setq beg (point)
  6642. heading (org-get-heading))
  6643. (org-end-of-subtree t t)
  6644. (if (org-at-heading-p) (backward-char 1))
  6645. (setq end (point)))
  6646. (if (and (buffer-live-p org-last-indirect-buffer)
  6647. (not (eq org-indirect-buffer-display 'new-frame))
  6648. (not arg))
  6649. (kill-buffer org-last-indirect-buffer))
  6650. (setq ibuf (org-get-indirect-buffer cbuf)
  6651. org-last-indirect-buffer ibuf)
  6652. (cond
  6653. ((or (eq org-indirect-buffer-display 'new-frame)
  6654. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6655. (select-frame (make-frame))
  6656. (delete-other-windows)
  6657. (org-pop-to-buffer-same-window ibuf)
  6658. (org-set-frame-title heading))
  6659. ((eq org-indirect-buffer-display 'dedicated-frame)
  6660. (raise-frame
  6661. (select-frame (or (and org-indirect-dedicated-frame
  6662. (frame-live-p org-indirect-dedicated-frame)
  6663. org-indirect-dedicated-frame)
  6664. (setq org-indirect-dedicated-frame (make-frame)))))
  6665. (delete-other-windows)
  6666. (org-pop-to-buffer-same-window ibuf)
  6667. (org-set-frame-title (concat "Indirect: " heading)))
  6668. ((eq org-indirect-buffer-display 'current-window)
  6669. (org-pop-to-buffer-same-window ibuf))
  6670. ((eq org-indirect-buffer-display 'other-window)
  6671. (pop-to-buffer ibuf))
  6672. (t (error "Invalid value")))
  6673. (if (featurep 'xemacs)
  6674. (save-excursion (org-mode) (turn-on-font-lock)))
  6675. (narrow-to-region beg end)
  6676. (show-all)
  6677. (goto-char pos)
  6678. (run-hook-with-args 'org-cycle-hook 'all)
  6679. (and (window-live-p cwin) (select-window cwin))))
  6680. (defun org-get-indirect-buffer (&optional buffer)
  6681. (setq buffer (or buffer (current-buffer)))
  6682. (let ((n 1) (base (buffer-name buffer)) bname)
  6683. (while (buffer-live-p
  6684. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6685. (setq n (1+ n)))
  6686. (condition-case nil
  6687. (make-indirect-buffer buffer bname 'clone)
  6688. (error (make-indirect-buffer buffer bname)))))
  6689. (defun org-set-frame-title (title)
  6690. "Set the title of the current frame to the string TITLE."
  6691. ;; FIXME: how to name a single frame in XEmacs???
  6692. (unless (featurep 'xemacs)
  6693. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6694. ;;;; Structure editing
  6695. ;;; Inserting headlines
  6696. (defun org-previous-line-empty-p (&optional next)
  6697. "Is the previous line a blank line?
  6698. When NEXT is non-nil, check the next line instead."
  6699. (save-excursion
  6700. (and (not (bobp))
  6701. (or (beginning-of-line (if next 2 0)) t)
  6702. (save-match-data
  6703. (looking-at "[ \t]*$")))))
  6704. (defun org-insert-heading (&optional arg invisible-ok)
  6705. "Insert a new heading or item with same depth at point.
  6706. If point is in a plain list and ARG is nil, create a new list item.
  6707. With one universal prefix argument, insert a heading even in lists.
  6708. With two universal prefix arguments, insert the heading at the end
  6709. of the parent subtree.
  6710. If point is at the beginning of a headline, insert a sibling before
  6711. the current headline. If point is not at the beginning, split the line
  6712. and create a new headline with the text in the current line after point
  6713. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6714. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6715. This is important for non-interactive uses of the command."
  6716. (interactive "P")
  6717. (if (org-called-interactively-p 'any) (org-reveal))
  6718. (cond
  6719. ((or (= (buffer-size) 0)
  6720. (and (not (save-excursion
  6721. (and (ignore-errors (org-back-to-heading invisible-ok))
  6722. (org-at-heading-p))))
  6723. (or arg (not (org-in-item-p)))))
  6724. (insert
  6725. (if (org-previous-line-empty-p) "" "\n")
  6726. (if (org-in-src-block-p) ",* " "* "))
  6727. (run-hooks 'org-insert-heading-hook))
  6728. ((or arg
  6729. (and (not (org-in-item-p)) org-insert-heading-respect-content)
  6730. (not (org-insert-item
  6731. (save-excursion
  6732. (beginning-of-line)
  6733. (looking-at org-list-full-item-re)
  6734. (match-string 3)))))
  6735. (let (begn endn)
  6736. (when (org-buffer-narrowed-p)
  6737. (setq begn (point-min) endn (point-max))
  6738. (widen))
  6739. (let* ((empty-line-p nil)
  6740. (eops (equal arg '(16))) ; insert at end of parent subtree
  6741. (org-insert-heading-respect-content
  6742. (or (not (null arg)) org-insert-heading-respect-content))
  6743. (level nil)
  6744. (on-heading (org-at-heading-p))
  6745. ;; Get a level to fall back on
  6746. (fix-level
  6747. (save-excursion
  6748. (org-back-to-heading t)
  6749. (looking-at org-outline-regexp)
  6750. (make-string (1- (length (match-string 0))) ?*)))
  6751. (on-empty-line
  6752. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6753. (head (save-excursion
  6754. (condition-case nil
  6755. (progn
  6756. (org-back-to-heading invisible-ok)
  6757. (when (and (not on-heading)
  6758. (featurep 'org-inlinetask)
  6759. (integerp org-inlinetask-min-level)
  6760. (>= (length (match-string 0))
  6761. org-inlinetask-min-level))
  6762. ;; Find a heading level before the inline task
  6763. (while (and (setq level (org-up-heading-safe))
  6764. (>= level org-inlinetask-min-level)))
  6765. (if (org-at-heading-p)
  6766. (org-back-to-heading invisible-ok)
  6767. (error "This should not happen")))
  6768. (unless (and (save-excursion
  6769. (save-match-data
  6770. (org-backward-heading-same-level 1 invisible-ok))
  6771. (= (point) (match-beginning 0)))
  6772. (not (org-previous-line-empty-p t)))
  6773. (setq empty-line-p (org-previous-line-empty-p)))
  6774. (match-string 0))
  6775. (error (or fix-level "* ")))))
  6776. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6777. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6778. pos hide-previous previous-pos)
  6779. (if ;; At the beginning of a heading, open a new line for insertion
  6780. (and (bolp) (org-at-heading-p)
  6781. (not eops)
  6782. (or (bobp)
  6783. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6784. (open-line (if blank 2 1))
  6785. (save-excursion
  6786. (setq previous-pos (point-at-bol))
  6787. (end-of-line)
  6788. (setq hide-previous (outline-invisible-p)))
  6789. (and org-insert-heading-respect-content
  6790. (save-excursion
  6791. (while (outline-invisible-p)
  6792. (org-show-subtree)
  6793. (org-up-heading-safe))))
  6794. (let ((split
  6795. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6796. (save-excursion
  6797. (let ((p (point)))
  6798. (goto-char (point-at-bol))
  6799. (and (looking-at org-complex-heading-regexp)
  6800. (match-beginning 4)
  6801. (> p (match-beginning 4)))))))
  6802. tags pos)
  6803. (cond
  6804. ;; Insert a new line, possibly at end of parent subtree
  6805. ((and (not arg) (not on-heading) (not on-empty-line)
  6806. (not (save-excursion
  6807. (beginning-of-line 1)
  6808. (or (looking-at org-list-full-item-re)
  6809. ;; Don't convert :end: lines to headline
  6810. (looking-at "^\\s-*:end:")
  6811. (looking-at "^\\s-*#\\+end_?")))))
  6812. (beginning-of-line 1))
  6813. (org-insert-heading-respect-content
  6814. (if (not eops)
  6815. (progn
  6816. (org-end-of-subtree nil t)
  6817. (and (looking-at "^\\*") (backward-char 1))
  6818. (while (and (not (bobp))
  6819. ;; Don't delete spaces in empty headlines
  6820. (not (looking-back org-outline-regexp))
  6821. (member (char-before) '(?\ ?\t ?\n)))
  6822. (backward-delete-char 1)))
  6823. (let ((p (point)))
  6824. (org-up-heading-safe)
  6825. (if (= p (point))
  6826. (goto-char (point-max))
  6827. (org-end-of-subtree nil t))))
  6828. (when (featurep 'org-inlinetask)
  6829. (while (and (not (eobp))
  6830. (looking-at "\\(\\*+\\)[ \t]+")
  6831. (>= (length (match-string 1))
  6832. org-inlinetask-min-level))
  6833. (org-end-of-subtree nil t)))
  6834. (or (bolp) (newline))
  6835. (or (org-previous-line-empty-p)
  6836. (and blank (newline)))
  6837. (if (or empty-line-p eops) (open-line 1)))
  6838. ;; Insert a headling containing text after point
  6839. ((org-at-heading-p)
  6840. (when hide-previous
  6841. (show-children)
  6842. (org-show-entry))
  6843. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6844. (setq tags (and (match-end 2) (match-string 2)))
  6845. (and (match-end 1)
  6846. (delete-region (match-beginning 1) (match-end 1)))
  6847. (setq pos (point-at-bol))
  6848. (or split (end-of-line 1))
  6849. (delete-horizontal-space)
  6850. (if (string-match "\\`\\*+\\'"
  6851. (buffer-substring (point-at-bol) (point)))
  6852. (insert " "))
  6853. (newline (if blank 2 1))
  6854. (when tags
  6855. (save-excursion
  6856. (goto-char pos)
  6857. (end-of-line 1)
  6858. (insert " " tags)
  6859. (org-set-tags nil 'align))))
  6860. (t
  6861. (or split (end-of-line 1))
  6862. (newline (cond ((and blank (not on-empty-line)) 2)
  6863. (blank 1)
  6864. (on-empty-line 0) (t 1)))))))
  6865. (insert head) (just-one-space)
  6866. (setq pos (point))
  6867. (end-of-line 1)
  6868. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6869. (when (and org-insert-heading-respect-content hide-previous)
  6870. (save-excursion
  6871. (goto-char previous-pos)
  6872. (hide-subtree)))
  6873. (when (and begn endn)
  6874. (narrow-to-region (min (point) begn) (max (point) endn)))
  6875. (run-hooks 'org-insert-heading-hook))))))
  6876. (defun org-get-heading (&optional no-tags no-todo)
  6877. "Return the heading of the current entry, without the stars.
  6878. When NO-TAGS is non-nil, don't include tags.
  6879. When NO-TODO is non-nil, don't include TODO keywords."
  6880. (save-excursion
  6881. (org-back-to-heading t)
  6882. (cond
  6883. ((and no-tags no-todo)
  6884. (looking-at org-complex-heading-regexp)
  6885. (match-string 4))
  6886. (no-tags
  6887. (looking-at (concat org-outline-regexp
  6888. "\\(.*?\\)"
  6889. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6890. (match-string 1))
  6891. (no-todo
  6892. (looking-at org-todo-line-regexp)
  6893. (match-string 3))
  6894. (t (looking-at org-heading-regexp)
  6895. (match-string 2)))))
  6896. (defvar orgstruct-mode) ; defined below
  6897. (defun org-heading-components ()
  6898. "Return the components of the current heading.
  6899. This is a list with the following elements:
  6900. - the level as an integer
  6901. - the reduced level, different if `org-odd-levels-only' is set.
  6902. - the TODO keyword, or nil
  6903. - the priority character, like ?A, or nil if no priority is given
  6904. - the headline text itself, or the tags string if no headline text
  6905. - the tags string, or nil."
  6906. (save-excursion
  6907. (org-back-to-heading t)
  6908. (if (let (case-fold-search)
  6909. (looking-at
  6910. (if orgstruct-mode
  6911. org-heading-regexp
  6912. org-complex-heading-regexp)))
  6913. (if orgstruct-mode
  6914. (list (length (match-string 1))
  6915. (org-reduced-level (length (match-string 1)))
  6916. nil
  6917. nil
  6918. (match-string 2)
  6919. nil)
  6920. (list (length (match-string 1))
  6921. (org-reduced-level (length (match-string 1)))
  6922. (org-match-string-no-properties 2)
  6923. (and (match-end 3) (aref (match-string 3) 2))
  6924. (org-match-string-no-properties 4)
  6925. (org-match-string-no-properties 5))))))
  6926. (defun org-get-entry ()
  6927. "Get the entry text, after heading, entire subtree."
  6928. (save-excursion
  6929. (org-back-to-heading t)
  6930. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6931. (defun org-insert-heading-after-current ()
  6932. "Insert a new heading with same level as current, after current subtree."
  6933. (interactive)
  6934. (org-back-to-heading)
  6935. (org-insert-heading)
  6936. (org-move-subtree-down)
  6937. (end-of-line 1))
  6938. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6939. "Insert heading with `org-insert-heading-respect-content' set to t."
  6940. (interactive "P")
  6941. (let ((org-insert-heading-respect-content t))
  6942. (org-insert-heading arg invisible-ok)))
  6943. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6944. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6945. (interactive "P")
  6946. (let ((org-insert-heading-respect-content t))
  6947. (org-insert-todo-heading force-state t)))
  6948. (defun org-insert-todo-heading (arg &optional force-heading)
  6949. "Insert a new heading with the same level and TODO state as current heading.
  6950. If the heading has no TODO state, or if the state is DONE, use the first
  6951. state (TODO by default). Also one prefix arg, force first state. With two
  6952. prefix args, force inserting at the end of the parent subtree."
  6953. (interactive "P")
  6954. (when (or force-heading (not (org-insert-item 'checkbox)))
  6955. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6956. force-heading))
  6957. (save-excursion
  6958. (org-back-to-heading)
  6959. (outline-previous-heading)
  6960. (looking-at org-todo-line-regexp))
  6961. (let*
  6962. ((new-mark-x
  6963. (if (or arg
  6964. (not (match-beginning 2))
  6965. (member (match-string 2) org-done-keywords))
  6966. (car org-todo-keywords-1)
  6967. (match-string 2)))
  6968. (new-mark
  6969. (or
  6970. (run-hook-with-args-until-success
  6971. 'org-todo-get-default-hook new-mark-x nil)
  6972. new-mark-x)))
  6973. (beginning-of-line 1)
  6974. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6975. (if org-treat-insert-todo-heading-as-state-change
  6976. (org-todo new-mark)
  6977. (insert new-mark " "))))
  6978. (when org-provide-todo-statistics
  6979. (org-update-parent-todo-statistics))))
  6980. (defun org-insert-subheading (arg)
  6981. "Insert a new subheading and demote it.
  6982. Works for outline headings and for plain lists alike."
  6983. (interactive "P")
  6984. (org-insert-heading arg)
  6985. (cond
  6986. ((org-at-heading-p) (org-do-demote))
  6987. ((org-at-item-p) (org-indent-item))))
  6988. (defun org-insert-todo-subheading (arg)
  6989. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6990. Works for outline headings and for plain lists alike."
  6991. (interactive "P")
  6992. (org-insert-todo-heading arg)
  6993. (cond
  6994. ((org-at-heading-p) (org-do-demote))
  6995. ((org-at-item-p) (org-indent-item))))
  6996. ;;; Promotion and Demotion
  6997. (defvar org-after-demote-entry-hook nil
  6998. "Hook run after an entry has been demoted.
  6999. The cursor will be at the beginning of the entry.
  7000. When a subtree is being demoted, the hook will be called for each node.")
  7001. (defvar org-after-promote-entry-hook nil
  7002. "Hook run after an entry has been promoted.
  7003. The cursor will be at the beginning of the entry.
  7004. When a subtree is being promoted, the hook will be called for each node.")
  7005. (defun org-promote-subtree ()
  7006. "Promote the entire subtree.
  7007. See also `org-promote'."
  7008. (interactive)
  7009. (save-excursion
  7010. (org-with-limited-levels (org-map-tree 'org-promote)))
  7011. (org-fix-position-after-promote))
  7012. (defun org-demote-subtree ()
  7013. "Demote the entire subtree. See `org-demote'.
  7014. See also `org-promote'."
  7015. (interactive)
  7016. (save-excursion
  7017. (org-with-limited-levels (org-map-tree 'org-demote)))
  7018. (org-fix-position-after-promote))
  7019. (defun org-do-promote ()
  7020. "Promote the current heading higher up the tree.
  7021. If the region is active in `transient-mark-mode', promote all headings
  7022. in the region."
  7023. (interactive)
  7024. (save-excursion
  7025. (if (org-region-active-p)
  7026. (org-map-region 'org-promote (region-beginning) (region-end))
  7027. (org-promote)))
  7028. (org-fix-position-after-promote))
  7029. (defun org-do-demote ()
  7030. "Demote the current heading lower down the tree.
  7031. If the region is active in `transient-mark-mode', demote all headings
  7032. in the region."
  7033. (interactive)
  7034. (save-excursion
  7035. (if (org-region-active-p)
  7036. (org-map-region 'org-demote (region-beginning) (region-end))
  7037. (org-demote)))
  7038. (org-fix-position-after-promote))
  7039. (defun org-fix-position-after-promote ()
  7040. "Make sure that after pro/demotion cursor position is right."
  7041. (let ((pos (point)))
  7042. (when (save-excursion
  7043. (beginning-of-line 1)
  7044. (looking-at org-todo-line-regexp)
  7045. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7046. (cond ((eobp) (insert " "))
  7047. ((eolp) (insert " "))
  7048. ((equal (char-after) ?\ ) (forward-char 1))))))
  7049. (defun org-current-level ()
  7050. "Return the level of the current entry, or nil if before the first headline.
  7051. The level is the number of stars at the beginning of the headline."
  7052. (save-excursion
  7053. (org-with-limited-levels
  7054. (if (ignore-errors (org-back-to-heading t))
  7055. (funcall outline-level)))))
  7056. (defun org-get-previous-line-level ()
  7057. "Return the outline depth of the last headline before the current line.
  7058. Returns 0 for the first headline in the buffer, and nil if before the
  7059. first headline."
  7060. (let ((current-level (org-current-level))
  7061. (prev-level (when (> (line-number-at-pos) 1)
  7062. (save-excursion
  7063. (beginning-of-line 0)
  7064. (org-current-level)))))
  7065. (cond ((null current-level) nil) ; Before first headline
  7066. ((null prev-level) 0) ; At first headline
  7067. (prev-level))))
  7068. (defun org-reduced-level (l)
  7069. "Compute the effective level of a heading.
  7070. This takes into account the setting of `org-odd-levels-only'."
  7071. (cond
  7072. ((zerop l) 0)
  7073. (org-odd-levels-only (1+ (floor (/ l 2))))
  7074. (t l)))
  7075. (defun org-level-increment ()
  7076. "Return the number of stars that will be added or removed at a
  7077. time to headlines when structure editing, based on the value of
  7078. `org-odd-levels-only'."
  7079. (if org-odd-levels-only 2 1))
  7080. (defun org-get-valid-level (level &optional change)
  7081. "Rectify a level change under the influence of `org-odd-levels-only'
  7082. LEVEL is a current level, CHANGE is by how much the level should be
  7083. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7084. even level numbers will become the next higher odd number."
  7085. (if org-odd-levels-only
  7086. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7087. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7088. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7089. (max 1 (+ level (or change 0)))))
  7090. (if (boundp 'define-obsolete-function-alias)
  7091. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7092. (define-obsolete-function-alias 'org-get-legal-level
  7093. 'org-get-valid-level)
  7094. (define-obsolete-function-alias 'org-get-legal-level
  7095. 'org-get-valid-level "23.1")))
  7096. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7097. ;; ̀org-with-limited-levels'
  7098. (defun org-promote ()
  7099. "Promote the current heading higher up the tree.
  7100. If the region is active in `transient-mark-mode', promote all headings
  7101. in the region."
  7102. (org-back-to-heading t)
  7103. (let* ((level (save-match-data (funcall outline-level)))
  7104. (after-change-functions (remove 'flyspell-after-change-function
  7105. after-change-functions))
  7106. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7107. (diff (abs (- level (length up-head) -1))))
  7108. (cond ((and (= level 1) org-called-with-limited-levels
  7109. org-allow-promoting-top-level-subtree)
  7110. (replace-match "# " nil t))
  7111. ((= level 1)
  7112. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7113. (t (replace-match up-head nil t)))
  7114. ;; Fixup tag positioning
  7115. (unless (= level 1)
  7116. (and org-auto-align-tags (org-set-tags nil t))
  7117. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7118. (run-hooks 'org-after-promote-entry-hook)))
  7119. (defun org-demote ()
  7120. "Demote the current heading lower down the tree.
  7121. If the region is active in `transient-mark-mode', demote all headings
  7122. in the region."
  7123. (org-back-to-heading t)
  7124. (let* ((level (save-match-data (funcall outline-level)))
  7125. (after-change-functions (remove 'flyspell-after-change-function
  7126. after-change-functions))
  7127. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7128. (diff (abs (- level (length down-head) -1))))
  7129. (replace-match down-head nil t)
  7130. ;; Fixup tag positioning
  7131. (and org-auto-align-tags (org-set-tags nil t))
  7132. (if org-adapt-indentation (org-fixup-indentation diff))
  7133. (run-hooks 'org-after-demote-entry-hook)))
  7134. (defun org-cycle-level ()
  7135. "Cycle the level of an empty headline through possible states.
  7136. This goes first to child, then to parent, level, then up the hierarchy.
  7137. After top level, it switches back to sibling level."
  7138. (interactive)
  7139. (let ((org-adapt-indentation nil))
  7140. (when (org-point-at-end-of-empty-headline)
  7141. (setq this-command 'org-cycle-level) ; Only needed for caching
  7142. (let ((cur-level (org-current-level))
  7143. (prev-level (org-get-previous-line-level)))
  7144. (cond
  7145. ;; If first headline in file, promote to top-level.
  7146. ((= prev-level 0)
  7147. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7148. do (org-do-promote)))
  7149. ;; If same level as prev, demote one.
  7150. ((= prev-level cur-level)
  7151. (org-do-demote))
  7152. ;; If parent is top-level, promote to top level if not already.
  7153. ((= prev-level 1)
  7154. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7155. do (org-do-promote)))
  7156. ;; If top-level, return to prev-level.
  7157. ((= cur-level 1)
  7158. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7159. do (org-do-demote)))
  7160. ;; If less than prev-level, promote one.
  7161. ((< cur-level prev-level)
  7162. (org-do-promote))
  7163. ;; If deeper than prev-level, promote until higher than
  7164. ;; prev-level.
  7165. ((> cur-level prev-level)
  7166. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7167. do (org-do-promote))))
  7168. t))))
  7169. (defun org-map-tree (fun)
  7170. "Call FUN for every heading underneath the current one."
  7171. (org-back-to-heading)
  7172. (let ((level (funcall outline-level)))
  7173. (save-excursion
  7174. (funcall fun)
  7175. (while (and (progn
  7176. (outline-next-heading)
  7177. (> (funcall outline-level) level))
  7178. (not (eobp)))
  7179. (funcall fun)))))
  7180. (defun org-map-region (fun beg end)
  7181. "Call FUN for every heading between BEG and END."
  7182. (let ((org-ignore-region t))
  7183. (save-excursion
  7184. (setq end (copy-marker end))
  7185. (goto-char beg)
  7186. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7187. (< (point) end))
  7188. (funcall fun))
  7189. (while (and (progn
  7190. (outline-next-heading)
  7191. (< (point) end))
  7192. (not (eobp)))
  7193. (funcall fun)))))
  7194. (defvar org-property-end-re) ; silence byte-compiler
  7195. (defun org-fixup-indentation (diff)
  7196. "Change the indentation in the current entry by DIFF.
  7197. However, if any line in the current entry has no indentation, or if it
  7198. would end up with no indentation after the change, nothing at all is done."
  7199. (save-excursion
  7200. (let ((end (save-excursion (outline-next-heading)
  7201. (point-marker)))
  7202. (prohibit (if (> diff 0)
  7203. "^\\S-"
  7204. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7205. col)
  7206. (unless (save-excursion (end-of-line 1)
  7207. (re-search-forward prohibit end t))
  7208. (while (and (< (point) end)
  7209. (re-search-forward "^[ \t]+" end t))
  7210. (goto-char (match-end 0))
  7211. (setq col (current-column))
  7212. (if (< diff 0) (replace-match ""))
  7213. (org-indent-to-column (+ diff col))))
  7214. (move-marker end nil))))
  7215. (defun org-convert-to-odd-levels ()
  7216. "Convert an org-mode file with all levels allowed to one with odd levels.
  7217. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7218. level 5 etc."
  7219. (interactive)
  7220. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7221. (let ((outline-level 'org-outline-level)
  7222. (org-odd-levels-only nil) n)
  7223. (save-excursion
  7224. (goto-char (point-min))
  7225. (while (re-search-forward "^\\*\\*+ " nil t)
  7226. (setq n (- (length (match-string 0)) 2))
  7227. (while (>= (setq n (1- n)) 0)
  7228. (org-demote))
  7229. (end-of-line 1))))))
  7230. (defun org-convert-to-oddeven-levels ()
  7231. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7232. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7233. file contains a section with an even level, conversion would
  7234. destroy the structure of the file. An error is signaled in this
  7235. case."
  7236. (interactive)
  7237. (goto-char (point-min))
  7238. ;; First check if there are no even levels
  7239. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7240. (org-show-context t)
  7241. (error "Not all levels are odd in this file. Conversion not possible"))
  7242. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7243. (let ((outline-regexp org-outline-regexp)
  7244. (outline-level 'org-outline-level)
  7245. (org-odd-levels-only nil) n)
  7246. (save-excursion
  7247. (goto-char (point-min))
  7248. (while (re-search-forward "^\\*\\*+ " nil t)
  7249. (setq n (/ (1- (length (match-string 0))) 2))
  7250. (while (>= (setq n (1- n)) 0)
  7251. (org-promote))
  7252. (end-of-line 1))))))
  7253. (defun org-tr-level (n)
  7254. "Make N odd if required."
  7255. (if org-odd-levels-only (1+ (/ n 2)) n))
  7256. ;;; Vertical tree motion, cutting and pasting of subtrees
  7257. (defun org-move-subtree-up (&optional arg)
  7258. "Move the current subtree up past ARG headlines of the same level."
  7259. (interactive "p")
  7260. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7261. (defun org-move-subtree-down (&optional arg)
  7262. "Move the current subtree down past ARG headlines of the same level."
  7263. (interactive "p")
  7264. (setq arg (prefix-numeric-value arg))
  7265. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7266. 'org-get-last-sibling))
  7267. (ins-point (make-marker))
  7268. (cnt (abs arg))
  7269. (col (current-column))
  7270. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7271. ;; Select the tree
  7272. (org-back-to-heading)
  7273. (setq beg0 (point))
  7274. (save-excursion
  7275. (setq ne-beg (org-back-over-empty-lines))
  7276. (setq beg (point)))
  7277. (save-match-data
  7278. (save-excursion (outline-end-of-heading)
  7279. (setq folded (outline-invisible-p)))
  7280. (outline-end-of-subtree))
  7281. (outline-next-heading)
  7282. (setq ne-end (org-back-over-empty-lines))
  7283. (setq end (point))
  7284. (goto-char beg0)
  7285. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7286. ;; include less whitespace
  7287. (save-excursion
  7288. (goto-char beg)
  7289. (forward-line (- ne-beg ne-end))
  7290. (setq beg (point))))
  7291. ;; Find insertion point, with error handling
  7292. (while (> cnt 0)
  7293. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7294. (progn (goto-char beg0)
  7295. (user-error "Cannot move past superior level or buffer limit")))
  7296. (setq cnt (1- cnt)))
  7297. (if (> arg 0)
  7298. ;; Moving forward - still need to move over subtree
  7299. (progn (org-end-of-subtree t t)
  7300. (save-excursion
  7301. (org-back-over-empty-lines)
  7302. (or (bolp) (newline)))))
  7303. (setq ne-ins (org-back-over-empty-lines))
  7304. (move-marker ins-point (point))
  7305. (setq txt (buffer-substring beg end))
  7306. (org-save-markers-in-region beg end)
  7307. (delete-region beg end)
  7308. (org-remove-empty-overlays-at beg)
  7309. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7310. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7311. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7312. (let ((bbb (point)))
  7313. (insert-before-markers txt)
  7314. (org-reinstall-markers-in-region bbb)
  7315. (move-marker ins-point bbb))
  7316. (or (bolp) (insert "\n"))
  7317. (setq ins-end (point))
  7318. (goto-char ins-point)
  7319. (org-skip-whitespace)
  7320. (when (and (< arg 0)
  7321. (org-first-sibling-p)
  7322. (> ne-ins ne-beg))
  7323. ;; Move whitespace back to beginning
  7324. (save-excursion
  7325. (goto-char ins-end)
  7326. (let ((kill-whole-line t))
  7327. (kill-line (- ne-ins ne-beg)) (point)))
  7328. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7329. (move-marker ins-point nil)
  7330. (if folded
  7331. (hide-subtree)
  7332. (org-show-entry)
  7333. (show-children)
  7334. (org-cycle-hide-drawers 'children))
  7335. (org-clean-visibility-after-subtree-move)
  7336. ;; move back to the initial column we were at
  7337. (move-to-column col)))
  7338. (defvar org-subtree-clip ""
  7339. "Clipboard for cut and paste of subtrees.
  7340. This is actually only a copy of the kill, because we use the normal kill
  7341. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7342. (defvar org-subtree-clip-folded nil
  7343. "Was the last copied subtree folded?
  7344. This is used to fold the tree back after pasting.")
  7345. (defun org-cut-subtree (&optional n)
  7346. "Cut the current subtree into the clipboard.
  7347. With prefix arg N, cut this many sequential subtrees.
  7348. This is a short-hand for marking the subtree and then cutting it."
  7349. (interactive "p")
  7350. (org-copy-subtree n 'cut))
  7351. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7352. "Cut the current subtree into the clipboard.
  7353. With prefix arg N, cut this many sequential subtrees.
  7354. This is a short-hand for marking the subtree and then copying it.
  7355. If CUT is non-nil, actually cut the subtree.
  7356. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7357. of some markers in the region, even if CUT is non-nil. This is
  7358. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7359. (interactive "p")
  7360. (let (beg end folded (beg0 (point)))
  7361. (if (org-called-interactively-p 'any)
  7362. (org-back-to-heading nil) ; take what looks like a subtree
  7363. (org-back-to-heading t)) ; take what is really there
  7364. (setq beg (point))
  7365. (skip-chars-forward " \t\r\n")
  7366. (save-match-data
  7367. (if nosubtrees
  7368. (outline-next-heading)
  7369. (save-excursion (outline-end-of-heading)
  7370. (setq folded (outline-invisible-p)))
  7371. (condition-case nil
  7372. (org-forward-heading-same-level (1- n) t)
  7373. (error nil))
  7374. (org-end-of-subtree t t)))
  7375. (setq end (point))
  7376. (goto-char beg0)
  7377. (when (> end beg)
  7378. (setq org-subtree-clip-folded folded)
  7379. (when (or cut force-store-markers)
  7380. (org-save-markers-in-region beg end))
  7381. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7382. (setq org-subtree-clip (current-kill 0))
  7383. (message "%s: Subtree(s) with %d characters"
  7384. (if cut "Cut" "Copied")
  7385. (length org-subtree-clip)))))
  7386. (defun org-paste-subtree (&optional level tree for-yank)
  7387. "Paste the clipboard as a subtree, with modification of headline level.
  7388. The entire subtree is promoted or demoted in order to match a new headline
  7389. level.
  7390. If the cursor is at the beginning of a headline, the same level as
  7391. that headline is used to paste the tree.
  7392. If not, the new level is derived from the *visible* headings
  7393. before and after the insertion point, and taken to be the inferior headline
  7394. level of the two. So if the previous visible heading is level 3 and the
  7395. next is level 4 (or vice versa), level 4 will be used for insertion.
  7396. This makes sure that the subtree remains an independent subtree and does
  7397. not swallow low level entries.
  7398. You can also force a different level, either by using a numeric prefix
  7399. argument, or by inserting the heading marker by hand. For example, if the
  7400. cursor is after \"*****\", then the tree will be shifted to level 5.
  7401. If optional TREE is given, use this text instead of the kill ring.
  7402. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7403. move back over whitespace before inserting, and move point to the end of
  7404. the inserted text when done."
  7405. (interactive "P")
  7406. (setq tree (or tree (and kill-ring (current-kill 0))))
  7407. (unless (org-kill-is-subtree-p tree)
  7408. (user-error "%s"
  7409. (substitute-command-keys
  7410. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7411. (org-with-limited-levels
  7412. (let* ((visp (not (outline-invisible-p)))
  7413. (txt tree)
  7414. (^re_ "\\(\\*+\\)[ \t]*")
  7415. (old-level (if (string-match org-outline-regexp-bol txt)
  7416. (- (match-end 0) (match-beginning 0) 1)
  7417. -1))
  7418. (force-level (cond (level (prefix-numeric-value level))
  7419. ((and (looking-at "[ \t]*$")
  7420. (string-match
  7421. "^\\*+$" (buffer-substring
  7422. (point-at-bol) (point))))
  7423. (- (match-end 1) (match-beginning 1)))
  7424. ((and (bolp)
  7425. (looking-at org-outline-regexp))
  7426. (- (match-end 0) (point) 1))))
  7427. (previous-level (save-excursion
  7428. (condition-case nil
  7429. (progn
  7430. (outline-previous-visible-heading 1)
  7431. (if (looking-at ^re_)
  7432. (- (match-end 0) (match-beginning 0) 1)
  7433. 1))
  7434. (error 1))))
  7435. (next-level (save-excursion
  7436. (condition-case nil
  7437. (progn
  7438. (or (looking-at org-outline-regexp)
  7439. (outline-next-visible-heading 1))
  7440. (if (looking-at ^re_)
  7441. (- (match-end 0) (match-beginning 0) 1)
  7442. 1))
  7443. (error 1))))
  7444. (new-level (or force-level (max previous-level next-level)))
  7445. (shift (if (or (= old-level -1)
  7446. (= new-level -1)
  7447. (= old-level new-level))
  7448. 0
  7449. (- new-level old-level)))
  7450. (delta (if (> shift 0) -1 1))
  7451. (func (if (> shift 0) 'org-demote 'org-promote))
  7452. (org-odd-levels-only nil)
  7453. beg end newend)
  7454. ;; Remove the forced level indicator
  7455. (if force-level
  7456. (delete-region (point-at-bol) (point)))
  7457. ;; Paste
  7458. (beginning-of-line (if (bolp) 1 2))
  7459. (setq beg (point))
  7460. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7461. (insert-before-markers txt)
  7462. (unless (string-match "\n\\'" txt) (insert "\n"))
  7463. (setq newend (point))
  7464. (org-reinstall-markers-in-region beg)
  7465. (setq end (point))
  7466. (goto-char beg)
  7467. (skip-chars-forward " \t\n\r")
  7468. (setq beg (point))
  7469. (if (and (outline-invisible-p) visp)
  7470. (save-excursion (outline-show-heading)))
  7471. ;; Shift if necessary
  7472. (unless (= shift 0)
  7473. (save-restriction
  7474. (narrow-to-region beg end)
  7475. (while (not (= shift 0))
  7476. (org-map-region func (point-min) (point-max))
  7477. (setq shift (+ delta shift)))
  7478. (goto-char (point-min))
  7479. (setq newend (point-max))))
  7480. (when (or (org-called-interactively-p 'interactive) for-yank)
  7481. (message "Clipboard pasted as level %d subtree" new-level))
  7482. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7483. kill-ring
  7484. (eq org-subtree-clip (current-kill 0))
  7485. org-subtree-clip-folded)
  7486. ;; The tree was folded before it was killed/copied
  7487. (hide-subtree))
  7488. (and for-yank (goto-char newend)))))
  7489. (defun org-kill-is-subtree-p (&optional txt)
  7490. "Check if the current kill is an outline subtree, or a set of trees.
  7491. Returns nil if kill does not start with a headline, or if the first
  7492. headline level is not the largest headline level in the tree.
  7493. So this will actually accept several entries of equal levels as well,
  7494. which is OK for `org-paste-subtree'.
  7495. If optional TXT is given, check this string instead of the current kill."
  7496. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7497. (re (org-get-limited-outline-regexp))
  7498. (^re (concat "^" re))
  7499. (start-level (and kill
  7500. (string-match
  7501. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7502. kill)
  7503. (- (match-end 2) (match-beginning 2) 1)))
  7504. (start (1+ (or (match-beginning 2) -1))))
  7505. (if (not start-level)
  7506. (progn
  7507. nil) ;; does not even start with a heading
  7508. (catch 'exit
  7509. (while (setq start (string-match ^re kill (1+ start)))
  7510. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7511. (throw 'exit nil)))
  7512. t))))
  7513. (defvar org-markers-to-move nil
  7514. "Markers that should be moved with a cut-and-paste operation.
  7515. Those markers are stored together with their positions relative to
  7516. the start of the region.")
  7517. (defun org-save-markers-in-region (beg end)
  7518. "Check markers in region.
  7519. If these markers are between BEG and END, record their position relative
  7520. to BEG, so that after moving the block of text, we can put the markers back
  7521. into place.
  7522. This function gets called just before an entry or tree gets cut from the
  7523. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7524. called immediately, to move the markers with the entries."
  7525. (setq org-markers-to-move nil)
  7526. (when (featurep 'org-clock)
  7527. (org-clock-save-markers-for-cut-and-paste beg end))
  7528. (when (featurep 'org-agenda)
  7529. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7530. (defun org-check-and-save-marker (marker beg end)
  7531. "Check if MARKER is between BEG and END.
  7532. If yes, remember the marker and the distance to BEG."
  7533. (when (and (marker-buffer marker)
  7534. (equal (marker-buffer marker) (current-buffer)))
  7535. (if (and (>= marker beg) (< marker end))
  7536. (push (cons marker (- marker beg)) org-markers-to-move))))
  7537. (defun org-reinstall-markers-in-region (beg)
  7538. "Move all remembered markers to their position relative to BEG."
  7539. (mapc (lambda (x)
  7540. (move-marker (car x) (+ beg (cdr x))))
  7541. org-markers-to-move)
  7542. (setq org-markers-to-move nil))
  7543. (defun org-narrow-to-subtree ()
  7544. "Narrow buffer to the current subtree."
  7545. (interactive)
  7546. (save-excursion
  7547. (save-match-data
  7548. (org-with-limited-levels
  7549. (narrow-to-region
  7550. (progn (org-back-to-heading t) (point))
  7551. (progn (org-end-of-subtree t t)
  7552. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7553. (point)))))))
  7554. (defun org-narrow-to-block ()
  7555. "Narrow buffer to the current block."
  7556. (interactive)
  7557. (let* ((case-fold-search t)
  7558. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7559. "^[ \t]*#\\+end_.*")))
  7560. (if blockp
  7561. (narrow-to-region (car blockp) (cdr blockp))
  7562. (user-error "Not in a block"))))
  7563. (eval-when-compile
  7564. (defvar org-property-drawer-re))
  7565. (defvar org-property-start-re) ;; defined below
  7566. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7567. "Clone the task (subtree) at point N times.
  7568. The clones will be inserted as siblings.
  7569. In interactive use, the user will be prompted for the number of
  7570. clones to be produced. If the entry has a timestamp, the user
  7571. will also be prompted for a time shift, which may be a repeater
  7572. as used in time stamps, for example `+3d'. To disable this,
  7573. you can call the function with a universal prefix argument.
  7574. When a valid repeater is given and the entry contains any time
  7575. stamps, the clones will become a sequence in time, with time
  7576. stamps in the subtree shifted for each clone produced. If SHIFT
  7577. is nil or the empty string, time stamps will be left alone. The
  7578. ID property of the original subtree is removed.
  7579. If the original subtree did contain time stamps with a repeater,
  7580. the following will happen:
  7581. - the repeater will be removed in each clone
  7582. - an additional clone will be produced, with the current, unshifted
  7583. date(s) in the entry.
  7584. - the original entry will be placed *after* all the clones, with
  7585. repeater intact.
  7586. - the start days in the repeater in the original entry will be shifted
  7587. to past the last clone.
  7588. In this way you can spell out a number of instances of a repeating task,
  7589. and still retain the repeater to cover future instances of the task."
  7590. (interactive "nNumber of clones to produce: ")
  7591. (let ((shift
  7592. (or shift
  7593. (if (and (not (equal current-prefix-arg '(4)))
  7594. (save-excursion
  7595. (re-search-forward org-ts-regexp-both
  7596. (save-excursion
  7597. (org-end-of-subtree t)
  7598. (point)) t)))
  7599. (read-from-minibuffer
  7600. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7601. ""))) ;; No time shift
  7602. (n-no-remove -1)
  7603. (drawer-re org-drawer-regexp)
  7604. beg end template task idprop
  7605. shift-n shift-what doshift nmin nmax)
  7606. (if (not (and (integerp n) (> n 0)))
  7607. (error "Invalid number of replications %s" n))
  7608. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7609. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7610. shift)))
  7611. (error "Invalid shift specification %s" shift))
  7612. (when doshift
  7613. (setq shift-n (string-to-number (match-string 1 shift))
  7614. shift-what (cdr (assoc (match-string 2 shift)
  7615. '(("d" . day) ("w" . week)
  7616. ("m" . month) ("y" . year))))))
  7617. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7618. (setq nmin 1 nmax n)
  7619. (org-back-to-heading t)
  7620. (setq beg (point))
  7621. (setq idprop (org-entry-get nil "ID"))
  7622. (org-end-of-subtree t t)
  7623. (or (bolp) (insert "\n"))
  7624. (setq end (point))
  7625. (setq template (buffer-substring beg end))
  7626. (when (and doshift
  7627. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7628. (delete-region beg end)
  7629. (setq end beg)
  7630. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7631. (goto-char end)
  7632. (loop for n from nmin to nmax do
  7633. ;; prepare clone
  7634. (with-temp-buffer
  7635. (insert template)
  7636. (org-mode)
  7637. (goto-char (point-min))
  7638. (org-show-subtree)
  7639. (and idprop (if org-clone-delete-id
  7640. (org-entry-delete nil "ID")
  7641. (org-id-get-create t)))
  7642. (unless (= n 0)
  7643. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7644. (kill-whole-line))
  7645. (goto-char (point-min))
  7646. (while (re-search-forward drawer-re nil t)
  7647. (mapc (lambda (d)
  7648. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7649. (goto-char (point-min))
  7650. (when doshift
  7651. (while (re-search-forward org-ts-regexp-both nil t)
  7652. (org-timestamp-change (* n shift-n) shift-what))
  7653. (unless (= n n-no-remove)
  7654. (goto-char (point-min))
  7655. (while (re-search-forward org-ts-regexp nil t)
  7656. (save-excursion
  7657. (goto-char (match-beginning 0))
  7658. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7659. (delete-region (match-beginning 1) (match-end 1)))))))
  7660. (setq task (buffer-string)))
  7661. (insert task))
  7662. (goto-char beg)))
  7663. ;;; Outline Sorting
  7664. (defun org-sort (with-case)
  7665. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7666. Optional argument WITH-CASE means sort case-sensitively."
  7667. (interactive "P")
  7668. (cond
  7669. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7670. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7671. (t
  7672. (org-call-with-arg 'org-sort-entries with-case))))
  7673. (defun org-sort-remove-invisible (s)
  7674. "Remove invisible links from string S."
  7675. (remove-text-properties 0 (length s) org-rm-props s)
  7676. (while (string-match org-bracket-link-regexp s)
  7677. (setq s (replace-match (if (match-end 2)
  7678. (match-string 3 s)
  7679. (match-string 1 s)) t t s)))
  7680. (let ((st (format " %s " s)))
  7681. (while (string-match org-emph-re st)
  7682. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7683. (setq s (substring st 1 -1)))
  7684. s)
  7685. (defvar org-priority-regexp) ; defined later in the file
  7686. (defvar org-after-sorting-entries-or-items-hook nil
  7687. "Hook that is run after a bunch of entries or items have been sorted.
  7688. When children are sorted, the cursor is in the parent line when this
  7689. hook gets called. When a region or a plain list is sorted, the cursor
  7690. will be in the first entry of the sorted region/list.")
  7691. (defun org-sort-entries
  7692. (&optional with-case sorting-type getkey-func compare-func property)
  7693. "Sort entries on a certain level of an outline tree.
  7694. If there is an active region, the entries in the region are sorted.
  7695. Else, if the cursor is before the first entry, sort the top-level items.
  7696. Else, the children of the entry at point are sorted.
  7697. Sorting can be alphabetically, numerically, by date/time as given by
  7698. a time stamp, by a property, by priority order, or by a custom function.
  7699. The command prompts for the sorting type unless it has been given to the
  7700. function through the SORTING-TYPE argument, which needs to be a character,
  7701. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7702. precise meaning of each character:
  7703. n Numerically, by converting the beginning of the entry/item to a number.
  7704. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7705. o By order of TODO keywords.
  7706. t By date/time, either the first active time stamp in the entry, or, if
  7707. none exist, by the first inactive one.
  7708. s By the scheduled date/time.
  7709. d By deadline date/time.
  7710. c By creation time, which is assumed to be the first inactive time stamp
  7711. at the beginning of a line.
  7712. p By priority according to the cookie.
  7713. r By the value of a property.
  7714. Capital letters will reverse the sort order.
  7715. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7716. called with point at the beginning of the record. It must return either
  7717. a string or a number that should serve as the sorting key for that record.
  7718. Comparing entries ignores case by default. However, with an optional argument
  7719. WITH-CASE, the sorting considers case as well.
  7720. Sorting is done against the visible part of the headlines, it ignores hidden
  7721. links."
  7722. (interactive "P")
  7723. (let ((case-func (if with-case 'identity 'downcase))
  7724. (cmstr
  7725. ;; The clock marker is lost when using `sort-subr', let's
  7726. ;; store the clocking string.
  7727. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7728. (save-excursion
  7729. (goto-char org-clock-marker)
  7730. (looking-back "^.*") (match-string-no-properties 0))))
  7731. start beg end stars re re2
  7732. txt what tmp)
  7733. ;; Find beginning and end of region to sort
  7734. (cond
  7735. ((org-region-active-p)
  7736. ;; we will sort the region
  7737. (setq end (region-end)
  7738. what "region")
  7739. (goto-char (region-beginning))
  7740. (if (not (org-at-heading-p)) (outline-next-heading))
  7741. (setq start (point)))
  7742. ((or (org-at-heading-p)
  7743. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7744. ;; we will sort the children of the current headline
  7745. (org-back-to-heading)
  7746. (setq start (point)
  7747. end (progn (org-end-of-subtree t t)
  7748. (or (bolp) (insert "\n"))
  7749. (org-back-over-empty-lines)
  7750. (point))
  7751. what "children")
  7752. (goto-char start)
  7753. (show-subtree)
  7754. (outline-next-heading))
  7755. (t
  7756. ;; we will sort the top-level entries in this file
  7757. (goto-char (point-min))
  7758. (or (org-at-heading-p) (outline-next-heading))
  7759. (setq start (point))
  7760. (goto-char (point-max))
  7761. (beginning-of-line 1)
  7762. (when (looking-at ".*?\\S-")
  7763. ;; File ends in a non-white line
  7764. (end-of-line 1)
  7765. (insert "\n"))
  7766. (setq end (point-max))
  7767. (setq what "top-level")
  7768. (goto-char start)
  7769. (show-all)))
  7770. (setq beg (point))
  7771. (if (>= beg end) (user-error "Nothing to sort"))
  7772. (looking-at "\\(\\*+\\)")
  7773. (setq stars (match-string 1)
  7774. re (concat "^" (regexp-quote stars) " +")
  7775. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7776. txt (buffer-substring beg end))
  7777. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7778. (if (and (not (equal stars "*")) (string-match re2 txt))
  7779. (user-error "Region to sort contains a level above the first entry"))
  7780. (unless sorting-type
  7781. (message
  7782. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7783. [t]ime [s]cheduled [d]eadline [c]reated
  7784. A/N/P/R/O/F/T/S/D/C means reversed:"
  7785. what)
  7786. (setq sorting-type (read-char-exclusive))
  7787. (unless getkey-func
  7788. (and (= (downcase sorting-type) ?f)
  7789. (setq getkey-func
  7790. (org-icompleting-read "Sort using function: "
  7791. obarray 'fboundp t nil nil))
  7792. (setq getkey-func (intern getkey-func))))
  7793. (and (= (downcase sorting-type) ?r)
  7794. (not property)
  7795. (setq property
  7796. (org-icompleting-read "Property: "
  7797. (mapcar 'list (org-buffer-property-keys t))
  7798. nil t))))
  7799. (message "Sorting entries...")
  7800. (save-restriction
  7801. (narrow-to-region start end)
  7802. (let ((dcst (downcase sorting-type))
  7803. (case-fold-search nil)
  7804. (now (current-time)))
  7805. (sort-subr
  7806. (/= dcst sorting-type)
  7807. ;; This function moves to the beginning character of the "record" to
  7808. ;; be sorted.
  7809. (lambda nil
  7810. (if (re-search-forward re nil t)
  7811. (goto-char (match-beginning 0))
  7812. (goto-char (point-max))))
  7813. ;; This function moves to the last character of the "record" being
  7814. ;; sorted.
  7815. (lambda nil
  7816. (save-match-data
  7817. (condition-case nil
  7818. (outline-forward-same-level 1)
  7819. (error
  7820. (goto-char (point-max))))))
  7821. ;; This function returns the value that gets sorted against.
  7822. (lambda nil
  7823. (cond
  7824. ((= dcst ?n)
  7825. (if (looking-at org-complex-heading-regexp)
  7826. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7827. nil))
  7828. ((= dcst ?a)
  7829. (if (looking-at org-complex-heading-regexp)
  7830. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7831. nil))
  7832. ((= dcst ?t)
  7833. (let ((end (save-excursion (outline-next-heading) (point))))
  7834. (if (or (re-search-forward org-ts-regexp end t)
  7835. (re-search-forward org-ts-regexp-both end t))
  7836. (org-time-string-to-seconds (match-string 0))
  7837. (org-float-time now))))
  7838. ((= dcst ?c)
  7839. (let ((end (save-excursion (outline-next-heading) (point))))
  7840. (if (re-search-forward
  7841. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7842. end t)
  7843. (org-time-string-to-seconds (match-string 0))
  7844. (org-float-time now))))
  7845. ((= dcst ?s)
  7846. (let ((end (save-excursion (outline-next-heading) (point))))
  7847. (if (re-search-forward org-scheduled-time-regexp end t)
  7848. (org-time-string-to-seconds (match-string 1))
  7849. (org-float-time now))))
  7850. ((= dcst ?d)
  7851. (let ((end (save-excursion (outline-next-heading) (point))))
  7852. (if (re-search-forward org-deadline-time-regexp end t)
  7853. (org-time-string-to-seconds (match-string 1))
  7854. (org-float-time now))))
  7855. ((= dcst ?p)
  7856. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7857. (string-to-char (match-string 2))
  7858. org-default-priority))
  7859. ((= dcst ?r)
  7860. (or (org-entry-get nil property) ""))
  7861. ((= dcst ?o)
  7862. (if (looking-at org-complex-heading-regexp)
  7863. (- 9999 (length (member (match-string 2)
  7864. org-todo-keywords-1)))))
  7865. ((= dcst ?f)
  7866. (if getkey-func
  7867. (progn
  7868. (setq tmp (funcall getkey-func))
  7869. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7870. tmp)
  7871. (error "Invalid key function `%s'" getkey-func)))
  7872. (t (error "Invalid sorting type `%c'" sorting-type))))
  7873. nil
  7874. (cond
  7875. ((= dcst ?a) 'string<)
  7876. ((= dcst ?f) compare-func)
  7877. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7878. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7879. ;; Reset the clock marker if needed
  7880. (when cmstr
  7881. (save-excursion
  7882. (goto-char start)
  7883. (search-forward cmstr nil t)
  7884. (move-marker org-clock-marker (point))))
  7885. (message "Sorting entries...done")))
  7886. (defun org-do-sort (table what &optional with-case sorting-type)
  7887. "Sort TABLE of WHAT according to SORTING-TYPE.
  7888. The user will be prompted for the SORTING-TYPE if the call to this
  7889. function does not specify it. WHAT is only for the prompt, to indicate
  7890. what is being sorted. The sorting key will be extracted from
  7891. the car of the elements of the table.
  7892. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7893. (unless sorting-type
  7894. (message
  7895. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7896. what)
  7897. (setq sorting-type (read-char-exclusive)))
  7898. (let ((dcst (downcase sorting-type))
  7899. extractfun comparefun)
  7900. ;; Define the appropriate functions
  7901. (cond
  7902. ((= dcst ?n)
  7903. (setq extractfun 'string-to-number
  7904. comparefun (if (= dcst sorting-type) '< '>)))
  7905. ((= dcst ?a)
  7906. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7907. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7908. comparefun (if (= dcst sorting-type)
  7909. 'string<
  7910. (lambda (a b) (and (not (string< a b))
  7911. (not (string= a b)))))))
  7912. ((= dcst ?t)
  7913. (setq extractfun
  7914. (lambda (x)
  7915. (if (or (string-match org-ts-regexp x)
  7916. (string-match org-ts-regexp-both x))
  7917. (org-float-time
  7918. (org-time-string-to-time (match-string 0 x)))
  7919. 0))
  7920. comparefun (if (= dcst sorting-type) '< '>)))
  7921. (t (error "Invalid sorting type `%c'" sorting-type)))
  7922. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7923. table)
  7924. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7925. ;;; The orgstruct minor mode
  7926. ;; Define a minor mode which can be used in other modes in order to
  7927. ;; integrate the org-mode structure editing commands.
  7928. ;; This is really a hack, because the org-mode structure commands use
  7929. ;; keys which normally belong to the major mode. Here is how it
  7930. ;; works: The minor mode defines all the keys necessary to operate the
  7931. ;; structure commands, but wraps the commands into a function which
  7932. ;; tests if the cursor is currently at a headline or a plain list
  7933. ;; item. If that is the case, the structure command is used,
  7934. ;; temporarily setting many Org-mode variables like regular
  7935. ;; expressions for filling etc. However, when any of those keys is
  7936. ;; used at a different location, function uses `key-binding' to look
  7937. ;; up if the key has an associated command in another currently active
  7938. ;; keymap (minor modes, major mode, global), and executes that
  7939. ;; command. There might be problems if any of the keys is otherwise
  7940. ;; used as a prefix key.
  7941. (defcustom orgstruct-heading-prefix-regexp nil
  7942. "Regexp that matches the custom prefix of Org headlines in
  7943. orgstruct(++)-mode."
  7944. :group 'org
  7945. :version "24.4"
  7946. :package-version '(Org . "8.0")
  7947. :type 'string)
  7948. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7949. (defcustom orgstruct-setup-hook nil
  7950. "Hook run after orgstruct-mode-map is filled."
  7951. :group 'org
  7952. :version "24.4"
  7953. :package-version '(Org . "8.0")
  7954. :type 'hook)
  7955. (defvar orgstruct-initialized nil)
  7956. (defvar org-local-vars nil
  7957. "List of local variables, for use by `orgstruct-mode'.")
  7958. ;;;###autoload
  7959. (define-minor-mode orgstruct-mode
  7960. "Toggle the minor mode `orgstruct-mode'.
  7961. This mode is for using Org-mode structure commands in other
  7962. modes. The following keys behave as if Org-mode were active, if
  7963. the cursor is on a headline, or on a plain list item (both as
  7964. defined by Org-mode)."
  7965. nil " OrgStruct" (make-sparse-keymap)
  7966. (funcall (if orgstruct-mode
  7967. 'add-to-invisibility-spec
  7968. 'remove-from-invisibility-spec)
  7969. '(outline . t))
  7970. (when orgstruct-mode
  7971. (org-load-modules-maybe)
  7972. (unless orgstruct-initialized
  7973. (orgstruct-setup)
  7974. (setq orgstruct-initialized t))))
  7975. ;;;###autoload
  7976. (defun turn-on-orgstruct ()
  7977. "Unconditionally turn on `orgstruct-mode'."
  7978. (orgstruct-mode 1))
  7979. (defvar org-fb-vars nil)
  7980. (make-variable-buffer-local 'org-fb-vars)
  7981. (defun orgstruct++-mode (&optional arg)
  7982. "Toggle `orgstruct-mode', the enhanced version of it.
  7983. In addition to setting orgstruct-mode, this also exports all
  7984. indentation and autofilling variables from org-mode into the
  7985. buffer. It will also recognize item context in multiline items."
  7986. (interactive "P")
  7987. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7988. (if (< arg 1)
  7989. (progn (orgstruct-mode -1)
  7990. (mapc (lambda(v)
  7991. (org-set-local (car v)
  7992. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7993. org-fb-vars))
  7994. (orgstruct-mode 1)
  7995. (setq org-fb-vars nil)
  7996. (let (var val)
  7997. (mapc
  7998. (lambda (x)
  7999. (when (string-match
  8000. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8001. (symbol-name (car x)))
  8002. (setq var (car x) val (nth 1 x))
  8003. (push (list var `(quote ,(eval var))) org-fb-vars)
  8004. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8005. org-local-vars)
  8006. (org-set-local 'orgstruct-is-++ t))))
  8007. (defvar orgstruct-is-++ nil
  8008. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8009. (make-variable-buffer-local 'orgstruct-is-++)
  8010. ;;;###autoload
  8011. (defun turn-on-orgstruct++ ()
  8012. "Unconditionally turn on `orgstruct++-mode'."
  8013. (orgstruct++-mode 1))
  8014. (defun orgstruct-error ()
  8015. "Error when there is no default binding for a structure key."
  8016. (interactive)
  8017. (funcall (if (fboundp 'user-error)
  8018. 'user-error
  8019. 'error)
  8020. "This key has no function outside structure elements"))
  8021. (defun orgstruct-setup ()
  8022. "Setup orgstruct keymap."
  8023. (dolist (cell '((org-demote . t)
  8024. (org-metaleft . t)
  8025. (org-metaright . t)
  8026. (org-promote . t)
  8027. (org-shiftmetaleft . t)
  8028. (org-shiftmetaright . t)
  8029. org-backward-element
  8030. org-backward-heading-same-level
  8031. org-ctrl-c-ret
  8032. org-ctrl-c-minus
  8033. org-ctrl-c-star
  8034. org-cycle
  8035. org-forward-heading-same-level
  8036. org-insert-heading
  8037. org-insert-heading-respect-content
  8038. org-kill-note-or-show-branches
  8039. org-mark-subtree
  8040. org-meta-return
  8041. org-metadown
  8042. org-metaup
  8043. org-narrow-to-subtree
  8044. org-promote-subtree
  8045. org-reveal
  8046. org-shiftdown
  8047. org-shiftleft
  8048. org-shiftmetadown
  8049. org-shiftmetaup
  8050. org-shiftright
  8051. org-shifttab
  8052. org-shifttab
  8053. org-shiftup
  8054. org-show-subtree
  8055. org-sort
  8056. org-up-element
  8057. outline-demote
  8058. outline-next-visible-heading
  8059. outline-previous-visible-heading
  8060. outline-promote
  8061. outline-up-heading
  8062. show-children))
  8063. (let ((f (or (car-safe cell) cell))
  8064. (disable-when-heading-prefix (cdr-safe cell)))
  8065. (when (fboundp f)
  8066. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8067. (where-is-internal f outline-mode-map)))
  8068. ;; TODO use local-function-key-map
  8069. (dolist (rep '(("<tab>" . "TAB")
  8070. ("<return>" . "RET")
  8071. ("<escape>" . "ESC")
  8072. ("<delete>" . "DEL")))
  8073. (setq binding (read-kbd-macro
  8074. (let ((case-fold-search))
  8075. (replace-regexp-in-string
  8076. (regexp-quote (cdr rep))
  8077. (car rep)
  8078. (key-description binding))))))
  8079. (let ((key (lookup-key orgstruct-mode-map binding)))
  8080. (when (or (not key) (numberp key))
  8081. (condition-case nil
  8082. (org-defkey orgstruct-mode-map
  8083. binding
  8084. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8085. (error nil))))))))
  8086. (run-hooks 'orgstruct-setup-hook))
  8087. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8088. "Create a function for binding in the structure minor mode.
  8089. FUN is the command to call inside a table. KEY is the key that
  8090. should be checked in for a command to execute outside of tables.
  8091. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8092. if `orgstruct-heading-prefix-regexp' is non-nil."
  8093. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8094. (let ((nname name)
  8095. (i 0))
  8096. (while (fboundp (intern nname))
  8097. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8098. (setq name (intern nname)))
  8099. (eval
  8100. (let ((bindings '((org-heading-regexp
  8101. (concat "^"
  8102. orgstruct-heading-prefix-regexp
  8103. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8104. (org-outline-regexp
  8105. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8106. (org-outline-regexp-bol
  8107. (concat "^" org-outline-regexp))
  8108. (outline-regexp org-outline-regexp)
  8109. (outline-heading-end-regexp "\n")
  8110. (outline-level 'org-outline-level)
  8111. (outline-heading-alist))))
  8112. `(defun ,name (arg)
  8113. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8114. "Outside of structure, run the binding of `"
  8115. (key-description key) "'."
  8116. (when disable-when-heading-prefix
  8117. (concat
  8118. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8119. "back to the default binding due to limitations of Org's implementation of\n"
  8120. "`" (symbol-name fun) "'.")))
  8121. (interactive "p")
  8122. (let* ((disable
  8123. ,(when disable-when-heading-prefix
  8124. '(and orgstruct-heading-prefix-regexp
  8125. (not (string= orgstruct-heading-prefix-regexp "")))))
  8126. (fallback
  8127. (or disable
  8128. (not
  8129. (let* ,bindings
  8130. (org-context-p 'headline 'item
  8131. ,(when (memq fun '(org-insert-heading))
  8132. '(when orgstruct-is-++
  8133. 'item-body))))))))
  8134. (if fallback
  8135. (let* ((orgstruct-mode)
  8136. (binding
  8137. (loop with key = ,key
  8138. for rep in
  8139. '(nil
  8140. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8141. ("<\\([^>]*\\)return>" . "\\1RET")
  8142. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8143. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8144. do
  8145. (when rep
  8146. (setq key (read-kbd-macro
  8147. (let ((case-fold-search))
  8148. (replace-regexp-in-string
  8149. (car rep)
  8150. (cdr rep)
  8151. (key-description key))))))
  8152. thereis (key-binding key))))
  8153. (if (keymapp binding)
  8154. (set-temporary-overlay-map binding)
  8155. (let ((func (or binding
  8156. (unless disable
  8157. 'orgstruct-error))))
  8158. (when func
  8159. (call-interactively func)))))
  8160. (org-run-like-in-org-mode
  8161. (lambda ()
  8162. (interactive)
  8163. (let* ,bindings
  8164. (call-interactively ',fun)))))))))
  8165. name))
  8166. (defun org-contextualize-keys (alist contexts)
  8167. "Return valid elements in ALIST depending on CONTEXTS.
  8168. `org-agenda-custom-commands' or `org-capture-templates' are the
  8169. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8170. or `org-capture-templates-contexts' are the associated contexts
  8171. definitions."
  8172. (let ((contexts
  8173. ;; normalize contexts
  8174. (mapcar
  8175. (lambda(c) (cond ((listp (cadr c))
  8176. (list (car c) (car c) (cadr c)))
  8177. ((string= "" (cadr c))
  8178. (list (car c) (car c) (caddr c)))
  8179. (t c))) contexts))
  8180. (a alist) c r s)
  8181. ;; loop over all commands or templates
  8182. (while (setq c (pop a))
  8183. (let (vrules repl)
  8184. (cond
  8185. ((not (assoc (car c) contexts))
  8186. (push c r))
  8187. ((and (assoc (car c) contexts)
  8188. (setq vrules (org-contextualize-validate-key
  8189. (car c) contexts)))
  8190. (mapc (lambda (vr)
  8191. (when (not (equal (car vr) (cadr vr)))
  8192. (setq repl vr))) vrules)
  8193. (if (not repl) (push c r)
  8194. (push (cadr repl) s)
  8195. (push
  8196. (cons (car c)
  8197. (cdr (or (assoc (cadr repl) alist)
  8198. (error "Undefined key `%s' as contextual replacement for `%s'"
  8199. (cadr repl) (car c)))))
  8200. r))))))
  8201. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8202. (delq
  8203. nil
  8204. (delete-dups
  8205. (mapcar (lambda (x)
  8206. (let ((tpl (car x)))
  8207. (when (not (delq
  8208. nil
  8209. (mapcar (lambda(y)
  8210. (equal y tpl)) s))) x)))
  8211. (reverse r))))))
  8212. (defun org-contextualize-validate-key (key contexts)
  8213. "Check CONTEXTS for agenda or capture KEY."
  8214. (let (r rr res)
  8215. (while (setq r (pop contexts))
  8216. (mapc
  8217. (lambda (rr)
  8218. (when
  8219. (and (equal key (car r))
  8220. (if (functionp rr) (funcall rr)
  8221. (or (and (eq (car rr) 'in-file)
  8222. (buffer-file-name)
  8223. (string-match (cdr rr) (buffer-file-name)))
  8224. (and (eq (car rr) 'in-mode)
  8225. (string-match (cdr rr) (symbol-name major-mode)))
  8226. (and (eq (car rr) 'in-buffer)
  8227. (string-match (cdr rr) (buffer-name)))
  8228. (when (and (eq (car rr) 'not-in-file)
  8229. (buffer-file-name))
  8230. (not (string-match (cdr rr) (buffer-file-name))))
  8231. (when (eq (car rr) 'not-in-mode)
  8232. (not (string-match (cdr rr) (symbol-name major-mode))))
  8233. (when (eq (car rr) 'not-in-buffer)
  8234. (not (string-match (cdr rr) (buffer-name)))))))
  8235. (push r res)))
  8236. (car (last r))))
  8237. (delete-dups (delq nil res))))
  8238. (defun org-context-p (&rest contexts)
  8239. "Check if local context is any of CONTEXTS.
  8240. Possible values in the list of contexts are `table', `headline', and `item'."
  8241. (let ((pos (point)))
  8242. (goto-char (point-at-bol))
  8243. (prog1 (or (and (memq 'table contexts)
  8244. (looking-at "[ \t]*|"))
  8245. (and (memq 'headline contexts)
  8246. (looking-at org-outline-regexp))
  8247. (and (memq 'item contexts)
  8248. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8249. (and (memq 'item-body contexts)
  8250. (org-in-item-p)))
  8251. (goto-char pos))))
  8252. (defun org-get-local-variables ()
  8253. "Return a list of all local variables in an Org mode buffer."
  8254. (let (varlist)
  8255. (with-current-buffer (get-buffer-create "*Org tmp*")
  8256. (erase-buffer)
  8257. (org-mode)
  8258. (setq varlist (buffer-local-variables)))
  8259. (kill-buffer "*Org tmp*")
  8260. (delq nil
  8261. (mapcar
  8262. (lambda (x)
  8263. (setq x
  8264. (if (symbolp x)
  8265. (list x)
  8266. (list (car x) (cdr x))))
  8267. (if (and (not (get (car x) 'org-state))
  8268. (string-match
  8269. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8270. (symbol-name (car x))))
  8271. x nil))
  8272. varlist))))
  8273. (defun org-clone-local-variables (from-buffer &optional regexp)
  8274. "Clone local variables from FROM-BUFFER.
  8275. Optional argument REGEXP selects variables to clone."
  8276. (mapc
  8277. (lambda (pair)
  8278. (and (symbolp (car pair))
  8279. (or (null regexp)
  8280. (string-match regexp (symbol-name (car pair))))
  8281. (set (make-local-variable (car pair))
  8282. (cdr pair))))
  8283. (buffer-local-variables from-buffer)))
  8284. ;;;###autoload
  8285. (defun org-run-like-in-org-mode (cmd)
  8286. "Run a command, pretending that the current buffer is in Org-mode.
  8287. This will temporarily bind local variables that are typically bound in
  8288. Org-mode to the values they have in Org-mode, and then interactively
  8289. call CMD."
  8290. (org-load-modules-maybe)
  8291. (unless org-local-vars
  8292. (setq org-local-vars (org-get-local-variables)))
  8293. (let (binds)
  8294. (dolist (var org-local-vars)
  8295. (when (or (not (boundp (car var)))
  8296. (eq (symbol-value (car var))
  8297. (default-value (car var))))
  8298. (push (list (car var) `(quote ,(cadr var))) binds)))
  8299. (eval `(let ,binds
  8300. (call-interactively (quote ,cmd))))))
  8301. ;;;; Archiving
  8302. (defun org-get-category (&optional pos force-refresh)
  8303. "Get the category applying to position POS."
  8304. (save-match-data
  8305. (if force-refresh (org-refresh-category-properties))
  8306. (let ((pos (or pos (point))))
  8307. (or (get-text-property pos 'org-category)
  8308. (progn (org-refresh-category-properties)
  8309. (get-text-property pos 'org-category))))))
  8310. (defun org-refresh-category-properties ()
  8311. "Refresh category text properties in the buffer."
  8312. (let ((case-fold-search t)
  8313. (inhibit-read-only t)
  8314. (def-cat (cond
  8315. ((null org-category)
  8316. (if buffer-file-name
  8317. (file-name-sans-extension
  8318. (file-name-nondirectory buffer-file-name))
  8319. "???"))
  8320. ((symbolp org-category) (symbol-name org-category))
  8321. (t org-category)))
  8322. beg end cat pos optionp)
  8323. (org-with-silent-modifications
  8324. (save-excursion
  8325. (save-restriction
  8326. (widen)
  8327. (goto-char (point-min))
  8328. (put-text-property (point) (point-max) 'org-category def-cat)
  8329. (while (re-search-forward
  8330. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8331. (setq pos (match-end 0)
  8332. optionp (equal (char-after (match-beginning 0)) ?#)
  8333. cat (org-trim (match-string 2)))
  8334. (if optionp
  8335. (setq beg (point-at-bol) end (point-max))
  8336. (org-back-to-heading t)
  8337. (setq beg (point) end (org-end-of-subtree t t)))
  8338. (put-text-property beg end 'org-category cat)
  8339. (put-text-property beg end 'org-category-position beg)
  8340. (goto-char pos)))))))
  8341. (defun org-refresh-properties (dprop tprop)
  8342. "Refresh buffer text properties.
  8343. DPROP is the drawer property and TPROP is the corresponding text
  8344. property to set."
  8345. (let ((case-fold-search t)
  8346. (inhibit-read-only t) p)
  8347. (org-with-silent-modifications
  8348. (save-excursion
  8349. (save-restriction
  8350. (widen)
  8351. (goto-char (point-min))
  8352. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8353. (setq p (org-match-string-no-properties 1))
  8354. (save-excursion
  8355. (org-back-to-heading t)
  8356. (put-text-property
  8357. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8358. ;;;; Link Stuff
  8359. ;;; Link abbreviations
  8360. (defun org-link-expand-abbrev (link)
  8361. "Apply replacements as defined in `org-link-abbrev-alist'."
  8362. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8363. (let* ((key (match-string 1 link))
  8364. (as (or (assoc key org-link-abbrev-alist-local)
  8365. (assoc key org-link-abbrev-alist)))
  8366. (tag (and (match-end 2) (match-string 3 link)))
  8367. rpl)
  8368. (if (not as)
  8369. link
  8370. (setq rpl (cdr as))
  8371. (cond
  8372. ((symbolp rpl) (funcall rpl tag))
  8373. ((string-match "%(\\([^)]+\\))" rpl)
  8374. (replace-match
  8375. (save-match-data
  8376. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8377. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8378. ((string-match "%h" rpl)
  8379. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8380. (t (concat rpl tag)))))
  8381. link))
  8382. ;;; Storing and inserting links
  8383. (defvar org-insert-link-history nil
  8384. "Minibuffer history for links inserted with `org-insert-link'.")
  8385. (defvar org-stored-links nil
  8386. "Contains the links stored with `org-store-link'.")
  8387. (defvar org-store-link-plist nil
  8388. "Plist with info about the most recently link created with `org-store-link'.")
  8389. (defvar org-link-protocols nil
  8390. "Link protocols added to Org-mode using `org-add-link-type'.")
  8391. (defvar org-store-link-functions nil
  8392. "List of functions that are called to create and store a link.
  8393. Each function will be called in turn until one returns a non-nil
  8394. value. Each function should check if it is responsible for creating
  8395. this link (for example by looking at the major mode).
  8396. If not, it must exit and return nil.
  8397. If yes, it should return a non-nil value after a calling
  8398. `org-store-link-props' with a list of properties and values.
  8399. Special properties are:
  8400. :type The link prefix, like \"http\". This must be given.
  8401. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8402. This is obligatory as well.
  8403. :description Optional default description for the second pair
  8404. of brackets in an Org-mode link. The user can still change
  8405. this when inserting this link into an Org-mode buffer.
  8406. In addition to these, any additional properties can be specified
  8407. and then used in capture templates.")
  8408. (defun org-add-link-type (type &optional follow export)
  8409. "Add TYPE to the list of `org-link-types'.
  8410. Re-compute all regular expressions depending on `org-link-types'
  8411. FOLLOW and EXPORT are two functions.
  8412. FOLLOW should take the link path as the single argument and do whatever
  8413. is necessary to follow the link, for example find a file or display
  8414. a mail message.
  8415. EXPORT should format the link path for export to one of the export formats.
  8416. It should be a function accepting three arguments:
  8417. path the path of the link, the text after the prefix (like \"http:\")
  8418. desc the description of the link, if any, or a description added by
  8419. org-export-normalize-links if there is none
  8420. format the export format, a symbol like `html' or `latex' or `ascii'..
  8421. The function may use the FORMAT information to return different values
  8422. depending on the format. The return value will be put literally into
  8423. the exported file. If the return value is nil, this means Org should
  8424. do what it normally does with links which do not have EXPORT defined.
  8425. Org-mode has a built-in default for exporting links. If you are happy with
  8426. this default, there is no need to define an export function for the link
  8427. type. For a simple example of an export function, see `org-bbdb.el'."
  8428. (add-to-list 'org-link-types type t)
  8429. (org-make-link-regexps)
  8430. (if (assoc type org-link-protocols)
  8431. (setcdr (assoc type org-link-protocols) (list follow export))
  8432. (push (list type follow export) org-link-protocols)))
  8433. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8434. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8435. ;;;###autoload
  8436. (defun org-store-link (arg)
  8437. "\\<org-mode-map>Store an org-link to the current location.
  8438. This link is added to `org-stored-links' and can later be inserted
  8439. into an org-buffer with \\[org-insert-link].
  8440. For some link types, a prefix arg is interpreted.
  8441. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8442. For file links, arg negates `org-context-in-file-links'.
  8443. A double prefix arg force skipping storing functions that are not
  8444. part of Org's core.
  8445. A triple prefix arg force storing a link for each line in the
  8446. active region."
  8447. (interactive "P")
  8448. (org-load-modules-maybe)
  8449. (if (and (equal arg '(64)) (org-region-active-p))
  8450. (save-excursion
  8451. (let ((end (region-end)))
  8452. (goto-char (region-beginning))
  8453. (set-mark (point))
  8454. (while (< (point-at-eol) end)
  8455. (move-end-of-line 1) (activate-mark)
  8456. (let (current-prefix-arg)
  8457. (call-interactively 'org-store-link))
  8458. (move-beginning-of-line 2)
  8459. (set-mark (point)))))
  8460. (org-with-limited-levels
  8461. (setq org-store-link-plist nil)
  8462. (let (link cpltxt desc description search
  8463. txt custom-id agenda-link sfuns sfunsn)
  8464. (cond
  8465. ;; Store a link using an external link type
  8466. ((and (not (equal arg '(16)))
  8467. (setq sfuns
  8468. (delq
  8469. nil (mapcar (lambda (f)
  8470. (let (fs) (if (funcall f) (push f fs))))
  8471. org-store-link-functions))
  8472. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8473. (or (and (cdr sfuns)
  8474. (funcall (intern
  8475. (completing-read
  8476. "Which function for creating the link? "
  8477. sfunsn t (car sfunsn)))))
  8478. (funcall (caar sfuns)))
  8479. (setq link (plist-get org-store-link-plist :link)
  8480. desc (or (plist-get org-store-link-plist
  8481. :description) link))))
  8482. ;; Store a link from a source code buffer
  8483. ((org-src-edit-buffer-p)
  8484. (let (label gc)
  8485. (while (or (not label)
  8486. (save-excursion
  8487. (save-restriction
  8488. (widen)
  8489. (goto-char (point-min))
  8490. (re-search-forward
  8491. (regexp-quote (format org-coderef-label-format label))
  8492. nil t))))
  8493. (when label (message "Label exists already") (sit-for 2))
  8494. (setq label (read-string "Code line label: " label)))
  8495. (end-of-line 1)
  8496. (setq link (format org-coderef-label-format label))
  8497. (setq gc (- 79 (length link)))
  8498. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8499. (insert link)
  8500. (setq link (concat "(" label ")") desc nil)))
  8501. ;; We are in the agenda, link to referenced location
  8502. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8503. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8504. (get-text-property (point) 'org-marker))))
  8505. (when m
  8506. (org-with-point-at m
  8507. (setq agenda-link
  8508. (if (org-called-interactively-p 'any)
  8509. (call-interactively 'org-store-link)
  8510. (org-store-link nil)))))))
  8511. ((eq major-mode 'calendar-mode)
  8512. (let ((cd (calendar-cursor-to-date)))
  8513. (setq link
  8514. (format-time-string
  8515. (car org-time-stamp-formats)
  8516. (apply 'encode-time
  8517. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8518. nil nil nil))))
  8519. (org-store-link-props :type "calendar" :date cd)))
  8520. ((eq major-mode 'help-mode)
  8521. (setq link (concat "help:" (save-excursion
  8522. (goto-char (point-min))
  8523. (looking-at "^[^ ]+")
  8524. (match-string 0))))
  8525. (org-store-link-props :type "help"))
  8526. ((eq major-mode 'w3-mode)
  8527. (setq cpltxt (if (and (buffer-name)
  8528. (not (string-match "Untitled" (buffer-name))))
  8529. (buffer-name)
  8530. (url-view-url t))
  8531. link (url-view-url t))
  8532. (org-store-link-props :type "w3" :url (url-view-url t)))
  8533. ((eq major-mode 'image-mode)
  8534. (setq cpltxt (concat "file:"
  8535. (abbreviate-file-name buffer-file-name))
  8536. link cpltxt)
  8537. (org-store-link-props :type "image" :file buffer-file-name))
  8538. ;; In dired, store a link to the file of the current line
  8539. ((eq major-mode 'dired-mode)
  8540. (let ((file (dired-get-filename nil t)))
  8541. (setq file (if file
  8542. (abbreviate-file-name
  8543. (expand-file-name (dired-get-filename nil t)))
  8544. ;; otherwise, no file so use current directory.
  8545. default-directory))
  8546. (setq cpltxt (concat "file:" file)
  8547. link cpltxt)))
  8548. ((setq search (run-hook-with-args-until-success
  8549. 'org-create-file-search-functions))
  8550. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8551. "::" search))
  8552. (setq cpltxt (or description link)))
  8553. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8554. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8555. (cond
  8556. ;; Store a link using the target at point
  8557. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8558. (setq cpltxt
  8559. (concat "file:"
  8560. (abbreviate-file-name
  8561. (buffer-file-name (buffer-base-buffer)))
  8562. "::" (match-string 1))
  8563. link cpltxt))
  8564. ((and (featurep 'org-id)
  8565. (or (eq org-id-link-to-org-use-id t)
  8566. (and (org-called-interactively-p 'any)
  8567. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8568. (and (eq org-id-link-to-org-use-id
  8569. 'create-if-interactive-and-no-custom-id)
  8570. (not custom-id))))
  8571. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8572. ;; Store a link using the ID at point
  8573. (setq link (condition-case nil
  8574. (prog1 (org-id-store-link)
  8575. (setq desc (plist-get org-store-link-plist
  8576. :description)))
  8577. (error
  8578. ;; Probably before first headline, link only to file
  8579. (concat "file:"
  8580. (abbreviate-file-name
  8581. (buffer-file-name (buffer-base-buffer))))))))
  8582. (t
  8583. ;; Just link to current headline
  8584. (setq cpltxt (concat "file:"
  8585. (abbreviate-file-name
  8586. (buffer-file-name (buffer-base-buffer)))))
  8587. ;; Add a context search string
  8588. (when (org-xor org-context-in-file-links arg)
  8589. (let* ((ee (org-element-at-point))
  8590. (et (org-element-type ee))
  8591. (ev (plist-get (cadr ee) :value))
  8592. (ek (plist-get (cadr ee) :key))
  8593. (eok (and (stringp ek) (string-match "name" ek))))
  8594. (setq txt (cond
  8595. ((org-at-heading-p) nil)
  8596. ((and (eq et 'keyword) eok) ev)
  8597. ((org-region-active-p)
  8598. (buffer-substring (region-beginning) (region-end)))))
  8599. (when (or (null txt) (string-match "\\S-" txt))
  8600. (setq cpltxt
  8601. (concat cpltxt "::"
  8602. (condition-case nil
  8603. (org-make-org-heading-search-string txt)
  8604. (error "")))
  8605. desc (or (and (eq et 'keyword) eok ev)
  8606. (nth 4 (ignore-errors (org-heading-components)))
  8607. "NONE")))))
  8608. (if (string-match "::\\'" cpltxt)
  8609. (setq cpltxt (substring cpltxt 0 -2)))
  8610. (setq link cpltxt))))
  8611. ((buffer-file-name (buffer-base-buffer))
  8612. ;; Just link to this file here.
  8613. (setq cpltxt (concat "file:"
  8614. (abbreviate-file-name
  8615. (buffer-file-name (buffer-base-buffer)))))
  8616. ;; Add a context string.
  8617. (when (org-xor org-context-in-file-links arg)
  8618. (setq txt (if (org-region-active-p)
  8619. (buffer-substring (region-beginning) (region-end))
  8620. (buffer-substring (point-at-bol) (point-at-eol))))
  8621. ;; Only use search option if there is some text.
  8622. (when (string-match "\\S-" txt)
  8623. (setq cpltxt
  8624. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8625. desc "NONE")))
  8626. (setq link cpltxt))
  8627. ((org-called-interactively-p 'interactive)
  8628. (user-error "No method for storing a link from this buffer"))
  8629. (t (setq link nil)))
  8630. ;; We're done setting link and desc, clean up
  8631. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8632. (setq link (or link cpltxt)
  8633. desc (or desc cpltxt))
  8634. (cond ((equal desc "NONE") (setq desc nil))
  8635. ((string-match org-bracket-link-analytic-regexp desc)
  8636. (let ((d0 (match-string 3 desc))
  8637. (p0 (match-string 5 desc)))
  8638. (setq desc
  8639. (replace-regexp-in-string
  8640. org-bracket-link-regexp
  8641. (concat (or p0 d0)
  8642. (if (equal (length (match-string 0 desc))
  8643. (length desc)) "*" "")) desc)))))
  8644. ;; Return the link
  8645. (if (not (and (or (org-called-interactively-p 'any)
  8646. executing-kbd-macro) link))
  8647. (or agenda-link (and link (org-make-link-string link desc)))
  8648. (push (list link desc) org-stored-links)
  8649. (message "Stored: %s" (or desc link))
  8650. (when custom-id
  8651. (setq link (concat "file:" (abbreviate-file-name
  8652. (buffer-file-name)) "::#" custom-id))
  8653. (push (list link desc) org-stored-links)))))))
  8654. (defun org-store-link-props (&rest plist)
  8655. "Store link properties, extract names and addresses."
  8656. (let (x adr)
  8657. (when (setq x (plist-get plist :from))
  8658. (setq adr (mail-extract-address-components x))
  8659. (setq plist (plist-put plist :fromname (car adr)))
  8660. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8661. (when (setq x (plist-get plist :to))
  8662. (setq adr (mail-extract-address-components x))
  8663. (setq plist (plist-put plist :toname (car adr)))
  8664. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8665. (let ((from (plist-get plist :from))
  8666. (to (plist-get plist :to)))
  8667. (when (and from to org-from-is-user-regexp)
  8668. (setq plist
  8669. (plist-put plist :fromto
  8670. (if (string-match org-from-is-user-regexp from)
  8671. (concat "to %t")
  8672. (concat "from %f"))))))
  8673. (setq org-store-link-plist plist))
  8674. (defun org-add-link-props (&rest plist)
  8675. "Add these properties to the link property list."
  8676. (let (key value)
  8677. (while plist
  8678. (setq key (pop plist) value (pop plist))
  8679. (setq org-store-link-plist
  8680. (plist-put org-store-link-plist key value)))))
  8681. (defun org-email-link-description (&optional fmt)
  8682. "Return the description part of an email link.
  8683. This takes information from `org-store-link-plist' and formats it
  8684. according to FMT (default from `org-email-link-description-format')."
  8685. (setq fmt (or fmt org-email-link-description-format))
  8686. (let* ((p org-store-link-plist)
  8687. (to (plist-get p :toaddress))
  8688. (from (plist-get p :fromaddress))
  8689. (table
  8690. (list
  8691. (cons "%c" (plist-get p :fromto))
  8692. (cons "%F" (plist-get p :from))
  8693. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8694. (cons "%T" (plist-get p :to))
  8695. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8696. (cons "%s" (plist-get p :subject))
  8697. (cons "%d" (plist-get p :date))
  8698. (cons "%m" (plist-get p :message-id)))))
  8699. (when (string-match "%c" fmt)
  8700. ;; Check if the user wrote this message
  8701. (if (and org-from-is-user-regexp from to
  8702. (save-match-data (string-match org-from-is-user-regexp from)))
  8703. (setq fmt (replace-match "to %t" t t fmt))
  8704. (setq fmt (replace-match "from %f" t t fmt))))
  8705. (org-replace-escapes fmt table)))
  8706. (defun org-make-org-heading-search-string (&optional string)
  8707. "Make search string for the current headline or STRING."
  8708. (let ((s (or string
  8709. (and (derived-mode-p 'org-mode)
  8710. (save-excursion
  8711. (org-back-to-heading t)
  8712. (org-element-property :raw-value (org-element-at-point))))))
  8713. (lines org-context-in-file-links))
  8714. (or string (setq s (concat "*" s))) ; Add * for headlines
  8715. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8716. (when (and string (integerp lines) (> lines 0))
  8717. (let ((slines (org-split-string s "\n")))
  8718. (when (< lines (length slines))
  8719. (setq s (mapconcat
  8720. 'identity
  8721. (reverse (nthcdr (- (length slines) lines)
  8722. (reverse slines))) "\n")))))
  8723. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8724. (defun org-make-link-string (link &optional description)
  8725. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8726. (unless (string-match "\\S-" link)
  8727. (error "Empty link"))
  8728. (when (and description
  8729. (stringp description)
  8730. (not (string-match "\\S-" description)))
  8731. (setq description nil))
  8732. (when (stringp description)
  8733. ;; Remove brackets from the description, they are fatal.
  8734. (while (string-match "\\[" description)
  8735. (setq description (replace-match "{" t t description)))
  8736. (while (string-match "\\]" description)
  8737. (setq description (replace-match "}" t t description))))
  8738. (when (equal link description)
  8739. ;; No description needed, it is identical
  8740. (setq description nil))
  8741. (when (and (not description)
  8742. (not (string-match (org-image-file-name-regexp) link))
  8743. (not (equal link (org-link-escape link))))
  8744. (setq description (org-extract-attributes link)))
  8745. (setq link
  8746. (cond ((string-match (org-image-file-name-regexp) link) link)
  8747. ((string-match org-link-types-re link)
  8748. (concat (match-string 1 link)
  8749. (org-link-escape (substring link (match-end 1)))))
  8750. (t (org-link-escape link))))
  8751. (concat "[[" link "]"
  8752. (if description (concat "[" description "]") "")
  8753. "]"))
  8754. (defconst org-link-escape-chars
  8755. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8756. "List of characters that should be escaped in link.
  8757. This is the list that is used for internal purposes.")
  8758. (defconst org-link-escape-chars-browser
  8759. '(?\ )
  8760. "List of escapes for characters that are problematic in links.
  8761. This is the list that is used before handing over to the browser.")
  8762. (defun org-link-escape (text &optional table merge)
  8763. "Return percent escaped representation of TEXT.
  8764. TEXT is a string with the text to escape.
  8765. Optional argument TABLE is a list with characters that should be
  8766. escaped. When nil, `org-link-escape-chars' is used.
  8767. If optional argument MERGE is set, merge TABLE into
  8768. `org-link-escape-chars'."
  8769. (cond
  8770. ((and table merge)
  8771. (mapc (lambda (defchr)
  8772. (unless (member defchr table)
  8773. (setq table (cons defchr table)))) org-link-escape-chars))
  8774. ((null table)
  8775. (setq table org-link-escape-chars)))
  8776. (mapconcat
  8777. (lambda (char)
  8778. (if (or (member char table)
  8779. (and (or (< char 32) (= char 37) (> char 126))
  8780. org-url-hexify-p))
  8781. (mapconcat (lambda (sequence-element)
  8782. (format "%%%.2X" sequence-element))
  8783. (or (encode-coding-char char 'utf-8)
  8784. (error "Unable to percent escape character: %s"
  8785. (char-to-string char))) "")
  8786. (char-to-string char))) text ""))
  8787. (defun org-link-unescape (str)
  8788. "Unhex hexified Unicode strings as returned from the JavaScript function
  8789. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8790. (unless (and (null str) (string= "" str))
  8791. (let ((pos 0) (case-fold-search t) unhexed)
  8792. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8793. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8794. (setq str (replace-match unhexed t t str))
  8795. (setq pos (+ pos (length unhexed))))))
  8796. str)
  8797. (defun org-link-unescape-compound (hex)
  8798. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8799. Note: this function also decodes single byte encodings like
  8800. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8801. (save-match-data
  8802. (let* ((bytes (cdr (split-string hex "%")))
  8803. (ret "")
  8804. (eat 0)
  8805. (sum 0))
  8806. (while bytes
  8807. (let* ((val (string-to-number (pop bytes) 16))
  8808. (shift-xor
  8809. (if (= 0 eat)
  8810. (cond
  8811. ((>= val 252) (cons 6 252))
  8812. ((>= val 248) (cons 5 248))
  8813. ((>= val 240) (cons 4 240))
  8814. ((>= val 224) (cons 3 224))
  8815. ((>= val 192) (cons 2 192))
  8816. (t (cons 0 0)))
  8817. (cons 6 128))))
  8818. (if (>= val 192) (setq eat (car shift-xor)))
  8819. (setq val (logxor val (cdr shift-xor)))
  8820. (setq sum (+ (lsh sum (car shift-xor)) val))
  8821. (if (> eat 0) (setq eat (- eat 1)))
  8822. (cond
  8823. ((= 0 eat) ;multi byte
  8824. (setq ret (concat ret (org-char-to-string sum)))
  8825. (setq sum 0))
  8826. ((not bytes) ; single byte(s)
  8827. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8828. )) ;; end (while bytes
  8829. ret )))
  8830. (defun org-link-unescape-single-byte-sequence (hex)
  8831. "Unhexify hex-encoded single byte character sequences."
  8832. (mapconcat (lambda (byte)
  8833. (char-to-string (string-to-number byte 16)))
  8834. (cdr (split-string hex "%")) ""))
  8835. (defun org-xor (a b)
  8836. "Exclusive or."
  8837. (if a (not b) b))
  8838. (defun org-fixup-message-id-for-http (s)
  8839. "Replace special characters in a message id, so it can be used in an http query."
  8840. (when (string-match "%" s)
  8841. (setq s (mapconcat (lambda (c)
  8842. (if (eq c ?%)
  8843. "%25"
  8844. (char-to-string c)))
  8845. s "")))
  8846. (while (string-match "<" s)
  8847. (setq s (replace-match "%3C" t t s)))
  8848. (while (string-match ">" s)
  8849. (setq s (replace-match "%3E" t t s)))
  8850. (while (string-match "@" s)
  8851. (setq s (replace-match "%40" t t s)))
  8852. s)
  8853. (defun org-link-prettify (link)
  8854. "Return a human-readable representation of LINK.
  8855. The car of LINK must be a raw link the cdr of LINK must be either
  8856. a link description or nil."
  8857. (let ((desc (or (cadr link) "<no description>")))
  8858. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8859. "<" (car link) ">")))
  8860. ;;;###autoload
  8861. (defun org-insert-link-global ()
  8862. "Insert a link like Org-mode does.
  8863. This command can be called in any mode to insert a link in Org-mode syntax."
  8864. (interactive)
  8865. (org-load-modules-maybe)
  8866. (org-run-like-in-org-mode 'org-insert-link))
  8867. (defun org-insert-all-links (&optional keep)
  8868. "Insert all links in `org-stored-links'."
  8869. (interactive "P")
  8870. (let ((links (copy-sequence org-stored-links)) l)
  8871. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8872. (insert "- ")
  8873. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8874. (insert "\n"))))
  8875. (defun org-link-fontify-links-to-this-file ()
  8876. "Fontify links to the current file in `org-stored-links'."
  8877. (let ((f (buffer-file-name)) a b)
  8878. (setq a (mapcar (lambda(l)
  8879. (let ((ll (car l)))
  8880. (when (and (string-match "^file:\\(.+\\)::" ll)
  8881. (equal f (expand-file-name (match-string 1 ll))))
  8882. ll)))
  8883. org-stored-links))
  8884. (when (featurep 'org-id)
  8885. (setq b (mapcar (lambda(l)
  8886. (let ((ll (car l)))
  8887. (when (and (string-match "^id:\\(.+\\)$" ll)
  8888. (equal f (expand-file-name
  8889. (or (org-id-find-id-file
  8890. (match-string 1 ll)) ""))))
  8891. ll)))
  8892. org-stored-links)))
  8893. (mapcar (lambda(l)
  8894. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8895. (delq nil (append a b)))))
  8896. (defvar org-link-links-in-this-file nil)
  8897. (defun org-insert-link (&optional complete-file link-location default-description)
  8898. "Insert a link. At the prompt, enter the link.
  8899. Completion can be used to insert any of the link protocol prefixes like
  8900. http or ftp in use.
  8901. The history can be used to select a link previously stored with
  8902. `org-store-link'. When the empty string is entered (i.e. if you just
  8903. press RET at the prompt), the link defaults to the most recently
  8904. stored link. As SPC triggers completion in the minibuffer, you need to
  8905. use M-SPC or C-q SPC to force the insertion of a space character.
  8906. You will also be prompted for a description, and if one is given, it will
  8907. be displayed in the buffer instead of the link.
  8908. If there is already a link at point, this command will allow you to edit link
  8909. and description parts.
  8910. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8911. be selected using completion. The path to the file will be relative to the
  8912. current directory if the file is in the current directory or a subdirectory.
  8913. Otherwise, the link will be the absolute path as completed in the minibuffer
  8914. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8915. option `org-link-file-path-type'.
  8916. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8917. the current directory or below.
  8918. With three \\[universal-argument] prefixes, negate the meaning of
  8919. `org-keep-stored-link-after-insertion'.
  8920. If `org-make-link-description-function' is non-nil, this function will be
  8921. called with the link target, and the result will be the default
  8922. link description.
  8923. If the LINK-LOCATION parameter is non-nil, this value will be
  8924. used as the link location instead of reading one interactively.
  8925. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8926. be used as the default description."
  8927. (interactive "P")
  8928. (let* ((wcf (current-window-configuration))
  8929. (origbuf (current-buffer))
  8930. (region (if (org-region-active-p)
  8931. (buffer-substring (region-beginning) (region-end))))
  8932. (remove (and region (list (region-beginning) (region-end))))
  8933. (desc region)
  8934. tmphist ; byte-compile incorrectly complains about this
  8935. (link link-location)
  8936. (abbrevs org-link-abbrev-alist-local)
  8937. entry file all-prefixes auto-desc)
  8938. (cond
  8939. (link-location) ; specified by arg, just use it.
  8940. ((org-in-regexp org-bracket-link-regexp 1)
  8941. ;; We do have a link at point, and we are going to edit it.
  8942. (setq remove (list (match-beginning 0) (match-end 0)))
  8943. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8944. (setq link (read-string "Link: "
  8945. (org-link-unescape
  8946. (org-match-string-no-properties 1)))))
  8947. ((or (org-in-regexp org-angle-link-re)
  8948. (org-in-regexp org-plain-link-re))
  8949. ;; Convert to bracket link
  8950. (setq remove (list (match-beginning 0) (match-end 0))
  8951. link (read-string "Link: "
  8952. (org-remove-angle-brackets (match-string 0)))))
  8953. ((member complete-file '((4) (16)))
  8954. ;; Completing read for file names.
  8955. (setq link (org-file-complete-link complete-file)))
  8956. (t
  8957. ;; Read link, with completion for stored links.
  8958. (org-link-fontify-links-to-this-file)
  8959. (org-switch-to-buffer-other-window "*Org Links*")
  8960. (with-current-buffer "*Org Links*"
  8961. (erase-buffer)
  8962. (insert "Insert a link.
  8963. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8964. (when org-stored-links
  8965. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8966. (insert (mapconcat 'org-link-prettify
  8967. (reverse org-stored-links) "\n")))
  8968. (goto-char (point-min)))
  8969. (let ((cw (selected-window)))
  8970. (select-window (get-buffer-window "*Org Links*" 'visible))
  8971. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8972. (unless (pos-visible-in-window-p (point-max))
  8973. (org-fit-window-to-buffer))
  8974. (and (window-live-p cw) (select-window cw)))
  8975. ;; Fake a link history, containing the stored links.
  8976. (setq tmphist (append (mapcar 'car org-stored-links)
  8977. org-insert-link-history))
  8978. (setq all-prefixes (append (mapcar 'car abbrevs)
  8979. (mapcar 'car org-link-abbrev-alist)
  8980. org-link-types))
  8981. (unwind-protect
  8982. (progn
  8983. (setq link
  8984. (org-completing-read
  8985. "Link: "
  8986. (append
  8987. (mapcar (lambda (x) (concat x ":"))
  8988. all-prefixes)
  8989. (mapcar 'car org-stored-links))
  8990. nil nil nil
  8991. 'tmphist
  8992. (caar org-stored-links)))
  8993. (if (not (string-match "\\S-" link))
  8994. (user-error "No link selected"))
  8995. (mapc (lambda(l)
  8996. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8997. org-stored-links)
  8998. (if (or (member link all-prefixes)
  8999. (and (equal ":" (substring link -1))
  9000. (member (substring link 0 -1) all-prefixes)
  9001. (setq link (substring link 0 -1))))
  9002. (setq link (with-current-buffer origbuf
  9003. (org-link-try-special-completion link)))))
  9004. (set-window-configuration wcf)
  9005. (kill-buffer "*Org Links*"))
  9006. (setq entry (assoc link org-stored-links))
  9007. (or entry (push link org-insert-link-history))
  9008. (setq desc (or desc (nth 1 entry)))))
  9009. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9010. (not org-keep-stored-link-after-insertion))
  9011. (setq org-stored-links (delq (assoc link org-stored-links)
  9012. org-stored-links)))
  9013. (if (string-match org-plain-link-re link)
  9014. ;; URL-like link, normalize the use of angular brackets.
  9015. (setq link (org-remove-angle-brackets link)))
  9016. ;; Check if we are linking to the current file with a search
  9017. ;; option If yes, simplify the link by using only the search
  9018. ;; option.
  9019. (when (and buffer-file-name
  9020. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9021. (let* ((path (match-string 1 link))
  9022. (case-fold-search nil)
  9023. (search (match-string 2 link)))
  9024. (save-match-data
  9025. (if (equal (file-truename buffer-file-name) (file-truename path))
  9026. ;; We are linking to this same file, with a search option
  9027. (setq link search)))))
  9028. ;; Check if we can/should use a relative path. If yes, simplify the link
  9029. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9030. (let* ((type (match-string 1 link))
  9031. (path (match-string 2 link))
  9032. (origpath path)
  9033. (case-fold-search nil))
  9034. (cond
  9035. ((or (eq org-link-file-path-type 'absolute)
  9036. (equal complete-file '(16)))
  9037. (setq path (abbreviate-file-name (expand-file-name path))))
  9038. ((eq org-link-file-path-type 'noabbrev)
  9039. (setq path (expand-file-name path)))
  9040. ((eq org-link-file-path-type 'relative)
  9041. (setq path (file-relative-name path)))
  9042. (t
  9043. (save-match-data
  9044. (if (string-match (concat "^" (regexp-quote
  9045. (expand-file-name
  9046. (file-name-as-directory
  9047. default-directory))))
  9048. (expand-file-name path))
  9049. ;; We are linking a file with relative path name.
  9050. (setq path (substring (expand-file-name path)
  9051. (match-end 0)))
  9052. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9053. (setq link (concat type path))
  9054. (if (equal desc origpath)
  9055. (setq desc path))))
  9056. (if org-make-link-description-function
  9057. (setq desc
  9058. (or (condition-case nil
  9059. (funcall org-make-link-description-function link desc)
  9060. (error (progn (message "Can't get link description from `%s'"
  9061. (symbol-name org-make-link-description-function))
  9062. (sit-for 2) nil)))
  9063. (read-string "Description: " default-description)))
  9064. (if default-description (setq desc default-description)
  9065. (setq desc (or (and auto-desc desc)
  9066. (read-string "Description: " desc)))))
  9067. (unless (string-match "\\S-" desc) (setq desc nil))
  9068. (if remove (apply 'delete-region remove))
  9069. (insert (org-make-link-string link desc))))
  9070. (defun org-link-try-special-completion (type)
  9071. "If there is completion support for link type TYPE, offer it."
  9072. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9073. (if (functionp fun)
  9074. (funcall fun)
  9075. (read-string "Link (no completion support): " (concat type ":")))))
  9076. (defun org-file-complete-link (&optional arg)
  9077. "Create a file link using completion."
  9078. (let (file link)
  9079. (setq file (org-iread-file-name "File: "))
  9080. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9081. (pwd1 (file-name-as-directory (abbreviate-file-name
  9082. (expand-file-name ".")))))
  9083. (cond
  9084. ((equal arg '(16))
  9085. (setq link (concat
  9086. "file:"
  9087. (abbreviate-file-name (expand-file-name file)))))
  9088. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9089. (setq link (concat "file:" (match-string 1 file))))
  9090. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9091. (expand-file-name file))
  9092. (setq link (concat
  9093. "file:" (match-string 1 (expand-file-name file)))))
  9094. (t (setq link (concat "file:" file)))))
  9095. link))
  9096. (defun org-iread-file-name (&rest args)
  9097. "Read-file-name using `ido-mode' speedup if available.
  9098. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9099. See `read-file-name' for a description of parameters."
  9100. (org-without-partial-completion
  9101. (if (and org-completion-use-ido
  9102. (fboundp 'ido-read-file-name)
  9103. (boundp 'ido-mode) ido-mode
  9104. (listp (second args)))
  9105. (let ((ido-enter-matching-directory nil))
  9106. (apply 'ido-read-file-name args))
  9107. (apply 'read-file-name args))))
  9108. (defun org-completing-read (&rest args)
  9109. "Completing-read with SPACE being a normal character."
  9110. (let ((enable-recursive-minibuffers t)
  9111. (minibuffer-local-completion-map
  9112. (copy-keymap minibuffer-local-completion-map)))
  9113. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9114. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9115. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9116. (apply 'org-icompleting-read args)))
  9117. (defun org-completing-read-no-i (&rest args)
  9118. (let (org-completion-use-ido org-completion-use-iswitchb)
  9119. (apply 'org-completing-read args)))
  9120. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9121. "Use iswitch as a completing-read replacement to choose from choices.
  9122. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9123. from."
  9124. (let* ((iswitchb-use-virtual-buffers nil)
  9125. (iswitchb-make-buflist-hook
  9126. (lambda ()
  9127. (setq iswitchb-temp-buflist choices))))
  9128. (iswitchb-read-buffer prompt)))
  9129. (defun org-icompleting-read (&rest args)
  9130. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9131. (org-without-partial-completion
  9132. (if (and org-completion-use-ido
  9133. (fboundp 'ido-completing-read)
  9134. (boundp 'ido-mode) ido-mode
  9135. (listp (second args)))
  9136. (let ((ido-enter-matching-directory nil))
  9137. (apply 'ido-completing-read (concat (car args))
  9138. (if (consp (car (nth 1 args)))
  9139. (mapcar 'car (nth 1 args))
  9140. (nth 1 args))
  9141. (cddr args)))
  9142. (if (and org-completion-use-iswitchb
  9143. (boundp 'iswitchb-mode) iswitchb-mode
  9144. (listp (second args)))
  9145. (apply 'org-iswitchb-completing-read (concat (car args))
  9146. (if (consp (car (nth 1 args)))
  9147. (mapcar 'car (nth 1 args))
  9148. (nth 1 args))
  9149. (cddr args))
  9150. (apply 'completing-read args)))))
  9151. (defun org-extract-attributes (s)
  9152. "Extract the attributes cookie from a string and set as text property."
  9153. (let (a attr (start 0) key value)
  9154. (save-match-data
  9155. (when (string-match "{{\\([^}]+\\)}}$" s)
  9156. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9157. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9158. (setq key (match-string 1 a) value (match-string 2 a)
  9159. start (match-end 0)
  9160. attr (plist-put attr (intern key) value))))
  9161. (org-add-props s nil 'org-attr attr))
  9162. s))
  9163. ;;; Opening/following a link
  9164. (defvar org-link-search-failed nil)
  9165. (defvar org-open-link-functions nil
  9166. "Hook for functions finding a plain text link.
  9167. These functions must take a single argument, the link content.
  9168. They will be called for links that look like [[link text][description]]
  9169. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9170. like a filename (e.g. \"./blue.png\").
  9171. These functions will be called *before* Org attempts to resolve the
  9172. link by doing text searches in the current buffer - so if you want a
  9173. link \"[[target]]\" to still find \"<<target>>\", your function should
  9174. handle this as a special case.
  9175. When the function does handle the link, it must return a non-nil value.
  9176. If it decides that it is not responsible for this link, it must return
  9177. nil to indicate that that Org-mode can continue with other options
  9178. like exact and fuzzy text search.")
  9179. (defun org-next-link (&optional search-backward)
  9180. "Move forward to the next link.
  9181. If the link is in hidden text, expose it."
  9182. (interactive "P")
  9183. (when (and org-link-search-failed (eq this-command last-command))
  9184. (goto-char (point-min))
  9185. (message "Link search wrapped back to beginning of buffer"))
  9186. (setq org-link-search-failed nil)
  9187. (let* ((pos (point))
  9188. (ct (org-context))
  9189. (a (assoc :link ct))
  9190. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9191. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9192. ((looking-at org-any-link-re)
  9193. ;; Don't stay stuck at link without an org-link face
  9194. (forward-char (if search-backward -1 1))))
  9195. (if (funcall srch-fun org-any-link-re nil t)
  9196. (progn
  9197. (goto-char (match-beginning 0))
  9198. (if (outline-invisible-p) (org-show-context)))
  9199. (goto-char pos)
  9200. (setq org-link-search-failed t)
  9201. (message "No further link found"))))
  9202. (defun org-previous-link ()
  9203. "Move backward to the previous link.
  9204. If the link is in hidden text, expose it."
  9205. (interactive)
  9206. (funcall 'org-next-link t))
  9207. (defun org-translate-link (s)
  9208. "Translate a link string if a translation function has been defined."
  9209. (if (and org-link-translation-function
  9210. (fboundp org-link-translation-function)
  9211. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9212. (progn
  9213. (setq s (funcall org-link-translation-function
  9214. (match-string 1 s) (match-string 2 s)))
  9215. (concat (car s) ":" (cdr s)))
  9216. s))
  9217. (defun org-translate-link-from-planner (type path)
  9218. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9219. This is still an experimental function, your mileage may vary."
  9220. (cond
  9221. ((member type '("http" "https" "news" "ftp"))
  9222. ;; standard Internet links are the same.
  9223. nil)
  9224. ((and (equal type "irc") (string-match "^//" path))
  9225. ;; Planner has two / at the beginning of an irc link, we have 1.
  9226. ;; We should have zero, actually....
  9227. (setq path (substring path 1)))
  9228. ((and (equal type "lisp") (string-match "^/" path))
  9229. ;; Planner has a slash, we do not.
  9230. (setq type "elisp" path (substring path 1)))
  9231. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9232. ;; A typical message link. Planner has the id after the final slash,
  9233. ;; we separate it with a hash mark
  9234. (setq path (concat (match-string 1 path) "#"
  9235. (org-remove-angle-brackets (match-string 2 path))))))
  9236. (cons type path))
  9237. (defun org-find-file-at-mouse (ev)
  9238. "Open file link or URL at mouse."
  9239. (interactive "e")
  9240. (mouse-set-point ev)
  9241. (org-open-at-point 'in-emacs))
  9242. (defun org-open-at-mouse (ev)
  9243. "Open file link or URL at mouse.
  9244. See the docstring of `org-open-file' for details."
  9245. (interactive "e")
  9246. (mouse-set-point ev)
  9247. (if (eq major-mode 'org-agenda-mode)
  9248. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9249. (org-open-at-point))
  9250. (defvar org-window-config-before-follow-link nil
  9251. "The window configuration before following a link.
  9252. This is saved in case the need arises to restore it.")
  9253. (defvar org-open-link-marker (make-marker)
  9254. "Marker pointing to the location where `org-open-at-point; was called.")
  9255. ;;;###autoload
  9256. (defun org-open-at-point-global ()
  9257. "Follow a link like Org-mode does.
  9258. This command can be called in any mode to follow a link that has
  9259. Org-mode syntax."
  9260. (interactive)
  9261. (org-run-like-in-org-mode 'org-open-at-point))
  9262. ;;;###autoload
  9263. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9264. "Open a link in the string S, as if it was in Org-mode."
  9265. (interactive "sLink: \nP")
  9266. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9267. (with-temp-buffer
  9268. (let ((org-inhibit-startup (not reference-buffer)))
  9269. (org-mode)
  9270. (insert s)
  9271. (goto-char (point-min))
  9272. (when reference-buffer
  9273. (setq org-link-abbrev-alist-local
  9274. (with-current-buffer reference-buffer
  9275. org-link-abbrev-alist-local)))
  9276. (org-open-at-point arg reference-buffer)))))
  9277. (defvar org-open-at-point-functions nil
  9278. "Hook that is run when following a link at point.
  9279. Functions in this hook must return t if they identify and follow
  9280. a link at point. If they don't find anything interesting at point,
  9281. they must return nil.")
  9282. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9283. (defun org-open-at-point (&optional arg reference-buffer)
  9284. "Open link at or after point.
  9285. If there is no link at point, this function will search forward up to
  9286. the end of the current line.
  9287. Normally, files will be opened by an appropriate application. If the
  9288. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9289. With a double prefix argument, try to open outside of Emacs, in the
  9290. application the system uses for this file type."
  9291. (interactive "P")
  9292. ;; if in a code block, then open the block's results
  9293. (unless (call-interactively #'org-babel-open-src-block-result)
  9294. (org-load-modules-maybe)
  9295. (move-marker org-open-link-marker (point))
  9296. (setq org-window-config-before-follow-link (current-window-configuration))
  9297. (org-remove-occur-highlights nil nil t)
  9298. (cond
  9299. ((and (org-at-heading-p)
  9300. (not (org-at-timestamp-p t))
  9301. (not (org-in-regexp
  9302. (concat org-plain-link-re "\\|"
  9303. org-bracket-link-regexp "\\|"
  9304. org-angle-link-re "\\|"
  9305. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9306. (not (get-text-property (point) 'org-linked-text)))
  9307. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9308. (lk0 (car lkall))
  9309. (lk (if (stringp lk0) (list lk0) lk0))
  9310. (lkend (cdr lkall)))
  9311. (mapcar (lambda(l)
  9312. (search-forward l nil lkend)
  9313. (goto-char (match-beginning 0))
  9314. (org-open-at-point))
  9315. lk))
  9316. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9317. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9318. ((and (org-at-timestamp-p t)
  9319. (not (org-in-regexp org-bracket-link-regexp)))
  9320. (org-follow-timestamp-link))
  9321. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9322. (not (org-in-regexp org-any-link-re)))
  9323. (org-footnote-action))
  9324. (t
  9325. (let (type path link line search (pos (point)))
  9326. (catch 'match
  9327. (save-excursion
  9328. (skip-chars-forward "^]\n\r")
  9329. (when (org-in-regexp org-bracket-link-regexp 1)
  9330. (setq link (org-extract-attributes
  9331. (org-link-unescape (org-match-string-no-properties 1))))
  9332. (while (string-match " *\n *" link)
  9333. (setq link (replace-match " " t t link)))
  9334. (setq link (org-link-expand-abbrev link))
  9335. (cond
  9336. ((or (file-name-absolute-p link)
  9337. (string-match "^\\.\\.?/" link))
  9338. (setq type "file" path link))
  9339. ((string-match org-link-re-with-space3 link)
  9340. (setq type (match-string 1 link) path (match-string 2 link)))
  9341. ((string-match "^help:+\\(.+\\)" link)
  9342. (setq type "help" path (match-string 1 link)))
  9343. (t (setq type "thisfile" path link)))
  9344. (throw 'match t)))
  9345. (when (get-text-property (point) 'org-linked-text)
  9346. (setq type "thisfile"
  9347. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9348. (1+ (point)) (point))
  9349. path (buffer-substring
  9350. (or (previous-single-property-change pos 'org-linked-text)
  9351. (point-min))
  9352. (or (next-single-property-change pos 'org-linked-text)
  9353. (point-max)))
  9354. ;; Ensure we will search for a <<<radio>>> link, not
  9355. ;; a simple reference like <<ref>>
  9356. path (concat "<" path))
  9357. (throw 'match t))
  9358. (save-excursion
  9359. (when (or (org-in-regexp org-angle-link-re)
  9360. (let ((match (org-in-regexp org-plain-link-re)))
  9361. ;; Check a plain link is not within a bracket link
  9362. (and match
  9363. (save-excursion
  9364. (progn
  9365. (goto-char (car match))
  9366. (not (org-in-regexp org-bracket-link-regexp))))))
  9367. (let ((line_ending (save-excursion (end-of-line) (point))))
  9368. ;; We are in a line before a plain or bracket link
  9369. (or (re-search-forward org-plain-link-re line_ending t)
  9370. (re-search-forward org-bracket-link-regexp line_ending t))))
  9371. (setq type (match-string 1)
  9372. path (org-link-unescape (match-string 2)))
  9373. (throw 'match t)))
  9374. (save-excursion
  9375. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9376. (setq type "tags"
  9377. path (match-string 1))
  9378. (while (string-match ":" path)
  9379. (setq path (replace-match "+" t t path)))
  9380. (throw 'match t)))
  9381. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9382. (setq type "tree-match"
  9383. path (match-string 1))
  9384. (throw 'match t)))
  9385. (unless path
  9386. (user-error "No link found"))
  9387. ;; switch back to reference buffer
  9388. ;; needed when if called in a temporary buffer through
  9389. ;; org-open-link-from-string
  9390. (with-current-buffer (or reference-buffer (current-buffer))
  9391. ;; Remove any trailing spaces in path
  9392. (if (string-match " +\\'" path)
  9393. (setq path (replace-match "" t t path)))
  9394. (if (and org-link-translation-function
  9395. (fboundp org-link-translation-function))
  9396. ;; Check if we need to translate the link
  9397. (let ((tmp (funcall org-link-translation-function type path)))
  9398. (setq type (car tmp) path (cdr tmp))))
  9399. (cond
  9400. ((assoc type org-link-protocols)
  9401. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9402. ((equal type "help")
  9403. (let ((f-or-v (intern path)))
  9404. (cond ((fboundp f-or-v)
  9405. (describe-function f-or-v))
  9406. ((boundp f-or-v)
  9407. (describe-variable f-or-v))
  9408. (t (error "Not a known function or variable")))))
  9409. ((equal type "mailto")
  9410. (let ((cmd (car org-link-mailto-program))
  9411. (args (cdr org-link-mailto-program)) args1
  9412. (address path) (subject "") a)
  9413. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9414. (setq address (match-string 1 path)
  9415. subject (org-link-escape (match-string 2 path))))
  9416. (while args
  9417. (cond
  9418. ((not (stringp (car args))) (push (pop args) args1))
  9419. (t (setq a (pop args))
  9420. (if (string-match "%a" a)
  9421. (setq a (replace-match address t t a)))
  9422. (if (string-match "%s" a)
  9423. (setq a (replace-match subject t t a)))
  9424. (push a args1))))
  9425. (apply cmd (nreverse args1))))
  9426. ((member type '("http" "https" "ftp" "news"))
  9427. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9428. (org-link-escape
  9429. path org-link-escape-chars-browser)
  9430. path))))
  9431. ((string= type "doi")
  9432. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9433. (org-link-escape
  9434. path org-link-escape-chars-browser)
  9435. path))))
  9436. ((member type '("message"))
  9437. (browse-url (concat type ":" path)))
  9438. ((string= type "tags")
  9439. (org-tags-view arg path))
  9440. ((string= type "tree-match")
  9441. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9442. ((string= type "file")
  9443. (if (string-match "::\\([0-9]+\\)\\'" path)
  9444. (setq line (string-to-number (match-string 1 path))
  9445. path (substring path 0 (match-beginning 0)))
  9446. (if (string-match "::\\(.+\\)\\'" path)
  9447. (setq search (match-string 1 path)
  9448. path (substring path 0 (match-beginning 0)))))
  9449. (if (string-match "[*?{]" (file-name-nondirectory path))
  9450. (dired path)
  9451. (org-open-file path arg line search)))
  9452. ((string= type "shell")
  9453. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9454. (cmd path))
  9455. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9456. (string-match org-confirm-shell-link-not-regexp cmd))
  9457. (not org-confirm-shell-link-function)
  9458. (funcall org-confirm-shell-link-function
  9459. (format "Execute \"%s\" in shell? "
  9460. (org-add-props cmd nil
  9461. 'face 'org-warning))))
  9462. (progn
  9463. (message "Executing %s" cmd)
  9464. (shell-command cmd buf)
  9465. (if (featurep 'midnight)
  9466. (setq clean-buffer-list-kill-buffer-names
  9467. (cons buf clean-buffer-list-kill-buffer-names))))
  9468. (error "Abort"))))
  9469. ((string= type "elisp")
  9470. (let ((cmd path))
  9471. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9472. (string-match org-confirm-elisp-link-not-regexp cmd))
  9473. (not org-confirm-elisp-link-function)
  9474. (funcall org-confirm-elisp-link-function
  9475. (format "Execute \"%s\" as elisp? "
  9476. (org-add-props cmd nil
  9477. 'face 'org-warning))))
  9478. (message "%s => %s" cmd
  9479. (if (equal (string-to-char cmd) ?\()
  9480. (eval (read cmd))
  9481. (call-interactively (read cmd))))
  9482. (error "Abort"))))
  9483. ((and (string= type "thisfile")
  9484. (run-hook-with-args-until-success
  9485. 'org-open-link-functions path)))
  9486. ((string= type "thisfile")
  9487. (if arg
  9488. (switch-to-buffer-other-window
  9489. (org-get-buffer-for-internal-link (current-buffer)))
  9490. (org-mark-ring-push))
  9491. (let ((cmd `(org-link-search
  9492. ,path
  9493. ,(cond ((equal arg '(4)) ''occur)
  9494. ((equal arg '(16)) ''org-occur))
  9495. ,pos)))
  9496. (condition-case nil (let ((org-link-search-inhibit-query t))
  9497. (eval cmd))
  9498. (error (progn (widen) (eval cmd))))))
  9499. (t (browse-url-at-point)))))))
  9500. (move-marker org-open-link-marker nil)
  9501. (run-hook-with-args 'org-follow-link-hook)))
  9502. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9503. "Offer links in the current entry and return the selected link.
  9504. If there is only one link, return it.
  9505. If NTH is an integer, return the NTH link found.
  9506. If ZERO is a string, check also this string for a link, and if
  9507. there is one, return it."
  9508. (with-current-buffer buffer
  9509. (save-excursion
  9510. (save-restriction
  9511. (widen)
  9512. (goto-char marker)
  9513. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9514. "\\(" org-angle-link-re "\\)\\|"
  9515. "\\(" org-plain-link-re "\\)"))
  9516. (cnt ?0)
  9517. (in-emacs (if (integerp nth) nil nth))
  9518. have-zero end links link c)
  9519. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9520. (push (match-string 0 zero) links)
  9521. (setq cnt (1- cnt) have-zero t))
  9522. (save-excursion
  9523. (org-back-to-heading t)
  9524. (setq end (save-excursion (outline-next-heading) (point)))
  9525. (while (re-search-forward re end t)
  9526. (push (match-string 0) links))
  9527. (setq links (org-uniquify (reverse links))))
  9528. (cond
  9529. ((null links)
  9530. (message "No links"))
  9531. ((equal (length links) 1)
  9532. (setq link (car links)))
  9533. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9534. (setq link (nth (if have-zero nth (1- nth)) links)))
  9535. (t ; we have to select a link
  9536. (save-excursion
  9537. (save-window-excursion
  9538. (delete-other-windows)
  9539. (with-output-to-temp-buffer "*Select Link*"
  9540. (mapc (lambda (l)
  9541. (if (not (string-match org-bracket-link-regexp l))
  9542. (princ (format "[%c] %s\n" (incf cnt)
  9543. (org-remove-angle-brackets l)))
  9544. (if (match-end 3)
  9545. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9546. (match-string 3 l) (match-string 1 l)))
  9547. (princ (format "[%c] %s\n" (incf cnt)
  9548. (match-string 1 l))))))
  9549. links))
  9550. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9551. (message "Select link to open, RET to open all:")
  9552. (setq c (read-char-exclusive))
  9553. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9554. (when (equal c ?q) (error "Abort"))
  9555. (if (equal c ?\C-m)
  9556. (setq link links)
  9557. (setq nth (- c ?0))
  9558. (if have-zero (setq nth (1+ nth)))
  9559. (unless (and (integerp nth) (>= (length links) nth))
  9560. (user-error "Invalid link selection"))
  9561. (setq link (nth (1- nth) links)))))
  9562. (cons link end))))))
  9563. ;; Add special file links that specify the way of opening
  9564. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9565. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9566. (defun org-open-file-with-system (path)
  9567. "Open file at PATH using the system way of opening it."
  9568. (org-open-file path 'system))
  9569. (defun org-open-file-with-emacs (path)
  9570. "Open file at PATH in Emacs."
  9571. (org-open-file path 'emacs))
  9572. ;;; File search
  9573. (defvar org-create-file-search-functions nil
  9574. "List of functions to construct the right search string for a file link.
  9575. These functions are called in turn with point at the location to
  9576. which the link should point.
  9577. A function in the hook should first test if it would like to
  9578. handle this file type, for example by checking the `major-mode'
  9579. or the file extension. If it decides not to handle this file, it
  9580. should just return nil to give other functions a chance. If it
  9581. does handle the file, it must return the search string to be used
  9582. when following the link. The search string will be part of the
  9583. file link, given after a double colon, and `org-open-at-point'
  9584. will automatically search for it. If special measures must be
  9585. taken to make the search successful, another function should be
  9586. added to the companion hook `org-execute-file-search-functions',
  9587. which see.
  9588. A function in this hook may also use `setq' to set the variable
  9589. `description' to provide a suggestion for the descriptive text to
  9590. be used for this link when it gets inserted into an Org-mode
  9591. buffer with \\[org-insert-link].")
  9592. (defvar org-execute-file-search-functions nil
  9593. "List of functions to execute a file search triggered by a link.
  9594. Functions added to this hook must accept a single argument, the
  9595. search string that was part of the file link, the part after the
  9596. double colon. The function must first check if it would like to
  9597. handle this search, for example by checking the `major-mode' or
  9598. the file extension. If it decides not to handle this search, it
  9599. should just return nil to give other functions a chance. If it
  9600. does handle the search, it must return a non-nil value to keep
  9601. other functions from trying.
  9602. Each function can access the current prefix argument through the
  9603. variable `current-prefix-arg'. Note that a single prefix is used
  9604. to force opening a link in Emacs, so it may be good to only use a
  9605. numeric or double prefix to guide the search function.
  9606. In case this is needed, a function in this hook can also restore
  9607. the window configuration before `org-open-at-point' was called using:
  9608. (set-window-configuration org-window-config-before-follow-link)")
  9609. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9610. (defun org-link-search (s &optional type avoid-pos stealth)
  9611. "Search for a link search option.
  9612. If S is surrounded by forward slashes, it is interpreted as a
  9613. regular expression. In org-mode files, this will create an `org-occur'
  9614. sparse tree. In ordinary files, `occur' will be used to list matches.
  9615. If the current buffer is in `dired-mode', grep will be used to search
  9616. in all files. If AVOID-POS is given, ignore matches near that position.
  9617. When optional argument STEALTH is non-nil, do not modify
  9618. visibility around point, thus ignoring
  9619. `org-show-hierarchy-above', `org-show-following-heading' and
  9620. `org-show-siblings' variables."
  9621. (let ((case-fold-search t)
  9622. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9623. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9624. (append '(("") (" ") ("\t") ("\n"))
  9625. org-emphasis-alist)
  9626. "\\|") "\\)"))
  9627. (pos (point))
  9628. (pre nil) (post nil)
  9629. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9630. (cond
  9631. ;; First check if there are any special search functions
  9632. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9633. ;; Now try the builtin stuff
  9634. ((and (equal (string-to-char s0) ?#)
  9635. (> (length s0) 1)
  9636. (save-excursion
  9637. (goto-char (point-min))
  9638. (and
  9639. (re-search-forward
  9640. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9641. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9642. (setq type 'dedicated
  9643. pos (match-beginning 0))))
  9644. ;; There is an exact target for this
  9645. (goto-char pos)
  9646. (org-back-to-heading t)))
  9647. ((save-excursion
  9648. (goto-char (point-min))
  9649. (and
  9650. (re-search-forward
  9651. (concat "<<" (regexp-quote s0) ">>") nil t)
  9652. (setq type 'dedicated
  9653. pos (match-beginning 0))))
  9654. ;; There is an exact target for this
  9655. (goto-char pos))
  9656. ((save-excursion
  9657. (goto-char (point-min))
  9658. (and
  9659. (re-search-forward
  9660. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9661. (setq type 'dedicated pos (match-beginning 0))))
  9662. ;; Found an element with a matching #+name affiliated keyword.
  9663. (goto-char pos))
  9664. ((and (string-match "^(\\(.*\\))$" s0)
  9665. (save-excursion
  9666. (goto-char (point-min))
  9667. (and
  9668. (re-search-forward
  9669. (concat "[^[]" (regexp-quote
  9670. (format org-coderef-label-format
  9671. (match-string 1 s0))))
  9672. nil t)
  9673. (setq type 'dedicated
  9674. pos (1+ (match-beginning 0))))))
  9675. ;; There is a coderef target for this
  9676. (goto-char pos))
  9677. ((string-match "^/\\(.*\\)/$" s)
  9678. ;; A regular expression
  9679. (cond
  9680. ((derived-mode-p 'org-mode)
  9681. (org-occur (match-string 1 s)))
  9682. (t (org-do-occur (match-string 1 s)))))
  9683. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9684. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9685. (goto-char (point-min))
  9686. (cond
  9687. ((let (case-fold-search)
  9688. (re-search-forward (format org-complex-heading-regexp-format
  9689. (regexp-quote s))
  9690. nil t))
  9691. ;; OK, found a match
  9692. (setq type 'dedicated)
  9693. (goto-char (match-beginning 0)))
  9694. ((and (not org-link-search-inhibit-query)
  9695. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9696. (y-or-n-p "No match - create this as a new heading? "))
  9697. (goto-char (point-max))
  9698. (or (bolp) (newline))
  9699. (insert "* " s "\n")
  9700. (beginning-of-line 0))
  9701. (t
  9702. (goto-char pos)
  9703. (error "No match"))))
  9704. (t
  9705. ;; A normal search string
  9706. (when (equal (string-to-char s) ?*)
  9707. ;; Anchor on headlines, post may include tags.
  9708. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9709. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9710. s (substring s 1)))
  9711. (remove-text-properties
  9712. 0 (length s)
  9713. '(face nil mouse-face nil keymap nil fontified nil) s)
  9714. ;; Make a series of regular expressions to find a match
  9715. (setq words (org-split-string s "[ \n\r\t]+")
  9716. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9717. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9718. "\\)" markers)
  9719. re2a_ (concat "\\(" (mapconcat 'downcase words
  9720. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9721. re2a (concat "[ \t\r\n]" re2a_)
  9722. re4_ (concat "\\(" (mapconcat 'downcase words
  9723. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9724. re4 (concat "[^a-zA-Z_]" re4_)
  9725. re1 (concat pre re2 post)
  9726. re3 (concat pre (if pre re4_ re4) post)
  9727. re5 (concat pre ".*" re4)
  9728. re2 (concat pre re2)
  9729. re2a (concat pre (if pre re2a_ re2a))
  9730. re4 (concat pre (if pre re4_ re4))
  9731. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9732. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9733. re5 "\\)"))
  9734. (cond
  9735. ((eq type 'org-occur) (org-occur reall))
  9736. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9737. (t (goto-char (point-min))
  9738. (setq type 'fuzzy)
  9739. (if (or (and (org-search-not-self 1 re0 nil t)
  9740. (setq type 'dedicated))
  9741. (org-search-not-self 1 re1 nil t)
  9742. (org-search-not-self 1 re2 nil t)
  9743. (org-search-not-self 1 re2a nil t)
  9744. (org-search-not-self 1 re3 nil t)
  9745. (org-search-not-self 1 re4 nil t)
  9746. (org-search-not-self 1 re5 nil t))
  9747. (goto-char (match-beginning 1))
  9748. (goto-char pos)
  9749. (error "No match"))))))
  9750. (and (derived-mode-p 'org-mode)
  9751. (not stealth)
  9752. (org-show-context 'link-search))
  9753. type))
  9754. (defun org-search-not-self (group &rest args)
  9755. "Execute `re-search-forward', but only accept matches that do not
  9756. enclose the position of `org-open-link-marker'."
  9757. (let ((m org-open-link-marker))
  9758. (catch 'exit
  9759. (while (apply 're-search-forward args)
  9760. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9761. (goto-char (match-end group))
  9762. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9763. (> (match-beginning 0) (marker-position m))
  9764. (< (match-end 0) (marker-position m)))
  9765. (save-match-data
  9766. (or (not (org-in-regexp
  9767. org-bracket-link-analytic-regexp 1))
  9768. (not (match-end 4)) ; no description
  9769. (and (<= (match-beginning 4) (point))
  9770. (>= (match-end 4) (point))))))
  9771. (throw 'exit (point))))))))
  9772. (defun org-get-buffer-for-internal-link (buffer)
  9773. "Return a buffer to be used for displaying the link target of internal links."
  9774. (cond
  9775. ((not org-display-internal-link-with-indirect-buffer)
  9776. buffer)
  9777. ((string-match "(Clone)$" (buffer-name buffer))
  9778. (message "Buffer is already a clone, not making another one")
  9779. ;; we also do not modify visibility in this case
  9780. buffer)
  9781. (t ; make a new indirect buffer for displaying the link
  9782. (let* ((bn (buffer-name buffer))
  9783. (ibn (concat bn "(Clone)"))
  9784. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9785. (with-current-buffer ib (org-overview))
  9786. ib))))
  9787. (defun org-do-occur (regexp &optional cleanup)
  9788. "Call the Emacs command `occur'.
  9789. If CLEANUP is non-nil, remove the printout of the regular expression
  9790. in the *Occur* buffer. This is useful if the regex is long and not useful
  9791. to read."
  9792. (occur regexp)
  9793. (when cleanup
  9794. (let ((cwin (selected-window)) win beg end)
  9795. (when (setq win (get-buffer-window "*Occur*"))
  9796. (select-window win))
  9797. (goto-char (point-min))
  9798. (when (re-search-forward "match[a-z]+" nil t)
  9799. (setq beg (match-end 0))
  9800. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9801. (setq end (1- (match-beginning 0)))))
  9802. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9803. (goto-char (point-min))
  9804. (select-window cwin))))
  9805. ;;; The mark ring for links jumps
  9806. (defvar org-mark-ring nil
  9807. "Mark ring for positions before jumps in Org-mode.")
  9808. (defvar org-mark-ring-last-goto nil
  9809. "Last position in the mark ring used to go back.")
  9810. ;; Fill and close the ring
  9811. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9812. (loop for i from 1 to org-mark-ring-length do
  9813. (push (make-marker) org-mark-ring))
  9814. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9815. org-mark-ring)
  9816. (defun org-mark-ring-push (&optional pos buffer)
  9817. "Put the current position or POS into the mark ring and rotate it."
  9818. (interactive)
  9819. (setq pos (or pos (point)))
  9820. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9821. (move-marker (car org-mark-ring)
  9822. (or pos (point))
  9823. (or buffer (current-buffer)))
  9824. (message "%s"
  9825. (substitute-command-keys
  9826. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9827. (defun org-mark-ring-goto (&optional n)
  9828. "Jump to the previous position in the mark ring.
  9829. With prefix arg N, jump back that many stored positions. When
  9830. called several times in succession, walk through the entire ring.
  9831. Org-mode commands jumping to a different position in the current file,
  9832. or to another Org-mode file, automatically push the old position
  9833. onto the ring."
  9834. (interactive "p")
  9835. (let (p m)
  9836. (if (eq last-command this-command)
  9837. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9838. (setq p org-mark-ring))
  9839. (setq org-mark-ring-last-goto p)
  9840. (setq m (car p))
  9841. (org-pop-to-buffer-same-window (marker-buffer m))
  9842. (goto-char m)
  9843. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9844. (defun org-remove-angle-brackets (s)
  9845. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9846. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9847. s)
  9848. (defun org-add-angle-brackets (s)
  9849. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9850. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9851. s)
  9852. (defun org-remove-double-quotes (s)
  9853. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9854. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9855. s)
  9856. ;;; Following specific links
  9857. (defun org-follow-timestamp-link ()
  9858. "Open an agenda view for the time-stamp date/range at point."
  9859. (cond
  9860. ((org-at-date-range-p t)
  9861. (let ((org-agenda-start-on-weekday)
  9862. (t1 (match-string 1))
  9863. (t2 (match-string 2)) tt1 tt2)
  9864. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9865. tt2 (time-to-days (org-time-string-to-time t2)))
  9866. (let ((org-agenda-buffer-tmp-name
  9867. (format "*Org Agenda(a:%s)"
  9868. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9869. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9870. ((org-at-timestamp-p t)
  9871. (let ((org-agenda-buffer-tmp-name
  9872. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9873. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9874. (substring (match-string 1) 0 10)))
  9875. 1)))
  9876. (t (error "This should not happen"))))
  9877. ;;; Following file links
  9878. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9879. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9880. (declare-function mailcap-mime-info
  9881. "mailcap" (string &optional request no-decode))
  9882. (defvar org-wait nil)
  9883. (defun org-open-file (path &optional in-emacs line search)
  9884. "Open the file at PATH.
  9885. First, this expands any special file name abbreviations. Then the
  9886. configuration variable `org-file-apps' is checked if it contains an
  9887. entry for this file type, and if yes, the corresponding command is launched.
  9888. If no application is found, Emacs simply visits the file.
  9889. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9890. With a double \\[universal-argument] \\[universal-argument] \
  9891. prefix arg, Org tries to avoid opening in Emacs
  9892. and to use an external application to visit the file.
  9893. Optional LINE specifies a line to go to, optional SEARCH a string
  9894. to search for. If LINE or SEARCH is given, the file will be
  9895. opened in Emacs, unless an entry from org-file-apps that makes
  9896. use of groups in a regexp matches.
  9897. If you want to change the way frames are used when following a
  9898. link, please customize `org-link-frame-setup'.
  9899. If the file does not exist, an error is thrown."
  9900. (let* ((file (if (equal path "")
  9901. buffer-file-name
  9902. (substitute-in-file-name (expand-file-name path))))
  9903. (file-apps (append org-file-apps (org-default-apps)))
  9904. (apps (org-remove-if
  9905. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9906. (apps-dlink (org-remove-if-not
  9907. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9908. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9909. (dirp (if remp nil (file-directory-p file)))
  9910. (file (if (and dirp org-open-directory-means-index-dot-org)
  9911. (concat (file-name-as-directory file) "index.org")
  9912. file))
  9913. (a-m-a-p (assq 'auto-mode apps))
  9914. (dfile (downcase file))
  9915. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9916. (link (cond ((and (eq line nil)
  9917. (eq search nil))
  9918. file)
  9919. (line
  9920. (concat file "::" (number-to-string line)))
  9921. (search
  9922. (concat file "::" search))))
  9923. (dlink (downcase link))
  9924. (old-buffer (current-buffer))
  9925. (old-pos (point))
  9926. (old-mode major-mode)
  9927. ext cmd link-match-data)
  9928. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9929. (setq ext (match-string 1 dfile))
  9930. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9931. (setq ext (match-string 1 dfile))))
  9932. (cond
  9933. ((member in-emacs '((16) system))
  9934. (setq cmd (cdr (assoc 'system apps))))
  9935. (in-emacs (setq cmd 'emacs))
  9936. (t
  9937. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9938. (and dirp (cdr (assoc 'directory apps)))
  9939. ; first, try matching against apps-dlink
  9940. ; if we get a match here, store the match data for later
  9941. (let ((match (assoc-default dlink apps-dlink
  9942. 'string-match)))
  9943. (if match
  9944. (progn (setq link-match-data (match-data))
  9945. match)
  9946. (progn (setq in-emacs (or in-emacs line search))
  9947. nil))) ; if we have no match in apps-dlink,
  9948. ; always open the file in emacs if line or search
  9949. ; is given (for backwards compatibility)
  9950. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9951. 'string-match)
  9952. (cdr (assoc ext apps))
  9953. (cdr (assoc t apps))))))
  9954. (when (eq cmd 'system)
  9955. (setq cmd (cdr (assoc 'system apps))))
  9956. (when (eq cmd 'default)
  9957. (setq cmd (cdr (assoc t apps))))
  9958. (when (eq cmd 'mailcap)
  9959. (require 'mailcap)
  9960. (mailcap-parse-mailcaps)
  9961. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9962. (command (mailcap-mime-info mime-type)))
  9963. (if (stringp command)
  9964. (setq cmd command)
  9965. (setq cmd 'emacs))))
  9966. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9967. (not (file-exists-p file))
  9968. (not org-open-non-existing-files))
  9969. (user-error "No such file: %s" file))
  9970. (cond
  9971. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9972. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9973. (while (string-match "['\"]%s['\"]" cmd)
  9974. (setq cmd (replace-match "%s" t t cmd)))
  9975. (while (string-match "%s" cmd)
  9976. (setq cmd (replace-match
  9977. (save-match-data
  9978. (shell-quote-argument
  9979. (convert-standard-filename file)))
  9980. t t cmd)))
  9981. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9982. (save-match-data
  9983. (let ((match-index 1)
  9984. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9985. (set-match-data link-match-data)
  9986. (while (<= match-index number-of-groups)
  9987. (let ((regex (concat "%" (number-to-string match-index)))
  9988. (replace-with (match-string match-index dlink)))
  9989. (while (string-match regex cmd)
  9990. (setq cmd (replace-match replace-with t t cmd))))
  9991. (setq match-index (+ match-index 1)))))
  9992. (save-window-excursion
  9993. (message "Running %s...done" cmd)
  9994. (start-process-shell-command cmd nil cmd)
  9995. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9996. ((or (stringp cmd)
  9997. (eq cmd 'emacs))
  9998. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9999. (widen)
  10000. (if line (org-goto-line line)
  10001. (if search (org-link-search search))))
  10002. ((consp cmd)
  10003. (let ((file (convert-standard-filename file)))
  10004. (save-match-data
  10005. (set-match-data link-match-data)
  10006. (eval cmd))))
  10007. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10008. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10009. (or (not (equal old-buffer (current-buffer)))
  10010. (not (equal old-pos (point))))
  10011. (org-mark-ring-push old-pos old-buffer))))
  10012. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10013. "This function returns non-nil if `entry' uses a regular
  10014. expression which should be matched against the whole link by
  10015. org-open-file.
  10016. It assumes that is the case when the entry uses a regular
  10017. expression which has at least one grouping construct and the
  10018. action is either a lisp form or a command string containing
  10019. '%1', i.e. using at least one subexpression match as a
  10020. parameter."
  10021. (let ((selector (car entry))
  10022. (action (cdr entry)))
  10023. (if (stringp selector)
  10024. (and (> (regexp-opt-depth selector) 0)
  10025. (or (and (stringp action)
  10026. (string-match "%[0-9]" action))
  10027. (consp action)))
  10028. nil)))
  10029. (defun org-default-apps ()
  10030. "Return the default applications for this operating system."
  10031. (cond
  10032. ((eq system-type 'darwin)
  10033. org-file-apps-defaults-macosx)
  10034. ((eq system-type 'windows-nt)
  10035. org-file-apps-defaults-windowsnt)
  10036. (t org-file-apps-defaults-gnu)))
  10037. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10038. "Convert extensions to regular expressions in the cars of LIST.
  10039. Also, weed out any non-string entries, because the return value is used
  10040. only for regexp matching.
  10041. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10042. point to the symbol `emacs', indicating that the file should
  10043. be opened in Emacs."
  10044. (append
  10045. (delq nil
  10046. (mapcar (lambda (x)
  10047. (if (not (stringp (car x)))
  10048. nil
  10049. (if (string-match "\\W" (car x))
  10050. x
  10051. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10052. list))
  10053. (if add-auto-mode
  10054. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10055. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10056. (defun org-file-remote-p (file)
  10057. "Test whether FILE specifies a location on a remote system.
  10058. Return non-nil if the location is indeed remote.
  10059. For example, the filename \"/user@host:/foo\" specifies a location
  10060. on the system \"/user@host:\"."
  10061. (cond ((fboundp 'file-remote-p)
  10062. (file-remote-p file))
  10063. ((fboundp 'tramp-handle-file-remote-p)
  10064. (tramp-handle-file-remote-p file))
  10065. ((and (boundp 'ange-ftp-name-format)
  10066. (string-match (car ange-ftp-name-format) file))
  10067. t)))
  10068. ;;;; Refiling
  10069. (defun org-get-org-file ()
  10070. "Read a filename, with default directory `org-directory'."
  10071. (let ((default (or org-default-notes-file remember-data-file)))
  10072. (read-file-name (format "File name [%s]: " default)
  10073. (file-name-as-directory org-directory)
  10074. default)))
  10075. (defun org-notes-order-reversed-p ()
  10076. "Check if the current file should receive notes in reversed order."
  10077. (cond
  10078. ((not org-reverse-note-order) nil)
  10079. ((eq t org-reverse-note-order) t)
  10080. ((not (listp org-reverse-note-order)) nil)
  10081. (t (catch 'exit
  10082. (let ((all org-reverse-note-order)
  10083. entry)
  10084. (while (setq entry (pop all))
  10085. (if (string-match (car entry) buffer-file-name)
  10086. (throw 'exit (cdr entry))))
  10087. nil)))))
  10088. (defvar org-refile-target-table nil
  10089. "The list of refile targets, created by `org-refile'.")
  10090. (defvar org-agenda-new-buffers nil
  10091. "Buffers created to visit agenda files.")
  10092. (defvar org-refile-cache nil
  10093. "Cache for refile targets.")
  10094. (defvar org-refile-markers nil
  10095. "All the markers used for caching refile locations.")
  10096. (defun org-refile-marker (pos)
  10097. "Get a new refile marker, but only if caching is in use."
  10098. (if (not org-refile-use-cache)
  10099. pos
  10100. (let ((m (make-marker)))
  10101. (move-marker m pos)
  10102. (push m org-refile-markers)
  10103. m)))
  10104. (defun org-refile-cache-clear ()
  10105. "Clear the refile cache and disable all the markers."
  10106. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10107. (setq org-refile-markers nil)
  10108. (setq org-refile-cache nil)
  10109. (message "Refile cache has been cleared"))
  10110. (defun org-refile-cache-check-set (set)
  10111. "Check if all the markers in the cache still have live buffers."
  10112. (let (marker)
  10113. (catch 'exit
  10114. (while (and set (setq marker (nth 3 (pop set))))
  10115. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10116. (when (and marker (null (marker-buffer marker)))
  10117. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10118. (sit-for 3)
  10119. (throw 'exit nil)))
  10120. t)))
  10121. (defun org-refile-cache-put (set &rest identifiers)
  10122. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10123. (let* ((key (sha1 (prin1-to-string identifiers)))
  10124. (entry (assoc key org-refile-cache)))
  10125. (if entry
  10126. (setcdr entry set)
  10127. (push (cons key set) org-refile-cache))))
  10128. (defun org-refile-cache-get (&rest identifiers)
  10129. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10130. (cond
  10131. ((not org-refile-cache) nil)
  10132. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10133. (t
  10134. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10135. org-refile-cache))))
  10136. (and set (org-refile-cache-check-set set) set)))))
  10137. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10138. "Produce a table with refile targets."
  10139. (let ((case-fold-search nil)
  10140. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10141. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10142. targets tgs txt re files f desc descre fast-path-p level pos0)
  10143. (message "Getting targets...")
  10144. (with-current-buffer (or default-buffer (current-buffer))
  10145. (while (setq entry (pop entries))
  10146. (setq files (car entry) desc (cdr entry))
  10147. (setq fast-path-p nil)
  10148. (cond
  10149. ((null files) (setq files (list (current-buffer))))
  10150. ((eq files 'org-agenda-files)
  10151. (setq files (org-agenda-files 'unrestricted)))
  10152. ((and (symbolp files) (fboundp files))
  10153. (setq files (funcall files)))
  10154. ((and (symbolp files) (boundp files))
  10155. (setq files (symbol-value files))))
  10156. (if (stringp files) (setq files (list files)))
  10157. (cond
  10158. ((eq (car desc) :tag)
  10159. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10160. ((eq (car desc) :todo)
  10161. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10162. ((eq (car desc) :regexp)
  10163. (setq descre (cdr desc)))
  10164. ((eq (car desc) :level)
  10165. (setq descre (concat "^\\*\\{" (number-to-string
  10166. (if org-odd-levels-only
  10167. (1- (* 2 (cdr desc)))
  10168. (cdr desc)))
  10169. "\\}[ \t]")))
  10170. ((eq (car desc) :maxlevel)
  10171. (setq fast-path-p t)
  10172. (setq descre (concat "^\\*\\{1," (number-to-string
  10173. (if org-odd-levels-only
  10174. (1- (* 2 (cdr desc)))
  10175. (cdr desc)))
  10176. "\\}[ \t]")))
  10177. (t (error "Bad refiling target description %s" desc)))
  10178. (while (setq f (pop files))
  10179. (with-current-buffer
  10180. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10181. (or
  10182. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10183. (progn
  10184. (if (bufferp f) (setq f (buffer-file-name
  10185. (buffer-base-buffer f))))
  10186. (setq f (and f (expand-file-name f)))
  10187. (if (eq org-refile-use-outline-path 'file)
  10188. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10189. (save-excursion
  10190. (save-restriction
  10191. (widen)
  10192. (goto-char (point-min))
  10193. (while (re-search-forward descre nil t)
  10194. (goto-char (setq pos0 (point-at-bol)))
  10195. (catch 'next
  10196. (when org-refile-target-verify-function
  10197. (save-match-data
  10198. (or (funcall org-refile-target-verify-function)
  10199. (throw 'next t))))
  10200. (when (and (looking-at org-complex-heading-regexp)
  10201. (not (member (match-string 4) excluded-entries))
  10202. (match-string 4))
  10203. (setq level (org-reduced-level
  10204. (- (match-end 1) (match-beginning 1)))
  10205. txt (org-link-display-format (match-string 4))
  10206. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10207. re (format org-complex-heading-regexp-format
  10208. (regexp-quote (match-string 4))))
  10209. (when org-refile-use-outline-path
  10210. (setq txt (mapconcat
  10211. 'org-protect-slash
  10212. (append
  10213. (if (eq org-refile-use-outline-path
  10214. 'file)
  10215. (list (file-name-nondirectory
  10216. (buffer-file-name
  10217. (buffer-base-buffer))))
  10218. (if (eq org-refile-use-outline-path
  10219. 'full-file-path)
  10220. (list (buffer-file-name
  10221. (buffer-base-buffer)))))
  10222. (org-get-outline-path fast-path-p
  10223. level txt)
  10224. (list txt))
  10225. "/")))
  10226. (push (list txt f re (org-refile-marker (point)))
  10227. tgs)))
  10228. (when (= (point) pos0)
  10229. ;; verification function has not moved point
  10230. (goto-char (point-at-eol))))))))
  10231. (when org-refile-use-cache
  10232. (org-refile-cache-put tgs (buffer-file-name) descre))
  10233. (setq targets (append tgs targets))))))
  10234. (message "Getting targets...done")
  10235. (nreverse targets)))
  10236. (defun org-protect-slash (s)
  10237. (while (string-match "/" s)
  10238. (setq s (replace-match "\\" t t s)))
  10239. s)
  10240. (defvar org-olpa (make-vector 20 nil))
  10241. (defun org-get-outline-path (&optional fastp level heading)
  10242. "Return the outline path to the current entry, as a list.
  10243. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10244. routine which makes outline path derivations for an entire file,
  10245. avoiding backtracing. Refile target collection makes use of that."
  10246. (if fastp
  10247. (progn
  10248. (if (> level 19)
  10249. (error "Outline path failure, more than 19 levels"))
  10250. (loop for i from level upto 19 do
  10251. (aset org-olpa i nil))
  10252. (prog1
  10253. (delq nil (append org-olpa nil))
  10254. (aset org-olpa level heading)))
  10255. (let (rtn case-fold-search)
  10256. (save-excursion
  10257. (save-restriction
  10258. (widen)
  10259. (while (org-up-heading-safe)
  10260. (when (looking-at org-complex-heading-regexp)
  10261. (push (org-trim
  10262. (replace-regexp-in-string
  10263. ;; Remove statistical/checkboxes cookies
  10264. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10265. (org-match-string-no-properties 4)))
  10266. rtn)))
  10267. rtn)))))
  10268. (defun org-format-outline-path (path &optional width prefix separator)
  10269. "Format the outline path PATH for display.
  10270. WIDTH is the maximum number of characters that is available.
  10271. PREFIX is a prefix to be included in the returned string,
  10272. such as the file name.
  10273. SEPARATOR is inserted between the different parts of the path,
  10274. the default is \"/\"."
  10275. (setq width (or width 79))
  10276. (if prefix (setq width (- width (length prefix))))
  10277. (if (not path)
  10278. (or prefix "")
  10279. (let* ((nsteps (length path))
  10280. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10281. (maxwidth (if (<= total-width width)
  10282. 10000 ;; everything fits
  10283. ;; we need to shorten the level headings
  10284. (/ (- width nsteps) nsteps)))
  10285. (org-odd-levels-only nil)
  10286. (n 0)
  10287. (total (1+ (length prefix))))
  10288. (setq maxwidth (max maxwidth 10))
  10289. (concat prefix
  10290. (if prefix (or separator "/"))
  10291. (mapconcat
  10292. (lambda (h)
  10293. (setq n (1+ n))
  10294. (if (and (= n nsteps) (< maxwidth 10000))
  10295. (setq maxwidth (- total-width total)))
  10296. (if (< (length h) maxwidth)
  10297. (progn (setq total (+ total (length h) 1)) h)
  10298. (setq h (substring h 0 (- maxwidth 2))
  10299. total (+ total maxwidth 1))
  10300. (if (string-match "[ \t]+\\'" h)
  10301. (setq h (substring h 0 (match-beginning 0))))
  10302. (setq h (concat h "..")))
  10303. (org-add-props h nil 'face
  10304. (nth (% (1- n) org-n-level-faces)
  10305. org-level-faces))
  10306. h)
  10307. path (or separator "/"))))))
  10308. (defun org-display-outline-path (&optional file current separator just-return-string)
  10309. "Display the current outline path in the echo area.
  10310. If FILE is non-nil, prepend the output with the file name.
  10311. If CURRENT is non-nil, append the current heading to the output.
  10312. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10313. the different parts of the path and defaults to \"/\".
  10314. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10315. (interactive "P")
  10316. (let* (case-fold-search
  10317. message-log-max ; Don't populate the *Messages* buffer
  10318. (bfn (buffer-file-name (buffer-base-buffer)))
  10319. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10320. res)
  10321. (if current (setq path (append path
  10322. (save-excursion
  10323. (org-back-to-heading t)
  10324. (if (looking-at org-complex-heading-regexp)
  10325. (list (match-string 4)))))))
  10326. (setq res
  10327. (org-format-outline-path
  10328. path
  10329. (1- (frame-width))
  10330. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10331. separator))
  10332. (if just-return-string
  10333. (org-no-properties res)
  10334. (message "%s" res))))
  10335. (defvar org-refile-history nil
  10336. "History for refiling operations.")
  10337. (defvar org-after-refile-insert-hook nil
  10338. "Hook run after `org-refile' has inserted its stuff at the new location.
  10339. Note that this is still *before* the stuff will be removed from
  10340. the *old* location.")
  10341. (defvar org-capture-last-stored-marker)
  10342. (defvar org-refile-keep nil
  10343. "Non-nil means `org-refile' will copy instead of refile.")
  10344. (defun org-copy ()
  10345. "Like `org-refile', but copy."
  10346. (interactive)
  10347. (let ((org-refile-keep t))
  10348. (funcall 'org-refile nil nil nil "Copy")))
  10349. (defun org-refile (&optional goto default-buffer rfloc msg)
  10350. "Move the entry or entries at point to another heading.
  10351. The list of target headings is compiled using the information in
  10352. `org-refile-targets', which see.
  10353. At the target location, the entry is filed as a subitem of the target
  10354. heading. Depending on `org-reverse-note-order', the new subitem will
  10355. either be the first or the last subitem.
  10356. If there is an active region, all entries in that region will be moved.
  10357. However, the region must fulfill the requirement that the first heading
  10358. is the first one sets the top-level of the moved text - at most siblings
  10359. below it are allowed.
  10360. With prefix arg GOTO, the command will only visit the target location
  10361. and not actually move anything.
  10362. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10363. go to the location where the last refiling operation has put the subtree.
  10364. With a prefix argument of `2', refile to the running clock.
  10365. RFLOC can be a refile location obtained in a different way.
  10366. MSG is a string to replace \"Refile\" in the default prompt with
  10367. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10368. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10369. If you are using target caching (see `org-refile-use-cache'),
  10370. you have to clear the target cache in order to find new targets.
  10371. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10372. prefix argument (`C-u C-u C-u C-c C-w')."
  10373. (interactive "P")
  10374. (if (member goto '(0 (64)))
  10375. (org-refile-cache-clear)
  10376. (let* ((actionmsg (or msg "Refile"))
  10377. (cbuf (current-buffer))
  10378. (regionp (org-region-active-p))
  10379. (region-start (and regionp (region-beginning)))
  10380. (region-end (and regionp (region-end)))
  10381. (region-length (and regionp (- region-end region-start)))
  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. (org-toggle-heading)))
  10393. (user-error "The region is not a (sequence of) subtree(s)")))
  10394. (if (equal goto '(16))
  10395. (org-refile-goto-last-stored)
  10396. (when (or
  10397. (and (equal goto 2)
  10398. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10399. (prog1
  10400. (setq it (list (or org-clock-heading "running clock")
  10401. (buffer-file-name
  10402. (marker-buffer org-clock-hd-marker))
  10403. ""
  10404. (marker-position org-clock-hd-marker)))
  10405. (setq goto nil)))
  10406. (setq it (or rfloc
  10407. (let (heading-text)
  10408. (save-excursion
  10409. (unless goto
  10410. (org-back-to-heading t)
  10411. (setq heading-text
  10412. (nth 4 (org-heading-components))))
  10413. (org-refile-get-location
  10414. (cond (goto "Goto")
  10415. (regionp (concat actionmsg " region to"))
  10416. (t (concat actionmsg " subtree \""
  10417. heading-text "\" to")))
  10418. default-buffer
  10419. (and (not (equal '(4) goto))
  10420. org-refile-allow-creating-parent-nodes)
  10421. goto))))))
  10422. (setq file (nth 1 it)
  10423. re (nth 2 it)
  10424. pos (nth 3 it))
  10425. (if (and (not goto)
  10426. pos
  10427. (equal (buffer-file-name) file)
  10428. (if regionp
  10429. (and (>= pos region-start)
  10430. (<= pos region-end))
  10431. (and (>= pos (point))
  10432. (< pos (save-excursion
  10433. (org-end-of-subtree t t))))))
  10434. (error "Cannot refile to position inside the tree or region"))
  10435. (setq nbuf (or (find-buffer-visiting file)
  10436. (find-file-noselect file)))
  10437. (if goto
  10438. (progn
  10439. (org-pop-to-buffer-same-window nbuf)
  10440. (goto-char pos)
  10441. (org-show-context 'org-goto))
  10442. (if regionp
  10443. (progn
  10444. (org-kill-new (buffer-substring region-start region-end))
  10445. (org-save-markers-in-region region-start region-end))
  10446. (org-copy-subtree 1 nil t))
  10447. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10448. (find-file-noselect file)))
  10449. (setq reversed (org-notes-order-reversed-p))
  10450. (save-excursion
  10451. (save-restriction
  10452. (widen)
  10453. (if pos
  10454. (progn
  10455. (goto-char pos)
  10456. (looking-at org-outline-regexp)
  10457. (setq level (org-get-valid-level (funcall outline-level) 1))
  10458. (goto-char
  10459. (if reversed
  10460. (or (outline-next-heading) (point-max))
  10461. (or (save-excursion (org-get-next-sibling))
  10462. (org-end-of-subtree t t)
  10463. (point-max)))))
  10464. (setq level 1)
  10465. (if (not reversed)
  10466. (goto-char (point-max))
  10467. (goto-char (point-min))
  10468. (or (outline-next-heading) (goto-char (point-max)))))
  10469. (if (not (bolp)) (newline))
  10470. (org-paste-subtree level)
  10471. (when org-log-refile
  10472. (org-add-log-setup 'refile nil nil 'findpos
  10473. org-log-refile)
  10474. (unless (eq org-log-refile 'note)
  10475. (save-excursion (org-add-log-note))))
  10476. (and org-auto-align-tags
  10477. (let ((org-loop-over-headlines-in-active-region nil))
  10478. (org-set-tags nil t)))
  10479. (with-demoted-errors
  10480. (bookmark-set "org-refile-last-stored"))
  10481. ;; If we are refiling for capture, make sure that the
  10482. ;; last-capture pointers point here
  10483. (when (org-bound-and-true-p org-refile-for-capture)
  10484. (with-demoted-errors
  10485. (bookmark-set "org-capture-last-stored-marker"))
  10486. (move-marker org-capture-last-stored-marker (point)))
  10487. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10488. (run-hooks 'org-after-refile-insert-hook))))
  10489. (unless org-refile-keep
  10490. (if regionp
  10491. (delete-region (point) (+ (point) region-length))
  10492. (org-cut-subtree)))
  10493. (when (featurep 'org-inlinetask)
  10494. (org-inlinetask-remove-END-maybe))
  10495. (setq org-markers-to-move nil)
  10496. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10497. (defun org-refile-goto-last-stored ()
  10498. "Go to the location where the last refile was stored."
  10499. (interactive)
  10500. (bookmark-jump "org-refile-last-stored")
  10501. (message "This is the location of the last refile"))
  10502. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10503. no-exclude)
  10504. "Prompt the user for a refile location, using PROMPT.
  10505. PROMPT should not be suffixed with a colon and a space, because
  10506. this function appends the default value from
  10507. `org-refile-history' automatically, if that is not empty.
  10508. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10509. this is used for the GOTO interface."
  10510. (let ((org-refile-targets org-refile-targets)
  10511. (org-refile-use-outline-path org-refile-use-outline-path)
  10512. excluded-entries)
  10513. (when (and (derived-mode-p 'org-mode)
  10514. (not org-refile-use-cache)
  10515. (not no-exclude))
  10516. (org-map-tree
  10517. (lambda()
  10518. (setq excluded-entries
  10519. (append excluded-entries (list (org-get-heading t t)))))))
  10520. (setq org-refile-target-table
  10521. (org-refile-get-targets default-buffer excluded-entries)))
  10522. (unless org-refile-target-table
  10523. (user-error "No refile targets"))
  10524. (let* ((cbuf (current-buffer))
  10525. (partial-completion-mode nil)
  10526. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10527. (cfunc (if (and org-refile-use-outline-path
  10528. org-outline-path-complete-in-steps)
  10529. 'org-olpath-completing-read
  10530. 'org-icompleting-read))
  10531. (extra (if org-refile-use-outline-path "/" ""))
  10532. (cbnex (concat (buffer-name) extra))
  10533. (filename (and cfn (expand-file-name cfn)))
  10534. (tbl (mapcar
  10535. (lambda (x)
  10536. (if (and (not (member org-refile-use-outline-path
  10537. '(file full-file-path)))
  10538. (not (equal filename (nth 1 x))))
  10539. (cons (concat (car x) extra " ("
  10540. (file-name-nondirectory (nth 1 x)) ")")
  10541. (cdr x))
  10542. (cons (concat (car x) extra) (cdr x))))
  10543. org-refile-target-table))
  10544. (completion-ignore-case t)
  10545. cdef
  10546. (prompt (concat prompt
  10547. (or (and (car org-refile-history)
  10548. (concat " (default " (car org-refile-history) ")"))
  10549. (and (assoc cbnex tbl) (setq cdef cbnex)
  10550. (concat " (default " cbnex ")"))) ": "))
  10551. pa answ parent-target child parent old-hist)
  10552. (setq old-hist org-refile-history)
  10553. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10554. nil 'org-refile-history (or cdef (car org-refile-history))))
  10555. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10556. (org-refile-check-position pa)
  10557. (if pa
  10558. (progn
  10559. (when (or (not org-refile-history)
  10560. (not (eq old-hist org-refile-history))
  10561. (not (equal (car pa) (car org-refile-history))))
  10562. (setq org-refile-history
  10563. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10564. org-refile-history
  10565. (cdr org-refile-history))))
  10566. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10567. (pop org-refile-history)))
  10568. pa)
  10569. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10570. (progn
  10571. (setq parent (match-string 1 answ)
  10572. child (match-string 2 answ))
  10573. (setq parent-target (or (assoc parent tbl)
  10574. (assoc (concat parent "/") tbl)))
  10575. (when (and parent-target
  10576. (or (eq new-nodes t)
  10577. (and (eq new-nodes 'confirm)
  10578. (y-or-n-p (format "Create new node \"%s\"? "
  10579. child)))))
  10580. (org-refile-new-child parent-target child)))
  10581. (user-error "Invalid target location")))))
  10582. (declare-function org-string-nw-p "org-macs" (s))
  10583. (defun org-refile-check-position (refile-pointer)
  10584. "Check if the refile pointer matches the headline to which it points."
  10585. (let* ((file (nth 1 refile-pointer))
  10586. (re (nth 2 refile-pointer))
  10587. (pos (nth 3 refile-pointer))
  10588. buffer)
  10589. (if (and (not (markerp pos)) (not file))
  10590. (user-error "Please save the buffer to a file before refiling")
  10591. (when (org-string-nw-p re)
  10592. (setq buffer (if (markerp pos)
  10593. (marker-buffer pos)
  10594. (or (find-buffer-visiting file)
  10595. (find-file-noselect file))))
  10596. (with-current-buffer buffer
  10597. (save-excursion
  10598. (save-restriction
  10599. (widen)
  10600. (goto-char pos)
  10601. (beginning-of-line 1)
  10602. (unless (org-looking-at-p re)
  10603. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10604. (defun org-refile-new-child (parent-target child)
  10605. "Use refile target PARENT-TARGET to add new CHILD below it."
  10606. (unless parent-target
  10607. (error "Cannot find parent for new node"))
  10608. (let ((file (nth 1 parent-target))
  10609. (pos (nth 3 parent-target))
  10610. level)
  10611. (with-current-buffer (or (find-buffer-visiting file)
  10612. (find-file-noselect file))
  10613. (save-excursion
  10614. (save-restriction
  10615. (widen)
  10616. (if pos
  10617. (goto-char pos)
  10618. (goto-char (point-max))
  10619. (if (not (bolp)) (newline)))
  10620. (when (looking-at org-outline-regexp)
  10621. (setq level (funcall outline-level))
  10622. (org-end-of-subtree t t))
  10623. (org-back-over-empty-lines)
  10624. (insert "\n" (make-string
  10625. (if pos (org-get-valid-level level 1) 1) ?*)
  10626. " " child "\n")
  10627. (beginning-of-line 0)
  10628. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10629. (defun org-olpath-completing-read (prompt collection &rest args)
  10630. "Read an outline path like a file name."
  10631. (let ((thetable collection)
  10632. (org-completion-use-ido nil) ; does not work with ido.
  10633. (org-completion-use-iswitchb nil)) ; or iswitchb
  10634. (apply
  10635. 'org-icompleting-read prompt
  10636. (lambda (string predicate &optional flag)
  10637. (let (rtn r f (l (length string)))
  10638. (cond
  10639. ((eq flag nil)
  10640. ;; try completion
  10641. (try-completion string thetable))
  10642. ((eq flag t)
  10643. ;; all-completions
  10644. (setq rtn (all-completions string thetable predicate))
  10645. (mapcar
  10646. (lambda (x)
  10647. (setq r (substring x l))
  10648. (if (string-match " ([^)]*)$" x)
  10649. (setq f (match-string 0 x))
  10650. (setq f ""))
  10651. (if (string-match "/" r)
  10652. (concat string (substring r 0 (match-end 0)) f)
  10653. x))
  10654. rtn))
  10655. ((eq flag 'lambda)
  10656. ;; exact match?
  10657. (assoc string thetable)))))
  10658. args)))
  10659. ;;;; Dynamic blocks
  10660. (defun org-find-dblock (name)
  10661. "Find the first dynamic block with name NAME in the buffer.
  10662. If not found, stay at current position and return nil."
  10663. (let ((case-fold-search t) pos)
  10664. (save-excursion
  10665. (goto-char (point-min))
  10666. (setq pos (and (re-search-forward
  10667. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10668. (match-beginning 0))))
  10669. (if pos (goto-char pos))
  10670. pos))
  10671. (defconst org-dblock-start-re
  10672. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10673. "Matches the start line of a dynamic block, with parameters.")
  10674. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10675. "Matches the end of a dynamic block.")
  10676. (defun org-create-dblock (plist)
  10677. "Create a dynamic block section, with parameters taken from PLIST.
  10678. PLIST must contain a :name entry which is used as name of the block."
  10679. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10680. (end-of-line 1)
  10681. (newline))
  10682. (let ((col (current-column))
  10683. (name (plist-get plist :name)))
  10684. (insert "#+BEGIN: " name)
  10685. (while plist
  10686. (if (eq (car plist) :name)
  10687. (setq plist (cddr plist))
  10688. (insert " " (prin1-to-string (pop plist)))))
  10689. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10690. (beginning-of-line -2)))
  10691. (defun org-prepare-dblock ()
  10692. "Prepare dynamic block for refresh.
  10693. This empties the block, puts the cursor at the insert position and returns
  10694. the property list including an extra property :name with the block name."
  10695. (unless (looking-at org-dblock-start-re)
  10696. (user-error "Not at a dynamic block"))
  10697. (let* ((begdel (1+ (match-end 0)))
  10698. (name (org-no-properties (match-string 1)))
  10699. (params (append (list :name name)
  10700. (read (concat "(" (match-string 3) ")")))))
  10701. (save-excursion
  10702. (beginning-of-line 1)
  10703. (skip-chars-forward " \t")
  10704. (setq params (plist-put params :indentation-column (current-column))))
  10705. (unless (re-search-forward org-dblock-end-re nil t)
  10706. (error "Dynamic block not terminated"))
  10707. (setq params
  10708. (append params
  10709. (list :content (buffer-substring
  10710. begdel (match-beginning 0)))))
  10711. (delete-region begdel (match-beginning 0))
  10712. (goto-char begdel)
  10713. (open-line 1)
  10714. params))
  10715. (defun org-map-dblocks (&optional command)
  10716. "Apply COMMAND to all dynamic blocks in the current buffer.
  10717. If COMMAND is not given, use `org-update-dblock'."
  10718. (let ((cmd (or command 'org-update-dblock)))
  10719. (save-excursion
  10720. (goto-char (point-min))
  10721. (while (re-search-forward org-dblock-start-re nil t)
  10722. (goto-char (match-beginning 0))
  10723. (save-excursion
  10724. (condition-case nil
  10725. (funcall cmd)
  10726. (error (message "Error during update of dynamic block"))))
  10727. (unless (re-search-forward org-dblock-end-re nil t)
  10728. (error "Dynamic block not terminated"))))))
  10729. (defun org-dblock-update (&optional arg)
  10730. "User command for updating dynamic blocks.
  10731. Update the dynamic block at point. With prefix ARG, update all dynamic
  10732. blocks in the buffer."
  10733. (interactive "P")
  10734. (if arg
  10735. (org-update-all-dblocks)
  10736. (or (looking-at org-dblock-start-re)
  10737. (org-beginning-of-dblock))
  10738. (org-update-dblock)))
  10739. (defun org-update-dblock ()
  10740. "Update the dynamic block at point.
  10741. This means to empty the block, parse for parameters and then call
  10742. the correct writing function."
  10743. (interactive)
  10744. (save-window-excursion
  10745. (let* ((pos (point))
  10746. (line (org-current-line))
  10747. (params (org-prepare-dblock))
  10748. (name (plist-get params :name))
  10749. (indent (plist-get params :indentation-column))
  10750. (cmd (intern (concat "org-dblock-write:" name))))
  10751. (message "Updating dynamic block `%s' at line %d..." name line)
  10752. (funcall cmd params)
  10753. (message "Updating dynamic block `%s' at line %d...done" name line)
  10754. (goto-char pos)
  10755. (when (and indent (> indent 0))
  10756. (setq indent (make-string indent ?\ ))
  10757. (save-excursion
  10758. (org-beginning-of-dblock)
  10759. (forward-line 1)
  10760. (while (not (looking-at org-dblock-end-re))
  10761. (insert indent)
  10762. (beginning-of-line 2))
  10763. (when (looking-at org-dblock-end-re)
  10764. (and (looking-at "[ \t]+")
  10765. (replace-match ""))
  10766. (insert indent)))))))
  10767. (defun org-beginning-of-dblock ()
  10768. "Find the beginning of the dynamic block at point.
  10769. Error if there is no such block at point."
  10770. (let ((pos (point))
  10771. beg)
  10772. (end-of-line 1)
  10773. (if (and (re-search-backward org-dblock-start-re nil t)
  10774. (setq beg (match-beginning 0))
  10775. (re-search-forward org-dblock-end-re nil t)
  10776. (> (match-end 0) pos))
  10777. (goto-char beg)
  10778. (goto-char pos)
  10779. (error "Not in a dynamic block"))))
  10780. (defun org-update-all-dblocks ()
  10781. "Update all dynamic blocks in the buffer.
  10782. This function can be used in a hook."
  10783. (interactive)
  10784. (when (derived-mode-p 'org-mode)
  10785. (org-map-dblocks 'org-update-dblock)))
  10786. ;;;; Completion
  10787. (defun org-get-export-keywords ()
  10788. "Return a list of all currently understood export keywords.
  10789. Export keywords include options, block names, attributes and
  10790. keywords relative to each registered export back-end."
  10791. (delq nil
  10792. (let (keywords)
  10793. (mapc
  10794. (lambda (back-end)
  10795. (let ((props (cdr back-end)))
  10796. ;; Back-end name (for keywords, like #+LATEX:)
  10797. (push (upcase (symbol-name (car back-end))) keywords)
  10798. ;; Back-end options.
  10799. (mapc (lambda (option) (push (cadr option) keywords))
  10800. (plist-get (cdr back-end) :options-alist))))
  10801. (org-bound-and-true-p org-export-registered-backends))
  10802. keywords)))
  10803. (defconst org-options-keywords
  10804. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10805. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10806. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10807. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10808. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10809. (defcustom org-structure-template-alist
  10810. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10811. "<src lang=\"?\">\n\n</src>")
  10812. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10813. "<example>\n?\n</example>")
  10814. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10815. "<quote>\n?\n</quote>")
  10816. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10817. "<verse>\n?\n</verse>")
  10818. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10819. "<verbatim>\n?\n</verbatim>")
  10820. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10821. "<center>\n?\n</center>")
  10822. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10823. "<literal style=\"latex\">\n?\n</literal>")
  10824. ("L" "#+LaTeX: "
  10825. "<literal style=\"latex\">?</literal>")
  10826. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10827. "<literal style=\"html\">\n?\n</literal>")
  10828. ("H" "#+HTML: "
  10829. "<literal style=\"html\">?</literal>")
  10830. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10831. ("A" "#+ASCII: ")
  10832. ("i" "#+INDEX: ?"
  10833. "#+INDEX: ?")
  10834. ("I" "#+INCLUDE: %file ?"
  10835. "<include file=%file markup=\"?\">"))
  10836. "Structure completion elements.
  10837. This is a list of abbreviation keys and values. The value gets inserted
  10838. if you type `<' followed by the key and then press the completion key,
  10839. usually `M-TAB'. %file will be replaced by a file name after prompting
  10840. for the file using completion. The cursor will be placed at the position
  10841. of the `?` in the template.
  10842. There are two templates for each key, the first uses the original Org syntax,
  10843. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10844. the default when the /org-mtags.el/ module has been loaded. See also the
  10845. variable `org-mtags-prefer-muse-templates'."
  10846. :group 'org-completion
  10847. :type '(repeat
  10848. (string :tag "Key")
  10849. (string :tag "Template")
  10850. (string :tag "Muse Template")))
  10851. (defun org-try-structure-completion ()
  10852. "Try to complete a structure template before point.
  10853. This looks for strings like \"<e\" on an otherwise empty line and
  10854. expands them."
  10855. (let ((l (buffer-substring (point-at-bol) (point)))
  10856. a)
  10857. (when (and (looking-at "[ \t]*$")
  10858. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10859. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10860. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10861. (match-beginning 1)) a)
  10862. t)))
  10863. (defun org-complete-expand-structure-template (start cell)
  10864. "Expand a structure template."
  10865. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10866. (rpl (nth (if musep 2 1) cell))
  10867. (ind ""))
  10868. (delete-region start (point))
  10869. (when (string-match "\\`#\\+" rpl)
  10870. (cond
  10871. ((bolp))
  10872. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10873. (setq ind (buffer-substring (point-at-bol) (point))))
  10874. (t (newline))))
  10875. (setq start (point))
  10876. (if (string-match "%file" rpl)
  10877. (setq rpl (replace-match
  10878. (concat
  10879. "\""
  10880. (save-match-data
  10881. (abbreviate-file-name (read-file-name "Include file: ")))
  10882. "\"")
  10883. t t rpl)))
  10884. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10885. (concat "\n" ind)))
  10886. (insert rpl)
  10887. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10888. ;;;; TODO, DEADLINE, Comments
  10889. (defun org-toggle-comment ()
  10890. "Change the COMMENT state of an entry."
  10891. (interactive)
  10892. (save-excursion
  10893. (org-back-to-heading)
  10894. (let (case-fold-search)
  10895. (cond
  10896. ((looking-at (format org-heading-keyword-regexp-format
  10897. org-comment-string))
  10898. (goto-char (match-end 1))
  10899. (looking-at (concat " +" org-comment-string))
  10900. (replace-match "" t t)
  10901. (when (eolp) (insert " ")))
  10902. ((looking-at org-outline-regexp)
  10903. (goto-char (match-end 0))
  10904. (insert org-comment-string " "))))))
  10905. (defvar org-last-todo-state-is-todo nil
  10906. "This is non-nil when the last TODO state change led to a TODO state.
  10907. If the last change removed the TODO tag or switched to DONE, then
  10908. this is nil.")
  10909. (defvar org-setting-tags nil) ; dynamically skipped
  10910. (defvar org-todo-setup-filter-hook nil
  10911. "Hook for functions that pre-filter todo specs.
  10912. Each function takes a todo spec and returns either nil or the spec
  10913. transformed into canonical form." )
  10914. (defvar org-todo-get-default-hook nil
  10915. "Hook for functions that get a default item for todo.
  10916. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10917. nil or a string to be used for the todo mark." )
  10918. (defvar org-agenda-headline-snapshot-before-repeat)
  10919. (defun org-current-effective-time ()
  10920. "Return current time adjusted for `org-extend-today-until' variable."
  10921. (let* ((ct (org-current-time))
  10922. (dct (decode-time ct))
  10923. (ct1
  10924. (cond
  10925. (org-use-last-clock-out-time-as-effective-time
  10926. (or (org-clock-get-last-clock-out-time) ct))
  10927. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10928. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10929. (t ct))))
  10930. ct1))
  10931. (defun org-todo-yesterday (&optional arg)
  10932. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10933. (interactive "P")
  10934. (if (eq major-mode 'org-agenda-mode)
  10935. (apply 'org-agenda-todo-yesterday arg)
  10936. (let* ((hour (third (decode-time
  10937. (org-current-time))))
  10938. (org-extend-today-until (1+ hour)))
  10939. (org-todo arg))))
  10940. (defvar org-block-entry-blocking ""
  10941. "First entry preventing the TODO state change.")
  10942. (defun org-todo (&optional arg)
  10943. "Change the TODO state of an item.
  10944. The state of an item is given by a keyword at the start of the heading,
  10945. like
  10946. *** TODO Write paper
  10947. *** DONE Call mom
  10948. The different keywords are specified in the variable `org-todo-keywords'.
  10949. By default the available states are \"TODO\" and \"DONE\".
  10950. So for this example: when the item starts with TODO, it is changed to DONE.
  10951. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10952. DONE are present, add TODO at the beginning of the heading.
  10953. With \\[universal-argument] prefix arg, use completion to determine the new \
  10954. state.
  10955. With numeric prefix arg, switch to that state.
  10956. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10957. keywords (nextset).
  10958. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10959. With a numeric prefix arg of 0, inhibit note taking for the change.
  10960. For calling through lisp, arg is also interpreted in the following way:
  10961. 'none -> empty state
  10962. \"\"(empty string) -> switch to empty state
  10963. 'done -> switch to DONE
  10964. 'nextset -> switch to the next set of keywords
  10965. 'previousset -> switch to the previous set of keywords
  10966. \"WAITING\" -> switch to the specified keyword, but only if it
  10967. really is a member of `org-todo-keywords'."
  10968. (interactive "P")
  10969. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10970. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10971. 'region-start-level 'region))
  10972. org-loop-over-headlines-in-active-region)
  10973. (org-map-entries
  10974. `(org-todo ,arg)
  10975. org-loop-over-headlines-in-active-region
  10976. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10977. (if (equal arg '(16)) (setq arg 'nextset))
  10978. (let ((org-blocker-hook org-blocker-hook)
  10979. commentp
  10980. case-fold-search)
  10981. (when (equal arg '(64))
  10982. (setq arg nil org-blocker-hook nil))
  10983. (when (and org-blocker-hook
  10984. (or org-inhibit-blocking
  10985. (org-entry-get nil "NOBLOCKING")))
  10986. (setq org-blocker-hook nil))
  10987. (save-excursion
  10988. (catch 'exit
  10989. (org-back-to-heading t)
  10990. (when (looking-at (concat "^\\*+ " org-comment-string))
  10991. (org-toggle-comment)
  10992. (setq commentp t))
  10993. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10994. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10995. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10996. (let* ((match-data (match-data))
  10997. (startpos (point-at-bol))
  10998. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10999. (org-log-done org-log-done)
  11000. (org-log-repeat org-log-repeat)
  11001. (org-todo-log-states org-todo-log-states)
  11002. (org-inhibit-logging
  11003. (if (equal arg 0)
  11004. (progn (setq arg nil) 'note) org-inhibit-logging))
  11005. (this (match-string 1))
  11006. (hl-pos (match-beginning 0))
  11007. (head (org-get-todo-sequence-head this))
  11008. (ass (assoc head org-todo-kwd-alist))
  11009. (interpret (nth 1 ass))
  11010. (done-word (nth 3 ass))
  11011. (final-done-word (nth 4 ass))
  11012. (org-last-state (or this ""))
  11013. (completion-ignore-case t)
  11014. (member (member this org-todo-keywords-1))
  11015. (tail (cdr member))
  11016. (org-state (cond
  11017. ((and org-todo-key-trigger
  11018. (or (and (equal arg '(4))
  11019. (eq org-use-fast-todo-selection 'prefix))
  11020. (and (not arg) org-use-fast-todo-selection
  11021. (not (eq org-use-fast-todo-selection
  11022. 'prefix)))))
  11023. ;; Use fast selection
  11024. (org-fast-todo-selection))
  11025. ((and (equal arg '(4))
  11026. (or (not org-use-fast-todo-selection)
  11027. (not org-todo-key-trigger)))
  11028. ;; Read a state with completion
  11029. (org-icompleting-read
  11030. "State: " (mapcar 'list org-todo-keywords-1)
  11031. nil t))
  11032. ((eq arg 'right)
  11033. (if this
  11034. (if tail (car tail) nil)
  11035. (car org-todo-keywords-1)))
  11036. ((eq arg 'left)
  11037. (if (equal member org-todo-keywords-1)
  11038. nil
  11039. (if this
  11040. (nth (- (length org-todo-keywords-1)
  11041. (length tail) 2)
  11042. org-todo-keywords-1)
  11043. (org-last org-todo-keywords-1))))
  11044. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11045. (setq arg nil))) ; hack to fall back to cycling
  11046. (arg
  11047. ;; user or caller requests a specific state
  11048. (cond
  11049. ((equal arg "") nil)
  11050. ((eq arg 'none) nil)
  11051. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11052. ((eq arg 'nextset)
  11053. (or (car (cdr (member head org-todo-heads)))
  11054. (car org-todo-heads)))
  11055. ((eq arg 'previousset)
  11056. (let ((org-todo-heads (reverse org-todo-heads)))
  11057. (or (car (cdr (member head org-todo-heads)))
  11058. (car org-todo-heads))))
  11059. ((car (member arg org-todo-keywords-1)))
  11060. ((stringp arg)
  11061. (user-error "State `%s' not valid in this file" arg))
  11062. ((nth (1- (prefix-numeric-value arg))
  11063. org-todo-keywords-1))))
  11064. ((null member) (or head (car org-todo-keywords-1)))
  11065. ((equal this final-done-word) nil) ;; -> make empty
  11066. ((null tail) nil) ;; -> first entry
  11067. ((memq interpret '(type priority))
  11068. (if (eq this-command last-command)
  11069. (car tail)
  11070. (if (> (length tail) 0)
  11071. (or done-word (car org-done-keywords))
  11072. nil)))
  11073. (t
  11074. (car tail))))
  11075. (org-state (or
  11076. (run-hook-with-args-until-success
  11077. 'org-todo-get-default-hook org-state org-last-state)
  11078. org-state))
  11079. (next (if org-state (concat " " org-state " ") " "))
  11080. (change-plist (list :type 'todo-state-change :from this :to org-state
  11081. :position startpos))
  11082. dolog now-done-p)
  11083. (when org-blocker-hook
  11084. (setq org-last-todo-state-is-todo
  11085. (not (member this org-done-keywords)))
  11086. (unless (save-excursion
  11087. (save-match-data
  11088. (org-with-wide-buffer
  11089. (run-hook-with-args-until-failure
  11090. 'org-blocker-hook change-plist))))
  11091. (if (org-called-interactively-p 'interactive)
  11092. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11093. this org-state org-block-entry-blocking)
  11094. ;; fail silently
  11095. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11096. this org-state org-block-entry-blocking)
  11097. (throw 'exit nil))))
  11098. (store-match-data match-data)
  11099. (replace-match next t t)
  11100. (unless (pos-visible-in-window-p hl-pos)
  11101. (message "TODO state changed to %s" (org-trim next)))
  11102. (unless head
  11103. (setq head (org-get-todo-sequence-head org-state)
  11104. ass (assoc head org-todo-kwd-alist)
  11105. interpret (nth 1 ass)
  11106. done-word (nth 3 ass)
  11107. final-done-word (nth 4 ass)))
  11108. (when (memq arg '(nextset previousset))
  11109. (message "Keyword-Set %d/%d: %s"
  11110. (- (length org-todo-sets) -1
  11111. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11112. (length org-todo-sets)
  11113. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11114. (setq org-last-todo-state-is-todo
  11115. (not (member org-state org-done-keywords)))
  11116. (setq now-done-p (and (member org-state org-done-keywords)
  11117. (not (member this org-done-keywords))))
  11118. (and logging (org-local-logging logging))
  11119. (when (and (or org-todo-log-states org-log-done)
  11120. (not (eq org-inhibit-logging t))
  11121. (not (memq arg '(nextset previousset))))
  11122. ;; we need to look at recording a time and note
  11123. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11124. (nth 2 (assoc this org-todo-log-states))))
  11125. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11126. (setq dolog 'time))
  11127. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11128. (and org-state
  11129. (member org-state org-not-done-keywords)
  11130. (not (member this org-not-done-keywords))))
  11131. ;; This is now a todo state and was not one before
  11132. ;; If there was a CLOSED time stamp, get rid of it.
  11133. (org-add-planning-info nil nil 'closed))
  11134. (when (and now-done-p org-log-done)
  11135. ;; It is now done, and it was not done before
  11136. (org-add-planning-info 'closed (org-current-effective-time))
  11137. (if (and (not dolog) (eq 'note org-log-done))
  11138. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11139. (when (and org-state dolog)
  11140. ;; This is a non-nil state, and we need to log it
  11141. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11142. ;; Fixup tag positioning
  11143. (org-todo-trigger-tag-changes org-state)
  11144. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11145. (when org-provide-todo-statistics
  11146. (org-update-parent-todo-statistics))
  11147. (run-hooks 'org-after-todo-state-change-hook)
  11148. (if (and arg (not (member org-state org-done-keywords)))
  11149. (setq head (org-get-todo-sequence-head org-state)))
  11150. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11151. ;; Do we need to trigger a repeat?
  11152. (when now-done-p
  11153. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11154. ;; This is for the agenda, take a snapshot of the headline.
  11155. (save-match-data
  11156. (setq org-agenda-headline-snapshot-before-repeat
  11157. (org-get-heading))))
  11158. (org-auto-repeat-maybe org-state))
  11159. ;; Fixup cursor location if close to the keyword
  11160. (if (and (outline-on-heading-p)
  11161. (not (bolp))
  11162. (save-excursion (beginning-of-line 1)
  11163. (looking-at org-todo-line-regexp))
  11164. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11165. (progn
  11166. (goto-char (or (match-end 2) (match-end 1)))
  11167. (and (looking-at " ") (just-one-space))))
  11168. (when org-trigger-hook
  11169. (save-excursion
  11170. (run-hook-with-args 'org-trigger-hook change-plist)))
  11171. (when commentp (org-toggle-comment))))))))
  11172. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11173. "Block turning an entry into a TODO, using the hierarchy.
  11174. This checks whether the current task should be blocked from state
  11175. changes. Such blocking occurs when:
  11176. 1. The task has children which are not all in a completed state.
  11177. 2. A task has a parent with the property :ORDERED:, and there
  11178. are siblings prior to the current task with incomplete
  11179. status.
  11180. 3. The parent of the task is blocked because it has siblings that should
  11181. be done first, or is child of a block grandparent TODO entry."
  11182. (if (not org-enforce-todo-dependencies)
  11183. t ; if locally turned off don't block
  11184. (catch 'dont-block
  11185. ;; If this is not a todo state change, or if this entry is already DONE,
  11186. ;; do not block
  11187. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11188. (member (plist-get change-plist :from)
  11189. (cons 'done org-done-keywords))
  11190. (member (plist-get change-plist :to)
  11191. (cons 'todo org-not-done-keywords))
  11192. (not (plist-get change-plist :to)))
  11193. (throw 'dont-block t))
  11194. ;; If this task has children, and any are undone, it's blocked
  11195. (save-excursion
  11196. (org-back-to-heading t)
  11197. (let ((this-level (funcall outline-level)))
  11198. (outline-next-heading)
  11199. (let ((child-level (funcall outline-level)))
  11200. (while (and (not (eobp))
  11201. (> child-level this-level))
  11202. ;; this todo has children, check whether they are all
  11203. ;; completed
  11204. (if (and (not (org-entry-is-done-p))
  11205. (org-entry-is-todo-p))
  11206. (progn (setq org-block-entry-blocking (org-get-heading))
  11207. (throw 'dont-block nil)))
  11208. (outline-next-heading)
  11209. (setq child-level (funcall outline-level))))))
  11210. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11211. ;; any previous siblings are undone, it's blocked
  11212. (save-excursion
  11213. (org-back-to-heading t)
  11214. (let* ((pos (point))
  11215. (parent-pos (and (org-up-heading-safe) (point))))
  11216. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11217. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11218. (forward-line 1)
  11219. (re-search-forward org-not-done-heading-regexp pos t))
  11220. (setq org-block-entry-blocking (match-string 0))
  11221. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11222. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11223. (while t
  11224. (goto-char parent-pos)
  11225. (if (not (looking-at org-not-done-heading-regexp))
  11226. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11227. (setq pos (point))
  11228. (setq parent-pos (and (org-up-heading-safe) (point)))
  11229. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11230. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11231. (forward-line 1)
  11232. (re-search-forward org-not-done-heading-regexp pos t)
  11233. (setq org-block-entry-blocking (org-get-heading)))
  11234. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11235. (defcustom org-track-ordered-property-with-tag nil
  11236. "Should the ORDERED property also be shown as a tag?
  11237. The ORDERED property decides if an entry should require subtasks to be
  11238. completed in sequence. Since a property is not very visible, setting
  11239. this option means that toggling the ORDERED property with the command
  11240. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11241. not relevant for the behavior, but it makes things more visible.
  11242. Note that toggling the tag with tags commands will not change the property
  11243. and therefore not influence behavior!
  11244. This can be t, meaning the tag ORDERED should be used, It can also be a
  11245. string to select a different tag for this task."
  11246. :group 'org-todo
  11247. :type '(choice
  11248. (const :tag "No tracking" nil)
  11249. (const :tag "Track with ORDERED tag" t)
  11250. (string :tag "Use other tag")))
  11251. (defun org-toggle-ordered-property ()
  11252. "Toggle the ORDERED property of the current entry.
  11253. For better visibility, you can track the value of this property with a tag.
  11254. See variable `org-track-ordered-property-with-tag'."
  11255. (interactive)
  11256. (let* ((t1 org-track-ordered-property-with-tag)
  11257. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11258. (save-excursion
  11259. (org-back-to-heading)
  11260. (if (org-entry-get nil "ORDERED")
  11261. (progn
  11262. (org-delete-property "ORDERED" "PROPERTIES")
  11263. (and tag (org-toggle-tag tag 'off))
  11264. (message "Subtasks can be completed in arbitrary order"))
  11265. (org-entry-put nil "ORDERED" "t")
  11266. (and tag (org-toggle-tag tag 'on))
  11267. (message "Subtasks must be completed in sequence")))))
  11268. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11269. (defun org-block-todo-from-checkboxes (change-plist)
  11270. "Block turning an entry into a TODO, using checkboxes.
  11271. This checks whether the current task should be blocked from state
  11272. changes because there are unchecked boxes in this entry."
  11273. (if (not org-enforce-todo-checkbox-dependencies)
  11274. t ; if locally turned off don't block
  11275. (catch 'dont-block
  11276. ;; If this is not a todo state change, or if this entry is already DONE,
  11277. ;; do not block
  11278. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11279. (member (plist-get change-plist :from)
  11280. (cons 'done org-done-keywords))
  11281. (member (plist-get change-plist :to)
  11282. (cons 'todo org-not-done-keywords))
  11283. (not (plist-get change-plist :to)))
  11284. (throw 'dont-block t))
  11285. ;; If this task has checkboxes that are not checked, it's blocked
  11286. (save-excursion
  11287. (org-back-to-heading t)
  11288. (let ((beg (point)) end)
  11289. (outline-next-heading)
  11290. (setq end (point))
  11291. (goto-char beg)
  11292. (if (org-list-search-forward
  11293. (concat (org-item-beginning-re)
  11294. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11295. "\\[[- ]\\]")
  11296. end t)
  11297. (progn
  11298. (if (boundp 'org-blocked-by-checkboxes)
  11299. (setq org-blocked-by-checkboxes t))
  11300. (throw 'dont-block nil)))))
  11301. t))) ; do not block
  11302. (defun org-entry-blocked-p ()
  11303. "Is the current entry blocked?"
  11304. (org-with-silent-modifications
  11305. (if (org-entry-get nil "NOBLOCKING")
  11306. nil ;; Never block this entry
  11307. (not (run-hook-with-args-until-failure
  11308. 'org-blocker-hook
  11309. (list :type 'todo-state-change
  11310. :position (point)
  11311. :from 'todo
  11312. :to 'done))))))
  11313. (defun org-update-statistics-cookies (all)
  11314. "Update the statistics cookie, either from TODO or from checkboxes.
  11315. This should be called with the cursor in a line with a statistics cookie."
  11316. (interactive "P")
  11317. (if all
  11318. (progn
  11319. (org-update-checkbox-count 'all)
  11320. (org-map-entries 'org-update-parent-todo-statistics))
  11321. (if (not (org-at-heading-p))
  11322. (org-update-checkbox-count)
  11323. (let ((pos (point-marker))
  11324. end l1 l2)
  11325. (ignore-errors (org-back-to-heading t))
  11326. (if (not (org-at-heading-p))
  11327. (org-update-checkbox-count)
  11328. (setq l1 (org-outline-level))
  11329. (setq end (save-excursion
  11330. (outline-next-heading)
  11331. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11332. (point)))
  11333. (if (and (save-excursion
  11334. (re-search-forward
  11335. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11336. (not (save-excursion (re-search-forward
  11337. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11338. (org-update-checkbox-count)
  11339. (if (and l2 (> l2 l1))
  11340. (progn
  11341. (goto-char end)
  11342. (org-update-parent-todo-statistics))
  11343. (goto-char pos)
  11344. (beginning-of-line 1)
  11345. (while (re-search-forward
  11346. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11347. (point-at-eol) t)
  11348. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11349. (goto-char pos)
  11350. (move-marker pos nil)))))
  11351. (defvar org-entry-property-inherited-from) ;; defined below
  11352. (defun org-update-parent-todo-statistics ()
  11353. "Update any statistics cookie in the parent of the current headline.
  11354. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11355. the entire subtree and this will travel up the hierarchy and update
  11356. statistics everywhere."
  11357. (let* ((prop (save-excursion (org-up-heading-safe)
  11358. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11359. (recursive (or (not org-hierarchical-todo-statistics)
  11360. (and prop (string-match "\\<recursive\\>" prop))))
  11361. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11362. 0))
  11363. (first t)
  11364. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11365. level ltoggle l1 new ndel
  11366. (cnt-all 0) (cnt-done 0) is-percent kwd
  11367. checkbox-beg ov ovs ove cookie-present)
  11368. (catch 'exit
  11369. (save-excursion
  11370. (beginning-of-line 1)
  11371. (setq ltoggle (funcall outline-level))
  11372. ;; Three situations are to consider:
  11373. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11374. ;; to the top-level ancestor on the headline;
  11375. ;; 2. If parent has "recursive" property, repeat up to the
  11376. ;; headline setting that property, taking inheritance into
  11377. ;; account;
  11378. ;; 3. Else, move up to direct parent and proceed only once.
  11379. (while (and (setq level (org-up-heading-safe))
  11380. (or recursive first)
  11381. (>= (point) lim))
  11382. (setq first nil cookie-present nil)
  11383. (unless (and level
  11384. (not (string-match
  11385. "\\<checkbox\\>"
  11386. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11387. "")))))
  11388. (throw 'exit nil))
  11389. (while (re-search-forward box-re (point-at-eol) t)
  11390. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11391. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11392. (save-match-data
  11393. (unless (outline-next-heading) (throw 'exit nil))
  11394. (while (and (looking-at org-complex-heading-regexp)
  11395. (> (setq l1 (length (match-string 1))) level))
  11396. (setq kwd (and (or recursive (= l1 ltoggle))
  11397. (match-string 2)))
  11398. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11399. (and (listp org-provide-todo-statistics)
  11400. (or (member kwd org-provide-todo-statistics)
  11401. (member kwd org-done-keywords))))
  11402. (setq cnt-all (1+ cnt-all))
  11403. (if (eq org-provide-todo-statistics t)
  11404. (and kwd (setq cnt-all (1+ cnt-all)))))
  11405. (and (member kwd org-done-keywords)
  11406. (setq cnt-done (1+ cnt-done)))
  11407. (outline-next-heading)))
  11408. (setq new
  11409. (if is-percent
  11410. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11411. (format "[%d/%d]" cnt-done cnt-all))
  11412. ndel (- (match-end 0) checkbox-beg))
  11413. ;; handle overlays when updating cookie from column view
  11414. (when (setq ov (car (overlays-at checkbox-beg)))
  11415. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11416. (delete-overlay ov))
  11417. (goto-char checkbox-beg)
  11418. (insert new)
  11419. (delete-region (point) (+ (point) ndel))
  11420. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11421. (when ov (move-overlay ov ovs ove)))
  11422. (when cookie-present
  11423. (run-hook-with-args 'org-after-todo-statistics-hook
  11424. cnt-done (- cnt-all cnt-done))))))
  11425. (run-hooks 'org-todo-statistics-hook)))
  11426. (defvar org-after-todo-statistics-hook nil
  11427. "Hook that is called after a TODO statistics cookie has been updated.
  11428. Each function is called with two arguments: the number of not-done entries
  11429. and the number of done entries.
  11430. For example, the following function, when added to this hook, will switch
  11431. an entry to DONE when all children are done, and back to TODO when new
  11432. entries are set to a TODO status. Note that this hook is only called
  11433. when there is a statistics cookie in the headline!
  11434. (defun org-summary-todo (n-done n-not-done)
  11435. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11436. (let (org-log-done org-log-states) ; turn off logging
  11437. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11438. ")
  11439. (defvar org-todo-statistics-hook nil
  11440. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11441. This hook runs even if there is no statistics cookie present, in which case
  11442. `org-after-todo-statistics-hook' would not run.")
  11443. (defun org-todo-trigger-tag-changes (state)
  11444. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11445. (let ((l org-todo-state-tags-triggers)
  11446. changes)
  11447. (when (or (not state) (equal state ""))
  11448. (setq changes (append changes (cdr (assoc "" l)))))
  11449. (when (and (stringp state) (> (length state) 0))
  11450. (setq changes (append changes (cdr (assoc state l)))))
  11451. (when (member state org-not-done-keywords)
  11452. (setq changes (append changes (cdr (assoc 'todo l)))))
  11453. (when (member state org-done-keywords)
  11454. (setq changes (append changes (cdr (assoc 'done l)))))
  11455. (dolist (c changes)
  11456. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11457. (defun org-local-logging (value)
  11458. "Get logging settings from a property VALUE."
  11459. (let* (words w a)
  11460. ;; directly set the variables, they are already local.
  11461. (setq org-log-done nil
  11462. org-log-repeat nil
  11463. org-todo-log-states nil)
  11464. (setq words (org-split-string value))
  11465. (while (setq w (pop words))
  11466. (cond
  11467. ((setq a (assoc w org-startup-options))
  11468. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11469. (set (nth 1 a) (nth 2 a))))
  11470. ((setq a (org-extract-log-state-settings w))
  11471. (and (member (car a) org-todo-keywords-1)
  11472. (push a org-todo-log-states)))))))
  11473. (defun org-get-todo-sequence-head (kwd)
  11474. "Return the head of the TODO sequence to which KWD belongs.
  11475. If KWD is not set, check if there is a text property remembering the
  11476. right sequence."
  11477. (let (p)
  11478. (cond
  11479. ((not kwd)
  11480. (or (get-text-property (point-at-bol) 'org-todo-head)
  11481. (progn
  11482. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11483. nil (point-at-eol)))
  11484. (get-text-property p 'org-todo-head))))
  11485. ((not (member kwd org-todo-keywords-1))
  11486. (car org-todo-keywords-1))
  11487. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11488. (defun org-fast-todo-selection ()
  11489. "Fast TODO keyword selection with single keys.
  11490. Returns the new TODO keyword, or nil if no state change should occur."
  11491. (let* ((fulltable org-todo-key-alist)
  11492. (done-keywords org-done-keywords) ;; needed for the faces.
  11493. (maxlen (apply 'max (mapcar
  11494. (lambda (x)
  11495. (if (stringp (car x)) (string-width (car x)) 0))
  11496. fulltable)))
  11497. (expert nil)
  11498. (fwidth (+ maxlen 3 1 3))
  11499. (ncol (/ (- (window-width) 4) fwidth))
  11500. tg cnt e c tbl
  11501. groups ingroup)
  11502. (save-excursion
  11503. (save-window-excursion
  11504. (if expert
  11505. (set-buffer (get-buffer-create " *Org todo*"))
  11506. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11507. (erase-buffer)
  11508. (org-set-local 'org-done-keywords done-keywords)
  11509. (setq tbl fulltable cnt 0)
  11510. (while (setq e (pop tbl))
  11511. (cond
  11512. ((equal e '(:startgroup))
  11513. (push '() groups) (setq ingroup t)
  11514. (when (not (= cnt 0))
  11515. (setq cnt 0)
  11516. (insert "\n"))
  11517. (insert "{ "))
  11518. ((equal e '(:endgroup))
  11519. (setq ingroup nil cnt 0)
  11520. (insert "}\n"))
  11521. ((equal e '(:newline))
  11522. (when (not (= cnt 0))
  11523. (setq cnt 0)
  11524. (insert "\n")
  11525. (setq e (car tbl))
  11526. (while (equal (car tbl) '(:newline))
  11527. (insert "\n")
  11528. (setq tbl (cdr tbl)))))
  11529. (t
  11530. (setq tg (car e) c (cdr e))
  11531. (if ingroup (push tg (car groups)))
  11532. (setq tg (org-add-props tg nil 'face
  11533. (org-get-todo-face tg)))
  11534. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11535. (insert "[" c "] " tg (make-string
  11536. (- fwidth 4 (length tg)) ?\ ))
  11537. (when (= (setq cnt (1+ cnt)) ncol)
  11538. (insert "\n")
  11539. (if ingroup (insert " "))
  11540. (setq cnt 0)))))
  11541. (insert "\n")
  11542. (goto-char (point-min))
  11543. (if (not expert) (org-fit-window-to-buffer))
  11544. (message "[a-z..]:Set [SPC]:clear")
  11545. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11546. (cond
  11547. ((or (= c ?\C-g)
  11548. (and (= c ?q) (not (rassoc c fulltable))))
  11549. (setq quit-flag t))
  11550. ((= c ?\ ) nil)
  11551. ((setq e (rassoc c fulltable) tg (car e))
  11552. tg)
  11553. (t (setq quit-flag t)))))))
  11554. (defun org-entry-is-todo-p ()
  11555. (member (org-get-todo-state) org-not-done-keywords))
  11556. (defun org-entry-is-done-p ()
  11557. (member (org-get-todo-state) org-done-keywords))
  11558. (defun org-get-todo-state ()
  11559. "Return the TODO keyword of the current subtree."
  11560. (save-excursion
  11561. (org-back-to-heading t)
  11562. (and (looking-at org-todo-line-regexp)
  11563. (match-end 2)
  11564. (match-string 2))))
  11565. (defun org-at-date-range-p (&optional inactive-ok)
  11566. "Is the cursor inside a date range?"
  11567. (interactive)
  11568. (save-excursion
  11569. (catch 'exit
  11570. (let ((pos (point)))
  11571. (skip-chars-backward "^[<\r\n")
  11572. (skip-chars-backward "<[")
  11573. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11574. (>= (match-end 0) pos)
  11575. (throw 'exit t))
  11576. (skip-chars-backward "^<[\r\n")
  11577. (skip-chars-backward "<[")
  11578. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11579. (>= (match-end 0) pos)
  11580. (throw 'exit t)))
  11581. nil)))
  11582. (defun org-get-repeat (&optional tagline)
  11583. "Check if there is a deadline/schedule with repeater in this entry."
  11584. (save-match-data
  11585. (save-excursion
  11586. (org-back-to-heading t)
  11587. (and (re-search-forward (if tagline
  11588. (concat tagline "\\s-*" org-repeat-re)
  11589. org-repeat-re)
  11590. (org-entry-end-position) t)
  11591. (match-string-no-properties 1)))))
  11592. (defvar org-last-changed-timestamp)
  11593. (defvar org-last-inserted-timestamp)
  11594. (defvar org-log-post-message)
  11595. (defvar org-log-note-purpose)
  11596. (defvar org-log-note-how)
  11597. (defvar org-log-note-extra)
  11598. (defun org-auto-repeat-maybe (done-word)
  11599. "Check if the current headline contains a repeated deadline/schedule.
  11600. If yes, set TODO state back to what it was and change the base date
  11601. of repeating deadline/scheduled time stamps to new date.
  11602. This function is run automatically after each state change to a DONE state."
  11603. ;; last-state is dynamically scoped into this function
  11604. (let* ((repeat (org-get-repeat))
  11605. (aa (assoc org-last-state org-todo-kwd-alist))
  11606. (interpret (nth 1 aa))
  11607. (head (nth 2 aa))
  11608. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11609. (msg "Entry repeats: ")
  11610. (org-log-done nil)
  11611. (org-todo-log-states nil)
  11612. re type n what ts time to-state)
  11613. (when repeat
  11614. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11615. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11616. org-todo-repeat-to-state))
  11617. (unless (and to-state (member to-state org-todo-keywords-1))
  11618. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11619. (org-todo to-state)
  11620. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11621. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11622. (org-time-stamp-format t t))))
  11623. (when org-log-repeat
  11624. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11625. (memq 'org-add-log-note post-command-hook))
  11626. ;; OK, we are already setup for some record
  11627. (if (eq org-log-repeat 'note)
  11628. ;; make sure we take a note, not only a time stamp
  11629. (setq org-log-note-how 'note))
  11630. ;; Set up for taking a record
  11631. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11632. org-last-state
  11633. 'findpos org-log-repeat)))
  11634. (org-back-to-heading t)
  11635. (org-add-planning-info nil nil 'closed)
  11636. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11637. org-deadline-time-regexp "\\)\\|\\("
  11638. org-ts-regexp "\\)"))
  11639. (while (re-search-forward
  11640. re (save-excursion (outline-next-heading) (point)) t)
  11641. (setq type (if (match-end 1) org-scheduled-string
  11642. (if (match-end 3) org-deadline-string "Plain:"))
  11643. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11644. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11645. (setq n (string-to-number (match-string 2 ts))
  11646. what (match-string 3 ts))
  11647. (if (equal what "w") (setq n (* n 7) what "d"))
  11648. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11649. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11650. ;; Preparation, see if we need to modify the start date for the change
  11651. (when (match-end 1)
  11652. (setq time (save-match-data (org-time-string-to-time ts)))
  11653. (cond
  11654. ((equal (match-string 1 ts) ".")
  11655. ;; Shift starting date to today
  11656. (org-timestamp-change
  11657. (- (org-today) (time-to-days time))
  11658. 'day))
  11659. ((equal (match-string 1 ts) "+")
  11660. (let ((nshiftmax 10) (nshift 0))
  11661. (while (or (= nshift 0)
  11662. (<= (time-to-days time)
  11663. (time-to-days (current-time))))
  11664. (when (= (incf nshift) nshiftmax)
  11665. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11666. (error "Abort")))
  11667. (org-timestamp-change n (cdr (assoc what whata)))
  11668. (org-at-timestamp-p t)
  11669. (setq ts (match-string 1))
  11670. (setq time (save-match-data (org-time-string-to-time ts)))))
  11671. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11672. ;; rematch, so that we have everything in place for the real shift
  11673. (org-at-timestamp-p t)
  11674. (setq ts (match-string 1))
  11675. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11676. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11677. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11678. (setq org-log-post-message msg)
  11679. (message "%s" msg))))
  11680. (defun org-show-todo-tree (arg)
  11681. "Make a compact tree which shows all headlines marked with TODO.
  11682. The tree will show the lines where the regexp matches, and all higher
  11683. headlines above the match.
  11684. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11685. With a numeric prefix N, construct a sparse tree for the Nth element
  11686. of `org-todo-keywords-1'."
  11687. (interactive "P")
  11688. (let ((case-fold-search nil)
  11689. (kwd-re
  11690. (cond ((null arg) org-not-done-regexp)
  11691. ((equal arg '(4))
  11692. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11693. (mapcar 'list org-todo-keywords-1))))
  11694. (concat "\\("
  11695. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11696. "\\)\\>")))
  11697. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11698. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11699. org-todo-keywords-1)))
  11700. (t (user-error "Invalid prefix argument: %s" arg)))))
  11701. (message "%d TODO entries found"
  11702. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11703. (defun org-deadline (arg &optional time)
  11704. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11705. With one universal prefix argument, remove any deadline from the item.
  11706. With two universal prefix arguments, prompt for a warning delay.
  11707. With argument TIME, set the deadline at the corresponding date. TIME
  11708. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11709. (interactive "P")
  11710. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11711. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11712. 'region-start-level 'region))
  11713. org-loop-over-headlines-in-active-region)
  11714. (org-map-entries
  11715. `(org-deadline ',arg ,time)
  11716. org-loop-over-headlines-in-active-region
  11717. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11718. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11719. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11720. (repeater (and old-date
  11721. (string-match
  11722. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11723. old-date)
  11724. (match-string 1 old-date))))
  11725. (cond
  11726. ((equal arg '(4))
  11727. (when (and old-date org-log-redeadline)
  11728. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11729. org-log-redeadline))
  11730. (org-remove-timestamp-with-keyword org-deadline-string)
  11731. (message "Item no longer has a deadline."))
  11732. ((equal arg '(16))
  11733. (save-excursion
  11734. (if (re-search-forward
  11735. org-deadline-time-regexp
  11736. (save-excursion (outline-next-heading) (point)) t)
  11737. (let* ((rpl0 (match-string 1))
  11738. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11739. (replace-match
  11740. (concat org-deadline-string
  11741. " <" rpl
  11742. (format " -%dd"
  11743. (abs
  11744. (- (time-to-days
  11745. (save-match-data
  11746. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11747. (time-to-days old-date-time))))
  11748. ">") t t))
  11749. (user-error "No deadline information to update"))))
  11750. (t
  11751. (org-add-planning-info 'deadline time 'closed)
  11752. (when (and old-date org-log-redeadline
  11753. (not (equal old-date
  11754. (substring org-last-inserted-timestamp 1 -1))))
  11755. (org-add-log-setup 'redeadline nil old-date 'findpos
  11756. org-log-redeadline))
  11757. (when repeater
  11758. (save-excursion
  11759. (org-back-to-heading t)
  11760. (when (re-search-forward (concat org-deadline-string " "
  11761. org-last-inserted-timestamp)
  11762. (save-excursion
  11763. (outline-next-heading) (point)) t)
  11764. (goto-char (1- (match-end 0)))
  11765. (insert " " repeater)
  11766. (setq org-last-inserted-timestamp
  11767. (concat (substring org-last-inserted-timestamp 0 -1)
  11768. " " repeater
  11769. (substring org-last-inserted-timestamp -1))))))
  11770. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11771. (defun org-schedule (arg &optional time)
  11772. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11773. With one universal prefix argument, remove any scheduling date from the item.
  11774. With two universal prefix arguments, prompt for a delay cookie.
  11775. With argument TIME, scheduled at the corresponding date. TIME can
  11776. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11777. (interactive "P")
  11778. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11779. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11780. 'region-start-level 'region))
  11781. org-loop-over-headlines-in-active-region)
  11782. (org-map-entries
  11783. `(org-schedule ',arg ,time)
  11784. org-loop-over-headlines-in-active-region
  11785. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11786. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11787. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11788. (repeater (and old-date
  11789. (string-match
  11790. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11791. old-date)
  11792. (match-string 1 old-date))))
  11793. (cond
  11794. ((equal arg '(4))
  11795. (progn
  11796. (when (and old-date org-log-reschedule)
  11797. (org-add-log-setup 'delschedule nil old-date 'findpos
  11798. org-log-reschedule))
  11799. (org-remove-timestamp-with-keyword org-scheduled-string)
  11800. (message "Item is no longer scheduled.")))
  11801. ((equal arg '(16))
  11802. (save-excursion
  11803. (if (re-search-forward
  11804. org-scheduled-time-regexp
  11805. (save-excursion (outline-next-heading) (point)) t)
  11806. (let* ((rpl0 (match-string 1))
  11807. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11808. (replace-match
  11809. (concat org-scheduled-string
  11810. " <" rpl
  11811. (format " -%dd"
  11812. (abs
  11813. (- (time-to-days
  11814. (save-match-data
  11815. (org-read-date nil t nil "Delay until" old-date-time)))
  11816. (time-to-days old-date-time))))
  11817. ">") t t))
  11818. (user-error "No scheduled information to update"))))
  11819. (t
  11820. (org-add-planning-info 'scheduled time 'closed)
  11821. (when (and old-date org-log-reschedule
  11822. (not (equal old-date
  11823. (substring org-last-inserted-timestamp 1 -1))))
  11824. (org-add-log-setup 'reschedule nil old-date 'findpos
  11825. org-log-reschedule))
  11826. (when repeater
  11827. (save-excursion
  11828. (org-back-to-heading t)
  11829. (when (re-search-forward (concat org-scheduled-string " "
  11830. org-last-inserted-timestamp)
  11831. (save-excursion
  11832. (outline-next-heading) (point)) t)
  11833. (goto-char (1- (match-end 0)))
  11834. (insert " " repeater)
  11835. (setq org-last-inserted-timestamp
  11836. (concat (substring org-last-inserted-timestamp 0 -1)
  11837. " " repeater
  11838. (substring org-last-inserted-timestamp -1))))))
  11839. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11840. (defun org-get-scheduled-time (pom &optional inherit)
  11841. "Get the scheduled time as a time tuple, of a format suitable
  11842. for calling org-schedule with, or if there is no scheduling,
  11843. returns nil."
  11844. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11845. (when time
  11846. (apply 'encode-time (org-parse-time-string time)))))
  11847. (defun org-get-deadline-time (pom &optional inherit)
  11848. "Get the deadline as a time tuple, of a format suitable for
  11849. calling org-deadline with, or if there is no scheduling, returns
  11850. nil."
  11851. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11852. (when time
  11853. (apply 'encode-time (org-parse-time-string time)))))
  11854. (defun org-remove-timestamp-with-keyword (keyword)
  11855. "Remove all time stamps with KEYWORD in the current entry."
  11856. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11857. beg)
  11858. (save-excursion
  11859. (org-back-to-heading t)
  11860. (setq beg (point))
  11861. (outline-next-heading)
  11862. (while (re-search-backward re beg t)
  11863. (replace-match "")
  11864. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11865. (equal (char-before) ?\ ))
  11866. (backward-delete-char 1)
  11867. (if (string-match "^[ \t]*$" (buffer-substring
  11868. (point-at-bol) (point-at-eol)))
  11869. (delete-region (point-at-bol)
  11870. (min (point-max) (1+ (point-at-eol))))))))))
  11871. (defun org-add-planning-info (what &optional time &rest remove)
  11872. "Insert new timestamp with keyword in the line directly after the headline.
  11873. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11874. If non is given, the user is prompted for a date.
  11875. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11876. be removed."
  11877. (interactive)
  11878. (let (org-time-was-given org-end-time-was-given ts
  11879. end default-time default-input)
  11880. (catch 'exit
  11881. (when (and (memq what '(scheduled deadline))
  11882. (or (not time)
  11883. (and (stringp time)
  11884. (string-match "^[-+]+[0-9]" time))))
  11885. ;; Try to get a default date/time from existing timestamp
  11886. (save-excursion
  11887. (org-back-to-heading t)
  11888. (setq end (save-excursion (outline-next-heading) (point)))
  11889. (when (re-search-forward (if (eq what 'scheduled)
  11890. org-scheduled-time-regexp
  11891. org-deadline-time-regexp)
  11892. end t)
  11893. (setq ts (match-string 1)
  11894. default-time
  11895. (apply 'encode-time (org-parse-time-string ts))
  11896. default-input (and ts (org-get-compact-tod ts))))))
  11897. (when what
  11898. (setq time
  11899. (if (stringp time)
  11900. ;; This is a string (relative or absolute), set proper date
  11901. (apply 'encode-time
  11902. (org-read-date-analyze
  11903. time default-time (decode-time default-time)))
  11904. ;; If necessary, get the time from the user
  11905. (or time (org-read-date nil 'to-time nil nil
  11906. default-time default-input)))))
  11907. (when (and org-insert-labeled-timestamps-at-point
  11908. (member what '(scheduled deadline)))
  11909. (insert
  11910. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11911. (org-insert-time-stamp time org-time-was-given
  11912. nil nil nil (list org-end-time-was-given))
  11913. (setq what nil))
  11914. (save-excursion
  11915. (save-restriction
  11916. (let (col list elt ts buffer-invisibility-spec)
  11917. (org-back-to-heading t)
  11918. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11919. (goto-char (match-end 1))
  11920. (setq col (current-column))
  11921. (goto-char (match-end 0))
  11922. (if (eobp) (insert "\n") (forward-char 1))
  11923. (when (and (not what)
  11924. (not (looking-at
  11925. (concat "[ \t]*"
  11926. org-keyword-time-not-clock-regexp))))
  11927. ;; Nothing to add, nothing to remove...... :-)
  11928. (throw 'exit nil))
  11929. (if (and (not (looking-at org-outline-regexp))
  11930. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11931. "[^\r\n]*"))
  11932. (not (equal (match-string 1) org-clock-string)))
  11933. (narrow-to-region (match-beginning 0) (match-end 0))
  11934. (insert-before-markers "\n")
  11935. (backward-char 1)
  11936. (narrow-to-region (point) (point))
  11937. (and org-adapt-indentation (org-indent-to-column col)))
  11938. ;; Check if we have to remove something.
  11939. (setq list (cons what remove))
  11940. (while list
  11941. (setq elt (pop list))
  11942. (when (or (and (eq elt 'scheduled)
  11943. (re-search-forward org-scheduled-time-regexp nil t))
  11944. (and (eq elt 'deadline)
  11945. (re-search-forward org-deadline-time-regexp nil t))
  11946. (and (eq elt 'closed)
  11947. (re-search-forward org-closed-time-regexp nil t)))
  11948. (replace-match "")
  11949. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11950. (and (looking-at "[ \t]+") (replace-match ""))
  11951. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11952. (when what
  11953. (insert
  11954. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11955. (cond ((eq what 'scheduled) org-scheduled-string)
  11956. ((eq what 'deadline) org-deadline-string)
  11957. ((eq what 'closed) org-closed-string))
  11958. " ")
  11959. (setq ts (org-insert-time-stamp
  11960. time
  11961. (or org-time-was-given
  11962. (and (eq what 'closed) org-log-done-with-time))
  11963. (eq what 'closed)
  11964. nil nil (list org-end-time-was-given)))
  11965. (insert
  11966. (if (not (or (bolp) (eq (char-before) ?\ )
  11967. (memq (char-after) '(32 10))
  11968. (eobp))) " " ""))
  11969. (end-of-line 1))
  11970. (goto-char (point-min))
  11971. (widen)
  11972. (if (and (looking-at "[ \t]*\n")
  11973. (equal (char-before) ?\n))
  11974. (delete-region (1- (point)) (point-at-eol)))
  11975. ts))))))
  11976. (defvar org-log-note-marker (make-marker))
  11977. (defvar org-log-note-purpose nil)
  11978. (defvar org-log-note-state nil)
  11979. (defvar org-log-note-previous-state nil)
  11980. (defvar org-log-note-how nil)
  11981. (defvar org-log-note-extra nil)
  11982. (defvar org-log-note-window-configuration nil)
  11983. (defvar org-log-note-return-to (make-marker))
  11984. (defvar org-log-note-effective-time nil
  11985. "Remembered current time so that dynamically scoped
  11986. `org-extend-today-until' affects tha timestamps in state change
  11987. log")
  11988. (defvar org-log-post-message nil
  11989. "Message to be displayed after a log note has been stored.
  11990. The auto-repeater uses this.")
  11991. (defun org-add-note ()
  11992. "Add a note to the current entry.
  11993. This is done in the same way as adding a state change note."
  11994. (interactive)
  11995. (org-add-log-setup 'note nil nil 'findpos nil))
  11996. (defvar org-property-end-re)
  11997. (defun org-add-log-setup (&optional purpose state prev-state
  11998. findpos how extra)
  11999. "Set up the post command hook to take a note.
  12000. If this is about to TODO state change, the new state is expected in STATE.
  12001. When FINDPOS is non-nil, find the correct position for the note in
  12002. the current entry. If not, assume that it can be inserted at point.
  12003. HOW is an indicator what kind of note should be created.
  12004. EXTRA is additional text that will be inserted into the notes buffer."
  12005. (let* ((org-log-into-drawer (org-log-into-drawer))
  12006. (drawer (cond ((stringp org-log-into-drawer)
  12007. org-log-into-drawer)
  12008. (org-log-into-drawer "LOGBOOK"))))
  12009. (save-restriction
  12010. (save-excursion
  12011. (when findpos
  12012. (org-back-to-heading t)
  12013. (narrow-to-region (point) (save-excursion
  12014. (outline-next-heading) (point)))
  12015. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12016. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12017. "[^\r\n]*\\)?"))
  12018. (goto-char (match-end 0))
  12019. (cond
  12020. (drawer
  12021. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12022. nil t)
  12023. (progn
  12024. (goto-char (match-end 0))
  12025. (or org-log-states-order-reversed
  12026. (and (re-search-forward org-property-end-re nil t)
  12027. (goto-char (1- (match-beginning 0))))))
  12028. (insert "\n:" drawer ":\n:END:")
  12029. (beginning-of-line 0)
  12030. (org-indent-line)
  12031. (beginning-of-line 2)
  12032. (org-indent-line)
  12033. (end-of-line 0)))
  12034. ((and org-log-state-notes-insert-after-drawers
  12035. (save-excursion
  12036. (forward-line) (looking-at org-drawer-regexp)))
  12037. (forward-line)
  12038. (while (looking-at org-drawer-regexp)
  12039. (goto-char (match-end 0))
  12040. (re-search-forward org-property-end-re (point-max) t)
  12041. (forward-line))
  12042. (forward-line -1)))
  12043. (unless org-log-states-order-reversed
  12044. (and (= (char-after) ?\n) (forward-char 1))
  12045. (org-skip-over-state-notes)
  12046. (skip-chars-backward " \t\n\r")))
  12047. (move-marker org-log-note-marker (point))
  12048. (setq org-log-note-purpose purpose
  12049. org-log-note-state state
  12050. org-log-note-previous-state prev-state
  12051. org-log-note-how how
  12052. org-log-note-extra extra
  12053. org-log-note-effective-time (org-current-effective-time))
  12054. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12055. (defun org-skip-over-state-notes ()
  12056. "Skip past the list of State notes in an entry."
  12057. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12058. (when (ignore-errors (goto-char (org-in-item-p)))
  12059. (let* ((struct (org-list-struct))
  12060. (prevs (org-list-prevs-alist struct)))
  12061. (while (looking-at "[ \t]*- State")
  12062. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12063. (org-list-get-item-end (point) struct)))))))
  12064. (defun org-add-log-note (&optional purpose)
  12065. "Pop up a window for taking a note, and add this note later at point."
  12066. (remove-hook 'post-command-hook 'org-add-log-note)
  12067. (setq org-log-note-window-configuration (current-window-configuration))
  12068. (delete-other-windows)
  12069. (move-marker org-log-note-return-to (point))
  12070. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12071. (goto-char org-log-note-marker)
  12072. (org-switch-to-buffer-other-window "*Org Note*")
  12073. (erase-buffer)
  12074. (if (memq org-log-note-how '(time state))
  12075. (let (current-prefix-arg) (org-store-log-note))
  12076. (let ((org-inhibit-startup t)) (org-mode))
  12077. (insert (format "# Insert note for %s.
  12078. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12079. (cond
  12080. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12081. ((eq org-log-note-purpose 'done) "closed todo item")
  12082. ((eq org-log-note-purpose 'state)
  12083. (format "state change from \"%s\" to \"%s\""
  12084. (or org-log-note-previous-state "")
  12085. (or org-log-note-state "")))
  12086. ((eq org-log-note-purpose 'reschedule)
  12087. "rescheduling")
  12088. ((eq org-log-note-purpose 'delschedule)
  12089. "no longer scheduled")
  12090. ((eq org-log-note-purpose 'redeadline)
  12091. "changing deadline")
  12092. ((eq org-log-note-purpose 'deldeadline)
  12093. "removing deadline")
  12094. ((eq org-log-note-purpose 'refile)
  12095. "refiling")
  12096. ((eq org-log-note-purpose 'note)
  12097. "this entry")
  12098. (t (error "This should not happen")))))
  12099. (if org-log-note-extra (insert org-log-note-extra))
  12100. (org-set-local 'org-finish-function 'org-store-log-note)
  12101. (run-hooks 'org-log-buffer-setup-hook)))
  12102. (defvar org-note-abort nil) ; dynamically scoped
  12103. (defun org-store-log-note ()
  12104. "Finish taking a log note, and insert it to where it belongs."
  12105. (let ((txt (buffer-string)))
  12106. (kill-buffer (current-buffer))
  12107. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12108. lines ind bul)
  12109. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12110. (setq txt (replace-match "" t t txt)))
  12111. (if (string-match "\\s-+\\'" txt)
  12112. (setq txt (replace-match "" t t txt)))
  12113. (setq lines (org-split-string txt "\n"))
  12114. (when (and note (string-match "\\S-" note))
  12115. (setq note
  12116. (org-replace-escapes
  12117. note
  12118. (list (cons "%u" (user-login-name))
  12119. (cons "%U" user-full-name)
  12120. (cons "%t" (format-time-string
  12121. (org-time-stamp-format 'long 'inactive)
  12122. org-log-note-effective-time))
  12123. (cons "%T" (format-time-string
  12124. (org-time-stamp-format 'long nil)
  12125. org-log-note-effective-time))
  12126. (cons "%d" (format-time-string
  12127. (org-time-stamp-format nil 'inactive)
  12128. org-log-note-effective-time))
  12129. (cons "%D" (format-time-string
  12130. (org-time-stamp-format nil nil)
  12131. org-log-note-effective-time))
  12132. (cons "%s" (if org-log-note-state
  12133. (concat "\"" org-log-note-state "\"")
  12134. ""))
  12135. (cons "%S" (if org-log-note-previous-state
  12136. (concat "\"" org-log-note-previous-state "\"")
  12137. "\"\"")))))
  12138. (if lines (setq note (concat note " \\\\")))
  12139. (push note lines))
  12140. (when (or current-prefix-arg org-note-abort)
  12141. (when org-log-into-drawer
  12142. (org-remove-empty-drawer-at
  12143. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12144. org-log-note-marker))
  12145. (setq lines nil))
  12146. (when lines
  12147. (with-current-buffer (marker-buffer org-log-note-marker)
  12148. (save-excursion
  12149. (goto-char org-log-note-marker)
  12150. (move-marker org-log-note-marker nil)
  12151. (end-of-line 1)
  12152. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12153. (setq ind (save-excursion
  12154. (if (ignore-errors (goto-char (org-in-item-p)))
  12155. (let ((struct (org-list-struct)))
  12156. (org-list-get-ind
  12157. (org-list-get-top-point struct) struct))
  12158. (skip-chars-backward " \r\t\n")
  12159. (cond
  12160. ((and (org-at-heading-p)
  12161. org-adapt-indentation)
  12162. (1+ (org-current-level)))
  12163. ((org-at-heading-p) 0)
  12164. (t (org-get-indentation))))))
  12165. (setq bul (org-list-bullet-string "-"))
  12166. (org-indent-line-to ind)
  12167. (insert bul (pop lines))
  12168. (let ((ind-body (+ (length bul) ind)))
  12169. (while lines
  12170. (insert "\n")
  12171. (org-indent-line-to ind-body)
  12172. (insert (pop lines))))
  12173. (message "Note stored")
  12174. (org-back-to-heading t)
  12175. (org-cycle-hide-drawers 'children))
  12176. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12177. ;; from within `org-add-log-note' because `buffer-undo-list'
  12178. ;; is then modified outside of `org-with-remote-undo'.
  12179. (when (eq this-command 'org-agenda-todo)
  12180. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12181. ;; Don't add undo information when called from `org-agenda-todo'
  12182. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12183. (set-window-configuration org-log-note-window-configuration)
  12184. (with-current-buffer (marker-buffer org-log-note-return-to)
  12185. (goto-char org-log-note-return-to))
  12186. (move-marker org-log-note-return-to nil)
  12187. (and org-log-post-message (message "%s" org-log-post-message))))
  12188. (defun org-remove-empty-drawer-at (drawer pos)
  12189. "Remove an empty drawer DRAWER at position POS.
  12190. POS may also be a marker."
  12191. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12192. (save-excursion
  12193. (save-restriction
  12194. (widen)
  12195. (goto-char pos)
  12196. (if (org-in-regexp
  12197. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12198. (replace-match ""))))))
  12199. (defvar org-ts-type nil)
  12200. (defun org-sparse-tree (&optional arg type)
  12201. "Create a sparse tree, prompt for the details.
  12202. This command can create sparse trees. You first need to select the type
  12203. of match used to create the tree:
  12204. t Show all TODO entries.
  12205. T Show entries with a specific TODO keyword.
  12206. m Show entries selected by a tags/property match.
  12207. p Enter a property name and its value (both with completion on existing
  12208. names/values) and show entries with that property.
  12209. r Show entries matching a regular expression (`/' can be used as well).
  12210. b Show deadlines and scheduled items before a date.
  12211. a Show deadlines and scheduled items after a date.
  12212. d Show deadlines due within `org-deadline-warning-days'.
  12213. D Show deadlines and scheduled items between a date range."
  12214. (interactive "P")
  12215. (let (ans kwd value ts-type)
  12216. (setq type (or type org-sparse-tree-default-date-type))
  12217. (setq org-ts-type type)
  12218. (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"
  12219. (cond ((eq type 'all) "all timestamps")
  12220. ((eq type 'scheduled) "only scheduled")
  12221. ((eq type 'deadline) "only deadline")
  12222. ((eq type 'active) "only active timestamps")
  12223. ((eq type 'inactive) "only inactive timestamps")
  12224. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12225. ((eq type 'closed) "with a closed time-stamp")
  12226. (t "scheduled/deadline")))
  12227. (setq ans (read-char-exclusive))
  12228. (cond
  12229. ((equal ans ?c)
  12230. (org-sparse-tree
  12231. arg (cadr (member type '(scheduled-or-deadline
  12232. all scheduled deadline active inactive closed)))))
  12233. ((equal ans ?d)
  12234. (call-interactively 'org-check-deadlines))
  12235. ((equal ans ?b)
  12236. (call-interactively 'org-check-before-date))
  12237. ((equal ans ?a)
  12238. (call-interactively 'org-check-after-date))
  12239. ((equal ans ?D)
  12240. (call-interactively 'org-check-dates-range))
  12241. ((equal ans ?t)
  12242. (call-interactively 'org-show-todo-tree))
  12243. ((equal ans ?T)
  12244. (org-show-todo-tree '(4)))
  12245. ((member ans '(?T ?m))
  12246. (call-interactively 'org-match-sparse-tree))
  12247. ((member ans '(?p ?P))
  12248. (setq kwd (org-icompleting-read "Property: "
  12249. (mapcar 'list (org-buffer-property-keys))))
  12250. (setq value (org-icompleting-read "Value: "
  12251. (mapcar 'list (org-property-values kwd))))
  12252. (unless (string-match "\\`{.*}\\'" value)
  12253. (setq value (concat "\"" value "\"")))
  12254. (org-match-sparse-tree arg (concat kwd "=" value)))
  12255. ((member ans '(?r ?R ?/))
  12256. (call-interactively 'org-occur))
  12257. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12258. (defvar org-occur-highlights nil
  12259. "List of overlays used for occur matches.")
  12260. (make-variable-buffer-local 'org-occur-highlights)
  12261. (defvar org-occur-parameters nil
  12262. "Parameters of the active org-occur calls.
  12263. This is a list, each call to org-occur pushes as cons cell,
  12264. containing the regular expression and the callback, onto the list.
  12265. The list can contain several entries if `org-occur' has been called
  12266. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12267. will only contain one set of parameters. When the highlights are
  12268. removed (for example with `C-c C-c', or with the next edit (depending
  12269. on `org-remove-highlights-with-change'), this variable is emptied
  12270. as well.")
  12271. (make-variable-buffer-local 'org-occur-parameters)
  12272. (defun org-occur (regexp &optional keep-previous callback)
  12273. "Make a compact tree which shows all matches of REGEXP.
  12274. The tree will show the lines where the regexp matches, and all higher
  12275. headlines above the match. It will also show the heading after the match,
  12276. to make sure editing the matching entry is easy.
  12277. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12278. call to `org-occur' will be kept, to allow stacking of calls to this
  12279. command.
  12280. If CALLBACK is non-nil, it is a function which is called to confirm
  12281. that the match should indeed be shown."
  12282. (interactive "sRegexp: \nP")
  12283. (when (equal regexp "")
  12284. (user-error "Regexp cannot be empty"))
  12285. (unless keep-previous
  12286. (org-remove-occur-highlights nil nil t))
  12287. (push (cons regexp callback) org-occur-parameters)
  12288. (let ((cnt 0))
  12289. (save-excursion
  12290. (goto-char (point-min))
  12291. (if (or (not keep-previous) ; do not want to keep
  12292. (not org-occur-highlights)) ; no previous matches
  12293. ;; hide everything
  12294. (org-overview))
  12295. (while (re-search-forward regexp nil t)
  12296. (when (or (not callback)
  12297. (save-match-data (funcall callback)))
  12298. (setq cnt (1+ cnt))
  12299. (when org-highlight-sparse-tree-matches
  12300. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12301. (org-show-context 'occur-tree))))
  12302. (when org-remove-highlights-with-change
  12303. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12304. nil 'local))
  12305. (unless org-sparse-tree-open-archived-trees
  12306. (org-hide-archived-subtrees (point-min) (point-max)))
  12307. (run-hooks 'org-occur-hook)
  12308. (if (org-called-interactively-p 'interactive)
  12309. (message "%d match(es) for regexp %s" cnt regexp))
  12310. cnt))
  12311. (defun org-occur-next-match (&optional n reset)
  12312. "Function for `next-error-function' to find sparse tree matches.
  12313. N is the number of matches to move, when negative move backwards.
  12314. RESET is entirely ignored - this function always goes back to the
  12315. starting point when no match is found."
  12316. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12317. (search-func (if (< n 0)
  12318. 'previous-single-char-property-change
  12319. 'next-single-char-property-change))
  12320. (n (abs n))
  12321. (pos (point))
  12322. p1)
  12323. (catch 'exit
  12324. (while (setq p1 (funcall search-func (point) 'org-type))
  12325. (when (equal p1 limit)
  12326. (goto-char pos)
  12327. (error "No more matches"))
  12328. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12329. (setq n (1- n))
  12330. (when (= n 0)
  12331. (goto-char p1)
  12332. (throw 'exit (point))))
  12333. (goto-char p1))
  12334. (goto-char p1)
  12335. (error "No more matches"))))
  12336. (defun org-show-context (&optional key)
  12337. "Make sure point and context are visible.
  12338. How much context is shown depends upon the variables
  12339. `org-show-hierarchy-above', `org-show-following-heading',
  12340. `org-show-entry-below' and `org-show-siblings'."
  12341. (let ((heading-p (org-at-heading-p t))
  12342. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12343. (following-p (org-get-alist-option org-show-following-heading key))
  12344. (entry-p (org-get-alist-option org-show-entry-below key))
  12345. (siblings-p (org-get-alist-option org-show-siblings key)))
  12346. ;; Show heading or entry text
  12347. (if (and heading-p (not entry-p))
  12348. (org-flag-heading nil) ; only show the heading
  12349. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12350. (org-show-hidden-entry))) ; show entire entry
  12351. (when following-p
  12352. ;; Show next sibling, or heading below text
  12353. (save-excursion
  12354. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12355. (org-flag-heading nil))))
  12356. (when siblings-p (org-show-siblings))
  12357. (when hierarchy-p
  12358. ;; show all higher headings, possibly with siblings
  12359. (save-excursion
  12360. (while (and (condition-case nil
  12361. (progn (org-up-heading-all 1) t)
  12362. (error nil))
  12363. (not (bobp)))
  12364. (org-flag-heading nil)
  12365. (when siblings-p (org-show-siblings)))))
  12366. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12367. (defvar org-reveal-start-hook nil
  12368. "Hook run before revealing a location.")
  12369. (defun org-reveal (&optional siblings)
  12370. "Show current entry, hierarchy above it, and the following headline.
  12371. This can be used to show a consistent set of context around locations
  12372. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12373. not t for the search context.
  12374. With optional argument SIBLINGS, on each level of the hierarchy all
  12375. siblings are shown. This repairs the tree structure to what it would
  12376. look like when opened with hierarchical calls to `org-cycle'.
  12377. With double optional argument \\[universal-argument] \\[universal-argument], \
  12378. go to the parent and show the
  12379. entire tree."
  12380. (interactive "P")
  12381. (run-hooks 'org-reveal-start-hook)
  12382. (let ((org-show-hierarchy-above t)
  12383. (org-show-following-heading t)
  12384. (org-show-siblings (if siblings t org-show-siblings)))
  12385. (org-show-context nil))
  12386. (when (equal siblings '(16))
  12387. (save-excursion
  12388. (when (org-up-heading-safe)
  12389. (org-show-subtree)
  12390. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12391. (defun org-highlight-new-match (beg end)
  12392. "Highlight from BEG to END and mark the highlight is an occur headline."
  12393. (let ((ov (make-overlay beg end)))
  12394. (overlay-put ov 'face 'secondary-selection)
  12395. (overlay-put ov 'org-type 'org-occur)
  12396. (push ov org-occur-highlights)))
  12397. (defun org-remove-occur-highlights (&optional beg end noremove)
  12398. "Remove the occur highlights from the buffer.
  12399. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12400. from the `before-change-functions' in the current buffer."
  12401. (interactive)
  12402. (unless org-inhibit-highlight-removal
  12403. (mapc 'delete-overlay org-occur-highlights)
  12404. (setq org-occur-highlights nil)
  12405. (setq org-occur-parameters nil)
  12406. (unless noremove
  12407. (remove-hook 'before-change-functions
  12408. 'org-remove-occur-highlights 'local))))
  12409. ;;;; Priorities
  12410. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12411. "Regular expression matching the priority indicator.")
  12412. (defvar org-remove-priority-next-time nil)
  12413. (defun org-priority-up ()
  12414. "Increase the priority of the current item."
  12415. (interactive)
  12416. (org-priority 'up))
  12417. (defun org-priority-down ()
  12418. "Decrease the priority of the current item."
  12419. (interactive)
  12420. (org-priority 'down))
  12421. (defun org-priority (&optional action show)
  12422. "Change the priority of an item.
  12423. ACTION can be `set', `up', `down', or a character."
  12424. (interactive "P")
  12425. (if (equal action '(4))
  12426. (org-show-priority)
  12427. (unless org-enable-priority-commands
  12428. (user-error "Priority commands are disabled"))
  12429. (setq action (or action 'set))
  12430. (let (current new news have remove)
  12431. (save-excursion
  12432. (org-back-to-heading t)
  12433. (if (looking-at org-priority-regexp)
  12434. (setq current (string-to-char (match-string 2))
  12435. have t))
  12436. (cond
  12437. ((eq action 'remove)
  12438. (setq remove t new ?\ ))
  12439. ((or (eq action 'set)
  12440. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12441. (if (not (eq action 'set))
  12442. (setq new action)
  12443. (message "Priority %c-%c, SPC to remove: "
  12444. org-highest-priority org-lowest-priority)
  12445. (save-match-data
  12446. (setq new (read-char-exclusive))))
  12447. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12448. (= (upcase org-lowest-priority) org-lowest-priority))
  12449. (setq new (upcase new)))
  12450. (cond ((equal new ?\ ) (setq remove t))
  12451. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12452. (user-error "Priority must be between `%c' and `%c'"
  12453. org-highest-priority org-lowest-priority))))
  12454. ((eq action 'up)
  12455. (setq new (if have
  12456. (1- current) ; normal cycling
  12457. ;; last priority was empty
  12458. (if (eq last-command this-command)
  12459. org-lowest-priority ; wrap around empty to lowest
  12460. ;; default
  12461. (if org-priority-start-cycle-with-default
  12462. org-default-priority
  12463. (1- org-default-priority))))))
  12464. ((eq action 'down)
  12465. (setq new (if have
  12466. (1+ current) ; normal cycling
  12467. ;; last priority was empty
  12468. (if (eq last-command this-command)
  12469. org-highest-priority ; wrap around empty to highest
  12470. ;; default
  12471. (if org-priority-start-cycle-with-default
  12472. org-default-priority
  12473. (1+ org-default-priority))))))
  12474. (t (user-error "Invalid action")))
  12475. (if (or (< (upcase new) org-highest-priority)
  12476. (> (upcase new) org-lowest-priority))
  12477. (if (and (memq action '(up down))
  12478. (not have) (not (eq last-command this-command)))
  12479. ;; `new' is from default priority
  12480. (error
  12481. "The default can not be set, see `org-default-priority' why")
  12482. ;; normal cycling: `new' is beyond highest/lowest priority
  12483. ;; and is wrapped around to the empty priority
  12484. (setq remove t)))
  12485. (setq news (format "%c" new))
  12486. (if have
  12487. (if remove
  12488. (replace-match "" t t nil 1)
  12489. (replace-match news t t nil 2))
  12490. (if remove
  12491. (user-error "No priority cookie found in line")
  12492. (let ((case-fold-search nil))
  12493. (looking-at org-todo-line-regexp))
  12494. (if (match-end 2)
  12495. (progn
  12496. (goto-char (match-end 2))
  12497. (insert " [#" news "]"))
  12498. (goto-char (match-beginning 3))
  12499. (insert "[#" news "] "))))
  12500. (org-preserve-lc (org-set-tags nil 'align)))
  12501. (if remove
  12502. (message "Priority removed")
  12503. (message "Priority of current item set to %s" news)))))
  12504. (defun org-show-priority ()
  12505. "Show the priority of the current item.
  12506. This priority is composed of the main priority given with the [#A] cookies,
  12507. and by additional input from the age of a schedules or deadline entry."
  12508. (interactive)
  12509. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12510. (org-get-at-bol 'priority)
  12511. (save-excursion
  12512. (save-match-data
  12513. (beginning-of-line)
  12514. (and (looking-at org-heading-regexp)
  12515. (org-get-priority (match-string 0))))))))
  12516. (message "Priority is %d" (if pri pri -1000))))
  12517. (defun org-get-priority (s)
  12518. "Find priority cookie and return priority."
  12519. (save-match-data
  12520. (if (functionp org-get-priority-function)
  12521. (funcall org-get-priority-function)
  12522. (if (not (string-match org-priority-regexp s))
  12523. (* 1000 (- org-lowest-priority org-default-priority))
  12524. (* 1000 (- org-lowest-priority
  12525. (string-to-char (match-string 2 s))))))))
  12526. ;;;; Tags
  12527. (defvar org-agenda-archives-mode)
  12528. (defvar org-map-continue-from nil
  12529. "Position from where mapping should continue.
  12530. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12531. (defvar org-scanner-tags nil
  12532. "The current tag list while the tags scanner is running.")
  12533. (defvar org-trust-scanner-tags nil
  12534. "Should `org-get-tags-at' use the tags for the scanner.
  12535. This is for internal dynamical scoping only.
  12536. When this is non-nil, the function `org-get-tags-at' will return the value
  12537. of `org-scanner-tags' instead of building the list by itself. This
  12538. can lead to large speed-ups when the tags scanner is used in a file with
  12539. many entries, and when the list of tags is retrieved, for example to
  12540. obtain a list of properties. Building the tags list for each entry in such
  12541. a file becomes an N^2 operation - but with this variable set, it scales
  12542. as N.")
  12543. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12544. "Sca headline tags with inheritance and produce output ACTION.
  12545. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12546. or `agenda' to produce an entry list for an agenda view. It can also be
  12547. a Lisp form or a function that should be called at each matched headline, in
  12548. this case the return value is a list of all return values from these calls.
  12549. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12550. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12551. only lines with a not-done TODO keyword are included in the output.
  12552. This should be the same variable that was scoped into
  12553. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12554. START-LEVEL can be a string with asterisks, reducing the scope to
  12555. headlines matching this string."
  12556. (require 'org-agenda)
  12557. (let* ((re (concat "^"
  12558. (if start-level
  12559. ;; Get the correct level to match
  12560. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12561. org-outline-regexp)
  12562. " *\\(\\<\\("
  12563. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12564. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12565. (props (list 'face 'default
  12566. 'done-face 'org-agenda-done
  12567. 'undone-face 'default
  12568. 'mouse-face 'highlight
  12569. 'org-not-done-regexp org-not-done-regexp
  12570. 'org-todo-regexp org-todo-regexp
  12571. 'org-complex-heading-regexp org-complex-heading-regexp
  12572. 'help-echo
  12573. (format "mouse-2 or RET jump to org file %s"
  12574. (abbreviate-file-name
  12575. (or (buffer-file-name (buffer-base-buffer))
  12576. (buffer-name (buffer-base-buffer)))))))
  12577. (case-fold-search nil)
  12578. (org-map-continue-from nil)
  12579. lspos tags tags-list
  12580. (tags-alist (list (cons 0 org-file-tags)))
  12581. (llast 0) rtn rtn1 level category i txt
  12582. todo marker entry priority)
  12583. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12584. (setq action (list 'lambda nil action)))
  12585. (save-excursion
  12586. (goto-char (point-min))
  12587. (when (eq action 'sparse-tree)
  12588. (org-overview)
  12589. (org-remove-occur-highlights))
  12590. (while (re-search-forward re nil t)
  12591. (setq org-map-continue-from nil)
  12592. (catch :skip
  12593. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12594. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12595. (goto-char (setq lspos (match-beginning 0)))
  12596. (setq level (org-reduced-level (org-outline-level))
  12597. category (org-get-category))
  12598. (setq i llast llast level)
  12599. ;; remove tag lists from same and sublevels
  12600. (while (>= i level)
  12601. (when (setq entry (assoc i tags-alist))
  12602. (setq tags-alist (delete entry tags-alist)))
  12603. (setq i (1- i)))
  12604. ;; add the next tags
  12605. (when tags
  12606. (setq tags (org-split-string tags ":")
  12607. tags-alist
  12608. (cons (cons level tags) tags-alist)))
  12609. ;; compile tags for current headline
  12610. (setq tags-list
  12611. (if org-use-tag-inheritance
  12612. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12613. tags)
  12614. org-scanner-tags tags-list)
  12615. (when org-use-tag-inheritance
  12616. (setcdr (car tags-alist)
  12617. (mapcar (lambda (x)
  12618. (setq x (copy-sequence x))
  12619. (org-add-prop-inherited x))
  12620. (cdar tags-alist))))
  12621. (when (and tags org-use-tag-inheritance
  12622. (or (not (eq t org-use-tag-inheritance))
  12623. org-tags-exclude-from-inheritance))
  12624. ;; selective inheritance, remove uninherited ones
  12625. (setcdr (car tags-alist)
  12626. (org-remove-uninherited-tags (cdar tags-alist))))
  12627. (when (and
  12628. ;; eval matcher only when the todo condition is OK
  12629. (and (or (not todo-only) (member todo org-not-done-keywords))
  12630. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12631. (eval matcher)))
  12632. ;; Call the skipper, but return t if it does not skip,
  12633. ;; so that the `and' form continues evaluating
  12634. (progn
  12635. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12636. t)
  12637. ;; Check if timestamps are deselecting this entry
  12638. (or (not todo-only)
  12639. (and (member todo org-not-done-keywords)
  12640. (or (not org-agenda-tags-todo-honor-ignore-options)
  12641. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12642. ;; select this headline
  12643. (cond
  12644. ((eq action 'sparse-tree)
  12645. (and org-highlight-sparse-tree-matches
  12646. (org-get-heading) (match-end 0)
  12647. (org-highlight-new-match
  12648. (match-beginning 1) (match-end 1)))
  12649. (org-show-context 'tags-tree))
  12650. ((eq action 'agenda)
  12651. (setq txt (org-agenda-format-item
  12652. ""
  12653. (concat
  12654. (if (eq org-tags-match-list-sublevels 'indented)
  12655. (make-string (1- level) ?.) "")
  12656. (org-get-heading))
  12657. level category
  12658. tags-list)
  12659. priority (org-get-priority txt))
  12660. (goto-char lspos)
  12661. (setq marker (org-agenda-new-marker))
  12662. (org-add-props txt props
  12663. 'org-marker marker 'org-hd-marker marker 'org-category category
  12664. 'todo-state todo
  12665. 'priority priority 'type "tagsmatch")
  12666. (push txt rtn))
  12667. ((functionp action)
  12668. (setq org-map-continue-from nil)
  12669. (save-excursion
  12670. (setq rtn1 (funcall action))
  12671. (push rtn1 rtn)))
  12672. (t (user-error "Invalid action")))
  12673. ;; if we are to skip sublevels, jump to end of subtree
  12674. (unless org-tags-match-list-sublevels
  12675. (org-end-of-subtree t)
  12676. (backward-char 1))))
  12677. ;; Get the correct position from where to continue
  12678. (if org-map-continue-from
  12679. (goto-char org-map-continue-from)
  12680. (and (= (point) lspos) (end-of-line 1)))))
  12681. (when (and (eq action 'sparse-tree)
  12682. (not org-sparse-tree-open-archived-trees))
  12683. (org-hide-archived-subtrees (point-min) (point-max)))
  12684. (nreverse rtn)))
  12685. (defun org-remove-uninherited-tags (tags)
  12686. "Remove all tags that are not inherited from the list TAGS."
  12687. (cond
  12688. ((eq org-use-tag-inheritance t)
  12689. (if org-tags-exclude-from-inheritance
  12690. (org-delete-all org-tags-exclude-from-inheritance tags)
  12691. tags))
  12692. ((not org-use-tag-inheritance) nil)
  12693. ((stringp org-use-tag-inheritance)
  12694. (delq nil (mapcar
  12695. (lambda (x)
  12696. (if (and (string-match org-use-tag-inheritance x)
  12697. (not (member x org-tags-exclude-from-inheritance)))
  12698. x nil))
  12699. tags)))
  12700. ((listp org-use-tag-inheritance)
  12701. (delq nil (mapcar
  12702. (lambda (x)
  12703. (if (member x org-use-tag-inheritance) x nil))
  12704. tags)))))
  12705. (defun org-match-sparse-tree (&optional todo-only match)
  12706. "Create a sparse tree according to tags string MATCH.
  12707. MATCH can contain positive and negative selection of tags, like
  12708. \"+WORK+URGENT-WITHBOSS\".
  12709. If optional argument TODO-ONLY is non-nil, only select lines that are
  12710. also TODO lines."
  12711. (interactive "P")
  12712. (org-agenda-prepare-buffers (list (current-buffer)))
  12713. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12714. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12715. (defvar org-cached-props nil)
  12716. (defun org-cached-entry-get (pom property)
  12717. (if (or (eq t org-use-property-inheritance)
  12718. (and (stringp org-use-property-inheritance)
  12719. (string-match org-use-property-inheritance property))
  12720. (and (listp org-use-property-inheritance)
  12721. (member property org-use-property-inheritance)))
  12722. ;; Caching is not possible, check it directly
  12723. (org-entry-get pom property 'inherit)
  12724. ;; Get all properties, so that we can do complicated checks easily
  12725. (cdr (assoc property (or org-cached-props
  12726. (setq org-cached-props
  12727. (org-entry-properties pom)))))))
  12728. (defun org-global-tags-completion-table (&optional files)
  12729. "Return the list of all tags in all agenda buffer/files.
  12730. Optional FILES argument is a list of files which can be used
  12731. instead of the agenda files."
  12732. (save-excursion
  12733. (org-uniquify
  12734. (delq nil
  12735. (apply 'append
  12736. (mapcar
  12737. (lambda (file)
  12738. (set-buffer (find-file-noselect file))
  12739. (append (org-get-buffer-tags)
  12740. (mapcar (lambda (x) (if (stringp (car-safe x))
  12741. (list (car-safe x)) nil))
  12742. org-tag-alist)))
  12743. (if (and files (car files))
  12744. files
  12745. (org-agenda-files))))))))
  12746. (defun org-make-tags-matcher (match)
  12747. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12748. The variable `todo-only' is scoped dynamically into this function.
  12749. It will be set to t if the matcher restricts matching to TODO entries,
  12750. otherwise will not be touched.
  12751. Returns a cons of the selection string MATCH and the constructed
  12752. lisp form implementing the matcher. The matcher is to be evaluated
  12753. at an Org entry, with point on the headline, and returns t if the
  12754. entry matches the selection string MATCH. The returned lisp form
  12755. references two variables with information about the entry, which
  12756. must be bound around the form's evaluation: todo, the TODO keyword
  12757. at the entry (or nil of none); and tags-list, the list of all tags
  12758. at the entry including inherited ones. Additionally, the category
  12759. of the entry (if any) must be specified as the text property
  12760. 'org-category on the headline.
  12761. See also `org-scan-tags'.
  12762. "
  12763. (declare (special todo-only))
  12764. (unless (boundp 'todo-only)
  12765. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12766. (unless match
  12767. ;; Get a new match request, with completion against the global
  12768. ;; tags table and the local tags in current buffer
  12769. (let ((org-last-tags-completion-table
  12770. (org-uniquify
  12771. (delq nil (append (org-get-buffer-tags)
  12772. (org-global-tags-completion-table))))))
  12773. (setq match (org-completing-read-no-i
  12774. "Match: " 'org-tags-completion-function nil nil nil
  12775. 'org-tags-history))))
  12776. ;; Parse the string and create a lisp form
  12777. (let ((match0 match)
  12778. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12779. minus tag mm
  12780. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12781. orterms term orlist re-p str-p level-p level-op time-p
  12782. prop-p pn pv po gv rest)
  12783. ;; Expand group tags
  12784. (setq match (org-tags-expand match))
  12785. (if (string-match "/+" match)
  12786. ;; match contains also a todo-matching request
  12787. (progn
  12788. (setq tagsmatch (substring match 0 (match-beginning 0))
  12789. todomatch (substring match (match-end 0)))
  12790. (if (string-match "^!" todomatch)
  12791. (setq todo-only t todomatch (substring todomatch 1)))
  12792. (if (string-match "^\\s-*$" todomatch)
  12793. (setq todomatch nil)))
  12794. ;; only matching tags
  12795. (setq tagsmatch match todomatch nil))
  12796. ;; Make the tags matcher
  12797. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12798. (setq tagsmatcher t)
  12799. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12800. (while (setq term (pop orterms))
  12801. (while (and (equal (substring term -1) "\\") orterms)
  12802. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12803. (while (string-match re term)
  12804. (setq rest (substring term (match-end 0))
  12805. minus (and (match-end 1)
  12806. (equal (match-string 1 term) "-"))
  12807. tag (save-match-data (replace-regexp-in-string
  12808. "\\\\-" "-"
  12809. (match-string 2 term)))
  12810. re-p (equal (string-to-char tag) ?{)
  12811. level-p (match-end 4)
  12812. prop-p (match-end 5)
  12813. mm (cond
  12814. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12815. (level-p
  12816. (setq level-op (org-op-to-function (match-string 3 term)))
  12817. `(,level-op level ,(string-to-number
  12818. (match-string 4 term))))
  12819. (prop-p
  12820. (setq pn (match-string 5 term)
  12821. po (match-string 6 term)
  12822. pv (match-string 7 term)
  12823. re-p (equal (string-to-char pv) ?{)
  12824. str-p (equal (string-to-char pv) ?\")
  12825. time-p (save-match-data
  12826. (string-match "^\"[[<].*[]>]\"$" pv))
  12827. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12828. (if time-p (setq pv (org-matcher-time pv)))
  12829. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12830. (cond
  12831. ((equal pn "CATEGORY")
  12832. (setq gv '(get-text-property (point) 'org-category)))
  12833. ((equal pn "TODO")
  12834. (setq gv 'todo))
  12835. (t
  12836. (setq gv `(org-cached-entry-get nil ,pn))))
  12837. (if re-p
  12838. (if (eq po 'org<>)
  12839. `(not (string-match ,pv (or ,gv "")))
  12840. `(string-match ,pv (or ,gv "")))
  12841. (if str-p
  12842. `(,po (or ,gv "") ,pv)
  12843. `(,po (string-to-number (or ,gv ""))
  12844. ,(string-to-number pv) ))))
  12845. (t `(member ,tag tags-list)))
  12846. mm (if minus (list 'not mm) mm)
  12847. term rest)
  12848. (push mm tagsmatcher))
  12849. (push (if (> (length tagsmatcher) 1)
  12850. (cons 'and tagsmatcher)
  12851. (car tagsmatcher))
  12852. orlist)
  12853. (setq tagsmatcher nil))
  12854. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12855. (setq tagsmatcher
  12856. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12857. ;; Make the todo matcher
  12858. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12859. (setq todomatcher t)
  12860. (setq orterms (org-split-string todomatch "|") orlist nil)
  12861. (while (setq term (pop orterms))
  12862. (while (string-match re term)
  12863. (setq minus (and (match-end 1)
  12864. (equal (match-string 1 term) "-"))
  12865. kwd (match-string 2 term)
  12866. re-p (equal (string-to-char kwd) ?{)
  12867. term (substring term (match-end 0))
  12868. mm (if re-p
  12869. `(string-match ,(substring kwd 1 -1) todo)
  12870. (list 'equal 'todo kwd))
  12871. mm (if minus (list 'not mm) mm))
  12872. (push mm todomatcher))
  12873. (push (if (> (length todomatcher) 1)
  12874. (cons 'and todomatcher)
  12875. (car todomatcher))
  12876. orlist)
  12877. (setq todomatcher nil))
  12878. (setq todomatcher (if (> (length orlist) 1)
  12879. (cons 'or orlist) (car orlist))))
  12880. ;; Return the string and lisp forms of the matcher
  12881. (setq matcher (if todomatcher
  12882. (list 'and tagsmatcher todomatcher)
  12883. tagsmatcher))
  12884. (when todo-only
  12885. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12886. matcher)))
  12887. (cons match0 matcher)))
  12888. (defun org-tags-expand (match &optional single-as-list downcased)
  12889. "Expand group tags in MATCH.
  12890. This replaces every group tag in MATCH with a regexp tag search.
  12891. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12892. will be replaced like this:
  12893. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12894. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12895. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12896. Replacing by a regexp preserves the structure of the match.
  12897. E.g., this expansion
  12898. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12899. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12900. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12901. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12902. assumed to be a single group tag, and the function will return
  12903. the list of tags in this group.
  12904. When DOWNCASE is non-nil, expand downcased TAGS."
  12905. (if org-group-tags
  12906. (let* ((case-fold-search t)
  12907. (stable org-mode-syntax-table)
  12908. (tal (or org-tag-groups-alist-for-agenda
  12909. org-tag-groups-alist))
  12910. (tal (if downcased
  12911. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12912. (tml (mapcar 'car tal))
  12913. (rtnmatch match) rpl)
  12914. ;; @ and _ are allowed as word-components in tags
  12915. (modify-syntax-entry ?@ "w" stable)
  12916. (modify-syntax-entry ?_ "w" stable)
  12917. (while (and tml
  12918. (string-match
  12919. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12920. (regexp-opt tml) "\\>\\)") rtnmatch))
  12921. (let* ((dir (match-string 1 rtnmatch))
  12922. (tag (match-string 2 rtnmatch))
  12923. (tag (if downcased (downcase tag) tag)))
  12924. (setq tml (delete tag tml))
  12925. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12926. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12927. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12928. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12929. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12930. (if single-as-list
  12931. (or (reverse rpl) (list rtnmatch))
  12932. rtnmatch))
  12933. (if single-as-list (list (if downcased (downcase match) match))
  12934. match)))
  12935. (defun org-op-to-function (op &optional stringp)
  12936. "Turn an operator into the appropriate function."
  12937. (setq op
  12938. (cond
  12939. ((equal op "<" ) '(< string< org-time<))
  12940. ((equal op ">" ) '(> org-string> org-time>))
  12941. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12942. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12943. ((member op '("=" "==")) '(= string= org-time=))
  12944. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12945. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12946. (defun org<> (a b) (not (= a b)))
  12947. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12948. (defun org-string>= (a b) (not (string< a b)))
  12949. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12950. (defun org-string<> (a b) (not (string= a b)))
  12951. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12952. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12953. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12954. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> 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) (org<> a b)))
  12957. (defun org-2ft (s)
  12958. "Convert S to a floating point time.
  12959. If S is already a number, just return it. If it is a string, parse
  12960. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12961. (cond
  12962. ((numberp s) s)
  12963. ((stringp s)
  12964. (condition-case nil
  12965. (float-time (apply 'encode-time (org-parse-time-string s)))
  12966. (error 0.)))
  12967. (t 0.)))
  12968. (defun org-time-today ()
  12969. "Time in seconds today at 0:00.
  12970. Returns the float number of seconds since the beginning of the
  12971. epoch to the beginning of today (00:00)."
  12972. (float-time (apply 'encode-time
  12973. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12974. (defun org-matcher-time (s)
  12975. "Interpret a time comparison value."
  12976. (save-match-data
  12977. (cond
  12978. ((string= s "<now>") (float-time))
  12979. ((string= s "<today>") (org-time-today))
  12980. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12981. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12982. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12983. (+ (org-time-today)
  12984. (* (string-to-number (match-string 1 s))
  12985. (cdr (assoc (match-string 2 s)
  12986. '(("d" . 86400.0) ("w" . 604800.0)
  12987. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12988. (t (org-2ft s)))))
  12989. (defun org-match-any-p (re list)
  12990. "Does re match any element of list?"
  12991. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12992. (delq nil list))
  12993. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12994. (defvar org-tags-overlay (make-overlay 1 1))
  12995. (org-detach-overlay org-tags-overlay)
  12996. (defun org-get-local-tags-at (&optional pos)
  12997. "Get a list of tags defined in the current headline."
  12998. (org-get-tags-at pos 'local))
  12999. (defun org-get-local-tags ()
  13000. "Get a list of tags defined in the current headline."
  13001. (org-get-tags-at nil 'local))
  13002. (defun org-get-tags-at (&optional pos local)
  13003. "Get a list of all headline tags applicable at POS.
  13004. POS defaults to point. If tags are inherited, the list contains
  13005. the targets in the same sequence as the headlines appear, i.e.
  13006. the tags of the current headline come last.
  13007. When LOCAL is non-nil, only return tags from the current headline,
  13008. ignore inherited ones."
  13009. (interactive)
  13010. (if (and org-trust-scanner-tags
  13011. (or (not pos) (equal pos (point)))
  13012. (not local))
  13013. org-scanner-tags
  13014. (let (tags ltags lastpos parent)
  13015. (save-excursion
  13016. (save-restriction
  13017. (widen)
  13018. (goto-char (or pos (point)))
  13019. (save-match-data
  13020. (catch 'done
  13021. (condition-case nil
  13022. (progn
  13023. (org-back-to-heading t)
  13024. (while (not (equal lastpos (point)))
  13025. (setq lastpos (point))
  13026. (when (looking-at
  13027. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13028. (setq ltags (org-split-string
  13029. (org-match-string-no-properties 1) ":"))
  13030. (when parent
  13031. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13032. (setq tags (append
  13033. (if parent
  13034. (org-remove-uninherited-tags ltags)
  13035. ltags)
  13036. tags)))
  13037. (or org-use-tag-inheritance (throw 'done t))
  13038. (if local (throw 'done t))
  13039. (or (org-up-heading-safe) (error nil))
  13040. (setq parent t)))
  13041. (error nil)))))
  13042. (if local
  13043. tags
  13044. (reverse (delete-dups
  13045. (reverse (append
  13046. (org-remove-uninherited-tags
  13047. org-file-tags) tags)))))))))
  13048. (defun org-add-prop-inherited (s)
  13049. (add-text-properties 0 (length s) '(inherited t) s)
  13050. s)
  13051. (defun org-toggle-tag (tag &optional onoff)
  13052. "Toggle the tag TAG for the current line.
  13053. If ONOFF is `on' or `off', don't toggle but set to this state."
  13054. (let (res current)
  13055. (save-excursion
  13056. (org-back-to-heading t)
  13057. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13058. (point-at-eol) t)
  13059. (progn
  13060. (setq current (match-string 1))
  13061. (replace-match ""))
  13062. (setq current ""))
  13063. (setq current (nreverse (org-split-string current ":")))
  13064. (cond
  13065. ((eq onoff 'on)
  13066. (setq res t)
  13067. (or (member tag current) (push tag current)))
  13068. ((eq onoff 'off)
  13069. (or (not (member tag current)) (setq current (delete tag current))))
  13070. (t (if (member tag current)
  13071. (setq current (delete tag current))
  13072. (setq res t)
  13073. (push tag current))))
  13074. (end-of-line 1)
  13075. (if current
  13076. (progn
  13077. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13078. (org-set-tags nil t))
  13079. (delete-horizontal-space))
  13080. (run-hooks 'org-after-tags-change-hook))
  13081. res))
  13082. (defun org-align-tags-here (to-col)
  13083. ;; Assumes that this is a headline
  13084. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13085. (beginning-of-line 1)
  13086. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13087. (< pos (match-beginning 2)))
  13088. (progn
  13089. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13090. (goto-char (match-beginning 1))
  13091. (insert " ")
  13092. (delete-region (point) (1+ (match-beginning 2)))
  13093. (setq ncol (max (current-column)
  13094. (1+ col)
  13095. (if (> to-col 0)
  13096. to-col
  13097. (- (abs to-col) tags-l))))
  13098. (setq p (point))
  13099. (insert (make-string (- ncol (current-column)) ?\ ))
  13100. (setq ncol (current-column))
  13101. (when indent-tabs-mode (tabify p (point-at-eol)))
  13102. (org-move-to-column (min ncol col) t))
  13103. (goto-char pos))))
  13104. (defun org-set-tags-command (&optional arg just-align)
  13105. "Call the set-tags command for the current entry."
  13106. (interactive "P")
  13107. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13108. (org-set-tags arg just-align)
  13109. (save-excursion
  13110. (unless (and (org-region-active-p)
  13111. org-loop-over-headlines-in-active-region)
  13112. (org-back-to-heading t))
  13113. (org-set-tags arg just-align))))
  13114. (defun org-set-tags-to (data)
  13115. "Set the tags of the current entry to DATA, replacing the current tags.
  13116. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13117. If DATA is nil or the empty string, any tags will be removed."
  13118. (interactive "sTags: ")
  13119. (setq data
  13120. (cond
  13121. ((eq data nil) "")
  13122. ((equal data "") "")
  13123. ((stringp data)
  13124. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13125. ":"))
  13126. ((listp data)
  13127. (concat ":" (mapconcat 'identity data ":") ":"))))
  13128. (when data
  13129. (save-excursion
  13130. (org-back-to-heading t)
  13131. (when (looking-at org-complex-heading-regexp)
  13132. (if (match-end 5)
  13133. (progn
  13134. (goto-char (match-beginning 5))
  13135. (insert data)
  13136. (delete-region (point) (point-at-eol))
  13137. (org-set-tags nil 'align))
  13138. (goto-char (point-at-eol))
  13139. (insert " " data)
  13140. (org-set-tags nil 'align)))
  13141. (beginning-of-line 1)
  13142. (if (looking-at ".*?\\([ \t]+\\)$")
  13143. (delete-region (match-beginning 1) (match-end 1))))))
  13144. (defun org-align-all-tags ()
  13145. "Align the tags i all headings."
  13146. (interactive)
  13147. (save-excursion
  13148. (or (ignore-errors (org-back-to-heading t))
  13149. (outline-next-heading))
  13150. (if (org-at-heading-p)
  13151. (org-set-tags t)
  13152. (message "No headings"))))
  13153. (defvar org-indent-indentation-per-level)
  13154. (defun org-set-tags (&optional arg just-align)
  13155. "Set the tags for the current headline.
  13156. With prefix ARG, realign all tags in headings in the current buffer."
  13157. (interactive "P")
  13158. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13159. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13160. 'region-start-level 'region))
  13161. org-loop-over-headlines-in-active-region)
  13162. (org-map-entries
  13163. ;; We don't use ARG and JUST-ALIGN here these args are not
  13164. ;; useful when looping over headlines
  13165. `(org-set-tags)
  13166. org-loop-over-headlines-in-active-region
  13167. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13168. (let* ((re org-outline-regexp-bol)
  13169. (current (unless arg (org-get-tags-string)))
  13170. (col (current-column))
  13171. (org-setting-tags t)
  13172. table current-tags inherited-tags ; computed below when needed
  13173. tags p0 c0 c1 rpl di tc level)
  13174. (if arg
  13175. (save-excursion
  13176. (goto-char (point-min))
  13177. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13178. (while (re-search-forward re nil t)
  13179. (org-set-tags nil t)
  13180. (end-of-line 1)))
  13181. (message "All tags realigned to column %d" org-tags-column))
  13182. (if just-align
  13183. (setq tags current)
  13184. ;; Get a new set of tags from the user
  13185. (save-excursion
  13186. (setq table (append org-tag-persistent-alist
  13187. (or org-tag-alist (org-get-buffer-tags))
  13188. (and
  13189. org-complete-tags-always-offer-all-agenda-tags
  13190. (org-global-tags-completion-table
  13191. (org-agenda-files))))
  13192. org-last-tags-completion-table table
  13193. current-tags (org-split-string current ":")
  13194. inherited-tags (nreverse
  13195. (nthcdr (length current-tags)
  13196. (nreverse (org-get-tags-at))))
  13197. tags
  13198. (if (or (eq t org-use-fast-tag-selection)
  13199. (and org-use-fast-tag-selection
  13200. (delq nil (mapcar 'cdr table))))
  13201. (org-fast-tag-selection
  13202. current-tags inherited-tags table
  13203. (if org-fast-tag-selection-include-todo
  13204. org-todo-key-alist))
  13205. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13206. (org-trim
  13207. (org-icompleting-read "Tags: "
  13208. 'org-tags-completion-function
  13209. nil nil current 'org-tags-history))))))
  13210. (while (string-match "[-+&]+" tags)
  13211. ;; No boolean logic, just a list
  13212. (setq tags (replace-match ":" t t tags))))
  13213. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13214. (if org-tags-sort-function
  13215. (setq tags (mapconcat 'identity
  13216. (sort (org-split-string
  13217. tags (org-re "[^[:alnum:]_@#%]+"))
  13218. org-tags-sort-function) ":")))
  13219. (if (string-match "\\`[\t ]*\\'" tags)
  13220. (setq tags "")
  13221. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13222. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13223. ;; Insert new tags at the correct column
  13224. (beginning-of-line 1)
  13225. (setq level (or (and (looking-at org-outline-regexp)
  13226. (- (match-end 0) (point) 1))
  13227. 1))
  13228. (cond
  13229. ((and (equal current "") (equal tags "")))
  13230. ((re-search-forward
  13231. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13232. (point-at-eol) t)
  13233. (if (equal tags "")
  13234. (setq rpl "")
  13235. (goto-char (match-beginning 0))
  13236. (setq c0 (current-column)
  13237. ;; compute offset for the case of org-indent-mode active
  13238. di (if org-indent-mode
  13239. (* (1- org-indent-indentation-per-level) (1- level))
  13240. 0)
  13241. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13242. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13243. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13244. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13245. (replace-match rpl t t)
  13246. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13247. tags)
  13248. (t (error "Tags alignment failed")))
  13249. (org-move-to-column col)
  13250. (unless just-align
  13251. (run-hooks 'org-after-tags-change-hook))))))
  13252. (defun org-change-tag-in-region (beg end tag off)
  13253. "Add or remove TAG for each entry in the region.
  13254. This works in the agenda, and also in an org-mode buffer."
  13255. (interactive
  13256. (list (region-beginning) (region-end)
  13257. (let ((org-last-tags-completion-table
  13258. (if (derived-mode-p 'org-mode)
  13259. (org-uniquify
  13260. (delq nil (append (org-get-buffer-tags)
  13261. (org-global-tags-completion-table))))
  13262. (org-global-tags-completion-table))))
  13263. (org-icompleting-read
  13264. "Tag: " 'org-tags-completion-function nil nil nil
  13265. 'org-tags-history))
  13266. (progn
  13267. (message "[s]et or [r]emove? ")
  13268. (equal (read-char-exclusive) ?r))))
  13269. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13270. (let ((agendap (equal major-mode 'org-agenda-mode))
  13271. l1 l2 m buf pos newhead (cnt 0))
  13272. (goto-char end)
  13273. (setq l2 (1- (org-current-line)))
  13274. (goto-char beg)
  13275. (setq l1 (org-current-line))
  13276. (loop for l from l1 to l2 do
  13277. (org-goto-line l)
  13278. (setq m (get-text-property (point) 'org-hd-marker))
  13279. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13280. (and agendap m))
  13281. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13282. pos (if agendap m (point)))
  13283. (with-current-buffer buf
  13284. (save-excursion
  13285. (save-restriction
  13286. (goto-char pos)
  13287. (setq cnt (1+ cnt))
  13288. (org-toggle-tag tag (if off 'off 'on))
  13289. (setq newhead (org-get-heading)))))
  13290. (and agendap (org-agenda-change-all-lines newhead m))))
  13291. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13292. (defun org-tags-completion-function (string predicate &optional flag)
  13293. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13294. (confirm (lambda (x) (stringp (car x)))))
  13295. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13296. (setq s1 (match-string 1 string)
  13297. s2 (match-string 2 string))
  13298. (setq s1 "" s2 string))
  13299. (cond
  13300. ((eq flag nil)
  13301. ;; try completion
  13302. (setq rtn (try-completion s2 ctable confirm))
  13303. (if (stringp rtn)
  13304. (setq rtn
  13305. (concat s1 s2 (substring rtn (length s2))
  13306. (if (and org-add-colon-after-tag-completion
  13307. (assoc rtn ctable))
  13308. ":" ""))))
  13309. rtn)
  13310. ((eq flag t)
  13311. ;; all-completions
  13312. (all-completions s2 ctable confirm))
  13313. ((eq flag 'lambda)
  13314. ;; exact match?
  13315. (assoc s2 ctable)))))
  13316. (defun org-fast-tag-insert (kwd tags face &optional end)
  13317. "Insert KDW, and the TAGS, the latter with face FACE.
  13318. Also insert END."
  13319. (insert (format "%-12s" (concat kwd ":"))
  13320. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13321. (or end "")))
  13322. (defun org-fast-tag-show-exit (flag)
  13323. (save-excursion
  13324. (org-goto-line 3)
  13325. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13326. (replace-match ""))
  13327. (when flag
  13328. (end-of-line 1)
  13329. (org-move-to-column (- (window-width) 19) t)
  13330. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13331. (defun org-set-current-tags-overlay (current prefix)
  13332. "Add an overlay to CURRENT tag with PREFIX."
  13333. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13334. (if (featurep 'xemacs)
  13335. (org-overlay-display org-tags-overlay (concat prefix s)
  13336. 'secondary-selection)
  13337. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13338. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13339. (defvar org-last-tag-selection-key nil)
  13340. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13341. "Fast tag selection with single keys.
  13342. CURRENT is the current list of tags in the headline, INHERITED is the
  13343. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13344. possibly with grouping information. TODO-TABLE is a similar table with
  13345. TODO keywords, should these have keys assigned to them.
  13346. If the keys are nil, a-z are automatically assigned.
  13347. Returns the new tags string, or nil to not change the current settings."
  13348. (let* ((fulltable (append table todo-table))
  13349. (maxlen (apply 'max (mapcar
  13350. (lambda (x)
  13351. (if (stringp (car x)) (string-width (car x)) 0))
  13352. fulltable)))
  13353. (buf (current-buffer))
  13354. (expert (eq org-fast-tag-selection-single-key 'expert))
  13355. (buffer-tags nil)
  13356. (fwidth (+ maxlen 3 1 3))
  13357. (ncol (/ (- (window-width) 4) fwidth))
  13358. (i-face 'org-done)
  13359. (c-face 'org-todo)
  13360. tg cnt e c char c1 c2 ntable tbl rtn
  13361. ov-start ov-end ov-prefix
  13362. (exit-after-next org-fast-tag-selection-single-key)
  13363. (done-keywords org-done-keywords)
  13364. groups ingroup)
  13365. (save-excursion
  13366. (beginning-of-line 1)
  13367. (if (looking-at
  13368. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13369. (setq ov-start (match-beginning 1)
  13370. ov-end (match-end 1)
  13371. ov-prefix "")
  13372. (setq ov-start (1- (point-at-eol))
  13373. ov-end (1+ ov-start))
  13374. (skip-chars-forward "^\n\r")
  13375. (setq ov-prefix
  13376. (concat
  13377. (buffer-substring (1- (point)) (point))
  13378. (if (> (current-column) org-tags-column)
  13379. " "
  13380. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13381. (move-overlay org-tags-overlay ov-start ov-end)
  13382. (save-window-excursion
  13383. (if expert
  13384. (set-buffer (get-buffer-create " *Org tags*"))
  13385. (delete-other-windows)
  13386. (split-window-vertically)
  13387. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13388. (erase-buffer)
  13389. (org-set-local 'org-done-keywords done-keywords)
  13390. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13391. (org-fast-tag-insert "Current" current c-face "\n\n")
  13392. (org-fast-tag-show-exit exit-after-next)
  13393. (org-set-current-tags-overlay current ov-prefix)
  13394. (setq tbl fulltable char ?a cnt 0)
  13395. (while (setq e (pop tbl))
  13396. (cond
  13397. ((equal (car e) :startgroup)
  13398. (push '() groups) (setq ingroup t)
  13399. (when (not (= cnt 0))
  13400. (setq cnt 0)
  13401. (insert "\n"))
  13402. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13403. ((equal (car e) :endgroup)
  13404. (setq ingroup nil cnt 0)
  13405. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13406. ((equal e '(:newline))
  13407. (when (not (= cnt 0))
  13408. (setq cnt 0)
  13409. (insert "\n")
  13410. (setq e (car tbl))
  13411. (while (equal (car tbl) '(:newline))
  13412. (insert "\n")
  13413. (setq tbl (cdr tbl)))))
  13414. ((equal e '(:grouptags)) nil)
  13415. (t
  13416. (setq tg (copy-sequence (car e)) c2 nil)
  13417. (if (cdr e)
  13418. (setq c (cdr e))
  13419. ;; automatically assign a character.
  13420. (setq c1 (string-to-char
  13421. (downcase (substring
  13422. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13423. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13424. (while (or (rassoc char ntable) (rassoc char table))
  13425. (setq char (1+ char)))
  13426. (setq c2 c1))
  13427. (setq c (or c2 char)))
  13428. (if ingroup (push tg (car groups)))
  13429. (setq tg (org-add-props tg nil 'face
  13430. (cond
  13431. ((not (assoc tg table))
  13432. (org-get-todo-face tg))
  13433. ((member tg current) c-face)
  13434. ((member tg inherited) i-face))))
  13435. (if (equal (caar tbl) :grouptags)
  13436. (org-add-props tg nil 'face 'org-tag-group))
  13437. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13438. (insert "[" c "] " tg (make-string
  13439. (- fwidth 4 (length tg)) ?\ ))
  13440. (push (cons tg c) ntable)
  13441. (when (= (setq cnt (1+ cnt)) ncol)
  13442. (insert "\n")
  13443. (if ingroup (insert " "))
  13444. (setq cnt 0)))))
  13445. (setq ntable (nreverse ntable))
  13446. (insert "\n")
  13447. (goto-char (point-min))
  13448. (if (not expert) (org-fit-window-to-buffer))
  13449. (setq rtn
  13450. (catch 'exit
  13451. (while t
  13452. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13453. (if (not groups) "no " "")
  13454. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13455. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13456. (setq org-last-tag-selection-key c)
  13457. (cond
  13458. ((= c ?\r) (throw 'exit t))
  13459. ((= c ?!)
  13460. (setq groups (not groups))
  13461. (goto-char (point-min))
  13462. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13463. ((= c ?\C-c)
  13464. (if (not expert)
  13465. (org-fast-tag-show-exit
  13466. (setq exit-after-next (not exit-after-next)))
  13467. (setq expert nil)
  13468. (delete-other-windows)
  13469. (set-window-buffer (split-window-vertically) " *Org tags*")
  13470. (org-switch-to-buffer-other-window " *Org tags*")
  13471. (org-fit-window-to-buffer)))
  13472. ((or (= c ?\C-g)
  13473. (and (= c ?q) (not (rassoc c ntable))))
  13474. (org-detach-overlay org-tags-overlay)
  13475. (setq quit-flag t))
  13476. ((= c ?\ )
  13477. (setq current nil)
  13478. (if exit-after-next (setq exit-after-next 'now)))
  13479. ((= c ?\t)
  13480. (condition-case nil
  13481. (setq tg (org-icompleting-read
  13482. "Tag: "
  13483. (or buffer-tags
  13484. (with-current-buffer buf
  13485. (org-get-buffer-tags)))))
  13486. (quit (setq tg "")))
  13487. (when (string-match "\\S-" tg)
  13488. (add-to-list 'buffer-tags (list tg))
  13489. (if (member tg current)
  13490. (setq current (delete tg current))
  13491. (push tg current)))
  13492. (if exit-after-next (setq exit-after-next 'now)))
  13493. ((setq e (rassoc c todo-table) tg (car e))
  13494. (with-current-buffer buf
  13495. (save-excursion (org-todo tg)))
  13496. (if exit-after-next (setq exit-after-next 'now)))
  13497. ((setq e (rassoc c ntable) tg (car e))
  13498. (if (member tg current)
  13499. (setq current (delete tg current))
  13500. (loop for g in groups do
  13501. (if (member tg g)
  13502. (mapc (lambda (x)
  13503. (setq current (delete x current)))
  13504. g)))
  13505. (push tg current))
  13506. (if exit-after-next (setq exit-after-next 'now))))
  13507. ;; Create a sorted list
  13508. (setq current
  13509. (sort current
  13510. (lambda (a b)
  13511. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13512. (if (eq exit-after-next 'now) (throw 'exit t))
  13513. (goto-char (point-min))
  13514. (beginning-of-line 2)
  13515. (delete-region (point) (point-at-eol))
  13516. (org-fast-tag-insert "Current" current c-face)
  13517. (org-set-current-tags-overlay current ov-prefix)
  13518. (while (re-search-forward
  13519. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13520. (setq tg (match-string 1))
  13521. (add-text-properties
  13522. (match-beginning 1) (match-end 1)
  13523. (list 'face
  13524. (cond
  13525. ((member tg current) c-face)
  13526. ((member tg inherited) i-face)
  13527. (t (get-text-property (match-beginning 1) 'face))))))
  13528. (goto-char (point-min)))))
  13529. (org-detach-overlay org-tags-overlay)
  13530. (if rtn
  13531. (mapconcat 'identity current ":")
  13532. nil))))
  13533. (defun org-get-tags-string ()
  13534. "Get the TAGS string in the current headline."
  13535. (unless (org-at-heading-p t)
  13536. (user-error "Not on a heading"))
  13537. (save-excursion
  13538. (beginning-of-line 1)
  13539. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13540. (org-match-string-no-properties 1)
  13541. "")))
  13542. (defun org-get-tags ()
  13543. "Get the list of tags specified in the current headline."
  13544. (org-split-string (org-get-tags-string) ":"))
  13545. (defun org-get-buffer-tags ()
  13546. "Get a table of all tags used in the buffer, for completion."
  13547. (let (tags)
  13548. (save-excursion
  13549. (goto-char (point-min))
  13550. (while (re-search-forward
  13551. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13552. (when (equal (char-after (point-at-bol 0)) ?*)
  13553. (mapc (lambda (x) (add-to-list 'tags x))
  13554. (org-split-string (org-match-string-no-properties 1) ":")))))
  13555. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13556. (mapcar 'list tags)))
  13557. ;;;; The mapping API
  13558. (defun org-map-entries (func &optional match scope &rest skip)
  13559. "Call FUNC at each headline selected by MATCH in SCOPE.
  13560. FUNC is a function or a lisp form. The function will be called without
  13561. arguments, with the cursor positioned at the beginning of the headline.
  13562. The return values of all calls to the function will be collected and
  13563. returned as a list.
  13564. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13565. does not need to preserve point. After evaluation, the cursor will be
  13566. moved to the end of the line (presumably of the headline of the
  13567. processed entry) and search continues from there. Under some
  13568. circumstances, this may not produce the wanted results. For example,
  13569. if you have removed (e.g. archived) the current (sub)tree it could
  13570. mean that the next entry will be skipped entirely. In such cases, you
  13571. can specify the position from where search should continue by making
  13572. FUNC set the variable `org-map-continue-from' to the desired buffer
  13573. position.
  13574. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13575. Only headlines that are matched by this query will be considered during
  13576. the iteration. When MATCH is nil or t, all headlines will be
  13577. visited by the iteration.
  13578. SCOPE determines the scope of this command. It can be any of:
  13579. nil The current buffer, respecting the restriction if any
  13580. tree The subtree started with the entry at point
  13581. region The entries within the active region, if any
  13582. region-start-level
  13583. The entries within the active region, but only those at
  13584. the same level than the first one.
  13585. file The current buffer, without restriction
  13586. file-with-archives
  13587. The current buffer, and any archives associated with it
  13588. agenda All agenda files
  13589. agenda-with-archives
  13590. All agenda files with any archive files associated with them
  13591. \(file1 file2 ...)
  13592. If this is a list, all files in the list will be scanned
  13593. The remaining args are treated as settings for the skipping facilities of
  13594. the scanner. The following items can be given here:
  13595. archive skip trees with the archive tag
  13596. comment skip trees with the COMMENT keyword
  13597. function or Emacs Lisp form:
  13598. will be used as value for `org-agenda-skip-function', so
  13599. whenever the function returns a position, FUNC will not be
  13600. called for that entry and search will continue from the
  13601. position returned
  13602. If your function needs to retrieve the tags including inherited tags
  13603. at the *current* entry, you can use the value of the variable
  13604. `org-scanner-tags' which will be much faster than getting the value
  13605. with `org-get-tags-at'. If your function gets properties with
  13606. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13607. to t around the call to `org-entry-properties' to get the same speedup.
  13608. Note that if your function moves around to retrieve tags and properties at
  13609. a *different* entry, you cannot use these techniques."
  13610. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13611. (not (org-region-active-p)))
  13612. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13613. (org-agenda-skip-archived-trees (memq 'archive skip))
  13614. (org-agenda-skip-comment-trees (memq 'comment skip))
  13615. (org-agenda-skip-function
  13616. (car (org-delete-all '(comment archive) skip)))
  13617. (org-tags-match-list-sublevels t)
  13618. (start-level (eq scope 'region-start-level))
  13619. matcher file res
  13620. org-todo-keywords-for-agenda
  13621. org-done-keywords-for-agenda
  13622. org-todo-keyword-alist-for-agenda
  13623. org-drawers-for-agenda
  13624. org-tag-alist-for-agenda
  13625. todo-only)
  13626. (cond
  13627. ((eq match t) (setq matcher t))
  13628. ((eq match nil) (setq matcher t))
  13629. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13630. (save-window-excursion
  13631. (save-restriction
  13632. (cond ((eq scope 'tree)
  13633. (org-back-to-heading t)
  13634. (org-narrow-to-subtree)
  13635. (setq scope nil))
  13636. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13637. (org-region-active-p))
  13638. ;; If needed, set start-level to a string like "2"
  13639. (when start-level
  13640. (save-excursion
  13641. (goto-char (region-beginning))
  13642. (unless (org-at-heading-p) (outline-next-heading))
  13643. (setq start-level (org-current-level))))
  13644. (narrow-to-region (region-beginning)
  13645. (save-excursion
  13646. (goto-char (region-end))
  13647. (unless (and (bolp) (org-at-heading-p))
  13648. (outline-next-heading))
  13649. (point)))
  13650. (setq scope nil)))
  13651. (if (not scope)
  13652. (progn
  13653. (org-agenda-prepare-buffers
  13654. (list (buffer-file-name (current-buffer))))
  13655. (setq res (org-scan-tags func matcher todo-only start-level)))
  13656. ;; Get the right scope
  13657. (cond
  13658. ((and scope (listp scope) (symbolp (car scope)))
  13659. (setq scope (eval scope)))
  13660. ((eq scope 'agenda)
  13661. (setq scope (org-agenda-files t)))
  13662. ((eq scope 'agenda-with-archives)
  13663. (setq scope (org-agenda-files t))
  13664. (setq scope (org-add-archive-files scope)))
  13665. ((eq scope 'file)
  13666. (setq scope (list (buffer-file-name))))
  13667. ((eq scope 'file-with-archives)
  13668. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13669. (org-agenda-prepare-buffers scope)
  13670. (while (setq file (pop scope))
  13671. (with-current-buffer (org-find-base-buffer-visiting file)
  13672. (save-excursion
  13673. (save-restriction
  13674. (widen)
  13675. (goto-char (point-min))
  13676. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13677. res)))
  13678. ;;;; Properties
  13679. ;;; Setting and retrieving properties
  13680. (defconst org-special-properties
  13681. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13682. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13683. "The special properties valid in Org-mode.
  13684. These are properties that are not defined in the property drawer,
  13685. but in some other way.")
  13686. (defconst org-default-properties
  13687. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13688. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13689. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13690. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13691. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13692. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13693. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13694. "Some properties that are used by Org-mode for various purposes.
  13695. Being in this list makes sure that they are offered for completion.")
  13696. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13697. "Regular expression matching the first line of a property drawer.")
  13698. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13699. "Regular expression matching the last line of a property drawer.")
  13700. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13701. "Regular expression matching the first line of a property drawer.")
  13702. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13703. "Regular expression matching the first line of a property drawer.")
  13704. (defconst org-property-drawer-re
  13705. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13706. org-property-end-re "\\)\n?")
  13707. "Matches an entire property drawer.")
  13708. (defconst org-clock-drawer-re
  13709. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13710. org-property-end-re "\\)\n?")
  13711. "Matches an entire clock drawer.")
  13712. (defsubst org-re-property (property)
  13713. "Return a regexp matching a PROPERTY line.
  13714. Match group 1 will be set to the value."
  13715. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13716. (defsubst org-re-property-keyword (property)
  13717. "Return a regexp matching a PROPERTY line, possibly with no
  13718. value for the property."
  13719. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13720. (defun org-property-action ()
  13721. "Do an action on properties."
  13722. (interactive)
  13723. (let (c)
  13724. (org-at-property-p)
  13725. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13726. (setq c (read-char-exclusive))
  13727. (cond
  13728. ((equal c ?s)
  13729. (call-interactively 'org-set-property))
  13730. ((equal c ?d)
  13731. (call-interactively 'org-delete-property))
  13732. ((equal c ?D)
  13733. (call-interactively 'org-delete-property-globally))
  13734. ((equal c ?c)
  13735. (call-interactively 'org-compute-property-at-point))
  13736. (t (user-error "No such property action %c" c)))))
  13737. (defun org-inc-effort ()
  13738. "Increment the value of the effort property in the current entry."
  13739. (interactive)
  13740. (org-set-effort nil t))
  13741. (defvar org-clock-effort) ;; Defined in org-clock.el
  13742. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13743. (defun org-set-effort (&optional value increment)
  13744. "Set the effort property of the current entry.
  13745. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13746. 10th allowed value.
  13747. When INCREMENT is non-nil, set the property to the next allowed value."
  13748. (interactive "P")
  13749. (if (equal value 0) (setq value 10))
  13750. (let* ((completion-ignore-case t)
  13751. (prop org-effort-property)
  13752. (cur (org-entry-get nil prop))
  13753. (allowed (org-property-get-allowed-values nil prop 'table))
  13754. (existing (mapcar 'list (org-property-values prop)))
  13755. (heading (nth 4 (org-heading-components)))
  13756. rpl
  13757. (val (cond
  13758. ((stringp value) value)
  13759. ((and allowed (integerp value))
  13760. (or (car (nth (1- value) allowed))
  13761. (car (org-last allowed))))
  13762. ((and allowed increment)
  13763. (or (caadr (member (list cur) allowed))
  13764. (user-error "Allowed effort values are not set")))
  13765. (allowed
  13766. (message "Select 1-9,0, [RET%s]: %s"
  13767. (if cur (concat "=" cur) "")
  13768. (mapconcat 'car allowed " "))
  13769. (setq rpl (read-char-exclusive))
  13770. (if (equal rpl ?\r)
  13771. cur
  13772. (setq rpl (- rpl ?0))
  13773. (if (equal rpl 0) (setq rpl 10))
  13774. (if (and (> rpl 0) (<= rpl (length allowed)))
  13775. (car (nth (1- rpl) allowed))
  13776. (org-completing-read "Effort: " allowed nil))))
  13777. (t
  13778. (let (org-completion-use-ido org-completion-use-iswitchb)
  13779. (org-completing-read
  13780. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13781. (concat "[" cur "]") "")
  13782. ": ")
  13783. existing nil nil "" nil cur))))))
  13784. (unless (equal (org-entry-get nil prop) val)
  13785. (org-entry-put nil prop val))
  13786. (save-excursion
  13787. (org-back-to-heading t)
  13788. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13789. (when (string= heading org-clock-current-task)
  13790. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13791. (org-clock-update-mode-line))
  13792. (message "%s is now %s" prop val)))
  13793. (defun org-at-property-p ()
  13794. "Is cursor inside a property drawer?"
  13795. (save-excursion
  13796. (beginning-of-line 1)
  13797. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13798. (save-match-data ;; Used by calling procedures
  13799. (let ((p (point))
  13800. (range (unless (org-before-first-heading-p)
  13801. (org-get-property-block))))
  13802. (and range (<= (car range) p) (< p (cdr range))))))))
  13803. (defun org-get-property-block (&optional beg end force)
  13804. "Return the (beg . end) range of the body of the property drawer.
  13805. BEG and END are the beginning and end of the current subtree, or of
  13806. the part before the first headline. If they are not given, they will
  13807. be found. If the drawer does not exist and FORCE is non-nil, create
  13808. the drawer."
  13809. (catch 'exit
  13810. (save-excursion
  13811. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13812. (progn (org-back-to-heading t) (point))))
  13813. (end (or end (and (not (outline-next-heading)) (point-max))
  13814. (point))))
  13815. (goto-char beg)
  13816. (if (re-search-forward org-property-start-re end t)
  13817. (setq beg (1+ (match-end 0)))
  13818. (if force
  13819. (save-excursion
  13820. (org-insert-property-drawer)
  13821. (setq end (progn (outline-next-heading) (point))))
  13822. (throw 'exit nil))
  13823. (goto-char beg)
  13824. (if (re-search-forward org-property-start-re end t)
  13825. (setq beg (1+ (match-end 0)))))
  13826. (if (re-search-forward org-property-end-re end t)
  13827. (setq end (match-beginning 0))
  13828. (or force (throw 'exit nil))
  13829. (goto-char beg)
  13830. (setq end beg)
  13831. (org-indent-line)
  13832. (insert ":END:\n"))
  13833. (cons beg end)))))
  13834. (defun org-entry-properties (&optional pom which specific)
  13835. "Get all properties of the entry at point-or-marker POM.
  13836. This includes the TODO keyword, the tags, time strings for deadline,
  13837. scheduled, and clocking, and any additional properties defined in the
  13838. entry. The return value is an alist, keys may occur multiple times
  13839. if the property key was used several times.
  13840. POM may also be nil, in which case the current entry is used.
  13841. If WHICH is nil or `all', get all properties. If WHICH is
  13842. `special' or `standard', only get that subclass. If WHICH
  13843. is a string only get exactly this property. SPECIFIC can be a string, the
  13844. specific property we are interested in. Specifying it can speed
  13845. things up because then unnecessary parsing is avoided."
  13846. (setq which (or which 'all))
  13847. (org-with-point-at pom
  13848. (let ((clockstr (substring org-clock-string 0 -1))
  13849. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13850. (case-fold-search nil)
  13851. beg end range props sum-props key key1 value string clocksum clocksumt)
  13852. (save-excursion
  13853. (when (condition-case nil
  13854. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13855. (error nil))
  13856. (setq beg (point))
  13857. (setq sum-props (get-text-property (point) 'org-summaries))
  13858. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13859. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13860. (outline-next-heading)
  13861. (setq end (point))
  13862. (when (memq which '(all special))
  13863. ;; Get the special properties, like TODO and tags
  13864. (goto-char beg)
  13865. (when (and (or (not specific) (string= specific "TODO"))
  13866. (looking-at org-todo-line-regexp) (match-end 2))
  13867. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13868. (when (and (or (not specific) (string= specific "PRIORITY"))
  13869. (looking-at org-priority-regexp))
  13870. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13871. (when (or (not specific) (string= specific "FILE"))
  13872. (push (cons "FILE" buffer-file-name) props))
  13873. (when (and (or (not specific) (string= specific "TAGS"))
  13874. (setq value (org-get-tags-string))
  13875. (string-match "\\S-" value))
  13876. (push (cons "TAGS" value) props))
  13877. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13878. (setq value (org-get-tags-at)))
  13879. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13880. ":"))
  13881. props))
  13882. (when (or (not specific) (string= specific "BLOCKED"))
  13883. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13884. (when (or (not specific)
  13885. (member specific
  13886. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13887. "TIMESTAMP" "TIMESTAMP_IA")))
  13888. (catch 'match
  13889. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13890. (setq key (if (match-end 1)
  13891. (substring (org-match-string-no-properties 1)
  13892. 0 -1))
  13893. string (if (equal key clockstr)
  13894. (org-trim
  13895. (buffer-substring-no-properties
  13896. (match-beginning 3) (goto-char
  13897. (point-at-eol))))
  13898. (substring (org-match-string-no-properties 3)
  13899. 1 -1)))
  13900. ;; Get the correct property name from the key. This is
  13901. ;; necessary if the user has configured time keywords.
  13902. (setq key1 (concat key ":"))
  13903. (cond
  13904. ((not key)
  13905. (setq key
  13906. (if (= (char-after (match-beginning 3)) ?\[)
  13907. "TIMESTAMP_IA" "TIMESTAMP")))
  13908. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13909. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13910. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13911. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13912. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13913. (progn
  13914. (push (cons key string) props)
  13915. ;; no need to search further if match is found
  13916. (throw 'match t))
  13917. (when (or (equal key "CLOCK") (not (assoc key props)))
  13918. (push (cons key string) props)))))))
  13919. (when (memq which '(all standard))
  13920. ;; Get the standard properties, like :PROP: ...
  13921. (setq range (org-get-property-block beg end))
  13922. (when range
  13923. (goto-char (car range))
  13924. (while (re-search-forward
  13925. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13926. (cdr range) t)
  13927. (setq key (org-match-string-no-properties 1)
  13928. value (org-trim (or (org-match-string-no-properties 2) "")))
  13929. (unless (member key excluded)
  13930. (push (cons key (or value "")) props)))))
  13931. (if clocksum
  13932. (push (cons "CLOCKSUM"
  13933. (org-columns-number-to-string (/ (float clocksum) 60.)
  13934. 'add_times))
  13935. props))
  13936. (if clocksumt
  13937. (push (cons "CLOCKSUM_T"
  13938. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13939. 'add_times))
  13940. props))
  13941. (unless (assoc "CATEGORY" props)
  13942. (push (cons "CATEGORY" (org-get-category)) props))
  13943. (append sum-props (nreverse props)))))))
  13944. (defun org-entry-get (pom property &optional inherit literal-nil)
  13945. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13946. If INHERIT is non-nil and the entry does not have the property,
  13947. then also check higher levels of the hierarchy.
  13948. If INHERIT is the symbol `selective', use inheritance only if the setting
  13949. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13950. If the property is present but empty, the return value is the empty string.
  13951. If the property is not present at all, nil is returned.
  13952. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13953. do not interpret it as the list atom nil. This is used for inheritance
  13954. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13955. (org-with-point-at pom
  13956. (if (and inherit (if (eq inherit 'selective)
  13957. (org-property-inherit-p property)
  13958. t))
  13959. (org-entry-get-with-inheritance property literal-nil)
  13960. (if (member property org-special-properties)
  13961. ;; We need a special property. Use `org-entry-properties' to
  13962. ;; retrieve it, but specify the wanted property
  13963. (cdr (assoc property (org-entry-properties nil 'special property)))
  13964. (let ((range (org-get-property-block)))
  13965. (when (and range (not (eq (car range) (cdr range))))
  13966. (let* ((props (list (or (assoc property org-file-properties)
  13967. (assoc property org-global-properties)
  13968. (assoc property org-global-properties-fixed))))
  13969. (ap (lambda (key)
  13970. (when (re-search-forward
  13971. (org-re-property key) (cdr range) t)
  13972. (setq props
  13973. (org-update-property-plist
  13974. key
  13975. (if (match-end 1)
  13976. (org-match-string-no-properties 1) "")
  13977. props)))))
  13978. val)
  13979. (goto-char (car range))
  13980. (funcall ap property)
  13981. (goto-char (car range))
  13982. (while (funcall ap (concat property "+")))
  13983. (setq val (cdr (assoc property props)))
  13984. (when val (if literal-nil val (org-not-nil val))))))))))
  13985. (defun org-property-or-variable-value (var &optional inherit)
  13986. "Check if there is a property fixing the value of VAR.
  13987. If yes, return this value. If not, return the current value of the variable."
  13988. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13989. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13990. (read prop)
  13991. (symbol-value var))))
  13992. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13993. "Delete the property PROPERTY from entry at point-or-marker POM.
  13994. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13995. an empty drawer to delete."
  13996. (org-with-point-at pom
  13997. (if (member property org-special-properties)
  13998. nil ; cannot delete these properties.
  13999. (let ((range (org-get-property-block)))
  14000. (if (and range
  14001. (goto-char (car range))
  14002. (re-search-forward
  14003. (org-re-property property)
  14004. (cdr range) t))
  14005. (progn
  14006. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14007. (and delete-empty-drawer
  14008. (org-remove-empty-drawer-at
  14009. delete-empty-drawer (car range)))
  14010. t)
  14011. nil)))))
  14012. ;; Multi-values properties are properties that contain multiple values
  14013. ;; These values are assumed to be single words, separated by whitespace.
  14014. (defun org-entry-add-to-multivalued-property (pom property value)
  14015. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14016. (let* ((old (org-entry-get pom property))
  14017. (values (and old (org-split-string old "[ \t]"))))
  14018. (setq value (org-entry-protect-space value))
  14019. (unless (member value values)
  14020. (setq values (append values (list value)))
  14021. (org-entry-put pom property
  14022. (mapconcat 'identity values " ")))))
  14023. (defun org-entry-remove-from-multivalued-property (pom property value)
  14024. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14025. (let* ((old (org-entry-get pom property))
  14026. (values (and old (org-split-string old "[ \t]"))))
  14027. (setq value (org-entry-protect-space value))
  14028. (when (member value values)
  14029. (setq values (delete value values))
  14030. (org-entry-put pom property
  14031. (mapconcat 'identity values " ")))))
  14032. (defun org-entry-member-in-multivalued-property (pom property value)
  14033. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14034. (let* ((old (org-entry-get pom property))
  14035. (values (and old (org-split-string old "[ \t]"))))
  14036. (setq value (org-entry-protect-space value))
  14037. (member value values)))
  14038. (defun org-entry-get-multivalued-property (pom property)
  14039. "Return a list of values in a multivalued property."
  14040. (let* ((value (org-entry-get pom property))
  14041. (values (and value (org-split-string value "[ \t]"))))
  14042. (mapcar 'org-entry-restore-space values)))
  14043. (defun org-entry-put-multivalued-property (pom property &rest values)
  14044. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14045. VALUES should be a list of strings. Spaces will be protected."
  14046. (org-entry-put pom property
  14047. (mapconcat 'org-entry-protect-space values " "))
  14048. (let* ((value (org-entry-get pom property))
  14049. (values (and value (org-split-string value "[ \t]"))))
  14050. (mapcar 'org-entry-restore-space values)))
  14051. (defun org-entry-protect-space (s)
  14052. "Protect spaces and newline in string S."
  14053. (while (string-match " " s)
  14054. (setq s (replace-match "%20" t t s)))
  14055. (while (string-match "\n" s)
  14056. (setq s (replace-match "%0A" t t s)))
  14057. s)
  14058. (defun org-entry-restore-space (s)
  14059. "Restore spaces and newline in string S."
  14060. (while (string-match "%20" s)
  14061. (setq s (replace-match " " t t s)))
  14062. (while (string-match "%0A" s)
  14063. (setq s (replace-match "\n" t t s)))
  14064. s)
  14065. (defvar org-entry-property-inherited-from (make-marker)
  14066. "Marker pointing to the entry from where a property was inherited.
  14067. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14068. location of the entry where the inheritance search matched. If there was
  14069. no match, the marker will point nowhere.
  14070. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14071. is set.")
  14072. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14073. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14074. The search will stop at the first ancestor which has the property defined.
  14075. If the value found is \"nil\", return nil to show that the property
  14076. should be considered as undefined (this is the meaning of nil here).
  14077. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14078. (move-marker org-entry-property-inherited-from nil)
  14079. (let (tmp)
  14080. (save-excursion
  14081. (save-restriction
  14082. (widen)
  14083. (catch 'ex
  14084. (while t
  14085. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14086. (or (ignore-errors (org-back-to-heading t))
  14087. (goto-char (point-min)))
  14088. (move-marker org-entry-property-inherited-from (point))
  14089. (throw 'ex tmp))
  14090. (or (ignore-errors (org-up-heading-safe))
  14091. (throw 'ex nil))))))
  14092. (setq tmp (or tmp
  14093. (cdr (assoc property org-file-properties))
  14094. (cdr (assoc property org-global-properties))
  14095. (cdr (assoc property org-global-properties-fixed))))
  14096. (if literal-nil tmp (org-not-nil tmp))))
  14097. (defvar org-property-changed-functions nil
  14098. "Hook called when the value of a property has changed.
  14099. Each hook function should accept two arguments, the name of the property
  14100. and the new value.")
  14101. (defun org-entry-put (pom property value)
  14102. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14103. (org-with-point-at pom
  14104. (org-back-to-heading t)
  14105. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14106. range)
  14107. (cond
  14108. ((equal property "TODO")
  14109. (when (and (stringp value) (string-match "\\S-" value)
  14110. (not (member value org-todo-keywords-1)))
  14111. (user-error "\"%s\" is not a valid TODO state" value))
  14112. (if (or (not value)
  14113. (not (string-match "\\S-" value)))
  14114. (setq value 'none))
  14115. (org-todo value)
  14116. (org-set-tags nil 'align))
  14117. ((equal property "PRIORITY")
  14118. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14119. (string-to-char value) ?\ ))
  14120. (org-set-tags nil 'align))
  14121. ((equal property "CLOCKSUM")
  14122. (if (not (re-search-forward
  14123. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14124. (error "Cannot find a clock log")
  14125. (goto-char (- (match-end 1) 2))
  14126. (cond
  14127. ((eq value 'earlier) (org-timestamp-down))
  14128. ((eq value 'later) (org-timestamp-up)))
  14129. (org-clock-sum-current-item)))
  14130. ((equal property "SCHEDULED")
  14131. (if (re-search-forward org-scheduled-time-regexp end t)
  14132. (cond
  14133. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14134. ((eq value 'later) (org-timestamp-change 1 'day))
  14135. (t (call-interactively 'org-schedule)))
  14136. (call-interactively 'org-schedule)))
  14137. ((equal property "DEADLINE")
  14138. (if (re-search-forward org-deadline-time-regexp end t)
  14139. (cond
  14140. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14141. ((eq value 'later) (org-timestamp-change 1 'day))
  14142. (t (call-interactively 'org-deadline)))
  14143. (call-interactively 'org-deadline)))
  14144. ((member property org-special-properties)
  14145. (error "The %s property can not yet be set with `org-entry-put'"
  14146. property))
  14147. (t ; a non-special property
  14148. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14149. (setq range (org-get-property-block beg end 'force))
  14150. (goto-char (car range))
  14151. (if (re-search-forward
  14152. (org-re-property-keyword property) (cdr range) t)
  14153. (progn
  14154. (delete-region (match-beginning 0) (match-end 0))
  14155. (goto-char (match-beginning 0)))
  14156. (goto-char (cdr range))
  14157. (insert "\n")
  14158. (backward-char 1)
  14159. (org-indent-line))
  14160. (insert ":" property ":")
  14161. (and value (insert " " value))
  14162. (org-indent-line)))))
  14163. (run-hook-with-args 'org-property-changed-functions property value)))
  14164. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14165. "Get all property keys in the current buffer.
  14166. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14167. like tags and TODO state.
  14168. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14169. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14170. and others.
  14171. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14172. formats in the current buffer."
  14173. (let (rtn range cfmt s p)
  14174. (save-excursion
  14175. (save-restriction
  14176. (widen)
  14177. (goto-char (point-min))
  14178. (while (re-search-forward org-property-start-re nil t)
  14179. (setq range (org-get-property-block))
  14180. (goto-char (car range))
  14181. (while (re-search-forward
  14182. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14183. (cdr range) t)
  14184. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14185. (outline-next-heading))))
  14186. (when include-specials
  14187. (setq rtn (append org-special-properties rtn)))
  14188. (when include-defaults
  14189. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14190. (add-to-list 'rtn org-effort-property))
  14191. (when include-columns
  14192. (save-excursion
  14193. (save-restriction
  14194. (widen)
  14195. (goto-char (point-min))
  14196. (while (re-search-forward
  14197. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14198. nil t)
  14199. (setq cfmt (match-string 2) s 0)
  14200. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14201. cfmt s)
  14202. (setq s (match-end 0)
  14203. p (match-string 1 cfmt))
  14204. (unless (or (equal p "ITEM")
  14205. (member p org-special-properties))
  14206. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14207. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14208. (defun org-property-values (key)
  14209. "Return a list of all values of property KEY in the current buffer."
  14210. (save-excursion
  14211. (save-restriction
  14212. (widen)
  14213. (goto-char (point-min))
  14214. (let ((re (org-re-property key))
  14215. values)
  14216. (while (re-search-forward re nil t)
  14217. (add-to-list 'values (org-trim (match-string 1))))
  14218. (delete "" values)))))
  14219. (defun org-insert-property-drawer ()
  14220. "Insert a property drawer into the current entry."
  14221. (org-back-to-heading t)
  14222. (looking-at org-outline-regexp)
  14223. (let ((indent (if org-adapt-indentation
  14224. (- (match-end 0) (match-beginning 0))
  14225. 0))
  14226. (beg (point))
  14227. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14228. end hiddenp)
  14229. (outline-next-heading)
  14230. (setq end (point))
  14231. (goto-char beg)
  14232. (while (re-search-forward re end t))
  14233. (setq hiddenp (outline-invisible-p))
  14234. (end-of-line 1)
  14235. (and (equal (char-after) ?\n) (forward-char 1))
  14236. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14237. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14238. ;; just skip this line
  14239. (beginning-of-line 2)
  14240. ;; Drawer start, find the end
  14241. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14242. (beginning-of-line 1)))
  14243. (org-skip-over-state-notes)
  14244. (skip-chars-backward " \t\n\r")
  14245. (if (eq (char-before) ?*) (forward-char 1))
  14246. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14247. (beginning-of-line 0)
  14248. (org-indent-to-column indent)
  14249. (beginning-of-line 2)
  14250. (org-indent-to-column indent)
  14251. (beginning-of-line 0)
  14252. (if hiddenp
  14253. (save-excursion
  14254. (org-back-to-heading t)
  14255. (hide-entry))
  14256. (org-flag-drawer t))))
  14257. (defun org-insert-drawer (&optional arg drawer)
  14258. "Insert a drawer at point.
  14259. Optional argument DRAWER, when non-nil, is a string representing
  14260. drawer's name. Otherwise, the user is prompted for a name.
  14261. If a region is active, insert the drawer around that region
  14262. instead.
  14263. Point is left between drawer's boundaries."
  14264. (interactive "P")
  14265. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14266. "LOGBOOK"))
  14267. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14268. ;; internally by Org. They are meant to be inserted
  14269. ;; automatically.
  14270. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14271. ;; Remove system drawers from list. Note: For some reason,
  14272. ;; `org-completing-read' ignores the predicate while
  14273. ;; `completing-read' handles it fine.
  14274. (drawer (if arg "PROPERTIES"
  14275. (or drawer
  14276. (completing-read
  14277. "Drawer: " org-drawers
  14278. (lambda (d) (not (member d system-drawers))))))))
  14279. (cond
  14280. ;; With C-u, fall back on `org-insert-property-drawer'
  14281. (arg (org-insert-property-drawer))
  14282. ;; With an active region, insert a drawer at point.
  14283. ((not (org-region-active-p))
  14284. (progn
  14285. (unless (bolp) (insert "\n"))
  14286. (insert (format ":%s:\n\n:END:\n" drawer))
  14287. (forward-line -2)))
  14288. ;; Otherwise, insert the drawer at point
  14289. (t
  14290. (let ((rbeg (region-beginning))
  14291. (rend (copy-marker (region-end))))
  14292. (unwind-protect
  14293. (progn
  14294. (goto-char rbeg)
  14295. (beginning-of-line)
  14296. (when (save-excursion
  14297. (re-search-forward org-outline-regexp-bol rend t))
  14298. (user-error "Drawers cannot contain headlines"))
  14299. ;; Position point at the beginning of the first
  14300. ;; non-blank line in region. Insert drawer's opening
  14301. ;; there, then indent it.
  14302. (org-skip-whitespace)
  14303. (beginning-of-line)
  14304. (insert ":" drawer ":\n")
  14305. (forward-line -1)
  14306. (indent-for-tab-command)
  14307. ;; Move point to the beginning of the first blank line
  14308. ;; after the last non-blank line in region. Insert
  14309. ;; drawer's closing, then indent it.
  14310. (goto-char rend)
  14311. (skip-chars-backward " \r\t\n")
  14312. (insert "\n:END:")
  14313. (deactivate-mark t)
  14314. (indent-for-tab-command)
  14315. (unless (eolp) (insert "\n")))
  14316. ;; Clear marker, whatever the outcome of insertion is.
  14317. (set-marker rend nil)))))))
  14318. (defvar org-property-set-functions-alist nil
  14319. "Property set function alist.
  14320. Each entry should have the following format:
  14321. (PROPERTY . READ-FUNCTION)
  14322. The read function will be called with the same argument as
  14323. `org-completing-read'.")
  14324. (defun org-set-property-function (property)
  14325. "Get the function that should be used to set PROPERTY.
  14326. This is computed according to `org-property-set-functions-alist'."
  14327. (or (cdr (assoc property org-property-set-functions-alist))
  14328. 'org-completing-read))
  14329. (defun org-read-property-value (property)
  14330. "Read PROPERTY value from user."
  14331. (let* ((completion-ignore-case t)
  14332. (allowed (org-property-get-allowed-values nil property 'table))
  14333. (cur (org-entry-get nil property))
  14334. (prompt (concat property " value"
  14335. (if (and cur (string-match "\\S-" cur))
  14336. (concat " [" cur "]") "") ": "))
  14337. (set-function (org-set-property-function property))
  14338. (val (if allowed
  14339. (funcall set-function prompt allowed nil
  14340. (not (get-text-property 0 'org-unrestricted
  14341. (caar allowed))))
  14342. (let (org-completion-use-ido org-completion-use-iswitchb)
  14343. (funcall set-function prompt
  14344. (mapcar 'list (org-property-values property))
  14345. nil nil "" nil cur)))))
  14346. (if (equal val "")
  14347. cur
  14348. val)))
  14349. (defvar org-last-set-property nil)
  14350. (defvar org-last-set-property-value nil)
  14351. (defun org-read-property-name ()
  14352. "Read a property name."
  14353. (let* ((completion-ignore-case t)
  14354. (keys (org-buffer-property-keys nil t t))
  14355. (default-prop (or (save-excursion
  14356. (save-match-data
  14357. (beginning-of-line)
  14358. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14359. (null (string= (match-string 1) "END"))
  14360. (match-string 1))))
  14361. org-last-set-property))
  14362. (property (org-icompleting-read
  14363. (concat "Property"
  14364. (if default-prop (concat " [" default-prop "]") "")
  14365. ": ")
  14366. (mapcar 'list keys)
  14367. nil nil nil nil
  14368. default-prop)))
  14369. (if (member property keys)
  14370. property
  14371. (or (cdr (assoc (downcase property)
  14372. (mapcar (lambda (x) (cons (downcase x) x))
  14373. keys)))
  14374. property))))
  14375. (defun org-set-property-and-value (use-last)
  14376. "Allow to set [PROPERTY]: [value] direction from prompt.
  14377. When use-default, don't even ask, just use the last
  14378. \"[PROPERTY]: [value]\" string from the history."
  14379. (interactive "P")
  14380. (let* ((completion-ignore-case t)
  14381. (pv (or (and use-last org-last-set-property-value)
  14382. (org-completing-read
  14383. "Enter a \"[Property]: [value]\" pair: "
  14384. nil nil nil nil nil
  14385. org-last-set-property-value)))
  14386. prop val)
  14387. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14388. (setq prop (match-string 1 pv)
  14389. val (match-string 2 pv))
  14390. (org-set-property prop val))))
  14391. (defun org-set-property (property value)
  14392. "In the current entry, set PROPERTY to VALUE.
  14393. When called interactively, this will prompt for a property name, offering
  14394. completion on existing and default properties. And then it will prompt
  14395. for a value, offering completion either on allowed values (via an inherited
  14396. xxx_ALL property) or on existing values in other instances of this property
  14397. in the current file."
  14398. (interactive (list nil nil))
  14399. (let* ((property (or property (org-read-property-name)))
  14400. (value (or value (org-read-property-value property)))
  14401. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14402. (setq org-last-set-property property)
  14403. (setq org-last-set-property-value (concat property ": " value))
  14404. ;; Possibly postprocess the inserted value:
  14405. (when fn (setq value (funcall fn value)))
  14406. (unless (equal (org-entry-get nil property) value)
  14407. (org-entry-put nil property value))))
  14408. (defun org-delete-property (property &optional delete-empty-drawer)
  14409. "In the current entry, delete PROPERTY.
  14410. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14411. an empty drawer to delete."
  14412. (interactive
  14413. (let* ((completion-ignore-case t)
  14414. (prop (org-icompleting-read "Property: "
  14415. (org-entry-properties nil 'standard))))
  14416. (list prop)))
  14417. (message "Property %s %s" property
  14418. (if (org-entry-delete nil property delete-empty-drawer)
  14419. "deleted"
  14420. "was not present in the entry")))
  14421. (defun org-delete-property-globally (property)
  14422. "Remove PROPERTY globally, from all entries."
  14423. (interactive
  14424. (let* ((completion-ignore-case t)
  14425. (prop (org-icompleting-read
  14426. "Globally remove property: "
  14427. (mapcar 'list (org-buffer-property-keys)))))
  14428. (list prop)))
  14429. (save-excursion
  14430. (save-restriction
  14431. (widen)
  14432. (goto-char (point-min))
  14433. (let ((cnt 0))
  14434. (while (re-search-forward
  14435. (org-re-property property)
  14436. nil t)
  14437. (setq cnt (1+ cnt))
  14438. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14439. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14440. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14441. (defun org-compute-property-at-point ()
  14442. "Compute the property at point.
  14443. This looks for an enclosing column format, extracts the operator and
  14444. then applies it to the property in the column format's scope."
  14445. (interactive)
  14446. (unless (org-at-property-p)
  14447. (user-error "Not at a property"))
  14448. (let ((prop (org-match-string-no-properties 2)))
  14449. (org-columns-get-format-and-top-level)
  14450. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14451. (user-error "No operator defined for property %s" prop))
  14452. (org-columns-compute prop)))
  14453. (defvar org-property-allowed-value-functions nil
  14454. "Hook for functions supplying allowed values for a specific property.
  14455. The functions must take a single argument, the name of the property, and
  14456. return a flat list of allowed values. If \":ETC\" is one of
  14457. the values, this means that these values are intended as defaults for
  14458. completion, but that other values should be allowed too.
  14459. The functions must return nil if they are not responsible for this
  14460. property.")
  14461. (defun org-property-get-allowed-values (pom property &optional table)
  14462. "Get allowed values for the property PROPERTY.
  14463. When TABLE is non-nil, return an alist that can directly be used for
  14464. completion."
  14465. (let (vals)
  14466. (cond
  14467. ((equal property "TODO")
  14468. (setq vals (org-with-point-at pom
  14469. (append org-todo-keywords-1 '("")))))
  14470. ((equal property "PRIORITY")
  14471. (let ((n org-lowest-priority))
  14472. (while (>= n org-highest-priority)
  14473. (push (char-to-string n) vals)
  14474. (setq n (1- n)))))
  14475. ((member property org-special-properties))
  14476. ((setq vals (run-hook-with-args-until-success
  14477. 'org-property-allowed-value-functions property)))
  14478. (t
  14479. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14480. (when (and vals (string-match "\\S-" vals))
  14481. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14482. (setq vals (mapcar (lambda (x)
  14483. (cond ((stringp x) x)
  14484. ((numberp x) (number-to-string x))
  14485. ((symbolp x) (symbol-name x))
  14486. (t "???")))
  14487. vals)))))
  14488. (when (member ":ETC" vals)
  14489. (setq vals (remove ":ETC" vals))
  14490. (org-add-props (car vals) '(org-unrestricted t)))
  14491. (if table (mapcar 'list vals) vals)))
  14492. (defun org-property-previous-allowed-value (&optional previous)
  14493. "Switch to the next allowed value for this property."
  14494. (interactive)
  14495. (org-property-next-allowed-value t))
  14496. (defun org-property-next-allowed-value (&optional previous)
  14497. "Switch to the next allowed value for this property."
  14498. (interactive)
  14499. (unless (org-at-property-p)
  14500. (user-error "Not at a property"))
  14501. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14502. (key (match-string 2))
  14503. (value (match-string 3))
  14504. (allowed (or (org-property-get-allowed-values (point) key)
  14505. (and (member value '("[ ]" "[-]" "[X]"))
  14506. '("[ ]" "[X]"))))
  14507. (heading (save-match-data (nth 4 (org-heading-components))))
  14508. nval)
  14509. (unless allowed
  14510. (user-error "Allowed values for this property have not been defined"))
  14511. (if previous (setq allowed (reverse allowed)))
  14512. (if (member value allowed)
  14513. (setq nval (car (cdr (member value allowed)))))
  14514. (setq nval (or nval (car allowed)))
  14515. (if (equal nval value)
  14516. (user-error "Only one allowed value for this property"))
  14517. (org-at-property-p)
  14518. (replace-match (concat " :" key ": " nval) t t)
  14519. (org-indent-line)
  14520. (beginning-of-line 1)
  14521. (skip-chars-forward " \t")
  14522. (when (equal prop org-effort-property)
  14523. (save-excursion
  14524. (org-back-to-heading t)
  14525. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14526. (when (string= org-clock-current-task heading)
  14527. (setq org-clock-effort nval)
  14528. (org-clock-update-mode-line)))
  14529. (run-hook-with-args 'org-property-changed-functions key nval)))
  14530. (defun org-find-olp (path &optional this-buffer)
  14531. "Return a marker pointing to the entry at outline path OLP.
  14532. If anything goes wrong, throw an error.
  14533. You can wrap this call to catch the error like this:
  14534. (condition-case msg
  14535. (org-mobile-locate-entry (match-string 4))
  14536. (error (nth 1 msg)))
  14537. The return value will then be either a string with the error message,
  14538. or a marker if everything is OK.
  14539. If THIS-BUFFER is set, the outline path does not contain a file,
  14540. only headings."
  14541. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14542. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14543. (level 1)
  14544. (lmin 1)
  14545. (lmax 1)
  14546. limit re end found pos heading cnt flevel)
  14547. (unless buffer (error "File not found :%s" file))
  14548. (with-current-buffer buffer
  14549. (save-excursion
  14550. (save-restriction
  14551. (widen)
  14552. (setq limit (point-max))
  14553. (goto-char (point-min))
  14554. (while (setq heading (pop path))
  14555. (setq re (format org-complex-heading-regexp-format
  14556. (regexp-quote heading)))
  14557. (setq cnt 0 pos (point))
  14558. (while (re-search-forward re end t)
  14559. (setq level (- (match-end 1) (match-beginning 1)))
  14560. (if (and (>= level lmin) (<= level lmax))
  14561. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14562. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14563. lmax heading))
  14564. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14565. lmax heading))
  14566. (goto-char found)
  14567. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14568. (setq end (save-excursion (org-end-of-subtree t t))))
  14569. (when (org-at-heading-p)
  14570. (point-marker)))))))
  14571. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14572. "Find node HEADING in BUFFER.
  14573. Return a marker to the heading if it was found, or nil if not.
  14574. If POS-ONLY is set, return just the position instead of a marker.
  14575. The heading text must match exact, but it may have a TODO keyword,
  14576. a priority cookie and tags in the standard locations."
  14577. (with-current-buffer (or buffer (current-buffer))
  14578. (save-excursion
  14579. (save-restriction
  14580. (widen)
  14581. (goto-char (point-min))
  14582. (let (case-fold-search)
  14583. (if (re-search-forward
  14584. (format org-complex-heading-regexp-format
  14585. (regexp-quote heading)) nil t)
  14586. (if pos-only
  14587. (match-beginning 0)
  14588. (move-marker (make-marker) (match-beginning 0)))))))))
  14589. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14590. "Find Org node headline HEADING in all .org files in directory DIR.
  14591. When the target headline is found, return a marker to this location."
  14592. (let ((files (directory-files (or dir default-directory)
  14593. nil "\\`[^.#].*\\.org\\'"))
  14594. file visiting m buffer)
  14595. (catch 'found
  14596. (while (setq file (pop files))
  14597. (message "trying %s" file)
  14598. (setq visiting (org-find-base-buffer-visiting file))
  14599. (setq buffer (or visiting (find-file-noselect file)))
  14600. (setq m (org-find-exact-headline-in-buffer
  14601. heading buffer))
  14602. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14603. (and m (throw 'found m))))))
  14604. (defun org-find-entry-with-id (ident)
  14605. "Locate the entry that contains the ID property with exact value IDENT.
  14606. IDENT can be a string, a symbol or a number, this function will search for
  14607. the string representation of it.
  14608. Return the position where this entry starts, or nil if there is no such entry."
  14609. (interactive "sID: ")
  14610. (let ((id (cond
  14611. ((stringp ident) ident)
  14612. ((symbol-name ident) (symbol-name ident))
  14613. ((numberp ident) (number-to-string ident))
  14614. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14615. (case-fold-search nil))
  14616. (save-excursion
  14617. (save-restriction
  14618. (widen)
  14619. (goto-char (point-min))
  14620. (when (re-search-forward
  14621. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14622. nil t)
  14623. (org-back-to-heading t)
  14624. (point))))))
  14625. ;;;; Timestamps
  14626. (defvar org-last-changed-timestamp nil)
  14627. (defvar org-last-inserted-timestamp nil
  14628. "The last time stamp inserted with `org-insert-time-stamp'.")
  14629. (defvar org-time-was-given) ; dynamically scoped parameter
  14630. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14631. (defvar org-ts-what) ; dynamically scoped parameter
  14632. (defun org-time-stamp (arg &optional inactive)
  14633. "Prompt for a date/time and insert a time stamp.
  14634. If the user specifies a time like HH:MM or if this command is
  14635. called with at least one prefix argument, the time stamp contains
  14636. the date and the time. Otherwise, only the date is be included.
  14637. All parts of a date not specified by the user is filled in from
  14638. the current date/time. So if you just press return without
  14639. typing anything, the time stamp will represent the current
  14640. date/time.
  14641. If there is already a timestamp at the cursor, it will be
  14642. modified.
  14643. With two universal prefix arguments, insert an active timestamp
  14644. with the current time without prompting the user."
  14645. (interactive "P")
  14646. (let* ((ts nil)
  14647. (default-time
  14648. ;; Default time is either today, or, when entering a range,
  14649. ;; the range start.
  14650. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14651. (save-excursion
  14652. (re-search-backward
  14653. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14654. (- (point) 20) t)))
  14655. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14656. (current-time)))
  14657. (default-input (and ts (org-get-compact-tod ts)))
  14658. (repeater (save-excursion
  14659. (save-match-data
  14660. (beginning-of-line)
  14661. (when (re-search-forward
  14662. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14663. (save-excursion (progn (end-of-line) (point))) t)
  14664. (match-string 0)))))
  14665. org-time-was-given org-end-time-was-given time)
  14666. (cond
  14667. ((and (org-at-timestamp-p t)
  14668. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14669. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14670. (insert "--")
  14671. (setq time (let ((this-command this-command))
  14672. (org-read-date arg 'totime nil nil
  14673. default-time default-input inactive)))
  14674. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14675. ((org-at-timestamp-p t)
  14676. (setq time (let ((this-command this-command))
  14677. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14678. (when (org-at-timestamp-p t) ; just to get the match data
  14679. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14680. (replace-match "")
  14681. (setq org-last-changed-timestamp
  14682. (org-insert-time-stamp
  14683. time (or org-time-was-given arg)
  14684. inactive nil nil (list org-end-time-was-given)))
  14685. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14686. (setq org-last-changed-timestamp
  14687. (concat (substring org-last-inserted-timestamp 0 -1)
  14688. " " repeater ">"))))
  14689. (message "Timestamp updated"))
  14690. ((equal arg '(16))
  14691. (org-insert-time-stamp (current-time) t))
  14692. (t
  14693. (setq time (let ((this-command this-command))
  14694. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14695. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14696. nil nil (list org-end-time-was-given))))))
  14697. ;; FIXME: can we use this for something else, like computing time differences?
  14698. (defun org-get-compact-tod (s)
  14699. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14700. (let* ((t1 (match-string 1 s))
  14701. (h1 (string-to-number (match-string 2 s)))
  14702. (m1 (string-to-number (match-string 3 s)))
  14703. (t2 (and (match-end 4) (match-string 5 s)))
  14704. (h2 (and t2 (string-to-number (match-string 6 s))))
  14705. (m2 (and t2 (string-to-number (match-string 7 s))))
  14706. dh dm)
  14707. (if (not t2)
  14708. t1
  14709. (setq dh (- h2 h1) dm (- m2 m1))
  14710. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14711. (concat t1 "+" (number-to-string dh)
  14712. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14713. (defun org-time-stamp-inactive (&optional arg)
  14714. "Insert an inactive time stamp.
  14715. An inactive time stamp is enclosed in square brackets instead of angle
  14716. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14717. does not link to the calendar and cannot be changed with the S-cursor keys.
  14718. So these are more for recording a certain time/date."
  14719. (interactive "P")
  14720. (org-time-stamp arg 'inactive))
  14721. (defvar org-date-ovl (make-overlay 1 1))
  14722. (overlay-put org-date-ovl 'face 'org-date-selected)
  14723. (org-detach-overlay org-date-ovl)
  14724. (defvar org-ans1) ; dynamically scoped parameter
  14725. (defvar org-ans2) ; dynamically scoped parameter
  14726. (defvar org-plain-time-of-day-regexp) ; defined below
  14727. (defvar org-overriding-default-time nil) ; dynamically scoped
  14728. (defvar org-read-date-overlay nil)
  14729. (defvar org-dcst nil) ; dynamically scoped
  14730. (defvar org-read-date-history nil)
  14731. (defvar org-read-date-final-answer nil)
  14732. (defvar org-read-date-analyze-futurep nil)
  14733. (defvar org-read-date-analyze-forced-year nil)
  14734. (defvar org-read-date-inactive)
  14735. (defvar org-read-date-minibuffer-local-map
  14736. (let ((map (make-sparse-keymap)))
  14737. (set-keymap-parent map minibuffer-local-map)
  14738. (org-defkey map (kbd ".")
  14739. (lambda () (interactive)
  14740. ;; Are we at the beginning of the prompt?
  14741. (if (looking-back "^[^:]+: ")
  14742. (org-eval-in-calendar '(calendar-goto-today))
  14743. (insert "."))))
  14744. (org-defkey map (kbd "C-.")
  14745. (lambda () (interactive)
  14746. (org-eval-in-calendar '(calendar-goto-today))))
  14747. (org-defkey map [(meta shift left)]
  14748. (lambda () (interactive)
  14749. (org-eval-in-calendar '(calendar-backward-month 1))))
  14750. (org-defkey map [(meta shift right)]
  14751. (lambda () (interactive)
  14752. (org-eval-in-calendar '(calendar-forward-month 1))))
  14753. (org-defkey map [(meta shift up)]
  14754. (lambda () (interactive)
  14755. (org-eval-in-calendar '(calendar-backward-year 1))))
  14756. (org-defkey map [(meta shift down)]
  14757. (lambda () (interactive)
  14758. (org-eval-in-calendar '(calendar-forward-year 1))))
  14759. (org-defkey map [?\e (shift left)]
  14760. (lambda () (interactive)
  14761. (org-eval-in-calendar '(calendar-backward-month 1))))
  14762. (org-defkey map [?\e (shift right)]
  14763. (lambda () (interactive)
  14764. (org-eval-in-calendar '(calendar-forward-month 1))))
  14765. (org-defkey map [?\e (shift up)]
  14766. (lambda () (interactive)
  14767. (org-eval-in-calendar '(calendar-backward-year 1))))
  14768. (org-defkey map [?\e (shift down)]
  14769. (lambda () (interactive)
  14770. (org-eval-in-calendar '(calendar-forward-year 1))))
  14771. (org-defkey map [(shift up)]
  14772. (lambda () (interactive)
  14773. (org-eval-in-calendar '(calendar-backward-week 1))))
  14774. (org-defkey map [(shift down)]
  14775. (lambda () (interactive)
  14776. (org-eval-in-calendar '(calendar-forward-week 1))))
  14777. (org-defkey map [(shift left)]
  14778. (lambda () (interactive)
  14779. (org-eval-in-calendar '(calendar-backward-day 1))))
  14780. (org-defkey map [(shift right)]
  14781. (lambda () (interactive)
  14782. (org-eval-in-calendar '(calendar-forward-day 1))))
  14783. (org-defkey map "!"
  14784. (lambda () (interactive)
  14785. (org-eval-in-calendar '(diary-view-entries))
  14786. (message "")))
  14787. (org-defkey map ">"
  14788. (lambda () (interactive)
  14789. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14790. (org-defkey map "<"
  14791. (lambda () (interactive)
  14792. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14793. (org-defkey map "\C-v"
  14794. (lambda () (interactive)
  14795. (org-eval-in-calendar
  14796. '(calendar-scroll-left-three-months 1))))
  14797. (org-defkey map "\M-v"
  14798. (lambda () (interactive)
  14799. (org-eval-in-calendar
  14800. '(calendar-scroll-right-three-months 1))))
  14801. map)
  14802. "Keymap for minibuffer commands when using `org-read-date'.")
  14803. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14804. default-time default-input inactive)
  14805. "Read a date, possibly a time, and make things smooth for the user.
  14806. The prompt will suggest to enter an ISO date, but you can also enter anything
  14807. which will at least partially be understood by `parse-time-string'.
  14808. Unrecognized parts of the date will default to the current day, month, year,
  14809. hour and minute. If this command is called to replace a timestamp at point,
  14810. or to enter the second timestamp of a range, the default time is taken
  14811. from the existing stamp. Furthermore, the command prefers the future,
  14812. so if you are giving a date where the year is not given, and the day-month
  14813. combination is already past in the current year, it will assume you
  14814. mean next year. For details, see the manual. A few examples:
  14815. 3-2-5 --> 2003-02-05
  14816. feb 15 --> currentyear-02-15
  14817. 2/15 --> currentyear-02-15
  14818. sep 12 9 --> 2009-09-12
  14819. 12:45 --> today 12:45
  14820. 22 sept 0:34 --> currentyear-09-22 0:34
  14821. 12 --> currentyear-currentmonth-12
  14822. Fri --> nearest Friday after today
  14823. -Tue --> last Tuesday
  14824. etc.
  14825. Furthermore you can specify a relative date by giving, as the *first* thing
  14826. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14827. change in days weeks, months, years.
  14828. With a single plus or minus, the date is relative to today. With a double
  14829. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14830. +4d --> four days from today
  14831. +4 --> same as above
  14832. +2w --> two weeks from today
  14833. ++5 --> five days from default date
  14834. The function understands only English month and weekday abbreviations.
  14835. While prompting, a calendar is popped up - you can also select the
  14836. date with the mouse (button 1). The calendar shows a period of three
  14837. months. To scroll it to other months, use the keys `>' and `<'.
  14838. If you don't like the calendar, turn it off with
  14839. \(setq org-read-date-popup-calendar nil)
  14840. With optional argument TO-TIME, the date will immediately be converted
  14841. to an internal time.
  14842. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14843. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14844. still enter a time, and this function will inform the calling routine
  14845. about this change. The calling routine may then choose to change the
  14846. format used to insert the time stamp into the buffer to include the time.
  14847. With optional argument FROM-STRING, read from this string instead from
  14848. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14849. the time/date that is used for everything that is not specified by the
  14850. user."
  14851. (require 'parse-time)
  14852. (let* ((org-time-stamp-rounding-minutes
  14853. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14854. (org-dcst org-display-custom-times)
  14855. (ct (org-current-time))
  14856. (org-def (or org-overriding-default-time default-time ct))
  14857. (org-defdecode (decode-time org-def))
  14858. (dummy (progn
  14859. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14860. (setcar (nthcdr 2 org-defdecode) -1)
  14861. (setcar (nthcdr 1 org-defdecode) 59)
  14862. (setq org-def (apply 'encode-time org-defdecode)
  14863. org-defdecode (decode-time org-def)))))
  14864. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14865. (calendar-frame-setup nil)
  14866. (calendar-setup nil)
  14867. (calendar-move-hook nil)
  14868. (calendar-view-diary-initially-flag nil)
  14869. (calendar-view-holidays-initially-flag nil)
  14870. (timestr (format-time-string
  14871. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14872. (prompt (concat (if prompt (concat prompt " ") "")
  14873. (format "Date+time [%s]: " timestr)))
  14874. ans (org-ans0 "") org-ans1 org-ans2 final)
  14875. (cond
  14876. (from-string (setq ans from-string))
  14877. (org-read-date-popup-calendar
  14878. (save-excursion
  14879. (save-window-excursion
  14880. (calendar)
  14881. (org-eval-in-calendar '(setq cursor-type nil) t)
  14882. (unwind-protect
  14883. (progn
  14884. (calendar-forward-day (- (time-to-days org-def)
  14885. (calendar-absolute-from-gregorian
  14886. (calendar-current-date))))
  14887. (org-eval-in-calendar nil t)
  14888. (let* ((old-map (current-local-map))
  14889. (map (copy-keymap calendar-mode-map))
  14890. (minibuffer-local-map
  14891. (copy-keymap org-read-date-minibuffer-local-map)))
  14892. (org-defkey map (kbd "RET") 'org-calendar-select)
  14893. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14894. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14895. (unwind-protect
  14896. (progn
  14897. (use-local-map map)
  14898. (setq org-read-date-inactive inactive)
  14899. (add-hook 'post-command-hook 'org-read-date-display)
  14900. (setq org-ans0 (read-string prompt default-input
  14901. 'org-read-date-history nil))
  14902. ;; org-ans0: from prompt
  14903. ;; org-ans1: from mouse click
  14904. ;; org-ans2: from calendar motion
  14905. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14906. (remove-hook 'post-command-hook 'org-read-date-display)
  14907. (use-local-map old-map)
  14908. (when org-read-date-overlay
  14909. (delete-overlay org-read-date-overlay)
  14910. (setq org-read-date-overlay nil)))))
  14911. (bury-buffer "*Calendar*")))))
  14912. (t ; Naked prompt only
  14913. (unwind-protect
  14914. (setq ans (read-string prompt default-input
  14915. 'org-read-date-history timestr))
  14916. (when org-read-date-overlay
  14917. (delete-overlay org-read-date-overlay)
  14918. (setq org-read-date-overlay nil)))))
  14919. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14920. (when org-read-date-analyze-forced-year
  14921. (message "Year was forced into %s"
  14922. (if org-read-date-force-compatible-dates
  14923. "compatible range (1970-2037)"
  14924. "range representable on this machine"))
  14925. (ding))
  14926. ;; One round trip to get rid of 34th of August and stuff like that....
  14927. (setq final (decode-time (apply 'encode-time final)))
  14928. (setq org-read-date-final-answer ans)
  14929. (if to-time
  14930. (apply 'encode-time final)
  14931. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14932. (format "%04d-%02d-%02d %02d:%02d"
  14933. (nth 5 final) (nth 4 final) (nth 3 final)
  14934. (nth 2 final) (nth 1 final))
  14935. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14936. (defvar org-def)
  14937. (defvar org-defdecode)
  14938. (defvar org-with-time)
  14939. (defun org-read-date-display ()
  14940. "Display the current date prompt interpretation in the minibuffer."
  14941. (when org-read-date-display-live
  14942. (when org-read-date-overlay
  14943. (delete-overlay org-read-date-overlay))
  14944. (when (minibufferp (current-buffer))
  14945. (save-excursion
  14946. (end-of-line 1)
  14947. (while (not (equal (buffer-substring
  14948. (max (point-min) (- (point) 4)) (point))
  14949. " "))
  14950. (insert " ")))
  14951. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14952. " " (or org-ans1 org-ans2)))
  14953. (org-end-time-was-given nil)
  14954. (f (org-read-date-analyze ans org-def org-defdecode))
  14955. (fmts (if org-dcst
  14956. org-time-stamp-custom-formats
  14957. org-time-stamp-formats))
  14958. (fmt (if (or org-with-time
  14959. (and (boundp 'org-time-was-given) org-time-was-given))
  14960. (cdr fmts)
  14961. (car fmts)))
  14962. (txt (format-time-string fmt (apply 'encode-time f)))
  14963. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14964. (txt (concat "=> " txt)))
  14965. (when (and org-end-time-was-given
  14966. (string-match org-plain-time-of-day-regexp txt))
  14967. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14968. org-end-time-was-given
  14969. (substring txt (match-end 0)))))
  14970. (when org-read-date-analyze-futurep
  14971. (setq txt (concat txt " (=>F)")))
  14972. (setq org-read-date-overlay
  14973. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14974. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14975. (defun org-read-date-analyze (ans org-def org-defdecode)
  14976. "Analyze the combined answer of the date prompt."
  14977. ;; FIXME: cleanup and comment
  14978. (let ((nowdecode (decode-time (current-time)))
  14979. delta deltan deltaw deltadef year month day
  14980. hour minute second wday pm h2 m2 tl wday1
  14981. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14982. (setq org-read-date-analyze-futurep nil
  14983. org-read-date-analyze-forced-year nil)
  14984. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14985. (setq ans "+0"))
  14986. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14987. (setq ans (replace-match "" t t ans)
  14988. deltan (car delta)
  14989. deltaw (nth 1 delta)
  14990. deltadef (nth 2 delta)))
  14991. ;; Check if there is an iso week date in there. If yes, store the
  14992. ;; info and postpone interpreting it until the rest of the parsing
  14993. ;; is done.
  14994. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14995. (setq iso-year (if (match-end 1)
  14996. (org-small-year-to-year
  14997. (string-to-number (match-string 1 ans))))
  14998. iso-weekday (if (match-end 3)
  14999. (string-to-number (match-string 3 ans)))
  15000. iso-week (string-to-number (match-string 2 ans)))
  15001. (setq ans (replace-match "" t t ans)))
  15002. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15003. (when (string-match
  15004. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15005. (setq year (if (match-end 2)
  15006. (string-to-number (match-string 2 ans))
  15007. (progn (setq kill-year t)
  15008. (string-to-number (format-time-string "%Y"))))
  15009. month (string-to-number (match-string 3 ans))
  15010. day (string-to-number (match-string 4 ans)))
  15011. (if (< year 100) (setq year (+ 2000 year)))
  15012. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15013. t nil ans)))
  15014. ;; Help matching dotted european dates
  15015. (when (string-match
  15016. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15017. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15018. (setq kill-year t)
  15019. (string-to-number (format-time-string "%Y")))
  15020. day (string-to-number (match-string 1 ans))
  15021. month (string-to-number (match-string 2 ans))
  15022. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15023. t nil ans)))
  15024. ;; Help matching american dates, like 5/30 or 5/30/7
  15025. (when (string-match
  15026. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15027. (setq year (if (match-end 4)
  15028. (string-to-number (match-string 4 ans))
  15029. (progn (setq kill-year t)
  15030. (string-to-number (format-time-string "%Y"))))
  15031. month (string-to-number (match-string 1 ans))
  15032. day (string-to-number (match-string 2 ans)))
  15033. (if (< year 100) (setq year (+ 2000 year)))
  15034. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15035. t nil ans)))
  15036. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15037. ;; If there is a time with am/pm, and *no* time without it, we convert
  15038. ;; so that matching will be successful.
  15039. (loop for i from 1 to 2 do ; twice, for end time as well
  15040. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15041. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15042. (setq hour (string-to-number (match-string 1 ans))
  15043. minute (if (match-end 3)
  15044. (string-to-number (match-string 3 ans))
  15045. 0)
  15046. pm (equal ?p
  15047. (string-to-char (downcase (match-string 4 ans)))))
  15048. (if (and (= hour 12) (not pm))
  15049. (setq hour 0)
  15050. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15051. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15052. t t ans))))
  15053. ;; Check if a time range is given as a duration
  15054. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15055. (setq hour (string-to-number (match-string 1 ans))
  15056. h2 (+ hour (string-to-number (match-string 3 ans)))
  15057. minute (string-to-number (match-string 2 ans))
  15058. m2 (+ minute (if (match-end 5) (string-to-number
  15059. (match-string 5 ans))0)))
  15060. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15061. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15062. t t ans)))
  15063. ;; Check if there is a time range
  15064. (when (boundp 'org-end-time-was-given)
  15065. (setq org-time-was-given nil)
  15066. (when (and (string-match org-plain-time-of-day-regexp ans)
  15067. (match-end 8))
  15068. (setq org-end-time-was-given (match-string 8 ans))
  15069. (setq ans (concat (substring ans 0 (match-beginning 7))
  15070. (substring ans (match-end 7))))))
  15071. (setq tl (parse-time-string ans)
  15072. day (or (nth 3 tl) (nth 3 org-defdecode))
  15073. month (or (nth 4 tl)
  15074. (if (and org-read-date-prefer-future
  15075. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15076. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15077. (nth 4 org-defdecode)))
  15078. year (or (and (not kill-year) (nth 5 tl))
  15079. (if (and org-read-date-prefer-future
  15080. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15081. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15082. (nth 5 org-defdecode)))
  15083. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15084. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15085. second (or (nth 0 tl) 0)
  15086. wday (nth 6 tl))
  15087. (when (and (eq org-read-date-prefer-future 'time)
  15088. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15089. (equal day (nth 3 nowdecode))
  15090. (equal month (nth 4 nowdecode))
  15091. (equal year (nth 5 nowdecode))
  15092. (nth 2 tl)
  15093. (or (< (nth 2 tl) (nth 2 nowdecode))
  15094. (and (= (nth 2 tl) (nth 2 nowdecode))
  15095. (nth 1 tl)
  15096. (< (nth 1 tl) (nth 1 nowdecode)))))
  15097. (setq day (1+ day)
  15098. futurep t))
  15099. ;; Special date definitions below
  15100. (cond
  15101. (iso-week
  15102. ;; There was an iso week
  15103. (require 'cal-iso)
  15104. (setq futurep nil)
  15105. (setq year (or iso-year year)
  15106. day (or iso-weekday wday 1)
  15107. wday nil ; to make sure that the trigger below does not match
  15108. iso-date (calendar-gregorian-from-absolute
  15109. (calendar-absolute-from-iso
  15110. (list iso-week day year))))
  15111. ; FIXME: Should we also push ISO weeks into the future?
  15112. ; (when (and org-read-date-prefer-future
  15113. ; (not iso-year)
  15114. ; (< (calendar-absolute-from-gregorian iso-date)
  15115. ; (time-to-days (current-time))))
  15116. ; (setq year (1+ year)
  15117. ; iso-date (calendar-gregorian-from-absolute
  15118. ; (calendar-absolute-from-iso
  15119. ; (list iso-week day year)))))
  15120. (setq month (car iso-date)
  15121. year (nth 2 iso-date)
  15122. day (nth 1 iso-date)))
  15123. (deltan
  15124. (setq futurep nil)
  15125. (unless deltadef
  15126. (let ((now (decode-time (current-time))))
  15127. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15128. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15129. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15130. ((equal deltaw "m") (setq month (+ month deltan)))
  15131. ((equal deltaw "y") (setq year (+ year deltan)))))
  15132. ((and wday (not (nth 3 tl)))
  15133. ;; Weekday was given, but no day, so pick that day in the week
  15134. ;; on or after the derived date.
  15135. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15136. (unless (equal wday wday1)
  15137. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15138. (if (and (boundp 'org-time-was-given)
  15139. (nth 2 tl))
  15140. (setq org-time-was-given t))
  15141. (if (< year 100) (setq year (+ 2000 year)))
  15142. ;; Check of the date is representable
  15143. (if org-read-date-force-compatible-dates
  15144. (progn
  15145. (if (< year 1970)
  15146. (setq year 1970 org-read-date-analyze-forced-year t))
  15147. (if (> year 2037)
  15148. (setq year 2037 org-read-date-analyze-forced-year t)))
  15149. (condition-case nil
  15150. (ignore (encode-time second minute hour day month year))
  15151. (error
  15152. (setq year (nth 5 org-defdecode))
  15153. (setq org-read-date-analyze-forced-year t))))
  15154. (setq org-read-date-analyze-futurep futurep)
  15155. (list second minute hour day month year)))
  15156. (defvar parse-time-weekdays)
  15157. (defun org-read-date-get-relative (s today default)
  15158. "Check string S for special relative date string.
  15159. TODAY and DEFAULT are internal times, for today and for a default.
  15160. Return shift list (N what def-flag)
  15161. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15162. N is the number of WHATs to shift.
  15163. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15164. the DEFAULT date rather than TODAY."
  15165. (require 'parse-time)
  15166. (when (and
  15167. (string-match
  15168. (concat
  15169. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15170. "\\([0-9]+\\)?"
  15171. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15172. "\\([ \t]\\|$\\)") s)
  15173. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15174. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15175. (string-to-char (substring (match-string 1 s) -1))
  15176. ?+))
  15177. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15178. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15179. (what (if (match-end 3) (match-string 3 s) "d"))
  15180. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15181. (date (if rel default today))
  15182. (wday (nth 6 (decode-time date)))
  15183. delta)
  15184. (if wday1
  15185. (progn
  15186. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15187. (if (= delta 0) (setq delta 7))
  15188. (if (= dir ?-)
  15189. (progn
  15190. (setq delta (- delta 7))
  15191. (if (= delta 0) (setq delta -7))))
  15192. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15193. (list delta "d" rel))
  15194. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15195. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15196. "Turn a user-specified date into the internal representation.
  15197. The internal representation needed by the calendar is (month day year).
  15198. This is a wrapper to handle the brain-dead convention in calendar that
  15199. user function argument order change dependent on argument order."
  15200. (if (boundp 'calendar-date-style)
  15201. (cond
  15202. ((eq calendar-date-style 'american)
  15203. (list arg1 arg2 arg3))
  15204. ((eq calendar-date-style 'european)
  15205. (list arg2 arg1 arg3))
  15206. ((eq calendar-date-style 'iso)
  15207. (list arg2 arg3 arg1)))
  15208. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15209. (if (org-bound-and-true-p european-calendar-style)
  15210. (list arg2 arg1 arg3)
  15211. (list arg1 arg2 arg3)))))
  15212. (defun org-eval-in-calendar (form &optional keepdate)
  15213. "Eval FORM in the calendar window and return to current window.
  15214. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15215. otherwise stick to the current value of `org-ans2'."
  15216. (let ((sf (selected-frame))
  15217. (sw (selected-window)))
  15218. (select-window (get-buffer-window "*Calendar*" t))
  15219. (eval form)
  15220. (when (and (not keepdate) (calendar-cursor-to-date))
  15221. (let* ((date (calendar-cursor-to-date))
  15222. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15223. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15224. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15225. (select-window sw)
  15226. (org-select-frame-set-input-focus sf)))
  15227. (defun org-calendar-select ()
  15228. "Return to `org-read-date' with the date currently selected.
  15229. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15230. (interactive)
  15231. (when (calendar-cursor-to-date)
  15232. (let* ((date (calendar-cursor-to-date))
  15233. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15234. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15235. (if (active-minibuffer-window) (exit-minibuffer))))
  15236. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15237. "Insert a date stamp for the date given by the internal TIME.
  15238. WITH-HM means use the stamp format that includes the time of the day.
  15239. INACTIVE means use square brackets instead of angular ones, so that the
  15240. stamp will not contribute to the agenda.
  15241. PRE and POST are optional strings to be inserted before and after the
  15242. stamp.
  15243. The command returns the inserted time stamp."
  15244. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15245. stamp)
  15246. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15247. (insert-before-markers (or pre ""))
  15248. (when (listp extra)
  15249. (setq extra (car extra))
  15250. (if (and (stringp extra)
  15251. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15252. (setq extra (format "-%02d:%02d"
  15253. (string-to-number (match-string 1 extra))
  15254. (string-to-number (match-string 2 extra))))
  15255. (setq extra nil)))
  15256. (when extra
  15257. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15258. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15259. (insert-before-markers (or post ""))
  15260. (setq org-last-inserted-timestamp stamp)))
  15261. (defun org-toggle-time-stamp-overlays ()
  15262. "Toggle the use of custom time stamp formats."
  15263. (interactive)
  15264. (setq org-display-custom-times (not org-display-custom-times))
  15265. (unless org-display-custom-times
  15266. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15267. (while (setq p (next-single-property-change p 'display))
  15268. (if (and (get-text-property p 'display)
  15269. (eq (get-text-property p 'face) 'org-date))
  15270. (remove-text-properties
  15271. p (setq p (next-single-property-change p 'display))
  15272. '(display t))))
  15273. (set-buffer-modified-p bmp)))
  15274. (if (featurep 'xemacs)
  15275. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15276. (org-restart-font-lock)
  15277. (setq org-table-may-need-update t)
  15278. (if org-display-custom-times
  15279. (message "Time stamps are overlaid with custom format")
  15280. (message "Time stamp overlays removed")))
  15281. (defun org-display-custom-time (beg end)
  15282. "Overlay modified time stamp format over timestamp between BEG and END."
  15283. (let* ((ts (buffer-substring beg end))
  15284. t1 w1 with-hm tf time str w2 (off 0))
  15285. (save-match-data
  15286. (setq t1 (org-parse-time-string ts t))
  15287. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15288. (setq off (- (match-end 0) (match-beginning 0)))))
  15289. (setq end (- end off))
  15290. (setq w1 (- end beg)
  15291. with-hm (and (nth 1 t1) (nth 2 t1))
  15292. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15293. time (org-fix-decoded-time t1)
  15294. str (org-add-props
  15295. (format-time-string
  15296. (substring tf 1 -1) (apply 'encode-time time))
  15297. nil 'mouse-face 'highlight)
  15298. w2 (length str))
  15299. (if (not (= w2 w1))
  15300. (add-text-properties (1+ beg) (+ 2 beg)
  15301. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15302. (if (featurep 'xemacs)
  15303. (progn
  15304. (put-text-property beg end 'invisible t)
  15305. (put-text-property beg end 'end-glyph (make-glyph str)))
  15306. (put-text-property beg end 'display str))))
  15307. (defun org-translate-time (string)
  15308. "Translate all timestamps in STRING to custom format.
  15309. But do this only if the variable `org-display-custom-times' is set."
  15310. (when org-display-custom-times
  15311. (save-match-data
  15312. (let* ((start 0)
  15313. (re org-ts-regexp-both)
  15314. t1 with-hm inactive tf time str beg end)
  15315. (while (setq start (string-match re string start))
  15316. (setq beg (match-beginning 0)
  15317. end (match-end 0)
  15318. t1 (save-match-data
  15319. (org-parse-time-string (substring string beg end) t))
  15320. with-hm (and (nth 1 t1) (nth 2 t1))
  15321. inactive (equal (substring string beg (1+ beg)) "[")
  15322. tf (funcall (if with-hm 'cdr 'car)
  15323. org-time-stamp-custom-formats)
  15324. time (org-fix-decoded-time t1)
  15325. str (format-time-string
  15326. (concat
  15327. (if inactive "[" "<") (substring tf 1 -1)
  15328. (if inactive "]" ">"))
  15329. (apply 'encode-time time))
  15330. string (replace-match str t t string)
  15331. start (+ start (length str)))))))
  15332. string)
  15333. (defun org-fix-decoded-time (time)
  15334. "Set 0 instead of nil for the first 6 elements of time.
  15335. Don't touch the rest."
  15336. (let ((n 0))
  15337. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15338. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15339. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15340. "Difference between TIMESTAMP-STRING and now in days.
  15341. If SECONDS is non-nil, return the difference in seconds."
  15342. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15343. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15344. (funcall fdiff (current-time)))))
  15345. (defun org-deadline-close (timestamp-string &optional ndays)
  15346. "Is the time in TIMESTAMP-STRING close to the current date?"
  15347. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15348. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15349. (not (org-entry-is-done-p))))
  15350. (defun org-get-wdays (ts &optional delay zero-delay)
  15351. "Get the deadline lead time appropriate for timestring TS.
  15352. When DELAY is non-nil, get the delay time for scheduled items
  15353. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15354. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15355. don't try to find the delay cookie in the scheduled timestamp."
  15356. (let ((tv (if delay org-scheduled-delay-days
  15357. org-deadline-warning-days)))
  15358. (cond
  15359. ((or (and delay (< tv 0))
  15360. (and delay zero-delay (<= tv 0))
  15361. (and (not delay) (<= tv 0)))
  15362. ;; Enforce this value no matter what
  15363. (- tv))
  15364. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15365. ;; lead time is specified.
  15366. (floor (* (string-to-number (match-string 1 ts))
  15367. (cdr (assoc (match-string 2 ts)
  15368. '(("d" . 1) ("w" . 7)
  15369. ("m" . 30.4) ("y" . 365.25)
  15370. ("h" . 0.041667)))))))
  15371. ;; go for the default.
  15372. (t tv))))
  15373. (defun org-calendar-select-mouse (ev)
  15374. "Return to `org-read-date' with the date currently selected.
  15375. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15376. (interactive "e")
  15377. (mouse-set-point ev)
  15378. (when (calendar-cursor-to-date)
  15379. (let* ((date (calendar-cursor-to-date))
  15380. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15381. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15382. (if (active-minibuffer-window) (exit-minibuffer))))
  15383. (defun org-check-deadlines (ndays)
  15384. "Check if there are any deadlines due or past due.
  15385. A deadline is considered due if it happens within `org-deadline-warning-days'
  15386. days from today's date. If the deadline appears in an entry marked DONE,
  15387. it is not shown. The prefix arg NDAYS can be used to test that many
  15388. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15389. (interactive "P")
  15390. (let* ((org-warn-days
  15391. (cond
  15392. ((equal ndays '(4)) 100000)
  15393. (ndays (prefix-numeric-value ndays))
  15394. (t (abs org-deadline-warning-days))))
  15395. (case-fold-search nil)
  15396. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15397. (callback
  15398. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15399. (message "%d deadlines past-due or due within %d days"
  15400. (org-occur regexp nil callback)
  15401. org-warn-days)))
  15402. (defsubst org-re-timestamp (type)
  15403. "Return a regexp for timestamp TYPE.
  15404. Allowed values for TYPE are:
  15405. all: all timestamps
  15406. active: only active timestamps (<...>)
  15407. inactive: only inactive timestamps ([...])
  15408. scheduled: only scheduled timestamps
  15409. deadline: only deadline timestamps
  15410. closed: only closed time-stamps
  15411. When TYPE is nil, fall back on returning a regexp that matches
  15412. both scheduled and deadline timestamps."
  15413. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15414. ((eq type 'active) org-ts-regexp)
  15415. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15416. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15417. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15418. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15419. ((eq type 'scheduled-or-deadline)
  15420. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15421. (defun org-check-before-date (date)
  15422. "Check if there are deadlines or scheduled entries before DATE."
  15423. (interactive (list (org-read-date)))
  15424. (let ((case-fold-search nil)
  15425. (regexp (org-re-timestamp org-ts-type))
  15426. (callback
  15427. (lambda () (time-less-p
  15428. (org-time-string-to-time (match-string 1))
  15429. (org-time-string-to-time date)))))
  15430. (message "%d entries before %s"
  15431. (org-occur regexp nil callback) date)))
  15432. (defun org-check-after-date (date)
  15433. "Check if there are deadlines or scheduled entries after DATE."
  15434. (interactive (list (org-read-date)))
  15435. (let ((case-fold-search nil)
  15436. (regexp (org-re-timestamp org-ts-type))
  15437. (callback
  15438. (lambda () (not
  15439. (time-less-p
  15440. (org-time-string-to-time (match-string 1))
  15441. (org-time-string-to-time date))))))
  15442. (message "%d entries after %s"
  15443. (org-occur regexp nil callback) date)))
  15444. (defun org-check-dates-range (start-date end-date)
  15445. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15446. (interactive (list (org-read-date nil nil nil "Range starts")
  15447. (org-read-date nil nil nil "Range end")))
  15448. (let ((case-fold-search nil)
  15449. (regexp (org-re-timestamp org-ts-type))
  15450. (callback
  15451. (lambda ()
  15452. (let ((match (match-string 1)))
  15453. (and
  15454. (not (time-less-p
  15455. (org-time-string-to-time match)
  15456. (org-time-string-to-time start-date)))
  15457. (time-less-p
  15458. (org-time-string-to-time match)
  15459. (org-time-string-to-time end-date)))))))
  15460. (message "%d entries between %s and %s"
  15461. (org-occur regexp nil callback) start-date end-date)))
  15462. (defun org-evaluate-time-range (&optional to-buffer)
  15463. "Evaluate a time range by computing the difference between start and end.
  15464. Normally the result is just printed in the echo area, but with prefix arg
  15465. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15466. If the time range is actually in a table, the result is inserted into the
  15467. next column.
  15468. For time difference computation, a year is assumed to be exactly 365
  15469. days in order to avoid rounding problems."
  15470. (interactive "P")
  15471. (or
  15472. (org-clock-update-time-maybe)
  15473. (save-excursion
  15474. (unless (org-at-date-range-p t)
  15475. (goto-char (point-at-bol))
  15476. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15477. (if (not (org-at-date-range-p t))
  15478. (user-error "Not at a time-stamp range, and none found in current line")))
  15479. (let* ((ts1 (match-string 1))
  15480. (ts2 (match-string 2))
  15481. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15482. (match-end (match-end 0))
  15483. (time1 (org-time-string-to-time ts1))
  15484. (time2 (org-time-string-to-time ts2))
  15485. (t1 (org-float-time time1))
  15486. (t2 (org-float-time time2))
  15487. (diff (abs (- t2 t1)))
  15488. (negative (< (- t2 t1) 0))
  15489. ;; (ys (floor (* 365 24 60 60)))
  15490. (ds (* 24 60 60))
  15491. (hs (* 60 60))
  15492. (fy "%dy %dd %02d:%02d")
  15493. (fy1 "%dy %dd")
  15494. (fd "%dd %02d:%02d")
  15495. (fd1 "%dd")
  15496. (fh "%02d:%02d")
  15497. y d h m align)
  15498. (if havetime
  15499. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15500. y 0
  15501. d (floor (/ diff ds)) diff (mod diff ds)
  15502. h (floor (/ diff hs)) diff (mod diff hs)
  15503. m (floor (/ diff 60)))
  15504. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15505. y 0
  15506. d (floor (+ (/ diff ds) 0.5))
  15507. h 0 m 0))
  15508. (if (not to-buffer)
  15509. (message "%s" (org-make-tdiff-string y d h m))
  15510. (if (org-at-table-p)
  15511. (progn
  15512. (goto-char match-end)
  15513. (setq align t)
  15514. (and (looking-at " *|") (goto-char (match-end 0))))
  15515. (goto-char match-end))
  15516. (if (looking-at
  15517. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15518. (replace-match ""))
  15519. (if negative (insert " -"))
  15520. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15521. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15522. (insert " " (format fh h m))))
  15523. (if align (org-table-align))
  15524. (message "Time difference inserted")))))
  15525. (defun org-make-tdiff-string (y d h m)
  15526. (let ((fmt "")
  15527. (l nil))
  15528. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15529. l (push y l)))
  15530. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15531. l (push d l)))
  15532. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15533. l (push h l)))
  15534. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15535. l (push m l)))
  15536. (apply 'format fmt (nreverse l))))
  15537. (defun org-time-string-to-time (s &optional buffer pos)
  15538. "Convert a timestamp string into internal time."
  15539. (condition-case errdata
  15540. (apply 'encode-time (org-parse-time-string s))
  15541. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15542. s (if (not (and buffer pos))
  15543. ""
  15544. (format " at %d in buffer `%s'" pos buffer))
  15545. (cdr errdata)))))
  15546. (defun org-time-string-to-seconds (s)
  15547. "Convert a timestamp string to a number of seconds."
  15548. (org-float-time (org-time-string-to-time s)))
  15549. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15550. "Convert a time stamp to an absolute day number.
  15551. If there is a specifier for a cyclic time stamp, get the closest
  15552. date to DAYNR.
  15553. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15554. The variable `date' is bound by the calendar when this is called."
  15555. (cond
  15556. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15557. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15558. daynr
  15559. (+ daynr 1000)))
  15560. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15561. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15562. (time-to-days (current-time))) (match-string 0 s)
  15563. prefer show-all))
  15564. (t (time-to-days
  15565. (condition-case errdata
  15566. (apply 'encode-time (org-parse-time-string s))
  15567. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15568. s (if (not (and buffer pos))
  15569. ""
  15570. (format " at %d in buffer `%s'" pos buffer))
  15571. (cdr errdata))))))))
  15572. (defun org-days-to-iso-week (days)
  15573. "Return the iso week number."
  15574. (require 'cal-iso)
  15575. (car (calendar-iso-from-absolute days)))
  15576. (defun org-small-year-to-year (year)
  15577. "Convert 2-digit years into 4-digit years.
  15578. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15579. The year 2000 cannot be abbreviated. Any year larger than 99
  15580. is returned unchanged."
  15581. (if (< year 38)
  15582. (setq year (+ 2000 year))
  15583. (if (< year 100)
  15584. (setq year (+ 1900 year))))
  15585. year)
  15586. (defun org-time-from-absolute (d)
  15587. "Return the time corresponding to date D.
  15588. D may be an absolute day number, or a calendar-type list (month day year)."
  15589. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15590. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15591. (defun org-calendar-holiday ()
  15592. "List of holidays, for Diary display in Org-mode."
  15593. (require 'holidays)
  15594. (let ((hl (funcall
  15595. (if (fboundp 'calendar-check-holidays)
  15596. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15597. (if hl (mapconcat 'identity hl "; "))))
  15598. (defun org-diary-sexp-entry (sexp entry date)
  15599. "Process a SEXP diary ENTRY for DATE."
  15600. (require 'diary-lib)
  15601. (let ((result (if calendar-debug-sexp
  15602. (let ((stack-trace-on-error t))
  15603. (eval (car (read-from-string sexp))))
  15604. (condition-case nil
  15605. (eval (car (read-from-string sexp)))
  15606. (error
  15607. (beep)
  15608. (message "Bad sexp at line %d in %s: %s"
  15609. (org-current-line)
  15610. (buffer-file-name) sexp)
  15611. (sleep-for 2))))))
  15612. (cond ((stringp result) (split-string result "; "))
  15613. ((and (consp result)
  15614. (not (consp (cdr result)))
  15615. (stringp (cdr result))) (cdr result))
  15616. ((and (consp result)
  15617. (stringp (car result))) result)
  15618. (result entry))))
  15619. (defun org-diary-to-ical-string (frombuf)
  15620. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15621. This uses the icalendar.el library."
  15622. (let* ((tmpdir (if (featurep 'xemacs)
  15623. (temp-directory)
  15624. temporary-file-directory))
  15625. (tmpfile (make-temp-name
  15626. (expand-file-name "orgics" tmpdir)))
  15627. buf rtn b e)
  15628. (with-current-buffer frombuf
  15629. (icalendar-export-region (point-min) (point-max) tmpfile)
  15630. (setq buf (find-buffer-visiting tmpfile))
  15631. (set-buffer buf)
  15632. (goto-char (point-min))
  15633. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15634. (setq b (match-beginning 0)))
  15635. (goto-char (point-max))
  15636. (if (re-search-backward "^END:VEVENT" nil t)
  15637. (setq e (match-end 0)))
  15638. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15639. (kill-buffer buf)
  15640. (delete-file tmpfile)
  15641. rtn))
  15642. (defun org-closest-date (start current change prefer show-all)
  15643. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15644. When PREFER is `past', return a date that is either CURRENT or past.
  15645. When PREFER is `future', return a date that is either CURRENT or future.
  15646. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15647. ;; Make the proper lists from the dates
  15648. (catch 'exit
  15649. (let ((a1 '(("h" . hour)
  15650. ("d" . day)
  15651. ("w" . week)
  15652. ("m" . month)
  15653. ("y" . year)))
  15654. (shour (nth 2 (org-parse-time-string start)))
  15655. dn dw sday cday n1 n2 n0
  15656. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15657. (setq start (org-date-to-gregorian start)
  15658. current (org-date-to-gregorian
  15659. (if show-all
  15660. current
  15661. (time-to-days (current-time))))
  15662. sday (calendar-absolute-from-gregorian start)
  15663. cday (calendar-absolute-from-gregorian current))
  15664. (if (<= cday sday) (throw 'exit sday))
  15665. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15666. (setq dn (string-to-number (match-string 1 change))
  15667. dw (cdr (assoc (match-string 2 change) a1)))
  15668. (user-error "Invalid change specifier: %s" change))
  15669. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15670. (cond
  15671. ((eq dw 'hour)
  15672. (let ((missing-hours
  15673. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15674. dn)))
  15675. (setq n1 (if (zerop missing-hours) cday
  15676. (- cday (1+ (floor (/ missing-hours 24)))))
  15677. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15678. ((eq dw 'day)
  15679. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15680. n2 (+ n1 dn)))
  15681. ((eq dw 'year)
  15682. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15683. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15684. (setq date1 (list m d y1)
  15685. n1 (calendar-absolute-from-gregorian date1)
  15686. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15687. n2 (calendar-absolute-from-gregorian date2)))
  15688. ((eq dw 'month)
  15689. ;; approx number of month between the two dates
  15690. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15691. ;; How often does dn fit in there?
  15692. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15693. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15694. m (+ m nm)
  15695. ny (floor (/ m 12))
  15696. y (+ y ny)
  15697. m (- m (* ny 12)))
  15698. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15699. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15700. (setq m2 (+ m dn) y2 y)
  15701. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15702. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15703. (while (<= n2 cday)
  15704. (setq n1 n2 m m2 y y2)
  15705. (setq m2 (+ m dn) y2 y)
  15706. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15707. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15708. ;; Make sure n1 is the earlier date
  15709. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15710. (if show-all
  15711. (cond
  15712. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15713. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15714. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15715. (cond
  15716. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15717. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15718. (t (if (= cday n1) n1 n2)))))))
  15719. (defun org-date-to-gregorian (date)
  15720. "Turn any specification of DATE into a Gregorian date for the calendar."
  15721. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15722. ((and (listp date) (= (length date) 3)) date)
  15723. ((stringp date)
  15724. (setq date (org-parse-time-string date))
  15725. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15726. ((listp date)
  15727. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15728. (defun org-parse-time-string (s &optional nodefault)
  15729. "Parse the standard Org-mode time string.
  15730. This should be a lot faster than the normal `parse-time-string'.
  15731. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15732. hour and minute fields will be nil if not given."
  15733. (cond ((string-match org-ts-regexp0 s)
  15734. (list 0
  15735. (if (or (match-beginning 8) (not nodefault))
  15736. (string-to-number (or (match-string 8 s) "0")))
  15737. (if (or (match-beginning 7) (not nodefault))
  15738. (string-to-number (or (match-string 7 s) "0")))
  15739. (string-to-number (match-string 4 s))
  15740. (string-to-number (match-string 3 s))
  15741. (string-to-number (match-string 2 s))
  15742. nil nil nil))
  15743. ((string-match "^<[^>]+>$" s)
  15744. (decode-time (seconds-to-time (org-matcher-time s))))
  15745. (t (error "Not a standard Org-mode time string: %s" s))))
  15746. (defun org-timestamp-up (&optional arg)
  15747. "Increase the date item at the cursor by one.
  15748. If the cursor is on the year, change the year. If it is on the month,
  15749. the day or the time, change that.
  15750. With prefix ARG, change by that many units."
  15751. (interactive "p")
  15752. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15753. (defun org-timestamp-down (&optional arg)
  15754. "Decrease the date item at the cursor by one.
  15755. If the cursor is on the year, change the year. If it is on the month,
  15756. the day or the time, change that.
  15757. With prefix ARG, change by that many units."
  15758. (interactive "p")
  15759. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15760. (defun org-timestamp-up-day (&optional arg)
  15761. "Increase the date in the time stamp by one day.
  15762. With prefix ARG, change that many days."
  15763. (interactive "p")
  15764. (if (and (not (org-at-timestamp-p t))
  15765. (org-at-heading-p))
  15766. (org-todo 'up)
  15767. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15768. (defun org-timestamp-down-day (&optional arg)
  15769. "Decrease the date in the time stamp by one day.
  15770. With prefix ARG, change that many days."
  15771. (interactive "p")
  15772. (if (and (not (org-at-timestamp-p t))
  15773. (org-at-heading-p))
  15774. (org-todo 'down)
  15775. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15776. (defun org-at-timestamp-p (&optional inactive-ok)
  15777. "Determine if the cursor is in or at a timestamp."
  15778. (interactive)
  15779. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15780. (pos (point))
  15781. (ans (or (looking-at tsr)
  15782. (save-excursion
  15783. (skip-chars-backward "^[<\n\r\t")
  15784. (if (> (point) (point-min)) (backward-char 1))
  15785. (and (looking-at tsr)
  15786. (> (- (match-end 0) pos) -1))))))
  15787. (and ans
  15788. (boundp 'org-ts-what)
  15789. (setq org-ts-what
  15790. (cond
  15791. ((= pos (match-beginning 0)) 'bracket)
  15792. ;; Point is considered to be "on the bracket" whether
  15793. ;; it's really on it or right after it.
  15794. ((= pos (1- (match-end 0))) 'bracket)
  15795. ((= pos (match-end 0)) 'after)
  15796. ((org-pos-in-match-range pos 2) 'year)
  15797. ((org-pos-in-match-range pos 3) 'month)
  15798. ((org-pos-in-match-range pos 7) 'hour)
  15799. ((org-pos-in-match-range pos 8) 'minute)
  15800. ((or (org-pos-in-match-range pos 4)
  15801. (org-pos-in-match-range pos 5)) 'day)
  15802. ((and (> pos (or (match-end 8) (match-end 5)))
  15803. (< pos (match-end 0)))
  15804. (- pos (or (match-end 8) (match-end 5))))
  15805. (t 'day))))
  15806. ans))
  15807. (defun org-toggle-timestamp-type ()
  15808. "Toggle the type (<active> or [inactive]) of a time stamp."
  15809. (interactive)
  15810. (when (org-at-timestamp-p t)
  15811. (let ((beg (match-beginning 0)) (end (match-end 0))
  15812. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15813. (save-excursion
  15814. (goto-char beg)
  15815. (while (re-search-forward "[][<>]" end t)
  15816. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15817. t t)))
  15818. (message "Timestamp is now %sactive"
  15819. (if (equal (char-after beg) ?<) "" "in")))))
  15820. (defun org-at-clock-log-p nil
  15821. "Is the cursor on the clock log line?"
  15822. (save-excursion
  15823. (move-beginning-of-line 1)
  15824. (looking-at "^[ \t]*CLOCK:")))
  15825. (defvar org-clock-history) ; defined in org-clock.el
  15826. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15827. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15828. "Change the date in the time stamp at point.
  15829. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15830. `year', `minute', `second'. If WHAT is not given, the cursor position
  15831. in the timestamp determines what will be changed.
  15832. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15833. (let ((origin (point)) origin-cat
  15834. with-hm inactive
  15835. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15836. org-ts-what
  15837. extra rem
  15838. ts time time0 fixnext clrgx)
  15839. (if (not (org-at-timestamp-p t))
  15840. (user-error "Not at a timestamp"))
  15841. (if (and (not what) (eq org-ts-what 'bracket))
  15842. (org-toggle-timestamp-type)
  15843. ;; Point isn't on brackets. Remember the part of the time-stamp
  15844. ;; the point was in. Indeed, size of time-stamps may change,
  15845. ;; but point must be kept in the same category nonetheless.
  15846. (setq origin-cat org-ts-what)
  15847. (if (and (not what) (not (eq org-ts-what 'day))
  15848. org-display-custom-times
  15849. (get-text-property (point) 'display)
  15850. (not (get-text-property (1- (point)) 'display)))
  15851. (setq org-ts-what 'day))
  15852. (setq org-ts-what (or what org-ts-what)
  15853. inactive (= (char-after (match-beginning 0)) ?\[)
  15854. ts (match-string 0))
  15855. (replace-match "")
  15856. (when (string-match
  15857. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15858. ts)
  15859. (setq extra (match-string 1 ts))
  15860. (if suppress-tmp-delay
  15861. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15862. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15863. (setq with-hm t))
  15864. (setq time0 (org-parse-time-string ts))
  15865. (when (and updown
  15866. (eq org-ts-what 'minute)
  15867. (not current-prefix-arg))
  15868. ;; This looks like s-up and s-down. Change by one rounding step.
  15869. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15870. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15871. (setcar (cdr time0) (+ (nth 1 time0)
  15872. (if (> n 0) (- rem) (- dm rem))))))
  15873. (setq time
  15874. (encode-time (or (car time0) 0)
  15875. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15876. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15877. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15878. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15879. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15880. (nthcdr 6 time0)))
  15881. (when (and (member org-ts-what '(hour minute))
  15882. extra
  15883. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15884. (setq extra (org-modify-ts-extra
  15885. extra
  15886. (if (eq org-ts-what 'hour) 2 5)
  15887. n dm)))
  15888. (when (integerp org-ts-what)
  15889. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15890. (if (eq what 'calendar)
  15891. (let ((cal-date (org-get-date-from-calendar)))
  15892. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15893. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15894. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15895. (setcar time0 (or (car time0) 0))
  15896. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15897. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15898. (setq time (apply 'encode-time time0))))
  15899. ;; Insert the new time-stamp, and ensure point stays in the same
  15900. ;; category as before (i.e. not after the last position in that
  15901. ;; category).
  15902. (let ((pos (point)))
  15903. ;; Stay before inserted string. `save-excursion' is of no use.
  15904. (setq org-last-changed-timestamp
  15905. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15906. (goto-char pos))
  15907. (save-match-data
  15908. (looking-at org-ts-regexp3)
  15909. (goto-char (cond
  15910. ;; `day' category ends before `hour' if any, or at
  15911. ;; the end of the day name.
  15912. ((eq origin-cat 'day)
  15913. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15914. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15915. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15916. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15917. ;; `year' and `month' have both fixed size: point
  15918. ;; couldn't have moved into another part.
  15919. (t origin))))
  15920. ;; Update clock if on a CLOCK line.
  15921. (org-clock-update-time-maybe)
  15922. ;; Maybe adjust the closest clock in `org-clock-history'
  15923. (when org-clock-adjust-closest
  15924. (if (not (and (org-at-clock-log-p)
  15925. (< 1 (length (delq nil (mapcar 'marker-position
  15926. org-clock-history))))))
  15927. (message "No clock to adjust")
  15928. (cond ((save-excursion ; fix previous clock?
  15929. (re-search-backward org-ts-regexp0 nil t)
  15930. (org-looking-back (concat org-clock-string " \\[")))
  15931. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15932. ((save-excursion ; fix next clock?
  15933. (re-search-backward org-ts-regexp0 nil t)
  15934. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15935. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15936. (save-window-excursion
  15937. ;; Find closest clock to point, adjust the previous/next one in history
  15938. (let* ((p (save-excursion (org-back-to-heading t)))
  15939. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15940. (clfixnth
  15941. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15942. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15943. (if (not clfixpos)
  15944. (message "No clock to adjust")
  15945. (save-excursion
  15946. (org-goto-marker-or-bmk clfixpos)
  15947. (org-show-subtree)
  15948. (when (re-search-forward clrgx nil t)
  15949. (goto-char (match-beginning 1))
  15950. (let (org-clock-adjust-closest)
  15951. (org-timestamp-change n org-ts-what updown))
  15952. (message "Clock adjusted in %s for heading: %s"
  15953. (file-name-nondirectory (buffer-file-name))
  15954. (org-get-heading t t)))))))))
  15955. ;; Try to recenter the calendar window, if any.
  15956. (if (and org-calendar-follow-timestamp-change
  15957. (get-buffer-window "*Calendar*" t)
  15958. (memq org-ts-what '(day month year)))
  15959. (org-recenter-calendar (time-to-days time))))))
  15960. (defun org-modify-ts-extra (s pos n dm)
  15961. "Change the different parts of the lead-time and repeat fields in timestamp."
  15962. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15963. ng h m new rem)
  15964. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15965. (cond
  15966. ((or (org-pos-in-match-range pos 2)
  15967. (org-pos-in-match-range pos 3))
  15968. (setq m (string-to-number (match-string 3 s))
  15969. h (string-to-number (match-string 2 s)))
  15970. (if (org-pos-in-match-range pos 2)
  15971. (setq h (+ h n))
  15972. (setq n (* dm (org-no-warnings (signum n))))
  15973. (when (not (= 0 (setq rem (% m dm))))
  15974. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15975. (setq m (+ m n)))
  15976. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15977. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15978. (setq h (min 24 (max 0 h)))
  15979. (setq ng 1 new (format "-%02d:%02d" h m)))
  15980. ((org-pos-in-match-range pos 6)
  15981. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15982. ((org-pos-in-match-range pos 5)
  15983. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15984. ((org-pos-in-match-range pos 9)
  15985. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15986. ((org-pos-in-match-range pos 8)
  15987. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15988. (when ng
  15989. (setq s (concat
  15990. (substring s 0 (match-beginning ng))
  15991. new
  15992. (substring s (match-end ng))))))
  15993. s))
  15994. (defun org-recenter-calendar (date)
  15995. "If the calendar is visible, recenter it to DATE."
  15996. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15997. (when cwin
  15998. (let ((calendar-move-hook nil))
  15999. (with-selected-window cwin
  16000. (calendar-goto-date (if (listp date) date
  16001. (calendar-gregorian-from-absolute date))))))))
  16002. (defun org-goto-calendar (&optional arg)
  16003. "Go to the Emacs calendar at the current date.
  16004. If there is a time stamp in the current line, go to that date.
  16005. A prefix ARG can be used to force the current date."
  16006. (interactive "P")
  16007. (let ((tsr org-ts-regexp) diff
  16008. (calendar-move-hook nil)
  16009. (calendar-view-holidays-initially-flag nil)
  16010. (calendar-view-diary-initially-flag nil))
  16011. (if (or (org-at-timestamp-p)
  16012. (save-excursion
  16013. (beginning-of-line 1)
  16014. (looking-at (concat ".*" tsr))))
  16015. (let ((d1 (time-to-days (current-time)))
  16016. (d2 (time-to-days
  16017. (org-time-string-to-time (match-string 1)))))
  16018. (setq diff (- d2 d1))))
  16019. (calendar)
  16020. (calendar-goto-today)
  16021. (if (and diff (not arg)) (calendar-forward-day diff))))
  16022. (defun org-get-date-from-calendar ()
  16023. "Return a list (month day year) of date at point in calendar."
  16024. (with-current-buffer "*Calendar*"
  16025. (save-match-data
  16026. (calendar-cursor-to-date))))
  16027. (defun org-date-from-calendar ()
  16028. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16029. If there is already a time stamp at the cursor position, update it."
  16030. (interactive)
  16031. (if (org-at-timestamp-p t)
  16032. (org-timestamp-change 0 'calendar)
  16033. (let ((cal-date (org-get-date-from-calendar)))
  16034. (org-insert-time-stamp
  16035. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16036. (defcustom org-effort-durations
  16037. `(("h" . 60)
  16038. ("d" . ,(* 60 8))
  16039. ("w" . ,(* 60 8 5))
  16040. ("m" . ,(* 60 8 5 4))
  16041. ("y" . ,(* 60 8 5 40)))
  16042. "Conversion factor to minutes for an effort modifier.
  16043. Each entry has the form (MODIFIER . MINUTES).
  16044. In an effort string, a number followed by MODIFIER is multiplied
  16045. by the specified number of MINUTES to obtain an effort in
  16046. minutes.
  16047. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16048. effort string \"2hours\" is equivalent to 120 minutes."
  16049. :group 'org-agenda
  16050. :version "24.1"
  16051. :type '(alist :key-type (string :tag "Modifier")
  16052. :value-type (number :tag "Minutes")))
  16053. (defun org-minutes-to-clocksum-string (m)
  16054. "Format number of minutes as a clocksum string.
  16055. The format is determined by `org-time-clocksum-format',
  16056. `org-time-clocksum-use-fractional' and
  16057. `org-time-clocksum-fractional-format' and
  16058. `org-time-clocksum-use-effort-durations'."
  16059. (let ((clocksum "")
  16060. (m (round m)) ; Don't allow fractions of minutes
  16061. h d w mo y fmt n)
  16062. (setq h (if org-time-clocksum-use-effort-durations
  16063. (cdr (assoc "h" org-effort-durations)) 60)
  16064. d (if org-time-clocksum-use-effort-durations
  16065. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16066. w (if org-time-clocksum-use-effort-durations
  16067. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16068. mo (if org-time-clocksum-use-effort-durations
  16069. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16070. y (if org-time-clocksum-use-effort-durations
  16071. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16072. ;; fractional format
  16073. (if org-time-clocksum-use-fractional
  16074. (cond
  16075. ;; single format string
  16076. ((stringp org-time-clocksum-fractional-format)
  16077. (format org-time-clocksum-fractional-format (/ m (float h))))
  16078. ;; choice of fractional formats for different time units
  16079. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16080. (> (/ (truncate m) (* y d h)) 0))
  16081. (format fmt (/ m (* y d (float h)))))
  16082. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16083. (> (/ (truncate m) (* mo d h)) 0))
  16084. (format fmt (/ m (* mo d (float h)))))
  16085. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16086. (> (/ (truncate m) (* w d h)) 0))
  16087. (format fmt (/ m (* w d (float h)))))
  16088. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16089. (> (/ (truncate m) (* d h)) 0))
  16090. (format fmt (/ m (* d (float h)))))
  16091. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16092. (> (/ (truncate m) h) 0))
  16093. (format fmt (/ m (float h))))
  16094. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16095. (format fmt m))
  16096. ;; fall back to smallest time unit with a format
  16097. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16098. (format fmt (/ m (float h))))
  16099. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16100. (format fmt (/ m (* d (float h)))))
  16101. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16102. (format fmt (/ m (* w d (float h)))))
  16103. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16104. (format fmt (/ m (* mo d (float h)))))
  16105. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16106. (format fmt (/ m (* y d (float h))))))
  16107. ;; standard (non-fractional) format, with single format string
  16108. (if (stringp org-time-clocksum-format)
  16109. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16110. ;; separate formats components
  16111. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16112. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16113. (plist-get org-time-clocksum-format :require-years))
  16114. (setq clocksum (concat clocksum (format fmt n))
  16115. m (- m (* n y d h))))
  16116. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16117. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16118. (plist-get org-time-clocksum-format :require-months))
  16119. (setq clocksum (concat clocksum (format fmt n))
  16120. m (- m (* n mo d h))))
  16121. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16122. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16123. (plist-get org-time-clocksum-format :require-weeks))
  16124. (setq clocksum (concat clocksum (format fmt n))
  16125. m (- m (* n w d h))))
  16126. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16127. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16128. (plist-get org-time-clocksum-format :require-days))
  16129. (setq clocksum (concat clocksum (format fmt n))
  16130. m (- m (* n d h))))
  16131. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16132. (or (> (setq n (/ (truncate m) h)) 0)
  16133. (plist-get org-time-clocksum-format :require-hours))
  16134. (setq clocksum (concat clocksum (format fmt n))
  16135. m (- m (* n h))))
  16136. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16137. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16138. (setq clocksum (concat clocksum (format fmt m))))
  16139. ;; return formatted time duration
  16140. clocksum))))
  16141. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16142. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16143. "Org mode version 8.0")
  16144. (defun org-hours-to-clocksum-string (n)
  16145. (org-minutes-to-clocksum-string (* n 60)))
  16146. (defun org-hh:mm-string-to-minutes (s)
  16147. "Convert a string H:MM to a number of minutes.
  16148. If the string is just a number, interpret it as minutes.
  16149. In fact, the first hh:mm or number in the string will be taken,
  16150. there can be extra stuff in the string.
  16151. If no number is found, the return value is 0."
  16152. (cond
  16153. ((integerp s) s)
  16154. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16155. (+ (* (string-to-number (match-string 1 s)) 60)
  16156. (string-to-number (match-string 2 s))))
  16157. ((string-match "\\([0-9]+\\)" s)
  16158. (string-to-number (match-string 1 s)))
  16159. (t 0)))
  16160. (defcustom org-image-actual-width t
  16161. "Should we use the actual width of images when inlining them?
  16162. When set to `t', always use the image width.
  16163. When set to a number, use imagemagick (when available) to set
  16164. the image's width to this value.
  16165. When set to a number in a list, try to get the width from any
  16166. #+ATTR.* keyword if it matches a width specification like
  16167. #+ATTR_HTML: :width 300px
  16168. and fall back on that number if none is found.
  16169. When set to nil, try to get the width from an #+ATTR.* keyword
  16170. and fall back on the original width if none is found.
  16171. This requires Emacs >= 24.1, build with imagemagick support."
  16172. :group 'org-appearance
  16173. :version "24.4"
  16174. :package-version '(Org . "8.0")
  16175. :type '(choice
  16176. (const :tag "Use the image width" t)
  16177. (integer :tag "Use a number of pixels")
  16178. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16179. (const :tag "Use #+ATTR* or don't resize" nil)))
  16180. (defcustom org-agenda-inhibit-startup nil
  16181. "Inhibit startup when preparing agenda buffers.
  16182. When this variable is `t' (the default), the initialization of
  16183. the Org agenda buffers is inhibited: e.g. the visibility state
  16184. is not set, the tables are not re-aligned, etc."
  16185. :type 'boolean
  16186. :version "24.3"
  16187. :group 'org-agenda)
  16188. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16189. "Convert a duration string S to minutes.
  16190. A bare number is interpreted as minutes, modifiers can be set by
  16191. customizing `org-effort-durations' (which see).
  16192. Entries containing a colon are interpreted as H:MM by
  16193. `org-hh:mm-string-to-minutes'."
  16194. (let ((result 0)
  16195. (re (concat "\\([0-9.]+\\) *\\("
  16196. (regexp-opt (mapcar 'car org-effort-durations))
  16197. "\\)")))
  16198. (while (string-match re s)
  16199. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16200. (string-to-number (match-string 1 s))))
  16201. (setq s (replace-match "" nil t s)))
  16202. (setq result (floor result))
  16203. (incf result (org-hh:mm-string-to-minutes s))
  16204. (if output-to-string (number-to-string result) result)))
  16205. ;;;; Files
  16206. (defun org-save-all-org-buffers ()
  16207. "Save all Org-mode buffers without user confirmation."
  16208. (interactive)
  16209. (message "Saving all Org-mode buffers...")
  16210. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16211. (when (featurep 'org-id) (org-id-locations-save))
  16212. (message "Saving all Org-mode buffers... done"))
  16213. (defun org-revert-all-org-buffers ()
  16214. "Revert all Org-mode buffers.
  16215. Prompt for confirmation when there are unsaved changes.
  16216. Be sure you know what you are doing before letting this function
  16217. overwrite your changes.
  16218. This function is useful in a setup where one tracks org files
  16219. with a version control system, to revert on one machine after pulling
  16220. changes from another. I believe the procedure must be like this:
  16221. 1. M-x org-save-all-org-buffers
  16222. 2. Pull changes from the other machine, resolve conflicts
  16223. 3. M-x org-revert-all-org-buffers"
  16224. (interactive)
  16225. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16226. (user-error "Abort"))
  16227. (save-excursion
  16228. (save-window-excursion
  16229. (mapc
  16230. (lambda (b)
  16231. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16232. (with-current-buffer b buffer-file-name))
  16233. (org-pop-to-buffer-same-window b)
  16234. (revert-buffer t 'no-confirm)))
  16235. (buffer-list))
  16236. (when (and (featurep 'org-id) org-id-track-globally)
  16237. (org-id-locations-load)))))
  16238. ;;;; Agenda files
  16239. ;;;###autoload
  16240. (defun org-switchb (&optional arg)
  16241. "Switch between Org buffers.
  16242. With one prefix argument, restrict available buffers to files.
  16243. With two prefix arguments, restrict available buffers to agenda files.
  16244. Defaults to `iswitchb' for buffer name completion.
  16245. Set `org-completion-use-ido' to make it use ido instead."
  16246. (interactive "P")
  16247. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16248. ((equal arg '(16)) (org-buffer-list 'agenda))
  16249. (t (org-buffer-list))))
  16250. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16251. (org-completion-use-ido org-completion-use-ido))
  16252. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16253. (setq org-completion-use-iswitchb t))
  16254. (org-pop-to-buffer-same-window
  16255. (org-icompleting-read "Org buffer: "
  16256. (mapcar 'list (mapcar 'buffer-name blist))
  16257. nil t))))
  16258. ;;; Define some older names previously used for this functionality
  16259. ;;;###autoload
  16260. (defalias 'org-ido-switchb 'org-switchb)
  16261. ;;;###autoload
  16262. (defalias 'org-iswitchb 'org-switchb)
  16263. (defun org-buffer-list (&optional predicate exclude-tmp)
  16264. "Return a list of Org buffers.
  16265. PREDICATE can be `export', `files' or `agenda'.
  16266. export restrict the list to Export buffers.
  16267. files restrict the list to buffers visiting Org files.
  16268. agenda restrict the list to buffers visiting agenda files.
  16269. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16270. (let* ((bfn nil)
  16271. (agenda-files (and (eq predicate 'agenda)
  16272. (mapcar 'file-truename (org-agenda-files t))))
  16273. (filter
  16274. (cond
  16275. ((eq predicate 'files)
  16276. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16277. ((eq predicate 'export)
  16278. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16279. ((eq predicate 'agenda)
  16280. (lambda (b)
  16281. (with-current-buffer b
  16282. (and (derived-mode-p 'org-mode)
  16283. (setq bfn (buffer-file-name b))
  16284. (member (file-truename bfn) agenda-files)))))
  16285. (t (lambda (b) (with-current-buffer b
  16286. (or (derived-mode-p 'org-mode)
  16287. (string-match "\*Org .*Export"
  16288. (buffer-name b)))))))))
  16289. (delq nil
  16290. (mapcar
  16291. (lambda(b)
  16292. (if (and (funcall filter b)
  16293. (or (not exclude-tmp)
  16294. (not (string-match "tmp" (buffer-name b)))))
  16295. b
  16296. nil))
  16297. (buffer-list)))))
  16298. (defun org-agenda-files (&optional unrestricted archives)
  16299. "Get the list of agenda files.
  16300. Optional UNRESTRICTED means return the full list even if a restriction
  16301. is currently in place.
  16302. When ARCHIVES is t, include all archive files that are really being
  16303. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16304. `org-agenda-archives-mode' is t."
  16305. (let ((files
  16306. (cond
  16307. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16308. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16309. ((listp org-agenda-files) org-agenda-files)
  16310. (t (error "Invalid value of `org-agenda-files'")))))
  16311. (setq files (apply 'append
  16312. (mapcar (lambda (f)
  16313. (if (file-directory-p f)
  16314. (directory-files
  16315. f t org-agenda-file-regexp)
  16316. (list f)))
  16317. files)))
  16318. (when org-agenda-skip-unavailable-files
  16319. (setq files (delq nil
  16320. (mapcar (function
  16321. (lambda (file)
  16322. (and (file-readable-p file) file)))
  16323. files))))
  16324. (when (or (eq archives t)
  16325. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16326. (setq files (org-add-archive-files files)))
  16327. files))
  16328. (defun org-agenda-file-p (&optional file)
  16329. "Return non-nil, if FILE is an agenda file.
  16330. If FILE is omitted, use the file associated with the current
  16331. buffer."
  16332. (member (or file (buffer-file-name))
  16333. (org-agenda-files t)))
  16334. (defun org-edit-agenda-file-list ()
  16335. "Edit the list of agenda files.
  16336. Depending on setup, this either uses customize to edit the variable
  16337. `org-agenda-files', or it visits the file that is holding the list. In the
  16338. latter case, the buffer is set up in a way that saving it automatically kills
  16339. the buffer and restores the previous window configuration."
  16340. (interactive)
  16341. (if (stringp org-agenda-files)
  16342. (let ((cw (current-window-configuration)))
  16343. (find-file org-agenda-files)
  16344. (org-set-local 'org-window-configuration cw)
  16345. (org-add-hook 'after-save-hook
  16346. (lambda ()
  16347. (set-window-configuration
  16348. (prog1 org-window-configuration
  16349. (kill-buffer (current-buffer))))
  16350. (org-install-agenda-files-menu)
  16351. (message "New agenda file list installed"))
  16352. nil 'local)
  16353. (message "%s" (substitute-command-keys
  16354. "Edit list and finish with \\[save-buffer]")))
  16355. (customize-variable 'org-agenda-files)))
  16356. (defun org-store-new-agenda-file-list (list)
  16357. "Set new value for the agenda file list and save it correctly."
  16358. (if (stringp org-agenda-files)
  16359. (let ((fe (org-read-agenda-file-list t)) b u)
  16360. (while (setq b (find-buffer-visiting org-agenda-files))
  16361. (kill-buffer b))
  16362. (with-temp-file org-agenda-files
  16363. (insert
  16364. (mapconcat
  16365. (lambda (f) ;; Keep un-expanded entries.
  16366. (if (setq u (assoc f fe))
  16367. (cdr u)
  16368. f))
  16369. list "\n")
  16370. "\n")))
  16371. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16372. (org-insert-mode-line-in-empty-file nil))
  16373. (setq org-agenda-files list)
  16374. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16375. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16376. "Read the list of agenda files from a file.
  16377. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16378. filenames, used by `org-store-new-agenda-file-list' to write back
  16379. un-expanded file names."
  16380. (when (file-directory-p org-agenda-files)
  16381. (error "`org-agenda-files' cannot be a single directory"))
  16382. (when (stringp org-agenda-files)
  16383. (with-temp-buffer
  16384. (insert-file-contents org-agenda-files)
  16385. (mapcar
  16386. (lambda (f)
  16387. (let ((e (expand-file-name (substitute-in-file-name f)
  16388. org-directory)))
  16389. (if pair-with-expansion
  16390. (cons e f)
  16391. e)))
  16392. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16393. ;;;###autoload
  16394. (defun org-cycle-agenda-files ()
  16395. "Cycle through the files in `org-agenda-files'.
  16396. If the current buffer visits an agenda file, find the next one in the list.
  16397. If the current buffer does not, find the first agenda file."
  16398. (interactive)
  16399. (let* ((fs (org-agenda-files t))
  16400. (files (append fs (list (car fs))))
  16401. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16402. file)
  16403. (unless files (user-error "No agenda files"))
  16404. (catch 'exit
  16405. (while (setq file (pop files))
  16406. (if (equal (file-truename file) tcf)
  16407. (when (car files)
  16408. (find-file (car files))
  16409. (throw 'exit t))))
  16410. (find-file (car fs)))
  16411. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16412. (defun org-agenda-file-to-front (&optional to-end)
  16413. "Move/add the current file to the top of the agenda file list.
  16414. If the file is not present in the list, it is added to the front. If it is
  16415. present, it is moved there. With optional argument TO-END, add/move to the
  16416. end of the list."
  16417. (interactive "P")
  16418. (let ((org-agenda-skip-unavailable-files nil)
  16419. (file-alist (mapcar (lambda (x)
  16420. (cons (file-truename x) x))
  16421. (org-agenda-files t)))
  16422. (ctf (file-truename
  16423. (or buffer-file-name
  16424. (user-error "Please save the current buffer to a file"))))
  16425. x had)
  16426. (setq x (assoc ctf file-alist) had x)
  16427. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16428. (if to-end
  16429. (setq file-alist (append (delq x file-alist) (list x)))
  16430. (setq file-alist (cons x (delq x file-alist))))
  16431. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16432. (org-install-agenda-files-menu)
  16433. (message "File %s to %s of agenda file list"
  16434. (if had "moved" "added") (if to-end "end" "front"))))
  16435. (defun org-remove-file (&optional file)
  16436. "Remove current file from the list of files in variable `org-agenda-files'.
  16437. These are the files which are being checked for agenda entries.
  16438. Optional argument FILE means use this file instead of the current."
  16439. (interactive)
  16440. (let* ((org-agenda-skip-unavailable-files nil)
  16441. (file (or file buffer-file-name
  16442. (user-error "Current buffer does not visit a file")))
  16443. (true-file (file-truename file))
  16444. (afile (abbreviate-file-name file))
  16445. (files (delq nil (mapcar
  16446. (lambda (x)
  16447. (if (equal true-file
  16448. (file-truename x))
  16449. nil x))
  16450. (org-agenda-files t)))))
  16451. (if (not (= (length files) (length (org-agenda-files t))))
  16452. (progn
  16453. (org-store-new-agenda-file-list files)
  16454. (org-install-agenda-files-menu)
  16455. (message "Removed file: %s" afile))
  16456. (message "File was not in list: %s (not removed)" afile))))
  16457. (defun org-file-menu-entry (file)
  16458. (vector file (list 'find-file file) t))
  16459. (defun org-check-agenda-file (file)
  16460. "Make sure FILE exists. If not, ask user what to do."
  16461. (when (not (file-exists-p file))
  16462. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16463. (abbreviate-file-name file))
  16464. (let ((r (downcase (read-char-exclusive))))
  16465. (cond
  16466. ((equal r ?r)
  16467. (org-remove-file file)
  16468. (throw 'nextfile t))
  16469. (t (error "Abort"))))))
  16470. (defun org-get-agenda-file-buffer (file)
  16471. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16472. it to the list of buffers which might be released later."
  16473. (let ((buf (org-find-base-buffer-visiting file)))
  16474. (if buf
  16475. buf ; just return it
  16476. ;; Make a new buffer and remember it
  16477. (setq buf (find-file-noselect file))
  16478. (if buf (push buf org-agenda-new-buffers))
  16479. buf)))
  16480. (defun org-release-buffers (blist)
  16481. "Release all buffers in list, asking the user for confirmation when needed.
  16482. When a buffer is unmodified, it is just killed. When modified, it is saved
  16483. \(if the user agrees) and then killed."
  16484. (let (buf file)
  16485. (while (setq buf (pop blist))
  16486. (setq file (buffer-file-name buf))
  16487. (when (and (buffer-modified-p buf)
  16488. file
  16489. (y-or-n-p (format "Save file %s? " file)))
  16490. (with-current-buffer buf (save-buffer)))
  16491. (kill-buffer buf))))
  16492. (defun org-agenda-prepare-buffers (files)
  16493. "Create buffers for all agenda files, protect archived trees and comments."
  16494. (interactive)
  16495. (let ((pa '(:org-archived t))
  16496. (pc '(:org-comment t))
  16497. (pall '(:org-archived t :org-comment t))
  16498. (inhibit-read-only t)
  16499. (org-inhibit-startup org-agenda-inhibit-startup)
  16500. (rea (concat ":" org-archive-tag ":"))
  16501. file re)
  16502. (setq org-tag-alist-for-agenda nil
  16503. org-tag-groups-alist-for-agenda nil)
  16504. (save-excursion
  16505. (save-restriction
  16506. (while (setq file (pop files))
  16507. (catch 'nextfile
  16508. (if (bufferp file)
  16509. (set-buffer file)
  16510. (org-check-agenda-file file)
  16511. (set-buffer (org-get-agenda-file-buffer file)))
  16512. (widen)
  16513. (org-set-regexps-and-options-for-tags)
  16514. (goto-char (point-min))
  16515. (let ((case-fold-search t))
  16516. (when (search-forward "#+setupfile" nil t)
  16517. ;; Don't set all regexps and options systematically as
  16518. ;; this is only run for setting agenda tags from setup
  16519. ;; file
  16520. (org-set-regexps-and-options)))
  16521. (org-refresh-category-properties)
  16522. (org-refresh-properties org-effort-property 'org-effort)
  16523. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16524. (setq org-todo-keywords-for-agenda
  16525. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16526. (setq org-done-keywords-for-agenda
  16527. (append org-done-keywords-for-agenda org-done-keywords))
  16528. (setq org-todo-keyword-alist-for-agenda
  16529. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16530. (setq org-drawers-for-agenda
  16531. (append org-drawers-for-agenda org-drawers))
  16532. (setq org-tag-alist-for-agenda
  16533. (org-uniquify
  16534. (append org-tag-alist-for-agenda
  16535. org-tag-alist
  16536. org-tag-persistent-alist)))
  16537. (if org-group-tags
  16538. (setq org-tag-groups-alist-for-agenda
  16539. (org-uniquify-alist
  16540. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16541. (org-with-silent-modifications
  16542. (save-excursion
  16543. (remove-text-properties (point-min) (point-max) pall)
  16544. (when org-agenda-skip-archived-trees
  16545. (goto-char (point-min))
  16546. (while (re-search-forward rea nil t)
  16547. (if (org-at-heading-p t)
  16548. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16549. (goto-char (point-min))
  16550. (setq re (format org-heading-keyword-regexp-format
  16551. org-comment-string))
  16552. (while (re-search-forward re nil t)
  16553. (add-text-properties
  16554. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16555. (setq org-todo-keywords-for-agenda
  16556. (org-uniquify org-todo-keywords-for-agenda))
  16557. (setq org-todo-keyword-alist-for-agenda
  16558. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16559. ;;;; CDLaTeX minor mode
  16560. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16561. "Keymap for the minor `org-cdlatex-mode'.")
  16562. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16563. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16564. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16565. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16566. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16567. (defvar org-cdlatex-texmathp-advice-is-done nil
  16568. "Flag remembering if we have applied the advice to texmathp already.")
  16569. (define-minor-mode org-cdlatex-mode
  16570. "Toggle the minor `org-cdlatex-mode'.
  16571. This mode supports entering LaTeX environment and math in LaTeX fragments
  16572. in Org-mode.
  16573. \\{org-cdlatex-mode-map}"
  16574. nil " OCDL" nil
  16575. (when org-cdlatex-mode
  16576. (require 'cdlatex)
  16577. (run-hooks 'cdlatex-mode-hook)
  16578. (cdlatex-compute-tables))
  16579. (unless org-cdlatex-texmathp-advice-is-done
  16580. (setq org-cdlatex-texmathp-advice-is-done t)
  16581. (defadvice texmathp (around org-math-always-on activate)
  16582. "Always return t in org-mode buffers.
  16583. This is because we want to insert math symbols without dollars even outside
  16584. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16585. an embedded LaTeX fragment, let texmathp do its job.
  16586. \\[org-cdlatex-mode-map]"
  16587. (interactive)
  16588. (let (p)
  16589. (cond
  16590. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16591. ((eq this-command 'cdlatex-math-symbol)
  16592. (setq ad-return-value t
  16593. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16594. (t
  16595. (let ((p (org-inside-LaTeX-fragment-p)))
  16596. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16597. (setq ad-return-value t
  16598. texmathp-why '("Org-mode embedded math" . 0))
  16599. (if p ad-do-it)))))))))
  16600. (defun turn-on-org-cdlatex ()
  16601. "Unconditionally turn on `org-cdlatex-mode'."
  16602. (org-cdlatex-mode 1))
  16603. (defun org-try-cdlatex-tab ()
  16604. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16605. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16606. - inside a LaTeX fragment, or
  16607. - after the first word in a line, where an abbreviation expansion could
  16608. insert a LaTeX environment."
  16609. (when org-cdlatex-mode
  16610. (cond
  16611. ;; Before any word on the line: No expansion possible.
  16612. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16613. ;; Just after first word on the line: Expand it. Make sure it
  16614. ;; cannot happen on headlines, though.
  16615. ((save-excursion
  16616. (skip-chars-backward "a-zA-Z0-9*")
  16617. (skip-chars-backward " \t")
  16618. (and (bolp) (not (org-at-heading-p))))
  16619. (cdlatex-tab) t)
  16620. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16621. (defun org-cdlatex-underscore-caret (&optional arg)
  16622. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16623. Revert to the normal definition outside of these fragments."
  16624. (interactive "P")
  16625. (if (org-inside-LaTeX-fragment-p)
  16626. (call-interactively 'cdlatex-sub-superscript)
  16627. (let (org-cdlatex-mode)
  16628. (call-interactively (key-binding (vector last-input-event))))))
  16629. (defun org-cdlatex-math-modify (&optional arg)
  16630. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16631. Revert to the normal definition outside of these fragments."
  16632. (interactive "P")
  16633. (if (org-inside-LaTeX-fragment-p)
  16634. (call-interactively 'cdlatex-math-modify)
  16635. (let (org-cdlatex-mode)
  16636. (call-interactively (key-binding (vector last-input-event))))))
  16637. ;;;; LaTeX fragments
  16638. (defvar org-latex-regexps
  16639. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16640. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16641. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16642. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16643. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16644. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16645. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16646. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16647. "Regular expressions for matching embedded LaTeX.")
  16648. (defun org-inside-LaTeX-fragment-p ()
  16649. "Test if point is inside a LaTeX fragment.
  16650. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16651. sequence appearing also before point.
  16652. Even though the matchers for math are configurable, this function assumes
  16653. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16654. delimiters are skipped when they have been removed by customization.
  16655. The return value is nil, or a cons cell with the delimiter and the
  16656. position of this delimiter.
  16657. This function does a reasonably good job, but can locally be fooled by
  16658. for example currency specifications. For example it will assume being in
  16659. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16660. fragments that are properly closed, but during editing, we have to live
  16661. with the uncertainty caused by missing closing delimiters. This function
  16662. looks only before point, not after."
  16663. (catch 'exit
  16664. (let ((pos (point))
  16665. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16666. (lim (progn
  16667. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16668. (point)))
  16669. dd-on str (start 0) m re)
  16670. (goto-char pos)
  16671. (when dodollar
  16672. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16673. re (nth 1 (assoc "$" org-latex-regexps)))
  16674. (while (string-match re str start)
  16675. (cond
  16676. ((= (match-end 0) (length str))
  16677. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16678. ((= (match-end 0) (- (length str) 5))
  16679. (throw 'exit nil))
  16680. (t (setq start (match-end 0))))))
  16681. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16682. (goto-char pos)
  16683. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16684. (and (match-beginning 2) (throw 'exit nil))
  16685. ;; count $$
  16686. (while (re-search-backward "\\$\\$" lim t)
  16687. (setq dd-on (not dd-on)))
  16688. (goto-char pos)
  16689. (if dd-on (cons "$$" m))))))
  16690. (defun org-inside-latex-macro-p ()
  16691. "Is point inside a LaTeX macro or its arguments?"
  16692. (save-match-data
  16693. (org-in-regexp
  16694. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16695. (defvar org-latex-fragment-image-overlays nil
  16696. "List of overlays carrying the images of latex fragments.")
  16697. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16698. (defun org-remove-latex-fragment-image-overlays ()
  16699. "Remove all overlays with LaTeX fragment images in current buffer."
  16700. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16701. (setq org-latex-fragment-image-overlays nil))
  16702. (defun org-preview-latex-fragment (&optional subtree)
  16703. "Preview the LaTeX fragment at point, or all locally or globally.
  16704. If the cursor is in a LaTeX fragment, create the image and overlay
  16705. it over the source code. If there is no fragment at point, display
  16706. all fragments in the current text, from one headline to the next. With
  16707. prefix SUBTREE, display all fragments in the current subtree. With a
  16708. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16709. the cursor is before the first headline,
  16710. display all fragments in the buffer.
  16711. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16712. (interactive "P")
  16713. (unless buffer-file-name
  16714. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16715. (org-remove-latex-fragment-image-overlays)
  16716. (save-excursion
  16717. (save-restriction
  16718. (let (beg end at msg)
  16719. (cond
  16720. ((or (equal subtree '(16))
  16721. (not (save-excursion
  16722. (re-search-backward org-outline-regexp-bol nil t))))
  16723. (setq beg (point-min) end (point-max)
  16724. msg "Creating images for buffer...%s"))
  16725. ((equal subtree '(4))
  16726. (org-back-to-heading)
  16727. (setq beg (point) end (org-end-of-subtree t)
  16728. msg "Creating images for subtree...%s"))
  16729. (t
  16730. (if (setq at (org-inside-LaTeX-fragment-p))
  16731. (goto-char (max (point-min) (- (cdr at) 2)))
  16732. (org-back-to-heading))
  16733. (setq beg (point) end (progn (outline-next-heading) (point))
  16734. msg (if at "Creating image...%s"
  16735. "Creating images for entry...%s"))))
  16736. (message msg "")
  16737. (narrow-to-region beg end)
  16738. (goto-char beg)
  16739. (org-format-latex
  16740. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16741. (file-name-nondirectory
  16742. buffer-file-name)))
  16743. default-directory 'overlays msg at 'forbuffer
  16744. org-latex-create-formula-image-program)
  16745. (message msg "done. Use `C-c C-c' to remove images.")))))
  16746. (defun org-format-latex (prefix &optional dir overlays msg at
  16747. forbuffer processing-type)
  16748. "Replace LaTeX fragments with links to an image, and produce images.
  16749. Some of the options can be changed using the variable
  16750. `org-format-latex-options'."
  16751. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16752. (let* ((prefixnodir (file-name-nondirectory prefix))
  16753. (absprefix (expand-file-name prefix dir))
  16754. (todir (file-name-directory absprefix))
  16755. (opt org-format-latex-options)
  16756. (optnew org-format-latex-options)
  16757. (matchers (plist-get opt :matchers))
  16758. (re-list org-latex-regexps)
  16759. (cnt 0) txt hash link beg end re e checkdir
  16760. string
  16761. m n block-type block linkfile movefile ov)
  16762. ;; Check the different regular expressions
  16763. (while (setq e (pop re-list))
  16764. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16765. block (if block-type "\n\n" ""))
  16766. (when (member m matchers)
  16767. (goto-char (point-min))
  16768. (while (re-search-forward re nil t)
  16769. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16770. (or (not overlays)
  16771. (not (eq (get-char-property (match-beginning n)
  16772. 'org-overlay-type)
  16773. 'org-latex-overlay))))
  16774. (cond
  16775. ((eq processing-type 'verbatim))
  16776. ((eq processing-type 'mathjax)
  16777. ;; Prepare for MathJax processing.
  16778. (setq string (match-string n))
  16779. (when (member m '("$" "$1"))
  16780. (save-excursion
  16781. (delete-region (match-beginning n) (match-end n))
  16782. (goto-char (match-beginning n))
  16783. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16784. ((or (eq processing-type 'dvipng)
  16785. (eq processing-type 'imagemagick))
  16786. ;; Process to an image.
  16787. (setq txt (match-string n)
  16788. beg (match-beginning n) end (match-end n)
  16789. cnt (1+ cnt))
  16790. (let ((face (face-at-point))
  16791. (fg (plist-get opt :foreground))
  16792. (bg (plist-get opt :background))
  16793. ;; Ensure full list is printed.
  16794. print-length print-level)
  16795. (when forbuffer
  16796. ;; Get the colors from the face at point.
  16797. (goto-char beg)
  16798. (when (eq fg 'auto)
  16799. (setq fg (face-attribute face :foreground nil 'default)))
  16800. (when (eq bg 'auto)
  16801. (setq bg (face-attribute face :background nil 'default)))
  16802. (setq optnew (copy-sequence opt))
  16803. (plist-put optnew :foreground fg)
  16804. (plist-put optnew :background bg))
  16805. (setq hash (sha1 (prin1-to-string
  16806. (list org-format-latex-header
  16807. org-latex-default-packages-alist
  16808. org-latex-packages-alist
  16809. org-format-latex-options
  16810. forbuffer txt fg bg)))
  16811. linkfile (format "%s_%s.png" prefix hash)
  16812. movefile (format "%s_%s.png" absprefix hash)))
  16813. (setq link (concat block "[[file:" linkfile "]]" block))
  16814. (if msg (message msg cnt))
  16815. (goto-char beg)
  16816. (unless checkdir ; Ensure the directory exists.
  16817. (setq checkdir t)
  16818. (or (file-directory-p todir) (make-directory todir t)))
  16819. (unless (file-exists-p movefile)
  16820. (org-create-formula-image
  16821. txt movefile optnew forbuffer processing-type))
  16822. (if overlays
  16823. (progn
  16824. (mapc (lambda (o)
  16825. (if (eq (overlay-get o 'org-overlay-type)
  16826. 'org-latex-overlay)
  16827. (delete-overlay o)))
  16828. (overlays-in beg end))
  16829. (setq ov (make-overlay beg end))
  16830. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16831. (if (featurep 'xemacs)
  16832. (progn
  16833. (overlay-put ov 'invisible t)
  16834. (overlay-put
  16835. ov 'end-glyph
  16836. (make-glyph (vector 'png :file movefile))))
  16837. (overlay-put
  16838. ov 'display
  16839. (list 'image :type 'png :file movefile :ascent 'center)))
  16840. (push ov org-latex-fragment-image-overlays)
  16841. (goto-char end))
  16842. (delete-region beg end)
  16843. (insert (org-add-props link
  16844. (list 'org-latex-src
  16845. (replace-regexp-in-string
  16846. "\"" "" txt)
  16847. 'org-latex-src-embed-type
  16848. (if block-type 'paragraph 'character))))))
  16849. ((eq processing-type 'mathml)
  16850. ;; Process to MathML
  16851. (unless (save-match-data (org-format-latex-mathml-available-p))
  16852. (user-error "LaTeX to MathML converter not configured"))
  16853. (setq txt (match-string n)
  16854. beg (match-beginning n) end (match-end n)
  16855. cnt (1+ cnt))
  16856. (if msg (message msg cnt))
  16857. (goto-char beg)
  16858. (delete-region beg end)
  16859. (insert (org-format-latex-as-mathml
  16860. txt block-type prefix dir)))
  16861. (t
  16862. (error "Unknown conversion type %s for LaTeX fragments"
  16863. processing-type)))))))))
  16864. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16865. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16866. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16867. sucessful, return the portion between \"<math...> </math>\"
  16868. elements otherwise return nil. When MATHML-FILE is specified,
  16869. write the results in to that file. When invoked as an
  16870. interactive command, prompt for LATEX-FRAG, with initial value
  16871. set to the current active region and echo the results for user
  16872. inspection."
  16873. (interactive (list (let ((frag (when (org-region-active-p)
  16874. (buffer-substring-no-properties
  16875. (region-beginning) (region-end)))))
  16876. (read-string "LaTeX Fragment: " frag nil frag))))
  16877. (unless latex-frag (error "Invalid LaTeX fragment"))
  16878. (let* ((tmp-in-file (file-relative-name
  16879. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16880. (ignore (write-region latex-frag nil tmp-in-file))
  16881. (tmp-out-file (file-relative-name
  16882. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16883. (cmd (format-spec
  16884. org-latex-to-mathml-convert-command
  16885. `((?j . ,(shell-quote-argument
  16886. (expand-file-name org-latex-to-mathml-jar-file)))
  16887. (?I . ,(shell-quote-argument tmp-in-file))
  16888. (?o . ,(shell-quote-argument tmp-out-file)))))
  16889. mathml shell-command-output)
  16890. (when (org-called-interactively-p 'any)
  16891. (unless (org-format-latex-mathml-available-p)
  16892. (user-error "LaTeX to MathML converter not configured")))
  16893. (message "Running %s" cmd)
  16894. (setq shell-command-output (shell-command-to-string cmd))
  16895. (setq mathml
  16896. (when (file-readable-p tmp-out-file)
  16897. (with-current-buffer (find-file-noselect tmp-out-file t)
  16898. (goto-char (point-min))
  16899. (when (re-search-forward
  16900. (concat
  16901. (regexp-quote
  16902. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16903. "\\(.\\|\n\\)*"
  16904. (regexp-quote "</math>")) nil t)
  16905. (prog1 (match-string 0) (kill-buffer))))))
  16906. (cond
  16907. (mathml
  16908. (setq mathml
  16909. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16910. (when mathml-file
  16911. (write-region mathml nil mathml-file))
  16912. (when (org-called-interactively-p 'any)
  16913. (message mathml)))
  16914. ((message "LaTeX to MathML conversion failed")
  16915. (message shell-command-output)))
  16916. (delete-file tmp-in-file)
  16917. (when (file-exists-p tmp-out-file)
  16918. (delete-file tmp-out-file))
  16919. mathml))
  16920. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16921. prefix &optional dir)
  16922. "Use `org-create-math-formula' but check local cache first."
  16923. (let* ((absprefix (expand-file-name prefix dir))
  16924. (print-length nil) (print-level nil)
  16925. (formula-id (concat
  16926. "formula-"
  16927. (sha1
  16928. (prin1-to-string
  16929. (list latex-frag
  16930. org-latex-to-mathml-convert-command)))))
  16931. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16932. (formula-cache-dir (file-name-directory formula-cache)))
  16933. (unless (file-directory-p formula-cache-dir)
  16934. (make-directory formula-cache-dir t))
  16935. (unless (file-exists-p formula-cache)
  16936. (org-create-math-formula latex-frag formula-cache))
  16937. (if (file-exists-p formula-cache)
  16938. ;; Successful conversion. Return the link to MathML file.
  16939. (org-add-props
  16940. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16941. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16942. 'org-latex-src-embed-type (if latex-frag-type
  16943. 'paragraph 'character)))
  16944. ;; Failed conversion. Return the LaTeX fragment verbatim
  16945. latex-frag)))
  16946. (defun org-create-formula-image (string tofile options buffer &optional type)
  16947. "Create an image from LaTeX source using dvipng or convert.
  16948. This function calls either `org-create-formula-image-with-dvipng'
  16949. or `org-create-formula-image-with-imagemagick' depending on the
  16950. value of `org-latex-create-formula-image-program' or on the value
  16951. of the optional TYPE variable.
  16952. Note: ultimately these two function should be combined as they
  16953. share a good deal of logic."
  16954. (org-check-external-command
  16955. "latex" "needed to convert LaTeX fragments to images")
  16956. (funcall
  16957. (case (or type org-latex-create-formula-image-program)
  16958. ('dvipng
  16959. (org-check-external-command
  16960. "dvipng" "needed to convert LaTeX fragments to images")
  16961. #'org-create-formula-image-with-dvipng)
  16962. ('imagemagick
  16963. (org-check-external-command
  16964. "convert" "you need to install imagemagick")
  16965. #'org-create-formula-image-with-imagemagick)
  16966. (t (error
  16967. "Invalid value of `org-latex-create-formula-image-program'")))
  16968. string tofile options buffer))
  16969. (declare-function org-export--get-global-options "ox" (&optional backend))
  16970. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16971. (defun org-create-formula--latex-header ()
  16972. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16973. (org-latex-guess-inputenc
  16974. (org-splice-latex-header
  16975. org-format-latex-header
  16976. org-latex-default-packages-alist
  16977. org-latex-packages-alist t
  16978. (plist-get
  16979. (org-combine-plists
  16980. (org-export--get-global-options 'latex)
  16981. (org-export--get-inbuffer-options 'latex))
  16982. :latex-header))))
  16983. ;; This function borrows from Ganesh Swami's latex2png.el
  16984. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16985. "This calls dvipng."
  16986. (require 'ox-latex)
  16987. (let* ((tmpdir (if (featurep 'xemacs)
  16988. (temp-directory)
  16989. temporary-file-directory))
  16990. (texfilebase (make-temp-name
  16991. (expand-file-name "orgtex" tmpdir)))
  16992. (texfile (concat texfilebase ".tex"))
  16993. (dvifile (concat texfilebase ".dvi"))
  16994. (pngfile (concat texfilebase ".png"))
  16995. (fnh (if (featurep 'xemacs)
  16996. (font-height (face-font 'default))
  16997. (face-attribute 'default :height nil)))
  16998. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16999. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17000. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17001. "Black"))
  17002. (bg (or (plist-get options (if buffer :background :html-background))
  17003. "Transparent")))
  17004. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17005. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17006. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17007. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17008. (let ((latex-header (org-create-formula--latex-header)))
  17009. (with-temp-file texfile
  17010. (insert latex-header)
  17011. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17012. (let ((dir default-directory))
  17013. (condition-case nil
  17014. (progn
  17015. (cd tmpdir)
  17016. (call-process "latex" nil nil nil texfile))
  17017. (error nil))
  17018. (cd dir))
  17019. (if (not (file-exists-p dvifile))
  17020. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17021. (condition-case nil
  17022. (if (featurep 'xemacs)
  17023. (call-process "dvipng" nil nil nil
  17024. "-fg" fg "-bg" bg
  17025. "-T" "tight"
  17026. "-o" pngfile
  17027. dvifile)
  17028. (call-process "dvipng" nil nil nil
  17029. "-fg" fg "-bg" bg
  17030. "-D" dpi
  17031. ;;"-x" scale "-y" scale
  17032. "-T" "tight"
  17033. "-o" pngfile
  17034. dvifile))
  17035. (error nil))
  17036. (if (not (file-exists-p pngfile))
  17037. (if org-format-latex-signal-error
  17038. (error "Failed to create png file from %s" texfile)
  17039. (message "Failed to create png file from %s" texfile)
  17040. nil)
  17041. ;; Use the requested file name and clean up
  17042. (copy-file pngfile tofile 'replace)
  17043. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17044. (if (file-exists-p (concat texfilebase e))
  17045. (delete-file (concat texfilebase e))))
  17046. pngfile))))
  17047. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17048. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17049. "This calls convert, which is included into imagemagick."
  17050. (require 'ox-latex)
  17051. (let* ((tmpdir (if (featurep 'xemacs)
  17052. (temp-directory)
  17053. temporary-file-directory))
  17054. (texfilebase (make-temp-name
  17055. (expand-file-name "orgtex" tmpdir)))
  17056. (texfile (concat texfilebase ".tex"))
  17057. (pdffile (concat texfilebase ".pdf"))
  17058. (pngfile (concat texfilebase ".png"))
  17059. (fnh (if (featurep 'xemacs)
  17060. (font-height (face-font 'default))
  17061. (face-attribute 'default :height nil)))
  17062. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17063. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17064. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17065. "black"))
  17066. (bg (or (plist-get options (if buffer :background :html-background))
  17067. "white")))
  17068. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17069. (setq fg (org-latex-color-format fg)))
  17070. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17071. (setq bg (org-latex-color-format
  17072. (if (string= bg "Transparent") "white" bg))))
  17073. (let ((latex-header (org-create-formula--latex-header)))
  17074. (with-temp-file texfile
  17075. (insert latex-header)
  17076. (insert "\n\\begin{document}\n"
  17077. "\\definecolor{fg}{rgb}{" fg "}\n"
  17078. "\\definecolor{bg}{rgb}{" bg "}\n"
  17079. "\n\\pagecolor{bg}\n"
  17080. "\n{\\color{fg}\n"
  17081. string
  17082. "\n}\n"
  17083. "\n\\end{document}\n")))
  17084. (org-latex-compile texfile t)
  17085. (if (not (file-exists-p pdffile))
  17086. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17087. (condition-case nil
  17088. (if (featurep 'xemacs)
  17089. (call-process "convert" nil nil nil
  17090. "-density" "96"
  17091. "-trim"
  17092. "-antialias"
  17093. pdffile
  17094. "-quality" "100"
  17095. ;; "-sharpen" "0x1.0"
  17096. pngfile)
  17097. (call-process "convert" nil nil nil
  17098. "-density" dpi
  17099. "-trim"
  17100. "-antialias"
  17101. pdffile
  17102. "-quality" "100"
  17103. ;; "-sharpen" "0x1.0"
  17104. pngfile))
  17105. (error nil))
  17106. (if (not (file-exists-p pngfile))
  17107. (if org-format-latex-signal-error
  17108. (error "Failed to create png file from %s" texfile)
  17109. (message "Failed to create png file from %s" texfile)
  17110. nil)
  17111. ;; Use the requested file name and clean up
  17112. (copy-file pngfile tofile 'replace)
  17113. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17114. (if (file-exists-p (concat texfilebase e))
  17115. (delete-file (concat texfilebase e))))
  17116. pngfile))))
  17117. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17118. "Fill a LaTeX header template TPL.
  17119. In the template, the following place holders will be recognized:
  17120. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17121. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17122. [PACKAGES] \\usepackage statements for PKG
  17123. [NO-PACKAGES] do not include PKG
  17124. [EXTRA] the string EXTRA
  17125. [NO-EXTRA] do not include EXTRA
  17126. For backward compatibility, if both the positive and the negative place
  17127. holder is missing, the positive one (without the \"NO-\") will be
  17128. assumed to be present at the end of the template.
  17129. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17130. EXTRA is a string.
  17131. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17132. (let (rpl (end ""))
  17133. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17134. (setq rpl (if (or (match-end 1) (not def-pkg))
  17135. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17136. tpl (replace-match rpl t t tpl))
  17137. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17138. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17139. (setq rpl (if (or (match-end 1) (not pkg))
  17140. "" (org-latex-packages-to-string pkg snippets-p t))
  17141. tpl (replace-match rpl t t tpl))
  17142. (if pkg (setq end
  17143. (concat end "\n"
  17144. (org-latex-packages-to-string pkg snippets-p)))))
  17145. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17146. (setq rpl (if (or (match-end 1) (not extra))
  17147. "" (concat extra "\n"))
  17148. tpl (replace-match rpl t t tpl))
  17149. (if (and extra (string-match "\\S-" extra))
  17150. (setq end (concat end "\n" extra))))
  17151. (if (string-match "\\S-" end)
  17152. (concat tpl "\n" end)
  17153. tpl)))
  17154. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17155. "Turn an alist of packages into a string with the \\usepackage macros."
  17156. (setq pkg (mapconcat (lambda(p)
  17157. (cond
  17158. ((stringp p) p)
  17159. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17160. (format "%% Package %s omitted" (cadr p)))
  17161. ((equal "" (car p))
  17162. (format "\\usepackage{%s}" (cadr p)))
  17163. (t
  17164. (format "\\usepackage[%s]{%s}"
  17165. (car p) (cadr p)))))
  17166. pkg
  17167. "\n"))
  17168. (if newline (concat pkg "\n") pkg))
  17169. (defun org-dvipng-color (attr)
  17170. "Return a RGB color specification for dvipng."
  17171. (apply 'format "rgb %s %s %s"
  17172. (mapcar 'org-normalize-color
  17173. (if (featurep 'xemacs)
  17174. (color-rgb-components
  17175. (face-property 'default
  17176. (cond ((eq attr :foreground) 'foreground)
  17177. ((eq attr :background) 'background))))
  17178. (color-values (face-attribute 'default attr nil))))))
  17179. (defun org-dvipng-color-format (color-name)
  17180. "Convert COLOR-NAME to a RGB color value for dvipng."
  17181. (apply 'format "rgb %s %s %s"
  17182. (mapcar 'org-normalize-color
  17183. (color-values color-name))))
  17184. (defun org-latex-color (attr)
  17185. "Return a RGB color for the LaTeX color package."
  17186. (apply 'format "%s,%s,%s"
  17187. (mapcar 'org-normalize-color
  17188. (if (featurep 'xemacs)
  17189. (color-rgb-components
  17190. (face-property 'default
  17191. (cond ((eq attr :foreground) 'foreground)
  17192. ((eq attr :background) 'background))))
  17193. (color-values (face-attribute 'default attr nil))))))
  17194. (defun org-latex-color-format (color-name)
  17195. "Convert COLOR-NAME to a RGB color value."
  17196. (apply 'format "%s,%s,%s"
  17197. (mapcar 'org-normalize-color
  17198. (color-values color-name))))
  17199. (defun org-normalize-color (value)
  17200. "Return string to be used as color value for an RGB component."
  17201. (format "%g" (/ value 65535.0)))
  17202. ;; Image display
  17203. (defvar org-inline-image-overlays nil)
  17204. (make-variable-buffer-local 'org-inline-image-overlays)
  17205. (defun org-toggle-inline-images (&optional include-linked)
  17206. "Toggle the display of inline images.
  17207. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17208. (interactive "P")
  17209. (if org-inline-image-overlays
  17210. (progn
  17211. (org-remove-inline-images)
  17212. (message "Inline image display turned off"))
  17213. (org-display-inline-images include-linked)
  17214. (if (and (org-called-interactively-p)
  17215. org-inline-image-overlays)
  17216. (message "%d images displayed inline"
  17217. (length org-inline-image-overlays))
  17218. (message "No images to display inline"))))
  17219. (defun org-redisplay-inline-images ()
  17220. "Refresh the display of inline images."
  17221. (interactive)
  17222. (if (not org-inline-image-overlays)
  17223. (org-toggle-inline-images)
  17224. (org-toggle-inline-images)
  17225. (org-toggle-inline-images)))
  17226. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17227. "Display inline images.
  17228. Normally only links without a description part are inlined, because this
  17229. is how it will work for export. When INCLUDE-LINKED is set, also links
  17230. with a description part will be inlined. This can be nice for a quick
  17231. look at those images, but it does not reflect what exported files will look
  17232. like.
  17233. When REFRESH is set, refresh existing images between BEG and END.
  17234. This will create new image displays only if necessary.
  17235. BEG and END default to the buffer boundaries."
  17236. (interactive "P")
  17237. (unless refresh
  17238. (org-remove-inline-images)
  17239. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17240. (save-excursion
  17241. (save-restriction
  17242. (widen)
  17243. (setq beg (or beg (point-min)) end (or end (point-max)))
  17244. (goto-char beg)
  17245. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17246. (substring (org-image-file-name-regexp) 0 -2)
  17247. "\\)\\]" (if include-linked "" "\\]")))
  17248. (case-fold-search t)
  17249. old file ov img type attrwidth width)
  17250. (while (re-search-forward re end t)
  17251. (setq old (get-char-property-and-overlay (match-beginning 1)
  17252. 'org-image-overlay)
  17253. file (expand-file-name
  17254. (concat (or (match-string 3) "") (match-string 4))))
  17255. (when (image-type-available-p 'imagemagick)
  17256. (setq attrwidth (if (or (listp org-image-actual-width)
  17257. (null org-image-actual-width))
  17258. (save-excursion
  17259. (save-match-data
  17260. (when (re-search-backward
  17261. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17262. (save-excursion
  17263. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17264. (string-to-number (match-string 1))))))
  17265. width (cond ((eq org-image-actual-width t) nil)
  17266. ((null org-image-actual-width) attrwidth)
  17267. ((numberp org-image-actual-width)
  17268. org-image-actual-width)
  17269. ((listp org-image-actual-width)
  17270. (or attrwidth (car org-image-actual-width))))
  17271. type (if width 'imagemagick)))
  17272. (when (file-exists-p file)
  17273. (if (and (car-safe old) refresh)
  17274. (image-refresh (overlay-get (cdr old) 'display))
  17275. (setq img (save-match-data (create-image file type nil :width width)))
  17276. (when img
  17277. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17278. (overlay-put ov 'display img)
  17279. (overlay-put ov 'face 'default)
  17280. (overlay-put ov 'org-image-overlay t)
  17281. (overlay-put ov 'modification-hooks
  17282. (list 'org-display-inline-remove-overlay))
  17283. (push ov org-inline-image-overlays)))))))))
  17284. (define-obsolete-function-alias
  17285. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17286. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17287. "Remove inline-display overlay if a corresponding region is modified."
  17288. (let ((inhibit-modification-hooks t))
  17289. (when (and ov after)
  17290. (delete ov org-inline-image-overlays)
  17291. (delete-overlay ov))))
  17292. (defun org-remove-inline-images ()
  17293. "Remove inline display of images."
  17294. (interactive)
  17295. (mapc 'delete-overlay org-inline-image-overlays)
  17296. (setq org-inline-image-overlays nil))
  17297. ;;;; Key bindings
  17298. ;; Outline functions from `outline-mode-prefix-map'
  17299. ;; that can be remapped in Org:
  17300. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17301. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17302. (define-key org-mode-map [remap outline-forward-same-level]
  17303. 'org-forward-heading-same-level)
  17304. (define-key org-mode-map [remap outline-backward-same-level]
  17305. 'org-backward-heading-same-level)
  17306. (define-key org-mode-map [remap show-branches]
  17307. 'org-kill-note-or-show-branches)
  17308. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17309. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17310. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17311. ;; Outline functions from `outline-mode-prefix-map' that can not
  17312. ;; be remapped in Org:
  17313. ;;
  17314. ;; - the column "key binding" shows whether the Outline function is still
  17315. ;; available in Org mode on the same key that it has been bound to in
  17316. ;; Outline mode:
  17317. ;; - "overridden": key used for a different functionality in Org mode
  17318. ;; - else: key still bound to the same Outline function in Org mode
  17319. ;;
  17320. ;; | Outline function | key binding | Org replacement |
  17321. ;; |------------------------------------+-------------+-----------------------|
  17322. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17323. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17324. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17325. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17326. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17327. ;; | `show-entry' | overridden | no replacement |
  17328. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17329. ;; | `show-branches' | `C-c C-k' | still same function |
  17330. ;; | `show-subtree' | overridden | visibility cycling |
  17331. ;; | `show-all' | overridden | no replacement |
  17332. ;; | `hide-subtree' | overridden | visibility cycling |
  17333. ;; | `hide-body' | overridden | no replacement |
  17334. ;; | `hide-entry' | overridden | visibility cycling |
  17335. ;; | `hide-leaves' | overridden | no replacement |
  17336. ;; | `hide-sublevels' | overridden | no replacement |
  17337. ;; | `hide-other' | overridden | no replacement |
  17338. ;; Make `C-c C-x' a prefix key
  17339. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17340. ;; TAB key with modifiers
  17341. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17342. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17343. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17344. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17345. ;; The following line is necessary under Suse GNU/Linux
  17346. (unless (featurep 'xemacs)
  17347. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17348. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17349. (define-key org-mode-map [backtab] 'org-shifttab)
  17350. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17351. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17352. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17353. ;; Cursor keys with modifiers
  17354. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17355. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17356. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17357. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17358. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17359. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17360. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17361. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17362. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17363. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17364. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17365. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17366. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17367. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17368. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17369. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17370. ;; Babel keys
  17371. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17372. (mapc (lambda (pair)
  17373. (define-key org-babel-map (car pair) (cdr pair)))
  17374. org-babel-key-bindings)
  17375. ;;; Extra keys for tty access.
  17376. ;; We only set them when really needed because otherwise the
  17377. ;; menus don't show the simple keys
  17378. (when (or org-use-extra-keys
  17379. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17380. (not window-system))
  17381. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17382. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17383. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17384. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17385. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17386. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17387. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17388. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17389. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17390. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17391. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17392. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17393. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17394. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17395. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17396. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17397. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17398. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17399. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17400. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17401. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17402. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17403. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17404. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17405. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17406. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17407. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17408. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17409. ;; All the other keys
  17410. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17411. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17412. (if (boundp 'narrow-map)
  17413. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17414. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17415. (if (boundp 'narrow-map)
  17416. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17417. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17418. (if (boundp 'narrow-map)
  17419. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17420. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17421. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17422. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17423. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17424. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17425. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17426. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17427. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17428. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17429. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17430. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17431. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17432. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17433. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17434. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17435. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17436. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17437. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17438. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17439. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17440. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17441. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17442. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17443. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17444. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17445. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17446. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17447. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17448. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17449. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17450. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17451. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17452. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17453. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17454. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17455. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17456. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17457. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17458. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17459. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17460. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17461. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17462. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17463. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17464. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17465. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17466. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17467. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17468. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17469. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17470. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17471. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17472. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17473. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17474. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17475. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17476. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17477. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17478. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17479. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17480. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17481. (org-defkey org-mode-map "\C-m" 'org-return)
  17482. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17483. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17484. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17485. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17486. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17487. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17488. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17489. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17490. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17491. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17492. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17493. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17494. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17495. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17496. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17497. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17498. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17499. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17500. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17501. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17502. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17503. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17504. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17505. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17506. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17507. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17508. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17509. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17510. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17511. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17512. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17513. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17514. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17515. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17516. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17517. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17518. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17519. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17520. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17521. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17522. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17523. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17524. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17525. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17526. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17527. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17528. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17529. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17530. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17531. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17532. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17533. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17534. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17535. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17536. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17537. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17538. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17539. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17540. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17541. (when (featurep 'xemacs)
  17542. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17543. (defconst org-speed-commands-default
  17544. '(
  17545. ("Outline Navigation")
  17546. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17547. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17548. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17549. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17550. ("F" . org-next-block)
  17551. ("B" . org-previous-block)
  17552. ("u" . (org-speed-move-safe 'outline-up-heading))
  17553. ("j" . org-goto)
  17554. ("g" . (org-refile t))
  17555. ("Outline Visibility")
  17556. ("c" . org-cycle)
  17557. ("C" . org-shifttab)
  17558. (" " . org-display-outline-path)
  17559. ("s" . org-narrow-to-subtree)
  17560. ("=" . org-columns)
  17561. ("Outline Structure Editing")
  17562. ("U" . org-shiftmetaup)
  17563. ("D" . org-shiftmetadown)
  17564. ("r" . org-metaright)
  17565. ("l" . org-metaleft)
  17566. ("R" . org-shiftmetaright)
  17567. ("L" . org-shiftmetaleft)
  17568. ("i" . (progn (forward-char 1) (call-interactively
  17569. 'org-insert-heading-respect-content)))
  17570. ("^" . org-sort)
  17571. ("w" . org-refile)
  17572. ("a" . org-archive-subtree-default-with-confirmation)
  17573. ("@" . org-mark-subtree)
  17574. ("#" . org-toggle-comment)
  17575. ("Clock Commands")
  17576. ("I" . org-clock-in)
  17577. ("O" . org-clock-out)
  17578. ("Meta Data Editing")
  17579. ("t" . org-todo)
  17580. ("," . (org-priority))
  17581. ("0" . (org-priority ?\ ))
  17582. ("1" . (org-priority ?A))
  17583. ("2" . (org-priority ?B))
  17584. ("3" . (org-priority ?C))
  17585. (":" . org-set-tags-command)
  17586. ("e" . org-set-effort)
  17587. ("E" . org-inc-effort)
  17588. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17589. (org-entry-put (point) "APPT_WARNTIME" m)))
  17590. ("Agenda Views etc")
  17591. ("v" . org-agenda)
  17592. ("/" . org-sparse-tree)
  17593. ("Misc")
  17594. ("o" . org-open-at-point)
  17595. ("?" . org-speed-command-help)
  17596. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17597. (">" . (org-agenda-remove-restriction-lock))
  17598. )
  17599. "The default speed commands.")
  17600. (defun org-print-speed-command (e)
  17601. (if (> (length (car e)) 1)
  17602. (progn
  17603. (princ "\n")
  17604. (princ (car e))
  17605. (princ "\n")
  17606. (princ (make-string (length (car e)) ?-))
  17607. (princ "\n"))
  17608. (princ (car e))
  17609. (princ " ")
  17610. (if (symbolp (cdr e))
  17611. (princ (symbol-name (cdr e)))
  17612. (prin1 (cdr e)))
  17613. (princ "\n")))
  17614. (defun org-speed-command-help ()
  17615. "Show the available speed commands."
  17616. (interactive)
  17617. (if (not org-use-speed-commands)
  17618. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17619. (with-output-to-temp-buffer "*Help*"
  17620. (princ "User-defined Speed commands\n===========================\n")
  17621. (mapc 'org-print-speed-command org-speed-commands-user)
  17622. (princ "\n")
  17623. (princ "Built-in Speed commands\n=======================\n")
  17624. (mapc 'org-print-speed-command org-speed-commands-default))
  17625. (with-current-buffer "*Help*"
  17626. (setq truncate-lines t))))
  17627. (defun org-speed-move-safe (cmd)
  17628. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17629. If not, return to the original position and throw an error."
  17630. (interactive)
  17631. (let ((pos (point)))
  17632. (call-interactively cmd)
  17633. (unless (and (bolp) (org-at-heading-p))
  17634. (goto-char pos)
  17635. (error "Boundary reached while executing %s" cmd))))
  17636. (defvar org-self-insert-command-undo-counter 0)
  17637. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17638. (defvar org-speed-command nil)
  17639. (define-obsolete-function-alias
  17640. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17641. (defun org-speed-command-activate (keys)
  17642. "Hook for activating single-letter speed commands.
  17643. `org-speed-commands-default' specifies a minimal command set.
  17644. Use `org-speed-commands-user' for further customization."
  17645. (when (or (and (bolp) (looking-at org-outline-regexp))
  17646. (and (functionp org-use-speed-commands)
  17647. (funcall org-use-speed-commands)))
  17648. (cdr (assoc keys (append org-speed-commands-user
  17649. org-speed-commands-default)))))
  17650. (define-obsolete-function-alias
  17651. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17652. (defun org-babel-speed-command-activate (keys)
  17653. "Hook for activating single-letter code block commands."
  17654. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17655. (cdr (assoc keys org-babel-key-bindings))))
  17656. (defcustom org-speed-command-hook
  17657. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17658. "Hook for activating speed commands at strategic locations.
  17659. Hook functions are called in sequence until a valid handler is
  17660. found.
  17661. Each hook takes a single argument, a user-pressed command key
  17662. which is also a `self-insert-command' from the global map.
  17663. Within the hook, examine the cursor position and the command key
  17664. and return nil or a valid handler as appropriate. Handler could
  17665. be one of an interactive command, a function, or a form.
  17666. Set `org-use-speed-commands' to non-nil value to enable this
  17667. hook. The default setting is `org-speed-command-activate'."
  17668. :group 'org-structure
  17669. :version "24.1"
  17670. :type 'hook)
  17671. (defun org-self-insert-command (N)
  17672. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17673. If the cursor is in a table looking at whitespace, the whitespace is
  17674. overwritten, and the table is not marked as requiring realignment."
  17675. (interactive "p")
  17676. (org-check-before-invisible-edit 'insert)
  17677. (cond
  17678. ((and org-use-speed-commands
  17679. (setq org-speed-command
  17680. (run-hook-with-args-until-success
  17681. 'org-speed-command-hook (this-command-keys))))
  17682. (cond
  17683. ((commandp org-speed-command)
  17684. (setq this-command org-speed-command)
  17685. (call-interactively org-speed-command))
  17686. ((functionp org-speed-command)
  17687. (funcall org-speed-command))
  17688. ((and org-speed-command (listp org-speed-command))
  17689. (eval org-speed-command))
  17690. (t (let (org-use-speed-commands)
  17691. (call-interactively 'org-self-insert-command)))))
  17692. ((and
  17693. (org-table-p)
  17694. (progn
  17695. ;; check if we blank the field, and if that triggers align
  17696. (and (featurep 'org-table) org-table-auto-blank-field
  17697. (member last-command
  17698. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17699. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17700. ;; got extra space, this field does not determine column width
  17701. (let (org-table-may-need-update) (org-table-blank-field))
  17702. ;; no extra space, this field may determine column width
  17703. (org-table-blank-field)))
  17704. t)
  17705. (eq N 1)
  17706. (looking-at "[^|\n]* |"))
  17707. (let (org-table-may-need-update)
  17708. (goto-char (1- (match-end 0)))
  17709. (backward-delete-char 1)
  17710. (goto-char (match-beginning 0))
  17711. (self-insert-command N)))
  17712. (t
  17713. (setq org-table-may-need-update t)
  17714. (self-insert-command N)
  17715. (org-fix-tags-on-the-fly)
  17716. (if org-self-insert-cluster-for-undo
  17717. (if (not (eq last-command 'org-self-insert-command))
  17718. (setq org-self-insert-command-undo-counter 1)
  17719. (if (>= org-self-insert-command-undo-counter 20)
  17720. (setq org-self-insert-command-undo-counter 1)
  17721. (and (> org-self-insert-command-undo-counter 0)
  17722. buffer-undo-list (listp buffer-undo-list)
  17723. (not (cadr buffer-undo-list)) ; remove nil entry
  17724. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17725. (setq org-self-insert-command-undo-counter
  17726. (1+ org-self-insert-command-undo-counter))))))))
  17727. (defun org-check-before-invisible-edit (kind)
  17728. "Check is editing if kind KIND would be dangerous with invisible text around.
  17729. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17730. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17731. (if (and org-catch-invisible-edits
  17732. (or (not (boundp 'visible-mode)) (not visible-mode))
  17733. (or (get-char-property (point) 'invisible)
  17734. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17735. ;; OK, we need to take a closer look
  17736. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17737. (invisible-before-point (if (bobp) nil (get-char-property
  17738. (1- (point)) 'invisible)))
  17739. (border-and-ok-direction
  17740. (or
  17741. ;; Check if we are acting predictably before invisible text
  17742. (and invisible-at-point (not invisible-before-point)
  17743. (memq kind '(insert delete-backward)))
  17744. ;; Check if we are acting predictably after invisible text
  17745. ;; This works not well, and I have turned it off. It seems
  17746. ;; better to always show and stop after invisible text.
  17747. ;; (and (not invisible-at-point) invisible-before-point
  17748. ;; (memq kind '(insert delete)))
  17749. )))
  17750. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17751. (memq invisible-before-point '(outline org-hide-block t)))
  17752. (if (eq org-catch-invisible-edits 'error)
  17753. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17754. (if (and org-custom-properties-overlays
  17755. (y-or-n-p "Display invisible properties in this buffer? "))
  17756. (org-toggle-custom-properties-visibility)
  17757. ;; Make the area visible
  17758. (save-excursion
  17759. (if invisible-before-point
  17760. (goto-char (previous-single-char-property-change
  17761. (point) 'invisible)))
  17762. (org-cycle))
  17763. (cond
  17764. ((eq org-catch-invisible-edits 'show)
  17765. ;; That's it, we do the edit after showing
  17766. (message
  17767. "Unfolding invisible region around point before editing")
  17768. (sit-for 1))
  17769. ((and (eq org-catch-invisible-edits 'smart)
  17770. border-and-ok-direction)
  17771. (message "Unfolding invisible region around point before editing"))
  17772. (t
  17773. ;; Don't do the edit, make the user repeat it in full visibility
  17774. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17775. (defun org-fix-tags-on-the-fly ()
  17776. (when (and (equal (char-after (point-at-bol)) ?*)
  17777. (org-at-heading-p))
  17778. (org-align-tags-here org-tags-column)))
  17779. (defun org-delete-backward-char (N)
  17780. "Like `delete-backward-char', insert whitespace at field end in tables.
  17781. When deleting backwards, in tables this function will insert whitespace in
  17782. front of the next \"|\" separator, to keep the table aligned. The table will
  17783. still be marked for re-alignment if the field did fill the entire column,
  17784. because, in this case the deletion might narrow the column."
  17785. (interactive "p")
  17786. (save-match-data
  17787. (org-check-before-invisible-edit 'delete-backward)
  17788. (if (and (org-table-p)
  17789. (eq N 1)
  17790. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17791. (looking-at ".*?|"))
  17792. (let ((pos (point))
  17793. (noalign (looking-at "[^|\n\r]* |"))
  17794. (c org-table-may-need-update))
  17795. (backward-delete-char N)
  17796. (if (not overwrite-mode)
  17797. (progn
  17798. (skip-chars-forward "^|")
  17799. (insert " ")
  17800. (goto-char (1- pos))))
  17801. ;; noalign: if there were two spaces at the end, this field
  17802. ;; does not determine the width of the column.
  17803. (if noalign (setq org-table-may-need-update c)))
  17804. (backward-delete-char N)
  17805. (org-fix-tags-on-the-fly))))
  17806. (defun org-delete-char (N)
  17807. "Like `delete-char', but insert whitespace at field end in tables.
  17808. When deleting characters, in tables this function will insert whitespace in
  17809. front of the next \"|\" separator, to keep the table aligned. The table will
  17810. still be marked for re-alignment if the field did fill the entire column,
  17811. because, in this case the deletion might narrow the column."
  17812. (interactive "p")
  17813. (save-match-data
  17814. (org-check-before-invisible-edit 'delete)
  17815. (if (and (org-table-p)
  17816. (not (bolp))
  17817. (not (= (char-after) ?|))
  17818. (eq N 1))
  17819. (if (looking-at ".*?|")
  17820. (let ((pos (point))
  17821. (noalign (looking-at "[^|\n\r]* |"))
  17822. (c org-table-may-need-update))
  17823. (replace-match
  17824. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17825. (goto-char pos)
  17826. ;; noalign: if there were two spaces at the end, this field
  17827. ;; does not determine the width of the column.
  17828. (if noalign (setq org-table-may-need-update c)))
  17829. (delete-char N))
  17830. (delete-char N)
  17831. (org-fix-tags-on-the-fly))))
  17832. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17833. (put 'org-self-insert-command 'delete-selection t)
  17834. (put 'orgtbl-self-insert-command 'delete-selection t)
  17835. (put 'org-delete-char 'delete-selection 'supersede)
  17836. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17837. (put 'org-yank 'delete-selection 'yank)
  17838. ;; Make `flyspell-mode' delay after some commands
  17839. (put 'org-self-insert-command 'flyspell-delayed t)
  17840. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17841. (put 'org-delete-char 'flyspell-delayed t)
  17842. (put 'org-delete-backward-char 'flyspell-delayed t)
  17843. ;; Make pabbrev-mode expand after org-mode commands
  17844. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17845. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17846. ;; How to do this: Measure non-white length of current string
  17847. ;; If equal to column width, we should realign.
  17848. (defun org-remap (map &rest commands)
  17849. "In MAP, remap the functions given in COMMANDS.
  17850. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17851. (let (new old)
  17852. (while commands
  17853. (setq old (pop commands) new (pop commands))
  17854. (if (fboundp 'command-remapping)
  17855. (org-defkey map (vector 'remap old) new)
  17856. (substitute-key-definition old new map global-map)))))
  17857. (defun org-transpose-words ()
  17858. "Transpose words for Org.
  17859. This uses the `org-mode-transpose-word-syntax-table' syntax
  17860. table, which interprets characters in `org-emphasis-alist' as
  17861. word constituants."
  17862. (interactive)
  17863. (with-syntax-table org-mode-transpose-word-syntax-table
  17864. (call-interactively 'transpose-words)))
  17865. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17866. (when (eq org-enable-table-editor 'optimized)
  17867. ;; If the user wants maximum table support, we need to hijack
  17868. ;; some standard editing functions
  17869. (org-remap org-mode-map
  17870. 'self-insert-command 'org-self-insert-command
  17871. 'delete-char 'org-delete-char
  17872. 'delete-backward-char 'org-delete-backward-char)
  17873. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17874. (defvar org-ctrl-c-ctrl-c-hook nil
  17875. "Hook for functions attaching themselves to `C-c C-c'.
  17876. This can be used to add additional functionality to the C-c C-c
  17877. key which executes context-dependent commands. This hook is run
  17878. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17879. run after the last test.
  17880. Each function will be called with no arguments. The function
  17881. must check if the context is appropriate for it to act. If yes,
  17882. it should do its thing and then return a non-nil value. If the
  17883. context is wrong, just do nothing and return nil.")
  17884. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17885. "Hook for functions attaching themselves to `C-c C-c'.
  17886. This can be used to add additional functionality to the C-c C-c
  17887. key which executes context-dependent commands. This hook is run
  17888. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17889. before the first test.
  17890. Each function will be called with no arguments. The function
  17891. must check if the context is appropriate for it to act. If yes,
  17892. it should do its thing and then return a non-nil value. If the
  17893. context is wrong, just do nothing and return nil.")
  17894. (defvar org-tab-first-hook nil
  17895. "Hook for functions to attach themselves to TAB.
  17896. See `org-ctrl-c-ctrl-c-hook' for more information.
  17897. This hook runs as the first action when TAB is pressed, even before
  17898. `org-cycle' messes around with the `outline-regexp' to cater for
  17899. inline tasks and plain list item folding.
  17900. If any function in this hook returns t, any other actions that
  17901. would have been caused by TAB (such as table field motion or visibility
  17902. cycling) will not occur.")
  17903. (defvar org-tab-after-check-for-table-hook nil
  17904. "Hook for functions to attach themselves to TAB.
  17905. See `org-ctrl-c-ctrl-c-hook' for more information.
  17906. This hook runs after it has been established that the cursor is not in a
  17907. table, but before checking if the cursor is in a headline or if global cycling
  17908. should be done.
  17909. If any function in this hook returns t, not other actions like visibility
  17910. cycling will be done.")
  17911. (defvar org-tab-after-check-for-cycling-hook nil
  17912. "Hook for functions to attach themselves to TAB.
  17913. See `org-ctrl-c-ctrl-c-hook' for more information.
  17914. This hook runs after it has been established that not table field motion and
  17915. not visibility should be done because of current context. This is probably
  17916. the place where a package like yasnippets can hook in.")
  17917. (defvar org-tab-before-tab-emulation-hook nil
  17918. "Hook for functions to attach themselves to TAB.
  17919. See `org-ctrl-c-ctrl-c-hook' for more information.
  17920. This hook runs after every other options for TAB have been exhausted, but
  17921. before indentation and \t insertion takes place.")
  17922. (defvar org-metaleft-hook nil
  17923. "Hook for functions attaching themselves to `M-left'.
  17924. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17925. (defvar org-metaright-hook nil
  17926. "Hook for functions attaching themselves to `M-right'.
  17927. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17928. (defvar org-metaup-hook nil
  17929. "Hook for functions attaching themselves to `M-up'.
  17930. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17931. (defvar org-metadown-hook nil
  17932. "Hook for functions attaching themselves to `M-down'.
  17933. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17934. (defvar org-shiftmetaleft-hook nil
  17935. "Hook for functions attaching themselves to `M-S-left'.
  17936. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17937. (defvar org-shiftmetaright-hook nil
  17938. "Hook for functions attaching themselves to `M-S-right'.
  17939. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17940. (defvar org-shiftmetaup-hook nil
  17941. "Hook for functions attaching themselves to `M-S-up'.
  17942. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17943. (defvar org-shiftmetadown-hook nil
  17944. "Hook for functions attaching themselves to `M-S-down'.
  17945. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17946. (defvar org-metareturn-hook nil
  17947. "Hook for functions attaching themselves to `M-RET'.
  17948. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17949. (defvar org-shiftup-hook nil
  17950. "Hook for functions attaching themselves to `S-up'.
  17951. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17952. (defvar org-shiftup-final-hook nil
  17953. "Hook for functions attaching themselves to `S-up'.
  17954. This one runs after all other options except shift-select have been excluded.
  17955. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17956. (defvar org-shiftdown-hook nil
  17957. "Hook for functions attaching themselves to `S-down'.
  17958. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17959. (defvar org-shiftdown-final-hook nil
  17960. "Hook for functions attaching themselves to `S-down'.
  17961. This one runs after all other options except shift-select have been excluded.
  17962. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17963. (defvar org-shiftleft-hook nil
  17964. "Hook for functions attaching themselves to `S-left'.
  17965. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17966. (defvar org-shiftleft-final-hook nil
  17967. "Hook for functions attaching themselves to `S-left'.
  17968. This one runs after all other options except shift-select have been excluded.
  17969. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17970. (defvar org-shiftright-hook nil
  17971. "Hook for functions attaching themselves to `S-right'.
  17972. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17973. (defvar org-shiftright-final-hook nil
  17974. "Hook for functions attaching themselves to `S-right'.
  17975. This one runs after all other options except shift-select have been excluded.
  17976. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17977. (defun org-modifier-cursor-error ()
  17978. "Throw an error, a modified cursor command was applied in wrong context."
  17979. (user-error "This command is active in special context like tables, headlines or items"))
  17980. (defun org-shiftselect-error ()
  17981. "Throw an error because Shift-Cursor command was applied in wrong context."
  17982. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17983. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17984. (user-error "This command works only in special context like headlines or timestamps")))
  17985. (defun org-call-for-shift-select (cmd)
  17986. (let ((this-command-keys-shift-translated t))
  17987. (call-interactively cmd)))
  17988. (defun org-shifttab (&optional arg)
  17989. "Global visibility cycling or move to previous table field.
  17990. Call `org-table-previous-field' within a table.
  17991. When ARG is nil, cycle globally through visibility states.
  17992. When ARG is a numeric prefix, show contents of this level."
  17993. (interactive "P")
  17994. (cond
  17995. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17996. ((integerp arg)
  17997. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17998. (message "Content view to level: %d" arg)
  17999. (org-content (prefix-numeric-value arg2))
  18000. (org-cycle-show-empty-lines t)
  18001. (setq org-cycle-global-status 'overview)))
  18002. (t (call-interactively 'org-global-cycle))))
  18003. (defun org-shiftmetaleft ()
  18004. "Promote subtree or delete table column.
  18005. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18006. `org-table-delete-column', depending on context. See the
  18007. individual commands for more information."
  18008. (interactive)
  18009. (cond
  18010. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18011. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18012. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18013. ((if (not (org-region-active-p)) (org-at-item-p)
  18014. (save-excursion (goto-char (region-beginning))
  18015. (org-at-item-p)))
  18016. (call-interactively 'org-outdent-item-tree))
  18017. (t (org-modifier-cursor-error))))
  18018. (defun org-shiftmetaright ()
  18019. "Demote subtree or insert table column.
  18020. Calls `org-demote-subtree', `org-indent-item-tree', or
  18021. `org-table-insert-column', depending on context. See the
  18022. individual commands for more information."
  18023. (interactive)
  18024. (cond
  18025. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18026. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18027. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18028. ((if (not (org-region-active-p)) (org-at-item-p)
  18029. (save-excursion (goto-char (region-beginning))
  18030. (org-at-item-p)))
  18031. (call-interactively 'org-indent-item-tree))
  18032. (t (org-modifier-cursor-error))))
  18033. (defun org-shiftmetaup (&optional arg)
  18034. "Move subtree up or kill table row.
  18035. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18036. `org-move-item-up' or `org-timestamp-up', depending on context.
  18037. See the individual commands for more information."
  18038. (interactive "P")
  18039. (cond
  18040. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18041. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18042. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18043. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18044. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18045. (call-interactively 'org-timestamp-up)))
  18046. (t (call-interactively 'org-drag-line-backward))))
  18047. (defun org-shiftmetadown (&optional arg)
  18048. "Move subtree down or insert table row.
  18049. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18050. `org-move-item-down' or `org-timestamp-up', depending on context.
  18051. See the individual commands for more information."
  18052. (interactive "P")
  18053. (cond
  18054. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18055. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18056. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18057. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18058. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18059. (call-interactively 'org-timestamp-down)))
  18060. (t (call-interactively 'org-drag-line-forward))))
  18061. (defsubst org-hidden-tree-error ()
  18062. (user-error
  18063. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18064. (defun org-metaleft (&optional arg)
  18065. "Promote heading or move table column to left.
  18066. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18067. With no specific context, calls the Emacs default `backward-word'.
  18068. See the individual commands for more information."
  18069. (interactive "P")
  18070. (cond
  18071. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18072. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18073. ((org-with-limited-levels
  18074. (or (org-at-heading-p)
  18075. (and (org-region-active-p)
  18076. (save-excursion
  18077. (goto-char (region-beginning))
  18078. (org-at-heading-p)))))
  18079. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18080. (call-interactively 'org-do-promote))
  18081. ;; At an inline task.
  18082. ((org-at-heading-p)
  18083. (call-interactively 'org-inlinetask-promote))
  18084. ((or (org-at-item-p)
  18085. (and (org-region-active-p)
  18086. (save-excursion
  18087. (goto-char (region-beginning))
  18088. (org-at-item-p))))
  18089. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18090. (call-interactively 'org-outdent-item))
  18091. (t (call-interactively 'backward-word))))
  18092. (defun org-metaright (&optional arg)
  18093. "Demote a subtree, a list item or move table column to right.
  18094. In front of a drawer or a block keyword, indent it correctly.
  18095. With no specific context, calls the Emacs default `forward-word'.
  18096. See the individual commands for more information."
  18097. (interactive "P")
  18098. (cond
  18099. ((run-hook-with-args-until-success 'org-metaright-hook))
  18100. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18101. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18102. ((org-at-block-p) (call-interactively 'org-indent-block))
  18103. ((org-with-limited-levels
  18104. (or (org-at-heading-p)
  18105. (and (org-region-active-p)
  18106. (save-excursion
  18107. (goto-char (region-beginning))
  18108. (org-at-heading-p)))))
  18109. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18110. (call-interactively 'org-do-demote))
  18111. ;; At an inline task.
  18112. ((org-at-heading-p)
  18113. (call-interactively 'org-inlinetask-demote))
  18114. ((or (org-at-item-p)
  18115. (and (org-region-active-p)
  18116. (save-excursion
  18117. (goto-char (region-beginning))
  18118. (org-at-item-p))))
  18119. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18120. (call-interactively 'org-indent-item))
  18121. (t (call-interactively 'forward-word))))
  18122. (defun org-check-for-hidden (what)
  18123. "Check if there are hidden headlines/items in the current visual line.
  18124. WHAT can be either `headlines' or `items'. If the current line is
  18125. an outline or item heading and it has a folded subtree below it,
  18126. this function returns t, nil otherwise."
  18127. (let ((re (cond
  18128. ((eq what 'headlines) org-outline-regexp-bol)
  18129. ((eq what 'items) (org-item-beginning-re))
  18130. (t (error "This should not happen"))))
  18131. beg end)
  18132. (save-excursion
  18133. (catch 'exit
  18134. (unless (org-region-active-p)
  18135. (setq beg (point-at-bol))
  18136. (beginning-of-line 2)
  18137. (while (and (not (eobp)) ;; this is like `next-line'
  18138. (get-char-property (1- (point)) 'invisible))
  18139. (beginning-of-line 2))
  18140. (setq end (point))
  18141. (goto-char beg)
  18142. (goto-char (point-at-eol))
  18143. (setq end (max end (point)))
  18144. (while (re-search-forward re end t)
  18145. (if (get-char-property (match-beginning 0) 'invisible)
  18146. (throw 'exit t))))
  18147. nil))))
  18148. (defun org-metaup (&optional arg)
  18149. "Move subtree up or move table row up.
  18150. Calls `org-move-subtree-up' or `org-table-move-row' or
  18151. `org-move-item-up', depending on context. See the individual commands
  18152. for more information."
  18153. (interactive "P")
  18154. (cond
  18155. ((run-hook-with-args-until-success 'org-metaup-hook))
  18156. ((org-region-active-p)
  18157. (let* ((a (min (region-beginning) (region-end)))
  18158. (b (1- (max (region-beginning) (region-end))))
  18159. (c (save-excursion (goto-char a)
  18160. (move-beginning-of-line 0)))
  18161. (d (save-excursion (goto-char a)
  18162. (move-end-of-line 0) (point))))
  18163. (transpose-regions a b c d)
  18164. (goto-char c)))
  18165. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18166. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18167. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18168. (t (org-drag-element-backward))))
  18169. (defun org-metadown (&optional arg)
  18170. "Move subtree down or move table row down.
  18171. Calls `org-move-subtree-down' or `org-table-move-row' or
  18172. `org-move-item-down', depending on context. See the individual
  18173. commands for more information."
  18174. (interactive "P")
  18175. (cond
  18176. ((run-hook-with-args-until-success 'org-metadown-hook))
  18177. ((org-region-active-p)
  18178. (let* ((a (min (region-beginning) (region-end)))
  18179. (b (max (region-beginning) (region-end)))
  18180. (c (save-excursion (goto-char b)
  18181. (move-beginning-of-line 1)))
  18182. (d (save-excursion (goto-char b)
  18183. (move-end-of-line 1) (1+ (point)))))
  18184. (transpose-regions a b c d)
  18185. (goto-char d)))
  18186. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18187. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18188. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18189. (t (org-drag-element-forward))))
  18190. (defun org-shiftup (&optional arg)
  18191. "Increase item in timestamp or increase priority of current headline.
  18192. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18193. depending on context. See the individual commands for more information."
  18194. (interactive "P")
  18195. (cond
  18196. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18197. ((and org-support-shift-select (org-region-active-p))
  18198. (org-call-for-shift-select 'previous-line))
  18199. ((org-at-timestamp-p t)
  18200. (call-interactively (if org-edit-timestamp-down-means-later
  18201. 'org-timestamp-down 'org-timestamp-up)))
  18202. ((and (not (eq org-support-shift-select 'always))
  18203. org-enable-priority-commands
  18204. (org-at-heading-p))
  18205. (call-interactively 'org-priority-up))
  18206. ((and (not org-support-shift-select) (org-at-item-p))
  18207. (call-interactively 'org-previous-item))
  18208. ((org-clocktable-try-shift 'up arg))
  18209. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18210. (org-support-shift-select
  18211. (org-call-for-shift-select 'previous-line))
  18212. (t (org-shiftselect-error))))
  18213. (defun org-shiftdown (&optional arg)
  18214. "Decrease item in timestamp or decrease priority of current headline.
  18215. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18216. depending on context. See the individual commands for more information."
  18217. (interactive "P")
  18218. (cond
  18219. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18220. ((and org-support-shift-select (org-region-active-p))
  18221. (org-call-for-shift-select 'next-line))
  18222. ((org-at-timestamp-p t)
  18223. (call-interactively (if org-edit-timestamp-down-means-later
  18224. 'org-timestamp-up 'org-timestamp-down)))
  18225. ((and (not (eq org-support-shift-select 'always))
  18226. org-enable-priority-commands
  18227. (org-at-heading-p))
  18228. (call-interactively 'org-priority-down))
  18229. ((and (not org-support-shift-select) (org-at-item-p))
  18230. (call-interactively 'org-next-item))
  18231. ((org-clocktable-try-shift 'down arg))
  18232. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18233. (org-support-shift-select
  18234. (org-call-for-shift-select 'next-line))
  18235. (t (org-shiftselect-error))))
  18236. (defun org-shiftright (&optional arg)
  18237. "Cycle the thing at point or in the current line, depending on context.
  18238. Depending on context, this does one of the following:
  18239. - switch a timestamp at point one day into the future
  18240. - on a headline, switch to the next TODO keyword.
  18241. - on an item, switch entire list to the next bullet type
  18242. - on a property line, switch to the next allowed value
  18243. - on a clocktable definition line, move time block into the future"
  18244. (interactive "P")
  18245. (cond
  18246. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18247. ((and org-support-shift-select (org-region-active-p))
  18248. (org-call-for-shift-select 'forward-char))
  18249. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18250. ((and (not (eq org-support-shift-select 'always))
  18251. (org-at-heading-p))
  18252. (let ((org-inhibit-logging
  18253. (not org-treat-S-cursor-todo-selection-as-state-change))
  18254. (org-inhibit-blocking
  18255. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18256. (org-call-with-arg 'org-todo 'right)))
  18257. ((or (and org-support-shift-select
  18258. (not (eq org-support-shift-select 'always))
  18259. (org-at-item-bullet-p))
  18260. (and (not org-support-shift-select) (org-at-item-p)))
  18261. (org-call-with-arg 'org-cycle-list-bullet nil))
  18262. ((and (not (eq org-support-shift-select 'always))
  18263. (org-at-property-p))
  18264. (call-interactively 'org-property-next-allowed-value))
  18265. ((org-clocktable-try-shift 'right arg))
  18266. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18267. (org-support-shift-select
  18268. (org-call-for-shift-select 'forward-char))
  18269. (t (org-shiftselect-error))))
  18270. (defun org-shiftleft (&optional arg)
  18271. "Cycle the thing at point or in the current line, depending on context.
  18272. Depending on context, this does one of the following:
  18273. - switch a timestamp at point one day into the past
  18274. - on a headline, switch to the previous TODO keyword.
  18275. - on an item, switch entire list to the previous bullet type
  18276. - on a property line, switch to the previous allowed value
  18277. - on a clocktable definition line, move time block into the past"
  18278. (interactive "P")
  18279. (cond
  18280. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18281. ((and org-support-shift-select (org-region-active-p))
  18282. (org-call-for-shift-select 'backward-char))
  18283. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18284. ((and (not (eq org-support-shift-select 'always))
  18285. (org-at-heading-p))
  18286. (let ((org-inhibit-logging
  18287. (not org-treat-S-cursor-todo-selection-as-state-change))
  18288. (org-inhibit-blocking
  18289. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18290. (org-call-with-arg 'org-todo 'left)))
  18291. ((or (and org-support-shift-select
  18292. (not (eq org-support-shift-select 'always))
  18293. (org-at-item-bullet-p))
  18294. (and (not org-support-shift-select) (org-at-item-p)))
  18295. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18296. ((and (not (eq org-support-shift-select 'always))
  18297. (org-at-property-p))
  18298. (call-interactively 'org-property-previous-allowed-value))
  18299. ((org-clocktable-try-shift 'left arg))
  18300. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18301. (org-support-shift-select
  18302. (org-call-for-shift-select 'backward-char))
  18303. (t (org-shiftselect-error))))
  18304. (defun org-shiftcontrolright ()
  18305. "Switch to next TODO set."
  18306. (interactive)
  18307. (cond
  18308. ((and org-support-shift-select (org-region-active-p))
  18309. (org-call-for-shift-select 'forward-word))
  18310. ((and (not (eq org-support-shift-select 'always))
  18311. (org-at-heading-p))
  18312. (org-call-with-arg 'org-todo 'nextset))
  18313. (org-support-shift-select
  18314. (org-call-for-shift-select 'forward-word))
  18315. (t (org-shiftselect-error))))
  18316. (defun org-shiftcontrolleft ()
  18317. "Switch to previous TODO set."
  18318. (interactive)
  18319. (cond
  18320. ((and org-support-shift-select (org-region-active-p))
  18321. (org-call-for-shift-select 'backward-word))
  18322. ((and (not (eq org-support-shift-select 'always))
  18323. (org-at-heading-p))
  18324. (org-call-with-arg 'org-todo 'previousset))
  18325. (org-support-shift-select
  18326. (org-call-for-shift-select 'backward-word))
  18327. (t (org-shiftselect-error))))
  18328. (defun org-shiftcontrolup (&optional n)
  18329. "Change timestamps synchronously up in CLOCK log lines.
  18330. Optional argument N tells to change by that many units."
  18331. (interactive "P")
  18332. (cond ((and (not org-support-shift-select)
  18333. (org-at-clock-log-p)
  18334. (org-at-timestamp-p t))
  18335. (org-clock-timestamps-up n))
  18336. (t (org-shiftselect-error))))
  18337. (defun org-shiftcontroldown (&optional n)
  18338. "Change timestamps synchronously down in CLOCK log lines.
  18339. Optional argument N tells to change by that many units."
  18340. (interactive "P")
  18341. (cond ((and (not org-support-shift-select)
  18342. (org-at-clock-log-p)
  18343. (org-at-timestamp-p t))
  18344. (org-clock-timestamps-down n))
  18345. (t (org-shiftselect-error))))
  18346. (defun org-ctrl-c-ret ()
  18347. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18348. (interactive)
  18349. (cond
  18350. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18351. (t (call-interactively 'org-insert-heading))))
  18352. (defun org-find-visible ()
  18353. (let ((s (point)))
  18354. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18355. (get-char-property s 'invisible)))
  18356. s))
  18357. (defun org-find-invisible ()
  18358. (let ((s (point)))
  18359. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18360. (not (get-char-property s 'invisible))))
  18361. s))
  18362. (defun org-copy-visible (beg end)
  18363. "Copy the visible parts of the region."
  18364. (interactive "r")
  18365. (let (snippets s)
  18366. (save-excursion
  18367. (save-restriction
  18368. (narrow-to-region beg end)
  18369. (setq s (goto-char (point-min)))
  18370. (while (not (= (point) (point-max)))
  18371. (goto-char (org-find-invisible))
  18372. (push (buffer-substring s (point)) snippets)
  18373. (setq s (goto-char (org-find-visible))))))
  18374. (kill-new (apply 'concat (nreverse snippets)))))
  18375. (defun org-copy-special ()
  18376. "Copy region in table or copy current subtree.
  18377. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18378. See the individual commands for more information."
  18379. (interactive)
  18380. (call-interactively
  18381. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18382. (defun org-cut-special ()
  18383. "Cut region in table or cut current subtree.
  18384. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18385. See the individual commands for more information."
  18386. (interactive)
  18387. (call-interactively
  18388. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18389. (defun org-paste-special (arg)
  18390. "Paste rectangular region into table, or past subtree relative to level.
  18391. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18392. See the individual commands for more information."
  18393. (interactive "P")
  18394. (if (org-at-table-p)
  18395. (org-table-paste-rectangle)
  18396. (org-paste-subtree arg)))
  18397. (defsubst org-in-fixed-width-region-p ()
  18398. "Is point in a fixed-width region?"
  18399. (save-match-data
  18400. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18401. (defun org-edit-special (&optional arg)
  18402. "Call a special editor for the element at point.
  18403. When at a table, call the formula editor with `org-table-edit-formulas'.
  18404. When in a source code block, call `org-edit-src-code'.
  18405. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18406. When at an #+INCLUDE keyword, visit the included file.
  18407. On a link, call `ffap' to visit the link at point.
  18408. Otherwise, return a user error."
  18409. (interactive "P")
  18410. (let ((element (org-element-at-point)))
  18411. (assert (not buffer-read-only) nil
  18412. "Buffer is read-only: %s" (buffer-name))
  18413. (case (org-element-type element)
  18414. (src-block
  18415. (if (not arg) (org-edit-src-code)
  18416. (let* ((info (org-babel-get-src-block-info))
  18417. (lang (nth 0 info))
  18418. (params (nth 2 info))
  18419. (session (cdr (assq :session params))))
  18420. (if (not session) (org-edit-src-code)
  18421. ;; At a src-block with a session and function called with
  18422. ;; an ARG: switch to the buffer related to the inferior
  18423. ;; process.
  18424. (switch-to-buffer
  18425. (funcall (intern (concat "org-babel-prep-session:" lang))
  18426. session params))))))
  18427. (keyword
  18428. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18429. (find-file
  18430. (org-remove-double-quotes
  18431. (car (org-split-string (org-element-property :value element)))))
  18432. (user-error "No special environment to edit here")))
  18433. (table
  18434. (if (eq (org-element-property :type element) 'table.el)
  18435. (org-edit-src-code)
  18436. (call-interactively 'org-table-edit-formulas)))
  18437. ;; Only Org tables contain `table-row' type elements.
  18438. (table-row (call-interactively 'org-table-edit-formulas))
  18439. ((example-block export-block) (org-edit-src-code))
  18440. (fixed-width (org-edit-fixed-width-region))
  18441. (otherwise
  18442. ;; No notable element at point. Though, we may be at a link,
  18443. ;; which is an object. Thus, scan deeper.
  18444. (if (eq (org-element-type (org-element-context element)) 'link)
  18445. (call-interactively 'ffap)
  18446. (user-error "No special environment to edit here"))))))
  18447. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18448. (defun org-ctrl-c-ctrl-c (&optional arg)
  18449. "Set tags in headline, or update according to changed information at point.
  18450. This command does many different things, depending on context:
  18451. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18452. this is what we do.
  18453. - If the cursor is on a statistics cookie, update it.
  18454. - If the cursor is in a headline, prompt for tags and insert them
  18455. into the current line, aligned to `org-tags-column'. When called
  18456. with prefix arg, realign all tags in the current buffer.
  18457. - If the cursor is in one of the special #+KEYWORD lines, this
  18458. triggers scanning the buffer for these lines and updating the
  18459. information.
  18460. - If the cursor is inside a table, realign the table. This command
  18461. works even if the automatic table editor has been turned off.
  18462. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18463. the entire table.
  18464. - If the cursor is at a footnote reference or definition, jump to
  18465. the corresponding definition or references, respectively.
  18466. - If the cursor is a the beginning of a dynamic block, update it.
  18467. - If the current buffer is a capture buffer, close note and file it.
  18468. - If the cursor is on a <<<target>>>, update radio targets and
  18469. corresponding links in this buffer.
  18470. - If the cursor is on a numbered item in a plain list, renumber the
  18471. ordered list.
  18472. - If the cursor is on a checkbox, toggle it.
  18473. - If the cursor is on a code block, evaluate it. The variable
  18474. `org-confirm-babel-evaluate' can be used to control prompting
  18475. before code block evaluation, by default every code block
  18476. evaluation requires confirmation. Code block evaluation can be
  18477. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18478. (interactive "P")
  18479. (cond
  18480. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18481. org-occur-highlights
  18482. org-latex-fragment-image-overlays)
  18483. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18484. (org-remove-occur-highlights)
  18485. (org-remove-latex-fragment-image-overlays)
  18486. (message "Temporary highlights/overlays removed from current buffer"))
  18487. ((and (local-variable-p 'org-finish-function (current-buffer))
  18488. (fboundp org-finish-function))
  18489. (funcall org-finish-function))
  18490. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18491. (t
  18492. (let* ((context (org-element-context)) (type (org-element-type context)))
  18493. ;; Test if point is within a blank line.
  18494. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18495. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18496. (user-error "C-c C-c can do nothing useful at this location"))
  18497. ;; For convenience: at the first line of a paragraph on the
  18498. ;; same line as an item, apply function on that item instead.
  18499. (when (eq type 'paragraph)
  18500. (let ((parent (org-element-property :parent context)))
  18501. (when (and (eq (org-element-type parent) 'item)
  18502. (= (point-at-bol) (org-element-property :begin parent)))
  18503. (setq context parent type 'item))))
  18504. ;; Act according to type of element or object at point.
  18505. (case type
  18506. (clock (org-clock-update-time-maybe))
  18507. (dynamic-block
  18508. (save-excursion
  18509. (goto-char (org-element-property :post-affiliated context))
  18510. (org-update-dblock)))
  18511. (footnote-definition
  18512. (goto-char (org-element-property :post-affiliated context))
  18513. (call-interactively 'org-footnote-action))
  18514. (footnote-reference (call-interactively 'org-footnote-action))
  18515. ((headline inlinetask)
  18516. (save-excursion (goto-char (org-element-property :begin context))
  18517. (call-interactively 'org-set-tags)))
  18518. (item
  18519. ;; At an item: a double C-u set checkbox to "[-]"
  18520. ;; unconditionally, whereas a single one will toggle its
  18521. ;; presence. Without an universal argument, if the item
  18522. ;; has a checkbox, toggle it. Otherwise repair the list.
  18523. (let* ((box (org-element-property :checkbox context))
  18524. (struct (org-element-property :structure context))
  18525. (old-struct (copy-tree struct))
  18526. (parents (org-list-parents-alist struct))
  18527. (prevs (org-list-prevs-alist struct))
  18528. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18529. (org-list-set-checkbox
  18530. (org-element-property :begin context) struct
  18531. (cond ((equal arg '(16)) "[-]")
  18532. ((and (not box) (equal arg '(4))) "[ ]")
  18533. ((or (not box) (equal arg '(4))) nil)
  18534. ((eq box 'on) "[ ]")
  18535. (t "[X]")))
  18536. ;; Mimic `org-list-write-struct' but with grabbing
  18537. ;; a return value from `org-list-struct-fix-box'.
  18538. (org-list-struct-fix-ind struct parents 2)
  18539. (org-list-struct-fix-item-end struct)
  18540. (org-list-struct-fix-bul struct prevs)
  18541. (org-list-struct-fix-ind struct parents)
  18542. (let ((block-item
  18543. (org-list-struct-fix-box struct parents prevs orderedp)))
  18544. (if (and box (equal struct old-struct))
  18545. (if (equal arg '(16))
  18546. (message "Checkboxes already reset")
  18547. (user-error "Cannot toggle this checkbox: %s"
  18548. (if (eq box 'on)
  18549. "all subitems checked"
  18550. "unchecked subitems")))
  18551. (org-list-struct-apply-struct struct old-struct)
  18552. (org-update-checkbox-count-maybe))
  18553. (when block-item
  18554. (message "Checkboxes were removed due to empty box at line %d"
  18555. (org-current-line block-item))))))
  18556. (keyword
  18557. (let ((org-inhibit-startup-visibility-stuff t)
  18558. (org-startup-align-all-tables nil))
  18559. (when (boundp 'org-table-coordinate-overlays)
  18560. (mapc 'delete-overlay org-table-coordinate-overlays)
  18561. (setq org-table-coordinate-overlays nil))
  18562. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18563. (message "Local setup has been refreshed"))
  18564. (plain-list
  18565. ;; At a plain list, with a double C-u argument, set
  18566. ;; checkboxes of each item to "[-]", whereas a single one
  18567. ;; will toggle their presence according to the state of the
  18568. ;; first item in the list. Without an argument, repair the
  18569. ;; list.
  18570. (let* ((begin (org-element-property :contents-begin context))
  18571. (beginm (move-marker (make-marker) begin))
  18572. (struct (org-element-property :structure context))
  18573. (old-struct (copy-tree struct))
  18574. (first-box (save-excursion
  18575. (goto-char begin)
  18576. (looking-at org-list-full-item-re)
  18577. (match-string-no-properties 3)))
  18578. (new-box (cond ((equal arg '(16)) "[-]")
  18579. ((equal arg '(4)) (unless first-box "[ ]"))
  18580. ((equal first-box "[X]") "[ ]")
  18581. (t "[X]"))))
  18582. (cond
  18583. (arg
  18584. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18585. (org-list-get-all-items
  18586. begin struct (org-list-prevs-alist struct))))
  18587. ((and first-box (eq (point) begin))
  18588. ;; For convenience, when point is at bol on the first
  18589. ;; item of the list and no argument is provided, simply
  18590. ;; toggle checkbox of that item, if any.
  18591. (org-list-set-checkbox begin struct new-box)))
  18592. (org-list-write-struct
  18593. struct (org-list-parents-alist struct) old-struct)
  18594. (org-update-checkbox-count-maybe)
  18595. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18596. ((property-drawer node-property)
  18597. (call-interactively 'org-property-action))
  18598. ((radio-target target)
  18599. (call-interactively 'org-update-radio-target-regexp))
  18600. (statistics-cookie
  18601. (call-interactively 'org-update-statistics-cookies))
  18602. ((table table-cell table-row)
  18603. ;; At a table, recalculate every field and align it. Also
  18604. ;; send the table if necessary. If the table has
  18605. ;; a `table.el' type, just give up. At a table row or
  18606. ;; cell, maybe recalculate line but always align table.
  18607. (if (eq (org-element-property :type context) 'table.el)
  18608. (message "Use C-c ' to edit table.el tables")
  18609. (let ((org-enable-table-editor t))
  18610. (if (or (eq type 'table)
  18611. ;; Check if point is at a TBLFM line.
  18612. (and (eq type 'table-row)
  18613. (= (point) (org-element-property :end context))))
  18614. (save-excursion
  18615. (if (org-at-TBLFM-p)
  18616. (progn (require 'org-table)
  18617. (org-table-calc-current-TBLFM))
  18618. (goto-char (org-element-property :contents-begin context))
  18619. (org-call-with-arg 'org-table-recalculate (or arg t))
  18620. (orgtbl-send-table 'maybe)))
  18621. (org-table-maybe-eval-formula)
  18622. (cond (arg (call-interactively 'org-table-recalculate))
  18623. ((org-table-maybe-recalculate-line))
  18624. (t (org-table-align)))))))
  18625. (timestamp (org-timestamp-change 0 'day))
  18626. (otherwise
  18627. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18628. (user-error
  18629. "C-c C-c can do nothing useful at this location")))))))))
  18630. (defun org-mode-restart ()
  18631. "Restart Org-mode, to scan again for special lines.
  18632. Also updates the keyword regular expressions."
  18633. (interactive)
  18634. (org-mode)
  18635. (message "Org-mode restarted"))
  18636. (defun org-kill-note-or-show-branches ()
  18637. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18638. (interactive)
  18639. (if (not org-finish-function)
  18640. (progn
  18641. (hide-subtree)
  18642. (call-interactively 'show-branches))
  18643. (let ((org-note-abort t))
  18644. (funcall org-finish-function))))
  18645. (defun org-open-line (n)
  18646. "Insert a new row in tables, call `open-line' elsewhere."
  18647. (interactive "*p")
  18648. (if (org-at-table-p)
  18649. (org-table-insert-row)
  18650. (open-line n)))
  18651. (defun org-return (&optional indent)
  18652. "Goto next table row or insert a newline.
  18653. Calls `org-table-next-row' or `newline', depending on context.
  18654. See the individual commands for more information."
  18655. (interactive)
  18656. (let (org-ts-what)
  18657. (cond
  18658. ((or (bobp) (org-in-src-block-p))
  18659. (if indent (newline-and-indent) (newline)))
  18660. ((org-at-table-p)
  18661. (org-table-justify-field-maybe)
  18662. (call-interactively 'org-table-next-row))
  18663. ;; when `newline-and-indent' is called within a list, make sure
  18664. ;; text moved stays inside the item.
  18665. ((and (org-in-item-p) indent)
  18666. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18667. (progn
  18668. (save-match-data (newline))
  18669. (org-indent-line-to (length (match-string 0))))
  18670. (let ((ind (org-get-indentation)))
  18671. (newline)
  18672. (if (org-looking-back org-list-end-re)
  18673. (org-indent-line)
  18674. (org-indent-line-to ind)))))
  18675. ((and org-return-follows-link
  18676. (org-at-timestamp-p t)
  18677. (not (eq org-ts-what 'after)))
  18678. (org-follow-timestamp-link))
  18679. ((and org-return-follows-link
  18680. (let ((tprop (get-text-property (point) 'face)))
  18681. (or (eq tprop 'org-link)
  18682. (and (listp tprop) (memq 'org-link tprop)))))
  18683. (call-interactively 'org-open-at-point))
  18684. ((and (org-at-heading-p)
  18685. (looking-at
  18686. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18687. (org-show-entry)
  18688. (end-of-line 1)
  18689. (newline))
  18690. (t (if indent (newline-and-indent) (newline))))))
  18691. (defun org-return-indent ()
  18692. "Goto next table row or insert a newline and indent.
  18693. Calls `org-table-next-row' or `newline-and-indent', depending on
  18694. context. See the individual commands for more information."
  18695. (interactive)
  18696. (org-return t))
  18697. (defun org-ctrl-c-star ()
  18698. "Compute table, or change heading status of lines.
  18699. Calls `org-table-recalculate' or `org-toggle-heading',
  18700. depending on context."
  18701. (interactive)
  18702. (cond
  18703. ((org-at-table-p)
  18704. (call-interactively 'org-table-recalculate))
  18705. (t
  18706. ;; Convert all lines in region to list items
  18707. (call-interactively 'org-toggle-heading))))
  18708. (defun org-ctrl-c-minus ()
  18709. "Insert separator line in table or modify bullet status of line.
  18710. Also turns a plain line or a region of lines into list items.
  18711. Calls `org-table-insert-hline', `org-toggle-item', or
  18712. `org-cycle-list-bullet', depending on context."
  18713. (interactive)
  18714. (cond
  18715. ((org-at-table-p)
  18716. (call-interactively 'org-table-insert-hline))
  18717. ((org-region-active-p)
  18718. (call-interactively 'org-toggle-item))
  18719. ((org-in-item-p)
  18720. (call-interactively 'org-cycle-list-bullet))
  18721. (t
  18722. (call-interactively 'org-toggle-item))))
  18723. (defun org-toggle-item (arg)
  18724. "Convert headings or normal lines to items, items to normal lines.
  18725. If there is no active region, only the current line is considered.
  18726. If the first non blank line in the region is a headline, convert
  18727. all headlines to items, shifting text accordingly.
  18728. If it is an item, convert all items to normal lines.
  18729. If it is normal text, change region into a list of items.
  18730. With a prefix argument ARG, change the region in a single item."
  18731. (interactive "P")
  18732. (let ((shift-text
  18733. (function
  18734. ;; Shift text in current section to IND, from point to END.
  18735. ;; The function leaves point to END line.
  18736. (lambda (ind end)
  18737. (let ((min-i 1000) (end (copy-marker end)))
  18738. ;; First determine the minimum indentation (MIN-I) of
  18739. ;; the text.
  18740. (save-excursion
  18741. (catch 'exit
  18742. (while (< (point) end)
  18743. (let ((i (org-get-indentation)))
  18744. (cond
  18745. ;; Skip blank lines and inline tasks.
  18746. ((looking-at "^[ \t]*$"))
  18747. ((looking-at org-outline-regexp-bol))
  18748. ;; We can't find less than 0 indentation.
  18749. ((zerop i) (throw 'exit (setq min-i 0)))
  18750. ((< i min-i) (setq min-i i))))
  18751. (forward-line))))
  18752. ;; Then indent each line so that a line indented to
  18753. ;; MIN-I becomes indented to IND. Ignore blank lines
  18754. ;; and inline tasks in the process.
  18755. (let ((delta (- ind min-i)))
  18756. (while (< (point) end)
  18757. (unless (or (looking-at "^[ \t]*$")
  18758. (looking-at org-outline-regexp-bol))
  18759. (org-indent-line-to (+ (org-get-indentation) delta)))
  18760. (forward-line)))))))
  18761. (skip-blanks
  18762. (function
  18763. ;; Return beginning of first non-blank line, starting from
  18764. ;; line at POS.
  18765. (lambda (pos)
  18766. (save-excursion
  18767. (goto-char pos)
  18768. (skip-chars-forward " \r\t\n")
  18769. (point-at-bol)))))
  18770. beg end)
  18771. ;; Determine boundaries of changes.
  18772. (if (org-region-active-p)
  18773. (setq beg (funcall skip-blanks (region-beginning))
  18774. end (copy-marker (region-end)))
  18775. (setq beg (funcall skip-blanks (point-at-bol))
  18776. end (copy-marker (point-at-eol))))
  18777. ;; Depending on the starting line, choose an action on the text
  18778. ;; between BEG and END.
  18779. (org-with-limited-levels
  18780. (save-excursion
  18781. (goto-char beg)
  18782. (cond
  18783. ;; Case 1. Start at an item: de-itemize. Note that it only
  18784. ;; happens when a region is active: `org-ctrl-c-minus'
  18785. ;; would call `org-cycle-list-bullet' otherwise.
  18786. ((org-at-item-p)
  18787. (while (< (point) end)
  18788. (when (org-at-item-p)
  18789. (skip-chars-forward " \t")
  18790. (delete-region (point) (match-end 0)))
  18791. (forward-line)))
  18792. ;; Case 2. Start at an heading: convert to items.
  18793. ((org-at-heading-p)
  18794. (let* ((bul (org-list-bullet-string "-"))
  18795. (bul-len (length bul))
  18796. ;; Indentation of the first heading. It should be
  18797. ;; relative to the indentation of its parent, if any.
  18798. (start-ind (save-excursion
  18799. (cond
  18800. ((not org-adapt-indentation) 0)
  18801. ((not (outline-previous-heading)) 0)
  18802. (t (length (match-string 0))))))
  18803. ;; Level of first heading. Further headings will be
  18804. ;; compared to it to determine hierarchy in the list.
  18805. (ref-level (org-reduced-level (org-outline-level))))
  18806. (while (< (point) end)
  18807. (let* ((level (org-reduced-level (org-outline-level)))
  18808. (delta (max 0 (- level ref-level))))
  18809. ;; If current headline is less indented than the first
  18810. ;; one, set it as reference, in order to preserve
  18811. ;; subtrees.
  18812. (when (< level ref-level) (setq ref-level level))
  18813. (replace-match bul t t)
  18814. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18815. ;; Ensure all text down to END (or SECTION-END) belongs
  18816. ;; to the newly created item.
  18817. (let ((section-end (save-excursion
  18818. (or (outline-next-heading) (point)))))
  18819. (forward-line)
  18820. (funcall shift-text
  18821. (+ start-ind (* (1+ delta) bul-len))
  18822. (min end section-end)))))))
  18823. ;; Case 3. Normal line with ARG: make the first line of region
  18824. ;; an item, and shift indentation of others lines to
  18825. ;; set them as item's body.
  18826. (arg (let* ((bul (org-list-bullet-string "-"))
  18827. (bul-len (length bul))
  18828. (ref-ind (org-get-indentation)))
  18829. (skip-chars-forward " \t")
  18830. (insert bul)
  18831. (forward-line)
  18832. (while (< (point) end)
  18833. ;; Ensure that lines less indented than first one
  18834. ;; still get included in item body.
  18835. (funcall shift-text
  18836. (+ ref-ind bul-len)
  18837. (min end (save-excursion (or (outline-next-heading)
  18838. (point)))))
  18839. (forward-line))))
  18840. ;; Case 4. Normal line without ARG: turn each non-item line
  18841. ;; into an item.
  18842. (t
  18843. (while (< (point) end)
  18844. (unless (or (org-at-heading-p) (org-at-item-p))
  18845. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18846. (replace-match
  18847. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18848. (forward-line))))))))
  18849. (defun org-toggle-heading (&optional nstars)
  18850. "Convert headings to normal text, or items or text to headings.
  18851. If there is no active region, only convert the current line.
  18852. With a \\[universal-argument] prefix, convert the whole list at
  18853. point into heading.
  18854. In a region:
  18855. - If the first non blank line is a headline, remove the stars
  18856. from all headlines in the region.
  18857. - If it is a normal line, turn each and every normal line (i.e.,
  18858. not an heading or an item) in the region into headings. If you
  18859. want to convert only the first line of this region, use one
  18860. universal prefix argument.
  18861. - If it is a plain list item, turn all plain list items into headings.
  18862. When converting a line into a heading, the number of stars is chosen
  18863. such that the lines become children of the current entry. However,
  18864. when a numeric prefix argument is given, its value determines the
  18865. number of stars to add."
  18866. (interactive "P")
  18867. (let ((skip-blanks
  18868. (function
  18869. ;; Return beginning of first non-blank line, starting from
  18870. ;; line at POS.
  18871. (lambda (pos)
  18872. (save-excursion
  18873. (goto-char pos)
  18874. (while (org-at-comment-p) (forward-line))
  18875. (skip-chars-forward " \r\t\n")
  18876. (point-at-bol)))))
  18877. beg end toggled)
  18878. ;; Determine boundaries of changes. If a universal prefix has
  18879. ;; been given, put the list in a region. If region ends at a bol,
  18880. ;; do not consider the last line to be in the region.
  18881. (when (and current-prefix-arg (org-at-item-p))
  18882. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18883. (org-mark-element))
  18884. (if (org-region-active-p)
  18885. (setq beg (funcall skip-blanks (region-beginning))
  18886. end (copy-marker (save-excursion
  18887. (goto-char (region-end))
  18888. (if (bolp) (point) (point-at-eol)))))
  18889. (setq beg (funcall skip-blanks (point-at-bol))
  18890. end (copy-marker (point-at-eol))))
  18891. ;; Ensure inline tasks don't count as headings.
  18892. (org-with-limited-levels
  18893. (save-excursion
  18894. (goto-char beg)
  18895. (cond
  18896. ;; Case 1. Started at an heading: de-star headings.
  18897. ((org-at-heading-p)
  18898. (while (< (point) end)
  18899. (when (org-at-heading-p t)
  18900. (looking-at org-outline-regexp) (replace-match "")
  18901. (setq toggled t))
  18902. (forward-line)))
  18903. ;; Case 2. Started at an item: change items into headlines.
  18904. ;; One star will be added by `org-list-to-subtree'.
  18905. ((org-at-item-p)
  18906. (let* ((stars (make-string
  18907. ;; subtract the star that will be added again by
  18908. ;; `org-list-to-subtree'
  18909. (if (numberp nstars) (1- nstars)
  18910. (or (org-current-level) 0))
  18911. ?*))
  18912. (add-stars
  18913. (cond (nstars "") ; stars from prefix only
  18914. ((equal stars "") "") ; before first heading
  18915. (org-odd-levels-only "*") ; inside heading, odd
  18916. (t "")))) ; inside heading, oddeven
  18917. (while (< (point) end)
  18918. (when (org-at-item-p)
  18919. ;; Pay attention to cases when region ends before list.
  18920. (let* ((struct (org-list-struct))
  18921. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18922. (save-restriction
  18923. (narrow-to-region (point) list-end)
  18924. (insert
  18925. (org-list-to-subtree
  18926. (org-list-parse-list t)
  18927. '(:istart (concat stars add-stars (funcall get-stars depth))
  18928. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18929. (setq toggled t))
  18930. (forward-line))))
  18931. ;; Case 3. Started at normal text: make every line an heading,
  18932. ;; skipping headlines and items.
  18933. (t (let* ((stars
  18934. (make-string
  18935. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18936. (add-stars
  18937. (cond (nstars "") ; stars from prefix only
  18938. ((equal stars "") "*") ; before first heading
  18939. (org-odd-levels-only "**") ; inside heading, odd
  18940. (t "*"))) ; inside heading, oddeven
  18941. (rpl (concat stars add-stars " "))
  18942. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18943. (while (< (point) (if (equal nstars '(4)) lend end))
  18944. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18945. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18946. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18947. (forward-line)))))))
  18948. (unless toggled (message "Cannot toggle heading from here"))))
  18949. (defun org-meta-return (&optional arg)
  18950. "Insert a new heading or wrap a region in a table.
  18951. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18952. See the individual commands for more information."
  18953. (interactive "P")
  18954. (org-check-before-invisible-edit 'insert)
  18955. (cond
  18956. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18957. ((or (org-at-drawer-p) (org-in-drawer-p) (org-at-property-p))
  18958. (newline-and-indent))
  18959. ((org-at-table-p)
  18960. (call-interactively 'org-table-wrap-region))
  18961. (t (call-interactively 'org-insert-heading))))
  18962. ;;; Menu entries
  18963. (defsubst org-in-subtree-not-table-p ()
  18964. "Are we in a subtree and not in a table?"
  18965. (and (not (org-before-first-heading-p))
  18966. (not (org-at-table-p))))
  18967. ;; Define the Org-mode menus
  18968. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18969. '("Tbl"
  18970. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18971. ["Next Field" org-cycle (org-at-table-p)]
  18972. ["Previous Field" org-shifttab (org-at-table-p)]
  18973. ["Next Row" org-return (org-at-table-p)]
  18974. "--"
  18975. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18976. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18977. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18978. "--"
  18979. ("Column"
  18980. ["Move Column Left" org-metaleft (org-at-table-p)]
  18981. ["Move Column Right" org-metaright (org-at-table-p)]
  18982. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18983. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18984. ("Row"
  18985. ["Move Row Up" org-metaup (org-at-table-p)]
  18986. ["Move Row Down" org-metadown (org-at-table-p)]
  18987. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18988. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18989. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18990. "--"
  18991. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18992. ("Rectangle"
  18993. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18994. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18995. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18996. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18997. "--"
  18998. ("Calculate"
  18999. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19000. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19001. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19002. "--"
  19003. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19004. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19005. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19006. "--"
  19007. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19008. "--"
  19009. ["Sum Column/Rectangle" org-table-sum
  19010. (or (org-at-table-p) (org-region-active-p))]
  19011. ["Which Column?" org-table-current-column (org-at-table-p)])
  19012. ["Debug Formulas"
  19013. org-table-toggle-formula-debugger
  19014. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19015. ["Show Col/Row Numbers"
  19016. org-table-toggle-coordinate-overlays
  19017. :style toggle
  19018. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19019. "--"
  19020. ["Create" org-table-create (and (not (org-at-table-p))
  19021. org-enable-table-editor)]
  19022. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19023. ["Import from File" org-table-import (not (org-at-table-p))]
  19024. ["Export to File" org-table-export (org-at-table-p)]
  19025. "--"
  19026. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19027. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19028. '("Org"
  19029. ("Show/Hide"
  19030. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19031. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19032. ["Sparse Tree..." org-sparse-tree t]
  19033. ["Reveal Context" org-reveal t]
  19034. ["Show All" show-all t]
  19035. "--"
  19036. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19037. "--"
  19038. ["New Heading" org-insert-heading t]
  19039. ("Navigate Headings"
  19040. ["Up" outline-up-heading t]
  19041. ["Next" outline-next-visible-heading t]
  19042. ["Previous" outline-previous-visible-heading t]
  19043. ["Next Same Level" outline-forward-same-level t]
  19044. ["Previous Same Level" outline-backward-same-level t]
  19045. "--"
  19046. ["Jump" org-goto t])
  19047. ("Edit Structure"
  19048. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19049. "--"
  19050. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19051. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19052. "--"
  19053. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19054. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19055. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19056. "--"
  19057. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19058. "--"
  19059. ["Copy visible text" org-copy-visible t]
  19060. "--"
  19061. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19062. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19063. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19064. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19065. "--"
  19066. ["Sort Region/Children" org-sort t]
  19067. "--"
  19068. ["Convert to odd levels" org-convert-to-odd-levels t]
  19069. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19070. ("Editing"
  19071. ["Emphasis..." org-emphasize t]
  19072. ["Edit Source Example" org-edit-special t]
  19073. "--"
  19074. ["Footnote new/jump" org-footnote-action t]
  19075. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19076. ("Archive"
  19077. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19078. "--"
  19079. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19080. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19081. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19082. )
  19083. "--"
  19084. ("Hyperlinks"
  19085. ["Store Link (Global)" org-store-link t]
  19086. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19087. ["Insert Link" org-insert-link t]
  19088. ["Follow Link" org-open-at-point t]
  19089. "--"
  19090. ["Next link" org-next-link t]
  19091. ["Previous link" org-previous-link t]
  19092. "--"
  19093. ["Descriptive Links"
  19094. org-toggle-link-display
  19095. :style radio
  19096. :selected org-descriptive-links
  19097. ]
  19098. ["Literal Links"
  19099. org-toggle-link-display
  19100. :style radio
  19101. :selected (not org-descriptive-links)])
  19102. "--"
  19103. ("TODO Lists"
  19104. ["TODO/DONE/-" org-todo t]
  19105. ("Select keyword"
  19106. ["Next keyword" org-shiftright (org-at-heading-p)]
  19107. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19108. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19109. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19110. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19111. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19112. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19113. "--"
  19114. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19115. :selected org-enforce-todo-dependencies :style toggle :active t]
  19116. "Settings for tree at point"
  19117. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19118. :selected (org-entry-get nil "ORDERED")
  19119. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19120. ["Do Children parallel" org-toggle-ordered-property :style radio
  19121. :selected (not (org-entry-get nil "ORDERED"))
  19122. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19123. "--"
  19124. ["Set Priority" org-priority t]
  19125. ["Priority Up" org-shiftup t]
  19126. ["Priority Down" org-shiftdown t]
  19127. "--"
  19128. ["Get news from all feeds" org-feed-update-all t]
  19129. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19130. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19131. ("TAGS and Properties"
  19132. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19133. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19134. "--"
  19135. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19136. ["Column view of properties" org-columns t]
  19137. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19138. ("Dates and Scheduling"
  19139. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19140. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19141. ("Change Date"
  19142. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19143. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19144. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19145. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19146. ["Compute Time Range" org-evaluate-time-range t]
  19147. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19148. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19149. "--"
  19150. ["Custom time format" org-toggle-time-stamp-overlays
  19151. :style radio :selected org-display-custom-times]
  19152. "--"
  19153. ["Goto Calendar" org-goto-calendar t]
  19154. ["Date from Calendar" org-date-from-calendar t]
  19155. "--"
  19156. ["Start/Restart Timer" org-timer-start t]
  19157. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19158. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19159. ["Insert Timer String" org-timer t]
  19160. ["Insert Timer Item" org-timer-item t])
  19161. ("Logging work"
  19162. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19163. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19164. ["Clock out" org-clock-out t]
  19165. ["Clock cancel" org-clock-cancel t]
  19166. "--"
  19167. ["Mark as default task" org-clock-mark-default-task t]
  19168. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19169. ["Goto running clock" org-clock-goto t]
  19170. "--"
  19171. ["Display times" org-clock-display t]
  19172. ["Create clock table" org-clock-report t]
  19173. "--"
  19174. ["Record DONE time"
  19175. (progn (setq org-log-done (not org-log-done))
  19176. (message "Switching to %s will %s record a timestamp"
  19177. (car org-done-keywords)
  19178. (if org-log-done "automatically" "not")))
  19179. :style toggle :selected org-log-done])
  19180. "--"
  19181. ["Agenda Command..." org-agenda t]
  19182. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19183. ("File List for Agenda")
  19184. ("Special views current file"
  19185. ["TODO Tree" org-show-todo-tree t]
  19186. ["Check Deadlines" org-check-deadlines t]
  19187. ["Timeline" org-timeline t]
  19188. ["Tags/Property tree" org-match-sparse-tree t])
  19189. "--"
  19190. ["Export/Publish..." org-export-dispatch t]
  19191. ("LaTeX"
  19192. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19193. :selected org-cdlatex-mode]
  19194. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19195. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19196. ["Modify math symbol" org-cdlatex-math-modify
  19197. (org-inside-LaTeX-fragment-p)]
  19198. ["Insert citation" org-reftex-citation t]
  19199. "--"
  19200. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19201. "--"
  19202. ("MobileOrg"
  19203. ["Push Files and Views" org-mobile-push t]
  19204. ["Get Captured and Flagged" org-mobile-pull t]
  19205. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19206. "--"
  19207. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19208. "--"
  19209. ("Documentation"
  19210. ["Show Version" org-version t]
  19211. ["Info Documentation" org-info t])
  19212. ("Customize"
  19213. ["Browse Org Group" org-customize t]
  19214. "--"
  19215. ["Expand This Menu" org-create-customize-menu
  19216. (fboundp 'customize-menu-create)])
  19217. ["Send bug report" org-submit-bug-report t]
  19218. "--"
  19219. ("Refresh/Reload"
  19220. ["Refresh setup current buffer" org-mode-restart t]
  19221. ["Reload Org (after update)" org-reload t]
  19222. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19223. ))
  19224. (defun org-info (&optional node)
  19225. "Read documentation for Org-mode in the info system.
  19226. With optional NODE, go directly to that node."
  19227. (interactive)
  19228. (info (format "(org)%s" (or node ""))))
  19229. ;;;###autoload
  19230. (defun org-submit-bug-report ()
  19231. "Submit a bug report on Org-mode via mail.
  19232. Don't hesitate to report any problems or inaccurate documentation.
  19233. If you don't have setup sending mail from (X)Emacs, please copy the
  19234. output buffer into your mail program, as it gives us important
  19235. information about your Org-mode version and configuration."
  19236. (interactive)
  19237. (require 'reporter)
  19238. (org-load-modules-maybe)
  19239. (org-require-autoloaded-modules)
  19240. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19241. (reporter-submit-bug-report
  19242. "emacs-orgmode@gnu.org"
  19243. (org-version nil 'full)
  19244. (let (list)
  19245. (save-window-excursion
  19246. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19247. (delete-other-windows)
  19248. (erase-buffer)
  19249. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19250. We would like to add your full Org-mode and Outline configuration to the
  19251. bug report. This greatly simplifies the work of the maintainer and
  19252. other experts on the mailing list.
  19253. HOWEVER, some variables you have customized may contain private
  19254. information. The names of customers, colleagues, or friends, might
  19255. appear in the form of file names, tags, todo states, or search strings.
  19256. If you answer yes to the prompt, you might want to check and remove
  19257. such private information before sending the email.")
  19258. (add-text-properties (point-min) (point-max) '(face org-warning))
  19259. (when (yes-or-no-p "Include your Org-mode configuration ")
  19260. (mapatoms
  19261. (lambda (v)
  19262. (and (boundp v)
  19263. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19264. (or (and (symbol-value v)
  19265. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19266. (and
  19267. (get v 'custom-type) (get v 'standard-value)
  19268. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19269. (push v list)))))
  19270. (kill-buffer (get-buffer "*Warn about privacy*"))
  19271. list))
  19272. nil nil
  19273. "Remember to cover the basics, that is, what you expected to happen and
  19274. what in fact did happen. You don't know how to make a good report? See
  19275. http://orgmode.org/manual/Feedback.html#Feedback
  19276. Your bug report will be posted to the Org-mode mailing list.
  19277. ------------------------------------------------------------------------")
  19278. (save-excursion
  19279. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19280. (replace-match "\\1Bug: \\3 [\\2]")))))
  19281. (defun org-install-agenda-files-menu ()
  19282. (let ((bl (buffer-list)))
  19283. (save-excursion
  19284. (while bl
  19285. (set-buffer (pop bl))
  19286. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19287. (when (derived-mode-p 'org-mode)
  19288. (easy-menu-change
  19289. '("Org") "File List for Agenda"
  19290. (append
  19291. (list
  19292. ["Edit File List" (org-edit-agenda-file-list) t]
  19293. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19294. ["Remove Current File from List" org-remove-file t]
  19295. ["Cycle through agenda files" org-cycle-agenda-files t]
  19296. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19297. "--")
  19298. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19299. ;;;; Documentation
  19300. (defun org-require-autoloaded-modules ()
  19301. (interactive)
  19302. (mapc 'require
  19303. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19304. org-remember org-table org-timer)))
  19305. ;;;###autoload
  19306. (defun org-reload (&optional uncompiled)
  19307. "Reload all org lisp files.
  19308. With prefix arg UNCOMPILED, load the uncompiled versions."
  19309. (interactive "P")
  19310. (require 'loadhist)
  19311. (let* ((org-dir (org-find-library-dir "org"))
  19312. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19313. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19314. (remove-re (mapconcat 'identity
  19315. (mapcar (lambda (f) (concat "^" f "$"))
  19316. (list (if (featurep 'xemacs)
  19317. "org-colview"
  19318. "org-colview-xemacs")
  19319. "org" "org-loaddefs" "org-version"))
  19320. "\\|"))
  19321. (feats (delete-dups
  19322. (mapcar 'file-name-sans-extension
  19323. (mapcar 'file-name-nondirectory
  19324. (delq nil
  19325. (mapcar 'feature-file
  19326. features))))))
  19327. (lfeat (append
  19328. (sort
  19329. (setq feats
  19330. (delq nil (mapcar
  19331. (lambda (f)
  19332. (if (and (string-match feature-re f)
  19333. (not (string-match remove-re f)))
  19334. f nil))
  19335. feats)))
  19336. 'string-lessp)
  19337. (list "org-version" "org")))
  19338. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19339. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19340. load-uncore load-misses)
  19341. (setq load-misses
  19342. (delq 't
  19343. (mapcar (lambda (f)
  19344. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19345. (and (string= org-dir contrib-dir)
  19346. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19347. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19348. (add-to-list 'load-uncore f 'append)
  19349. 't)
  19350. f))
  19351. lfeat)))
  19352. (if load-uncore
  19353. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19354. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19355. (if load-misses
  19356. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19357. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19358. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19359. ;;;###autoload
  19360. (defun org-customize ()
  19361. "Call the customize function with org as argument."
  19362. (interactive)
  19363. (org-load-modules-maybe)
  19364. (org-require-autoloaded-modules)
  19365. (customize-browse 'org))
  19366. (defun org-create-customize-menu ()
  19367. "Create a full customization menu for Org-mode, insert it into the menu."
  19368. (interactive)
  19369. (org-load-modules-maybe)
  19370. (org-require-autoloaded-modules)
  19371. (if (fboundp 'customize-menu-create)
  19372. (progn
  19373. (easy-menu-change
  19374. '("Org") "Customize"
  19375. `(["Browse Org group" org-customize t]
  19376. "--"
  19377. ,(customize-menu-create 'org)
  19378. ["Set" Custom-set t]
  19379. ["Save" Custom-save t]
  19380. ["Reset to Current" Custom-reset-current t]
  19381. ["Reset to Saved" Custom-reset-saved t]
  19382. ["Reset to Standard Settings" Custom-reset-standard t]))
  19383. (message "\"Org\"-menu now contains full customization menu"))
  19384. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19385. ;;;; Miscellaneous stuff
  19386. ;;; Generally useful functions
  19387. (defun org-get-at-bol (property)
  19388. "Get text property PROPERTY at beginning of line."
  19389. (get-text-property (point-at-bol) property))
  19390. (defun org-find-text-property-in-string (prop s)
  19391. "Return the first non-nil value of property PROP in string S."
  19392. (or (get-text-property 0 prop s)
  19393. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19394. prop s)))
  19395. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19396. "Display the given MESSAGE as a warning."
  19397. (if (fboundp 'display-warning)
  19398. (display-warning 'org message
  19399. (if (featurep 'xemacs) 'warning :warning))
  19400. (let ((buf (get-buffer-create "*Org warnings*")))
  19401. (with-current-buffer buf
  19402. (goto-char (point-max))
  19403. (insert "Warning (Org): " message)
  19404. (unless (bolp)
  19405. (newline)))
  19406. (display-buffer buf)
  19407. (sit-for 0))))
  19408. (defun org-eval (form)
  19409. "Eval FORM and return result."
  19410. (condition-case error
  19411. (eval form)
  19412. (error (format "%%![Error: %s]" error))))
  19413. (defun org-in-clocktable-p ()
  19414. "Check if the cursor is in a clocktable."
  19415. (let ((pos (point)) start)
  19416. (save-excursion
  19417. (end-of-line 1)
  19418. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19419. (setq start (match-beginning 0))
  19420. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19421. (>= (match-end 0) pos)
  19422. start))))
  19423. (defun org-in-commented-line ()
  19424. "Is point in a line starting with `#'?"
  19425. (equal (char-after (point-at-bol)) ?#))
  19426. (defun org-in-indented-comment-line ()
  19427. "Is point in a line starting with `#' after some white space?"
  19428. (save-excursion
  19429. (save-match-data
  19430. (goto-char (point-at-bol))
  19431. (looking-at "[ \t]*#"))))
  19432. (defun org-in-verbatim-emphasis ()
  19433. (save-match-data
  19434. (and (org-in-regexp org-emph-re 2)
  19435. (>= (point) (match-beginning 3))
  19436. (<= (point) (match-end 4))
  19437. (member (match-string 3) '("=" "~")))))
  19438. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19439. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19440. (if (and marker (marker-buffer marker)
  19441. (buffer-live-p (marker-buffer marker)))
  19442. (progn
  19443. (org-pop-to-buffer-same-window (marker-buffer marker))
  19444. (if (or (> marker (point-max)) (< marker (point-min)))
  19445. (widen))
  19446. (goto-char marker)
  19447. (org-show-context 'org-goto))
  19448. (if bookmark
  19449. (bookmark-jump bookmark)
  19450. (error "Cannot find location"))))
  19451. (defun org-quote-csv-field (s)
  19452. "Quote field for inclusion in CSV material."
  19453. (if (string-match "[\",]" s)
  19454. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19455. s))
  19456. (defun org-force-self-insert (N)
  19457. "Needed to enforce self-insert under remapping."
  19458. (interactive "p")
  19459. (self-insert-command N))
  19460. (defun org-string-width (s)
  19461. "Compute width of string, ignoring invisible characters.
  19462. This ignores character with invisibility property `org-link', and also
  19463. characters with property `org-cwidth', because these will become invisible
  19464. upon the next fontification round."
  19465. (let (b l)
  19466. (when (or (eq t buffer-invisibility-spec)
  19467. (assq 'org-link buffer-invisibility-spec))
  19468. (while (setq b (text-property-any 0 (length s)
  19469. 'invisible 'org-link s))
  19470. (setq s (concat (substring s 0 b)
  19471. (substring s (or (next-single-property-change
  19472. b 'invisible s) (length s)))))))
  19473. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19474. (setq s (concat (substring s 0 b)
  19475. (substring s (or (next-single-property-change
  19476. b 'org-cwidth s) (length s))))))
  19477. (setq l (string-width s) b -1)
  19478. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19479. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19480. l))
  19481. (defun org-shorten-string (s maxlength)
  19482. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19483. If the string is shorter or has length MAXLENGTH, just return the
  19484. original string. If it is longer, the functions finds a space in the
  19485. string, breaks this string off at that locations and adds three dots
  19486. as ellipsis. Including the ellipsis, the string will not be longer
  19487. than MAXLENGTH. If finding a good breaking point in the string does
  19488. not work, the string is just chopped off in the middle of a word
  19489. if necessary."
  19490. (if (<= (length s) maxlength)
  19491. s
  19492. (let* ((n (max (- maxlength 4) 1))
  19493. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19494. (if (string-match re s)
  19495. (concat (match-string 1 s) "...")
  19496. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19497. (defun org-get-indentation (&optional line)
  19498. "Get the indentation of the current line, interpreting tabs.
  19499. When LINE is given, assume it represents a line and compute its indentation."
  19500. (if line
  19501. (if (string-match "^ *" (org-remove-tabs line))
  19502. (match-end 0))
  19503. (save-excursion
  19504. (beginning-of-line 1)
  19505. (skip-chars-forward " \t")
  19506. (current-column))))
  19507. (defun org-get-string-indentation (s)
  19508. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19509. (let ((n -1) (i 0) (w tab-width) c)
  19510. (catch 'exit
  19511. (while (< (setq n (1+ n)) (length s))
  19512. (setq c (aref s n))
  19513. (cond ((= c ?\ ) (setq i (1+ i)))
  19514. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19515. (t (throw 'exit t)))))
  19516. i))
  19517. (defun org-remove-tabs (s &optional width)
  19518. "Replace tabulators in S with spaces.
  19519. Assumes that s is a single line, starting in column 0."
  19520. (setq width (or width tab-width))
  19521. (while (string-match "\t" s)
  19522. (setq s (replace-match
  19523. (make-string
  19524. (- (* width (/ (+ (match-beginning 0) width) width))
  19525. (match-beginning 0)) ?\ )
  19526. t t s)))
  19527. s)
  19528. (defun org-fix-indentation (line ind)
  19529. "Fix indentation in LINE.
  19530. IND is a cons cell with target and minimum indentation.
  19531. If the current indentation in LINE is smaller than the minimum,
  19532. leave it alone. If it is larger than ind, set it to the target."
  19533. (let* ((l (org-remove-tabs line))
  19534. (i (org-get-indentation l))
  19535. (i1 (car ind)) (i2 (cdr ind)))
  19536. (if (>= i i2) (setq l (substring line i2)))
  19537. (if (> i1 0)
  19538. (concat (make-string i1 ?\ ) l)
  19539. l)))
  19540. (defun org-remove-indentation (code &optional n)
  19541. "Remove the maximum common indentation from the lines in CODE.
  19542. N may optionally be the number of spaces to remove."
  19543. (with-temp-buffer
  19544. (insert code)
  19545. (org-do-remove-indentation n)
  19546. (buffer-string)))
  19547. (defun org-do-remove-indentation (&optional n)
  19548. "Remove the maximum common indentation from the buffer."
  19549. (untabify (point-min) (point-max))
  19550. (let ((min 10000) re)
  19551. (if n
  19552. (setq min n)
  19553. (goto-char (point-min))
  19554. (while (re-search-forward "^ *[^ \n]" nil t)
  19555. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19556. (unless (or (= min 0) (= min 10000))
  19557. (setq re (format "^ \\{%d\\}" min))
  19558. (goto-char (point-min))
  19559. (while (re-search-forward re nil t)
  19560. (replace-match "")
  19561. (end-of-line 1))
  19562. min)))
  19563. (defun org-fill-template (template alist)
  19564. "Find each %key of ALIST in TEMPLATE and replace it."
  19565. (let ((case-fold-search nil)
  19566. entry key value)
  19567. (setq alist (sort (copy-sequence alist)
  19568. (lambda (a b) (< (length (car a)) (length (car b))))))
  19569. (while (setq entry (pop alist))
  19570. (setq template
  19571. (replace-regexp-in-string
  19572. (concat "%" (regexp-quote (car entry)))
  19573. (or (cdr entry) "") template t t)))
  19574. template))
  19575. (defun org-base-buffer (buffer)
  19576. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19577. (if (not buffer)
  19578. buffer
  19579. (or (buffer-base-buffer buffer)
  19580. buffer)))
  19581. (defun org-trim (s)
  19582. "Remove whitespace at beginning and end of string."
  19583. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19584. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19585. s)
  19586. (defun org-wrap (string &optional width lines)
  19587. "Wrap string to either a number of lines, or a width in characters.
  19588. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19589. that costs. If there is a word longer than WIDTH, the text is actually
  19590. wrapped to the length of that word.
  19591. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19592. many lines, whatever width that takes.
  19593. The return value is a list of lines, without newlines at the end."
  19594. (let* ((words (org-split-string string "[ \t\n]+"))
  19595. (maxword (apply 'max (mapcar 'org-string-width words)))
  19596. w ll)
  19597. (cond (width
  19598. (org-do-wrap words (max maxword width)))
  19599. (lines
  19600. (setq w maxword)
  19601. (setq ll (org-do-wrap words maxword))
  19602. (if (<= (length ll) lines)
  19603. ll
  19604. (setq ll words)
  19605. (while (> (length ll) lines)
  19606. (setq w (1+ w))
  19607. (setq ll (org-do-wrap words w)))
  19608. ll))
  19609. (t (error "Cannot wrap this")))))
  19610. (defun org-do-wrap (words width)
  19611. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19612. (let (lines line)
  19613. (while words
  19614. (setq line (pop words))
  19615. (while (and words (< (+ (length line) (length (car words))) width))
  19616. (setq line (concat line " " (pop words))))
  19617. (setq lines (push line lines)))
  19618. (nreverse lines)))
  19619. (defun org-split-string (string &optional separators)
  19620. "Splits STRING into substrings at SEPARATORS.
  19621. No empty strings are returned if there are matches at the beginning
  19622. and end of string."
  19623. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19624. (start 0)
  19625. notfirst
  19626. (list nil))
  19627. (while (and (string-match rexp string
  19628. (if (and notfirst
  19629. (= start (match-beginning 0))
  19630. (< start (length string)))
  19631. (1+ start) start))
  19632. (< (match-beginning 0) (length string)))
  19633. (setq notfirst t)
  19634. (or (eq (match-beginning 0) 0)
  19635. (and (eq (match-beginning 0) (match-end 0))
  19636. (eq (match-beginning 0) start))
  19637. (setq list
  19638. (cons (substring string start (match-beginning 0))
  19639. list)))
  19640. (setq start (match-end 0)))
  19641. (or (eq start (length string))
  19642. (setq list
  19643. (cons (substring string start)
  19644. list)))
  19645. (nreverse list)))
  19646. (defun org-quote-vert (s)
  19647. "Replace \"|\" with \"\\vert\"."
  19648. (while (string-match "|" s)
  19649. (setq s (replace-match "\\vert" t t s)))
  19650. s)
  19651. (defun org-uuidgen-p (s)
  19652. "Is S an ID created by UUIDGEN?"
  19653. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19654. (defun org-in-src-block-p (&optional inside)
  19655. "Whether point is in a code source block.
  19656. When INSIDE is non-nil, don't consider we are within a src block
  19657. when point is at #+BEGIN_SRC or #+END_SRC."
  19658. (let ((case-fold-search t) ov)
  19659. (or (and (setq ov (overlays-at (point)))
  19660. (memq 'org-block-background
  19661. (overlay-properties (car ov))))
  19662. (and (not inside)
  19663. (save-match-data
  19664. (save-excursion
  19665. (beginning-of-line)
  19666. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19667. (defun org-context ()
  19668. "Return a list of contexts of the current cursor position.
  19669. If several contexts apply, all are returned.
  19670. Each context entry is a list with a symbol naming the context, and
  19671. two positions indicating start and end of the context. Possible
  19672. contexts are:
  19673. :headline anywhere in a headline
  19674. :headline-stars on the leading stars in a headline
  19675. :todo-keyword on a TODO keyword (including DONE) in a headline
  19676. :tags on the TAGS in a headline
  19677. :priority on the priority cookie in a headline
  19678. :item on the first line of a plain list item
  19679. :item-bullet on the bullet/number of a plain list item
  19680. :checkbox on the checkbox in a plain list item
  19681. :table in an org-mode table
  19682. :table-special on a special filed in a table
  19683. :table-table in a table.el table
  19684. :clocktable in a clocktable
  19685. :src-block in a source block
  19686. :link on a hyperlink
  19687. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19688. :target on a <<target>>
  19689. :radio-target on a <<<radio-target>>>
  19690. :latex-fragment on a LaTeX fragment
  19691. :latex-preview on a LaTeX fragment with overlaid preview image
  19692. This function expects the position to be visible because it uses font-lock
  19693. faces as a help to recognize the following contexts: :table-special, :link,
  19694. and :keyword."
  19695. (let* ((f (get-text-property (point) 'face))
  19696. (faces (if (listp f) f (list f)))
  19697. (case-fold-search t)
  19698. (p (point)) clist o)
  19699. ;; First the large context
  19700. (cond
  19701. ((org-at-heading-p t)
  19702. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19703. (when (progn
  19704. (beginning-of-line 1)
  19705. (looking-at org-todo-line-tags-regexp))
  19706. (push (org-point-in-group p 1 :headline-stars) clist)
  19707. (push (org-point-in-group p 2 :todo-keyword) clist)
  19708. (push (org-point-in-group p 4 :tags) clist))
  19709. (goto-char p)
  19710. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19711. (if (looking-at "\\[#[A-Z0-9]\\]")
  19712. (push (org-point-in-group p 0 :priority) clist)))
  19713. ((org-at-item-p)
  19714. (push (org-point-in-group p 2 :item-bullet) clist)
  19715. (push (list :item (point-at-bol)
  19716. (save-excursion (org-end-of-item) (point)))
  19717. clist)
  19718. (and (org-at-item-checkbox-p)
  19719. (push (org-point-in-group p 0 :checkbox) clist)))
  19720. ((org-at-table-p)
  19721. (push (list :table (org-table-begin) (org-table-end)) clist)
  19722. (if (memq 'org-formula faces)
  19723. (push (list :table-special
  19724. (previous-single-property-change p 'face)
  19725. (next-single-property-change p 'face)) clist)))
  19726. ((org-at-table-p 'any)
  19727. (push (list :table-table) clist)))
  19728. (goto-char p)
  19729. (let ((case-fold-search t))
  19730. ;; New the "medium" contexts: clocktables, source blocks
  19731. (cond ((org-in-clocktable-p)
  19732. (push (list :clocktable
  19733. (and (or (looking-at "#\\+BEGIN: clocktable")
  19734. (search-backward "#+BEGIN: clocktable" nil t))
  19735. (match-beginning 0))
  19736. (and (re-search-forward "#\\+END:?" nil t)
  19737. (match-end 0))) clist))
  19738. ((org-in-src-block-p)
  19739. (push (list :src-block
  19740. (and (or (looking-at "#\\+BEGIN_SRC")
  19741. (search-backward "#+BEGIN_SRC" nil t))
  19742. (match-beginning 0))
  19743. (and (search-forward "#+END_SRC" nil t)
  19744. (match-beginning 0))) clist))))
  19745. (goto-char p)
  19746. ;; Now the small context
  19747. (cond
  19748. ((org-at-timestamp-p)
  19749. (push (org-point-in-group p 0 :timestamp) clist))
  19750. ((memq 'org-link faces)
  19751. (push (list :link
  19752. (previous-single-property-change p 'face)
  19753. (next-single-property-change p 'face)) clist))
  19754. ((memq 'org-special-keyword faces)
  19755. (push (list :keyword
  19756. (previous-single-property-change p 'face)
  19757. (next-single-property-change p 'face)) clist))
  19758. ((org-at-target-p)
  19759. (push (org-point-in-group p 0 :target) clist)
  19760. (goto-char (1- (match-beginning 0)))
  19761. (if (looking-at org-radio-target-regexp)
  19762. (push (org-point-in-group p 0 :radio-target) clist))
  19763. (goto-char p))
  19764. ((setq o (car (delq nil
  19765. (mapcar
  19766. (lambda (x)
  19767. (if (memq x org-latex-fragment-image-overlays) x))
  19768. (overlays-at (point))))))
  19769. (push (list :latex-fragment
  19770. (overlay-start o) (overlay-end o)) clist)
  19771. (push (list :latex-preview
  19772. (overlay-start o) (overlay-end o)) clist))
  19773. ((org-inside-LaTeX-fragment-p)
  19774. ;; FIXME: positions wrong.
  19775. (push (list :latex-fragment (point) (point)) clist)))
  19776. (setq clist (nreverse (delq nil clist)))
  19777. clist))
  19778. ;; FIXME: Compare with at-regexp-p Do we need both?
  19779. (defun org-in-regexp (re &optional nlines visually)
  19780. "Check if point is inside a match of regexp.
  19781. Normally only the current line is checked, but you can include NLINES extra
  19782. lines both before and after point into the search.
  19783. If VISUALLY is set, require that the cursor is not after the match but
  19784. really on, so that the block visually is on the match."
  19785. (catch 'exit
  19786. (let ((pos (point))
  19787. (eol (point-at-eol (+ 1 (or nlines 0))))
  19788. (inc (if visually 1 0)))
  19789. (save-excursion
  19790. (beginning-of-line (- 1 (or nlines 0)))
  19791. (while (re-search-forward re eol t)
  19792. (if (and (<= (match-beginning 0) pos)
  19793. (>= (+ inc (match-end 0)) pos))
  19794. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19795. (defun org-at-regexp-p (regexp)
  19796. "Is point inside a match of REGEXP in the current line?"
  19797. (catch 'exit
  19798. (save-excursion
  19799. (let ((pos (point)) (end (point-at-eol)))
  19800. (beginning-of-line 1)
  19801. (while (re-search-forward regexp end t)
  19802. (if (and (<= (match-beginning 0) pos)
  19803. (>= (match-end 0) pos))
  19804. (throw 'exit t)))
  19805. nil))))
  19806. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19807. "Non-nil when point is between matches of START-RE and END-RE.
  19808. Also return a non-nil value when point is on one of the matches.
  19809. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19810. buffer positions. Default values are the positions of headlines
  19811. surrounding the point.
  19812. The functions returns a cons cell whose car (resp. cdr) is the
  19813. position before START-RE (resp. after END-RE)."
  19814. (save-match-data
  19815. (let ((pos (point))
  19816. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19817. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19818. beg end)
  19819. (save-excursion
  19820. ;; Point is on a block when on START-RE or if START-RE can be
  19821. ;; found before it...
  19822. (and (or (org-at-regexp-p start-re)
  19823. (re-search-backward start-re limit-up t))
  19824. (setq beg (match-beginning 0))
  19825. ;; ... and END-RE after it...
  19826. (goto-char (match-end 0))
  19827. (re-search-forward end-re limit-down t)
  19828. (> (setq end (match-end 0)) pos)
  19829. ;; ... without another START-RE in-between.
  19830. (goto-char (match-beginning 0))
  19831. (not (re-search-backward start-re (1+ beg) t))
  19832. ;; Return value.
  19833. (cons beg end))))))
  19834. (defun org-in-block-p (names)
  19835. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19836. NAMES is a list of strings containing names of blocks.
  19837. Return first block name matched, or nil. Beware that in case of
  19838. nested blocks, the returned name may not belong to the closest
  19839. block from point."
  19840. (save-match-data
  19841. (catch 'exit
  19842. (let ((case-fold-search t)
  19843. (lim-up (save-excursion (outline-previous-heading)))
  19844. (lim-down (save-excursion (outline-next-heading))))
  19845. (mapc (lambda (name)
  19846. (let ((n (regexp-quote name)))
  19847. (when (org-between-regexps-p
  19848. (concat "^[ \t]*#\\+begin_" n)
  19849. (concat "^[ \t]*#\\+end_" n)
  19850. lim-up lim-down)
  19851. (throw 'exit n))))
  19852. names))
  19853. nil)))
  19854. (defun org-in-drawer-p ()
  19855. "Is point within a drawer?"
  19856. (save-match-data
  19857. (let ((case-fold-search t)
  19858. (lim-up (save-excursion (outline-previous-heading)))
  19859. (lim-down (save-excursion (outline-next-heading))))
  19860. (org-between-regexps-p
  19861. (concat "^[ \t]*:" (regexp-opt org-drawers) ":")
  19862. "^[ \t]*:end:.*$"
  19863. lim-up lim-down))))
  19864. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19865. "Call `multi-occur' with buffers for all agenda files."
  19866. (interactive "sOrg-files matching: \np")
  19867. (let* ((files (org-agenda-files))
  19868. (tnames (mapcar 'file-truename files))
  19869. (extra org-agenda-text-search-extra-files)
  19870. f)
  19871. (when (eq (car extra) 'agenda-archives)
  19872. (setq extra (cdr extra))
  19873. (setq files (org-add-archive-files files)))
  19874. (while (setq f (pop extra))
  19875. (unless (member (file-truename f) tnames)
  19876. (add-to-list 'files f 'append)
  19877. (add-to-list 'tnames (file-truename f) 'append)))
  19878. (multi-occur
  19879. (mapcar (lambda (x)
  19880. (with-current-buffer
  19881. (or (get-file-buffer x) (find-file-noselect x))
  19882. (widen)
  19883. (current-buffer)))
  19884. files)
  19885. regexp)))
  19886. (if (boundp 'occur-mode-find-occurrence-hook)
  19887. ;; Emacs 23
  19888. (add-hook 'occur-mode-find-occurrence-hook
  19889. (lambda ()
  19890. (when (derived-mode-p 'org-mode)
  19891. (org-reveal))))
  19892. ;; Emacs 22
  19893. (defadvice occur-mode-goto-occurrence
  19894. (after org-occur-reveal activate)
  19895. (and (derived-mode-p 'org-mode) (org-reveal)))
  19896. (defadvice occur-mode-goto-occurrence-other-window
  19897. (after org-occur-reveal activate)
  19898. (and (derived-mode-p 'org-mode) (org-reveal)))
  19899. (defadvice occur-mode-display-occurrence
  19900. (after org-occur-reveal activate)
  19901. (when (derived-mode-p 'org-mode)
  19902. (let ((pos (occur-mode-find-occurrence)))
  19903. (with-current-buffer (marker-buffer pos)
  19904. (save-excursion
  19905. (goto-char pos)
  19906. (org-reveal)))))))
  19907. (defun org-occur-link-in-agenda-files ()
  19908. "Create a link and search for it in the agendas.
  19909. The link is not stored in `org-stored-links', it is just created
  19910. for the search purpose."
  19911. (interactive)
  19912. (let ((link (condition-case nil
  19913. (org-store-link nil)
  19914. (error "Unable to create a link to here"))))
  19915. (org-occur-in-agenda-files (regexp-quote link))))
  19916. (defun org-reverse-string (string)
  19917. "Return the reverse of STRING."
  19918. (apply 'string (reverse (string-to-list string))))
  19919. (defsubst org-uniquify (list)
  19920. "Non-destructively remove duplicate elements from LIST."
  19921. (let ((res (copy-sequence list))) (delete-dups res)))
  19922. (defun org-uniquify-alist (alist)
  19923. "Merge elements of ALIST with the same key.
  19924. For example, in this alist:
  19925. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19926. => '((a 1 3) (b 2))
  19927. merge (a 1) and (a 3) into (a 1 3).
  19928. The function returns the new ALIST."
  19929. (let (rtn)
  19930. (mapc
  19931. (lambda (e)
  19932. (let (n)
  19933. (if (not (assoc (car e) rtn))
  19934. (push e rtn)
  19935. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19936. (setq rtn (assq-delete-all (car e) rtn))
  19937. (push n rtn))))
  19938. alist)
  19939. rtn))
  19940. (defun org-delete-all (elts list)
  19941. "Remove all elements in ELTS from LIST."
  19942. (while elts
  19943. (setq list (delete (pop elts) list)))
  19944. list)
  19945. (defun org-count (cl-item cl-seq)
  19946. "Count the number of occurrences of ITEM in SEQ.
  19947. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19948. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19949. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19950. (while (< cl-start cl-end)
  19951. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19952. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19953. (setq cl-start (1+ cl-start)))
  19954. cl-count))
  19955. (defun org-remove-if (predicate seq)
  19956. "Remove everything from SEQ that fulfills PREDICATE."
  19957. (let (res e)
  19958. (while seq
  19959. (setq e (pop seq))
  19960. (if (not (funcall predicate e)) (push e res)))
  19961. (nreverse res)))
  19962. (defun org-remove-if-not (predicate seq)
  19963. "Remove everything from SEQ that does not fulfill PREDICATE."
  19964. (let (res e)
  19965. (while seq
  19966. (setq e (pop seq))
  19967. (if (funcall predicate e) (push e res)))
  19968. (nreverse res)))
  19969. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19970. "Reduce two-argument FUNCTION across SEQ.
  19971. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19972. \":initial-value\" removed."
  19973. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19974. (cadr (memq :initial-value cl-keys)))
  19975. (cl-seq (pop cl-seq))
  19976. (t (funcall cl-func)))))
  19977. (while cl-seq
  19978. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19979. cl-accum))
  19980. (defun org-back-over-empty-lines ()
  19981. "Move backwards over whitespace, to the beginning of the first empty line.
  19982. Returns the number of empty lines passed."
  19983. (let ((pos (point)))
  19984. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19985. (skip-chars-backward " \t\n\r")
  19986. (unless (eobp)
  19987. (forward-line -1)))
  19988. (beginning-of-line 2)
  19989. (goto-char (min (point) pos))
  19990. (count-lines (point) pos)))
  19991. (defun org-skip-whitespace ()
  19992. (skip-chars-forward " \t\n\r"))
  19993. (defun org-point-in-group (point group &optional context)
  19994. "Check if POINT is in match-group GROUP.
  19995. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19996. match. If the match group does not exist or point is not inside it,
  19997. return nil."
  19998. (and (match-beginning group)
  19999. (>= point (match-beginning group))
  20000. (<= point (match-end group))
  20001. (if context
  20002. (list context (match-beginning group) (match-end group))
  20003. t)))
  20004. (defun org-switch-to-buffer-other-window (&rest args)
  20005. "Switch to buffer in a second window on the current frame.
  20006. In particular, do not allow pop-up frames.
  20007. Returns the newly created buffer."
  20008. (org-no-popups
  20009. (apply 'switch-to-buffer-other-window args)))
  20010. (defun org-combine-plists (&rest plists)
  20011. "Create a single property list from all plists in PLISTS.
  20012. The process starts by copying the first list, and then setting properties
  20013. from the other lists. Settings in the last list are the most significant
  20014. ones and overrule settings in the other lists."
  20015. (let ((rtn (copy-sequence (pop plists)))
  20016. p v ls)
  20017. (while plists
  20018. (setq ls (pop plists))
  20019. (while ls
  20020. (setq p (pop ls) v (pop ls))
  20021. (setq rtn (plist-put rtn p v))))
  20022. rtn))
  20023. (defun org-replace-escapes (string table)
  20024. "Replace %-escapes in STRING with values in TABLE.
  20025. TABLE is an association list with keys like \"%a\" and string values.
  20026. The sequences in STRING may contain normal field width and padding information,
  20027. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20028. so values can contain further %-escapes if they are define later in TABLE."
  20029. (let ((tbl (copy-alist table))
  20030. (case-fold-search nil)
  20031. (pchg 0)
  20032. e re rpl)
  20033. (while (setq e (pop tbl))
  20034. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20035. (when (and (cdr e) (string-match re (cdr e)))
  20036. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20037. (safe "SREF"))
  20038. (add-text-properties 0 3 (list 'sref sref) safe)
  20039. (setcdr e (replace-match safe t t (cdr e)))))
  20040. (while (string-match re string)
  20041. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20042. (cdr e)))
  20043. (setq string (replace-match rpl t t string))))
  20044. (while (setq pchg (next-property-change pchg string))
  20045. (let ((sref (get-text-property pchg 'sref string)))
  20046. (when (and sref (string-match "SREF" string pchg))
  20047. (setq string (replace-match sref t t string)))))
  20048. string))
  20049. (defun org-sublist (list start end)
  20050. "Return a section of LIST, from START to END.
  20051. Counting starts at 1."
  20052. (let (rtn (c start))
  20053. (setq list (nthcdr (1- start) list))
  20054. (while (and list (<= c end))
  20055. (push (pop list) rtn)
  20056. (setq c (1+ c)))
  20057. (nreverse rtn)))
  20058. (defun org-find-base-buffer-visiting (file)
  20059. "Like `find-buffer-visiting' but always return the base buffer and
  20060. not an indirect buffer."
  20061. (let ((buf (or (get-file-buffer file)
  20062. (find-buffer-visiting file))))
  20063. (if buf
  20064. (or (buffer-base-buffer buf) buf)
  20065. nil)))
  20066. (defun org-image-file-name-regexp (&optional extensions)
  20067. "Return regexp matching the file names of images.
  20068. If EXTENSIONS is given, only match these."
  20069. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20070. (image-file-name-regexp)
  20071. (let ((image-file-name-extensions
  20072. (or extensions
  20073. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20074. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20075. (concat "\\."
  20076. (regexp-opt (nconc (mapcar 'upcase
  20077. image-file-name-extensions)
  20078. image-file-name-extensions)
  20079. t)
  20080. "\\'"))))
  20081. (defun org-file-image-p (file &optional extensions)
  20082. "Return non-nil if FILE is an image."
  20083. (save-match-data
  20084. (string-match (org-image-file-name-regexp extensions) file)))
  20085. (defun org-get-cursor-date (&optional with-time)
  20086. "Return the date at cursor in as a time.
  20087. This works in the calendar and in the agenda, anywhere else it just
  20088. returns the current time.
  20089. If WITH-TIME is non-nil, returns the time of the event at point (in
  20090. the agenda) or the current time of the day."
  20091. (let (date day defd tp tm hod mod)
  20092. (when with-time
  20093. (setq tp (get-text-property (point) 'time))
  20094. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20095. (setq hod (string-to-number (match-string 1 tp))
  20096. mod (string-to-number (match-string 2 tp))))
  20097. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20098. mod (nth 1 (decode-time (current-time))))))
  20099. (cond
  20100. ((eq major-mode 'calendar-mode)
  20101. (setq date (calendar-cursor-to-date)
  20102. defd (encode-time 0 (or mod 0) (or hod 0)
  20103. (nth 1 date) (nth 0 date) (nth 2 date))))
  20104. ((eq major-mode 'org-agenda-mode)
  20105. (setq day (get-text-property (point) 'day))
  20106. (if day
  20107. (setq date (calendar-gregorian-from-absolute day)
  20108. defd (encode-time 0 (or mod 0) (or hod 0)
  20109. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20110. (or defd (current-time))))
  20111. (defun org-mark-subtree (&optional up)
  20112. "Mark the current subtree.
  20113. This puts point at the start of the current subtree, and mark at
  20114. the end. If a numeric prefix UP is given, move up into the
  20115. hierarchy of headlines by UP levels before marking the subtree."
  20116. (interactive "P")
  20117. (org-with-limited-levels
  20118. (cond ((org-at-heading-p) (beginning-of-line))
  20119. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20120. (t (outline-previous-visible-heading 1))))
  20121. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20122. (if (org-called-interactively-p 'any)
  20123. (call-interactively 'org-mark-element)
  20124. (org-mark-element)))
  20125. ;;; Indentation
  20126. (defun org-indent-line ()
  20127. "Indent line depending on context."
  20128. (interactive)
  20129. (let* ((pos (point))
  20130. (itemp (org-at-item-p))
  20131. (case-fold-search t)
  20132. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20133. (inline-task-p (and (featurep 'org-inlinetask)
  20134. (org-inlinetask-in-task-p)))
  20135. (inline-re (and inline-task-p
  20136. (org-inlinetask-outline-regexp)))
  20137. column)
  20138. (if (and orgstruct-is-++ (eq pos (point)))
  20139. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20140. (indent-according-to-mode))
  20141. (beginning-of-line 1)
  20142. (cond
  20143. ;; Headings
  20144. ((looking-at org-outline-regexp) (setq column 0))
  20145. ;; Footnote definition
  20146. ((looking-at org-footnote-definition-re) (setq column 0))
  20147. ;; Literal examples
  20148. ((looking-at "[ \t]*:\\( \\|$\\)")
  20149. (setq column (org-get-indentation))) ; do nothing
  20150. ;; Lists
  20151. ((ignore-errors (goto-char (org-in-item-p)))
  20152. (setq column (if itemp
  20153. (org-get-indentation)
  20154. (org-list-item-body-column (point))))
  20155. (goto-char pos))
  20156. ;; Drawers
  20157. ((and (looking-at "[ \t]*:END:")
  20158. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20159. (save-excursion
  20160. (goto-char (1- (match-beginning 1)))
  20161. (setq column (current-column))))
  20162. ;; Special blocks
  20163. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20164. (save-excursion
  20165. (re-search-backward
  20166. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20167. (setq column (org-get-indentation (match-string 0))))
  20168. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20169. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20170. (save-excursion
  20171. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20172. (setq column
  20173. (cond ((equal (downcase (match-string 1)) "src")
  20174. ;; src blocks: let `org-edit-src-exit' handle them
  20175. (org-get-indentation))
  20176. ((equal (downcase (match-string 1)) "example")
  20177. (max (org-get-indentation)
  20178. (org-get-indentation (match-string 0))))
  20179. (t
  20180. (org-get-indentation (match-string 0))))))
  20181. ;; This line has nothing special, look at the previous relevant
  20182. ;; line to compute indentation
  20183. (t
  20184. (beginning-of-line 0)
  20185. (while (and (not (bobp))
  20186. (not (looking-at org-table-line-regexp))
  20187. (not (looking-at org-drawer-regexp))
  20188. ;; When point started in an inline task, do not move
  20189. ;; above task starting line.
  20190. (not (and inline-task-p (looking-at inline-re)))
  20191. ;; Skip drawers, blocks, empty lines, verbatim,
  20192. ;; comments, tables, footnotes definitions, lists,
  20193. ;; inline tasks.
  20194. (or (and (looking-at "[ \t]*:END:")
  20195. (re-search-backward org-drawer-regexp nil t))
  20196. (and (looking-at "[ \t]*#\\+end_")
  20197. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20198. (looking-at "[ \t]*[\n:#|]")
  20199. (looking-at org-footnote-definition-re)
  20200. (and (not inline-task-p)
  20201. (featurep 'org-inlinetask)
  20202. (org-inlinetask-in-task-p)
  20203. (or (org-inlinetask-goto-beginning) t))))
  20204. (beginning-of-line 0))
  20205. (cond
  20206. ;; There was a list item above.
  20207. ((save-excursion
  20208. (and (ignore-errors (goto-char (org-in-item-p)))
  20209. (goto-char
  20210. (org-list-get-top-point (org-list-struct)))))
  20211. (looking-at org-list-full-item-re)
  20212. (setq column (length (match-string 0))))
  20213. ;; There was an heading above.
  20214. ((looking-at "\\*+[ \t]+")
  20215. (if (not org-adapt-indentation)
  20216. (setq column 0)
  20217. (goto-char (match-end 0))
  20218. (setq column (current-column))))
  20219. ;; A drawer had started and is unfinished
  20220. ((looking-at org-drawer-regexp)
  20221. (goto-char (1- (match-beginning 1)))
  20222. (setq column (current-column)))
  20223. ;; Else, nothing noticeable found: get indentation and go on.
  20224. (t (setq column (org-get-indentation))))))
  20225. ;; Now apply indentation and move cursor accordingly
  20226. (goto-char pos)
  20227. (if (<= (current-column) (current-indentation))
  20228. (org-indent-line-to column)
  20229. (save-excursion (org-indent-line-to column)))
  20230. ;; Special polishing for properties, see `org-property-format'
  20231. (setq column (current-column))
  20232. (beginning-of-line 1)
  20233. (if (looking-at
  20234. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20235. (replace-match (concat (match-string 1)
  20236. (format org-property-format
  20237. (match-string 2) (match-string 3)))
  20238. t t))
  20239. (org-move-to-column column))))
  20240. (defun org-indent-drawer ()
  20241. "Indent the drawer at point."
  20242. (interactive)
  20243. (let ((p (point))
  20244. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20245. (match-end 0)))
  20246. (folded
  20247. (save-excursion
  20248. (end-of-line)
  20249. (when (overlays-at (point))
  20250. (member 'invisible (overlay-properties
  20251. (car (overlays-at (point)))))))))
  20252. (when folded (org-cycle))
  20253. (indent-for-tab-command)
  20254. (while (and (move-beginning-of-line 2) (< (point) e))
  20255. (indent-for-tab-command))
  20256. (goto-char p)
  20257. (when folded (org-cycle)))
  20258. (message "Drawer at point indented"))
  20259. (defun org-indent-block ()
  20260. "Indent the block at point."
  20261. (interactive)
  20262. (let ((p (point))
  20263. (case-fold-search t)
  20264. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20265. (match-end 0)))
  20266. (folded
  20267. (save-excursion
  20268. (end-of-line)
  20269. (when (overlays-at (point))
  20270. (member 'invisible (overlay-properties
  20271. (car (overlays-at (point)))))))))
  20272. (when folded (org-cycle))
  20273. (indent-for-tab-command)
  20274. (while (and (move-beginning-of-line 2) (< (point) e))
  20275. (indent-for-tab-command))
  20276. (goto-char p)
  20277. (when folded (org-cycle)))
  20278. (message "Block at point indented"))
  20279. (defun org-indent-region (start end)
  20280. "Indent region."
  20281. (interactive "r")
  20282. (save-excursion
  20283. (let ((line-end (org-current-line end)))
  20284. (goto-char start)
  20285. (while (< (org-current-line) line-end)
  20286. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20287. (t (call-interactively 'org-indent-line)))
  20288. (move-beginning-of-line 2)))))
  20289. ;;; Filling
  20290. ;; We use our own fill-paragraph and auto-fill functions.
  20291. ;; `org-fill-paragraph' relies on adaptive filling and context
  20292. ;; checking. Appropriate `fill-prefix' is computed with
  20293. ;; `org-adaptive-fill-function'.
  20294. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20295. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20296. ;; `org-adaptive-fill-function' value. Internally,
  20297. ;; `org-comment-line-break-function' breaks the line.
  20298. ;; `org-setup-filling' installs filling and auto-filling related
  20299. ;; variables during `org-mode' initialization.
  20300. (defun org-setup-filling ()
  20301. (interactive)
  20302. ;; Prevent auto-fill from inserting unwanted new items.
  20303. (when (boundp 'fill-nobreak-predicate)
  20304. (org-set-local
  20305. 'fill-nobreak-predicate
  20306. (org-uniquify
  20307. (append fill-nobreak-predicate
  20308. '(org-fill-paragraph-separate-nobreak-p
  20309. org-fill-line-break-nobreak-p
  20310. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20311. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20312. (org-set-local 'auto-fill-inhibit-regexp nil)
  20313. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20314. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20315. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20316. (defvar org-element-paragraph-separate) ; org-element.el
  20317. (defun org-fill-paragraph-separate-nobreak-p ()
  20318. "Non-nil when a line break at point would insert a new item."
  20319. (looking-at (substring org-element-paragraph-separate 1)))
  20320. (defun org-fill-line-break-nobreak-p ()
  20321. "Non-nil when a line break at point would create an Org line break."
  20322. (save-excursion
  20323. (skip-chars-backward "[ \t]")
  20324. (skip-chars-backward "\\\\")
  20325. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20326. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20327. "Non-nil when a line break at point would insert a new item."
  20328. (and (org-at-timestamp-p t)
  20329. (not (looking-at org-ts-regexp-both))))
  20330. (declare-function message-in-body-p "message" ())
  20331. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20332. (defun org-adaptive-fill-function ()
  20333. "Compute a fill prefix for the current line.
  20334. Return fill prefix, as a string, or nil if current line isn't
  20335. meant to be filled."
  20336. (let (prefix)
  20337. (catch 'exit
  20338. (when (derived-mode-p 'message-mode)
  20339. (save-excursion
  20340. (beginning-of-line)
  20341. (cond ((or (not (message-in-body-p))
  20342. (looking-at orgtbl-line-start-regexp))
  20343. (throw 'exit nil))
  20344. ((looking-at message-cite-prefix-regexp)
  20345. (throw 'exit (match-string-no-properties 0)))
  20346. ((looking-at org-outline-regexp)
  20347. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20348. (org-with-wide-buffer
  20349. (let* ((p (line-beginning-position))
  20350. (element (save-excursion
  20351. (beginning-of-line)
  20352. (or (ignore-errors (org-element-at-point))
  20353. (user-error "An element cannot be parsed line %d"
  20354. (line-number-at-pos (point))))))
  20355. (type (org-element-type element))
  20356. (post-affiliated (org-element-property :post-affiliated element)))
  20357. (unless (and post-affiliated (< p post-affiliated))
  20358. (case type
  20359. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20360. (footnote-definition "")
  20361. ((item plain-list)
  20362. (make-string (org-list-item-body-column
  20363. (or post-affiliated
  20364. (org-element-property :begin element)))
  20365. ? ))
  20366. (paragraph
  20367. ;; Fill prefix is usually the same as the current line,
  20368. ;; except if the paragraph is at the beginning of an item.
  20369. (let ((parent (org-element-property :parent element)))
  20370. (cond ((eq (org-element-type parent) 'item)
  20371. (make-string (org-list-item-body-column
  20372. (org-element-property :begin parent))
  20373. ? ))
  20374. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20375. (match-string 0))
  20376. (t ""))))
  20377. (comment-block
  20378. ;; Only fill contents if P is within block boundaries.
  20379. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20380. (forward-line)
  20381. (point)))
  20382. (cend (save-excursion
  20383. (goto-char (org-element-property :end element))
  20384. (skip-chars-backward " \r\t\n")
  20385. (line-beginning-position))))
  20386. (when (and (>= p cbeg) (< p cend))
  20387. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20388. (match-string 0)
  20389. "")))))))))))
  20390. (declare-function message-goto-body "message" ())
  20391. (defvar message-cite-prefix-regexp) ; From message.el
  20392. (defun org-fill-paragraph (&optional justify)
  20393. "Fill element at point, when applicable.
  20394. This function only applies to comment blocks, comments, example
  20395. blocks and paragraphs. Also, as a special case, re-align table
  20396. when point is at one.
  20397. If JUSTIFY is non-nil (interactively, with prefix argument),
  20398. justify as well. If `sentence-end-double-space' is non-nil, then
  20399. period followed by one space does not end a sentence, so don't
  20400. break a line there. The variable `fill-column' controls the
  20401. width for filling.
  20402. For convenience, when point is at a plain list, an item or
  20403. a footnote definition, try to fill the first paragraph within."
  20404. (interactive)
  20405. (if (and (derived-mode-p 'message-mode)
  20406. (or (not (message-in-body-p))
  20407. (save-excursion (move-beginning-of-line 1)
  20408. (looking-at message-cite-prefix-regexp))))
  20409. ;; First ensure filling is correct in message-mode.
  20410. (let ((fill-paragraph-function
  20411. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20412. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20413. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20414. (paragraph-separate
  20415. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20416. (fill-paragraph nil))
  20417. (with-syntax-table org-mode-transpose-word-syntax-table
  20418. ;; Move to end of line in order to get the first paragraph
  20419. ;; within a plain list or a footnote definition.
  20420. (let ((element (save-excursion
  20421. (end-of-line)
  20422. (or (ignore-errors (org-element-at-point))
  20423. (user-error "An element cannot be parsed line %d"
  20424. (line-number-at-pos (point)))))))
  20425. ;; First check if point is in a blank line at the beginning of
  20426. ;; the buffer. In that case, ignore filling.
  20427. (case (org-element-type element)
  20428. ;; Use major mode filling function is src blocks.
  20429. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20430. ;; Align Org tables, leave table.el tables as-is.
  20431. (table-row (org-table-align) t)
  20432. (table
  20433. (when (eq (org-element-property :type element) 'org)
  20434. (save-excursion
  20435. (goto-char (org-element-property :post-affiliated element))
  20436. (org-table-align)))
  20437. t)
  20438. (paragraph
  20439. ;; Paragraphs may contain `line-break' type objects.
  20440. (let ((beg (max (point-min)
  20441. (org-element-property :contents-begin element)))
  20442. (end (min (point-max)
  20443. (org-element-property :contents-end element))))
  20444. ;; Do nothing if point is at an affiliated keyword.
  20445. (if (< (line-end-position) beg) t
  20446. (when (derived-mode-p 'message-mode)
  20447. ;; In `message-mode', do not fill following citation
  20448. ;; in current paragraph nor text before message body.
  20449. (let ((body-start (save-excursion (message-goto-body))))
  20450. (when body-start (setq beg (max body-start beg))))
  20451. (when (save-excursion
  20452. (re-search-forward
  20453. (concat "^" message-cite-prefix-regexp) end t))
  20454. (setq end (match-beginning 0))))
  20455. ;; Fill paragraph, taking line breaks into account.
  20456. ;; For that, slice the paragraph using line breaks as
  20457. ;; separators, and fill the parts in reverse order to
  20458. ;; avoid messing with markers.
  20459. (save-excursion
  20460. (goto-char end)
  20461. (mapc
  20462. (lambda (pos)
  20463. (fill-region-as-paragraph pos (point) justify)
  20464. (goto-char pos))
  20465. ;; Find the list of ending positions for line breaks
  20466. ;; in the current paragraph. Add paragraph
  20467. ;; beginning to include first slice.
  20468. (nreverse
  20469. (cons beg
  20470. (org-element-map
  20471. (org-element--parse-objects
  20472. beg end nil (org-element-restriction 'paragraph))
  20473. 'line-break
  20474. (lambda (lb) (org-element-property :end lb)))))))
  20475. t)))
  20476. ;; Contents of `comment-block' type elements should be
  20477. ;; filled as plain text, but only if point is within block
  20478. ;; markers.
  20479. (comment-block
  20480. (let* ((case-fold-search t)
  20481. (beg (save-excursion
  20482. (goto-char (org-element-property :begin element))
  20483. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20484. (forward-line)
  20485. (point)))
  20486. (end (save-excursion
  20487. (goto-char (org-element-property :end element))
  20488. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20489. (line-beginning-position))))
  20490. (when (and (>= (point) beg) (< (point) end))
  20491. (fill-region-as-paragraph
  20492. (save-excursion
  20493. (end-of-line)
  20494. (re-search-backward "^[ \t]*$" beg 'move)
  20495. (line-beginning-position))
  20496. (save-excursion
  20497. (beginning-of-line)
  20498. (re-search-forward "^[ \t]*$" end 'move)
  20499. (line-beginning-position))
  20500. justify)))
  20501. t)
  20502. ;; Fill comments.
  20503. (comment (fill-comment-paragraph justify))
  20504. ;; Ignore every other element.
  20505. (otherwise t))))))
  20506. (defun org-auto-fill-function ()
  20507. "Auto-fill function."
  20508. ;; Check if auto-filling is meaningful.
  20509. (let ((fc (current-fill-column)))
  20510. (when (and fc (> (current-column) fc))
  20511. (let* ((fill-prefix (org-adaptive-fill-function))
  20512. ;; Enforce empty fill prefix, if required. Otherwise, it
  20513. ;; will be computed again.
  20514. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20515. (when fill-prefix (do-auto-fill))))))
  20516. (defun org-comment-line-break-function (&optional soft)
  20517. "Break line at point and indent, continuing comment if within one.
  20518. The inserted newline is marked hard if variable
  20519. `use-hard-newlines' is true, unless optional argument SOFT is
  20520. non-nil."
  20521. (if soft (insert-and-inherit ?\n) (newline 1))
  20522. (save-excursion (forward-char -1) (delete-horizontal-space))
  20523. (delete-horizontal-space)
  20524. (indent-to-left-margin)
  20525. (insert-before-markers-and-inherit fill-prefix))
  20526. ;;; Comments
  20527. ;; Org comments syntax is quite complex. It requires the entire line
  20528. ;; to be just a comment. Also, even with the right syntax at the
  20529. ;; beginning of line, some some elements (i.e. verse-block or
  20530. ;; example-block) don't accept comments. Usual Emacs comment commands
  20531. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20532. ;; Org still relies on `comment-dwim', but cannot trust
  20533. ;; `comment-only-p'. So, `comment-region-function' and
  20534. ;; `uncomment-region-function' both point
  20535. ;; to`org-comment-or-uncomment-region'. Eventually,
  20536. ;; `org-insert-comment' takes care of insertion of comments at the
  20537. ;; beginning of line.
  20538. ;; `org-setup-comments-handling' install comments related variables
  20539. ;; during `org-mode' initialization.
  20540. (defun org-setup-comments-handling ()
  20541. (interactive)
  20542. (org-set-local 'comment-use-syntax nil)
  20543. (org-set-local 'comment-start "# ")
  20544. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20545. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20546. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20547. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20548. (defun org-insert-comment ()
  20549. "Insert an empty comment above current line.
  20550. If the line is empty, insert comment at its beginning."
  20551. (beginning-of-line)
  20552. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20553. (org-indent-line)
  20554. (insert "# "))
  20555. (defvar comment-empty-lines) ; From newcomment.el.
  20556. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20557. "Comment or uncomment each non-blank line in the region.
  20558. Uncomment each non-blank line between BEG and END if it only
  20559. contains commented lines. Otherwise, comment them."
  20560. (save-restriction
  20561. ;; Restrict region
  20562. (narrow-to-region (save-excursion (goto-char beg)
  20563. (skip-chars-forward " \r\t\n" end)
  20564. (line-beginning-position))
  20565. (save-excursion (goto-char end)
  20566. (skip-chars-backward " \r\t\n" beg)
  20567. (line-end-position)))
  20568. (let ((uncommentp
  20569. ;; UNCOMMENTP is non-nil when every non blank line between
  20570. ;; BEG and END is a comment.
  20571. (save-excursion
  20572. (goto-char (point-min))
  20573. (while (and (not (eobp))
  20574. (let ((element (org-element-at-point)))
  20575. (and (eq (org-element-type element) 'comment)
  20576. (goto-char (min (point-max)
  20577. (org-element-property
  20578. :end element)))))))
  20579. (eobp))))
  20580. (if uncommentp
  20581. ;; Only blank lines and comments in region: uncomment it.
  20582. (save-excursion
  20583. (goto-char (point-min))
  20584. (while (not (eobp))
  20585. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20586. (replace-match "" nil nil nil 1))
  20587. (forward-line)))
  20588. ;; Comment each line in region.
  20589. (let ((min-indent (point-max)))
  20590. ;; First find the minimum indentation across all lines.
  20591. (save-excursion
  20592. (goto-char (point-min))
  20593. (while (and (not (eobp)) (not (zerop min-indent)))
  20594. (unless (looking-at "[ \t]*$")
  20595. (setq min-indent (min min-indent (current-indentation))))
  20596. (forward-line)))
  20597. ;; Then loop over all lines.
  20598. (save-excursion
  20599. (goto-char (point-min))
  20600. (while (not (eobp))
  20601. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20602. ;; Don't get fooled by invisible text (e.g. link path)
  20603. ;; when moving to column MIN-INDENT.
  20604. (let ((buffer-invisibility-spec nil))
  20605. (org-move-to-column min-indent t))
  20606. (insert comment-start))
  20607. (forward-line))))))))
  20608. ;;; Planning
  20609. ;; This section contains tools to operate on timestamp objects, as
  20610. ;; returned by, e.g. `org-element-context'.
  20611. (defun org-timestamp-has-time-p (timestamp)
  20612. "Non-nil when TIMESTAMP has a time specified."
  20613. (org-element-property :hour-start timestamp))
  20614. (defun org-timestamp-format (timestamp format &optional end utc)
  20615. "Format a TIMESTAMP element into a string.
  20616. FORMAT is a format specifier to be passed to
  20617. `format-time-string'.
  20618. When optional argument END is non-nil, use end of date-range or
  20619. time-range, if possible.
  20620. When optional argument UTC is non-nil, time will be expressed as
  20621. Universal Time."
  20622. (format-time-string
  20623. format
  20624. (apply 'encode-time
  20625. (cons 0
  20626. (mapcar
  20627. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20628. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20629. '(:minute-start :hour-start :day-start :month-start
  20630. :year-start)))))
  20631. utc))
  20632. (defun org-timestamp-split-range (timestamp &optional end)
  20633. "Extract a timestamp object from a date or time range.
  20634. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20635. the end of the range. Otherwise, extract its start.
  20636. Return a new timestamp object sharing the same parent as
  20637. TIMESTAMP."
  20638. (let ((type (org-element-property :type timestamp)))
  20639. (if (memq type '(active inactive diary)) timestamp
  20640. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20641. ;; Set new type.
  20642. (org-element-put-property
  20643. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20644. ;; Copy start properties over end properties if END is
  20645. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20646. (let ((p-alist '((:minute-start . :minute-end)
  20647. (:hour-start . :hour-end)
  20648. (:day-start . :day-end)
  20649. (:month-start . :month-end)
  20650. (:year-start . :year-end))))
  20651. (dolist (p-cell p-alist)
  20652. (org-element-put-property
  20653. split-ts
  20654. (funcall (if end 'car 'cdr) p-cell)
  20655. (org-element-property
  20656. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20657. ;; Eventually refresh `:raw-value'.
  20658. (org-element-put-property split-ts :raw-value nil)
  20659. (org-element-put-property
  20660. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20661. (defun org-timestamp-translate (timestamp &optional boundary)
  20662. "Apply `org-translate-time' on a TIMESTAMP object.
  20663. When optional argument BOUNDARY is non-nil, it is either the
  20664. symbol `start' or `end'. In this case, only translate the
  20665. starting or ending part of TIMESTAMP if it is a date or time
  20666. range. Otherwise, translate both parts."
  20667. (if (and (not boundary)
  20668. (memq (org-element-property :type timestamp)
  20669. '(active-range inactive-range)))
  20670. (concat
  20671. (org-translate-time
  20672. (org-element-property :raw-value
  20673. (org-timestamp-split-range timestamp)))
  20674. "--"
  20675. (org-translate-time
  20676. (org-element-property :raw-value
  20677. (org-timestamp-split-range timestamp t))))
  20678. (org-translate-time
  20679. (org-element-property
  20680. :raw-value
  20681. (if (not boundary) timestamp
  20682. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20683. ;;; Other stuff.
  20684. (defun org-toggle-fixed-width-section (arg)
  20685. "Toggle the fixed-width export.
  20686. If there is no active region, the QUOTE keyword at the current headline is
  20687. inserted or removed. When present, it causes the text between this headline
  20688. and the next to be exported as fixed-width text, and unmodified.
  20689. If there is an active region, this command adds or removes a colon as the
  20690. first character of this line. If the first character of a line is a colon,
  20691. this line is also exported in fixed-width font."
  20692. (interactive "P")
  20693. (let* ((cc 0)
  20694. (regionp (org-region-active-p))
  20695. (beg (if regionp (region-beginning) (point)))
  20696. (end (if regionp (region-end)))
  20697. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20698. (case-fold-search nil)
  20699. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20700. off)
  20701. (if regionp
  20702. (save-excursion
  20703. (goto-char beg)
  20704. (setq cc (current-column))
  20705. (beginning-of-line 1)
  20706. (setq off (looking-at re))
  20707. (while (> nlines 0)
  20708. (setq nlines (1- nlines))
  20709. (beginning-of-line 1)
  20710. (cond
  20711. (arg
  20712. (org-move-to-column cc t)
  20713. (insert ": \n")
  20714. (forward-line -1))
  20715. ((and off (looking-at re))
  20716. (replace-match "" t t nil 1))
  20717. ((not off) (org-move-to-column cc t) (insert ": ")))
  20718. (forward-line 1)))
  20719. (save-excursion
  20720. (org-back-to-heading)
  20721. (cond
  20722. ((looking-at (format org-heading-keyword-regexp-format
  20723. org-quote-string))
  20724. (goto-char (match-end 1))
  20725. (looking-at (concat " +" org-quote-string))
  20726. (replace-match "" t t)
  20727. (when (eolp) (insert " ")))
  20728. ((looking-at org-outline-regexp)
  20729. (goto-char (match-end 0))
  20730. (insert org-quote-string " ")))))))
  20731. (defun org-reftex-citation ()
  20732. "Use reftex-citation to insert a citation into the buffer.
  20733. This looks for a line like
  20734. #+BIBLIOGRAPHY: foo plain option:-d
  20735. and derives from it that foo.bib is the bibliography file relevant
  20736. for this document. It then installs the necessary environment for RefTeX
  20737. to work in this buffer and calls `reftex-citation' to insert a citation
  20738. into the buffer.
  20739. Export of such citations to both LaTeX and HTML is handled by the contributed
  20740. package org-exp-bibtex by Taru Karttunen."
  20741. (interactive)
  20742. (let ((reftex-docstruct-symbol 'rds)
  20743. (reftex-cite-format "\\cite{%l}")
  20744. rds bib)
  20745. (save-excursion
  20746. (save-restriction
  20747. (widen)
  20748. (let ((case-fold-search t)
  20749. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20750. (if (not (save-excursion
  20751. (or (re-search-forward re nil t)
  20752. (re-search-backward re nil t))))
  20753. (error "No bibliography defined in file")
  20754. (setq bib (concat (match-string 1) ".bib")
  20755. rds (list (list 'bib bib)))))))
  20756. (call-interactively 'reftex-citation)))
  20757. ;;;; Functions extending outline functionality
  20758. (defun org-beginning-of-line (&optional arg)
  20759. "Go to the beginning of the current line. If that is invisible, continue
  20760. to a visible line beginning. This makes the function of C-a more intuitive.
  20761. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20762. first attempt, and only move to after the tags when the cursor is already
  20763. beyond the end of the headline."
  20764. (interactive "P")
  20765. (let ((pos (point))
  20766. (special (if (consp org-special-ctrl-a/e)
  20767. (car org-special-ctrl-a/e)
  20768. org-special-ctrl-a/e))
  20769. refpos)
  20770. (if (org-bound-and-true-p visual-line-mode)
  20771. (beginning-of-visual-line 1)
  20772. (beginning-of-line 1))
  20773. (if (and arg (fboundp 'move-beginning-of-line))
  20774. (call-interactively 'move-beginning-of-line)
  20775. (if (bobp)
  20776. nil
  20777. (backward-char 1)
  20778. (if (org-truely-invisible-p)
  20779. (while (and (not (bobp)) (org-truely-invisible-p))
  20780. (backward-char 1)
  20781. (beginning-of-line 1))
  20782. (forward-char 1))))
  20783. (when special
  20784. (cond
  20785. ((and (looking-at org-complex-heading-regexp)
  20786. (= (char-after (match-end 1)) ?\ ))
  20787. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20788. (point-at-eol)))
  20789. (goto-char
  20790. (if (eq special t)
  20791. (cond ((> pos refpos) refpos)
  20792. ((= pos (point)) refpos)
  20793. (t (point)))
  20794. (cond ((> pos (point)) (point))
  20795. ((not (eq last-command this-command)) (point))
  20796. (t refpos)))))
  20797. ((org-at-item-p)
  20798. ;; Being at an item and not looking at an the item means point
  20799. ;; was previously moved to beginning of a visual line, which
  20800. ;; doesn't contain the item. Therefore, do nothing special,
  20801. ;; just stay here.
  20802. (when (looking-at org-list-full-item-re)
  20803. ;; Set special position at first white space character after
  20804. ;; bullet, and check-box, if any.
  20805. (let ((after-bullet
  20806. (let ((box (match-end 3)))
  20807. (if (not box) (match-end 1)
  20808. (let ((after (char-after box)))
  20809. (if (and after (= after ? )) (1+ box) box))))))
  20810. ;; Special case: Move point to special position when
  20811. ;; currently after it or at beginning of line.
  20812. (if (eq special t)
  20813. (when (or (> pos after-bullet) (= (point) pos))
  20814. (goto-char after-bullet))
  20815. ;; Reversed case: Move point to special position when
  20816. ;; point was already at beginning of line and command is
  20817. ;; repeated.
  20818. (when (and (= (point) pos) (eq last-command this-command))
  20819. (goto-char after-bullet))))))))
  20820. (org-no-warnings
  20821. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20822. (defun org-end-of-line (&optional arg)
  20823. "Go to the end of the line.
  20824. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20825. tags on the first attempt, and only move to after the tags when
  20826. the cursor is already beyond the end of the headline."
  20827. (interactive "P")
  20828. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20829. org-special-ctrl-a/e))
  20830. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20831. 'end-of-visual-line)
  20832. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20833. (t 'end-of-line))))
  20834. (if (or (not special) arg) (call-interactively move-fun)
  20835. (let* ((element (save-excursion (beginning-of-line)
  20836. (org-element-at-point)))
  20837. (type (org-element-type element)))
  20838. (cond
  20839. ((memq type '(headline inlinetask))
  20840. (let ((pos (point)))
  20841. (beginning-of-line 1)
  20842. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20843. (if (eq special t)
  20844. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20845. (goto-char (match-beginning 1))
  20846. (goto-char (match-end 0)))
  20847. (if (or (< pos (match-end 0))
  20848. (not (eq this-command last-command)))
  20849. (goto-char (match-end 0))
  20850. (goto-char (match-beginning 1))))
  20851. (call-interactively move-fun))))
  20852. ((org-element-property :hiddenp element)
  20853. ;; If element is hidden, `move-end-of-line' would put point
  20854. ;; after it. Use `end-of-line' to stay on current line.
  20855. (call-interactively 'end-of-line))
  20856. (t (call-interactively move-fun)))))
  20857. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20858. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20859. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20860. (defun org-backward-sentence (&optional arg)
  20861. "Go to beginning of sentence, or beginning of table field.
  20862. This will call `backward-sentence' or `org-table-beginning-of-field',
  20863. depending on context."
  20864. (interactive "P")
  20865. (cond
  20866. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20867. (t (call-interactively 'backward-sentence))))
  20868. (defun org-forward-sentence (&optional arg)
  20869. "Go to end of sentence, or end of table field.
  20870. This will call `forward-sentence' or `org-table-end-of-field',
  20871. depending on context."
  20872. (interactive "P")
  20873. (cond
  20874. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20875. (t (call-interactively 'forward-sentence))))
  20876. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20877. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20878. (defun org-kill-line (&optional arg)
  20879. "Kill line, to tags or end of line."
  20880. (interactive "P")
  20881. (cond
  20882. ((or (not org-special-ctrl-k)
  20883. (bolp)
  20884. (not (org-at-heading-p)))
  20885. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20886. org-ctrl-k-protect-subtree)
  20887. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20888. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20889. (user-error "C-k aborted as it would kill a hidden subtree")))
  20890. (call-interactively
  20891. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20892. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20893. (kill-region (point) (match-beginning 1))
  20894. (org-set-tags nil t))
  20895. (t (kill-region (point) (point-at-eol)))))
  20896. (define-key org-mode-map "\C-k" 'org-kill-line)
  20897. (defun org-yank (&optional arg)
  20898. "Yank. If the kill is a subtree, treat it specially.
  20899. This command will look at the current kill and check if is a single
  20900. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20901. cursor is at the beginning of a line or after the stars of a currently
  20902. empty headline, then the yank is handled specially. How exactly depends
  20903. on the value of the following variables, both set by default.
  20904. org-yank-folded-subtrees
  20905. When set, the subtree(s) will be folded after insertion, but only
  20906. if doing so would now swallow text after the yanked text.
  20907. org-yank-adjusted-subtrees
  20908. When set, the subtree will be promoted or demoted in order to
  20909. fit into the local outline tree structure, which means that the level
  20910. will be adjusted so that it becomes the smaller one of the two
  20911. *visible* surrounding headings.
  20912. Any prefix to this command will cause `yank' to be called directly with
  20913. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20914. will just
  20915. plainly yank the text as it is.
  20916. \[1] The test checks if the first non-white line is a heading
  20917. and if there are no other headings with fewer stars."
  20918. (interactive "P")
  20919. (org-yank-generic 'yank arg))
  20920. (defun org-yank-generic (command arg)
  20921. "Perform some yank-like command.
  20922. This function implements the behavior described in the `org-yank'
  20923. documentation. However, it has been generalized to work for any
  20924. interactive command with similar behavior."
  20925. ;; pretend to be command COMMAND
  20926. (setq this-command command)
  20927. (if arg
  20928. (call-interactively command)
  20929. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20930. (and (org-kill-is-subtree-p)
  20931. (or (bolp)
  20932. (and (looking-at "[ \t]*$")
  20933. (string-match
  20934. "\\`\\*+\\'"
  20935. (buffer-substring (point-at-bol) (point)))))))
  20936. swallowp)
  20937. (cond
  20938. ((and subtreep org-yank-folded-subtrees)
  20939. (let ((beg (point))
  20940. end)
  20941. (if (and subtreep org-yank-adjusted-subtrees)
  20942. (org-paste-subtree nil nil 'for-yank)
  20943. (call-interactively command))
  20944. (setq end (point))
  20945. (goto-char beg)
  20946. (when (and (bolp) subtreep
  20947. (not (setq swallowp
  20948. (org-yank-folding-would-swallow-text beg end))))
  20949. (org-with-limited-levels
  20950. (or (looking-at org-outline-regexp)
  20951. (re-search-forward org-outline-regexp-bol end t))
  20952. (while (and (< (point) end) (looking-at org-outline-regexp))
  20953. (hide-subtree)
  20954. (org-cycle-show-empty-lines 'folded)
  20955. (condition-case nil
  20956. (outline-forward-same-level 1)
  20957. (error (goto-char end))))))
  20958. (when swallowp
  20959. (message
  20960. "Inserted text not folded because that would swallow text"))
  20961. (goto-char end)
  20962. (skip-chars-forward " \t\n\r")
  20963. (beginning-of-line 1)
  20964. (push-mark beg 'nomsg)))
  20965. ((and subtreep org-yank-adjusted-subtrees)
  20966. (let ((beg (point-at-bol)))
  20967. (org-paste-subtree nil nil 'for-yank)
  20968. (push-mark beg 'nomsg)))
  20969. (t
  20970. (call-interactively command))))))
  20971. (defun org-yank-folding-would-swallow-text (beg end)
  20972. "Would hide-subtree at BEG swallow any text after END?"
  20973. (let (level)
  20974. (org-with-limited-levels
  20975. (save-excursion
  20976. (goto-char beg)
  20977. (when (or (looking-at org-outline-regexp)
  20978. (re-search-forward org-outline-regexp-bol end t))
  20979. (setq level (org-outline-level)))
  20980. (goto-char end)
  20981. (skip-chars-forward " \t\r\n\v\f")
  20982. (if (or (eobp)
  20983. (and (bolp) (looking-at org-outline-regexp)
  20984. (<= (org-outline-level) level)))
  20985. nil ; Nothing would be swallowed
  20986. t))))) ; something would swallow
  20987. (define-key org-mode-map "\C-y" 'org-yank)
  20988. (defun org-truely-invisible-p ()
  20989. "Check if point is at a character currently not visible.
  20990. This version does not only check the character property, but also
  20991. `visible-mode'."
  20992. ;; Early versions of noutline don't have `outline-invisible-p'.
  20993. (if (org-bound-and-true-p visible-mode)
  20994. nil
  20995. (outline-invisible-p)))
  20996. (defun org-invisible-p2 ()
  20997. "Check if point is at a character currently not visible."
  20998. (save-excursion
  20999. (if (and (eolp) (not (bobp))) (backward-char 1))
  21000. ;; Early versions of noutline don't have `outline-invisible-p'.
  21001. (outline-invisible-p)))
  21002. (defun org-back-to-heading (&optional invisible-ok)
  21003. "Call `outline-back-to-heading', but provide a better error message."
  21004. (condition-case nil
  21005. (outline-back-to-heading invisible-ok)
  21006. (error (error "Before first headline at position %d in buffer %s"
  21007. (point) (current-buffer)))))
  21008. (defun org-before-first-heading-p ()
  21009. "Before first heading?"
  21010. (save-excursion
  21011. (end-of-line)
  21012. (null (re-search-backward org-outline-regexp-bol nil t))))
  21013. (defun org-at-heading-p (&optional ignored)
  21014. (outline-on-heading-p t))
  21015. ;; Compatibility alias with Org versions < 7.8.03
  21016. (defalias 'org-on-heading-p 'org-at-heading-p)
  21017. (defun org-at-comment-p nil
  21018. "Is cursor in a line starting with a # character?"
  21019. (save-excursion
  21020. (beginning-of-line)
  21021. (looking-at "^#")))
  21022. (defun org-at-drawer-p nil
  21023. "Is cursor at a drawer keyword?"
  21024. (save-excursion
  21025. (move-beginning-of-line 1)
  21026. (looking-at org-drawer-regexp)))
  21027. (defun org-at-block-p nil
  21028. "Is cursor at a block keyword?"
  21029. (save-excursion
  21030. (move-beginning-of-line 1)
  21031. (looking-at org-block-regexp)))
  21032. (defun org-point-at-end-of-empty-headline ()
  21033. "If point is at the end of an empty headline, return t, else nil.
  21034. If the heading only contains a TODO keyword, it is still still considered
  21035. empty."
  21036. (and (looking-at "[ \t]*$")
  21037. (when org-todo-line-regexp
  21038. (save-excursion
  21039. (beginning-of-line 1)
  21040. (let ((case-fold-search nil))
  21041. (looking-at org-todo-line-regexp)
  21042. (string= (match-string 3) ""))))))
  21043. (defun org-at-heading-or-item-p ()
  21044. (or (org-at-heading-p) (org-at-item-p)))
  21045. (defun org-at-target-p ()
  21046. (or (org-in-regexp org-radio-target-regexp)
  21047. (org-in-regexp org-target-regexp)))
  21048. ;; Compatibility alias with Org versions < 7.8.03
  21049. (defalias 'org-on-target-p 'org-at-target-p)
  21050. (defun org-up-heading-all (arg)
  21051. "Move to the heading line of which the present line is a subheading.
  21052. This function considers both visible and invisible heading lines.
  21053. With argument, move up ARG levels."
  21054. (if (fboundp 'outline-up-heading-all)
  21055. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21056. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21057. (defun org-up-heading-safe ()
  21058. "Move to the heading line of which the present line is a subheading.
  21059. This version will not throw an error. It will return the level of the
  21060. headline found, or nil if no higher level is found.
  21061. Also, this function will be a lot faster than `outline-up-heading',
  21062. because it relies on stars being the outline starters. This can really
  21063. make a significant difference in outlines with very many siblings."
  21064. (let (start-level re)
  21065. (org-back-to-heading t)
  21066. (setq start-level (funcall outline-level))
  21067. (if (equal start-level 1)
  21068. nil
  21069. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21070. (if (re-search-backward re nil t)
  21071. (funcall outline-level)))))
  21072. (defun org-first-sibling-p ()
  21073. "Is this heading the first child of its parents?"
  21074. (interactive)
  21075. (let ((re org-outline-regexp-bol)
  21076. level l)
  21077. (unless (org-at-heading-p t)
  21078. (user-error "Not at a heading"))
  21079. (setq level (funcall outline-level))
  21080. (save-excursion
  21081. (if (not (re-search-backward re nil t))
  21082. t
  21083. (setq l (funcall outline-level))
  21084. (< l level)))))
  21085. (defun org-goto-sibling (&optional previous)
  21086. "Goto the next sibling, even if it is invisible.
  21087. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21088. when a sibling was found. When none is found, return nil and don't
  21089. move point."
  21090. (let ((fun (if previous 're-search-backward 're-search-forward))
  21091. (pos (point))
  21092. (re org-outline-regexp-bol)
  21093. level l)
  21094. (when (condition-case nil (org-back-to-heading t) (error nil))
  21095. (setq level (funcall outline-level))
  21096. (catch 'exit
  21097. (or previous (forward-char 1))
  21098. (while (funcall fun re nil t)
  21099. (setq l (funcall outline-level))
  21100. (when (< l level) (goto-char pos) (throw 'exit nil))
  21101. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21102. (goto-char pos)
  21103. nil))))
  21104. (defun org-show-siblings ()
  21105. "Show all siblings of the current headline."
  21106. (save-excursion
  21107. (while (org-goto-sibling) (org-flag-heading nil)))
  21108. (save-excursion
  21109. (while (org-goto-sibling 'previous)
  21110. (org-flag-heading nil))))
  21111. (defun org-goto-first-child ()
  21112. "Goto the first child, even if it is invisible.
  21113. Return t when a child was found. Otherwise don't move point and
  21114. return nil."
  21115. (let (level (pos (point)) (re org-outline-regexp-bol))
  21116. (when (condition-case nil (org-back-to-heading t) (error nil))
  21117. (setq level (outline-level))
  21118. (forward-char 1)
  21119. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21120. (progn (goto-char (match-beginning 0)) t)
  21121. (goto-char pos) nil))))
  21122. (defun org-show-hidden-entry ()
  21123. "Show an entry where even the heading is hidden."
  21124. (save-excursion
  21125. (org-show-entry)))
  21126. (defun org-flag-heading (flag &optional entry)
  21127. "Flag the current heading. FLAG non-nil means make invisible.
  21128. When ENTRY is non-nil, show the entire entry."
  21129. (save-excursion
  21130. (org-back-to-heading t)
  21131. ;; Check if we should show the entire entry
  21132. (if entry
  21133. (progn
  21134. (org-show-entry)
  21135. (save-excursion
  21136. (and (outline-next-heading)
  21137. (org-flag-heading nil))))
  21138. (outline-flag-region (max (point-min) (1- (point)))
  21139. (save-excursion (outline-end-of-heading) (point))
  21140. flag))))
  21141. (defun org-get-next-sibling ()
  21142. "Move to next heading of the same level, and return point.
  21143. If there is no such heading, return nil.
  21144. This is like outline-next-sibling, but invisible headings are ok."
  21145. (let ((level (funcall outline-level)))
  21146. (outline-next-heading)
  21147. (while (and (not (eobp)) (> (funcall outline-level) level))
  21148. (outline-next-heading))
  21149. (if (or (eobp) (< (funcall outline-level) level))
  21150. nil
  21151. (point))))
  21152. (defun org-get-last-sibling ()
  21153. "Move to previous heading of the same level, and return point.
  21154. If there is no such heading, return nil."
  21155. (let ((opoint (point))
  21156. (level (funcall outline-level)))
  21157. (outline-previous-heading)
  21158. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21159. (while (and (> (funcall outline-level) level)
  21160. (not (bobp)))
  21161. (outline-previous-heading))
  21162. (if (< (funcall outline-level) level)
  21163. nil
  21164. (point)))))
  21165. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21166. "Goto to the end of a subtree."
  21167. ;; This contains an exact copy of the original function, but it uses
  21168. ;; `org-back-to-heading', to make it work also in invisible
  21169. ;; trees. And is uses an invisible-ok argument.
  21170. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21171. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21172. ;; with many children and grandchildren etc, this can be much faster
  21173. ;; than the outline version.
  21174. (org-back-to-heading invisible-ok)
  21175. (let ((first t)
  21176. (level (funcall outline-level)))
  21177. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21178. ;; A true heading (not a plain list item), in Org-mode
  21179. ;; This means we can easily find the end by looking
  21180. ;; only for the right number of stars. Using a regexp to do
  21181. ;; this is so much faster than using a Lisp loop.
  21182. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21183. (forward-char 1)
  21184. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21185. ;; something else, do it the slow way
  21186. (while (and (not (eobp))
  21187. (or first (> (funcall outline-level) level)))
  21188. (setq first nil)
  21189. (outline-next-heading)))
  21190. (unless to-heading
  21191. (if (memq (preceding-char) '(?\n ?\^M))
  21192. (progn
  21193. ;; Go to end of line before heading
  21194. (forward-char -1)
  21195. (if (memq (preceding-char) '(?\n ?\^M))
  21196. ;; leave blank line before heading
  21197. (forward-char -1))))))
  21198. (point))
  21199. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21200. "Use Org version in org-mode, for dramatic speed-up."
  21201. (if (derived-mode-p 'org-mode)
  21202. (progn
  21203. (org-end-of-subtree nil t)
  21204. (unless (eobp) (backward-char 1)))
  21205. ad-do-it))
  21206. (defun org-end-of-meta-data-and-drawers ()
  21207. "Jump to the first text after meta data and drawers in the current entry.
  21208. This will move over empty lines, lines with planning time stamps,
  21209. clocking lines, and drawers."
  21210. (org-back-to-heading t)
  21211. (let ((end (save-excursion (outline-next-heading) (point)))
  21212. (re (concat "\\(" org-drawer-regexp "\\)"
  21213. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21214. (forward-line 1)
  21215. (while (re-search-forward re end t)
  21216. (if (not (match-end 1))
  21217. ;; empty or planning line
  21218. (forward-line 1)
  21219. ;; a drawer, find the end
  21220. (re-search-forward "^[ \t]*:END:" end 'move)
  21221. (forward-line 1)))
  21222. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21223. (point)))
  21224. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21225. "Move forward to the ARG'th subheading at same level as this one.
  21226. Stop at the first and last subheadings of a superior heading.
  21227. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21228. non-nil it will also look at invisible ones."
  21229. (interactive "p")
  21230. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21231. (if (and arg (< arg 0))
  21232. (goto-char (point-min))
  21233. (outline-next-heading))
  21234. (org-at-heading-p)
  21235. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21236. (f (if (and arg (< arg 0))
  21237. 're-search-backward
  21238. 're-search-forward))
  21239. (count (if arg (abs arg) 1))
  21240. (result (point)))
  21241. (while (and (prog1 (> count 0)
  21242. (forward-char (if (and arg (< arg 0)) -1 1)))
  21243. (funcall f org-outline-regexp-bol nil 'move))
  21244. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21245. (cond ((< l level) (setq count 0))
  21246. ((and (= l level)
  21247. (or invisible-ok
  21248. (progn
  21249. (goto-char (line-beginning-position))
  21250. (not (outline-invisible-p)))))
  21251. (setq count (1- count))
  21252. (when (eq l level)
  21253. (setq result (point)))))))
  21254. (goto-char result))
  21255. (beginning-of-line 1)))
  21256. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21257. "Move backward to the ARG'th subheading at same level as this one.
  21258. Stop at the first and last subheadings of a superior heading."
  21259. (interactive "p")
  21260. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21261. (defun org-next-block (arg &optional backward block-regexp)
  21262. "Jump to the next block.
  21263. With a prefix argument ARG, jump forward ARG many source blocks.
  21264. When BACKWARD is non-nil, jump to the previous block.
  21265. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21266. (interactive "p")
  21267. (let ((re (or block-regexp org-block-regexp))
  21268. (re-search-fn (or (and backward 're-search-backward)
  21269. 're-search-forward)))
  21270. (if (looking-at re) (forward-char 1))
  21271. (condition-case nil
  21272. (funcall re-search-fn re nil nil arg)
  21273. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21274. (goto-char (match-beginning 0)) (org-show-context)))
  21275. (defun org-previous-block (arg &optional block-regexp)
  21276. "Jump to the previous block.
  21277. With a prefix argument ARG, jump backward ARG many source blocks.
  21278. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21279. (interactive "p")
  21280. (org-next-block arg t block-regexp))
  21281. (defun org-forward-element ()
  21282. "Move forward by one element.
  21283. Move to the next element at the same level, when possible."
  21284. (interactive)
  21285. (cond ((eobp) (user-error "Cannot move further down"))
  21286. ((org-with-limited-levels (org-at-heading-p))
  21287. (let ((origin (point)))
  21288. (goto-char (org-end-of-subtree nil t))
  21289. (unless (org-with-limited-levels (org-at-heading-p))
  21290. (goto-char origin)
  21291. (user-error "Cannot move further down"))))
  21292. (t
  21293. (let* ((elem (org-element-at-point))
  21294. (end (org-element-property :end elem))
  21295. (parent (org-element-property :parent elem)))
  21296. (if (and parent (= (org-element-property :contents-end parent) end))
  21297. (goto-char (org-element-property :end parent))
  21298. (goto-char end))))))
  21299. (defun org-backward-element ()
  21300. "Move backward by one element.
  21301. Move to the previous element at the same level, when possible."
  21302. (interactive)
  21303. (cond ((bobp) (user-error "Cannot move further up"))
  21304. ((org-with-limited-levels (org-at-heading-p))
  21305. ;; At a headline, move to the previous one, if any, or stay
  21306. ;; here.
  21307. (let ((origin (point)))
  21308. (org-with-limited-levels (org-backward-heading-same-level 1))
  21309. ;; When current headline has no sibling above, move to its
  21310. ;; parent.
  21311. (when (= (point) origin)
  21312. (or (org-with-limited-levels (org-up-heading-safe))
  21313. (progn (goto-char origin)
  21314. (user-error "Cannot move further up"))))))
  21315. (t
  21316. (let* ((trail (org-element-at-point 'keep-trail))
  21317. (elem (car trail))
  21318. (prev-elem (nth 1 trail))
  21319. (beg (org-element-property :begin elem)))
  21320. (cond
  21321. ;; Move to beginning of current element if point isn't
  21322. ;; there already.
  21323. ((/= (point) beg) (goto-char beg))
  21324. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21325. ((org-before-first-heading-p) (goto-char (point-min)))
  21326. (t (org-back-to-heading)))))))
  21327. (defun org-up-element ()
  21328. "Move to upper element."
  21329. (interactive)
  21330. (if (org-with-limited-levels (org-at-heading-p))
  21331. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21332. (let* ((elem (org-element-at-point))
  21333. (parent (org-element-property :parent elem)))
  21334. (if parent (goto-char (org-element-property :begin parent))
  21335. (if (org-with-limited-levels (org-before-first-heading-p))
  21336. (user-error "No surrounding element")
  21337. (org-with-limited-levels (org-back-to-heading)))))))
  21338. (defvar org-element-greater-elements)
  21339. (defun org-down-element ()
  21340. "Move to inner element."
  21341. (interactive)
  21342. (let ((element (org-element-at-point)))
  21343. (cond
  21344. ((memq (org-element-type element) '(plain-list table))
  21345. (goto-char (org-element-property :contents-begin element))
  21346. (forward-char))
  21347. ((memq (org-element-type element) org-element-greater-elements)
  21348. ;; If contents are hidden, first disclose them.
  21349. (when (org-element-property :hiddenp element) (org-cycle))
  21350. (goto-char (or (org-element-property :contents-begin element)
  21351. (user-error "No content for this element"))))
  21352. (t (user-error "No inner element")))))
  21353. (defun org-drag-element-backward ()
  21354. "Move backward element at point."
  21355. (interactive)
  21356. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21357. (let* ((trail (org-element-at-point 'keep-trail))
  21358. (elem (car trail))
  21359. (prev-elem (nth 1 trail)))
  21360. ;; Error out if no previous element or previous element is
  21361. ;; a parent of the current one.
  21362. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21363. (user-error "Cannot drag element backward")
  21364. (let ((pos (point)))
  21365. (org-element-swap-A-B prev-elem elem)
  21366. (goto-char (+ (org-element-property :begin prev-elem)
  21367. (- pos (org-element-property :begin elem)))))))))
  21368. (defun org-drag-element-forward ()
  21369. "Move forward element at point."
  21370. (interactive)
  21371. (let* ((pos (point))
  21372. (elem (org-element-at-point)))
  21373. (when (= (point-max) (org-element-property :end elem))
  21374. (user-error "Cannot drag element forward"))
  21375. (goto-char (org-element-property :end elem))
  21376. (let ((next-elem (org-element-at-point)))
  21377. (when (or (org-element-nested-p elem next-elem)
  21378. (and (eq (org-element-type next-elem) 'headline)
  21379. (not (eq (org-element-type elem) 'headline))))
  21380. (goto-char pos)
  21381. (user-error "Cannot drag element forward"))
  21382. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21383. ;; body's length (without final blanks) and by the length of
  21384. ;; blanks between ELEM and NEXT-ELEM.
  21385. (let ((size-next (- (save-excursion
  21386. (goto-char (org-element-property :end next-elem))
  21387. (skip-chars-backward " \r\t\n")
  21388. (forward-line)
  21389. ;; Small correction if buffer doesn't end
  21390. ;; with a newline character.
  21391. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21392. (org-element-property :begin next-elem)))
  21393. (size-blank (- (org-element-property :end elem)
  21394. (save-excursion
  21395. (goto-char (org-element-property :end elem))
  21396. (skip-chars-backward " \r\t\n")
  21397. (forward-line)
  21398. (point)))))
  21399. (org-element-swap-A-B elem next-elem)
  21400. (goto-char (+ pos size-next size-blank))))))
  21401. (defun org-drag-line-forward (arg)
  21402. "Drag the line at point ARG lines forward."
  21403. (interactive "p")
  21404. (dotimes (n (abs arg))
  21405. (let ((c (current-column)))
  21406. (if (< 0 arg)
  21407. (progn
  21408. (beginning-of-line 2)
  21409. (transpose-lines 1)
  21410. (beginning-of-line 0))
  21411. (transpose-lines 1)
  21412. (beginning-of-line -1))
  21413. (org-move-to-column c))))
  21414. (defun org-drag-line-backward (arg)
  21415. "Drag the line at point ARG lines backward."
  21416. (interactive "p")
  21417. (org-drag-line-forward (- arg)))
  21418. (defun org-mark-element ()
  21419. "Put point at beginning of this element, mark at end.
  21420. Interactively, if this command is repeated or (in Transient Mark
  21421. mode) if the mark is active, it marks the next element after the
  21422. ones already marked."
  21423. (interactive)
  21424. (let (deactivate-mark)
  21425. (if (and (org-called-interactively-p 'any)
  21426. (or (and (eq last-command this-command) (mark t))
  21427. (and transient-mark-mode mark-active)))
  21428. (set-mark
  21429. (save-excursion
  21430. (goto-char (mark))
  21431. (goto-char (org-element-property :end (org-element-at-point)))))
  21432. (let ((element (org-element-at-point)))
  21433. (end-of-line)
  21434. (push-mark (org-element-property :end element) t t)
  21435. (goto-char (org-element-property :begin element))))))
  21436. (defun org-narrow-to-element ()
  21437. "Narrow buffer to current element."
  21438. (interactive)
  21439. (let ((elem (org-element-at-point)))
  21440. (cond
  21441. ((eq (car elem) 'headline)
  21442. (narrow-to-region
  21443. (org-element-property :begin elem)
  21444. (org-element-property :end elem)))
  21445. ((memq (car elem) org-element-greater-elements)
  21446. (narrow-to-region
  21447. (org-element-property :contents-begin elem)
  21448. (org-element-property :contents-end elem)))
  21449. (t
  21450. (narrow-to-region
  21451. (org-element-property :begin elem)
  21452. (org-element-property :end elem))))))
  21453. (defun org-transpose-element ()
  21454. "Transpose current and previous elements, keeping blank lines between.
  21455. Point is moved after both elements."
  21456. (interactive)
  21457. (org-skip-whitespace)
  21458. (let ((end (org-element-property :end (org-element-at-point))))
  21459. (org-drag-element-backward)
  21460. (goto-char end)))
  21461. (defun org-unindent-buffer ()
  21462. "Un-indent the visible part of the buffer.
  21463. Relative indentation (between items, inside blocks, etc.) isn't
  21464. modified."
  21465. (interactive)
  21466. (unless (eq major-mode 'org-mode)
  21467. (user-error "Cannot un-indent a buffer not in Org mode"))
  21468. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21469. unindent-tree ; For byte-compiler.
  21470. (unindent-tree
  21471. (function
  21472. (lambda (contents)
  21473. (mapc
  21474. (lambda (element)
  21475. (if (memq (org-element-type element) '(headline section))
  21476. (funcall unindent-tree (org-element-contents element))
  21477. (save-excursion
  21478. (save-restriction
  21479. (narrow-to-region
  21480. (org-element-property :begin element)
  21481. (org-element-property :end element))
  21482. (org-do-remove-indentation)))))
  21483. (reverse contents))))))
  21484. (funcall unindent-tree (org-element-contents parse-tree))))
  21485. (defun org-show-subtree ()
  21486. "Show everything after this heading at deeper levels."
  21487. (interactive)
  21488. (outline-flag-region
  21489. (point)
  21490. (save-excursion
  21491. (org-end-of-subtree t t))
  21492. nil))
  21493. (defun org-show-entry ()
  21494. "Show the body directly following this heading.
  21495. Show the heading too, if it is currently invisible."
  21496. (interactive)
  21497. (save-excursion
  21498. (condition-case nil
  21499. (progn
  21500. (org-back-to-heading t)
  21501. (outline-flag-region
  21502. (max (point-min) (1- (point)))
  21503. (save-excursion
  21504. (if (re-search-forward
  21505. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21506. (match-beginning 1)
  21507. (point-max)))
  21508. nil)
  21509. (org-cycle-hide-drawers 'children))
  21510. (error nil))))
  21511. (defun org-make-options-regexp (kwds &optional extra)
  21512. "Make a regular expression for keyword lines."
  21513. (concat
  21514. "^#\\+\\("
  21515. (mapconcat 'regexp-quote kwds "\\|")
  21516. (if extra (concat "\\|" extra))
  21517. "\\):[ \t]*\\(.*\\)"))
  21518. ;; Make isearch reveal the necessary context
  21519. (defun org-isearch-end ()
  21520. "Reveal context after isearch exits."
  21521. (when isearch-success ; only if search was successful
  21522. (if (featurep 'xemacs)
  21523. ;; Under XEmacs, the hook is run in the correct place,
  21524. ;; we directly show the context.
  21525. (org-show-context 'isearch)
  21526. ;; In Emacs the hook runs *before* restoring the overlays.
  21527. ;; So we have to use a one-time post-command-hook to do this.
  21528. ;; (Emacs 22 has a special variable, see function `org-mode')
  21529. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21530. isearch-mode-end-hook-quit)
  21531. ;; Only when the isearch was not quitted.
  21532. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21533. 'append 'local)))
  21534. (org-fix-ellipsis-at-bol)))
  21535. (defun org-isearch-post-command ()
  21536. "Remove self from hook, and show context."
  21537. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21538. (org-show-context 'isearch))
  21539. ;;;; Integration with and fixes for other packages
  21540. ;;; Imenu support
  21541. (defvar org-imenu-markers nil
  21542. "All markers currently used by Imenu.")
  21543. (make-variable-buffer-local 'org-imenu-markers)
  21544. (defun org-imenu-new-marker (&optional pos)
  21545. "Return a new marker for use by Imenu, and remember the marker."
  21546. (let ((m (make-marker)))
  21547. (move-marker m (or pos (point)))
  21548. (push m org-imenu-markers)
  21549. m))
  21550. (defun org-imenu-get-tree ()
  21551. "Produce the index for Imenu."
  21552. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21553. (setq org-imenu-markers nil)
  21554. (let* ((n org-imenu-depth)
  21555. (re (concat "^" (org-get-limited-outline-regexp)))
  21556. (subs (make-vector (1+ n) nil))
  21557. (last-level 0)
  21558. m level head0 head)
  21559. (save-excursion
  21560. (save-restriction
  21561. (widen)
  21562. (goto-char (point-max))
  21563. (while (re-search-backward re nil t)
  21564. (setq level (org-reduced-level (funcall outline-level)))
  21565. (when (and (<= level n)
  21566. (looking-at org-complex-heading-regexp)
  21567. (setq head0 (org-match-string-no-properties 4)))
  21568. (setq head (org-link-display-format head0)
  21569. m (org-imenu-new-marker))
  21570. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21571. (if (>= level last-level)
  21572. (push (cons head m) (aref subs level))
  21573. (push (cons head (aref subs (1+ level))) (aref subs level))
  21574. (loop for i from (1+ level) to n do (aset subs i nil)))
  21575. (setq last-level level)))))
  21576. (aref subs 1)))
  21577. (eval-after-load "imenu"
  21578. '(progn
  21579. (add-hook 'imenu-after-jump-hook
  21580. (lambda ()
  21581. (if (derived-mode-p 'org-mode)
  21582. (org-show-context 'org-goto))))))
  21583. (defun org-link-display-format (link)
  21584. "Replace a link with its the description.
  21585. If there is no description, use the link target."
  21586. (save-match-data
  21587. (if (string-match org-bracket-link-analytic-regexp link)
  21588. (replace-match (if (match-end 5)
  21589. (match-string 5 link)
  21590. (concat (match-string 1 link)
  21591. (match-string 3 link)))
  21592. nil t link)
  21593. link)))
  21594. (defun org-toggle-link-display ()
  21595. "Toggle the literal or descriptive display of links."
  21596. (interactive)
  21597. (if org-descriptive-links
  21598. (progn (org-remove-from-invisibility-spec '(org-link))
  21599. (org-restart-font-lock)
  21600. (setq org-descriptive-links nil))
  21601. (progn (add-to-invisibility-spec '(org-link))
  21602. (org-restart-font-lock)
  21603. (setq org-descriptive-links t))))
  21604. ;; Speedbar support
  21605. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21606. "Overlay marking the agenda restriction line in speedbar.")
  21607. (overlay-put org-speedbar-restriction-lock-overlay
  21608. 'face 'org-agenda-restriction-lock)
  21609. (overlay-put org-speedbar-restriction-lock-overlay
  21610. 'help-echo "Agendas are currently limited to this item.")
  21611. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21612. (defun org-speedbar-set-agenda-restriction ()
  21613. "Restrict future agenda commands to the location at point in speedbar.
  21614. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21615. (interactive)
  21616. (require 'org-agenda)
  21617. (let (p m tp np dir txt)
  21618. (cond
  21619. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21620. 'org-imenu t))
  21621. (setq m (get-text-property p 'org-imenu-marker))
  21622. (with-current-buffer (marker-buffer m)
  21623. (goto-char m)
  21624. (org-agenda-set-restriction-lock 'subtree)))
  21625. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21626. 'speedbar-function 'speedbar-find-file))
  21627. (setq tp (previous-single-property-change
  21628. (1+ p) 'speedbar-function)
  21629. np (next-single-property-change
  21630. tp 'speedbar-function)
  21631. dir (speedbar-line-directory)
  21632. txt (buffer-substring-no-properties (or tp (point-min))
  21633. (or np (point-max))))
  21634. (with-current-buffer (find-file-noselect
  21635. (let ((default-directory dir))
  21636. (expand-file-name txt)))
  21637. (unless (derived-mode-p 'org-mode)
  21638. (user-error "Cannot restrict to non-Org-mode file"))
  21639. (org-agenda-set-restriction-lock 'file)))
  21640. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21641. (move-overlay org-speedbar-restriction-lock-overlay
  21642. (point-at-bol) (point-at-eol))
  21643. (setq current-prefix-arg nil)
  21644. (org-agenda-maybe-redo)))
  21645. (eval-after-load "speedbar"
  21646. '(progn
  21647. (speedbar-add-supported-extension ".org")
  21648. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21649. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21650. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21651. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21652. (add-hook 'speedbar-visiting-tag-hook
  21653. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21654. ;;; Fixes and Hacks for problems with other packages
  21655. ;; Make flyspell not check words in links, to not mess up our keymap
  21656. (defvar org-element-affiliated-keywords) ; From org-element.el
  21657. (defvar org-element-block-name-alist) ; From org-element.el
  21658. (defun org-mode-flyspell-verify ()
  21659. "Don't let flyspell put overlays at active buttons, or on
  21660. {todo,all-time,additional-option-like}-keywords."
  21661. (require 'org-element) ; For `org-element-affiliated-keywords'
  21662. (let ((pos (max (1- (point)) (point-min)))
  21663. (word (thing-at-point 'word)))
  21664. (and (not (get-text-property pos 'keymap))
  21665. (not (get-text-property pos 'org-no-flyspell))
  21666. (not (member word org-todo-keywords-1))
  21667. (not (member word org-all-time-keywords))
  21668. (not (member word org-options-keywords))
  21669. (not (member word (mapcar 'car org-startup-options)))
  21670. (not (member-ignore-case word org-element-affiliated-keywords))
  21671. (not (member-ignore-case word (org-get-export-keywords)))
  21672. (not (member-ignore-case
  21673. word (mapcar 'car org-element-block-name-alist)))
  21674. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21675. (defun org-remove-flyspell-overlays-in (beg end)
  21676. "Remove flyspell overlays in region."
  21677. (and (org-bound-and-true-p flyspell-mode)
  21678. (fboundp 'flyspell-delete-region-overlays)
  21679. (flyspell-delete-region-overlays beg end))
  21680. (add-text-properties beg end '(org-no-flyspell t)))
  21681. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21682. (eval-after-load "bookmark"
  21683. '(if (boundp 'bookmark-after-jump-hook)
  21684. ;; We can use the hook
  21685. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21686. ;; Hook not available, use advice
  21687. (defadvice bookmark-jump (after org-make-visible activate)
  21688. "Make the position visible."
  21689. (org-bookmark-jump-unhide))))
  21690. ;; Make sure saveplace shows the location if it was hidden
  21691. (eval-after-load "saveplace"
  21692. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21693. "Make the position visible."
  21694. (org-bookmark-jump-unhide)))
  21695. ;; Make sure ecb shows the location if it was hidden
  21696. (eval-after-load "ecb"
  21697. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21698. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21699. (if (derived-mode-p 'org-mode)
  21700. (org-show-context))))
  21701. (defun org-bookmark-jump-unhide ()
  21702. "Unhide the current position, to show the bookmark location."
  21703. (and (derived-mode-p 'org-mode)
  21704. (or (outline-invisible-p)
  21705. (save-excursion (goto-char (max (point-min) (1- (point))))
  21706. (outline-invisible-p)))
  21707. (org-show-context 'bookmark-jump)))
  21708. ;; Make session.el ignore our circular variable
  21709. (eval-after-load "session"
  21710. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21711. ;;;; Finish up
  21712. (provide 'org)
  21713. (run-hooks 'org-load-hook)
  21714. ;;; org.el ends here