org.el 920 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2014 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-bound 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-clocktable-shift "org-clock" (dir n))
  105. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  106. (declare-function org-clock-update-time-maybe "org-clock" ())
  107. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  108. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  109. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  110. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  111. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  112. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  113. (declare-function orgtbl-mode "org-table" (&optional arg))
  114. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  115. (declare-function org-beamer-mode "ox-beamer" ())
  116. (declare-function org-table-blank-field "org-table" ())
  117. (declare-function org-table-edit-field "org-table" (arg))
  118. (declare-function org-table-insert-row "org-table" (&optional arg))
  119. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  120. (declare-function org-table-set-constants "org-table" ())
  121. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  122. (declare-function org-id-get-create "org-id" (&optional force))
  123. (declare-function org-add-archive-files "org-archive" (files))
  124. (declare-function org-id-find-id-file "org-id" (id))
  125. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  126. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  127. (declare-function org-agenda-redo "org-agenda" (&optional all))
  128. (declare-function org-table-align "org-table" ())
  129. (declare-function org-table-begin "org-table" (&optional table-type))
  130. (declare-function org-table-end "org-table" (&optional table-type))
  131. (declare-function org-table-paste-rectangle "org-table" ())
  132. (declare-function org-table-maybe-eval-formula "org-table" ())
  133. (declare-function org-table-maybe-recalculate-line "org-table" ())
  134. (declare-function org-element--parse-objects "org-element"
  135. (beg end acc restriction))
  136. (declare-function org-element-at-point "org-element" ())
  137. (declare-function org-element-cache-reset "org-element" (&optional all))
  138. (declare-function org-element-contents "org-element" (element))
  139. (declare-function org-element-context "org-element" (&optional element))
  140. (declare-function org-element-interpret-data "org-element"
  141. (data &optional parent))
  142. (declare-function org-element-map "org-element"
  143. (data types fun &optional info first-match no-recursion))
  144. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  145. (declare-function org-element-parse-buffer "org-element"
  146. (&optional granularity visible-only))
  147. (declare-function org-element-property "org-element" (property element))
  148. (declare-function org-element-put-property "org-element"
  149. (element property value))
  150. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  151. (declare-function org-element--parse-objects "org-element"
  152. (beg end acc restriction))
  153. (declare-function org-element-parse-buffer "org-element"
  154. (&optional granularity visible-only))
  155. (declare-function org-element-restriction "org-element" (element))
  156. (declare-function org-element-type "org-element" (element))
  157. (defsubst org-uniquify (list)
  158. "Non-destructively remove duplicate elements from LIST."
  159. (let ((res (copy-sequence list))) (delete-dups res)))
  160. ;; load languages based on value of `org-babel-load-languages'
  161. (defvar org-babel-load-languages)
  162. ;;;###autoload
  163. (defun org-babel-do-load-languages (sym value)
  164. "Load the languages defined in `org-babel-load-languages'."
  165. (set-default sym value)
  166. (mapc (lambda (pair)
  167. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  168. (if active
  169. (progn
  170. (require (intern (concat "ob-" lang))))
  171. (progn
  172. (funcall 'fmakunbound
  173. (intern (concat "org-babel-execute:" lang)))
  174. (funcall 'fmakunbound
  175. (intern (concat "org-babel-expand-body:" lang)))))))
  176. org-babel-load-languages))
  177. ;;;###autoload
  178. (defun org-babel-load-file (file &optional compile)
  179. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  180. This function exports the source code using `org-babel-tangle'
  181. and then loads the resulting file using `load-file'. With prefix
  182. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  183. file to byte-code before it is loaded."
  184. (interactive "fFile to load: \nP")
  185. (let* ((age (lambda (file)
  186. (float-time
  187. (time-subtract (current-time)
  188. (nth 5 (or (file-attributes (file-truename file))
  189. (file-attributes file)))))))
  190. (base-name (file-name-sans-extension file))
  191. (exported-file (concat base-name ".el")))
  192. ;; tangle if the org-mode file is newer than the elisp file
  193. (unless (and (file-exists-p exported-file)
  194. (> (funcall age file) (funcall age exported-file)))
  195. (setq exported-file
  196. (car (org-babel-tangle-file file exported-file "emacs-lisp"))))
  197. (message "%s %s"
  198. (if compile
  199. (progn (byte-compile-file exported-file 'load)
  200. "Compiled and loaded")
  201. (progn (load-file exported-file) "Loaded"))
  202. exported-file)))
  203. (defcustom org-babel-load-languages '((emacs-lisp . t))
  204. "Languages which can be evaluated in Org-mode buffers.
  205. This list can be used to load support for any of the languages
  206. below, note that each language will depend on a different set of
  207. system executables and/or Emacs modes. When a language is
  208. \"loaded\", then code blocks in that language can be evaluated
  209. with `org-babel-execute-src-block' bound by default to C-c
  210. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  211. be set to remove code block evaluation from the C-c C-c
  212. keybinding. By default only Emacs Lisp (which has no
  213. requirements) is loaded."
  214. :group 'org-babel
  215. :set 'org-babel-do-load-languages
  216. :version "24.1"
  217. :type '(alist :tag "Babel Languages"
  218. :key-type
  219. (choice
  220. (const :tag "Awk" awk)
  221. (const :tag "C" C)
  222. (const :tag "R" R)
  223. (const :tag "Asymptote" asymptote)
  224. (const :tag "Calc" calc)
  225. (const :tag "Clojure" clojure)
  226. (const :tag "CSS" css)
  227. (const :tag "Ditaa" ditaa)
  228. (const :tag "Dot" dot)
  229. (const :tag "Emacs Lisp" emacs-lisp)
  230. (const :tag "Fortran" fortran)
  231. (const :tag "Gnuplot" gnuplot)
  232. (const :tag "Haskell" haskell)
  233. (const :tag "IO" io)
  234. (const :tag "J" J)
  235. (const :tag "Java" java)
  236. (const :tag "Javascript" js)
  237. (const :tag "LaTeX" latex)
  238. (const :tag "Ledger" ledger)
  239. (const :tag "Lilypond" lilypond)
  240. (const :tag "Lisp" lisp)
  241. (const :tag "Makefile" makefile)
  242. (const :tag "Maxima" maxima)
  243. (const :tag "Matlab" matlab)
  244. (const :tag "Mscgen" mscgen)
  245. (const :tag "Ocaml" ocaml)
  246. (const :tag "Octave" octave)
  247. (const :tag "Org" org)
  248. (const :tag "Perl" perl)
  249. (const :tag "Pico Lisp" picolisp)
  250. (const :tag "PlantUML" plantuml)
  251. (const :tag "Python" python)
  252. (const :tag "Ruby" ruby)
  253. (const :tag "Sass" sass)
  254. (const :tag "Scala" scala)
  255. (const :tag "Scheme" scheme)
  256. (const :tag "Screen" screen)
  257. (const :tag "Shell Script" shell)
  258. (const :tag "Shen" shen)
  259. (const :tag "Sql" sql)
  260. (const :tag "Sqlite" sqlite)
  261. (const :tag "ebnf2ps" ebnf2ps))
  262. :value-type (boolean :tag "Activate" :value t)))
  263. ;;;; Customization variables
  264. (defcustom org-clone-delete-id nil
  265. "Remove ID property of clones of a subtree.
  266. When non-nil, clones of a subtree don't inherit the ID property.
  267. Otherwise they inherit the ID property with a new unique
  268. identifier."
  269. :type 'boolean
  270. :version "24.1"
  271. :group 'org-id)
  272. ;;; Version
  273. (org-check-version)
  274. ;;;###autoload
  275. (defun org-version (&optional here full message)
  276. "Show the org-mode version in the echo area.
  277. With prefix argument HERE, insert it at point.
  278. When FULL is non-nil, use a verbose version string.
  279. When MESSAGE is non-nil, display a message with the version."
  280. (interactive "P")
  281. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  282. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  283. (load-suffixes (list ".el"))
  284. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  285. (org-trash (or
  286. (and (fboundp 'org-release) (fboundp 'org-git-version))
  287. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  288. (load-suffixes save-load-suffixes)
  289. (org-version (org-release))
  290. (git-version (org-git-version))
  291. (version (format "Org-mode version %s (%s @ %s)"
  292. org-version
  293. git-version
  294. (if org-install-dir
  295. (if (string= org-dir org-install-dir)
  296. org-install-dir
  297. (concat "mixed installation! " org-install-dir " and " org-dir))
  298. "org-loaddefs.el can not be found!")))
  299. (version1 (if full version org-version)))
  300. (if (org-called-interactively-p 'interactive)
  301. (if here
  302. (insert version)
  303. (message version))
  304. (if message (message version1))
  305. version1)))
  306. (defconst org-version (org-version))
  307. ;;; Syntax Constants
  308. ;;;; Block
  309. (defconst org-block-regexp
  310. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  311. "Regular expression for hiding blocks.")
  312. (defconst org-dblock-start-re
  313. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  314. "Matches the start line of a dynamic block, with parameters.")
  315. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  316. "Matches the end of a dynamic block.")
  317. ;;;; Clock and Planning
  318. (defconst org-clock-string "CLOCK:"
  319. "String used as prefix for timestamps clocking work hours on an item.")
  320. (defconst org-closed-string "CLOSED:"
  321. "String used as the prefix for timestamps logging closing a TODO entry.")
  322. (defconst org-deadline-string "DEADLINE:"
  323. "String to mark deadline entries.
  324. A deadline is this string, followed by a time stamp. Should be a word,
  325. terminated by a colon. You can insert a schedule keyword and
  326. a timestamp with \\[org-deadline].")
  327. (defconst org-scheduled-string "SCHEDULED:"
  328. "String to mark scheduled TODO entries.
  329. A schedule is this string, followed by a time stamp. Should be a word,
  330. terminated by a colon. You can insert a schedule keyword and
  331. a timestamp with \\[org-schedule].")
  332. (defconst org-planning-or-clock-line-re
  333. (concat "^[ \t]*"
  334. (regexp-opt
  335. (list org-clock-string org-closed-string org-deadline-string
  336. org-scheduled-string)
  337. t))
  338. "Matches a line with planning or clock info.
  339. Matched keyword is in group 1.")
  340. ;;;; Drawer
  341. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  342. "Matches first or last line of a hidden block.
  343. Group 1 contains drawer's name or \"END\".")
  344. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  345. "Regular expression matching the first line of a property drawer.")
  346. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  347. "Regular expression matching the last line of a property drawer.")
  348. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  349. "Regular expression matching the first line of a clock drawer.")
  350. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  351. "Regular expression matching the last line of a clock drawer.")
  352. (defconst org-property-drawer-re
  353. (concat "\\(" org-property-start-re "\\)[^\000]*?\\("
  354. org-property-end-re "\\)\n?")
  355. "Matches an entire property drawer.")
  356. (defconst org-clock-drawer-re
  357. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  358. org-clock-drawer-end-re "\\)\n?")
  359. "Matches an entire clock drawer.")
  360. ;;;; Headline
  361. (defconst org-heading-keyword-regexp-format
  362. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  363. "Printf format for a regexp matching a headline with some keyword.
  364. This regexp will match the headline of any node which has the
  365. exact keyword that is put into the format. The keyword isn't in
  366. any group by default, but the stars and the body are.")
  367. (defconst org-heading-keyword-maybe-regexp-format
  368. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  369. "Printf format for a regexp matching a headline, possibly with some keyword.
  370. This regexp can match any headline with the specified keyword, or
  371. without a keyword. The keyword isn't in any group by default,
  372. but the stars and the body are.")
  373. (defconst org-archive-tag "ARCHIVE"
  374. "The tag that marks a subtree as archived.
  375. An archived subtree does not open during visibility cycling, and does
  376. not contribute to the agenda listings.")
  377. (defconst org-comment-string "COMMENT"
  378. "Entries starting with this keyword will never be exported.
  379. An entry can be toggled between COMMENT and normal with
  380. \\[org-toggle-comment].")
  381. ;;;; LaTeX Environments and Fragments
  382. (defconst org-latex-regexps
  383. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  384. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  385. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  386. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  387. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  388. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  389. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  390. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  391. "Regular expressions for matching embedded LaTeX.")
  392. ;;;; Node Property
  393. (defconst org-effort-property "Effort"
  394. "The property that is being used to keep track of effort estimates.
  395. Effort estimates given in this property need to have the format H:MM.")
  396. ;;;; Table
  397. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  398. "Detect an org-type or table-type table.")
  399. (defconst org-table-line-regexp "^[ \t]*|"
  400. "Detect an org-type table line.")
  401. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  402. "Detect an org-type table line.")
  403. (defconst org-table-hline-regexp "^[ \t]*|-"
  404. "Detect an org-type table hline.")
  405. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  406. "Detect a table-type table hline.")
  407. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  408. "Detect the first line outside a table when searching from within it.
  409. This works for both table types.")
  410. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  411. "Detect a #+TBLFM line.")
  412. ;;;; Timestamp
  413. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  414. "Regular expression for fast time stamp matching.")
  415. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  416. "Regular expression for fast time stamp matching.")
  417. (defconst org-ts-regexp0
  418. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  419. "Regular expression matching time strings for analysis.
  420. This one does not require the space after the date, so it can be used
  421. on a string that terminates immediately after the date.")
  422. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  423. "Regular expression matching time strings for analysis.")
  424. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  425. "Regular expression matching time stamps, with groups.")
  426. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  427. "Regular expression matching time stamps (also [..]), with groups.")
  428. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  429. "Regular expression matching a time stamp range.")
  430. (defconst org-tr-regexp-both
  431. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  432. "Regular expression matching a time stamp range.")
  433. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  434. org-ts-regexp "\\)?")
  435. "Regular expression matching a time stamp or time stamp range.")
  436. (defconst org-tsr-regexp-both
  437. (concat org-ts-regexp-both "\\(--?-?"
  438. org-ts-regexp-both "\\)?")
  439. "Regular expression matching a time stamp or time stamp range.
  440. The time stamps may be either active or inactive.")
  441. (defconst org-repeat-re
  442. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  443. "Regular expression for specifying repeated events.
  444. After a match, group 1 contains the repeat expression.")
  445. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  446. "Formats for `format-time-string' which are used for time stamps.")
  447. ;;; The custom variables
  448. (defgroup org nil
  449. "Outline-based notes management and organizer."
  450. :tag "Org"
  451. :group 'outlines
  452. :group 'calendar)
  453. (defcustom org-mode-hook nil
  454. "Mode hook for Org-mode, run after the mode was turned on."
  455. :group 'org
  456. :type 'hook)
  457. (defcustom org-load-hook nil
  458. "Hook that is run after org.el has been loaded."
  459. :group 'org
  460. :type 'hook)
  461. (defcustom org-log-buffer-setup-hook nil
  462. "Hook that is run after an Org log buffer is created."
  463. :group 'org
  464. :version "24.1"
  465. :type 'hook)
  466. (defvar org-modules) ; defined below
  467. (defvar org-modules-loaded nil
  468. "Have the modules been loaded already?")
  469. (defun org-load-modules-maybe (&optional force)
  470. "Load all extensions listed in `org-modules'."
  471. (when (or force (not org-modules-loaded))
  472. (mapc (lambda (ext)
  473. (condition-case nil (require ext)
  474. (error (message "Problems while trying to load feature `%s'" ext))))
  475. org-modules)
  476. (setq org-modules-loaded t)))
  477. (defun org-set-modules (var value)
  478. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  479. (set var value)
  480. (when (featurep 'org)
  481. (org-load-modules-maybe 'force)
  482. (org-element-cache-reset 'all)))
  483. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  484. "Modules that should always be loaded together with org.el.
  485. If a description starts with <C>, the file is not part of Emacs
  486. and loading it will require that you have downloaded and properly
  487. installed the Org mode distribution.
  488. You can also use this system to load external packages (i.e. neither Org
  489. core modules, nor modules from the CONTRIB directory). Just add symbols
  490. to the end of the list. If the package is called org-xyz.el, then you need
  491. to add the symbol `xyz', and the package must have a call to:
  492. \(provide 'org-xyz)
  493. For export specific modules, see also `org-export-backends'."
  494. :group 'org
  495. :set 'org-set-modules
  496. :version "24.4"
  497. :package-version '(Org . "8.0")
  498. :type
  499. '(set :greedy t
  500. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  501. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  502. (const :tag " crypt: Encryption of subtrees" org-crypt)
  503. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  504. (const :tag " docview: Links to doc-view buffers" org-docview)
  505. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  506. (const :tag " habit: Track your consistency with habits" org-habit)
  507. (const :tag " id: Global IDs for identifying entries" org-id)
  508. (const :tag " info: Links to Info nodes" org-info)
  509. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  510. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  511. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  512. (const :tag " mouse: Additional mouse support" org-mouse)
  513. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  514. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  515. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  516. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  517. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  518. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  519. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  520. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  521. (const :tag "C collector: Collect properties into tables" org-collector)
  522. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  523. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  524. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  525. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  526. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  527. (const :tag "C eval: Include command output as text" org-eval)
  528. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  529. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  530. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  531. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  532. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  533. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  534. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  535. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  536. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  537. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  538. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  539. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  540. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  541. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  542. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  543. (const :tag "C registry: A registry for Org-mode links" org-registry)
  544. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  545. (const :tag "C secretary: Team management with org-mode" org-secretary)
  546. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  547. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  548. (const :tag "C track: Keep up with Org-mode development" org-track)
  549. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  550. (const :tag "C vm: Links to VM folders/messages" org-vm)
  551. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  552. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  553. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  554. (defvar org-export--registered-backends) ; From ox.el.
  555. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  556. (declare-function org-export-backend-name "ox" (backend))
  557. (defcustom org-export-backends '(ascii html icalendar latex)
  558. "List of export back-ends that should be always available.
  559. If a description starts with <C>, the file is not part of Emacs
  560. and loading it will require that you have downloaded and properly
  561. installed the Org mode distribution.
  562. Unlike to `org-modules', libraries in this list will not be
  563. loaded along with Org, but only once the export framework is
  564. needed.
  565. This variable needs to be set before org.el is loaded. If you
  566. need to make a change while Emacs is running, use the customize
  567. interface or run the following code, where VAL stands for the new
  568. value of the variable, after updating it:
  569. \(progn
  570. \(setq org-export--registered-backends
  571. \(org-remove-if-not
  572. \(lambda (backend)
  573. \(let ((name (org-export-backend-name backend)))
  574. \(or (memq name val)
  575. \(catch 'parentp
  576. \(dolist (b val)
  577. \(and (org-export-derived-backend-p b name)
  578. \(throw 'parentp t)))))))
  579. org-export--registered-backends))
  580. \(let ((new-list (mapcar 'org-export-backend-name
  581. org-export--registered-backends)))
  582. \(dolist (backend val)
  583. \(cond
  584. \((not (load (format \"ox-%s\" backend) t t))
  585. \(message \"Problems while trying to load export back-end `%s'\"
  586. backend))
  587. \((not (memq backend new-list)) (push backend new-list))))
  588. \(set-default 'org-export-backends new-list)))
  589. Adding a back-end to this list will also pull the back-end it
  590. depends on, if any."
  591. :group 'org
  592. :group 'org-export
  593. :version "24.4"
  594. :package-version '(Org . "8.0")
  595. :initialize 'custom-initialize-set
  596. :set (lambda (var val)
  597. (if (not (featurep 'ox)) (set-default var val)
  598. ;; Any back-end not required anymore (not present in VAL and not
  599. ;; a parent of any back-end in the new value) is removed from the
  600. ;; list of registered back-ends.
  601. (setq org-export--registered-backends
  602. (org-remove-if-not
  603. (lambda (backend)
  604. (let ((name (org-export-backend-name backend)))
  605. (or (memq name val)
  606. (catch 'parentp
  607. (dolist (b val)
  608. (and (org-export-derived-backend-p b name)
  609. (throw 'parentp t)))))))
  610. org-export--registered-backends))
  611. ;; Now build NEW-LIST of both new back-ends and required
  612. ;; parents.
  613. (let ((new-list (mapcar 'org-export-backend-name
  614. org-export--registered-backends)))
  615. (dolist (backend val)
  616. (cond
  617. ((not (load (format "ox-%s" backend) t t))
  618. (message "Problems while trying to load export back-end `%s'"
  619. backend))
  620. ((not (memq backend new-list)) (push backend new-list))))
  621. ;; Set VAR to that list with fixed dependencies.
  622. (set-default var new-list))))
  623. :type '(set :greedy t
  624. (const :tag " ascii Export buffer to ASCII format" ascii)
  625. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  626. (const :tag " html Export buffer to HTML format" html)
  627. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  628. (const :tag " latex Export buffer to LaTeX format" latex)
  629. (const :tag " man Export buffer to MAN format" man)
  630. (const :tag " md Export buffer to Markdown format" md)
  631. (const :tag " odt Export buffer to ODT format" odt)
  632. (const :tag " org Export buffer to Org format" org)
  633. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  634. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  635. (const :tag "C deck Export buffer to deck.js presentations" deck)
  636. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  637. (const :tag "C groff Export buffer to Groff format" groff)
  638. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  639. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  640. (const :tag "C s5 Export buffer to s5 presentations" s5)
  641. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  642. (eval-after-load 'ox
  643. '(mapc
  644. (lambda (backend)
  645. (condition-case nil (require (intern (format "ox-%s" backend)))
  646. (error (message "Problems while trying to load export back-end `%s'"
  647. backend))))
  648. org-export-backends))
  649. (defcustom org-support-shift-select nil
  650. "Non-nil means make shift-cursor commands select text when possible.
  651. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  652. start selecting a region, or enlarge regions started in this way.
  653. In Org-mode, in special contexts, these same keys are used for
  654. other purposes, important enough to compete with shift selection.
  655. Org tries to balance these needs by supporting `shift-select-mode'
  656. outside these special contexts, under control of this variable.
  657. The default of this variable is nil, to avoid confusing behavior. Shifted
  658. cursor keys will then execute Org commands in the following contexts:
  659. - on a headline, changing TODO state (left/right) and priority (up/down)
  660. - on a time stamp, changing the time
  661. - in a plain list item, changing the bullet type
  662. - in a property definition line, switching between allowed values
  663. - in the BEGIN line of a clock table (changing the time block).
  664. Outside these contexts, the commands will throw an error.
  665. When this variable is t and the cursor is not in a special
  666. context, Org-mode will support shift-selection for making and
  667. enlarging regions. To make this more effective, the bullet
  668. cycling will no longer happen anywhere in an item line, but only
  669. if the cursor is exactly on the bullet.
  670. If you set this variable to the symbol `always', then the keys
  671. will not be special in headlines, property lines, and item lines,
  672. to make shift selection work there as well. If this is what you
  673. want, you can use the following alternative commands: `C-c C-t'
  674. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  675. can be used to switch TODO sets, `C-c -' to cycle item bullet
  676. types, and properties can be edited by hand or in column view.
  677. However, when the cursor is on a timestamp, shift-cursor commands
  678. will still edit the time stamp - this is just too good to give up.
  679. XEmacs user should have this variable set to nil, because
  680. `shift-select-mode' is in Emacs 23 or later only."
  681. :group 'org
  682. :type '(choice
  683. (const :tag "Never" nil)
  684. (const :tag "When outside special context" t)
  685. (const :tag "Everywhere except timestamps" always)))
  686. (defcustom org-loop-over-headlines-in-active-region nil
  687. "Shall some commands act upon headlines in the active region?
  688. When set to `t', some commands will be performed in all headlines
  689. within the active region.
  690. When set to `start-level', some commands will be performed in all
  691. headlines within the active region, provided that these headlines
  692. are of the same level than the first one.
  693. When set to a string, those commands will be performed on the
  694. matching headlines within the active region. Such string must be
  695. a tags/property/todo match as it is used in the agenda tags view.
  696. The list of commands is: `org-schedule', `org-deadline',
  697. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  698. `org-archive-to-archive-sibling'. The archiving commands skip
  699. already archived entries."
  700. :type '(choice (const :tag "Don't loop" nil)
  701. (const :tag "All headlines in active region" t)
  702. (const :tag "In active region, headlines at the same level than the first one" start-level)
  703. (string :tag "Tags/Property/Todo matcher"))
  704. :version "24.1"
  705. :group 'org-todo
  706. :group 'org-archive)
  707. (defgroup org-startup nil
  708. "Options concerning startup of Org-mode."
  709. :tag "Org Startup"
  710. :group 'org)
  711. (defcustom org-startup-folded t
  712. "Non-nil means entering Org-mode will switch to OVERVIEW.
  713. This can also be configured on a per-file basis by adding one of
  714. the following lines anywhere in the buffer:
  715. #+STARTUP: fold (or `overview', this is equivalent)
  716. #+STARTUP: nofold (or `showall', this is equivalent)
  717. #+STARTUP: content
  718. #+STARTUP: showeverything
  719. By default, this option is ignored when Org opens agenda files
  720. for the first time. If you want the agenda to honor the startup
  721. option, set `org-agenda-inhibit-startup' to nil."
  722. :group 'org-startup
  723. :type '(choice
  724. (const :tag "nofold: show all" nil)
  725. (const :tag "fold: overview" t)
  726. (const :tag "content: all headlines" content)
  727. (const :tag "show everything, even drawers" showeverything)))
  728. (defcustom org-startup-truncated t
  729. "Non-nil means entering Org-mode will set `truncate-lines'.
  730. This is useful since some lines containing links can be very long and
  731. uninteresting. Also tables look terrible when wrapped."
  732. :group 'org-startup
  733. :type 'boolean)
  734. (defcustom org-startup-indented nil
  735. "Non-nil means turn on `org-indent-mode' on startup.
  736. This can also be configured on a per-file basis by adding one of
  737. the following lines anywhere in the buffer:
  738. #+STARTUP: indent
  739. #+STARTUP: noindent"
  740. :group 'org-structure
  741. :type '(choice
  742. (const :tag "Not" nil)
  743. (const :tag "Globally (slow on startup in large files)" t)))
  744. (defcustom org-use-sub-superscripts t
  745. "Non-nil means interpret \"_\" and \"^\" for display.
  746. If you want to control how Org exports those characters, see
  747. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  748. used to be an alias for `org-export-with-sub-superscripts' in
  749. Org <8.0, it is not anymore.
  750. When this option is turned on, you can use TeX-like syntax for
  751. sub- and superscripts within the buffer. Several characters after
  752. \"_\" or \"^\" will be considered as a single item - so grouping
  753. with {} is normally not needed. For example, the following things
  754. will be parsed as single sub- or superscripts:
  755. 10^24 or 10^tau several digits will be considered 1 item.
  756. 10^-12 or 10^-tau a leading sign with digits or a word
  757. x^2-y^3 will be read as x^2 - y^3, because items are
  758. terminated by almost any nonword/nondigit char.
  759. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  760. Still, ambiguity is possible. So when in doubt, use {} to enclose
  761. the sub/superscript. If you set this variable to the symbol `{}',
  762. the braces are *required* in order to trigger interpretations as
  763. sub/superscript. This can be helpful in documents that need \"_\"
  764. frequently in plain text."
  765. :group 'org-startup
  766. :version "24.4"
  767. :package-version '(Org . "8.0")
  768. :type '(choice
  769. (const :tag "Always interpret" t)
  770. (const :tag "Only with braces" {})
  771. (const :tag "Never interpret" nil)))
  772. (defcustom org-startup-with-beamer-mode nil
  773. "Non-nil means turn on `org-beamer-mode' on startup.
  774. This can also be configured on a per-file basis by adding one of
  775. the following lines anywhere in the buffer:
  776. #+STARTUP: beamer"
  777. :group 'org-startup
  778. :version "24.1"
  779. :type 'boolean)
  780. (defcustom org-startup-align-all-tables nil
  781. "Non-nil means align all tables when visiting a file.
  782. This is useful when the column width in tables is forced with <N> cookies
  783. in table fields. Such tables will look correct only after the first re-align.
  784. This can also be configured on a per-file basis by adding one of
  785. the following lines anywhere in the buffer:
  786. #+STARTUP: align
  787. #+STARTUP: noalign"
  788. :group 'org-startup
  789. :type 'boolean)
  790. (defcustom org-startup-with-inline-images nil
  791. "Non-nil means show inline images when loading a new Org file.
  792. This can also be configured on a per-file basis by adding one of
  793. the following lines anywhere in the buffer:
  794. #+STARTUP: inlineimages
  795. #+STARTUP: noinlineimages"
  796. :group 'org-startup
  797. :version "24.1"
  798. :type 'boolean)
  799. (defcustom org-startup-with-latex-preview nil
  800. "Non-nil means preview LaTeX fragments when loading a new Org file.
  801. This can also be configured on a per-file basis by adding one of
  802. the following lines anywhere in the buffer:
  803. #+STARTUP: latexpreview
  804. #+STARTUP: nolatexpreview"
  805. :group 'org-startup
  806. :version "24.4"
  807. :package-version '(Org . "8.0")
  808. :type 'boolean)
  809. (defcustom org-insert-mode-line-in-empty-file nil
  810. "Non-nil means insert the first line setting Org-mode in empty files.
  811. When the function `org-mode' is called interactively in an empty file, this
  812. normally means that the file name does not automatically trigger Org-mode.
  813. To ensure that the file will always be in Org-mode in the future, a
  814. line enforcing Org-mode will be inserted into the buffer, if this option
  815. has been set."
  816. :group 'org-startup
  817. :type 'boolean)
  818. (defcustom org-replace-disputed-keys nil
  819. "Non-nil means use alternative key bindings for some keys.
  820. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  821. These keys are also used by other packages like shift-selection-mode'
  822. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  823. If you want to use Org-mode together with one of these other modes,
  824. or more generally if you would like to move some Org-mode commands to
  825. other keys, set this variable and configure the keys with the variable
  826. `org-disputed-keys'.
  827. This option is only relevant at load-time of Org-mode, and must be set
  828. *before* org.el is loaded. Changing it requires a restart of Emacs to
  829. become effective."
  830. :group 'org-startup
  831. :type 'boolean)
  832. (defcustom org-use-extra-keys nil
  833. "Non-nil means use extra key sequence definitions for certain commands.
  834. This happens automatically if you run XEmacs or if `window-system'
  835. is nil. This variable lets you do the same manually. You must
  836. set it before loading org.
  837. Example: on Carbon Emacs 22 running graphically, with an external
  838. keyboard on a Powerbook, the default way of setting M-left might
  839. not work for either Alt or ESC. Setting this variable will make
  840. it work for ESC."
  841. :group 'org-startup
  842. :type 'boolean)
  843. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  844. (defcustom org-disputed-keys
  845. '(([(shift up)] . [(meta p)])
  846. ([(shift down)] . [(meta n)])
  847. ([(shift left)] . [(meta -)])
  848. ([(shift right)] . [(meta +)])
  849. ([(control shift right)] . [(meta shift +)])
  850. ([(control shift left)] . [(meta shift -)]))
  851. "Keys for which Org-mode and other modes compete.
  852. This is an alist, cars are the default keys, second element specifies
  853. the alternative to use when `org-replace-disputed-keys' is t.
  854. Keys can be specified in any syntax supported by `define-key'.
  855. The value of this option takes effect only at Org-mode's startup,
  856. therefore you'll have to restart Emacs to apply it after changing."
  857. :group 'org-startup
  858. :type 'alist)
  859. (defun org-key (key)
  860. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  861. Or return the original if not disputed.
  862. Also apply the translations defined in `org-xemacs-key-equivalents'."
  863. (when org-replace-disputed-keys
  864. (let* ((nkey (key-description key))
  865. (x (org-find-if (lambda (x)
  866. (equal (key-description (car x)) nkey))
  867. org-disputed-keys)))
  868. (setq key (if x (cdr x) key))))
  869. (when (featurep 'xemacs)
  870. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  871. key)
  872. (defun org-find-if (predicate seq)
  873. (catch 'exit
  874. (while seq
  875. (if (funcall predicate (car seq))
  876. (throw 'exit (car seq))
  877. (pop seq)))))
  878. (defun org-defkey (keymap key def)
  879. "Define a key, possibly translated, as returned by `org-key'."
  880. (define-key keymap (org-key key) def))
  881. (defcustom org-ellipsis nil
  882. "The ellipsis to use in the Org-mode outline.
  883. When nil, just use the standard three dots.
  884. When a string, use that string instead.
  885. When a face, use the standard 3 dots, but with the specified face.
  886. The change affects only Org-mode (which will then use its own display table).
  887. Changing this requires executing `M-x org-mode RET' in a buffer to become
  888. effective."
  889. :group 'org-startup
  890. :type '(choice (const :tag "Default" nil)
  891. (face :tag "Face" :value org-warning)
  892. (string :tag "String" :value "...#")))
  893. (defvar org-display-table nil
  894. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  895. (defgroup org-keywords nil
  896. "Keywords in Org-mode."
  897. :tag "Org Keywords"
  898. :group 'org)
  899. (defcustom org-closed-keep-when-no-todo nil
  900. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  901. :group 'org-todo
  902. :group 'org-keywords
  903. :version "24.4"
  904. :package-version '(Org . "8.0")
  905. :type 'boolean)
  906. (defgroup org-structure nil
  907. "Options concerning the general structure of Org-mode files."
  908. :tag "Org Structure"
  909. :group 'org)
  910. (defgroup org-reveal-location nil
  911. "Options about how to make context of a location visible."
  912. :tag "Org Reveal Location"
  913. :group 'org-structure)
  914. (defconst org-context-choice
  915. '(choice
  916. (const :tag "Always" t)
  917. (const :tag "Never" nil)
  918. (repeat :greedy t :tag "Individual contexts"
  919. (cons
  920. (choice :tag "Context"
  921. (const agenda)
  922. (const org-goto)
  923. (const occur-tree)
  924. (const tags-tree)
  925. (const link-search)
  926. (const mark-goto)
  927. (const bookmark-jump)
  928. (const isearch)
  929. (const default))
  930. (boolean))))
  931. "Contexts for the reveal options.")
  932. (defcustom org-show-hierarchy-above '((default . t))
  933. "Non-nil means show full hierarchy when revealing a location.
  934. Org-mode often shows locations in an org-mode file which might have
  935. been invisible before. When this is set, the hierarchy of headings
  936. above the exposed location is shown.
  937. Turning this off for example for sparse trees makes them very compact.
  938. Instead of t, this can also be an alist specifying this option for different
  939. contexts. Valid contexts are
  940. agenda when exposing an entry from the agenda
  941. org-goto when using the command `org-goto' on key C-c C-j
  942. occur-tree when using the command `org-occur' on key C-c /
  943. tags-tree when constructing a sparse tree based on tags matches
  944. link-search when exposing search matches associated with a link
  945. mark-goto when exposing the jump goal of a mark
  946. bookmark-jump when exposing a bookmark location
  947. isearch when exiting from an incremental search
  948. default default for all contexts not set explicitly"
  949. :group 'org-reveal-location
  950. :type org-context-choice)
  951. (defcustom org-show-following-heading '((default . nil))
  952. "Non-nil means show following heading when revealing a location.
  953. Org-mode often shows locations in an org-mode file which might have
  954. been invisible before. When this is set, the heading following the
  955. match is shown.
  956. Turning this off for example for sparse trees makes them very compact,
  957. but makes it harder to edit the location of the match. In such a case,
  958. use the command \\[org-reveal] to show more context.
  959. Instead of t, this can also be an alist specifying this option for different
  960. contexts. See `org-show-hierarchy-above' for valid contexts."
  961. :group 'org-reveal-location
  962. :type org-context-choice)
  963. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  964. "Non-nil means show all sibling heading when revealing a location.
  965. Org-mode often shows locations in an org-mode file which might have
  966. been invisible before. When this is set, the sibling of the current entry
  967. heading are all made visible. If `org-show-hierarchy-above' is t,
  968. the same happens on each level of the hierarchy above the current entry.
  969. By default this is on for the isearch context, off for all other contexts.
  970. Turning this off for example for sparse trees makes them very compact,
  971. but makes it harder to edit the location of the match. In such a case,
  972. use the command \\[org-reveal] to show more context.
  973. Instead of t, this can also be an alist specifying this option for different
  974. contexts. See `org-show-hierarchy-above' for valid contexts."
  975. :group 'org-reveal-location
  976. :type org-context-choice
  977. :version "24.4"
  978. :package-version '(Org . "8.0"))
  979. (defcustom org-show-entry-below '((default . nil))
  980. "Non-nil means show the entry below a headline when revealing a location.
  981. Org-mode often shows locations in an org-mode file which might have
  982. been invisible before. When this is set, the text below the headline that is
  983. exposed is also shown.
  984. By default this is off for all contexts.
  985. Instead of t, this can also be an alist specifying this option for different
  986. contexts. See `org-show-hierarchy-above' for valid contexts."
  987. :group 'org-reveal-location
  988. :type org-context-choice)
  989. (defcustom org-indirect-buffer-display 'other-window
  990. "How should indirect tree buffers be displayed?
  991. This applies to indirect buffers created with the commands
  992. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  993. Valid values are:
  994. current-window Display in the current window
  995. other-window Just display in another window.
  996. dedicated-frame Create one new frame, and re-use it each time.
  997. new-frame Make a new frame each time. Note that in this case
  998. previously-made indirect buffers are kept, and you need to
  999. kill these buffers yourself."
  1000. :group 'org-structure
  1001. :group 'org-agenda-windows
  1002. :type '(choice
  1003. (const :tag "In current window" current-window)
  1004. (const :tag "In current frame, other window" other-window)
  1005. (const :tag "Each time a new frame" new-frame)
  1006. (const :tag "One dedicated frame" dedicated-frame)))
  1007. (defcustom org-use-speed-commands nil
  1008. "Non-nil means activate single letter commands at beginning of a headline.
  1009. This may also be a function to test for appropriate locations where speed
  1010. commands should be active.
  1011. For example, to activate speed commands when the point is on any
  1012. star at the beginning of the headline, you can do this:
  1013. (setq org-use-speed-commands
  1014. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1015. :group 'org-structure
  1016. :type '(choice
  1017. (const :tag "Never" nil)
  1018. (const :tag "At beginning of headline stars" t)
  1019. (function)))
  1020. (defcustom org-speed-commands-user nil
  1021. "Alist of additional speed commands.
  1022. This list will be checked before `org-speed-commands-default'
  1023. when the variable `org-use-speed-commands' is non-nil
  1024. and when the cursor is at the beginning of a headline.
  1025. The car if each entry is a string with a single letter, which must
  1026. be assigned to `self-insert-command' in the global map.
  1027. The cdr is either a command to be called interactively, a function
  1028. to be called, or a form to be evaluated.
  1029. An entry that is just a list with a single string will be interpreted
  1030. as a descriptive headline that will be added when listing the speed
  1031. commands in the Help buffer using the `?' speed command."
  1032. :group 'org-structure
  1033. :type '(repeat :value ("k" . ignore)
  1034. (choice :value ("k" . ignore)
  1035. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1036. (cons :tag "Letter and Command"
  1037. (string :tag "Command letter")
  1038. (choice
  1039. (function)
  1040. (sexp))))))
  1041. (defcustom org-bookmark-names-plist
  1042. '(:last-capture "org-capture-last-stored"
  1043. :last-refile "org-refile-last-stored"
  1044. :last-capture-marker "org-capture-last-stored-marker")
  1045. "Names for bookmarks automatically set by some Org commands.
  1046. This can provide strings as names for a number of bookmarks Org sets
  1047. automatically. The following keys are currently implemented:
  1048. :last-capture
  1049. :last-capture-marker
  1050. :last-refile
  1051. When a key does not show up in the property list, the corresponding bookmark
  1052. is not set."
  1053. :group 'org-structure
  1054. :type 'plist)
  1055. (defgroup org-cycle nil
  1056. "Options concerning visibility cycling in Org-mode."
  1057. :tag "Org Cycle"
  1058. :group 'org-structure)
  1059. (defcustom org-cycle-skip-children-state-if-no-children t
  1060. "Non-nil means skip CHILDREN state in entries that don't have any."
  1061. :group 'org-cycle
  1062. :type 'boolean)
  1063. (defcustom org-cycle-max-level nil
  1064. "Maximum level which should still be subject to visibility cycling.
  1065. Levels higher than this will, for cycling, be treated as text, not a headline.
  1066. When `org-odd-levels-only' is set, a value of N in this variable actually
  1067. means 2N-1 stars as the limiting headline.
  1068. When nil, cycle all levels.
  1069. Note that the limiting level of cycling is also influenced by
  1070. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1071. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1072. than its value."
  1073. :group 'org-cycle
  1074. :type '(choice
  1075. (const :tag "No limit" nil)
  1076. (integer :tag "Maximum level")))
  1077. (defcustom org-hide-block-startup nil
  1078. "Non-nil means entering Org-mode will fold all blocks.
  1079. This can also be set in on a per-file basis with
  1080. #+STARTUP: hideblocks
  1081. #+STARTUP: showblocks"
  1082. :group 'org-startup
  1083. :group 'org-cycle
  1084. :type 'boolean)
  1085. (defcustom org-cycle-global-at-bob nil
  1086. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1087. This makes it possible to do global cycling without having to use S-TAB or
  1088. \\[universal-argument] TAB. For this special case to work, the first line
  1089. of the buffer must not be a headline -- it may be empty or some other text.
  1090. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1091. sure the cursor stays at the beginning of the buffer. When this option is
  1092. nil, don't do anything special at the beginning of the buffer."
  1093. :group 'org-cycle
  1094. :type 'boolean)
  1095. (defcustom org-cycle-level-after-item/entry-creation t
  1096. "Non-nil means cycle entry level or item indentation in new empty entries.
  1097. When the cursor is at the end of an empty headline, i.e., with only stars
  1098. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1099. and then all possible ancestor states, before returning to the original state.
  1100. This makes data entry extremely fast: M-RET to create a new headline,
  1101. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1102. When the cursor is at the end of an empty plain list item, one TAB will
  1103. make it a subitem, two or more tabs will back up to make this an item
  1104. higher up in the item hierarchy."
  1105. :group 'org-cycle
  1106. :type 'boolean)
  1107. (defcustom org-cycle-emulate-tab t
  1108. "Where should `org-cycle' emulate TAB.
  1109. nil Never
  1110. white Only in completely white lines
  1111. whitestart Only at the beginning of lines, before the first non-white char
  1112. t Everywhere except in headlines
  1113. exc-hl-bol Everywhere except at the start of a headline
  1114. If TAB is used in a place where it does not emulate TAB, the current subtree
  1115. visibility is cycled."
  1116. :group 'org-cycle
  1117. :type '(choice (const :tag "Never" nil)
  1118. (const :tag "Only in completely white lines" white)
  1119. (const :tag "Before first char in a line" whitestart)
  1120. (const :tag "Everywhere except in headlines" t)
  1121. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1122. (defcustom org-cycle-separator-lines 2
  1123. "Number of empty lines needed to keep an empty line between collapsed trees.
  1124. If you leave an empty line between the end of a subtree and the following
  1125. headline, this empty line is hidden when the subtree is folded.
  1126. Org-mode will leave (exactly) one empty line visible if the number of
  1127. empty lines is equal or larger to the number given in this variable.
  1128. So the default 2 means at least 2 empty lines after the end of a subtree
  1129. are needed to produce free space between a collapsed subtree and the
  1130. following headline.
  1131. If the number is negative, and the number of empty lines is at least -N,
  1132. all empty lines are shown.
  1133. Special case: when 0, never leave empty lines in collapsed view."
  1134. :group 'org-cycle
  1135. :type 'integer)
  1136. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1137. (defcustom org-pre-cycle-hook nil
  1138. "Hook that is run before visibility cycling is happening.
  1139. The function(s) in this hook must accept a single argument which indicates
  1140. the new state that will be set right after running this hook. The
  1141. argument is a symbol. Before a global state change, it can have the values
  1142. `overview', `content', or `all'. Before a local state change, it can have
  1143. the values `folded', `children', or `subtree'."
  1144. :group 'org-cycle
  1145. :type 'hook)
  1146. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1147. org-cycle-hide-drawers
  1148. org-cycle-hide-inline-tasks
  1149. org-cycle-show-empty-lines
  1150. org-optimize-window-after-visibility-change)
  1151. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1152. The function(s) in this hook must accept a single argument which indicates
  1153. the new state that was set by the most recent `org-cycle' command. The
  1154. argument is a symbol. After a global state change, it can have the values
  1155. `overview', `contents', or `all'. After a local state change, it can have
  1156. the values `folded', `children', or `subtree'."
  1157. :group 'org-cycle
  1158. :type 'hook)
  1159. (defgroup org-edit-structure nil
  1160. "Options concerning structure editing in Org-mode."
  1161. :tag "Org Edit Structure"
  1162. :group 'org-structure)
  1163. (defcustom org-odd-levels-only nil
  1164. "Non-nil means skip even levels and only use odd levels for the outline.
  1165. This has the effect that two stars are being added/taken away in
  1166. promotion/demotion commands. It also influences how levels are
  1167. handled by the exporters.
  1168. Changing it requires restart of `font-lock-mode' to become effective
  1169. for fontification also in regions already fontified.
  1170. You may also set this on a per-file basis by adding one of the following
  1171. lines to the buffer:
  1172. #+STARTUP: odd
  1173. #+STARTUP: oddeven"
  1174. :group 'org-edit-structure
  1175. :group 'org-appearance
  1176. :type 'boolean)
  1177. (defcustom org-adapt-indentation t
  1178. "Non-nil means adapt indentation to outline node level.
  1179. When this variable is set, Org assumes that you write outlines by
  1180. indenting text in each node to align with the headline (after the stars).
  1181. The following issues are influenced by this variable:
  1182. - When this is set and the *entire* text in an entry is indented, the
  1183. indentation is increased by one space in a demotion command, and
  1184. decreased by one in a promotion command. If any line in the entry
  1185. body starts with text at column 0, indentation is not changed at all.
  1186. - Property drawers and planning information is inserted indented when
  1187. this variable s set. When nil, they will not be indented.
  1188. - TAB indents a line relative to context. The lines below a headline
  1189. will be indented when this variable is set.
  1190. Note that this is all about true indentation, by adding and removing
  1191. space characters. See also `org-indent.el' which does level-dependent
  1192. indentation in a virtual way, i.e. at display time in Emacs."
  1193. :group 'org-edit-structure
  1194. :type 'boolean)
  1195. (defcustom org-special-ctrl-a/e nil
  1196. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1197. When t, `C-a' will bring back the cursor to the beginning of the
  1198. headline text, i.e. after the stars and after a possible TODO
  1199. keyword. In an item, this will be the position after bullet and
  1200. check-box, if any. When the cursor is already at that position,
  1201. another `C-a' will bring it to the beginning of the line.
  1202. `C-e' will jump to the end of the headline, ignoring the presence
  1203. of tags in the headline. A second `C-e' will then jump to the
  1204. true end of the line, after any tags. This also means that, when
  1205. this variable is non-nil, `C-e' also will never jump beyond the
  1206. end of the heading of a folded section, i.e. not after the
  1207. ellipses.
  1208. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1209. normally, going to the true line boundary first. Only a directly
  1210. following, identical keypress will bring the cursor to the
  1211. special positions.
  1212. This may also be a cons cell where the behavior for `C-a' and
  1213. `C-e' is set separately."
  1214. :group 'org-edit-structure
  1215. :type '(choice
  1216. (const :tag "off" nil)
  1217. (const :tag "on: after stars/bullet and before tags first" t)
  1218. (const :tag "reversed: true line boundary first" reversed)
  1219. (cons :tag "Set C-a and C-e separately"
  1220. (choice :tag "Special C-a"
  1221. (const :tag "off" nil)
  1222. (const :tag "on: after stars/bullet first" t)
  1223. (const :tag "reversed: before stars/bullet first" reversed))
  1224. (choice :tag "Special C-e"
  1225. (const :tag "off" nil)
  1226. (const :tag "on: before tags first" t)
  1227. (const :tag "reversed: after tags first" reversed)))))
  1228. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1229. (defcustom org-special-ctrl-k nil
  1230. "Non-nil means `C-k' will behave specially in headlines.
  1231. When nil, `C-k' will call the default `kill-line' command.
  1232. When t, the following will happen while the cursor is in the headline:
  1233. - When the cursor is at the beginning of a headline, kill the entire
  1234. line and possible the folded subtree below the line.
  1235. - When in the middle of the headline text, kill the headline up to the tags.
  1236. - When after the headline text, kill the tags."
  1237. :group 'org-edit-structure
  1238. :type 'boolean)
  1239. (defcustom org-ctrl-k-protect-subtree nil
  1240. "Non-nil means, do not delete a hidden subtree with C-k.
  1241. When set to the symbol `error', simply throw an error when C-k is
  1242. used to kill (part-of) a headline that has hidden text behind it.
  1243. Any other non-nil value will result in a query to the user, if it is
  1244. OK to kill that hidden subtree. When nil, kill without remorse."
  1245. :group 'org-edit-structure
  1246. :version "24.1"
  1247. :type '(choice
  1248. (const :tag "Do not protect hidden subtrees" nil)
  1249. (const :tag "Protect hidden subtrees with a security query" t)
  1250. (const :tag "Never kill a hidden subtree with C-k" error)))
  1251. (defcustom org-special-ctrl-o t
  1252. "Non-nil means, make `C-o' insert a row in tables."
  1253. :group 'org-edit-structure
  1254. :type 'boolean)
  1255. (defcustom org-catch-invisible-edits nil
  1256. "Check if in invisible region before inserting or deleting a character.
  1257. Valid values are:
  1258. nil Do not check, so just do invisible edits.
  1259. error Throw an error and do nothing.
  1260. show Make point visible, and do the requested edit.
  1261. show-and-error Make point visible, then throw an error and abort the edit.
  1262. smart Make point visible, and do insertion/deletion if it is
  1263. adjacent to visible text and the change feels predictable.
  1264. Never delete a previously invisible character or add in the
  1265. middle or right after an invisible region. Basically, this
  1266. allows insertion and backward-delete right before ellipses.
  1267. FIXME: maybe in this case we should not even show?"
  1268. :group 'org-edit-structure
  1269. :version "24.1"
  1270. :type '(choice
  1271. (const :tag "Do not check" nil)
  1272. (const :tag "Throw error when trying to edit" error)
  1273. (const :tag "Unhide, but do not do the edit" show-and-error)
  1274. (const :tag "Show invisible part and do the edit" show)
  1275. (const :tag "Be smart and do the right thing" smart)))
  1276. (defcustom org-yank-folded-subtrees t
  1277. "Non-nil means when yanking subtrees, fold them.
  1278. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1279. it starts with a heading and all other headings in it are either children
  1280. or siblings, then fold all the subtrees. However, do this only if no
  1281. text after the yank would be swallowed into a folded tree by this action."
  1282. :group 'org-edit-structure
  1283. :type 'boolean)
  1284. (defcustom org-yank-adjusted-subtrees nil
  1285. "Non-nil means when yanking subtrees, adjust the level.
  1286. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1287. this function for details."
  1288. :group 'org-edit-structure
  1289. :type 'boolean)
  1290. (defcustom org-M-RET-may-split-line '((default . t))
  1291. "Non-nil means M-RET will split the line at the cursor position.
  1292. When nil, it will go to the end of the line before making a
  1293. new line.
  1294. You may also set this option in a different way for different
  1295. contexts. Valid contexts are:
  1296. headline when creating a new headline
  1297. item when creating a new item
  1298. table in a table field
  1299. default the value to be used for all contexts not explicitly
  1300. customized"
  1301. :group 'org-structure
  1302. :group 'org-table
  1303. :type '(choice
  1304. (const :tag "Always" t)
  1305. (const :tag "Never" nil)
  1306. (repeat :greedy t :tag "Individual contexts"
  1307. (cons
  1308. (choice :tag "Context"
  1309. (const headline)
  1310. (const item)
  1311. (const table)
  1312. (const default))
  1313. (boolean)))))
  1314. (defcustom org-insert-heading-respect-content nil
  1315. "Non-nil means insert new headings after the current subtree.
  1316. When nil, the new heading is created directly after the current line.
  1317. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1318. this variable on for the duration of the command."
  1319. :group 'org-structure
  1320. :type 'boolean)
  1321. (defcustom org-blank-before-new-entry '((heading . auto)
  1322. (plain-list-item . auto))
  1323. "Should `org-insert-heading' leave a blank line before new heading/item?
  1324. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1325. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1326. which case Org will look at the surrounding headings/items and try to
  1327. make an intelligent decision whether to insert a blank line or not.
  1328. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1329. the setting here is ignored and no empty line is inserted to avoid breaking
  1330. the list structure."
  1331. :group 'org-edit-structure
  1332. :type '(list
  1333. (cons (const heading)
  1334. (choice (const :tag "Never" nil)
  1335. (const :tag "Always" t)
  1336. (const :tag "Auto" auto)))
  1337. (cons (const plain-list-item)
  1338. (choice (const :tag "Never" nil)
  1339. (const :tag "Always" t)
  1340. (const :tag "Auto" auto)))))
  1341. (defcustom org-insert-heading-hook nil
  1342. "Hook being run after inserting a new heading."
  1343. :group 'org-edit-structure
  1344. :type 'hook)
  1345. (defcustom org-enable-fixed-width-editor t
  1346. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1347. This currently only means they are never auto-wrapped.
  1348. When nil, such lines will be treated like ordinary lines."
  1349. :group 'org-edit-structure
  1350. :type 'boolean)
  1351. (defcustom org-goto-auto-isearch t
  1352. "Non-nil means typing characters in `org-goto' starts incremental search.
  1353. When nil, you can use these keybindings to navigate the buffer:
  1354. q Quit the org-goto interface
  1355. n Go to the next visible heading
  1356. p Go to the previous visible heading
  1357. f Go one heading forward on same level
  1358. b Go one heading backward on same level
  1359. u Go one heading up"
  1360. :group 'org-edit-structure
  1361. :type 'boolean)
  1362. (defgroup org-sparse-trees nil
  1363. "Options concerning sparse trees in Org-mode."
  1364. :tag "Org Sparse Trees"
  1365. :group 'org-structure)
  1366. (defcustom org-highlight-sparse-tree-matches t
  1367. "Non-nil means highlight all matches that define a sparse tree.
  1368. The highlights will automatically disappear the next time the buffer is
  1369. changed by an edit command."
  1370. :group 'org-sparse-trees
  1371. :type 'boolean)
  1372. (defcustom org-remove-highlights-with-change t
  1373. "Non-nil means any change to the buffer will remove temporary highlights.
  1374. Such highlights are created by `org-occur' and `org-clock-display'.
  1375. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1376. The highlights created by `org-preview-latex-fragment' always need
  1377. `C-c C-c' to be removed."
  1378. :group 'org-sparse-trees
  1379. :group 'org-time
  1380. :type 'boolean)
  1381. (defcustom org-occur-hook '(org-first-headline-recenter)
  1382. "Hook that is run after `org-occur' has constructed a sparse tree.
  1383. This can be used to recenter the window to show as much of the structure
  1384. as possible."
  1385. :group 'org-sparse-trees
  1386. :type 'hook)
  1387. (defgroup org-imenu-and-speedbar nil
  1388. "Options concerning imenu and speedbar in Org-mode."
  1389. :tag "Org Imenu and Speedbar"
  1390. :group 'org-structure)
  1391. (defcustom org-imenu-depth 2
  1392. "The maximum level for Imenu access to Org-mode headlines.
  1393. This also applied for speedbar access."
  1394. :group 'org-imenu-and-speedbar
  1395. :type 'integer)
  1396. (defgroup org-table nil
  1397. "Options concerning tables in Org-mode."
  1398. :tag "Org Table"
  1399. :group 'org)
  1400. (defcustom org-enable-table-editor 'optimized
  1401. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1402. When nil, such lines will be treated like ordinary lines.
  1403. When equal to the symbol `optimized', the table editor will be optimized to
  1404. do the following:
  1405. - Automatic overwrite mode in front of whitespace in table fields.
  1406. This makes the structure of the table stay in tact as long as the edited
  1407. field does not exceed the column width.
  1408. - Minimize the number of realigns. Normally, the table is aligned each time
  1409. TAB or RET are pressed to move to another field. With optimization this
  1410. happens only if changes to a field might have changed the column width.
  1411. Optimization requires replacing the functions `self-insert-command',
  1412. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1413. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1414. very good at guessing when a re-align will be necessary, but you can always
  1415. force one with \\[org-ctrl-c-ctrl-c].
  1416. If you would like to use the optimized version in Org-mode, but the
  1417. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1418. This variable can be used to turn on and off the table editor during a session,
  1419. but in order to toggle optimization, a restart is required.
  1420. See also the variable `org-table-auto-blank-field'."
  1421. :group 'org-table
  1422. :type '(choice
  1423. (const :tag "off" nil)
  1424. (const :tag "on" t)
  1425. (const :tag "on, optimized" optimized)))
  1426. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1427. (version<= emacs-version "24.1"))
  1428. "Non-nil means cluster self-insert commands for undo when possible.
  1429. If this is set, then, like in the Emacs command loop, 20 consecutive
  1430. characters will be undone together.
  1431. This is configurable, because there is some impact on typing performance."
  1432. :group 'org-table
  1433. :type 'boolean)
  1434. (defcustom org-table-tab-recognizes-table.el t
  1435. "Non-nil means TAB will automatically notice a table.el table.
  1436. When it sees such a table, it moves point into it and - if necessary -
  1437. calls `table-recognize-table'."
  1438. :group 'org-table-editing
  1439. :type 'boolean)
  1440. (defgroup org-link nil
  1441. "Options concerning links in Org-mode."
  1442. :tag "Org Link"
  1443. :group 'org)
  1444. (defvar org-link-abbrev-alist-local nil
  1445. "Buffer-local version of `org-link-abbrev-alist', which see.
  1446. The value of this is taken from the #+LINK lines.")
  1447. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1448. (defcustom org-link-abbrev-alist nil
  1449. "Alist of link abbreviations.
  1450. The car of each element is a string, to be replaced at the start of a link.
  1451. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1452. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1453. [[linkkey:tag][description]]
  1454. The 'linkkey' must be a word word, starting with a letter, followed
  1455. by letters, numbers, '-' or '_'.
  1456. If REPLACE is a string, the tag will simply be appended to create the link.
  1457. If the string contains \"%s\", the tag will be inserted there. If the string
  1458. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1459. at that point (see the function `url-hexify-string'). If the string contains
  1460. the specifier \"%(my-function)\", then the custom function `my-function' will
  1461. be invoked: this function takes the tag as its only argument and must return
  1462. a string.
  1463. REPLACE may also be a function that will be called with the tag as the
  1464. only argument to create the link, which should be returned as a string.
  1465. See the manual for examples."
  1466. :group 'org-link
  1467. :type '(repeat
  1468. (cons
  1469. (string :tag "Protocol")
  1470. (choice
  1471. (string :tag "Format")
  1472. (function)))))
  1473. (defcustom org-descriptive-links t
  1474. "Non-nil means Org will display descriptive links.
  1475. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1476. \"Org Website\", hiding the link itself and just displaying its
  1477. description. When set to `nil', Org will display the full links
  1478. literally.
  1479. You can interactively set the value of this variable by calling
  1480. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1481. :group 'org-link
  1482. :type 'boolean)
  1483. (defcustom org-link-file-path-type 'adaptive
  1484. "How the path name in file links should be stored.
  1485. Valid values are:
  1486. relative Relative to the current directory, i.e. the directory of the file
  1487. into which the link is being inserted.
  1488. absolute Absolute path, if possible with ~ for home directory.
  1489. noabbrev Absolute path, no abbreviation of home directory.
  1490. adaptive Use relative path for files in the current directory and sub-
  1491. directories of it. For other files, use an absolute path."
  1492. :group 'org-link
  1493. :type '(choice
  1494. (const relative)
  1495. (const absolute)
  1496. (const noabbrev)
  1497. (const adaptive)))
  1498. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1499. "Types of links that should be activated in Org-mode files.
  1500. This is a list of symbols, each leading to the activation of a certain link
  1501. type. In principle, it does not hurt to turn on most link types - there may
  1502. be a small gain when turning off unused link types. The types are:
  1503. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1504. angle Links in angular brackets that may contain whitespace like
  1505. <bbdb:Carsten Dominik>.
  1506. plain Plain links in normal text, no whitespace, like http://google.com.
  1507. radio Text that is matched by a radio target, see manual for details.
  1508. tag Tag settings in a headline (link to tag search).
  1509. date Time stamps (link to calendar).
  1510. footnote Footnote labels.
  1511. Changing this variable requires a restart of Emacs to become effective."
  1512. :group 'org-link
  1513. :type '(set :greedy t
  1514. (const :tag "Double bracket links" bracket)
  1515. (const :tag "Angular bracket links" angle)
  1516. (const :tag "Plain text links" plain)
  1517. (const :tag "Radio target matches" radio)
  1518. (const :tag "Tags" tag)
  1519. (const :tag "Timestamps" date)
  1520. (const :tag "Footnotes" footnote)))
  1521. (defcustom org-make-link-description-function nil
  1522. "Function to use for generating link descriptions from links.
  1523. When nil, the link location will be used. This function must take
  1524. two parameters: the first one is the link, the second one is the
  1525. description generated by `org-insert-link'. The function should
  1526. return the description to use."
  1527. :group 'org-link
  1528. :type '(choice (const nil) (function)))
  1529. (defgroup org-link-store nil
  1530. "Options concerning storing links in Org-mode."
  1531. :tag "Org Store Link"
  1532. :group 'org-link)
  1533. (defcustom org-url-hexify-p t
  1534. "When non-nil, hexify URL when creating a link."
  1535. :type 'boolean
  1536. :version "24.3"
  1537. :group 'org-link-store)
  1538. (defcustom org-email-link-description-format "Email %c: %.30s"
  1539. "Format of the description part of a link to an email or usenet message.
  1540. The following %-escapes will be replaced by corresponding information:
  1541. %F full \"From\" field
  1542. %f name, taken from \"From\" field, address if no name
  1543. %T full \"To\" field
  1544. %t first name in \"To\" field, address if no name
  1545. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1546. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1547. %s subject
  1548. %d date
  1549. %m message-id.
  1550. You may use normal field width specification between the % and the letter.
  1551. This is for example useful to limit the length of the subject.
  1552. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1553. :group 'org-link-store
  1554. :type 'string)
  1555. (defcustom org-from-is-user-regexp
  1556. (let (r1 r2)
  1557. (when (and user-mail-address (not (string= user-mail-address "")))
  1558. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1559. (when (and user-full-name (not (string= user-full-name "")))
  1560. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1561. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1562. "Regexp matched against the \"From:\" header of an email or usenet message.
  1563. It should match if the message is from the user him/herself."
  1564. :group 'org-link-store
  1565. :type 'regexp)
  1566. (defcustom org-context-in-file-links t
  1567. "Non-nil means file links from `org-store-link' contain context.
  1568. A search string will be added to the file name with :: as separator and
  1569. used to find the context when the link is activated by the command
  1570. `org-open-at-point'. When this option is t, the entire active region
  1571. will be placed in the search string of the file link. If set to a
  1572. positive integer, only the first n lines of context will be stored.
  1573. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1574. negates this setting for the duration of the command."
  1575. :group 'org-link-store
  1576. :type '(choice boolean integer))
  1577. (defcustom org-keep-stored-link-after-insertion nil
  1578. "Non-nil means keep link in list for entire session.
  1579. The command `org-store-link' adds a link pointing to the current
  1580. location to an internal list. These links accumulate during a session.
  1581. The command `org-insert-link' can be used to insert links into any
  1582. Org-mode file (offering completion for all stored links). When this
  1583. option is nil, every link which has been inserted once using \\[org-insert-link]
  1584. will be removed from the list, to make completing the unused links
  1585. more efficient."
  1586. :group 'org-link-store
  1587. :type 'boolean)
  1588. (defgroup org-link-follow nil
  1589. "Options concerning following links in Org-mode."
  1590. :tag "Org Follow Link"
  1591. :group 'org-link)
  1592. (defcustom org-link-translation-function nil
  1593. "Function to translate links with different syntax to Org syntax.
  1594. This can be used to translate links created for example by the Planner
  1595. or emacs-wiki packages to Org syntax.
  1596. The function must accept two parameters, a TYPE containing the link
  1597. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1598. which is everything after the link protocol. It should return a cons
  1599. with possibly modified values of type and path.
  1600. Org contains a function for this, so if you set this variable to
  1601. `org-translate-link-from-planner', you should be able follow many
  1602. links created by planner."
  1603. :group 'org-link-follow
  1604. :type '(choice (const nil) (function)))
  1605. (defcustom org-follow-link-hook nil
  1606. "Hook that is run after a link has been followed."
  1607. :group 'org-link-follow
  1608. :type 'hook)
  1609. (defcustom org-tab-follows-link nil
  1610. "Non-nil means on links TAB will follow the link.
  1611. Needs to be set before org.el is loaded.
  1612. This really should not be used, it does not make sense, and the
  1613. implementation is bad."
  1614. :group 'org-link-follow
  1615. :type 'boolean)
  1616. (defcustom org-return-follows-link nil
  1617. "Non-nil means on links RET will follow the link.
  1618. In tables, the special behavior of RET has precedence."
  1619. :group 'org-link-follow
  1620. :type 'boolean)
  1621. (defcustom org-mouse-1-follows-link
  1622. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1623. "Non-nil means mouse-1 on a link will follow the link.
  1624. A longer mouse click will still set point. Does not work on XEmacs.
  1625. Needs to be set before org.el is loaded."
  1626. :group 'org-link-follow
  1627. :version "24.4"
  1628. :package-version '(Org . "8.3")
  1629. :type '(choice
  1630. (const :tag "A double click follows the link" double)
  1631. (const :tag "Unconditionally follow the link with mouse-1" t)
  1632. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1633. (defcustom org-mark-ring-length 4
  1634. "Number of different positions to be recorded in the ring.
  1635. Changing this requires a restart of Emacs to work correctly."
  1636. :group 'org-link-follow
  1637. :type 'integer)
  1638. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1639. "Non-nil means internal links in Org files must exactly match a headline.
  1640. When nil, the link search tries to match a phrase with all words
  1641. in the search text."
  1642. :group 'org-link-follow
  1643. :version "24.1"
  1644. :type '(choice
  1645. (const :tag "Use fuzzy text search" nil)
  1646. (const :tag "Match only exact headline" t)
  1647. (const :tag "Match exact headline or query to create it"
  1648. query-to-create)))
  1649. (defcustom org-link-frame-setup
  1650. '((vm . vm-visit-folder-other-frame)
  1651. (vm-imap . vm-visit-imap-folder-other-frame)
  1652. (gnus . org-gnus-no-new-news)
  1653. (file . find-file-other-window)
  1654. (wl . wl-other-frame))
  1655. "Setup the frame configuration for following links.
  1656. When following a link with Emacs, it may often be useful to display
  1657. this link in another window or frame. This variable can be used to
  1658. set this up for the different types of links.
  1659. For VM, use any of
  1660. `vm-visit-folder'
  1661. `vm-visit-folder-other-window'
  1662. `vm-visit-folder-other-frame'
  1663. For Gnus, use any of
  1664. `gnus'
  1665. `gnus-other-frame'
  1666. `org-gnus-no-new-news'
  1667. For FILE, use any of
  1668. `find-file'
  1669. `find-file-other-window'
  1670. `find-file-other-frame'
  1671. For Wanderlust use any of
  1672. `wl'
  1673. `wl-other-frame'
  1674. For the calendar, use the variable `calendar-setup'.
  1675. For BBDB, it is currently only possible to display the matches in
  1676. another window."
  1677. :group 'org-link-follow
  1678. :type '(list
  1679. (cons (const vm)
  1680. (choice
  1681. (const vm-visit-folder)
  1682. (const vm-visit-folder-other-window)
  1683. (const vm-visit-folder-other-frame)))
  1684. (cons (const vm-imap)
  1685. (choice
  1686. (const vm-visit-imap-folder)
  1687. (const vm-visit-imap-folder-other-window)
  1688. (const vm-visit-imap-folder-other-frame)))
  1689. (cons (const gnus)
  1690. (choice
  1691. (const gnus)
  1692. (const gnus-other-frame)
  1693. (const org-gnus-no-new-news)))
  1694. (cons (const file)
  1695. (choice
  1696. (const find-file)
  1697. (const find-file-other-window)
  1698. (const find-file-other-frame)))
  1699. (cons (const wl)
  1700. (choice
  1701. (const wl)
  1702. (const wl-other-frame)))))
  1703. (defcustom org-display-internal-link-with-indirect-buffer nil
  1704. "Non-nil means use indirect buffer to display infile links.
  1705. Activating internal links (from one location in a file to another location
  1706. in the same file) normally just jumps to the location. When the link is
  1707. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1708. is displayed in
  1709. another window. When this option is set, the other window actually displays
  1710. an indirect buffer clone of the current buffer, to avoid any visibility
  1711. changes to the current buffer."
  1712. :group 'org-link-follow
  1713. :type 'boolean)
  1714. (defcustom org-open-non-existing-files nil
  1715. "Non-nil means `org-open-file' will open non-existing files.
  1716. When nil, an error will be generated.
  1717. This variable applies only to external applications because they
  1718. might choke on non-existing files. If the link is to a file that
  1719. will be opened in Emacs, the variable is ignored."
  1720. :group 'org-link-follow
  1721. :type 'boolean)
  1722. (defcustom org-open-directory-means-index-dot-org nil
  1723. "Non-nil means a link to a directory really means to index.org.
  1724. When nil, following a directory link will run dired or open a finder/explorer
  1725. window on that directory."
  1726. :group 'org-link-follow
  1727. :type 'boolean)
  1728. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1729. "Function and arguments to call for following mailto links.
  1730. This is a list with the first element being a Lisp function, and the
  1731. remaining elements being arguments to the function. In string arguments,
  1732. %a will be replaced by the address, and %s will be replaced by the subject
  1733. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1734. :group 'org-link-follow
  1735. :type '(choice
  1736. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1737. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1738. (const :tag "message-mail" (message-mail "%a" "%s"))
  1739. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1740. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1741. "Non-nil means ask for confirmation before executing shell links.
  1742. Shell links can be dangerous: just think about a link
  1743. [[shell:rm -rf ~/*][Google Search]]
  1744. This link would show up in your Org-mode document as \"Google Search\",
  1745. but really it would remove your entire home directory.
  1746. Therefore we advise against setting this variable to nil.
  1747. Just change it to `y-or-n-p' if you want to confirm with a
  1748. single keystroke rather than having to type \"yes\"."
  1749. :group 'org-link-follow
  1750. :type '(choice
  1751. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1752. (const :tag "with y-or-n (faster)" y-or-n-p)
  1753. (const :tag "no confirmation (dangerous)" nil)))
  1754. (put 'org-confirm-shell-link-function
  1755. 'safe-local-variable
  1756. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1757. (defcustom org-confirm-shell-link-not-regexp ""
  1758. "A regexp to skip confirmation for shell links."
  1759. :group 'org-link-follow
  1760. :version "24.1"
  1761. :type 'regexp)
  1762. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1763. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1764. Elisp links can be dangerous: just think about a link
  1765. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1766. This link would show up in your Org-mode document as \"Google Search\",
  1767. but really it would remove your entire home directory.
  1768. Therefore we advise against setting this variable to nil.
  1769. Just change it to `y-or-n-p' if you want to confirm with a
  1770. single keystroke rather than having to type \"yes\"."
  1771. :group 'org-link-follow
  1772. :type '(choice
  1773. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1774. (const :tag "with y-or-n (faster)" y-or-n-p)
  1775. (const :tag "no confirmation (dangerous)" nil)))
  1776. (put 'org-confirm-shell-link-function
  1777. 'safe-local-variable
  1778. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1779. (defcustom org-confirm-elisp-link-not-regexp ""
  1780. "A regexp to skip confirmation for Elisp links."
  1781. :group 'org-link-follow
  1782. :version "24.1"
  1783. :type 'regexp)
  1784. (defconst org-file-apps-defaults-gnu
  1785. '((remote . emacs)
  1786. (system . mailcap)
  1787. (t . mailcap))
  1788. "Default file applications on a UNIX or GNU/Linux system.
  1789. See `org-file-apps'.")
  1790. (defconst org-file-apps-defaults-macosx
  1791. '((remote . emacs)
  1792. (t . "open %s")
  1793. (system . "open %s")
  1794. ("ps.gz" . "gv %s")
  1795. ("eps.gz" . "gv %s")
  1796. ("dvi" . "xdvi %s")
  1797. ("fig" . "xfig %s"))
  1798. "Default file applications on a MacOS X system.
  1799. The system \"open\" is known as a default, but we use X11 applications
  1800. for some files for which the OS does not have a good default.
  1801. See `org-file-apps'.")
  1802. (defconst org-file-apps-defaults-windowsnt
  1803. (list
  1804. '(remote . emacs)
  1805. (cons t
  1806. (list (if (featurep 'xemacs)
  1807. 'mswindows-shell-execute
  1808. 'w32-shell-execute)
  1809. "open" 'file))
  1810. (cons 'system
  1811. (list (if (featurep 'xemacs)
  1812. 'mswindows-shell-execute
  1813. 'w32-shell-execute)
  1814. "open" 'file)))
  1815. "Default file applications on a Windows NT system.
  1816. The system \"open\" is used for most files.
  1817. See `org-file-apps'.")
  1818. (defcustom org-file-apps
  1819. '((auto-mode . emacs)
  1820. ("\\.mm\\'" . default)
  1821. ("\\.x?html?\\'" . default)
  1822. ("\\.pdf\\'" . default))
  1823. "External applications for opening `file:path' items in a document.
  1824. Org-mode uses system defaults for different file types, but
  1825. you can use this variable to set the application for a given file
  1826. extension. The entries in this list are cons cells where the car identifies
  1827. files and the cdr the corresponding command. Possible values for the
  1828. file identifier are
  1829. \"string\" A string as a file identifier can be interpreted in different
  1830. ways, depending on its contents:
  1831. - Alphanumeric characters only:
  1832. Match links with this file extension.
  1833. Example: (\"pdf\" . \"evince %s\")
  1834. to open PDFs with evince.
  1835. - Regular expression: Match links where the
  1836. filename matches the regexp. If you want to
  1837. use groups here, use shy groups.
  1838. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1839. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1840. to open *.html and *.xhtml with firefox.
  1841. - Regular expression which contains (non-shy) groups:
  1842. Match links where the whole link, including \"::\", and
  1843. anything after that, matches the regexp.
  1844. In a custom command string, %1, %2, etc. are replaced with
  1845. the parts of the link that were matched by the groups.
  1846. For backwards compatibility, if a command string is given
  1847. that does not use any of the group matches, this case is
  1848. handled identically to the second one (i.e. match against
  1849. file name only).
  1850. In a custom lisp form, you can access the group matches with
  1851. (match-string n link).
  1852. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1853. to open [[file:document.pdf::5]] with evince at page 5.
  1854. `directory' Matches a directory
  1855. `remote' Matches a remote file, accessible through tramp or efs.
  1856. Remote files most likely should be visited through Emacs
  1857. because external applications cannot handle such paths.
  1858. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1859. so all files Emacs knows how to handle. Using this with
  1860. command `emacs' will open most files in Emacs. Beware that this
  1861. will also open html files inside Emacs, unless you add
  1862. (\"html\" . default) to the list as well.
  1863. t Default for files not matched by any of the other options.
  1864. `system' The system command to open files, like `open' on Windows
  1865. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1866. that will be selected if you call `C-c C-o' with a double
  1867. \\[universal-argument] \\[universal-argument] prefix.
  1868. Possible values for the command are:
  1869. `emacs' The file will be visited by the current Emacs process.
  1870. `default' Use the default application for this file type, which is the
  1871. association for t in the list, most likely in the system-specific
  1872. part.
  1873. This can be used to overrule an unwanted setting in the
  1874. system-specific variable.
  1875. `system' Use the system command for opening files, like \"open\".
  1876. This command is specified by the entry whose car is `system'.
  1877. Most likely, the system-specific version of this variable
  1878. does define this command, but you can overrule/replace it
  1879. here.
  1880. string A command to be executed by a shell; %s will be replaced
  1881. by the path to the file.
  1882. sexp A Lisp form which will be evaluated. The file path will
  1883. be available in the Lisp variable `file'.
  1884. For more examples, see the system specific constants
  1885. `org-file-apps-defaults-macosx'
  1886. `org-file-apps-defaults-windowsnt'
  1887. `org-file-apps-defaults-gnu'."
  1888. :group 'org-link-follow
  1889. :type '(repeat
  1890. (cons (choice :value ""
  1891. (string :tag "Extension")
  1892. (const :tag "System command to open files" system)
  1893. (const :tag "Default for unrecognized files" t)
  1894. (const :tag "Remote file" remote)
  1895. (const :tag "Links to a directory" directory)
  1896. (const :tag "Any files that have Emacs modes"
  1897. auto-mode))
  1898. (choice :value ""
  1899. (const :tag "Visit with Emacs" emacs)
  1900. (const :tag "Use default" default)
  1901. (const :tag "Use the system command" system)
  1902. (string :tag "Command")
  1903. (sexp :tag "Lisp form")))))
  1904. (defcustom org-doi-server-url "http://dx.doi.org/"
  1905. "The URL of the DOI server."
  1906. :type 'string
  1907. :version "24.3"
  1908. :group 'org-link-follow)
  1909. (defgroup org-refile nil
  1910. "Options concerning refiling entries in Org-mode."
  1911. :tag "Org Refile"
  1912. :group 'org)
  1913. (defcustom org-directory "~/org"
  1914. "Directory with org files.
  1915. This is just a default location to look for Org files. There is no need
  1916. at all to put your files into this directory. It is only used in the
  1917. following situations:
  1918. 1. When a capture template specifies a target file that is not an
  1919. absolute path. The path will then be interpreted relative to
  1920. `org-directory'
  1921. 2. When a capture note is filed away in an interactive way (when exiting the
  1922. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1923. with `org-directory' as the default path."
  1924. :group 'org-refile
  1925. :group 'org-capture
  1926. :type 'directory)
  1927. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1928. "Default target for storing notes.
  1929. Used as a fall back file for org-capture.el, for templates that
  1930. do not specify a target file."
  1931. :group 'org-refile
  1932. :group 'org-capture
  1933. :type '(choice
  1934. (const :tag "Default from remember-data-file" nil)
  1935. file))
  1936. (defcustom org-goto-interface 'outline
  1937. "The default interface to be used for `org-goto'.
  1938. Allowed values are:
  1939. outline The interface shows an outline of the relevant file
  1940. and the correct heading is found by moving through
  1941. the outline or by searching with incremental search.
  1942. outline-path-completion Headlines in the current buffer are offered via
  1943. completion. This is the interface also used by
  1944. the refile command."
  1945. :group 'org-refile
  1946. :type '(choice
  1947. (const :tag "Outline" outline)
  1948. (const :tag "Outline-path-completion" outline-path-completion)))
  1949. (defcustom org-goto-max-level 5
  1950. "Maximum target level when running `org-goto' with refile interface."
  1951. :group 'org-refile
  1952. :type 'integer)
  1953. (defcustom org-reverse-note-order nil
  1954. "Non-nil means store new notes at the beginning of a file or entry.
  1955. When nil, new notes will be filed to the end of a file or entry.
  1956. This can also be a list with cons cells of regular expressions that
  1957. are matched against file names, and values."
  1958. :group 'org-capture
  1959. :group 'org-refile
  1960. :type '(choice
  1961. (const :tag "Reverse always" t)
  1962. (const :tag "Reverse never" nil)
  1963. (repeat :tag "By file name regexp"
  1964. (cons regexp boolean))))
  1965. (defcustom org-log-refile nil
  1966. "Information to record when a task is refiled.
  1967. Possible values are:
  1968. nil Don't add anything
  1969. time Add a time stamp to the task
  1970. note Prompt for a note and add it with template `org-log-note-headings'
  1971. This option can also be set with on a per-file-basis with
  1972. #+STARTUP: nologrefile
  1973. #+STARTUP: logrefile
  1974. #+STARTUP: lognoterefile
  1975. You can have local logging settings for a subtree by setting the LOGGING
  1976. property to one or more of these keywords.
  1977. When bulk-refiling from the agenda, the value `note' is forbidden and
  1978. will temporarily be changed to `time'."
  1979. :group 'org-refile
  1980. :group 'org-progress
  1981. :version "24.1"
  1982. :type '(choice
  1983. (const :tag "No logging" nil)
  1984. (const :tag "Record timestamp" time)
  1985. (const :tag "Record timestamp with note." note)))
  1986. (defcustom org-refile-targets nil
  1987. "Targets for refiling entries with \\[org-refile].
  1988. This is a list of cons cells. Each cell contains:
  1989. - a specification of the files to be considered, either a list of files,
  1990. or a symbol whose function or variable value will be used to retrieve
  1991. a file name or a list of file names. If you use `org-agenda-files' for
  1992. that, all agenda files will be scanned for targets. Nil means consider
  1993. headings in the current buffer.
  1994. - A specification of how to find candidate refile targets. This may be
  1995. any of:
  1996. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1997. This tag has to be present in all target headlines, inheritance will
  1998. not be considered.
  1999. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2000. todo keyword.
  2001. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2002. headlines that are refiling targets.
  2003. - a cons cell (:level . N). Any headline of level N is considered a target.
  2004. Note that, when `org-odd-levels-only' is set, level corresponds to
  2005. order in hierarchy, not to the number of stars.
  2006. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2007. Note that, when `org-odd-levels-only' is set, level corresponds to
  2008. order in hierarchy, not to the number of stars.
  2009. Each element of this list generates a set of possible targets.
  2010. The union of these sets is presented (with completion) to
  2011. the user by `org-refile'.
  2012. You can set the variable `org-refile-target-verify-function' to a function
  2013. to verify each headline found by the simple criteria above.
  2014. When this variable is nil, all top-level headlines in the current buffer
  2015. are used, equivalent to the value `((nil . (:level . 1))'."
  2016. :group 'org-refile
  2017. :type '(repeat
  2018. (cons
  2019. (choice :value org-agenda-files
  2020. (const :tag "All agenda files" org-agenda-files)
  2021. (const :tag "Current buffer" nil)
  2022. (function) (variable) (file))
  2023. (choice :tag "Identify target headline by"
  2024. (cons :tag "Specific tag" (const :value :tag) (string))
  2025. (cons :tag "TODO keyword" (const :value :todo) (string))
  2026. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2027. (cons :tag "Level number" (const :value :level) (integer))
  2028. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2029. (defcustom org-refile-target-verify-function nil
  2030. "Function to verify if the headline at point should be a refile target.
  2031. The function will be called without arguments, with point at the
  2032. beginning of the headline. It should return t and leave point
  2033. where it is if the headline is a valid target for refiling.
  2034. If the target should not be selected, the function must return nil.
  2035. In addition to this, it may move point to a place from where the search
  2036. should be continued. For example, the function may decide that the entire
  2037. subtree of the current entry should be excluded and move point to the end
  2038. of the subtree."
  2039. :group 'org-refile
  2040. :type '(choice
  2041. (const nil)
  2042. (function)))
  2043. (defcustom org-refile-use-cache nil
  2044. "Non-nil means cache refile targets to speed up the process.
  2045. The cache for a particular file will be updated automatically when
  2046. the buffer has been killed, or when any of the marker used for flagging
  2047. refile targets no longer points at a live buffer.
  2048. If you have added new entries to a buffer that might themselves be targets,
  2049. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2050. find that easier, `C-u C-u C-u C-c C-w'."
  2051. :group 'org-refile
  2052. :version "24.1"
  2053. :type 'boolean)
  2054. (defcustom org-refile-use-outline-path nil
  2055. "Non-nil means provide refile targets as paths.
  2056. So a level 3 headline will be available as level1/level2/level3.
  2057. When the value is `file', also include the file name (without directory)
  2058. into the path. In this case, you can also stop the completion after
  2059. the file name, to get entries inserted as top level in the file.
  2060. When `full-file-path', include the full file path."
  2061. :group 'org-refile
  2062. :type '(choice
  2063. (const :tag "Not" nil)
  2064. (const :tag "Yes" t)
  2065. (const :tag "Start with file name" file)
  2066. (const :tag "Start with full file path" full-file-path)))
  2067. (defcustom org-outline-path-complete-in-steps t
  2068. "Non-nil means complete the outline path in hierarchical steps.
  2069. When Org-mode uses the refile interface to select an outline path
  2070. \(see variable `org-refile-use-outline-path'), the completion of
  2071. the path can be done is a single go, or if can be done in steps down
  2072. the headline hierarchy. Going in steps is probably the best if you
  2073. do not use a special completion package like `ido' or `icicles'.
  2074. However, when using these packages, going in one step can be very
  2075. fast, while still showing the whole path to the entry."
  2076. :group 'org-refile
  2077. :type 'boolean)
  2078. (defcustom org-refile-allow-creating-parent-nodes nil
  2079. "Non-nil means allow to create new nodes as refile targets.
  2080. New nodes are then created by adding \"/new node name\" to the completion
  2081. of an existing node. When the value of this variable is `confirm',
  2082. new node creation must be confirmed by the user (recommended).
  2083. When nil, the completion must match an existing entry.
  2084. Note that, if the new heading is not seen by the criteria
  2085. listed in `org-refile-targets', multiple instances of the same
  2086. heading would be created by trying again to file under the new
  2087. heading."
  2088. :group 'org-refile
  2089. :type '(choice
  2090. (const :tag "Never" nil)
  2091. (const :tag "Always" t)
  2092. (const :tag "Prompt for confirmation" confirm)))
  2093. (defcustom org-refile-active-region-within-subtree nil
  2094. "Non-nil means also refile active region within a subtree.
  2095. By default `org-refile' doesn't allow refiling regions if they
  2096. don't contain a set of subtrees, but it might be convenient to
  2097. do so sometimes: in that case, the first line of the region is
  2098. converted to a headline before refiling."
  2099. :group 'org-refile
  2100. :version "24.1"
  2101. :type 'boolean)
  2102. (defgroup org-todo nil
  2103. "Options concerning TODO items in Org-mode."
  2104. :tag "Org TODO"
  2105. :group 'org)
  2106. (defgroup org-progress nil
  2107. "Options concerning Progress logging in Org-mode."
  2108. :tag "Org Progress"
  2109. :group 'org-time)
  2110. (defvar org-todo-interpretation-widgets
  2111. '((:tag "Sequence (cycling hits every state)" sequence)
  2112. (:tag "Type (cycling directly to DONE)" type))
  2113. "The available interpretation symbols for customizing `org-todo-keywords'.
  2114. Interested libraries should add to this list.")
  2115. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2116. "List of TODO entry keyword sequences and their interpretation.
  2117. \\<org-mode-map>This is a list of sequences.
  2118. Each sequence starts with a symbol, either `sequence' or `type',
  2119. indicating if the keywords should be interpreted as a sequence of
  2120. action steps, or as different types of TODO items. The first
  2121. keywords are states requiring action - these states will select a headline
  2122. for inclusion into the global TODO list Org-mode produces. If one of
  2123. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2124. signify that no further action is necessary. If \"|\" is not found,
  2125. the last keyword is treated as the only DONE state of the sequence.
  2126. The command \\[org-todo] cycles an entry through these states, and one
  2127. additional state where no keyword is present. For details about this
  2128. cycling, see the manual.
  2129. TODO keywords and interpretation can also be set on a per-file basis with
  2130. the special #+SEQ_TODO and #+TYP_TODO lines.
  2131. Each keyword can optionally specify a character for fast state selection
  2132. \(in combination with the variable `org-use-fast-todo-selection')
  2133. and specifiers for state change logging, using the same syntax that
  2134. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2135. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2136. indicates to record a time stamp each time this state is selected.
  2137. Each keyword may also specify if a timestamp or a note should be
  2138. recorded when entering or leaving the state, by adding additional
  2139. characters in the parenthesis after the keyword. This looks like this:
  2140. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2141. record only the time of the state change. With X and Y being either
  2142. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2143. Y when leaving the state if and only if the *target* state does not
  2144. define X. You may omit any of the fast-selection key or X or /Y,
  2145. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2146. For backward compatibility, this variable may also be just a list
  2147. of keywords. In this case the interpretation (sequence or type) will be
  2148. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2149. :group 'org-todo
  2150. :group 'org-keywords
  2151. :type '(choice
  2152. (repeat :tag "Old syntax, just keywords"
  2153. (string :tag "Keyword"))
  2154. (repeat :tag "New syntax"
  2155. (cons
  2156. (choice
  2157. :tag "Interpretation"
  2158. ;;Quick and dirty way to see
  2159. ;;`org-todo-interpretations'. This takes the
  2160. ;;place of item arguments
  2161. :convert-widget
  2162. (lambda (widget)
  2163. (widget-put widget
  2164. :args (mapcar
  2165. #'(lambda (x)
  2166. (widget-convert
  2167. (cons 'const x)))
  2168. org-todo-interpretation-widgets))
  2169. widget))
  2170. (repeat
  2171. (string :tag "Keyword"))))))
  2172. (defvar org-todo-keywords-1 nil
  2173. "All TODO and DONE keywords active in a buffer.")
  2174. (make-variable-buffer-local 'org-todo-keywords-1)
  2175. (defvar org-todo-keywords-for-agenda nil)
  2176. (defvar org-done-keywords-for-agenda nil)
  2177. (defvar org-todo-keyword-alist-for-agenda nil)
  2178. (defvar org-tag-alist-for-agenda nil
  2179. "Alist of all tags from all agenda files.")
  2180. (defvar org-tag-groups-alist-for-agenda nil
  2181. "Alist of all groups tags from all current agenda files.")
  2182. (defvar org-tag-groups-alist nil)
  2183. (make-variable-buffer-local 'org-tag-groups-alist)
  2184. (defvar org-agenda-contributing-files nil)
  2185. (defvar org-not-done-keywords nil)
  2186. (make-variable-buffer-local 'org-not-done-keywords)
  2187. (defvar org-done-keywords nil)
  2188. (make-variable-buffer-local 'org-done-keywords)
  2189. (defvar org-todo-heads nil)
  2190. (make-variable-buffer-local 'org-todo-heads)
  2191. (defvar org-todo-sets nil)
  2192. (make-variable-buffer-local 'org-todo-sets)
  2193. (defvar org-todo-log-states nil)
  2194. (make-variable-buffer-local 'org-todo-log-states)
  2195. (defvar org-todo-kwd-alist nil)
  2196. (make-variable-buffer-local 'org-todo-kwd-alist)
  2197. (defvar org-todo-key-alist nil)
  2198. (make-variable-buffer-local 'org-todo-key-alist)
  2199. (defvar org-todo-key-trigger nil)
  2200. (make-variable-buffer-local 'org-todo-key-trigger)
  2201. (defcustom org-todo-interpretation 'sequence
  2202. "Controls how TODO keywords are interpreted.
  2203. This variable is in principle obsolete and is only used for
  2204. backward compatibility, if the interpretation of todo keywords is
  2205. not given already in `org-todo-keywords'. See that variable for
  2206. more information."
  2207. :group 'org-todo
  2208. :group 'org-keywords
  2209. :type '(choice (const sequence)
  2210. (const type)))
  2211. (defcustom org-use-fast-todo-selection t
  2212. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2213. This variable describes if and under what circumstances the cycling
  2214. mechanism for TODO keywords will be replaced by a single-key, direct
  2215. selection scheme.
  2216. When nil, fast selection is never used.
  2217. When the symbol `prefix', it will be used when `org-todo' is called
  2218. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2219. `C-u t' in an agenda buffer.
  2220. When t, fast selection is used by default. In this case, the prefix
  2221. argument forces cycling instead.
  2222. In all cases, the special interface is only used if access keys have
  2223. actually been assigned by the user, i.e. if keywords in the configuration
  2224. are followed by a letter in parenthesis, like TODO(t)."
  2225. :group 'org-todo
  2226. :type '(choice
  2227. (const :tag "Never" nil)
  2228. (const :tag "By default" t)
  2229. (const :tag "Only with C-u C-c C-t" prefix)))
  2230. (defcustom org-provide-todo-statistics t
  2231. "Non-nil means update todo statistics after insert and toggle.
  2232. ALL-HEADLINES means update todo statistics by including headlines
  2233. with no TODO keyword as well, counting them as not done.
  2234. A list of TODO keywords means the same, but skip keywords that are
  2235. not in this list.
  2236. When set to a list of two lists, the first list contains keywords
  2237. to consider as TODO keywords, the second list contains keywords
  2238. to consider as DONE keywords.
  2239. When this is set, todo statistics is updated in the parent of the
  2240. current entry each time a todo state is changed."
  2241. :group 'org-todo
  2242. :type '(choice
  2243. (const :tag "Yes, only for TODO entries" t)
  2244. (const :tag "Yes, including all entries" all-headlines)
  2245. (repeat :tag "Yes, for TODOs in this list"
  2246. (string :tag "TODO keyword"))
  2247. (list :tag "Yes, for TODOs and DONEs in these lists"
  2248. (repeat (string :tag "TODO keyword"))
  2249. (repeat (string :tag "DONE keyword")))
  2250. (other :tag "No TODO statistics" nil)))
  2251. (defcustom org-hierarchical-todo-statistics t
  2252. "Non-nil means TODO statistics covers just direct children.
  2253. When nil, all entries in the subtree are considered.
  2254. This has only an effect if `org-provide-todo-statistics' is set.
  2255. To set this to nil for only a single subtree, use a COOKIE_DATA
  2256. property and include the word \"recursive\" into the value."
  2257. :group 'org-todo
  2258. :type 'boolean)
  2259. (defcustom org-after-todo-state-change-hook nil
  2260. "Hook which is run after the state of a TODO item was changed.
  2261. The new state (a string with a TODO keyword, or nil) is available in the
  2262. Lisp variable `org-state'."
  2263. :group 'org-todo
  2264. :type 'hook)
  2265. (defvar org-blocker-hook nil
  2266. "Hook for functions that are allowed to block a state change.
  2267. Functions in this hook should not modify the buffer.
  2268. Each function gets as its single argument a property list,
  2269. see `org-trigger-hook' for more information about this list.
  2270. If any of the functions in this hook returns nil, the state change
  2271. is blocked.")
  2272. (defvar org-trigger-hook nil
  2273. "Hook for functions that are triggered by a state change.
  2274. Each function gets as its single argument a property list with at
  2275. least the following elements:
  2276. (:type type-of-change :position pos-at-entry-start
  2277. :from old-state :to new-state)
  2278. Depending on the type, more properties may be present.
  2279. This mechanism is currently implemented for:
  2280. TODO state changes
  2281. ------------------
  2282. :type todo-state-change
  2283. :from previous state (keyword as a string), or nil, or a symbol
  2284. 'todo' or 'done', to indicate the general type of state.
  2285. :to new state, like in :from")
  2286. (defcustom org-enforce-todo-dependencies nil
  2287. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2288. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2289. be blocked if any prior sibling is not yet done.
  2290. Finally, if the parent is blocked because of ordered siblings of its own,
  2291. the child will also be blocked."
  2292. :set (lambda (var val)
  2293. (set var val)
  2294. (if val
  2295. (add-hook 'org-blocker-hook
  2296. 'org-block-todo-from-children-or-siblings-or-parent)
  2297. (remove-hook 'org-blocker-hook
  2298. 'org-block-todo-from-children-or-siblings-or-parent)))
  2299. :group 'org-todo
  2300. :type 'boolean)
  2301. (defcustom org-enforce-todo-checkbox-dependencies nil
  2302. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2303. When this is nil, checkboxes have no influence on switching TODO states.
  2304. When non-nil, you first need to check off all check boxes before the TODO
  2305. entry can be switched to DONE.
  2306. This variable needs to be set before org.el is loaded, and you need to
  2307. restart Emacs after a change to make the change effective. The only way
  2308. to change is while Emacs is running is through the customize interface."
  2309. :set (lambda (var val)
  2310. (set var val)
  2311. (if val
  2312. (add-hook 'org-blocker-hook
  2313. 'org-block-todo-from-checkboxes)
  2314. (remove-hook 'org-blocker-hook
  2315. 'org-block-todo-from-checkboxes)))
  2316. :group 'org-todo
  2317. :type 'boolean)
  2318. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2319. "Non-nil means inserting a TODO heading is treated as state change.
  2320. So when the command \\[org-insert-todo-heading] is used, state change
  2321. logging will apply if appropriate. When nil, the new TODO item will
  2322. be inserted directly, and no logging will take place."
  2323. :group 'org-todo
  2324. :type 'boolean)
  2325. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2326. "Non-nil means switching TODO states with S-cursor counts as state change.
  2327. This is the default behavior. However, setting this to nil allows a
  2328. convenient way to select a TODO state and bypass any logging associated
  2329. with that."
  2330. :group 'org-todo
  2331. :type 'boolean)
  2332. (defcustom org-todo-state-tags-triggers nil
  2333. "Tag changes that should be triggered by TODO state changes.
  2334. This is a list. Each entry is
  2335. (state-change (tag . flag) .......)
  2336. State-change can be a string with a state, and empty string to indicate the
  2337. state that has no TODO keyword, or it can be one of the symbols `todo'
  2338. or `done', meaning any not-done or done state, respectively."
  2339. :group 'org-todo
  2340. :group 'org-tags
  2341. :type '(repeat
  2342. (cons (choice :tag "When changing to"
  2343. (const :tag "Not-done state" todo)
  2344. (const :tag "Done state" done)
  2345. (string :tag "State"))
  2346. (repeat
  2347. (cons :tag "Tag action"
  2348. (string :tag "Tag")
  2349. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2350. (defcustom org-log-done nil
  2351. "Information to record when a task moves to the DONE state.
  2352. Possible values are:
  2353. nil Don't add anything, just change the keyword
  2354. time Add a time stamp to the task
  2355. note Prompt for a note and add it with template `org-log-note-headings'
  2356. This option can also be set with on a per-file-basis with
  2357. #+STARTUP: nologdone
  2358. #+STARTUP: logdone
  2359. #+STARTUP: lognotedone
  2360. You can have local logging settings for a subtree by setting the LOGGING
  2361. property to one or more of these keywords."
  2362. :group 'org-todo
  2363. :group 'org-progress
  2364. :type '(choice
  2365. (const :tag "No logging" nil)
  2366. (const :tag "Record CLOSED timestamp" time)
  2367. (const :tag "Record CLOSED timestamp with note." note)))
  2368. ;; Normalize old uses of org-log-done.
  2369. (cond
  2370. ((eq org-log-done t) (setq org-log-done 'time))
  2371. ((and (listp org-log-done) (memq 'done org-log-done))
  2372. (setq org-log-done 'note)))
  2373. (defcustom org-log-reschedule nil
  2374. "Information to record when the scheduling date of a tasks is modified.
  2375. Possible values are:
  2376. nil Don't add anything, just change the date
  2377. time Add a time stamp to the task
  2378. note Prompt for a note and add it with template `org-log-note-headings'
  2379. This option can also be set with on a per-file-basis with
  2380. #+STARTUP: nologreschedule
  2381. #+STARTUP: logreschedule
  2382. #+STARTUP: lognotereschedule"
  2383. :group 'org-todo
  2384. :group 'org-progress
  2385. :type '(choice
  2386. (const :tag "No logging" nil)
  2387. (const :tag "Record timestamp" time)
  2388. (const :tag "Record timestamp with note." note)))
  2389. (defcustom org-log-redeadline nil
  2390. "Information to record when the deadline date of a tasks is modified.
  2391. Possible values are:
  2392. nil Don't add anything, just change the date
  2393. time Add a time stamp to the task
  2394. note Prompt for a note and add it with template `org-log-note-headings'
  2395. This option can also be set with on a per-file-basis with
  2396. #+STARTUP: nologredeadline
  2397. #+STARTUP: logredeadline
  2398. #+STARTUP: lognoteredeadline
  2399. You can have local logging settings for a subtree by setting the LOGGING
  2400. property to one or more of these keywords."
  2401. :group 'org-todo
  2402. :group 'org-progress
  2403. :type '(choice
  2404. (const :tag "No logging" nil)
  2405. (const :tag "Record timestamp" time)
  2406. (const :tag "Record timestamp with note." note)))
  2407. (defcustom org-log-note-clock-out nil
  2408. "Non-nil means record a note when clocking out of an item.
  2409. This can also be configured on a per-file basis by adding one of
  2410. the following lines anywhere in the buffer:
  2411. #+STARTUP: lognoteclock-out
  2412. #+STARTUP: nolognoteclock-out"
  2413. :group 'org-todo
  2414. :group 'org-progress
  2415. :type 'boolean)
  2416. (defcustom org-log-done-with-time t
  2417. "Non-nil means the CLOSED time stamp will contain date and time.
  2418. When nil, only the date will be recorded."
  2419. :group 'org-progress
  2420. :type 'boolean)
  2421. (defcustom org-log-note-headings
  2422. '((done . "CLOSING NOTE %t")
  2423. (state . "State %-12s from %-12S %t")
  2424. (note . "Note taken on %t")
  2425. (reschedule . "Rescheduled from %S on %t")
  2426. (delschedule . "Not scheduled, was %S on %t")
  2427. (redeadline . "New deadline from %S on %t")
  2428. (deldeadline . "Removed deadline, was %S on %t")
  2429. (refile . "Refiled on %t")
  2430. (clock-out . ""))
  2431. "Headings for notes added to entries.
  2432. The value is an alist, with the car being a symbol indicating the note
  2433. context, and the cdr is the heading to be used. The heading may also be the
  2434. empty string.
  2435. %t in the heading will be replaced by a time stamp.
  2436. %T will be an active time stamp instead the default inactive one
  2437. %d will be replaced by a short-format time stamp.
  2438. %D will be replaced by an active short-format time stamp.
  2439. %s will be replaced by the new TODO state, in double quotes.
  2440. %S will be replaced by the old TODO state, in double quotes.
  2441. %u will be replaced by the user name.
  2442. %U will be replaced by the full user name.
  2443. In fact, it is not a good idea to change the `state' entry, because
  2444. agenda log mode depends on the format of these entries."
  2445. :group 'org-todo
  2446. :group 'org-progress
  2447. :type '(list :greedy t
  2448. (cons (const :tag "Heading when closing an item" done) string)
  2449. (cons (const :tag
  2450. "Heading when changing todo state (todo sequence only)"
  2451. state) string)
  2452. (cons (const :tag "Heading when just taking a note" note) string)
  2453. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2454. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2455. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2456. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2457. (cons (const :tag "Heading when refiling" refile) string)
  2458. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2459. (unless (assq 'note org-log-note-headings)
  2460. (push '(note . "%t") org-log-note-headings))
  2461. (defcustom org-log-into-drawer nil
  2462. "Non-nil means insert state change notes and time stamps into a drawer.
  2463. When nil, state changes notes will be inserted after the headline and
  2464. any scheduling and clock lines, but not inside a drawer.
  2465. The value of this variable should be the name of the drawer to use.
  2466. LOGBOOK is proposed as the default drawer for this purpose, you can
  2467. also set this to a string to define the drawer of your choice.
  2468. A value of t is also allowed, representing \"LOGBOOK\".
  2469. A value of t or nil can also be set with on a per-file-basis with
  2470. #+STARTUP: logdrawer
  2471. #+STARTUP: nologdrawer
  2472. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2473. will be ignored.
  2474. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2475. a subtree."
  2476. :group 'org-todo
  2477. :group 'org-progress
  2478. :type '(choice
  2479. (const :tag "Not into a drawer" nil)
  2480. (const :tag "LOGBOOK" t)
  2481. (string :tag "Other")))
  2482. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2483. (defun org-log-into-drawer ()
  2484. "Return the value of `org-log-into-drawer', but let properties overrule.
  2485. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2486. used instead of the default value."
  2487. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2488. (cond
  2489. ((not p) org-log-into-drawer)
  2490. ((equal p "nil") nil)
  2491. ((equal p "t") "LOGBOOK")
  2492. (t p))))
  2493. (defcustom org-log-state-notes-insert-after-drawers nil
  2494. "Non-nil means insert state change notes after any drawers in entry.
  2495. Only the drawers that *immediately* follow the headline and the
  2496. deadline/scheduled line are skipped.
  2497. When nil, insert notes right after the heading and perhaps the line
  2498. with deadline/scheduling if present.
  2499. This variable will have no effect if `org-log-into-drawer' is
  2500. set."
  2501. :group 'org-todo
  2502. :group 'org-progress
  2503. :type 'boolean)
  2504. (defcustom org-log-states-order-reversed t
  2505. "Non-nil means the latest state note will be directly after heading.
  2506. When nil, the state change notes will be ordered according to time.
  2507. This option can also be set with on a per-file-basis with
  2508. #+STARTUP: logstatesreversed
  2509. #+STARTUP: nologstatesreversed"
  2510. :group 'org-todo
  2511. :group 'org-progress
  2512. :type 'boolean)
  2513. (defcustom org-todo-repeat-to-state nil
  2514. "The TODO state to which a repeater should return the repeating task.
  2515. By default this is the first task in a TODO sequence, or the previous state
  2516. in a TODO_TYP set. But you can specify another task here.
  2517. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2518. :group 'org-todo
  2519. :version "24.1"
  2520. :type '(choice (const :tag "Head of sequence" nil)
  2521. (string :tag "Specific state")))
  2522. (defcustom org-log-repeat 'time
  2523. "Non-nil means record moving through the DONE state when triggering repeat.
  2524. An auto-repeating task is immediately switched back to TODO when
  2525. marked DONE. If you are not logging state changes (by adding \"@\"
  2526. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2527. record a closing note, there will be no record of the task moving
  2528. through DONE. This variable forces taking a note anyway.
  2529. nil Don't force a record
  2530. time Record a time stamp
  2531. note Prompt for a note and add it with template `org-log-note-headings'
  2532. This option can also be set with on a per-file-basis with
  2533. #+STARTUP: nologrepeat
  2534. #+STARTUP: logrepeat
  2535. #+STARTUP: lognoterepeat
  2536. You can have local logging settings for a subtree by setting the LOGGING
  2537. property to one or more of these keywords."
  2538. :group 'org-todo
  2539. :group 'org-progress
  2540. :type '(choice
  2541. (const :tag "Don't force a record" nil)
  2542. (const :tag "Force recording the DONE state" time)
  2543. (const :tag "Force recording a note with the DONE state" note)))
  2544. (defgroup org-priorities nil
  2545. "Priorities in Org-mode."
  2546. :tag "Org Priorities"
  2547. :group 'org-todo)
  2548. (defcustom org-enable-priority-commands t
  2549. "Non-nil means priority commands are active.
  2550. When nil, these commands will be disabled, so that you never accidentally
  2551. set a priority."
  2552. :group 'org-priorities
  2553. :type 'boolean)
  2554. (defcustom org-highest-priority ?A
  2555. "The highest priority of TODO items. A character like ?A, ?B etc.
  2556. Must have a smaller ASCII number than `org-lowest-priority'."
  2557. :group 'org-priorities
  2558. :type 'character)
  2559. (defcustom org-lowest-priority ?C
  2560. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2561. Must have a larger ASCII number than `org-highest-priority'."
  2562. :group 'org-priorities
  2563. :type 'character)
  2564. (defcustom org-default-priority ?B
  2565. "The default priority of TODO items.
  2566. This is the priority an item gets if no explicit priority is given.
  2567. When starting to cycle on an empty priority the first step in the cycle
  2568. depends on `org-priority-start-cycle-with-default'. The resulting first
  2569. step priority must not exceed the range from `org-highest-priority' to
  2570. `org-lowest-priority' which means that `org-default-priority' has to be
  2571. in this range exclusive or inclusive the range boundaries. Else the
  2572. first step refuses to set the default and the second will fall back
  2573. to (depending on the command used) the highest or lowest priority."
  2574. :group 'org-priorities
  2575. :type 'character)
  2576. (defcustom org-priority-start-cycle-with-default t
  2577. "Non-nil means start with default priority when starting to cycle.
  2578. When this is nil, the first step in the cycle will be (depending on the
  2579. command used) one higher or lower than the default priority.
  2580. See also `org-default-priority'."
  2581. :group 'org-priorities
  2582. :type 'boolean)
  2583. (defcustom org-get-priority-function nil
  2584. "Function to extract the priority from a string.
  2585. The string is normally the headline. If this is nil Org computes the
  2586. priority from the priority cookie like [#A] in the headline. It returns
  2587. an integer, increasing by 1000 for each priority level.
  2588. The user can set a different function here, which should take a string
  2589. as an argument and return the numeric priority."
  2590. :group 'org-priorities
  2591. :version "24.1"
  2592. :type '(choice
  2593. (const nil)
  2594. (function)))
  2595. (defgroup org-time nil
  2596. "Options concerning time stamps and deadlines in Org-mode."
  2597. :tag "Org Time"
  2598. :group 'org)
  2599. (defcustom org-insert-labeled-timestamps-at-point nil
  2600. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2601. When nil, these labeled time stamps are forces into the second line of an
  2602. entry, just after the headline. When scheduling from the global TODO list,
  2603. the time stamp will always be forced into the second line."
  2604. :group 'org-time
  2605. :type 'boolean)
  2606. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2607. "Number of minutes to round time stamps to.
  2608. These are two values, the first applies when first creating a time stamp.
  2609. The second applies when changing it with the commands `S-up' and `S-down'.
  2610. When changing the time stamp, this means that it will change in steps
  2611. of N minutes, as given by the second value.
  2612. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2613. numbers should be factors of 60, so for example 5, 10, 15.
  2614. When this is larger than 1, you can still force an exact time stamp by using
  2615. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2616. and by using a prefix arg to `S-up/down' to specify the exact number
  2617. of minutes to shift."
  2618. :group 'org-time
  2619. :get #'(lambda (var) ; Make sure both elements are there
  2620. (if (integerp (default-value var))
  2621. (list (default-value var) 5)
  2622. (default-value var)))
  2623. :type '(list
  2624. (integer :tag "when inserting times")
  2625. (integer :tag "when modifying times")))
  2626. ;; Normalize old customizations of this variable.
  2627. (when (integerp org-time-stamp-rounding-minutes)
  2628. (setq org-time-stamp-rounding-minutes
  2629. (list org-time-stamp-rounding-minutes
  2630. org-time-stamp-rounding-minutes)))
  2631. (defcustom org-display-custom-times nil
  2632. "Non-nil means overlay custom formats over all time stamps.
  2633. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2634. To turn this on on a per-file basis, insert anywhere in the file:
  2635. #+STARTUP: customtime"
  2636. :group 'org-time
  2637. :set 'set-default
  2638. :type 'sexp)
  2639. (make-variable-buffer-local 'org-display-custom-times)
  2640. (defcustom org-time-stamp-custom-formats
  2641. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2642. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2643. These are overlaid over the default ISO format if the variable
  2644. `org-display-custom-times' is set. Time like %H:%M should be at the
  2645. end of the second format. The custom formats are also honored by export
  2646. commands, if custom time display is turned on at the time of export."
  2647. :group 'org-time
  2648. :type 'sexp)
  2649. (defun org-time-stamp-format (&optional long inactive)
  2650. "Get the right format for a time string."
  2651. (let ((f (if long (cdr org-time-stamp-formats)
  2652. (car org-time-stamp-formats))))
  2653. (if inactive
  2654. (concat "[" (substring f 1 -1) "]")
  2655. f)))
  2656. (defcustom org-time-clocksum-format
  2657. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2658. "The format string used when creating CLOCKSUM lines.
  2659. This is also used when Org mode generates a time duration.
  2660. The value can be a single format string containing two
  2661. %-sequences, which will be filled with the number of hours and
  2662. minutes in that order.
  2663. Alternatively, the value can be a plist associating any of the
  2664. keys :years, :months, :weeks, :days, :hours or :minutes with
  2665. format strings. The time duration is formatted using only the
  2666. time components that are needed and concatenating the results.
  2667. If a time unit in absent, it falls back to the next smallest
  2668. unit.
  2669. The keys :require-years, :require-months, :require-days,
  2670. :require-weeks, :require-hours, :require-minutes are also
  2671. meaningful. A non-nil value for these keys indicates that the
  2672. corresponding time component should always be included, even if
  2673. its value is 0.
  2674. For example,
  2675. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2676. :require-minutes t)
  2677. means durations longer than a day will be expressed in days,
  2678. hours and minutes, and durations less than a day will always be
  2679. expressed in hours and minutes (even for durations less than an
  2680. hour).
  2681. The value
  2682. \(:days \"%dd\" :minutes \"%dm\")
  2683. means durations longer than a day will be expressed in days and
  2684. minutes, and durations less than a day will be expressed entirely
  2685. in minutes (even for durations longer than an hour)."
  2686. :group 'org-time
  2687. :group 'org-clock
  2688. :version "24.4"
  2689. :package-version '(Org . "8.0")
  2690. :type '(choice (string :tag "Format string")
  2691. (set :tag "Plist"
  2692. (group :inline t (const :tag "Years" :years)
  2693. (string :tag "Format string"))
  2694. (group :inline t
  2695. (const :tag "Always show years" :require-years)
  2696. (const t))
  2697. (group :inline t (const :tag "Months" :months)
  2698. (string :tag "Format string"))
  2699. (group :inline t
  2700. (const :tag "Always show months" :require-months)
  2701. (const t))
  2702. (group :inline t (const :tag "Weeks" :weeks)
  2703. (string :tag "Format string"))
  2704. (group :inline t
  2705. (const :tag "Always show weeks" :require-weeks)
  2706. (const t))
  2707. (group :inline t (const :tag "Days" :days)
  2708. (string :tag "Format string"))
  2709. (group :inline t
  2710. (const :tag "Always show days" :require-days)
  2711. (const t))
  2712. (group :inline t (const :tag "Hours" :hours)
  2713. (string :tag "Format string"))
  2714. (group :inline t
  2715. (const :tag "Always show hours" :require-hours)
  2716. (const t))
  2717. (group :inline t (const :tag "Minutes" :minutes)
  2718. (string :tag "Format string"))
  2719. (group :inline t
  2720. (const :tag "Always show minutes" :require-minutes)
  2721. (const t)))))
  2722. (defcustom org-time-clocksum-use-fractional nil
  2723. "When non-nil, \\[org-clock-display] uses fractional times.
  2724. See `org-time-clocksum-format' for more on time clock formats."
  2725. :group 'org-time
  2726. :group 'org-clock
  2727. :version "24.3"
  2728. :type 'boolean)
  2729. (defcustom org-time-clocksum-use-effort-durations nil
  2730. "When non-nil, \\[org-clock-display] uses effort durations.
  2731. E.g. by default, one day is considered to be a 8 hours effort,
  2732. so a task that has been clocked for 16 hours will be displayed
  2733. as during 2 days in the clock display or in the clocktable.
  2734. See `org-effort-durations' on how to set effort durations
  2735. and `org-time-clocksum-format' for more on time clock formats."
  2736. :group 'org-time
  2737. :group 'org-clock
  2738. :version "24.4"
  2739. :package-version '(Org . "8.0")
  2740. :type 'boolean)
  2741. (defcustom org-time-clocksum-fractional-format "%.2f"
  2742. "The format string used when creating CLOCKSUM lines,
  2743. or when Org mode generates a time duration, if
  2744. `org-time-clocksum-use-fractional' is enabled.
  2745. The value can be a single format string containing one
  2746. %-sequence, which will be filled with the number of hours as
  2747. a float.
  2748. Alternatively, the value can be a plist associating any of the
  2749. keys :years, :months, :weeks, :days, :hours or :minutes with
  2750. a format string. The time duration is formatted using the
  2751. largest time unit which gives a non-zero integer part. If all
  2752. specified formats have zero integer part, the smallest time unit
  2753. is used."
  2754. :group 'org-time
  2755. :type '(choice (string :tag "Format string")
  2756. (set (group :inline t (const :tag "Years" :years)
  2757. (string :tag "Format string"))
  2758. (group :inline t (const :tag "Months" :months)
  2759. (string :tag "Format string"))
  2760. (group :inline t (const :tag "Weeks" :weeks)
  2761. (string :tag "Format string"))
  2762. (group :inline t (const :tag "Days" :days)
  2763. (string :tag "Format string"))
  2764. (group :inline t (const :tag "Hours" :hours)
  2765. (string :tag "Format string"))
  2766. (group :inline t (const :tag "Minutes" :minutes)
  2767. (string :tag "Format string")))))
  2768. (defcustom org-deadline-warning-days 14
  2769. "Number of days before expiration during which a deadline becomes active.
  2770. This variable governs the display in sparse trees and in the agenda.
  2771. When 0 or negative, it means use this number (the absolute value of it)
  2772. even if a deadline has a different individual lead time specified.
  2773. Custom commands can set this variable in the options section."
  2774. :group 'org-time
  2775. :group 'org-agenda-daily/weekly
  2776. :type 'integer)
  2777. (defcustom org-scheduled-delay-days 0
  2778. "Number of days before a scheduled item becomes active.
  2779. This variable governs the display in sparse trees and in the agenda.
  2780. The default value (i.e. 0) means: don't delay scheduled item.
  2781. When negative, it means use this number (the absolute value of it)
  2782. even if a scheduled item has a different individual delay time
  2783. specified.
  2784. Custom commands can set this variable in the options section."
  2785. :group 'org-time
  2786. :group 'org-agenda-daily/weekly
  2787. :version "24.4"
  2788. :package-version '(Org . "8.0")
  2789. :type 'integer)
  2790. (defcustom org-read-date-prefer-future t
  2791. "Non-nil means assume future for incomplete date input from user.
  2792. This affects the following situations:
  2793. 1. The user gives a month but not a year.
  2794. For example, if it is April and you enter \"feb 2\", this will be read
  2795. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2796. 2. The user gives a day, but no month.
  2797. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2798. read this as the third of *next* month. However, if you enter \"17\",
  2799. it will be considered as *this* month.
  2800. If you set this variable to the symbol `time', then also the following
  2801. will work:
  2802. 3. If the user gives a time.
  2803. If the time is before now, it will be interpreted as tomorrow.
  2804. Currently none of this works for ISO week specifications.
  2805. When this option is nil, the current day, month and year will always be
  2806. used as defaults.
  2807. See also `org-agenda-jump-prefer-future'."
  2808. :group 'org-time
  2809. :type '(choice
  2810. (const :tag "Never" nil)
  2811. (const :tag "Check month and day" t)
  2812. (const :tag "Check month, day, and time" time)))
  2813. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2814. "Should the agenda jump command prefer the future for incomplete dates?
  2815. The default is to do the same as configured in `org-read-date-prefer-future'.
  2816. But you can also set a deviating value here.
  2817. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2818. :group 'org-agenda
  2819. :group 'org-time
  2820. :version "24.1"
  2821. :type '(choice
  2822. (const :tag "Use org-read-date-prefer-future"
  2823. org-read-date-prefer-future)
  2824. (const :tag "Never" nil)
  2825. (const :tag "Always" t)))
  2826. (defcustom org-read-date-force-compatible-dates t
  2827. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2828. Depending on the system Emacs is running on, certain dates cannot
  2829. be represented with the type used internally to represent time.
  2830. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2831. correctly. Some systems allow for earlier dates, some for later,
  2832. some for both. One way to find out it to insert any date into an
  2833. Org buffer, putting the cursor on the year and hitting S-up and
  2834. S-down to test the range.
  2835. When this variable is set to t, the date/time prompt will not let
  2836. you specify dates outside the 1970-2037 range, so it is certain that
  2837. these dates will work in whatever version of Emacs you are
  2838. running, and also that you can move a file from one Emacs implementation
  2839. to another. WHenever Org is forcing the year for you, it will display
  2840. a message and beep.
  2841. When this variable is nil, Org will check if the date is
  2842. representable in the specific Emacs implementation you are using.
  2843. If not, it will force a year, usually the current year, and beep
  2844. to remind you. Currently this setting is not recommended because
  2845. the likelihood that you will open your Org files in an Emacs that
  2846. has limited date range is not negligible.
  2847. A workaround for this problem is to use diary sexp dates for time
  2848. stamps outside of this range."
  2849. :group 'org-time
  2850. :version "24.1"
  2851. :type 'boolean)
  2852. (defcustom org-read-date-display-live t
  2853. "Non-nil means display current interpretation of date prompt live.
  2854. This display will be in an overlay, in the minibuffer."
  2855. :group 'org-time
  2856. :type 'boolean)
  2857. (defcustom org-read-date-popup-calendar t
  2858. "Non-nil means pop up a calendar when prompting for a date.
  2859. In the calendar, the date can be selected with mouse-1. However, the
  2860. minibuffer will also be active, and you can simply enter the date as well.
  2861. When nil, only the minibuffer will be available."
  2862. :group 'org-time
  2863. :type 'boolean)
  2864. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2865. 'org-read-date-popup-calendar)
  2866. (make-obsolete-variable
  2867. 'org-read-date-minibuffer-setup-hook
  2868. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2869. (defcustom org-read-date-minibuffer-setup-hook nil
  2870. "Hook to be used to set up keys for the date/time interface.
  2871. Add key definitions to `minibuffer-local-map', which will be a
  2872. temporary copy.
  2873. WARNING: This option is obsolete, you should use
  2874. `org-read-date-minibuffer-local-map' to set up keys."
  2875. :group 'org-time
  2876. :type 'hook)
  2877. (defcustom org-extend-today-until 0
  2878. "The hour when your day really ends. Must be an integer.
  2879. This has influence for the following applications:
  2880. - When switching the agenda to \"today\". It it is still earlier than
  2881. the time given here, the day recognized as TODAY is actually yesterday.
  2882. - When a date is read from the user and it is still before the time given
  2883. here, the current date and time will be assumed to be yesterday, 23:59.
  2884. Also, timestamps inserted in capture templates follow this rule.
  2885. IMPORTANT: This is a feature whose implementation is and likely will
  2886. remain incomplete. Really, it is only here because past midnight seems to
  2887. be the favorite working time of John Wiegley :-)"
  2888. :group 'org-time
  2889. :type 'integer)
  2890. (defcustom org-use-effective-time nil
  2891. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2892. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2893. \"effective time\" of any timestamps between midnight and 8am will be
  2894. 23:59 of the previous day."
  2895. :group 'org-time
  2896. :version "24.1"
  2897. :type 'boolean)
  2898. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2899. "When non-nil, use the last clock out time for `org-todo'.
  2900. Note that this option has precedence over the combined use of
  2901. `org-use-effective-time' and `org-extend-today-until'."
  2902. :group 'org-time
  2903. :version "24.4"
  2904. :package-version '(Org . "8.0")
  2905. :type 'boolean)
  2906. (defcustom org-edit-timestamp-down-means-later nil
  2907. "Non-nil means S-down will increase the time in a time stamp.
  2908. When nil, S-up will increase."
  2909. :group 'org-time
  2910. :type 'boolean)
  2911. (defcustom org-calendar-follow-timestamp-change t
  2912. "Non-nil means make the calendar window follow timestamp changes.
  2913. When a timestamp is modified and the calendar window is visible, it will be
  2914. moved to the new date."
  2915. :group 'org-time
  2916. :type 'boolean)
  2917. (defgroup org-tags nil
  2918. "Options concerning tags in Org-mode."
  2919. :tag "Org Tags"
  2920. :group 'org)
  2921. (defcustom org-tag-alist nil
  2922. "List of tags allowed in Org-mode files.
  2923. When this list is nil, Org-mode will base TAG input on what is already in the
  2924. buffer.
  2925. The value of this variable is an alist, the car of each entry must be a
  2926. keyword as a string, the cdr may be a character that is used to select
  2927. that tag through the fast-tag-selection interface.
  2928. See the manual for details."
  2929. :group 'org-tags
  2930. :type '(repeat
  2931. (choice
  2932. (cons (string :tag "Tag name")
  2933. (character :tag "Access char"))
  2934. (list :tag "Start radio group"
  2935. (const :startgroup)
  2936. (option (string :tag "Group description")))
  2937. (list :tag "Group tags delimiter"
  2938. (const :grouptags))
  2939. (list :tag "End radio group"
  2940. (const :endgroup)
  2941. (option (string :tag "Group description")))
  2942. (const :tag "New line" (:newline)))))
  2943. (defcustom org-tag-persistent-alist nil
  2944. "List of tags that will always appear in all Org-mode files.
  2945. This is in addition to any in buffer settings or customizations
  2946. of `org-tag-alist'.
  2947. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2948. The value of this variable is an alist, the car of each entry must be a
  2949. keyword as a string, the cdr may be a character that is used to select
  2950. that tag through the fast-tag-selection interface.
  2951. See the manual for details.
  2952. To disable these tags on a per-file basis, insert anywhere in the file:
  2953. #+STARTUP: noptag"
  2954. :group 'org-tags
  2955. :type '(repeat
  2956. (choice
  2957. (cons (string :tag "Tag name")
  2958. (character :tag "Access char"))
  2959. (const :tag "Start radio group" (:startgroup))
  2960. (const :tag "Group tags delimiter" (:grouptags))
  2961. (const :tag "End radio group" (:endgroup))
  2962. (const :tag "New line" (:newline)))))
  2963. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2964. "If non-nil, always offer completion for all tags of all agenda files.
  2965. Instead of customizing this variable directly, you might want to
  2966. set it locally for capture buffers, because there no list of
  2967. tags in that file can be created dynamically (there are none).
  2968. (add-hook 'org-capture-mode-hook
  2969. (lambda ()
  2970. (set (make-local-variable
  2971. 'org-complete-tags-always-offer-all-agenda-tags)
  2972. t)))"
  2973. :group 'org-tags
  2974. :version "24.1"
  2975. :type 'boolean)
  2976. (defvar org-file-tags nil
  2977. "List of tags that can be inherited by all entries in the file.
  2978. The tags will be inherited if the variable `org-use-tag-inheritance'
  2979. says they should be.
  2980. This variable is populated from #+FILETAGS lines.")
  2981. (defcustom org-use-fast-tag-selection 'auto
  2982. "Non-nil means use fast tag selection scheme.
  2983. This is a special interface to select and deselect tags with single keys.
  2984. When nil, fast selection is never used.
  2985. When the symbol `auto', fast selection is used if and only if selection
  2986. characters for tags have been configured, either through the variable
  2987. `org-tag-alist' or through a #+TAGS line in the buffer.
  2988. When t, fast selection is always used and selection keys are assigned
  2989. automatically if necessary."
  2990. :group 'org-tags
  2991. :type '(choice
  2992. (const :tag "Always" t)
  2993. (const :tag "Never" nil)
  2994. (const :tag "When selection characters are configured" auto)))
  2995. (defcustom org-fast-tag-selection-single-key nil
  2996. "Non-nil means fast tag selection exits after first change.
  2997. When nil, you have to press RET to exit it.
  2998. During fast tag selection, you can toggle this flag with `C-c'.
  2999. This variable can also have the value `expert'. In this case, the window
  3000. displaying the tags menu is not even shown, until you press C-c again."
  3001. :group 'org-tags
  3002. :type '(choice
  3003. (const :tag "No" nil)
  3004. (const :tag "Yes" t)
  3005. (const :tag "Expert" expert)))
  3006. (defvar org-fast-tag-selection-include-todo nil
  3007. "Non-nil means fast tags selection interface will also offer TODO states.
  3008. This is an undocumented feature, you should not rely on it.")
  3009. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3010. "The column to which tags should be indented in a headline.
  3011. If this number is positive, it specifies the column. If it is negative,
  3012. it means that the tags should be flushright to that column. For example,
  3013. -80 works well for a normal 80 character screen.
  3014. When 0, place tags directly after headline text, with only one space in
  3015. between."
  3016. :group 'org-tags
  3017. :type 'integer)
  3018. (defcustom org-auto-align-tags t
  3019. "Non-nil keeps tags aligned when modifying headlines.
  3020. Some operations (i.e. demoting) change the length of a headline and
  3021. therefore shift the tags around. With this option turned on, after
  3022. each such operation the tags are again aligned to `org-tags-column'."
  3023. :group 'org-tags
  3024. :type 'boolean)
  3025. (defcustom org-use-tag-inheritance t
  3026. "Non-nil means tags in levels apply also for sublevels.
  3027. When nil, only the tags directly given in a specific line apply there.
  3028. This may also be a list of tags that should be inherited, or a regexp that
  3029. matches tags that should be inherited. Additional control is possible
  3030. with the variable `org-tags-exclude-from-inheritance' which gives an
  3031. explicit list of tags to be excluded from inheritance, even if the value of
  3032. `org-use-tag-inheritance' would select it for inheritance.
  3033. If this option is t, a match early-on in a tree can lead to a large
  3034. number of matches in the subtree when constructing the agenda or creating
  3035. a sparse tree. If you only want to see the first match in a tree during
  3036. a search, check out the variable `org-tags-match-list-sublevels'."
  3037. :group 'org-tags
  3038. :type '(choice
  3039. (const :tag "Not" nil)
  3040. (const :tag "Always" t)
  3041. (repeat :tag "Specific tags" (string :tag "Tag"))
  3042. (regexp :tag "Tags matched by regexp")))
  3043. (defcustom org-tags-exclude-from-inheritance nil
  3044. "List of tags that should never be inherited.
  3045. This is a way to exclude a few tags from inheritance. For way to do
  3046. the opposite, to actively allow inheritance for selected tags,
  3047. see the variable `org-use-tag-inheritance'."
  3048. :group 'org-tags
  3049. :type '(repeat (string :tag "Tag")))
  3050. (defun org-tag-inherit-p (tag)
  3051. "Check if TAG is one that should be inherited."
  3052. (cond
  3053. ((member tag org-tags-exclude-from-inheritance) nil)
  3054. ((eq org-use-tag-inheritance t) t)
  3055. ((not org-use-tag-inheritance) nil)
  3056. ((stringp org-use-tag-inheritance)
  3057. (string-match org-use-tag-inheritance tag))
  3058. ((listp org-use-tag-inheritance)
  3059. (member tag org-use-tag-inheritance))
  3060. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3061. (defcustom org-tags-match-list-sublevels t
  3062. "Non-nil means list also sublevels of headlines matching a search.
  3063. This variable applies to tags/property searches, and also to stuck
  3064. projects because this search is based on a tags match as well.
  3065. When set to the symbol `indented', sublevels are indented with
  3066. leading dots.
  3067. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3068. the sublevels of a headline matching a tag search often also match
  3069. the same search. Listing all of them can create very long lists.
  3070. Setting this variable to nil causes subtrees of a match to be skipped.
  3071. This variable is semi-obsolete and probably should always be true. It
  3072. is better to limit inheritance to certain tags using the variables
  3073. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3074. :group 'org-tags
  3075. :type '(choice
  3076. (const :tag "No, don't list them" nil)
  3077. (const :tag "Yes, do list them" t)
  3078. (const :tag "List them, indented with leading dots" indented)))
  3079. (defcustom org-tags-sort-function nil
  3080. "When set, tags are sorted using this function as a comparator."
  3081. :group 'org-tags
  3082. :type '(choice
  3083. (const :tag "No sorting" nil)
  3084. (const :tag "Alphabetical" string<)
  3085. (const :tag "Reverse alphabetical" string>)
  3086. (function :tag "Custom function" nil)))
  3087. (defvar org-tags-history nil
  3088. "History of minibuffer reads for tags.")
  3089. (defvar org-last-tags-completion-table nil
  3090. "The last used completion table for tags.")
  3091. (defvar org-after-tags-change-hook nil
  3092. "Hook that is run after the tags in a line have changed.")
  3093. (defgroup org-properties nil
  3094. "Options concerning properties in Org-mode."
  3095. :tag "Org Properties"
  3096. :group 'org)
  3097. (defcustom org-property-format "%-10s %s"
  3098. "How property key/value pairs should be formatted by `indent-line'.
  3099. When `indent-line' hits a property definition, it will format the line
  3100. according to this format, mainly to make sure that the values are
  3101. lined-up with respect to each other."
  3102. :group 'org-properties
  3103. :type 'string)
  3104. (defcustom org-properties-postprocess-alist nil
  3105. "Alist of properties and functions to adjust inserted values.
  3106. Elements of this alist must be of the form
  3107. ([string] [function])
  3108. where [string] must be a property name and [function] must be a
  3109. lambda expression: this lambda expression must take one argument,
  3110. the value to adjust, and return the new value as a string.
  3111. For example, this element will allow the property \"Remaining\"
  3112. to be updated wrt the relation between the \"Effort\" property
  3113. and the clock summary:
  3114. ((\"Remaining\" (lambda(value)
  3115. (let ((clocksum (org-clock-sum-current-item))
  3116. (effort (org-duration-string-to-minutes
  3117. (org-entry-get (point) \"Effort\"))))
  3118. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3119. :group 'org-properties
  3120. :version "24.1"
  3121. :type '(alist :key-type (string :tag "Property")
  3122. :value-type (function :tag "Function")))
  3123. (defcustom org-use-property-inheritance nil
  3124. "Non-nil means properties apply also for sublevels.
  3125. This setting is chiefly used during property searches. Turning it on can
  3126. cause significant overhead when doing a search, which is why it is not
  3127. on by default.
  3128. When nil, only the properties directly given in the current entry count.
  3129. When t, every property is inherited. The value may also be a list of
  3130. properties that should have inheritance, or a regular expression matching
  3131. properties that should be inherited.
  3132. However, note that some special properties use inheritance under special
  3133. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3134. and the properties ending in \"_ALL\" when they are used as descriptor
  3135. for valid values of a property.
  3136. Note for programmers:
  3137. When querying an entry with `org-entry-get', you can control if inheritance
  3138. should be used. By default, `org-entry-get' looks only at the local
  3139. properties. You can request inheritance by setting the inherit argument
  3140. to t (to force inheritance) or to `selective' (to respect the setting
  3141. in this variable)."
  3142. :group 'org-properties
  3143. :type '(choice
  3144. (const :tag "Not" nil)
  3145. (const :tag "Always" t)
  3146. (repeat :tag "Specific properties" (string :tag "Property"))
  3147. (regexp :tag "Properties matched by regexp")))
  3148. (defun org-property-inherit-p (property)
  3149. "Check if PROPERTY is one that should be inherited."
  3150. (cond
  3151. ((eq org-use-property-inheritance t) t)
  3152. ((not org-use-property-inheritance) nil)
  3153. ((stringp org-use-property-inheritance)
  3154. (string-match org-use-property-inheritance property))
  3155. ((listp org-use-property-inheritance)
  3156. (member property org-use-property-inheritance))
  3157. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3158. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3159. "The default column format, if no other format has been defined.
  3160. This variable can be set on the per-file basis by inserting a line
  3161. #+COLUMNS: %25ITEM ....."
  3162. :group 'org-properties
  3163. :type 'string)
  3164. (defcustom org-columns-ellipses ".."
  3165. "The ellipses to be used when a field in column view is truncated.
  3166. When this is the empty string, as many characters as possible are shown,
  3167. but then there will be no visual indication that the field has been truncated.
  3168. When this is a string of length N, the last N characters of a truncated
  3169. field are replaced by this string. If the column is narrower than the
  3170. ellipses string, only part of the ellipses string will be shown."
  3171. :group 'org-properties
  3172. :type 'string)
  3173. (defcustom org-columns-modify-value-for-display-function nil
  3174. "Function that modifies values for display in column view.
  3175. For example, it can be used to cut out a certain part from a time stamp.
  3176. The function must take 2 arguments:
  3177. column-title The title of the column (*not* the property name)
  3178. value The value that should be modified.
  3179. The function should return the value that should be displayed,
  3180. or nil if the normal value should be used."
  3181. :group 'org-properties
  3182. :type '(choice (const nil) (function)))
  3183. (defconst org-global-properties-fixed
  3184. '(("VISIBILITY_ALL" . "folded children content all")
  3185. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3186. "List of property/value pairs that can be inherited by any entry.
  3187. These are fixed values, for the preset properties. The user variable
  3188. that can be used to add to this list is `org-global-properties'.
  3189. The entries in this list are cons cells where the car is a property
  3190. name and cdr is a string with the value. If the value represents
  3191. multiple items like an \"_ALL\" property, separate the items by
  3192. spaces.")
  3193. (defcustom org-global-properties nil
  3194. "List of property/value pairs that can be inherited by any entry.
  3195. This list will be combined with the constant `org-global-properties-fixed'.
  3196. The entries in this list are cons cells where the car is a property
  3197. name and cdr is a string with the value.
  3198. You can set buffer-local values for the same purpose in the variable
  3199. `org-file-properties' this by adding lines like
  3200. #+PROPERTY: NAME VALUE"
  3201. :group 'org-properties
  3202. :type '(repeat
  3203. (cons (string :tag "Property")
  3204. (string :tag "Value"))))
  3205. (defvar org-file-properties nil
  3206. "List of property/value pairs that can be inherited by any entry.
  3207. Valid for the current buffer.
  3208. This variable is populated from #+PROPERTY lines.")
  3209. (make-variable-buffer-local 'org-file-properties)
  3210. (defgroup org-agenda nil
  3211. "Options concerning agenda views in Org-mode."
  3212. :tag "Org Agenda"
  3213. :group 'org)
  3214. (defvar org-category nil
  3215. "Variable used by org files to set a category for agenda display.
  3216. Such files should use a file variable to set it, for example
  3217. # -*- mode: org; org-category: \"ELisp\"
  3218. or contain a special line
  3219. #+CATEGORY: ELisp
  3220. If the file does not specify a category, then file's base name
  3221. is used instead.")
  3222. (make-variable-buffer-local 'org-category)
  3223. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3224. (defcustom org-agenda-files nil
  3225. "The files to be used for agenda display.
  3226. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3227. \\[org-remove-file]. You can also use customize to edit the list.
  3228. If an entry is a directory, all files in that directory that are matched by
  3229. `org-agenda-file-regexp' will be part of the file list.
  3230. If the value of the variable is not a list but a single file name, then
  3231. the list of agenda files is actually stored and maintained in that file, one
  3232. agenda file per line. In this file paths can be given relative to
  3233. `org-directory'. Tilde expansion and environment variable substitution
  3234. are also made."
  3235. :group 'org-agenda
  3236. :type '(choice
  3237. (repeat :tag "List of files and directories" file)
  3238. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3239. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3240. "Regular expression to match files for `org-agenda-files'.
  3241. If any element in the list in that variable contains a directory instead
  3242. of a normal file, all files in that directory that are matched by this
  3243. regular expression will be included."
  3244. :group 'org-agenda
  3245. :type 'regexp)
  3246. (defcustom org-agenda-text-search-extra-files nil
  3247. "List of extra files to be searched by text search commands.
  3248. These files will be searched in addition to the agenda files by the
  3249. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3250. Note that these files will only be searched for text search commands,
  3251. not for the other agenda views like todo lists, tag searches or the weekly
  3252. agenda. This variable is intended to list notes and possibly archive files
  3253. that should also be searched by these two commands.
  3254. In fact, if the first element in the list is the symbol `agenda-archives',
  3255. then all archive files of all agenda files will be added to the search
  3256. scope."
  3257. :group 'org-agenda
  3258. :type '(set :greedy t
  3259. (const :tag "Agenda Archives" agenda-archives)
  3260. (repeat :inline t (file))))
  3261. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3262. 'org-agenda-text-search-extra-files)
  3263. (defcustom org-agenda-skip-unavailable-files nil
  3264. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3265. A nil value means to remove them, after a query, from the list."
  3266. :group 'org-agenda
  3267. :type 'boolean)
  3268. (defcustom org-calendar-to-agenda-key [?c]
  3269. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3270. The command `org-calendar-goto-agenda' will be bound to this key. The
  3271. default is the character `c' because then `c' can be used to switch back and
  3272. forth between agenda and calendar."
  3273. :group 'org-agenda
  3274. :type 'sexp)
  3275. (defcustom org-calendar-insert-diary-entry-key [?i]
  3276. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3277. This option is irrelevant until `org-agenda-diary-file' has been configured
  3278. to point to an Org-mode file. When that is the case, the command
  3279. `org-agenda-diary-entry' will be bound to the key given here, by default
  3280. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3281. if you want to continue doing this, you need to change this to a different
  3282. key."
  3283. :group 'org-agenda
  3284. :type 'sexp)
  3285. (defcustom org-agenda-diary-file 'diary-file
  3286. "File to which to add new entries with the `i' key in agenda and calendar.
  3287. When this is the symbol `diary-file', the functionality in the Emacs
  3288. calendar will be used to add entries to the `diary-file'. But when this
  3289. points to a file, `org-agenda-diary-entry' will be used instead."
  3290. :group 'org-agenda
  3291. :type '(choice
  3292. (const :tag "The standard Emacs diary file" diary-file)
  3293. (file :tag "Special Org file diary entries")))
  3294. (eval-after-load "calendar"
  3295. '(progn
  3296. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3297. 'org-calendar-goto-agenda)
  3298. (add-hook 'calendar-mode-hook
  3299. (lambda ()
  3300. (unless (eq org-agenda-diary-file 'diary-file)
  3301. (define-key calendar-mode-map
  3302. org-calendar-insert-diary-entry-key
  3303. 'org-agenda-diary-entry))))))
  3304. (defgroup org-latex nil
  3305. "Options for embedding LaTeX code into Org-mode."
  3306. :tag "Org LaTeX"
  3307. :group 'org)
  3308. (defcustom org-format-latex-options
  3309. '(:foreground default :background default :scale 1.0
  3310. :html-foreground "Black" :html-background "Transparent"
  3311. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3312. "Options for creating images from LaTeX fragments.
  3313. This is a property list with the following properties:
  3314. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3315. `default' means use the foreground of the default face.
  3316. `auto' means use the foreground from the text face.
  3317. :background the background color, or \"Transparent\".
  3318. `default' means use the background of the default face.
  3319. `auto' means use the background from the text face.
  3320. :scale a scaling factor for the size of the images, to get more pixels
  3321. :html-foreground, :html-background, :html-scale
  3322. the same numbers for HTML export.
  3323. :matchers a list indicating which matchers should be used to
  3324. find LaTeX fragments. Valid members of this list are:
  3325. \"begin\" find environments
  3326. \"$1\" find single characters surrounded by $.$
  3327. \"$\" find math expressions surrounded by $...$
  3328. \"$$\" find math expressions surrounded by $$....$$
  3329. \"\\(\" find math expressions surrounded by \\(...\\)
  3330. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3331. :group 'org-latex
  3332. :type 'plist)
  3333. (defcustom org-format-latex-signal-error t
  3334. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3335. When nil, just push out a message."
  3336. :group 'org-latex
  3337. :version "24.1"
  3338. :type 'boolean)
  3339. (defcustom org-latex-to-mathml-jar-file nil
  3340. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3341. Use this to specify additional executable file say a jar file.
  3342. When using MathToWeb as the converter, specify the full-path to
  3343. your mathtoweb.jar file."
  3344. :group 'org-latex
  3345. :version "24.1"
  3346. :type '(choice
  3347. (const :tag "None" nil)
  3348. (file :tag "JAR file" :must-match t)))
  3349. (defcustom org-latex-to-mathml-convert-command nil
  3350. "Command to convert LaTeX fragments to MathML.
  3351. Replace format-specifiers in the command as noted below and use
  3352. `shell-command' to convert LaTeX to MathML.
  3353. %j: Executable file in fully expanded form as specified by
  3354. `org-latex-to-mathml-jar-file'.
  3355. %I: Input LaTeX file in fully expanded form
  3356. %o: Output MathML file
  3357. This command is used by `org-create-math-formula'.
  3358. When using MathToWeb as the converter, set this to
  3359. \"java -jar %j -unicode -force -df %o %I\"."
  3360. :group 'org-latex
  3361. :version "24.1"
  3362. :type '(choice
  3363. (const :tag "None" nil)
  3364. (string :tag "\nShell command")))
  3365. (defcustom org-latex-create-formula-image-program 'dvipng
  3366. "Program to convert LaTeX fragments with.
  3367. dvipng Process the LaTeX fragments to dvi file, then convert
  3368. dvi files to png files using dvipng.
  3369. This will also include processing of non-math environments.
  3370. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3371. to convert pdf files to png files"
  3372. :group 'org-latex
  3373. :version "24.1"
  3374. :type '(choice
  3375. (const :tag "dvipng" dvipng)
  3376. (const :tag "imagemagick" imagemagick)))
  3377. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3378. "Path to store latex preview images.
  3379. A relative path here creates many directories relative to the
  3380. processed org files paths. An absolute path puts all preview
  3381. images at the same place."
  3382. :group 'org-latex
  3383. :version "24.3"
  3384. :type 'string)
  3385. (defun org-format-latex-mathml-available-p ()
  3386. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3387. (save-match-data
  3388. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3389. org-latex-to-mathml-convert-command)
  3390. (let ((executable (car (split-string
  3391. org-latex-to-mathml-convert-command))))
  3392. (when (executable-find executable)
  3393. (if (string-match
  3394. "%j" org-latex-to-mathml-convert-command)
  3395. (file-readable-p org-latex-to-mathml-jar-file)
  3396. t))))))
  3397. (defcustom org-format-latex-header "\\documentclass{article}
  3398. \\usepackage[usenames]{color}
  3399. \[PACKAGES]
  3400. \[DEFAULT-PACKAGES]
  3401. \\pagestyle{empty} % do not remove
  3402. % The settings below are copied from fullpage.sty
  3403. \\setlength{\\textwidth}{\\paperwidth}
  3404. \\addtolength{\\textwidth}{-3cm}
  3405. \\setlength{\\oddsidemargin}{1.5cm}
  3406. \\addtolength{\\oddsidemargin}{-2.54cm}
  3407. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3408. \\setlength{\\textheight}{\\paperheight}
  3409. \\addtolength{\\textheight}{-\\headheight}
  3410. \\addtolength{\\textheight}{-\\headsep}
  3411. \\addtolength{\\textheight}{-\\footskip}
  3412. \\addtolength{\\textheight}{-3cm}
  3413. \\setlength{\\topmargin}{1.5cm}
  3414. \\addtolength{\\topmargin}{-2.54cm}"
  3415. "The document header used for processing LaTeX fragments.
  3416. It is imperative that this header make sure that no page number
  3417. appears on the page. The package defined in the variables
  3418. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3419. will either replace the placeholder \"[PACKAGES]\" in this
  3420. header, or they will be appended."
  3421. :group 'org-latex
  3422. :type 'string)
  3423. (defun org-set-packages-alist (var val)
  3424. "Set the packages alist and make sure it has 3 elements per entry."
  3425. (set var (mapcar (lambda (x)
  3426. (if (and (consp x) (= (length x) 2))
  3427. (list (car x) (nth 1 x) t)
  3428. x))
  3429. val)))
  3430. (defun org-get-packages-alist (var)
  3431. "Get the packages alist and make sure it has 3 elements per entry."
  3432. (mapcar (lambda (x)
  3433. (if (and (consp x) (= (length x) 2))
  3434. (list (car x) (nth 1 x) t)
  3435. x))
  3436. (default-value var)))
  3437. (defcustom org-latex-default-packages-alist
  3438. '(("AUTO" "inputenc" t)
  3439. ("T1" "fontenc" t)
  3440. ("" "fixltx2e" nil)
  3441. ("" "graphicx" t)
  3442. ("" "longtable" nil)
  3443. ("" "float" nil)
  3444. ("" "wrapfig" nil)
  3445. ("" "rotating" nil)
  3446. ("normalem" "ulem" t)
  3447. ("" "amsmath" t)
  3448. ("" "textcomp" t)
  3449. ("" "marvosym" t)
  3450. ("" "wasysym" t)
  3451. ("" "amssymb" t)
  3452. ("" "hyperref" nil)
  3453. "\\tolerance=1000")
  3454. "Alist of default packages to be inserted in the header.
  3455. Change this only if one of the packages here causes an
  3456. incompatibility with another package you are using.
  3457. The packages in this list are needed by one part or another of
  3458. Org mode to function properly:
  3459. - inputenc, fontenc: for basic font and character selection
  3460. - fixltx2e: Important patches of LaTeX itself
  3461. - graphicx: for including images
  3462. - longtable: For multipage tables
  3463. - float, wrapfig: for figure placement
  3464. - rotating: for sideways figures and tables
  3465. - ulem: for underline and strike-through
  3466. - amsmath: for subscript and superscript and math environments
  3467. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3468. for interpreting the entities in `org-entities'. You can skip
  3469. some of these packages if you don't use any of their symbols.
  3470. - hyperref: for cross references
  3471. Therefore you should not modify this variable unless you know
  3472. what you are doing. The one reason to change it anyway is that
  3473. you might be loading some other package that conflicts with one
  3474. of the default packages. Each cell is of the format
  3475. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3476. the package also needs to be included when compiling LaTeX
  3477. snippets into images for inclusion into non-LaTeX output."
  3478. :group 'org-latex
  3479. :group 'org-export-latex
  3480. :set 'org-set-packages-alist
  3481. :get 'org-get-packages-alist
  3482. :version "24.1"
  3483. :type '(repeat
  3484. (choice
  3485. (list :tag "options/package pair"
  3486. (string :tag "options")
  3487. (string :tag "package")
  3488. (boolean :tag "Snippet"))
  3489. (string :tag "A line of LaTeX"))))
  3490. (defcustom org-latex-packages-alist nil
  3491. "Alist of packages to be inserted in every LaTeX header.
  3492. These will be inserted after `org-latex-default-packages-alist'.
  3493. Each cell is of the format:
  3494. \(\"options\" \"package\" snippet-flag)
  3495. SNIPPET-FLAG, when t, indicates that this package is also needed
  3496. when turning LaTeX snippets into images for inclusion into
  3497. non-LaTeX output.
  3498. Make sure that you only list packages here which:
  3499. - you want in every file
  3500. - do not conflict with the setup in `org-format-latex-header'.
  3501. - do not conflict with the default packages in
  3502. `org-latex-default-packages-alist'."
  3503. :group 'org-latex
  3504. :group 'org-export-latex
  3505. :set 'org-set-packages-alist
  3506. :get 'org-get-packages-alist
  3507. :type '(repeat
  3508. (choice
  3509. (list :tag "options/package pair"
  3510. (string :tag "options")
  3511. (string :tag "package")
  3512. (boolean :tag "Snippet"))
  3513. (string :tag "A line of LaTeX"))))
  3514. (defgroup org-appearance nil
  3515. "Settings for Org-mode appearance."
  3516. :tag "Org Appearance"
  3517. :group 'org)
  3518. (defcustom org-level-color-stars-only nil
  3519. "Non-nil means fontify only the stars in each headline.
  3520. When nil, the entire headline is fontified.
  3521. Changing it requires restart of `font-lock-mode' to become effective
  3522. also in regions already fontified."
  3523. :group 'org-appearance
  3524. :type 'boolean)
  3525. (defcustom org-hide-leading-stars nil
  3526. "Non-nil means hide the first N-1 stars in a headline.
  3527. This works by using the face `org-hide' for these stars. This
  3528. face is white for a light background, and black for a dark
  3529. background. You may have to customize the face `org-hide' to
  3530. make this work.
  3531. Changing it requires restart of `font-lock-mode' to become effective
  3532. also in regions already fontified.
  3533. You may also set this on a per-file basis by adding one of the following
  3534. lines to the buffer:
  3535. #+STARTUP: hidestars
  3536. #+STARTUP: showstars"
  3537. :group 'org-appearance
  3538. :type 'boolean)
  3539. (defcustom org-hidden-keywords nil
  3540. "List of symbols corresponding to keywords to be hidden the org buffer.
  3541. For example, a value '(title) for this list will make the document's title
  3542. appear in the buffer without the initial #+TITLE: keyword."
  3543. :group 'org-appearance
  3544. :version "24.1"
  3545. :type '(set (const :tag "#+AUTHOR" author)
  3546. (const :tag "#+DATE" date)
  3547. (const :tag "#+EMAIL" email)
  3548. (const :tag "#+TITLE" title)))
  3549. (defcustom org-custom-properties nil
  3550. "List of properties (as strings) with a special meaning.
  3551. The default use of these custom properties is to let the user
  3552. hide them with `org-toggle-custom-properties-visibility'."
  3553. :group 'org-properties
  3554. :group 'org-appearance
  3555. :version "24.3"
  3556. :type '(repeat (string :tag "Property Name")))
  3557. (defcustom org-fontify-done-headline nil
  3558. "Non-nil means change the face of a headline if it is marked DONE.
  3559. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3560. When this is non-nil, the headline after the keyword is set to the
  3561. `org-headline-done' as an additional indication."
  3562. :group 'org-appearance
  3563. :type 'boolean)
  3564. (defcustom org-fontify-emphasized-text t
  3565. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3566. Changing this variable requires a restart of Emacs to take effect."
  3567. :group 'org-appearance
  3568. :type 'boolean)
  3569. (defcustom org-fontify-whole-heading-line nil
  3570. "Non-nil means fontify the whole line for headings.
  3571. This is useful when setting a background color for the
  3572. org-level-* faces."
  3573. :group 'org-appearance
  3574. :type 'boolean)
  3575. (defcustom org-highlight-latex-and-related nil
  3576. "Non-nil means highlight LaTeX related syntax in the buffer.
  3577. When non nil, the value should be a list containing any of the
  3578. following symbols:
  3579. `latex' Highlight LaTeX snippets and environments.
  3580. `script' Highlight subscript and superscript.
  3581. `entities' Highlight entities."
  3582. :group 'org-appearance
  3583. :version "24.4"
  3584. :package-version '(Org . "8.0")
  3585. :type '(choice
  3586. (const :tag "No highlighting" nil)
  3587. (set :greedy t :tag "Highlight"
  3588. (const :tag "LaTeX snippets and environments" latex)
  3589. (const :tag "Subscript and superscript" script)
  3590. (const :tag "Entities" entities))))
  3591. (defcustom org-hide-emphasis-markers nil
  3592. "Non-nil mean font-lock should hide the emphasis marker characters."
  3593. :group 'org-appearance
  3594. :type 'boolean)
  3595. (defcustom org-hide-macro-markers nil
  3596. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3597. :group 'org-appearance
  3598. :type 'boolean)
  3599. (defcustom org-pretty-entities nil
  3600. "Non-nil means show entities as UTF8 characters.
  3601. When nil, the \\name form remains in the buffer."
  3602. :group 'org-appearance
  3603. :version "24.1"
  3604. :type 'boolean)
  3605. (defcustom org-pretty-entities-include-sub-superscripts t
  3606. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3607. :group 'org-appearance
  3608. :version "24.1"
  3609. :type 'boolean)
  3610. (defvar org-emph-re nil
  3611. "Regular expression for matching emphasis.
  3612. After a match, the match groups contain these elements:
  3613. 0 The match of the full regular expression, including the characters
  3614. before and after the proper match
  3615. 1 The character before the proper match, or empty at beginning of line
  3616. 2 The proper match, including the leading and trailing markers
  3617. 3 The leading marker like * or /, indicating the type of highlighting
  3618. 4 The text between the emphasis markers, not including the markers
  3619. 5 The character after the match, empty at the end of a line")
  3620. (defvar org-verbatim-re nil
  3621. "Regular expression for matching verbatim text.")
  3622. (defvar org-emphasis-regexp-components) ; defined just below
  3623. (defvar org-emphasis-alist) ; defined just below
  3624. (defun org-set-emph-re (var val)
  3625. "Set variable and compute the emphasis regular expression."
  3626. (set var val)
  3627. (when (and (boundp 'org-emphasis-alist)
  3628. (boundp 'org-emphasis-regexp-components)
  3629. org-emphasis-alist org-emphasis-regexp-components)
  3630. (let* ((e org-emphasis-regexp-components)
  3631. (pre (car e))
  3632. (post (nth 1 e))
  3633. (border (nth 2 e))
  3634. (body (nth 3 e))
  3635. (nl (nth 4 e))
  3636. (body1 (concat body "*?"))
  3637. (markers (mapconcat 'car org-emphasis-alist ""))
  3638. (vmarkers (mapconcat
  3639. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3640. org-emphasis-alist "")))
  3641. ;; make sure special characters appear at the right position in the class
  3642. (if (string-match "\\^" markers)
  3643. (setq markers (concat (replace-match "" t t markers) "^")))
  3644. (if (string-match "-" markers)
  3645. (setq markers (concat (replace-match "" t t markers) "-")))
  3646. (if (string-match "\\^" vmarkers)
  3647. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3648. (if (string-match "-" vmarkers)
  3649. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3650. (if (> nl 0)
  3651. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3652. (int-to-string nl) "\\}")))
  3653. ;; Make the regexp
  3654. (setq org-emph-re
  3655. (concat "\\([" pre "]\\|^\\)"
  3656. "\\("
  3657. "\\([" markers "]\\)"
  3658. "\\("
  3659. "[^" border "]\\|"
  3660. "[^" border "]"
  3661. body1
  3662. "[^" border "]"
  3663. "\\)"
  3664. "\\3\\)"
  3665. "\\([" post "]\\|$\\)"))
  3666. (setq org-verbatim-re
  3667. (concat "\\([" pre "]\\|^\\)"
  3668. "\\("
  3669. "\\([" vmarkers "]\\)"
  3670. "\\("
  3671. "[^" border "]\\|"
  3672. "[^" border "]"
  3673. body1
  3674. "[^" border "]"
  3675. "\\)"
  3676. "\\3\\)"
  3677. "\\([" post "]\\|$\\)")))))
  3678. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3679. ;; set this option proved cumbersome. See this message/thread:
  3680. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3681. (defvar org-emphasis-regexp-components
  3682. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3683. "Components used to build the regular expression for emphasis.
  3684. This is a list with five entries. Terminology: In an emphasis string
  3685. like \" *strong word* \", we call the initial space PREMATCH, the final
  3686. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3687. and \"trong wor\" is the body. The different components in this variable
  3688. specify what is allowed/forbidden in each part:
  3689. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3690. post Chars allowed as postmatch. End of line will be allowed too.
  3691. border The chars *forbidden* as border characters.
  3692. body-regexp A regexp like \".\" to match a body character. Don't use
  3693. non-shy groups here, and don't allow newline here.
  3694. newline The maximum number of newlines allowed in an emphasis exp.
  3695. You need to reload Org or to restart Emacs after customizing this.")
  3696. (defcustom org-emphasis-alist
  3697. `(("*" bold)
  3698. ("/" italic)
  3699. ("_" underline)
  3700. ("=" org-code verbatim)
  3701. ("~" org-verbatim verbatim)
  3702. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3703. "Alist of characters and faces to emphasize text.
  3704. Text starting and ending with a special character will be emphasized,
  3705. for example *bold*, _underlined_ and /italic/. This variable sets the
  3706. marker characters and the face to be used by font-lock for highlighting
  3707. in Org-mode Emacs buffers.
  3708. You need to reload Org or to restart Emacs after customizing this."
  3709. :group 'org-appearance
  3710. :set 'org-set-emph-re
  3711. :version "24.4"
  3712. :package-version '(Org . "8.0")
  3713. :type '(repeat
  3714. (list
  3715. (string :tag "Marker character")
  3716. (choice
  3717. (face :tag "Font-lock-face")
  3718. (plist :tag "Face property list"))
  3719. (option (const verbatim)))))
  3720. (defvar org-protecting-blocks
  3721. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3722. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3723. This is needed for font-lock setup.")
  3724. ;;; Miscellaneous options
  3725. (defgroup org-completion nil
  3726. "Completion in Org-mode."
  3727. :tag "Org Completion"
  3728. :group 'org)
  3729. (defcustom org-completion-use-ido nil
  3730. "Non-nil means use ido completion wherever possible.
  3731. Note that `ido-mode' must be active for this variable to be relevant.
  3732. If you decide to turn this variable on, you might well want to turn off
  3733. `org-outline-path-complete-in-steps'.
  3734. See also `org-completion-use-iswitchb'."
  3735. :group 'org-completion
  3736. :type 'boolean)
  3737. (defcustom org-completion-use-iswitchb nil
  3738. "Non-nil means use iswitchb completion wherever possible.
  3739. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3740. If you decide to turn this variable on, you might well want to turn off
  3741. `org-outline-path-complete-in-steps'.
  3742. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3743. :group 'org-completion
  3744. :type 'boolean)
  3745. (defcustom org-completion-fallback-command 'hippie-expand
  3746. "The expansion command called by \\[pcomplete] in normal context.
  3747. Normal means, no org-mode-specific context."
  3748. :group 'org-completion
  3749. :type 'function)
  3750. ;;; Functions and variables from their packages
  3751. ;; Declared here to avoid compiler warnings
  3752. ;; XEmacs only
  3753. (defvar outline-mode-menu-heading)
  3754. (defvar outline-mode-menu-show)
  3755. (defvar outline-mode-menu-hide)
  3756. (defvar zmacs-regions) ; XEmacs regions
  3757. ;; Emacs only
  3758. (defvar mark-active)
  3759. ;; Various packages
  3760. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3761. (declare-function calendar-forward-day "cal-move" (arg))
  3762. (declare-function calendar-goto-date "cal-move" (date))
  3763. (declare-function calendar-goto-today "cal-move" ())
  3764. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3765. (defvar calc-embedded-close-formula)
  3766. (defvar calc-embedded-open-formula)
  3767. (declare-function cdlatex-tab "ext:cdlatex" ())
  3768. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3769. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3770. (defvar font-lock-unfontify-region-function)
  3771. (declare-function iswitchb-read-buffer "iswitchb"
  3772. (prompt &optional default require-match start matches-set))
  3773. (defvar iswitchb-temp-buflist)
  3774. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3775. (defvar org-agenda-tags-todo-honor-ignore-options)
  3776. (declare-function org-agenda-skip "org-agenda" ())
  3777. (declare-function
  3778. org-agenda-format-item "org-agenda"
  3779. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3780. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3781. (declare-function org-agenda-change-all-lines "org-agenda"
  3782. (newhead hdmarker &optional fixface just-this))
  3783. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3784. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3785. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3786. (beg end))
  3787. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3788. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3789. "org-agenda" (&optional end))
  3790. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3791. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3792. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3793. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3794. (declare-function org-indent-mode "org-indent" (&optional arg))
  3795. (declare-function parse-time-string "parse-time" (string))
  3796. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3797. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3798. (defvar remember-data-file)
  3799. (defvar texmathp-why)
  3800. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3801. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3802. ;;;###autoload
  3803. (defun turn-on-orgtbl ()
  3804. "Unconditionally turn on `orgtbl-mode'."
  3805. (require 'org-table)
  3806. (orgtbl-mode 1))
  3807. (defun org-at-table-p (&optional table-type)
  3808. "Return t if the cursor is inside an org-type table.
  3809. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3810. (if org-enable-table-editor
  3811. (save-excursion
  3812. (beginning-of-line 1)
  3813. (looking-at (if table-type org-table-any-line-regexp
  3814. org-table-line-regexp)))
  3815. nil))
  3816. (defsubst org-table-p () (org-at-table-p))
  3817. (defun org-at-table.el-p ()
  3818. "Return t if and only if we are at a table.el table."
  3819. (and (org-at-table-p 'any)
  3820. (save-excursion
  3821. (goto-char (org-table-begin 'any))
  3822. (looking-at org-table1-hline-regexp))))
  3823. (defun org-table-recognize-table.el ()
  3824. "If there is a table.el table nearby, recognize it and move into it."
  3825. (if org-table-tab-recognizes-table.el
  3826. (if (org-at-table.el-p)
  3827. (progn
  3828. (beginning-of-line 1)
  3829. (if (looking-at org-table-dataline-regexp)
  3830. nil
  3831. (if (looking-at org-table1-hline-regexp)
  3832. (progn
  3833. (beginning-of-line 2)
  3834. (if (looking-at org-table-any-border-regexp)
  3835. (beginning-of-line -1)))))
  3836. (if (re-search-forward "|" (org-table-end t) t)
  3837. (progn
  3838. (require 'table)
  3839. (if (table--at-cell-p (point))
  3840. t
  3841. (message "recognizing table.el table...")
  3842. (table-recognize-table)
  3843. (message "recognizing table.el table...done")))
  3844. (error "This should not happen"))
  3845. t)
  3846. nil)
  3847. nil))
  3848. (defun org-at-table-hline-p ()
  3849. "Return t if the cursor is inside a hline in a table."
  3850. (if org-enable-table-editor
  3851. (save-excursion
  3852. (beginning-of-line 1)
  3853. (looking-at org-table-hline-regexp))
  3854. nil))
  3855. (defun org-table-map-tables (function &optional quietly)
  3856. "Apply FUNCTION to the start of all tables in the buffer."
  3857. (save-excursion
  3858. (save-restriction
  3859. (widen)
  3860. (goto-char (point-min))
  3861. (while (re-search-forward org-table-any-line-regexp nil t)
  3862. (unless quietly
  3863. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3864. (beginning-of-line 1)
  3865. (when (and (looking-at org-table-line-regexp)
  3866. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3867. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3868. (save-excursion (funcall function))
  3869. (or (looking-at org-table-line-regexp)
  3870. (forward-char 1)))
  3871. (re-search-forward org-table-any-border-regexp nil 1))))
  3872. (unless quietly (message "Mapping tables: done")))
  3873. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3874. (declare-function org-clock-update-mode-line "org-clock" ())
  3875. (declare-function org-resolve-clocks "org-clock"
  3876. (&optional also-non-dangling-p prompt last-valid))
  3877. (defun org-at-TBLFM-p (&optional pos)
  3878. "Return t when point (or POS) is in #+TBLFM line."
  3879. (save-excursion
  3880. (let ((pos pos)))
  3881. (goto-char (or pos (point)))
  3882. (beginning-of-line 1)
  3883. (looking-at org-TBLFM-regexp)))
  3884. (defvar org-clock-start-time)
  3885. (defvar org-clock-marker (make-marker)
  3886. "Marker recording the last clock-in.")
  3887. (defvar org-clock-hd-marker (make-marker)
  3888. "Marker recording the last clock-in, but the headline position.")
  3889. (defvar org-clock-heading ""
  3890. "The heading of the current clock entry.")
  3891. (defun org-clock-is-active ()
  3892. "Return the buffer where the clock is currently running.
  3893. Return nil if no clock is running."
  3894. (marker-buffer org-clock-marker))
  3895. (defun org-check-running-clock ()
  3896. "Check if the current buffer contains the running clock.
  3897. If yes, offer to stop it and to save the buffer with the changes."
  3898. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3899. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3900. (buffer-name))))
  3901. (org-clock-out)
  3902. (when (y-or-n-p "Save changed buffer?")
  3903. (save-buffer))))
  3904. (defun org-clocktable-try-shift (dir n)
  3905. "Check if this line starts a clock table, if yes, shift the time block."
  3906. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3907. (org-clocktable-shift dir n)))
  3908. ;;;###autoload
  3909. (defun org-clock-persistence-insinuate ()
  3910. "Set up hooks for clock persistence."
  3911. (require 'org-clock)
  3912. (add-hook 'org-mode-hook 'org-clock-load)
  3913. (add-hook 'kill-emacs-hook 'org-clock-save))
  3914. (defgroup org-archive nil
  3915. "Options concerning archiving in Org-mode."
  3916. :tag "Org Archive"
  3917. :group 'org-structure)
  3918. (defcustom org-archive-location "%s_archive::"
  3919. "The location where subtrees should be archived.
  3920. The value of this variable is a string, consisting of two parts,
  3921. separated by a double-colon. The first part is a filename and
  3922. the second part is a headline.
  3923. When the filename is omitted, archiving happens in the same file.
  3924. %s in the filename will be replaced by the current file
  3925. name (without the directory part). Archiving to a different file
  3926. is useful to keep archived entries from contributing to the
  3927. Org-mode Agenda.
  3928. The archived entries will be filed as subtrees of the specified
  3929. headline. When the headline is omitted, the subtrees are simply
  3930. filed away at the end of the file, as top-level entries. Also in
  3931. the heading you can use %s to represent the file name, this can be
  3932. useful when using the same archive for a number of different files.
  3933. Here are a few examples:
  3934. \"%s_archive::\"
  3935. If the current file is Projects.org, archive in file
  3936. Projects.org_archive, as top-level trees. This is the default.
  3937. \"::* Archived Tasks\"
  3938. Archive in the current file, under the top-level headline
  3939. \"* Archived Tasks\".
  3940. \"~/org/archive.org::\"
  3941. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3942. \"~/org/archive.org::* From %s\"
  3943. Archive in file ~/org/archive.org (absolute path), under headlines
  3944. \"From FILENAME\" where file name is the current file name.
  3945. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3946. The \"datetree/\" string is special, signifying to archive
  3947. items to the datetree. Items are placed in either the CLOSED
  3948. date of the item, or the current date if there is no CLOSED date.
  3949. The heading will be a subentry to the current date. There doesn't
  3950. need to be a heading, but there always needs to be a slash after
  3951. datetree. For example, to store archived items directly in the
  3952. datetree, use \"~/org/datetree.org::datetree/\".
  3953. \"basement::** Finished Tasks\"
  3954. Archive in file ./basement (relative path), as level 3 trees
  3955. below the level 2 heading \"** Finished Tasks\".
  3956. You may set this option on a per-file basis by adding to the buffer a
  3957. line like
  3958. #+ARCHIVE: basement::** Finished Tasks
  3959. You may also define it locally for a subtree by setting an ARCHIVE property
  3960. in the entry. If such a property is found in an entry, or anywhere up
  3961. the hierarchy, it will be used."
  3962. :group 'org-archive
  3963. :type 'string)
  3964. (defcustom org-agenda-skip-archived-trees t
  3965. "Non-nil means the agenda will skip any items located in archived trees.
  3966. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3967. variable is no longer recommended, you should leave it at the value t.
  3968. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3969. :group 'org-archive
  3970. :group 'org-agenda-skip
  3971. :type 'boolean)
  3972. (defcustom org-columns-skip-archived-trees t
  3973. "Non-nil means ignore archived trees when creating column view."
  3974. :group 'org-archive
  3975. :group 'org-properties
  3976. :type 'boolean)
  3977. (defcustom org-cycle-open-archived-trees nil
  3978. "Non-nil means `org-cycle' will open archived trees.
  3979. An archived tree is a tree marked with the tag ARCHIVE.
  3980. When nil, archived trees will stay folded. You can still open them with
  3981. normal outline commands like `show-all', but not with the cycling commands."
  3982. :group 'org-archive
  3983. :group 'org-cycle
  3984. :type 'boolean)
  3985. (defcustom org-sparse-tree-open-archived-trees nil
  3986. "Non-nil means sparse tree construction shows matches in archived trees.
  3987. When nil, matches in these trees are highlighted, but the trees are kept in
  3988. collapsed state."
  3989. :group 'org-archive
  3990. :group 'org-sparse-trees
  3991. :type 'boolean)
  3992. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3993. "The default date type when building a sparse tree.
  3994. When this is nil, a date is a scheduled or a deadline timestamp.
  3995. Otherwise, these types are allowed:
  3996. all: all timestamps
  3997. active: only active timestamps (<...>)
  3998. inactive: only inactive timestamps (<...)
  3999. scheduled: only scheduled timestamps
  4000. deadline: only deadline timestamps"
  4001. :type '(choice (const :tag "Scheduled or deadline" scheduled-or-deadline)
  4002. (const :tag "All timestamps" all)
  4003. (const :tag "Only active timestamps" active)
  4004. (const :tag "Only inactive timestamps" inactive)
  4005. (const :tag "Only scheduled timestamps" scheduled)
  4006. (const :tag "Only deadline timestamps" deadline)
  4007. (const :tag "Only closed timestamps" closed))
  4008. :version "24.3"
  4009. :group 'org-sparse-trees)
  4010. (defun org-cycle-hide-archived-subtrees (state)
  4011. "Re-hide all archived subtrees after a visibility state change."
  4012. (when (and (not org-cycle-open-archived-trees)
  4013. (not (memq state '(overview folded))))
  4014. (save-excursion
  4015. (let* ((globalp (memq state '(contents all)))
  4016. (beg (if globalp (point-min) (point)))
  4017. (end (if globalp (point-max) (org-end-of-subtree t))))
  4018. (org-hide-archived-subtrees beg end)
  4019. (goto-char beg)
  4020. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4021. (message "%s" (substitute-command-keys
  4022. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4023. (defun org-force-cycle-archived ()
  4024. "Cycle subtree even if it is archived."
  4025. (interactive)
  4026. (setq this-command 'org-cycle)
  4027. (let ((org-cycle-open-archived-trees t))
  4028. (call-interactively 'org-cycle)))
  4029. (defun org-hide-archived-subtrees (beg end)
  4030. "Re-hide all archived subtrees after a visibility state change."
  4031. (save-excursion
  4032. (let* ((re (concat ":" org-archive-tag ":")))
  4033. (goto-char beg)
  4034. (while (re-search-forward re end t)
  4035. (when (org-at-heading-p)
  4036. (org-flag-subtree t)
  4037. (org-end-of-subtree t))))))
  4038. (declare-function outline-end-of-heading "outline" ())
  4039. (declare-function outline-flag-region "outline" (from to flag))
  4040. (defun org-flag-subtree (flag)
  4041. (save-excursion
  4042. (org-back-to-heading t)
  4043. (outline-end-of-heading)
  4044. (outline-flag-region (point)
  4045. (progn (org-end-of-subtree t) (point))
  4046. flag)))
  4047. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4048. ;; Declare Column View Code
  4049. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4050. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4051. (declare-function org-columns-compute "org-colview" (property))
  4052. ;; Declare ID code
  4053. (declare-function org-id-store-link "org-id")
  4054. (declare-function org-id-locations-load "org-id")
  4055. (declare-function org-id-locations-save "org-id")
  4056. (defvar org-id-track-globally)
  4057. ;;; Variables for pre-computed regular expressions, all buffer local
  4058. (defvar org-todo-regexp nil
  4059. "Matches any of the TODO state keywords.")
  4060. (make-variable-buffer-local 'org-todo-regexp)
  4061. (defvar org-not-done-regexp nil
  4062. "Matches any of the TODO state keywords except the last one.")
  4063. (make-variable-buffer-local 'org-not-done-regexp)
  4064. (defvar org-not-done-heading-regexp nil
  4065. "Matches a TODO headline that is not done.")
  4066. (make-variable-buffer-local 'org-not-done-regexp)
  4067. (defvar org-todo-line-regexp nil
  4068. "Matches a headline and puts TODO state into group 2 if present.")
  4069. (make-variable-buffer-local 'org-todo-line-regexp)
  4070. (defvar org-complex-heading-regexp nil
  4071. "Matches a headline and puts everything into groups:
  4072. group 1: the stars
  4073. group 2: The todo keyword, maybe
  4074. group 3: Priority cookie
  4075. group 4: True headline
  4076. group 5: Tags")
  4077. (make-variable-buffer-local 'org-complex-heading-regexp)
  4078. (defvar org-complex-heading-regexp-format nil
  4079. "Printf format to make regexp to match an exact headline.
  4080. This regexp will match the headline of any node which has the
  4081. exact headline text that is put into the format, but may have any
  4082. TODO state, priority and tags.")
  4083. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4084. (defvar org-todo-line-tags-regexp nil
  4085. "Matches a headline and puts TODO state into group 2 if present.
  4086. Also put tags into group 4 if tags are present.")
  4087. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4088. (defvar org-ds-keyword-length 12
  4089. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4090. (make-variable-buffer-local 'org-ds-keyword-length)
  4091. (defvar org-deadline-regexp nil
  4092. "Matches the DEADLINE keyword.")
  4093. (make-variable-buffer-local 'org-deadline-regexp)
  4094. (defvar org-deadline-time-regexp nil
  4095. "Matches the DEADLINE keyword together with a time stamp.")
  4096. (make-variable-buffer-local 'org-deadline-time-regexp)
  4097. (defvar org-deadline-time-hour-regexp nil
  4098. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4099. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4100. (defvar org-deadline-line-regexp nil
  4101. "Matches the DEADLINE keyword and the rest of the line.")
  4102. (make-variable-buffer-local 'org-deadline-line-regexp)
  4103. (defvar org-scheduled-regexp nil
  4104. "Matches the SCHEDULED keyword.")
  4105. (make-variable-buffer-local 'org-scheduled-regexp)
  4106. (defvar org-scheduled-time-regexp nil
  4107. "Matches the SCHEDULED keyword together with a time stamp.")
  4108. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4109. (defvar org-scheduled-time-hour-regexp nil
  4110. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4111. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4112. (defvar org-closed-time-regexp nil
  4113. "Matches the CLOSED keyword together with a time stamp.")
  4114. (make-variable-buffer-local 'org-closed-time-regexp)
  4115. (defvar org-keyword-time-regexp nil
  4116. "Matches any of the 4 keywords, together with the time stamp.")
  4117. (make-variable-buffer-local 'org-keyword-time-regexp)
  4118. (defvar org-keyword-time-not-clock-regexp nil
  4119. "Matches any of the 3 keywords, together with the time stamp.")
  4120. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4121. (defvar org-maybe-keyword-time-regexp nil
  4122. "Matches a timestamp, possibly preceded by a keyword.")
  4123. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4124. (defvar org-all-time-keywords nil
  4125. "List of time keywords.")
  4126. (make-variable-buffer-local 'org-all-time-keywords)
  4127. (defconst org-plain-time-of-day-regexp
  4128. (concat
  4129. "\\(\\<[012]?[0-9]"
  4130. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4131. "\\(--?"
  4132. "\\(\\<[012]?[0-9]"
  4133. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4134. "\\)?")
  4135. "Regular expression to match a plain time or time range.
  4136. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4137. groups carry important information:
  4138. 0 the full match
  4139. 1 the first time, range or not
  4140. 8 the second time, if it is a range.")
  4141. (defconst org-plain-time-extension-regexp
  4142. (concat
  4143. "\\(\\<[012]?[0-9]"
  4144. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4145. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4146. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4147. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4148. groups carry important information:
  4149. 0 the full match
  4150. 7 hours of duration
  4151. 9 minutes of duration")
  4152. (defconst org-stamp-time-of-day-regexp
  4153. (concat
  4154. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4155. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4156. "\\(--?"
  4157. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4158. "Regular expression to match a timestamp time or time range.
  4159. After a match, the following groups carry important information:
  4160. 0 the full match
  4161. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4162. 2 the first time, range or not
  4163. 4 the second time, if it is a range.")
  4164. (defconst org-startup-options
  4165. '(("fold" org-startup-folded t)
  4166. ("overview" org-startup-folded t)
  4167. ("nofold" org-startup-folded nil)
  4168. ("showall" org-startup-folded nil)
  4169. ("showeverything" org-startup-folded showeverything)
  4170. ("content" org-startup-folded content)
  4171. ("indent" org-startup-indented t)
  4172. ("noindent" org-startup-indented nil)
  4173. ("hidestars" org-hide-leading-stars t)
  4174. ("showstars" org-hide-leading-stars nil)
  4175. ("odd" org-odd-levels-only t)
  4176. ("oddeven" org-odd-levels-only nil)
  4177. ("align" org-startup-align-all-tables t)
  4178. ("noalign" org-startup-align-all-tables nil)
  4179. ("inlineimages" org-startup-with-inline-images t)
  4180. ("noinlineimages" org-startup-with-inline-images nil)
  4181. ("latexpreview" org-startup-with-latex-preview t)
  4182. ("nolatexpreview" org-startup-with-latex-preview nil)
  4183. ("customtime" org-display-custom-times t)
  4184. ("logdone" org-log-done time)
  4185. ("lognotedone" org-log-done note)
  4186. ("nologdone" org-log-done nil)
  4187. ("lognoteclock-out" org-log-note-clock-out t)
  4188. ("nolognoteclock-out" org-log-note-clock-out nil)
  4189. ("logrepeat" org-log-repeat state)
  4190. ("lognoterepeat" org-log-repeat note)
  4191. ("logdrawer" org-log-into-drawer t)
  4192. ("nologdrawer" org-log-into-drawer nil)
  4193. ("logstatesreversed" org-log-states-order-reversed t)
  4194. ("nologstatesreversed" org-log-states-order-reversed nil)
  4195. ("nologrepeat" org-log-repeat nil)
  4196. ("logreschedule" org-log-reschedule time)
  4197. ("lognotereschedule" org-log-reschedule note)
  4198. ("nologreschedule" org-log-reschedule nil)
  4199. ("logredeadline" org-log-redeadline time)
  4200. ("lognoteredeadline" org-log-redeadline note)
  4201. ("nologredeadline" org-log-redeadline nil)
  4202. ("logrefile" org-log-refile time)
  4203. ("lognoterefile" org-log-refile note)
  4204. ("nologrefile" org-log-refile nil)
  4205. ("fninline" org-footnote-define-inline t)
  4206. ("nofninline" org-footnote-define-inline nil)
  4207. ("fnlocal" org-footnote-section nil)
  4208. ("fnauto" org-footnote-auto-label t)
  4209. ("fnprompt" org-footnote-auto-label nil)
  4210. ("fnconfirm" org-footnote-auto-label confirm)
  4211. ("fnplain" org-footnote-auto-label plain)
  4212. ("fnadjust" org-footnote-auto-adjust t)
  4213. ("nofnadjust" org-footnote-auto-adjust nil)
  4214. ("constcgs" constants-unit-system cgs)
  4215. ("constSI" constants-unit-system SI)
  4216. ("noptag" org-tag-persistent-alist nil)
  4217. ("hideblocks" org-hide-block-startup t)
  4218. ("nohideblocks" org-hide-block-startup nil)
  4219. ("beamer" org-startup-with-beamer-mode t)
  4220. ("entitiespretty" org-pretty-entities t)
  4221. ("entitiesplain" org-pretty-entities nil))
  4222. "Variable associated with STARTUP options for org-mode.
  4223. Each element is a list of three items: the startup options (as written
  4224. in the #+STARTUP line), the corresponding variable, and the value to set
  4225. this variable to if the option is found. An optional forth element PUSH
  4226. means to push this value onto the list in the variable.")
  4227. (defun org-update-property-plist (key val props)
  4228. "Update PROPS with KEY and VAL."
  4229. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4230. (key (if appending (substring key 0 (- (length key) 1)) key))
  4231. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4232. (previous (cdr (assoc key props))))
  4233. (if appending
  4234. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4235. (cons (cons key val) remainder))))
  4236. (defcustom org-group-tags t
  4237. "When non-nil (the default), use group tags.
  4238. This can be turned on/off through `org-toggle-tags-groups'."
  4239. :group 'org-tags
  4240. :group 'org-startup
  4241. :type 'boolean)
  4242. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4243. (defun org-toggle-tags-groups ()
  4244. "Toggle support for group tags.
  4245. Support for group tags is controlled by the option
  4246. `org-group-tags', which is non-nil by default."
  4247. (interactive)
  4248. (setq org-group-tags (not org-group-tags))
  4249. (cond ((and (derived-mode-p 'org-agenda-mode)
  4250. org-group-tags)
  4251. (org-agenda-redo))
  4252. ((derived-mode-p 'org-mode)
  4253. (let ((org-inhibit-startup t)) (org-mode))))
  4254. (message "Groups tags support has been turned %s"
  4255. (if org-group-tags "on" "off")))
  4256. (defun org-set-regexps-and-options-for-tags ()
  4257. "Precompute variables used for tags."
  4258. (when (derived-mode-p 'org-mode)
  4259. (org-set-local 'org-file-tags nil)
  4260. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4261. (splitre "[ \t]+")
  4262. (start 0)
  4263. tags ftags key value)
  4264. (save-excursion
  4265. (save-restriction
  4266. (widen)
  4267. (goto-char (point-min))
  4268. (while (re-search-forward re nil t)
  4269. (setq key (upcase (org-match-string-no-properties 1))
  4270. value (org-match-string-no-properties 2))
  4271. (if (stringp value) (setq value (org-trim value)))
  4272. (cond
  4273. ((equal key "TAGS")
  4274. (setq tags (append tags (if tags '("\\n") nil)
  4275. (org-split-string value splitre))))
  4276. ((equal key "FILETAGS")
  4277. (when (string-match "\\S-" value)
  4278. (setq ftags
  4279. (append
  4280. ftags
  4281. (apply 'append
  4282. (mapcar (lambda (x) (org-split-string x ":"))
  4283. (org-split-string value)))))))))))
  4284. ;; Process the file tags.
  4285. (and ftags (org-set-local 'org-file-tags
  4286. (mapcar 'org-add-prop-inherited ftags)))
  4287. (org-set-local 'org-tag-groups-alist nil)
  4288. ;; Process the tags.
  4289. (when (and (not tags) org-tag-alist)
  4290. (setq tags
  4291. (mapcar
  4292. (lambda (tg) (cond ((eq (car tg) :startgroup) "{")
  4293. ((eq (car tg) :endgroup) "}")
  4294. ((eq (car tg) :grouptags) ":")
  4295. ((eq (car tg) :newline) "\n")
  4296. (t (concat (car tg)
  4297. (if (characterp (cdr tg))
  4298. (format "(%s)" (char-to-string (cdr tg))) "")))))
  4299. org-tag-alist)))
  4300. (let (e tgs g)
  4301. (while (setq e (pop tags))
  4302. (cond
  4303. ((equal e "{")
  4304. (progn (push '(:startgroup) tgs)
  4305. (when (equal (nth 1 tags) ":")
  4306. (push (list (replace-regexp-in-string
  4307. "(.+)$" "" (nth 0 tags)))
  4308. org-tag-groups-alist)
  4309. (setq g 0))))
  4310. ((equal e ":") (push '(:grouptags) tgs))
  4311. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4312. ((equal e "\\n") (push '(:newline) tgs))
  4313. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4314. (push (cons (match-string 1 e)
  4315. (string-to-char (match-string 2 e))) tgs)
  4316. (if (and g (> g 0))
  4317. (setcar org-tag-groups-alist
  4318. (append (car org-tag-groups-alist)
  4319. (list (match-string 1 e)))))
  4320. (if g (setq g (1+ g))))
  4321. (t (push (list e) tgs)
  4322. (if (and g (> g 0))
  4323. (setcar org-tag-groups-alist
  4324. (append (car org-tag-groups-alist) (list e))))
  4325. (if g (setq g (1+ g))))))
  4326. (org-set-local 'org-tag-alist nil)
  4327. (while (setq e (pop tgs))
  4328. (or (and (stringp (car e))
  4329. (assoc (car e) org-tag-alist))
  4330. (push e org-tag-alist)))
  4331. ;; Return a list with tag variables
  4332. (list org-file-tags org-tag-alist org-tag-groups-alist)))))
  4333. (defvar org-ota nil)
  4334. (defun org-set-regexps-and-options ()
  4335. "Precompute regular expressions used in the current buffer."
  4336. (when (derived-mode-p 'org-mode)
  4337. (org-set-local 'org-todo-kwd-alist nil)
  4338. (org-set-local 'org-todo-key-alist nil)
  4339. (org-set-local 'org-todo-key-trigger nil)
  4340. (org-set-local 'org-todo-keywords-1 nil)
  4341. (org-set-local 'org-done-keywords nil)
  4342. (org-set-local 'org-todo-heads nil)
  4343. (org-set-local 'org-todo-sets nil)
  4344. (org-set-local 'org-todo-log-states nil)
  4345. (org-set-local 'org-file-properties nil)
  4346. (let ((re (org-make-options-regexp
  4347. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4348. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4349. "SETUPFILE" "OPTIONS")
  4350. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4351. (splitre "[ \t]+")
  4352. (scripts org-use-sub-superscripts)
  4353. kwds kws0 kwsa key log value cat arch const links hw dws
  4354. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4355. (start 0))
  4356. (save-excursion
  4357. (save-restriction
  4358. (widen)
  4359. (goto-char (point-min))
  4360. (while
  4361. (or (and
  4362. ext-setup-or-nil
  4363. (not org-ota)
  4364. (let (ret)
  4365. (with-temp-buffer
  4366. (insert ext-setup-or-nil)
  4367. (let ((major-mode 'org-mode) org-ota)
  4368. (setq ret (save-match-data
  4369. (org-set-regexps-and-options-for-tags)))))
  4370. ;; Append setupfile tags to existing tags
  4371. (setq org-ota t)
  4372. (setq org-file-tags
  4373. (delq nil (append org-file-tags (nth 0 ret)))
  4374. org-tag-alist
  4375. (delq nil (append org-tag-alist (nth 1 ret)))
  4376. org-tag-groups-alist
  4377. (delq nil (append org-tag-groups-alist (nth 2 ret))))))
  4378. (and ext-setup-or-nil
  4379. (string-match re ext-setup-or-nil start)
  4380. (setq start (match-end 0)))
  4381. (and (setq ext-setup-or-nil nil start 0)
  4382. (re-search-forward re nil t)))
  4383. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4384. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4385. (if (stringp value) (setq value (org-trim value)))
  4386. (cond
  4387. ((equal key "CATEGORY")
  4388. (setq cat value))
  4389. ((member key '("SEQ_TODO" "TODO"))
  4390. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4391. ((equal key "TYP_TODO")
  4392. (push (cons 'type (org-split-string value splitre)) kwds))
  4393. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4394. ;; general TODO-like setup
  4395. (push (cons (intern (downcase (match-string 1 key)))
  4396. (org-split-string value splitre)) kwds))
  4397. ((equal key "COLUMNS")
  4398. (org-set-local 'org-columns-default-format value))
  4399. ((equal key "LINK")
  4400. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4401. (push (cons (match-string 1 value)
  4402. (org-trim (match-string 2 value)))
  4403. links)))
  4404. ((equal key "PRIORITIES")
  4405. (setq prio (org-split-string value " +")))
  4406. ((equal key "PROPERTY")
  4407. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4408. (setq props (org-update-property-plist (match-string 1 value)
  4409. (match-string 2 value)
  4410. props))))
  4411. ((equal key "CONSTANTS")
  4412. (org-table-set-constants))
  4413. ((equal key "STARTUP")
  4414. (let ((opts (org-split-string value splitre))
  4415. l var val)
  4416. (while (setq l (pop opts))
  4417. (when (setq l (assoc l org-startup-options))
  4418. (setq var (nth 1 l) val (nth 2 l))
  4419. (if (not (nth 3 l))
  4420. (set (make-local-variable var) val)
  4421. (if (not (listp (symbol-value var)))
  4422. (set (make-local-variable var) nil))
  4423. (set (make-local-variable var) (symbol-value var))
  4424. (add-to-list var val))))))
  4425. ((equal key "ARCHIVE")
  4426. (setq arch value)
  4427. (remove-text-properties 0 (length arch)
  4428. '(face t fontified t) arch))
  4429. ((equal key "OPTIONS")
  4430. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4431. (setq scripts (read (match-string 2 value)))))
  4432. ((and (equal key "SETUPFILE")
  4433. ;; Prevent checking in Gnus messages
  4434. (not buffer-read-only))
  4435. (setq setup-contents (org-file-contents
  4436. (expand-file-name
  4437. (org-remove-double-quotes value))
  4438. 'noerror))
  4439. (if (not ext-setup-or-nil)
  4440. (setq ext-setup-or-nil setup-contents start 0)
  4441. (setq ext-setup-or-nil
  4442. (concat (substring ext-setup-or-nil 0 start)
  4443. "\n" setup-contents "\n"
  4444. (substring ext-setup-or-nil start)))))))
  4445. ;; search for property blocks
  4446. (goto-char (point-min))
  4447. (while (re-search-forward org-block-regexp nil t)
  4448. (when (equal "PROPERTY" (upcase (match-string 1)))
  4449. (setq value (replace-regexp-in-string
  4450. "[\n\r]" " " (match-string 4)))
  4451. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4452. (setq props (org-update-property-plist (match-string 1 value)
  4453. (match-string 2 value)
  4454. props)))))))
  4455. (org-set-local 'org-use-sub-superscripts scripts)
  4456. (when cat
  4457. (org-set-local 'org-category (intern cat))
  4458. (push (cons "CATEGORY" cat) props))
  4459. (when prio
  4460. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4461. (setq prio (mapcar 'string-to-char prio))
  4462. (org-set-local 'org-highest-priority (nth 0 prio))
  4463. (org-set-local 'org-lowest-priority (nth 1 prio))
  4464. (org-set-local 'org-default-priority (nth 2 prio)))
  4465. (and props (org-set-local 'org-file-properties (nreverse props)))
  4466. (and arch (org-set-local 'org-archive-location arch))
  4467. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4468. ;; Process the TODO keywords
  4469. (unless kwds
  4470. ;; Use the global values as if they had been given locally.
  4471. (setq kwds (default-value 'org-todo-keywords))
  4472. (if (stringp (car kwds))
  4473. (setq kwds (list (cons org-todo-interpretation
  4474. (default-value 'org-todo-keywords)))))
  4475. (setq kwds (reverse kwds)))
  4476. (setq kwds (nreverse kwds))
  4477. (let (inter kws kw)
  4478. (while (setq kws (pop kwds))
  4479. (let ((kws (or
  4480. (run-hook-with-args-until-success
  4481. 'org-todo-setup-filter-hook kws)
  4482. kws)))
  4483. (setq inter (pop kws) sep (member "|" kws)
  4484. kws0 (delete "|" (copy-sequence kws))
  4485. kwsa nil
  4486. kws1 (mapcar
  4487. (lambda (x)
  4488. ;; 1 2
  4489. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4490. (progn
  4491. (setq kw (match-string 1 x)
  4492. key (and (match-end 2) (match-string 2 x))
  4493. log (org-extract-log-state-settings x))
  4494. (push (cons kw (and key (string-to-char key))) kwsa)
  4495. (and log (push log org-todo-log-states))
  4496. kw)
  4497. (error "Invalid TODO keyword %s" x)))
  4498. kws0)
  4499. kwsa (if kwsa (append '((:startgroup))
  4500. (nreverse kwsa)
  4501. '((:endgroup))))
  4502. hw (car kws1)
  4503. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4504. tail (list inter hw (car dws) (org-last dws))))
  4505. (add-to-list 'org-todo-heads hw 'append)
  4506. (push kws1 org-todo-sets)
  4507. (setq org-done-keywords (append org-done-keywords dws nil))
  4508. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4509. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4510. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4511. (setq org-todo-sets (nreverse org-todo-sets)
  4512. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4513. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4514. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4515. ;; Compute the regular expressions and other local variables.
  4516. ;; Using `org-outline-regexp-bol' would complicate them much,
  4517. ;; because of the fixed white space at the end of that string.
  4518. (if (not org-done-keywords)
  4519. (setq org-done-keywords (and org-todo-keywords-1
  4520. (list (org-last org-todo-keywords-1)))))
  4521. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4522. (length org-scheduled-string)
  4523. (length org-clock-string)
  4524. (length org-closed-string)))
  4525. org-not-done-keywords
  4526. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4527. org-todo-regexp
  4528. (concat "\\("
  4529. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4530. "\\)")
  4531. org-not-done-regexp
  4532. (concat "\\("
  4533. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4534. "\\)")
  4535. org-not-done-heading-regexp
  4536. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4537. org-todo-line-regexp
  4538. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4539. org-complex-heading-regexp
  4540. (concat "^\\(\\*+\\)"
  4541. "\\(?: +" org-todo-regexp "\\)?"
  4542. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4543. "\\(?: +\\(.*?\\)\\)??"
  4544. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4545. "[ \t]*$")
  4546. org-complex-heading-regexp-format
  4547. (concat "^\\(\\*+\\)"
  4548. "\\(?: +" org-todo-regexp "\\)?"
  4549. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4550. "\\(?: +"
  4551. ;; Stats cookies can be stuck to body.
  4552. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4553. "\\(%s\\)"
  4554. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4555. "\\)"
  4556. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4557. "[ \t]*$")
  4558. org-todo-line-tags-regexp
  4559. (concat "^\\(\\*+\\)"
  4560. "\\(?: +" org-todo-regexp "\\)?"
  4561. "\\(?: +\\(.*?\\)\\)??"
  4562. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4563. "[ \t]*$")
  4564. org-deadline-regexp (concat "\\<" org-deadline-string)
  4565. org-deadline-time-regexp
  4566. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4567. org-deadline-time-hour-regexp
  4568. (concat "\\<" org-deadline-string
  4569. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4570. org-deadline-line-regexp
  4571. (concat "\\<\\(" org-deadline-string "\\).*")
  4572. org-scheduled-regexp
  4573. (concat "\\<" org-scheduled-string)
  4574. org-scheduled-time-regexp
  4575. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4576. org-scheduled-time-hour-regexp
  4577. (concat "\\<" org-scheduled-string
  4578. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  4579. org-closed-time-regexp
  4580. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4581. org-keyword-time-regexp
  4582. (concat "\\<\\(" org-scheduled-string
  4583. "\\|" org-deadline-string
  4584. "\\|" org-closed-string
  4585. "\\|" org-clock-string "\\)"
  4586. " *[[<]\\([^]>]+\\)[]>]")
  4587. org-keyword-time-not-clock-regexp
  4588. (concat "\\<\\(" org-scheduled-string
  4589. "\\|" org-deadline-string
  4590. "\\|" org-closed-string
  4591. "\\)"
  4592. " *[[<]\\([^]>]+\\)[]>]")
  4593. org-maybe-keyword-time-regexp
  4594. (concat "\\(\\<\\(" org-scheduled-string
  4595. "\\|" org-deadline-string
  4596. "\\|" org-closed-string
  4597. "\\|" org-clock-string "\\)\\)?"
  4598. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4599. org-all-time-keywords
  4600. (mapcar (lambda (w) (substring w 0 -1))
  4601. (list org-scheduled-string org-deadline-string
  4602. org-clock-string org-closed-string)))
  4603. (setq org-ota nil)
  4604. (org-compute-latex-and-related-regexp))))
  4605. (defun org-file-contents (file &optional noerror)
  4606. "Return the contents of FILE, as a string."
  4607. (if (and file (file-readable-p file))
  4608. (with-temp-buffer
  4609. (insert-file-contents file)
  4610. (buffer-string))
  4611. (funcall (if noerror #'message #'error)
  4612. "Cannot read file \"%s\"%s"
  4613. file
  4614. (let ((from (buffer-file-name (buffer-base-buffer))))
  4615. (if from (concat " (referenced in file \"" from "\")") "")))))
  4616. (defun org-extract-log-state-settings (x)
  4617. "Extract the log state setting from a TODO keyword string.
  4618. This will extract info from a string like \"WAIT(w@/!)\"."
  4619. (let (kw key log1 log2)
  4620. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4621. (setq kw (match-string 1 x)
  4622. key (and (match-end 2) (match-string 2 x))
  4623. log1 (and (match-end 3) (match-string 3 x))
  4624. log2 (and (match-end 4) (match-string 4 x)))
  4625. (and (or log1 log2)
  4626. (list kw
  4627. (and log1 (if (equal log1 "!") 'time 'note))
  4628. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4629. (defun org-remove-keyword-keys (list)
  4630. "Remove a pair of parenthesis at the end of each string in LIST."
  4631. (mapcar (lambda (x)
  4632. (if (string-match "(.*)$" x)
  4633. (substring x 0 (match-beginning 0))
  4634. x))
  4635. list))
  4636. (defun org-assign-fast-keys (alist)
  4637. "Assign fast keys to a keyword-key alist.
  4638. Respect keys that are already there."
  4639. (let (new e (alt ?0))
  4640. (while (setq e (pop alist))
  4641. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4642. (cdr e)) ;; Key already assigned.
  4643. (push e new)
  4644. (let ((clist (string-to-list (downcase (car e))))
  4645. (used (append new alist)))
  4646. (when (= (car clist) ?@)
  4647. (pop clist))
  4648. (while (and clist (rassoc (car clist) used))
  4649. (pop clist))
  4650. (unless clist
  4651. (while (rassoc alt used)
  4652. (incf alt)))
  4653. (push (cons (car e) (or (car clist) alt)) new))))
  4654. (nreverse new)))
  4655. ;;; Some variables used in various places
  4656. (defvar org-window-configuration nil
  4657. "Used in various places to store a window configuration.")
  4658. (defvar org-selected-window nil
  4659. "Used in various places to store a window configuration.")
  4660. (defvar org-finish-function nil
  4661. "Function to be called when `C-c C-c' is used.
  4662. This is for getting out of special buffers like capture.")
  4663. ;; FIXME: Occasionally check by commenting these, to make sure
  4664. ;; no other functions uses these, forgetting to let-bind them.
  4665. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4666. (defvar org-last-state)
  4667. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4668. ;; Defined somewhere in this file, but used before definition.
  4669. (defvar org-entities) ;; defined in org-entities.el
  4670. (defvar org-struct-menu)
  4671. (defvar org-org-menu)
  4672. (defvar org-tbl-menu)
  4673. ;;;; Define the Org-mode
  4674. ;; We use a before-change function to check if a table might need
  4675. ;; an update.
  4676. (defvar org-table-may-need-update t
  4677. "Indicates that a table might need an update.
  4678. This variable is set by `org-before-change-function'.
  4679. `org-table-align' sets it back to nil.")
  4680. (defun org-before-change-function (beg end)
  4681. "Every change indicates that a table might need an update."
  4682. (setq org-table-may-need-update t))
  4683. (defvar org-mode-map)
  4684. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4685. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4686. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4687. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4688. (defvar org-table-buffer-is-an nil)
  4689. (defvar bidi-paragraph-direction)
  4690. (defvar buffer-face-mode-face)
  4691. (require 'outline)
  4692. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4693. (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"))
  4694. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4695. ;; Other stuff we need.
  4696. (require 'time-date)
  4697. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4698. (require 'easymenu)
  4699. (require 'overlay)
  4700. ;; (require 'org-macs) moved higher up in the file before it is first used
  4701. (require 'org-entities)
  4702. ;; (require 'org-compat) moved higher up in the file before it is first used
  4703. (require 'org-faces)
  4704. (require 'org-list)
  4705. (require 'org-pcomplete)
  4706. (require 'org-src)
  4707. (require 'org-footnote)
  4708. (require 'org-macro)
  4709. ;; babel
  4710. (require 'ob)
  4711. ;;;###autoload
  4712. (define-derived-mode org-mode outline-mode "Org"
  4713. "Outline-based notes management and organizer, alias
  4714. \"Carsten's outline-mode for keeping track of everything.\"
  4715. Org-mode develops organizational tasks around a NOTES file which
  4716. contains information about projects as plain text. Org-mode is
  4717. implemented on top of outline-mode, which is ideal to keep the content
  4718. of large files well structured. It supports ToDo items, deadlines and
  4719. time stamps, which magically appear in the diary listing of the Emacs
  4720. calendar. Tables are easily created with a built-in table editor.
  4721. Plain text URL-like links connect to websites, emails (VM), Usenet
  4722. messages (Gnus), BBDB entries, and any files related to the project.
  4723. For printing and sharing of notes, an Org-mode file (or a part of it)
  4724. can be exported as a structured ASCII or HTML file.
  4725. The following commands are available:
  4726. \\{org-mode-map}"
  4727. ;; Get rid of Outline menus, they are not needed
  4728. ;; Need to do this here because define-derived-mode sets up
  4729. ;; the keymap so late. Still, it is a waste to call this each time
  4730. ;; we switch another buffer into org-mode.
  4731. (if (featurep 'xemacs)
  4732. (when (boundp 'outline-mode-menu-heading)
  4733. ;; Assume this is Greg's port, it uses easymenu
  4734. (easy-menu-remove outline-mode-menu-heading)
  4735. (easy-menu-remove outline-mode-menu-show)
  4736. (easy-menu-remove outline-mode-menu-hide))
  4737. (define-key org-mode-map [menu-bar headings] 'undefined)
  4738. (define-key org-mode-map [menu-bar hide] 'undefined)
  4739. (define-key org-mode-map [menu-bar show] 'undefined))
  4740. (org-load-modules-maybe)
  4741. (easy-menu-add org-org-menu)
  4742. (easy-menu-add org-tbl-menu)
  4743. (org-install-agenda-files-menu)
  4744. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4745. (add-to-invisibility-spec '(org-cwidth))
  4746. (add-to-invisibility-spec '(org-hide-block . t))
  4747. (when (featurep 'xemacs)
  4748. (org-set-local 'line-move-ignore-invisible t))
  4749. (org-set-local 'outline-regexp org-outline-regexp)
  4750. (org-set-local 'outline-level 'org-outline-level)
  4751. (setq bidi-paragraph-direction 'left-to-right)
  4752. (when (and org-ellipsis
  4753. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4754. (fboundp 'make-glyph-code))
  4755. (unless org-display-table
  4756. (setq org-display-table (make-display-table)))
  4757. (set-display-table-slot
  4758. org-display-table 4
  4759. (vconcat (mapcar
  4760. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4761. org-ellipsis)))
  4762. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4763. (setq buffer-display-table org-display-table))
  4764. (org-set-regexps-and-options-for-tags)
  4765. (org-set-regexps-and-options)
  4766. (org-set-font-lock-defaults)
  4767. (when (and org-tag-faces (not org-tags-special-faces-re))
  4768. ;; tag faces set outside customize.... force initialization.
  4769. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4770. ;; Calc embedded
  4771. (org-set-local 'calc-embedded-open-mode "# ")
  4772. ;; Modify a few syntax entries
  4773. (modify-syntax-entry ?@ "w")
  4774. (modify-syntax-entry ?\" "\"")
  4775. (if org-startup-truncated (setq truncate-lines t))
  4776. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4777. (org-set-local 'font-lock-unfontify-region-function
  4778. 'org-unfontify-region)
  4779. ;; Activate before-change-function
  4780. (org-set-local 'org-table-may-need-update t)
  4781. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4782. 'local)
  4783. ;; Check for running clock before killing a buffer
  4784. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4785. ;; Initialize macros templates.
  4786. (org-macro-initialize-templates)
  4787. ;; Initialize radio targets.
  4788. (org-update-radio-target-regexp)
  4789. ;; Indentation.
  4790. (org-set-local 'indent-line-function 'org-indent-line)
  4791. (org-set-local 'indent-region-function 'org-indent-region)
  4792. ;; Filling and auto-filling.
  4793. (org-setup-filling)
  4794. ;; Comments.
  4795. (org-setup-comments-handling)
  4796. ;; Initialize cache.
  4797. (org-element-cache-reset)
  4798. ;; Beginning/end of defun
  4799. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4800. (org-set-local 'end-of-defun-function 'org-forward-element)
  4801. ;; Next error for sparse trees
  4802. (org-set-local 'next-error-function 'org-occur-next-match)
  4803. ;; Make sure dependence stuff works reliably, even for users who set it
  4804. ;; too late :-(
  4805. (if org-enforce-todo-dependencies
  4806. (add-hook 'org-blocker-hook
  4807. 'org-block-todo-from-children-or-siblings-or-parent)
  4808. (remove-hook 'org-blocker-hook
  4809. 'org-block-todo-from-children-or-siblings-or-parent))
  4810. (if org-enforce-todo-checkbox-dependencies
  4811. (add-hook 'org-blocker-hook
  4812. 'org-block-todo-from-checkboxes)
  4813. (remove-hook 'org-blocker-hook
  4814. 'org-block-todo-from-checkboxes))
  4815. ;; Align options lines
  4816. (org-set-local
  4817. 'align-mode-rules-list
  4818. '((org-in-buffer-settings
  4819. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4820. (modes . '(org-mode)))))
  4821. ;; Imenu
  4822. (org-set-local 'imenu-create-index-function
  4823. 'org-imenu-get-tree)
  4824. ;; Make isearch reveal context
  4825. (if (or (featurep 'xemacs)
  4826. (not (boundp 'outline-isearch-open-invisible-function)))
  4827. ;; Emacs 21 and XEmacs make use of the hook
  4828. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4829. ;; Emacs 22 deals with this through a special variable
  4830. (org-set-local 'outline-isearch-open-invisible-function
  4831. (lambda (&rest ignore) (org-show-context 'isearch)))
  4832. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4833. ;; Setup the pcomplete hooks
  4834. (set (make-local-variable 'pcomplete-command-completion-function)
  4835. 'org-pcomplete-initial)
  4836. (set (make-local-variable 'pcomplete-command-name-function)
  4837. 'org-command-at-point)
  4838. (set (make-local-variable 'pcomplete-default-completion-function)
  4839. 'ignore)
  4840. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4841. 'org-parse-arguments)
  4842. (set (make-local-variable 'pcomplete-termination-string) "")
  4843. (when (>= emacs-major-version 23)
  4844. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4845. ;; If empty file that did not turn on org-mode automatically, make it to.
  4846. (if (and org-insert-mode-line-in-empty-file
  4847. (org-called-interactively-p 'any)
  4848. (= (point-min) (point-max)))
  4849. (insert "# -*- mode: org -*-\n\n"))
  4850. (unless org-inhibit-startup
  4851. (org-unmodified
  4852. (and org-startup-with-beamer-mode (org-beamer-mode))
  4853. (when org-startup-align-all-tables
  4854. (org-table-map-tables 'org-table-align 'quietly))
  4855. (when org-startup-with-inline-images
  4856. (org-display-inline-images))
  4857. (when org-startup-with-latex-preview
  4858. (org-preview-latex-fragment))
  4859. (unless org-inhibit-startup-visibility-stuff
  4860. (org-set-startup-visibility))))
  4861. ;; Try to set org-hide correctly
  4862. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4863. ;; Update `customize-package-emacs-version-alist'
  4864. (add-to-list 'customize-package-emacs-version-alist
  4865. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4866. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4867. ("8.2.6" . "24.4")))
  4868. (defvar org-mode-transpose-word-syntax-table
  4869. (let ((st (make-syntax-table)))
  4870. (mapc (lambda(c) (modify-syntax-entry
  4871. (string-to-char (car c)) "w p" st))
  4872. org-emphasis-alist)
  4873. st))
  4874. (when (fboundp 'abbrev-table-put)
  4875. (abbrev-table-put org-mode-abbrev-table
  4876. :parents (list text-mode-abbrev-table)))
  4877. (defsubst org-fix-ellipsis-at-bol ()
  4878. (save-excursion (goto-char (window-start)) (recenter 0)))
  4879. (defun org-find-invisible-foreground ()
  4880. (let ((candidates (remove
  4881. "unspecified-bg"
  4882. (nconc
  4883. (list (face-background 'default)
  4884. (face-background 'org-default))
  4885. (mapcar
  4886. (lambda (alist)
  4887. (when (boundp alist)
  4888. (cdr (assoc 'background-color (symbol-value alist)))))
  4889. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4890. (list (face-foreground 'org-hide))))))
  4891. (car (remove nil candidates))))
  4892. (defun org-current-time (&optional rounding-minutes past)
  4893. "Current time, possibly rounded to ROUNDING-MINUTES.
  4894. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4895. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4896. the rounding returns a past time."
  4897. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4898. (car org-time-stamp-rounding-minutes)))
  4899. (time (decode-time)) res)
  4900. (if (< r 1)
  4901. (current-time)
  4902. (setq res
  4903. (apply 'encode-time
  4904. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4905. (nthcdr 2 time))))
  4906. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4907. (seconds-to-time (- (org-float-time res) (* r 60)))
  4908. res))))
  4909. (defun org-today ()
  4910. "Return today date, considering `org-extend-today-until'."
  4911. (time-to-days
  4912. (time-subtract (current-time)
  4913. (list 0 (* 3600 org-extend-today-until) 0))))
  4914. ;;;; Font-Lock stuff, including the activators
  4915. (defvar org-mouse-map (make-sparse-keymap))
  4916. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4917. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4918. (when org-mouse-1-follows-link
  4919. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4920. (when org-tab-follows-link
  4921. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4922. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4923. (require 'font-lock)
  4924. (defconst org-non-link-chars "]\t\n\r<>")
  4925. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4926. "file+sys" "news" "shell" "elisp" "doi" "message"
  4927. "help"))
  4928. (defvar org-link-types-re nil
  4929. "Matches a link that has a url-like prefix like \"http:\"")
  4930. (defvar org-link-re-with-space nil
  4931. "Matches a link with spaces, optional angular brackets around it.")
  4932. (defvar org-link-re-with-space2 nil
  4933. "Matches a link with spaces, optional angular brackets around it.")
  4934. (defvar org-link-re-with-space3 nil
  4935. "Matches a link with spaces, only for internal part in bracket links.")
  4936. (defvar org-angle-link-re nil
  4937. "Matches link with angular brackets, spaces are allowed.")
  4938. (defvar org-plain-link-re nil
  4939. "Matches plain link, without spaces.")
  4940. (defvar org-bracket-link-regexp nil
  4941. "Matches a link in double brackets.")
  4942. (defvar org-bracket-link-analytic-regexp nil
  4943. "Regular expression used to analyze links.
  4944. Here is what the match groups contain after a match:
  4945. 1: http:
  4946. 2: http
  4947. 3: path
  4948. 4: [desc]
  4949. 5: desc")
  4950. (defvar org-bracket-link-analytic-regexp++ nil
  4951. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4952. (defvar org-any-link-re nil
  4953. "Regular expression matching any link.")
  4954. (defconst org-match-sexp-depth 3
  4955. "Number of stacked braces for sub/superscript matching.")
  4956. (defun org-create-multibrace-regexp (left right n)
  4957. "Create a regular expression which will match a balanced sexp.
  4958. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4959. as single character strings.
  4960. The regexp returned will match the entire expression including the
  4961. delimiters. It will also define a single group which contains the
  4962. match except for the outermost delimiters. The maximum depth of
  4963. stacked delimiters is N. Escaping delimiters is not possible."
  4964. (let* ((nothing (concat "[^" left right "]*?"))
  4965. (or "\\|")
  4966. (re nothing)
  4967. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4968. (while (> n 1)
  4969. (setq n (1- n)
  4970. re (concat re or next)
  4971. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4972. (concat left "\\(" re "\\)" right)))
  4973. (defconst org-match-substring-regexp
  4974. (concat
  4975. "\\(\\S-\\)\\([_^]\\)\\("
  4976. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4977. "\\|"
  4978. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4979. "\\|"
  4980. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4981. "The regular expression matching a sub- or superscript.")
  4982. (defconst org-match-substring-with-braces-regexp
  4983. (concat
  4984. "\\(\\S-\\)\\([_^]\\)"
  4985. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4986. "The regular expression matching a sub- or superscript, forcing braces.")
  4987. (defun org-make-link-regexps ()
  4988. "Update the link regular expressions.
  4989. This should be called after the variable `org-link-types' has changed."
  4990. (setq org-link-types-re
  4991. (concat
  4992. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4993. org-link-re-with-space
  4994. (concat
  4995. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4996. "\\([^" org-non-link-chars " ]"
  4997. "[^" org-non-link-chars "]*"
  4998. "[^" org-non-link-chars " ]\\)>?")
  4999. org-link-re-with-space2
  5000. (concat
  5001. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  5002. "\\([^" org-non-link-chars " ]"
  5003. "[^\t\n\r]*"
  5004. "[^" org-non-link-chars " ]\\)>?")
  5005. org-link-re-with-space3
  5006. (concat
  5007. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  5008. "\\([^" org-non-link-chars " ]"
  5009. "[^\t\n\r]*\\)")
  5010. org-angle-link-re
  5011. (concat
  5012. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  5013. "\\([^" org-non-link-chars " ]"
  5014. "[^" org-non-link-chars "]*"
  5015. "\\)>")
  5016. org-plain-link-re
  5017. (concat
  5018. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  5019. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5020. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5021. org-bracket-link-regexp
  5022. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5023. org-bracket-link-analytic-regexp
  5024. (concat
  5025. "\\[\\["
  5026. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  5027. "\\([^]]+\\)"
  5028. "\\]"
  5029. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5030. "\\]")
  5031. org-bracket-link-analytic-regexp++
  5032. (concat
  5033. "\\[\\["
  5034. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  5035. "\\([^]]+\\)"
  5036. "\\]"
  5037. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5038. "\\]")
  5039. org-any-link-re
  5040. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5041. org-angle-link-re "\\)\\|\\("
  5042. org-plain-link-re "\\)")))
  5043. (org-make-link-regexps)
  5044. (defvar org-emph-face nil)
  5045. (defun org-do-emphasis-faces (limit)
  5046. "Run through the buffer and add overlays to emphasized strings."
  5047. (let (rtn a)
  5048. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5049. (if (not (= (char-after (match-beginning 3))
  5050. (char-after (match-beginning 4))))
  5051. (progn
  5052. (setq rtn t)
  5053. (setq a (assoc (match-string 3) org-emphasis-alist))
  5054. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5055. 'face
  5056. (nth 1 a))
  5057. (and (nth 2 a)
  5058. (org-remove-flyspell-overlays-in
  5059. (match-beginning 0) (match-end 0)))
  5060. (add-text-properties (match-beginning 2) (match-end 2)
  5061. '(font-lock-multiline t org-emphasis t))
  5062. (when org-hide-emphasis-markers
  5063. (add-text-properties (match-end 4) (match-beginning 5)
  5064. '(invisible org-link))
  5065. (add-text-properties (match-beginning 3) (match-end 3)
  5066. '(invisible org-link)))))
  5067. (goto-char (1+ (match-beginning 0))))
  5068. rtn))
  5069. (defun org-emphasize (&optional char)
  5070. "Insert or change an emphasis, i.e. a font like bold or italic.
  5071. If there is an active region, change that region to a new emphasis.
  5072. If there is no region, just insert the marker characters and position
  5073. the cursor between them.
  5074. CHAR should be the marker character. If it is a space, it means to
  5075. remove the emphasis of the selected region.
  5076. If CHAR is not given (for example in an interactive call) it will be
  5077. prompted for."
  5078. (interactive)
  5079. (let ((erc org-emphasis-regexp-components)
  5080. (prompt "")
  5081. (string "") beg end move c s)
  5082. (if (org-region-active-p)
  5083. (setq beg (region-beginning) end (region-end)
  5084. string (buffer-substring beg end))
  5085. (setq move t))
  5086. (unless char
  5087. (message "Emphasis marker or tag: [%s]"
  5088. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5089. (setq char (read-char-exclusive)))
  5090. (if (equal char ?\ )
  5091. (setq s "" move nil)
  5092. (unless (assoc (char-to-string char) org-emphasis-alist)
  5093. (user-error "No such emphasis marker: \"%c\"" char))
  5094. (setq s (char-to-string char)))
  5095. (while (and (> (length string) 1)
  5096. (equal (substring string 0 1) (substring string -1))
  5097. (assoc (substring string 0 1) org-emphasis-alist))
  5098. (setq string (substring string 1 -1)))
  5099. (setq string (concat s string s))
  5100. (if beg (delete-region beg end))
  5101. (unless (or (bolp)
  5102. (string-match (concat "[" (nth 0 erc) "\n]")
  5103. (char-to-string (char-before (point)))))
  5104. (insert " "))
  5105. (unless (or (eobp)
  5106. (string-match (concat "[" (nth 1 erc) "\n]")
  5107. (char-to-string (char-after (point)))))
  5108. (insert " ") (backward-char 1))
  5109. (insert string)
  5110. (and move (backward-char 1))))
  5111. (defconst org-nonsticky-props
  5112. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5113. (defsubst org-rear-nonsticky-at (pos)
  5114. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5115. (defun org-activate-plain-links (limit)
  5116. "Run through the buffer and add overlays to links."
  5117. (let (f hl)
  5118. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5119. (not (org-in-src-block-p)))
  5120. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5121. (setq f (get-text-property (match-beginning 0) 'face))
  5122. (setq hl (org-match-string-no-properties 0))
  5123. (if (or (eq f 'org-tag)
  5124. (and (listp f) (memq 'org-tag f)))
  5125. nil
  5126. (add-text-properties (match-beginning 0) (match-end 0)
  5127. (list 'mouse-face 'highlight
  5128. 'face 'org-link
  5129. 'htmlize-link `(:uri ,hl)
  5130. 'keymap org-mouse-map))
  5131. (org-rear-nonsticky-at (match-end 0)))
  5132. t)))
  5133. (defun org-activate-code (limit)
  5134. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5135. (progn
  5136. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5137. (remove-text-properties (match-beginning 0) (match-end 0)
  5138. '(display t invisible t intangible t))
  5139. t)))
  5140. (defcustom org-src-fontify-natively t
  5141. "When non-nil, fontify code in code blocks."
  5142. :type 'boolean
  5143. :version "24.4"
  5144. :package-version '(Org . "8.3")
  5145. :group 'org-appearance
  5146. :group 'org-babel)
  5147. (defcustom org-allow-promoting-top-level-subtree nil
  5148. "When non-nil, allow promoting a top level subtree.
  5149. The leading star of the top level headline will be replaced
  5150. by a #."
  5151. :type 'boolean
  5152. :version "24.1"
  5153. :group 'org-appearance)
  5154. (defun org-fontify-meta-lines-and-blocks (limit)
  5155. (condition-case nil
  5156. (org-fontify-meta-lines-and-blocks-1 limit)
  5157. (error (message "org-mode fontification error"))))
  5158. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5159. "Fontify #+ lines and blocks."
  5160. (let ((case-fold-search t))
  5161. (if (re-search-forward
  5162. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5163. limit t)
  5164. (let ((beg (match-beginning 0))
  5165. (block-start (match-end 0))
  5166. (block-end nil)
  5167. (lang (match-string 7))
  5168. (beg1 (line-beginning-position 2))
  5169. (dc1 (downcase (match-string 2)))
  5170. (dc3 (downcase (match-string 3)))
  5171. end end1 quoting block-type ovl)
  5172. (cond
  5173. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5174. ;; a single line of backend-specific content
  5175. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5176. (remove-text-properties (match-beginning 0) (match-end 0)
  5177. '(display t invisible t intangible t))
  5178. (add-text-properties (match-beginning 1) (match-end 3)
  5179. '(font-lock-fontified t face org-meta-line))
  5180. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5181. '(font-lock-fontified t face org-block))
  5182. ; for backend-specific code
  5183. t)
  5184. ((and (match-end 4) (equal dc3 "+begin"))
  5185. ;; Truly a block
  5186. (setq block-type (downcase (match-string 5))
  5187. quoting (member block-type org-protecting-blocks))
  5188. (when (re-search-forward
  5189. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5190. nil t) ;; on purpose, we look further than LIMIT
  5191. (setq end (min (point-max) (match-end 0))
  5192. end1 (min (point-max) (1- (match-beginning 0))))
  5193. (setq block-end (match-beginning 0))
  5194. (when quoting
  5195. (org-remove-flyspell-overlays-in beg1 end1)
  5196. (remove-text-properties beg end
  5197. '(display t invisible t intangible t)))
  5198. (add-text-properties
  5199. beg end '(font-lock-fontified t font-lock-multiline t))
  5200. (add-text-properties beg beg1 '(face org-meta-line))
  5201. (org-remove-flyspell-overlays-in beg beg1)
  5202. (add-text-properties ; For end_src
  5203. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5204. (org-remove-flyspell-overlays-in end1 end)
  5205. (cond
  5206. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5207. (org-src-font-lock-fontify-block lang block-start block-end)
  5208. ;; remove old background overlays
  5209. (mapc (lambda (ov)
  5210. (if (eq (overlay-get ov 'face) 'org-block-background)
  5211. (delete-overlay ov)))
  5212. (overlays-at (/ (+ beg1 block-end) 2)))
  5213. ;; add a background overlay
  5214. (setq ovl (make-overlay beg1 block-end))
  5215. (overlay-put ovl 'face 'org-block-background)
  5216. (overlay-put ovl 'evaporate t)) ; make it go away when empty
  5217. (quoting
  5218. (add-text-properties beg1 (min (point-max) (1+ end1))
  5219. '(face org-block))) ; end of source block
  5220. ((not org-fontify-quote-and-verse-blocks))
  5221. ((string= block-type "quote")
  5222. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5223. ((string= block-type "verse")
  5224. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5225. (add-text-properties beg beg1 '(face org-block-begin-line))
  5226. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5227. '(face org-block-end-line))
  5228. t))
  5229. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5230. (org-remove-flyspell-overlays-in
  5231. (match-beginning 0)
  5232. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5233. (add-text-properties
  5234. beg (match-end 3)
  5235. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5236. '(font-lock-fontified t invisible t)
  5237. '(font-lock-fontified t face org-document-info-keyword)))
  5238. (add-text-properties
  5239. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5240. (if (string-equal dc1 "+title:")
  5241. '(font-lock-fontified t face org-document-title)
  5242. '(font-lock-fontified t face org-document-info))))
  5243. ((or (equal dc1 "+results")
  5244. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5245. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5246. "+call:" "+header:" "+headers:" "+name:"))
  5247. (and (match-end 4) (equal dc3 "+attr")))
  5248. (org-remove-flyspell-overlays-in
  5249. (match-beginning 0)
  5250. (if (equal "+caption:" dc1) (match-end 2) (match-end 0)))
  5251. (add-text-properties
  5252. beg (match-end 0)
  5253. '(font-lock-fontified t face org-meta-line))
  5254. t)
  5255. ((member dc3 '(" " ""))
  5256. (org-remove-flyspell-overlays-in beg (match-end 0))
  5257. (add-text-properties
  5258. beg (match-end 0)
  5259. '(font-lock-fontified t face font-lock-comment-face)))
  5260. ((not (member (char-after beg) '(?\ ?\t)))
  5261. ;; just any other in-buffer setting, but not indented
  5262. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5263. (add-text-properties
  5264. beg (match-end 0)
  5265. '(font-lock-fontified t face org-meta-line))
  5266. t)
  5267. (t nil))))))
  5268. (defun org-fontify-drawers (limit)
  5269. "Fontify drawers."
  5270. (when (re-search-forward org-drawer-regexp limit t)
  5271. (add-text-properties
  5272. (match-beginning 0) (match-end 0)
  5273. '(font-lock-fontified t face org-special-keyword))
  5274. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5275. t))
  5276. (defun org-fontify-macros (limit)
  5277. "Fontify macros."
  5278. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5279. (add-text-properties
  5280. (match-beginning 0) (match-end 0)
  5281. '(font-lock-fontified t face org-macro))
  5282. (when org-hide-macro-markers
  5283. (add-text-properties (match-end 2) (match-beginning 2)
  5284. '(invisible t))
  5285. (add-text-properties (match-beginning 1) (match-end 1)
  5286. '(invisible t)))
  5287. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5288. t))
  5289. (defun org-activate-angle-links (limit)
  5290. "Run through the buffer and add overlays to links."
  5291. (if (and (re-search-forward org-angle-link-re limit t)
  5292. (not (org-in-src-block-p)))
  5293. (progn
  5294. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5295. (add-text-properties (match-beginning 0) (match-end 0)
  5296. (list 'mouse-face 'highlight
  5297. 'keymap org-mouse-map))
  5298. (org-rear-nonsticky-at (match-end 0))
  5299. t)))
  5300. (defun org-activate-footnote-links (limit)
  5301. "Run through the buffer and add overlays to footnotes."
  5302. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5303. (when fn
  5304. (let* ((beg (nth 1 fn))
  5305. (end (nth 2 fn))
  5306. (label (car fn))
  5307. (referencep (/= (line-beginning-position) beg)))
  5308. (when (and referencep (nth 3 fn))
  5309. (save-excursion
  5310. (goto-char beg)
  5311. (search-forward (or label "fn:"))
  5312. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5313. (add-text-properties beg end
  5314. (list 'mouse-face 'highlight
  5315. 'keymap org-mouse-map
  5316. 'help-echo
  5317. (if referencep "Footnote reference"
  5318. "Footnote definition")
  5319. 'font-lock-fontified t
  5320. 'font-lock-multiline t
  5321. 'face 'org-footnote))))))
  5322. (defun org-activate-bracket-links (limit)
  5323. "Run through the buffer and add overlays to bracketed links."
  5324. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5325. (not (org-in-src-block-p)))
  5326. (let* ((hl (org-match-string-no-properties 1))
  5327. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5328. (ip (org-maybe-intangible
  5329. (list 'invisible 'org-link
  5330. 'keymap org-mouse-map 'mouse-face 'highlight
  5331. 'font-lock-multiline t 'help-echo help
  5332. 'htmlize-link `(:uri ,hl))))
  5333. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5334. 'font-lock-multiline t 'help-echo help
  5335. 'htmlize-link `(:uri ,hl))))
  5336. ;; We need to remove the invisible property here. Table narrowing
  5337. ;; may have made some of this invisible.
  5338. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5339. (remove-text-properties (match-beginning 0) (match-end 0)
  5340. '(invisible nil))
  5341. (if (match-end 3)
  5342. (progn
  5343. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5344. (org-rear-nonsticky-at (match-beginning 3))
  5345. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5346. (org-rear-nonsticky-at (match-end 3))
  5347. (add-text-properties (match-end 3) (match-end 0) ip)
  5348. (org-rear-nonsticky-at (match-end 0)))
  5349. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5350. (org-rear-nonsticky-at (match-beginning 1))
  5351. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5352. (org-rear-nonsticky-at (match-end 1))
  5353. (add-text-properties (match-end 1) (match-end 0) ip)
  5354. (org-rear-nonsticky-at (match-end 0)))
  5355. t)))
  5356. (defun org-activate-dates (limit)
  5357. "Run through the buffer and add overlays to dates."
  5358. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5359. (not (equal (char-before (match-beginning 0)) 91)))
  5360. (progn
  5361. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5362. (add-text-properties (match-beginning 0) (match-end 0)
  5363. (list 'mouse-face 'highlight
  5364. 'keymap org-mouse-map))
  5365. (org-rear-nonsticky-at (match-end 0))
  5366. (when org-display-custom-times
  5367. (if (match-end 3)
  5368. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5369. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5370. t)))
  5371. (defvar org-target-link-regexp nil
  5372. "Regular expression matching radio targets in plain text.")
  5373. (make-variable-buffer-local 'org-target-link-regexp)
  5374. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5375. "Regular expression matching a link target.")
  5376. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5377. "Regular expression matching a radio target.")
  5378. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5379. "Regular expression matching any target.")
  5380. (defun org-activate-target-links (limit)
  5381. "Run through the buffer and add overlays to target matches."
  5382. (when org-target-link-regexp
  5383. (let ((case-fold-search t))
  5384. (if (re-search-forward org-target-link-regexp limit t)
  5385. (progn
  5386. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5387. (add-text-properties (match-beginning 0) (match-end 0)
  5388. (list 'mouse-face 'highlight
  5389. 'keymap org-mouse-map
  5390. 'help-echo "Radio target link"
  5391. 'org-linked-text t))
  5392. (org-rear-nonsticky-at (match-end 0))
  5393. t)))))
  5394. (defun org-update-radio-target-regexp ()
  5395. "Find all radio targets in this file and update the regular expression."
  5396. (interactive)
  5397. (when (memq 'radio org-activate-links)
  5398. (setq org-target-link-regexp
  5399. (org-make-target-link-regexp (org-all-targets 'radio)))
  5400. (org-restart-font-lock)))
  5401. (defun org-hide-wide-columns (limit)
  5402. (let (s e)
  5403. (setq s (text-property-any (point) (or limit (point-max))
  5404. 'org-cwidth t))
  5405. (when s
  5406. (setq e (next-single-property-change s 'org-cwidth))
  5407. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5408. (goto-char e)
  5409. t)))
  5410. (defvar org-latex-and-related-regexp nil
  5411. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5412. (defun org-compute-latex-and-related-regexp ()
  5413. "Compute regular expression for LaTeX, entities and sub/superscript.
  5414. Result depends on variable `org-highlight-latex-and-related'."
  5415. (org-set-local
  5416. 'org-latex-and-related-regexp
  5417. (let* ((re-sub
  5418. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5419. ((eq org-use-sub-superscripts '{})
  5420. (list org-match-substring-with-braces-regexp))
  5421. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5422. (re-latex
  5423. (when (memq 'latex org-highlight-latex-and-related)
  5424. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5425. (delq nil
  5426. (mapcar (lambda (x)
  5427. (and (member (car x) matchers) (nth 1 x)))
  5428. org-latex-regexps)))))
  5429. (re-entities
  5430. (when (memq 'entities org-highlight-latex-and-related)
  5431. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5432. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5433. (defun org-do-latex-and-related (limit)
  5434. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5435. LIMIT bounds the search for syntax to highlight. Stop at first
  5436. highlighted object, if any. Return t if some highlighting was
  5437. done, nil otherwise."
  5438. (when (org-string-nw-p org-latex-and-related-regexp)
  5439. (catch 'found
  5440. (while (re-search-forward org-latex-and-related-regexp limit t)
  5441. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5442. 'face))
  5443. '(org-code org-verbatim underline))
  5444. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5445. '(?_ ?^))
  5446. 1
  5447. 0)))
  5448. (font-lock-prepend-text-property
  5449. (+ offset (match-beginning 0)) (match-end 0)
  5450. 'face 'org-latex-and-related)
  5451. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5452. '(font-lock-multiline t)))
  5453. (throw 'found t)))
  5454. nil)))
  5455. (defun org-restart-font-lock ()
  5456. "Restart `font-lock-mode', to force refontification."
  5457. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5458. (font-lock-mode -1)
  5459. (font-lock-mode 1)))
  5460. (defun org-all-targets (&optional radio)
  5461. "Return a list of all targets in this file.
  5462. When optional argument RADIO is non-nil, only find radio
  5463. targets."
  5464. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5465. (save-excursion
  5466. (goto-char (point-min))
  5467. (while (re-search-forward re nil t)
  5468. ;; Make sure point is really within the object.
  5469. (backward-char)
  5470. (let ((obj (org-element-context)))
  5471. (when (memq (org-element-type obj) '(radio-target target))
  5472. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5473. rtn)))
  5474. (defun org-make-target-link-regexp (targets)
  5475. "Make regular expression matching all strings in TARGETS.
  5476. The regular expression finds the targets also if there is a line break
  5477. between words."
  5478. (and targets
  5479. (concat
  5480. "\\<\\("
  5481. (mapconcat
  5482. (lambda (x)
  5483. (setq x (regexp-quote x))
  5484. (while (string-match " +" x)
  5485. (setq x (replace-match "\\s-+" t t x)))
  5486. x)
  5487. targets
  5488. "\\|")
  5489. "\\)\\>")))
  5490. (defun org-activate-tags (limit)
  5491. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5492. (progn
  5493. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5494. (add-text-properties (match-beginning 1) (match-end 1)
  5495. (list 'mouse-face 'highlight
  5496. 'keymap org-mouse-map))
  5497. (org-rear-nonsticky-at (match-end 1))
  5498. t)))
  5499. (defun org-outline-level ()
  5500. "Compute the outline level of the heading at point.
  5501. If this is called at a normal headline, the level is the number of stars.
  5502. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5503. (save-excursion
  5504. (if (not (condition-case nil
  5505. (org-back-to-heading t)
  5506. (error nil)))
  5507. 0
  5508. (looking-at org-outline-regexp)
  5509. (1- (- (match-end 0) (match-beginning 0))))))
  5510. (defvar org-font-lock-keywords nil)
  5511. (defsubst org-re-property (property &optional literal)
  5512. "Return a regexp matching a PROPERTY line.
  5513. Match group 3 will be set to the value if it exists."
  5514. (concat "^\\(?4:[ \t]*\\)\\(?1::\\(?2:"
  5515. (if literal property (regexp-quote property))
  5516. "\\):\\)[ \t]+\\(?3:[^ \t\r\n].*?\\)\\(?5:[ \t]*\\)$"))
  5517. (defconst org-property-re
  5518. (org-re-property ".*?" 'literal)
  5519. "Regular expression matching a property line.
  5520. There are four matching groups:
  5521. 1: :PROPKEY: including the leading and trailing colon,
  5522. 2: PROPKEY without the leading and trailing colon,
  5523. 3: PROPVAL without leading or trailing spaces,
  5524. 4: the indentation of the current line,
  5525. 5: trailing whitespace.")
  5526. (defvar org-font-lock-hook nil
  5527. "Functions to be called for special font lock stuff.")
  5528. (defvar org-font-lock-set-keywords-hook nil
  5529. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5530. This is called after `org-font-lock-extra-keywords' is defined, but before
  5531. it is installed to be used by font lock. This can be useful if something
  5532. needs to be inserted at a specific position in the font-lock sequence.")
  5533. (defun org-font-lock-hook (limit)
  5534. "Run `org-font-lock-hook' within LIMIT."
  5535. (run-hook-with-args 'org-font-lock-hook limit))
  5536. (defun org-set-font-lock-defaults ()
  5537. "Set font lock defaults for the current buffer."
  5538. (let* ((em org-fontify-emphasized-text)
  5539. (lk org-activate-links)
  5540. (org-font-lock-extra-keywords
  5541. (list
  5542. ;; Call the hook
  5543. '(org-font-lock-hook)
  5544. ;; Headlines
  5545. `(,(if org-fontify-whole-heading-line
  5546. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5547. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5548. (1 (org-get-level-face 1))
  5549. (2 (org-get-level-face 2))
  5550. (3 (org-get-level-face 3)))
  5551. ;; Table lines
  5552. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5553. (1 'org-table t))
  5554. ;; Table internals
  5555. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5556. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5557. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5558. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5559. ;; Drawers
  5560. '(org-fontify-drawers)
  5561. ;; Properties
  5562. (list org-property-re
  5563. '(1 'org-special-keyword t)
  5564. '(3 'org-property-value t))
  5565. ;; Links
  5566. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5567. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5568. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5569. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5570. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5571. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5572. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5573. ;; Targets.
  5574. (list org-any-target-regexp '(0 'org-target t))
  5575. ;; Diary sexps.
  5576. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5577. ;; Macro
  5578. '(org-fontify-macros)
  5579. '(org-hide-wide-columns (0 nil append))
  5580. ;; TODO keyword
  5581. (list (format org-heading-keyword-regexp-format
  5582. org-todo-regexp)
  5583. '(2 (org-get-todo-face 2) t))
  5584. ;; DONE
  5585. (if org-fontify-done-headline
  5586. (list (format org-heading-keyword-regexp-format
  5587. (concat
  5588. "\\(?:"
  5589. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5590. "\\)"))
  5591. '(2 'org-headline-done t))
  5592. nil)
  5593. ;; Priorities
  5594. '(org-font-lock-add-priority-faces)
  5595. ;; Tags
  5596. '(org-font-lock-add-tag-faces)
  5597. ;; Tags groups
  5598. (if (and org-group-tags org-tag-groups-alist)
  5599. (list (concat org-outline-regexp-bol ".+\\(:"
  5600. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5601. ":\\).*$")
  5602. '(1 'org-tag-group prepend)))
  5603. ;; Special keywords
  5604. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5605. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5606. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5607. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5608. ;; Emphasis
  5609. (if em
  5610. (if (featurep 'xemacs)
  5611. '(org-do-emphasis-faces (0 nil append))
  5612. '(org-do-emphasis-faces)))
  5613. ;; Checkboxes
  5614. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5615. 1 'org-checkbox prepend)
  5616. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5617. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5618. (0 (org-get-checkbox-statistics-face) t)))
  5619. ;; Description list items
  5620. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5621. 1 'org-list-dt prepend)
  5622. ;; ARCHIVEd headings
  5623. (list (concat
  5624. org-outline-regexp-bol
  5625. "\\(.*:" org-archive-tag ":.*\\)")
  5626. '(1 'org-archived prepend))
  5627. ;; Specials
  5628. '(org-do-latex-and-related)
  5629. '(org-fontify-entities)
  5630. '(org-raise-scripts)
  5631. ;; Code
  5632. '(org-activate-code (1 'org-code t))
  5633. ;; COMMENT
  5634. (list (format org-heading-keyword-regexp-format
  5635. (concat "\\(" org-comment-string "\\)"))
  5636. '(2 'org-special-keyword t))
  5637. ;; Blocks and meta lines
  5638. '(org-fontify-meta-lines-and-blocks))))
  5639. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5640. (run-hooks 'org-font-lock-set-keywords-hook)
  5641. ;; Now set the full font-lock-keywords
  5642. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5643. (org-set-local 'font-lock-defaults
  5644. '(org-font-lock-keywords t nil nil backward-paragraph))
  5645. (kill-local-variable 'font-lock-keywords) nil))
  5646. (defun org-toggle-pretty-entities ()
  5647. "Toggle the composition display of entities as UTF8 characters."
  5648. (interactive)
  5649. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5650. (org-restart-font-lock)
  5651. (if org-pretty-entities
  5652. (message "Entities are now displayed as UTF8 characters")
  5653. (save-restriction
  5654. (widen)
  5655. (org-decompose-region (point-min) (point-max))
  5656. (message "Entities are now displayed as plain text"))))
  5657. (defvar org-custom-properties-overlays nil
  5658. "List of overlays used for custom properties.")
  5659. (make-variable-buffer-local 'org-custom-properties-overlays)
  5660. (defun org-toggle-custom-properties-visibility ()
  5661. "Display or hide properties in `org-custom-properties'."
  5662. (interactive)
  5663. (if org-custom-properties-overlays
  5664. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5665. (setq org-custom-properties-overlays nil))
  5666. (unless (not org-custom-properties)
  5667. (save-excursion
  5668. (save-restriction
  5669. (widen)
  5670. (goto-char (point-min))
  5671. (while (re-search-forward org-property-re nil t)
  5672. (mapc (lambda(p)
  5673. (when (equal p (substring (match-string 1) 1 -1))
  5674. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5675. (overlay-put o 'invisible t)
  5676. (overlay-put o 'org-custom-property t)
  5677. (push o org-custom-properties-overlays))))
  5678. org-custom-properties)))))))
  5679. (defun org-fontify-entities (limit)
  5680. "Find an entity to fontify."
  5681. (let (ee)
  5682. (when org-pretty-entities
  5683. (catch 'match
  5684. (while (re-search-forward
  5685. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5686. limit t)
  5687. (if (and (not (org-in-indented-comment-line))
  5688. (setq ee (org-entity-get (match-string 1)))
  5689. (= (length (nth 6 ee)) 1))
  5690. (let*
  5691. ((end (if (equal (match-string 2) "{}")
  5692. (match-end 2)
  5693. (match-end 1))))
  5694. (add-text-properties
  5695. (match-beginning 0) end
  5696. (list 'font-lock-fontified t))
  5697. (compose-region (match-beginning 0) end
  5698. (nth 6 ee) nil)
  5699. (backward-char 1)
  5700. (throw 'match t))))
  5701. nil))))
  5702. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5703. "Fontify string S like in Org-mode."
  5704. (with-temp-buffer
  5705. (insert s)
  5706. (let ((org-odd-levels-only odd-levels))
  5707. (org-mode)
  5708. (font-lock-fontify-buffer)
  5709. (buffer-string))))
  5710. (defvar org-m nil)
  5711. (defvar org-l nil)
  5712. (defvar org-f nil)
  5713. (defun org-get-level-face (n)
  5714. "Get the right face for match N in font-lock matching of headlines."
  5715. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5716. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5717. (if org-cycle-level-faces
  5718. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5719. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5720. (cond
  5721. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5722. ((eq n 2) org-f)
  5723. (t (if org-level-color-stars-only nil org-f))))
  5724. (defun org-get-todo-face (kwd)
  5725. "Get the right face for a TODO keyword KWD.
  5726. If KWD is a number, get the corresponding match group."
  5727. (if (numberp kwd) (setq kwd (match-string kwd)))
  5728. (or (org-face-from-face-or-color
  5729. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5730. (and (member kwd org-done-keywords) 'org-done)
  5731. 'org-todo))
  5732. (defun org-face-from-face-or-color (context inherit face-or-color)
  5733. "Create a face list that inherits INHERIT, but sets the foreground color.
  5734. When FACE-OR-COLOR is not a string, just return it."
  5735. (if (stringp face-or-color)
  5736. (list :inherit inherit
  5737. (cdr (assoc context org-faces-easy-properties))
  5738. face-or-color)
  5739. face-or-color))
  5740. (defun org-font-lock-add-tag-faces (limit)
  5741. "Add the special tag faces."
  5742. (when (and org-tag-faces org-tags-special-faces-re)
  5743. (while (re-search-forward org-tags-special-faces-re limit t)
  5744. (add-text-properties (match-beginning 1) (match-end 1)
  5745. (list 'face (org-get-tag-face 1)
  5746. 'font-lock-fontified t))
  5747. (backward-char 1))))
  5748. (defun org-font-lock-add-priority-faces (limit)
  5749. "Add the special priority faces."
  5750. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5751. (when (save-match-data (org-at-heading-p))
  5752. (add-text-properties
  5753. (match-beginning 0) (match-end 0)
  5754. (list 'face (or (org-face-from-face-or-color
  5755. 'priority 'org-priority
  5756. (cdr (assoc (char-after (match-beginning 1))
  5757. org-priority-faces)))
  5758. 'org-priority)
  5759. 'font-lock-fontified t)))))
  5760. (defun org-get-tag-face (kwd)
  5761. "Get the right face for a TODO keyword KWD.
  5762. If KWD is a number, get the corresponding match group."
  5763. (if (numberp kwd) (setq kwd (match-string kwd)))
  5764. (or (org-face-from-face-or-color
  5765. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5766. 'org-tag))
  5767. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5768. "Remove fontification and activation overlays from links."
  5769. (font-lock-default-unfontify-region beg end)
  5770. (let* ((buffer-undo-list t)
  5771. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5772. (inhibit-modification-hooks t)
  5773. deactivate-mark buffer-file-name buffer-file-truename)
  5774. (org-decompose-region beg end)
  5775. (remove-text-properties beg end
  5776. '(mouse-face t keymap t org-linked-text t
  5777. invisible t intangible t
  5778. org-emphasis t))
  5779. (org-remove-font-lock-display-properties beg end)))
  5780. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5781. ((raise 0.3) (height 0.7))
  5782. ((raise -0.5))
  5783. ((raise 0.5)))
  5784. "Display properties for showing superscripts and subscripts.")
  5785. (defun org-remove-font-lock-display-properties (beg end)
  5786. "Remove specific display properties that have been added by font lock.
  5787. The will remove the raise properties that are used to show superscripts
  5788. and subscripts."
  5789. (let (next prop)
  5790. (while (< beg end)
  5791. (setq next (next-single-property-change beg 'display nil end)
  5792. prop (get-text-property beg 'display))
  5793. (if (member prop org-script-display)
  5794. (put-text-property beg next 'display nil))
  5795. (setq beg next))))
  5796. (defun org-raise-scripts (limit)
  5797. "Add raise properties to sub/superscripts."
  5798. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5799. (if (re-search-forward
  5800. (if (eq org-use-sub-superscripts t)
  5801. org-match-substring-regexp
  5802. org-match-substring-with-braces-regexp)
  5803. limit t)
  5804. (let* ((pos (point)) table-p comment-p
  5805. (mpos (match-beginning 3))
  5806. (emph-p (get-text-property mpos 'org-emphasis))
  5807. (link-p (get-text-property mpos 'mouse-face))
  5808. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5809. (goto-char (point-at-bol))
  5810. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5811. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5812. (goto-char pos)
  5813. ;; Handle a_b^c
  5814. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5815. (if (or comment-p emph-p link-p keyw-p)
  5816. t
  5817. (put-text-property (match-beginning 3) (match-end 0)
  5818. 'display
  5819. (if (equal (char-after (match-beginning 2)) ?^)
  5820. (nth (if table-p 3 1) org-script-display)
  5821. (nth (if table-p 2 0) org-script-display)))
  5822. (add-text-properties (match-beginning 2) (match-end 2)
  5823. (list 'invisible t
  5824. 'org-dwidth t 'org-dwidth-n 1))
  5825. (if (and (eq (char-after (match-beginning 3)) ?{)
  5826. (eq (char-before (match-end 3)) ?}))
  5827. (progn
  5828. (add-text-properties
  5829. (match-beginning 3) (1+ (match-beginning 3))
  5830. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5831. (add-text-properties
  5832. (1- (match-end 3)) (match-end 3)
  5833. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5834. t)))))
  5835. ;;;; Visibility cycling, including org-goto and indirect buffer
  5836. ;;; Cycling
  5837. (defvar org-cycle-global-status nil)
  5838. (make-variable-buffer-local 'org-cycle-global-status)
  5839. (put 'org-cycle-global-status 'org-state t)
  5840. (defvar org-cycle-subtree-status nil)
  5841. (make-variable-buffer-local 'org-cycle-subtree-status)
  5842. (put 'org-cycle-subtree-status 'org-state t)
  5843. (defvar org-inlinetask-min-level)
  5844. (defun org-unlogged-message (&rest args)
  5845. "Display a message, but avoid logging it in the *Messages* buffer."
  5846. (let ((message-log-max nil))
  5847. (apply 'message args)))
  5848. ;;;###autoload
  5849. (defun org-cycle (&optional arg)
  5850. "TAB-action and visibility cycling for Org-mode.
  5851. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5852. is outline visibility cycling, but it also invokes other actions
  5853. in special contexts.
  5854. - When this function is called with a prefix argument, rotate the entire
  5855. buffer through 3 states (global cycling)
  5856. 1. OVERVIEW: Show only top-level headlines.
  5857. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5858. 3. SHOW ALL: Show everything.
  5859. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5860. determined by the variable `org-startup-folded', and by any VISIBILITY
  5861. properties in the buffer.
  5862. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5863. including any drawers.
  5864. - When inside a table, re-align the table and move to the next field.
  5865. - When point is at the beginning of a headline, rotate the subtree started
  5866. by this line through 3 different states (local cycling)
  5867. 1. FOLDED: Only the main headline is shown.
  5868. 2. CHILDREN: The main headline and the direct children are shown.
  5869. From this state, you can move to one of the children
  5870. and zoom in further.
  5871. 3. SUBTREE: Show the entire subtree, including body text.
  5872. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5873. - When point is at the beginning of an empty headline and the variable
  5874. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5875. of the headline by demoting and promoting it to likely levels. This
  5876. speeds up creation document structure by pressing TAB once or several
  5877. times right after creating a new headline.
  5878. - When there is a numeric prefix, go up to a heading with level ARG, do
  5879. a `show-subtree' and return to the previous cursor position. If ARG
  5880. is negative, go up that many levels.
  5881. - When point is not at the beginning of a headline, execute the global
  5882. binding for TAB, which is re-indenting the line. See the option
  5883. `org-cycle-emulate-tab' for details.
  5884. - Special case: if point is at the beginning of the buffer and there is
  5885. no headline in line 1, this function will act as if called with prefix arg
  5886. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5887. But only if also the variable `org-cycle-global-at-bob' is t."
  5888. (interactive "P")
  5889. (org-load-modules-maybe)
  5890. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5891. (and org-cycle-level-after-item/entry-creation
  5892. (or (org-cycle-level)
  5893. (org-cycle-item-indentation))))
  5894. (let* ((limit-level
  5895. (or org-cycle-max-level
  5896. (and (boundp 'org-inlinetask-min-level)
  5897. org-inlinetask-min-level
  5898. (1- org-inlinetask-min-level))))
  5899. (nstars (and limit-level
  5900. (if org-odd-levels-only
  5901. (and limit-level (1- (* limit-level 2)))
  5902. limit-level)))
  5903. (org-outline-regexp
  5904. (if (not (derived-mode-p 'org-mode))
  5905. outline-regexp
  5906. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5907. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5908. (not (looking-at org-outline-regexp))))
  5909. (org-cycle-hook
  5910. (if bob-special
  5911. (delq 'org-optimize-window-after-visibility-change
  5912. (copy-sequence org-cycle-hook))
  5913. org-cycle-hook))
  5914. (pos (point)))
  5915. (if (or bob-special (equal arg '(4)))
  5916. ;; special case: use global cycling
  5917. (setq arg t))
  5918. (cond
  5919. ((equal arg '(16))
  5920. (setq last-command 'dummy)
  5921. (org-set-startup-visibility)
  5922. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5923. ((equal arg '(64))
  5924. (show-all)
  5925. (org-unlogged-message "Entire buffer visible, including drawers"))
  5926. ;; Table: enter it or move to the next field.
  5927. ((org-at-table-p 'any)
  5928. (if (org-at-table.el-p)
  5929. (message "Use C-c ' to edit table.el tables")
  5930. (if arg (org-table-edit-field t)
  5931. (org-table-justify-field-maybe)
  5932. (call-interactively 'org-table-next-field))))
  5933. ((run-hook-with-args-until-success
  5934. 'org-tab-after-check-for-table-hook))
  5935. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5936. ((eq arg t) (org-cycle-internal-global))
  5937. ;; Drawers: delegate to `org-flag-drawer'.
  5938. ((save-excursion
  5939. (beginning-of-line 1)
  5940. (looking-at org-drawer-regexp))
  5941. (org-flag-drawer ; toggle block visibility
  5942. (not (get-char-property (match-end 0) 'invisible))))
  5943. ;; Show-subtree, ARG levels up from here.
  5944. ((integerp arg)
  5945. (save-excursion
  5946. (org-back-to-heading)
  5947. (outline-up-heading (if (< arg 0) (- arg)
  5948. (- (funcall outline-level) arg)))
  5949. (org-show-subtree)))
  5950. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5951. ((and (featurep 'org-inlinetask)
  5952. (org-inlinetask-at-task-p)
  5953. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5954. (org-inlinetask-toggle-visibility))
  5955. ((org-try-cdlatex-tab))
  5956. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5957. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5958. (save-excursion (beginning-of-line 1)
  5959. (looking-at org-outline-regexp)))
  5960. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5961. (org-cycle-internal-local))
  5962. ;; From there: TAB emulation and template completion.
  5963. (buffer-read-only (org-back-to-heading))
  5964. ((run-hook-with-args-until-success
  5965. 'org-tab-after-check-for-cycling-hook))
  5966. ((org-try-structure-completion))
  5967. ((run-hook-with-args-until-success
  5968. 'org-tab-before-tab-emulation-hook))
  5969. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5970. (or (not (bolp))
  5971. (not (looking-at org-outline-regexp))))
  5972. (call-interactively (global-key-binding "\t")))
  5973. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5974. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5975. (or (and (eq org-cycle-emulate-tab 'white)
  5976. (= (match-end 0) (point-at-eol)))
  5977. (and (eq org-cycle-emulate-tab 'whitestart)
  5978. (>= (match-end 0) pos))))
  5979. t
  5980. (eq org-cycle-emulate-tab t))
  5981. (call-interactively (global-key-binding "\t")))
  5982. (t (save-excursion
  5983. (org-back-to-heading)
  5984. (org-cycle)))))))
  5985. (defun org-cycle-internal-global ()
  5986. "Do the global cycling action."
  5987. ;; Hack to avoid display of messages for .org attachments in Gnus
  5988. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5989. (cond
  5990. ((and (eq last-command this-command)
  5991. (eq org-cycle-global-status 'overview))
  5992. ;; We just created the overview - now do table of contents
  5993. ;; This can be slow in very large buffers, so indicate action
  5994. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5995. (unless ga (org-unlogged-message "CONTENTS..."))
  5996. (org-content)
  5997. (unless ga (org-unlogged-message "CONTENTS...done"))
  5998. (setq org-cycle-global-status 'contents)
  5999. (run-hook-with-args 'org-cycle-hook 'contents))
  6000. ((and (eq last-command this-command)
  6001. (eq org-cycle-global-status 'contents))
  6002. ;; We just showed the table of contents - now show everything
  6003. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6004. (show-all)
  6005. (unless ga (org-unlogged-message "SHOW ALL"))
  6006. (setq org-cycle-global-status 'all)
  6007. (run-hook-with-args 'org-cycle-hook 'all))
  6008. (t
  6009. ;; Default action: go to overview
  6010. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6011. (org-overview)
  6012. (unless ga (org-unlogged-message "OVERVIEW"))
  6013. (setq org-cycle-global-status 'overview)
  6014. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6015. (defvar org-called-with-limited-levels);Dyn-bound in ̀org-with-limited-levels'.
  6016. (defun org-cycle-internal-local ()
  6017. "Do the local cycling action."
  6018. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6019. ;; First, determine end of headline (EOH), end of subtree or item
  6020. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6021. (save-excursion
  6022. (if (org-at-item-p)
  6023. (progn
  6024. (beginning-of-line)
  6025. (setq struct (org-list-struct))
  6026. (setq eoh (point-at-eol))
  6027. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6028. (setq has-children (org-list-has-child-p (point) struct)))
  6029. (org-back-to-heading)
  6030. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6031. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  6032. (setq has-children
  6033. (or (save-excursion
  6034. (let ((level (funcall outline-level)))
  6035. (outline-next-heading)
  6036. (and (org-at-heading-p t)
  6037. (> (funcall outline-level) level))))
  6038. (save-excursion
  6039. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6040. ;; Determine end invisible part of buffer (EOL)
  6041. (beginning-of-line 2)
  6042. ;; XEmacs doesn't have `next-single-char-property-change'
  6043. (if (featurep 'xemacs)
  6044. (while (and (not (eobp)) ;; this is like `next-line'
  6045. (get-char-property (1- (point)) 'invisible))
  6046. (beginning-of-line 2))
  6047. (while (and (not (eobp)) ;; this is like `next-line'
  6048. (get-char-property (1- (point)) 'invisible))
  6049. (goto-char (next-single-char-property-change (point) 'invisible))
  6050. (and (eolp) (beginning-of-line 2))))
  6051. (setq eol (point)))
  6052. ;; Find out what to do next and set `this-command'
  6053. (cond
  6054. ((= eos eoh)
  6055. ;; Nothing is hidden behind this heading
  6056. (unless (org-before-first-heading-p)
  6057. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6058. (org-unlogged-message "EMPTY ENTRY")
  6059. (setq org-cycle-subtree-status nil)
  6060. (save-excursion
  6061. (goto-char eos)
  6062. (outline-next-heading)
  6063. (if (outline-invisible-p) (org-flag-heading nil))))
  6064. ((and (or (>= eol eos)
  6065. (not (string-match "\\S-" (buffer-substring eol eos))))
  6066. (or has-children
  6067. (not (setq children-skipped
  6068. org-cycle-skip-children-state-if-no-children))))
  6069. ;; Entire subtree is hidden in one line: children view
  6070. (unless (org-before-first-heading-p)
  6071. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6072. (if (org-at-item-p)
  6073. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6074. (org-show-entry)
  6075. (org-with-limited-levels (show-children))
  6076. ;; FIXME: This slows down the func way too much.
  6077. ;; How keep drawers hidden in subtree anyway?
  6078. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6079. ;; (org-cycle-hide-drawers 'subtree))
  6080. ;; Fold every list in subtree to top-level items.
  6081. (when (eq org-cycle-include-plain-lists 'integrate)
  6082. (save-excursion
  6083. (org-back-to-heading)
  6084. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6085. (beginning-of-line 1)
  6086. (let* ((struct (org-list-struct))
  6087. (prevs (org-list-prevs-alist struct))
  6088. (end (org-list-get-bottom-point struct)))
  6089. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6090. (org-list-get-all-items (point) struct prevs))
  6091. (goto-char (if (< end eos) end eos)))))))
  6092. (org-unlogged-message "CHILDREN")
  6093. (save-excursion
  6094. (goto-char eos)
  6095. (outline-next-heading)
  6096. (if (outline-invisible-p) (org-flag-heading nil)))
  6097. (setq org-cycle-subtree-status 'children)
  6098. (unless (org-before-first-heading-p)
  6099. (run-hook-with-args 'org-cycle-hook 'children)))
  6100. ((or children-skipped
  6101. (and (eq last-command this-command)
  6102. (eq org-cycle-subtree-status 'children)))
  6103. ;; We just showed the children, or no children are there,
  6104. ;; now show everything.
  6105. (unless (org-before-first-heading-p)
  6106. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6107. (outline-flag-region eoh eos nil)
  6108. (org-unlogged-message
  6109. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6110. (setq org-cycle-subtree-status 'subtree)
  6111. (unless (org-before-first-heading-p)
  6112. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6113. (t
  6114. ;; Default action: hide the subtree.
  6115. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6116. (outline-flag-region eoh eos t)
  6117. (org-unlogged-message "FOLDED")
  6118. (setq org-cycle-subtree-status 'folded)
  6119. (unless (org-before-first-heading-p)
  6120. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6121. ;;;###autoload
  6122. (defun org-global-cycle (&optional arg)
  6123. "Cycle the global visibility. For details see `org-cycle'.
  6124. With \\[universal-argument] prefix arg, switch to startup visibility.
  6125. With a numeric prefix, show all headlines up to that level."
  6126. (interactive "P")
  6127. (let ((org-cycle-include-plain-lists
  6128. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6129. (cond
  6130. ((integerp arg)
  6131. (show-all)
  6132. (hide-sublevels arg)
  6133. (setq org-cycle-global-status 'contents))
  6134. ((equal arg '(4))
  6135. (org-set-startup-visibility)
  6136. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6137. (t
  6138. (org-cycle '(4))))))
  6139. (defun org-set-startup-visibility ()
  6140. "Set the visibility required by startup options and properties."
  6141. (cond
  6142. ((eq org-startup-folded t)
  6143. (org-overview))
  6144. ((eq org-startup-folded 'content)
  6145. (org-content))
  6146. ((or (eq org-startup-folded 'showeverything)
  6147. (eq org-startup-folded nil))
  6148. (show-all)))
  6149. (unless (eq org-startup-folded 'showeverything)
  6150. (if org-hide-block-startup (org-hide-block-all))
  6151. (org-set-visibility-according-to-property 'no-cleanup)
  6152. (org-cycle-hide-archived-subtrees 'all)
  6153. (org-cycle-hide-drawers 'all)
  6154. (org-cycle-show-empty-lines t)))
  6155. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6156. "Switch subtree visibilities according to :VISIBILITY: property."
  6157. (interactive)
  6158. (let (org-show-entry-below state)
  6159. (save-excursion
  6160. (goto-char (point-min))
  6161. (while (re-search-forward
  6162. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6163. nil t)
  6164. (setq state (match-string 1))
  6165. (save-excursion
  6166. (org-back-to-heading t)
  6167. (hide-subtree)
  6168. (org-reveal)
  6169. (cond
  6170. ((equal state '("fold" "folded"))
  6171. (hide-subtree))
  6172. ((equal state "children")
  6173. (org-show-hidden-entry)
  6174. (show-children))
  6175. ((equal state "content")
  6176. (save-excursion
  6177. (save-restriction
  6178. (org-narrow-to-subtree)
  6179. (org-content))))
  6180. ((member state '("all" "showall"))
  6181. (show-subtree)))))
  6182. (unless no-cleanup
  6183. (org-cycle-hide-archived-subtrees 'all)
  6184. (org-cycle-hide-drawers 'all)
  6185. (org-cycle-show-empty-lines 'all)))))
  6186. ;; This function uses outline-regexp instead of the more fundamental
  6187. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6188. ;; buffers, where outline-regexp is needed.
  6189. (defun org-overview ()
  6190. "Switch to overview mode, showing only top-level headlines.
  6191. Really, this shows all headlines with level equal or greater than the level
  6192. of the first headline in the buffer. This is important, because if the
  6193. first headline is not level one, then (hide-sublevels 1) gives confusing
  6194. results."
  6195. (interactive)
  6196. (let ((pos (point))
  6197. (level (save-excursion
  6198. (goto-char (point-min))
  6199. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6200. (progn
  6201. (goto-char (match-beginning 0))
  6202. (funcall outline-level))))))
  6203. (and level (hide-sublevels level))
  6204. (recenter '(4))
  6205. (goto-char pos)))
  6206. (defun org-content (&optional arg)
  6207. "Show all headlines in the buffer, like a table of contents.
  6208. With numerical argument N, show content up to level N."
  6209. (interactive "P")
  6210. (org-overview)
  6211. (save-excursion
  6212. ;; Visit all headings and show their offspring
  6213. (and (integerp arg) (org-overview))
  6214. (goto-char (point-max))
  6215. (catch 'exit
  6216. (while (and (progn (condition-case nil
  6217. (outline-previous-visible-heading 1)
  6218. (error (goto-char (point-min))))
  6219. t)
  6220. (looking-at org-outline-regexp))
  6221. (if (integerp arg)
  6222. (show-children (1- arg))
  6223. (show-branches))
  6224. (if (bobp) (throw 'exit nil))))))
  6225. (defun org-optimize-window-after-visibility-change (state)
  6226. "Adjust the window after a change in outline visibility.
  6227. This function is the default value of the hook `org-cycle-hook'."
  6228. (when (get-buffer-window (current-buffer))
  6229. (cond
  6230. ((eq state 'content) nil)
  6231. ((eq state 'all) nil)
  6232. ((eq state 'folded) nil)
  6233. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6234. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6235. (defun org-remove-empty-overlays-at (pos)
  6236. "Remove outline overlays that do not contain non-white stuff."
  6237. (mapc
  6238. (lambda (o)
  6239. (and (eq 'outline (overlay-get o 'invisible))
  6240. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6241. (overlay-end o))))
  6242. (delete-overlay o)))
  6243. (overlays-at pos)))
  6244. (defun org-clean-visibility-after-subtree-move ()
  6245. "Fix visibility issues after moving a subtree."
  6246. ;; First, find a reasonable region to look at:
  6247. ;; Start two siblings above, end three below
  6248. (let* ((beg (save-excursion
  6249. (and (org-get-last-sibling)
  6250. (org-get-last-sibling))
  6251. (point)))
  6252. (end (save-excursion
  6253. (and (org-get-next-sibling)
  6254. (org-get-next-sibling)
  6255. (org-get-next-sibling))
  6256. (if (org-at-heading-p)
  6257. (point-at-eol)
  6258. (point))))
  6259. (level (looking-at "\\*+"))
  6260. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6261. (save-excursion
  6262. (save-restriction
  6263. (narrow-to-region beg end)
  6264. (when re
  6265. ;; Properly fold already folded siblings
  6266. (goto-char (point-min))
  6267. (while (re-search-forward re nil t)
  6268. (if (and (not (outline-invisible-p))
  6269. (save-excursion
  6270. (goto-char (point-at-eol)) (outline-invisible-p)))
  6271. (hide-entry))))
  6272. (org-cycle-show-empty-lines 'overview)
  6273. (org-cycle-hide-drawers 'overview)))))
  6274. (defun org-cycle-show-empty-lines (state)
  6275. "Show empty lines above all visible headlines.
  6276. The region to be covered depends on STATE when called through
  6277. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6278. entire buffer covered. Note that an empty line is only shown if there
  6279. are at least `org-cycle-separator-lines' empty lines before the headline."
  6280. (when (not (= org-cycle-separator-lines 0))
  6281. (save-excursion
  6282. (let* ((n (abs org-cycle-separator-lines))
  6283. (re (cond
  6284. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6285. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6286. (t (let ((ns (number-to-string (- n 2))))
  6287. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6288. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6289. beg end b e)
  6290. (cond
  6291. ((memq state '(overview contents t))
  6292. (setq beg (point-min) end (point-max)))
  6293. ((memq state '(children folded))
  6294. (setq beg (point) end (progn (org-end-of-subtree t t)
  6295. (beginning-of-line 2)
  6296. (point)))))
  6297. (when beg
  6298. (goto-char beg)
  6299. (while (re-search-forward re end t)
  6300. (unless (get-char-property (match-end 1) 'invisible)
  6301. (setq e (match-end 1))
  6302. (if (< org-cycle-separator-lines 0)
  6303. (setq b (save-excursion
  6304. (goto-char (match-beginning 0))
  6305. (org-back-over-empty-lines)
  6306. (if (save-excursion
  6307. (goto-char (max (point-min) (1- (point))))
  6308. (org-at-heading-p))
  6309. (1- (point))
  6310. (point))))
  6311. (setq b (match-beginning 1)))
  6312. (outline-flag-region b e nil)))))))
  6313. ;; Never hide empty lines at the end of the file.
  6314. (save-excursion
  6315. (goto-char (point-max))
  6316. (outline-previous-heading)
  6317. (outline-end-of-heading)
  6318. (if (and (looking-at "[ \t\n]+")
  6319. (= (match-end 0) (point-max)))
  6320. (outline-flag-region (point) (match-end 0) nil))))
  6321. (defun org-show-empty-lines-in-parent ()
  6322. "Move to the parent and re-show empty lines before visible headlines."
  6323. (save-excursion
  6324. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6325. (org-cycle-show-empty-lines context))))
  6326. (defun org-files-list ()
  6327. "Return `org-agenda-files' list, plus all open org-mode files.
  6328. This is useful for operations that need to scan all of a user's
  6329. open and agenda-wise Org files."
  6330. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6331. (dolist (buf (buffer-list))
  6332. (with-current-buffer buf
  6333. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6334. (let ((file (expand-file-name (buffer-file-name))))
  6335. (unless (member file files)
  6336. (push file files))))))
  6337. files))
  6338. (defsubst org-entry-beginning-position ()
  6339. "Return the beginning position of the current entry."
  6340. (save-excursion (outline-back-to-heading t) (point)))
  6341. (defsubst org-entry-end-position ()
  6342. "Return the end position of the current entry."
  6343. (save-excursion (outline-next-heading) (point)))
  6344. (defun org-cycle-hide-drawers (state &optional exceptions)
  6345. "Re-hide all drawers after a visibility state change.
  6346. When non-nil, optional argument EXCEPTIONS is a list of strings
  6347. specifying which drawers should not be hidden."
  6348. (when (and (derived-mode-p 'org-mode)
  6349. (not (memq state '(overview folded contents))))
  6350. (save-excursion
  6351. (let* ((globalp (memq state '(contents all)))
  6352. (beg (if globalp (point-min) (point)))
  6353. (end (if globalp (point-max)
  6354. (if (eq state 'children)
  6355. (save-excursion (outline-next-heading) (point))
  6356. (org-end-of-subtree t)))))
  6357. (goto-char beg)
  6358. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6359. (unless (member-ignore-case (match-string 1) exceptions)
  6360. (let ((drawer (org-element-at-point)))
  6361. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6362. (org-flag-drawer t drawer)
  6363. ;; Make sure to skip drawer entirely or we might flag
  6364. ;; it another time when matching its ending line with
  6365. ;; `org-drawer-regexp'.
  6366. (goto-char (org-element-property :end drawer))))))))))
  6367. (defun org-cycle-hide-inline-tasks (state)
  6368. "Re-hide inline tasks when switching to 'contents or 'children
  6369. visibility state."
  6370. (case state
  6371. (contents
  6372. (when (org-bound-and-true-p org-inlinetask-min-level)
  6373. (hide-sublevels (1- org-inlinetask-min-level))))
  6374. (children
  6375. (when (featurep 'org-inlinetask)
  6376. (save-excursion
  6377. (while (and (outline-next-heading)
  6378. (org-inlinetask-at-task-p))
  6379. (org-inlinetask-toggle-visibility)
  6380. (org-inlinetask-goto-end)))))))
  6381. (defun org-flag-drawer (flag &optional element)
  6382. "When FLAG is non-nil, hide the drawer we are at.
  6383. Otherwise make it visible. When optional argument ELEMENT is
  6384. a parsed drawer, as returned by `org-element-at-point', hide or
  6385. show that drawer instead."
  6386. (let ((drawer (or element (org-element-at-point))))
  6387. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6388. (save-excursion
  6389. (goto-char (org-element-property :post-affiliated drawer))
  6390. (outline-flag-region
  6391. (line-end-position)
  6392. (progn (goto-char (org-element-property :end drawer))
  6393. (skip-chars-backward " \r\t\n")
  6394. (line-end-position))
  6395. flag)))))
  6396. (defun org-subtree-end-visible-p ()
  6397. "Is the end of the current subtree visible?"
  6398. (pos-visible-in-window-p
  6399. (save-excursion (org-end-of-subtree t) (point))))
  6400. (defun org-first-headline-recenter (&optional N)
  6401. "Move cursor to the first headline and recenter the headline.
  6402. Optional argument N means put the headline into the Nth line of the window."
  6403. (goto-char (point-min))
  6404. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6405. (beginning-of-line)
  6406. (recenter (prefix-numeric-value N))))
  6407. ;;; Saving and restoring visibility
  6408. (defun org-outline-overlay-data (&optional use-markers)
  6409. "Return a list of the locations of all outline overlays.
  6410. These are overlays with the `invisible' property value `outline'.
  6411. The return value is a list of cons cells, with start and stop
  6412. positions for each overlay.
  6413. If USE-MARKERS is set, return the positions as markers."
  6414. (let (beg end)
  6415. (save-excursion
  6416. (save-restriction
  6417. (widen)
  6418. (delq nil
  6419. (mapcar (lambda (o)
  6420. (when (eq (overlay-get o 'invisible) 'outline)
  6421. (setq beg (overlay-start o)
  6422. end (overlay-end o))
  6423. (and beg end (> end beg)
  6424. (if use-markers
  6425. (cons (copy-marker beg)
  6426. (copy-marker end t))
  6427. (cons beg end)))))
  6428. (overlays-in (point-min) (point-max))))))))
  6429. (defun org-set-outline-overlay-data (data)
  6430. "Create visibility overlays for all positions in DATA.
  6431. DATA should have been made by `org-outline-overlay-data'."
  6432. (let (o)
  6433. (save-excursion
  6434. (save-restriction
  6435. (widen)
  6436. (show-all)
  6437. (mapc (lambda (c)
  6438. (outline-flag-region (car c) (cdr c) t))
  6439. data)))))
  6440. ;;; Folding of blocks
  6441. (defvar org-hide-block-overlays nil
  6442. "Overlays hiding blocks.")
  6443. (make-variable-buffer-local 'org-hide-block-overlays)
  6444. (defun org-block-map (function &optional start end)
  6445. "Call FUNCTION at the head of all source blocks in the current buffer.
  6446. Optional arguments START and END can be used to limit the range."
  6447. (let ((start (or start (point-min)))
  6448. (end (or end (point-max))))
  6449. (save-excursion
  6450. (goto-char start)
  6451. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6452. (save-excursion
  6453. (save-match-data
  6454. (goto-char (match-beginning 0))
  6455. (funcall function)))))))
  6456. (defun org-hide-block-toggle-all ()
  6457. "Toggle the visibility of all blocks in the current buffer."
  6458. (org-block-map #'org-hide-block-toggle))
  6459. (defun org-hide-block-all ()
  6460. "Fold all blocks in the current buffer."
  6461. (interactive)
  6462. (org-show-block-all)
  6463. (org-block-map #'org-hide-block-toggle-maybe))
  6464. (defun org-show-block-all ()
  6465. "Unfold all blocks in the current buffer."
  6466. (interactive)
  6467. (mapc 'delete-overlay org-hide-block-overlays)
  6468. (setq org-hide-block-overlays nil))
  6469. (defun org-hide-block-toggle-maybe ()
  6470. "Toggle visibility of block at point."
  6471. (interactive)
  6472. (let ((case-fold-search t))
  6473. (if (save-excursion
  6474. (beginning-of-line 1)
  6475. (looking-at org-block-regexp))
  6476. (progn (org-hide-block-toggle)
  6477. t) ;; to signal that we took action
  6478. nil))) ;; to signal that we did not
  6479. (defun org-hide-block-toggle (&optional force)
  6480. "Toggle the visibility of the current block."
  6481. (interactive)
  6482. (save-excursion
  6483. (beginning-of-line)
  6484. (if (re-search-forward org-block-regexp nil t)
  6485. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6486. (end (match-end 0)) ;; end of entire body
  6487. ov)
  6488. (if (memq t (mapcar (lambda (overlay)
  6489. (eq (overlay-get overlay 'invisible)
  6490. 'org-hide-block))
  6491. (overlays-at start)))
  6492. (if (or (not force) (eq force 'off))
  6493. (mapc (lambda (ov)
  6494. (when (member ov org-hide-block-overlays)
  6495. (setq org-hide-block-overlays
  6496. (delq ov org-hide-block-overlays)))
  6497. (when (eq (overlay-get ov 'invisible)
  6498. 'org-hide-block)
  6499. (delete-overlay ov)))
  6500. (overlays-at start)))
  6501. (setq ov (make-overlay start end))
  6502. (overlay-put ov 'invisible 'org-hide-block)
  6503. ;; make the block accessible to isearch
  6504. (overlay-put
  6505. ov 'isearch-open-invisible
  6506. (lambda (ov)
  6507. (when (member ov org-hide-block-overlays)
  6508. (setq org-hide-block-overlays
  6509. (delq ov org-hide-block-overlays)))
  6510. (when (eq (overlay-get ov 'invisible)
  6511. 'org-hide-block)
  6512. (delete-overlay ov))))
  6513. (push ov org-hide-block-overlays)))
  6514. (user-error "Not looking at a source block"))))
  6515. ;; org-tab-after-check-for-cycling-hook
  6516. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6517. ;; Remove overlays when changing major mode
  6518. (add-hook 'org-mode-hook
  6519. (lambda () (org-add-hook 'change-major-mode-hook
  6520. 'org-show-block-all 'append 'local)))
  6521. ;;; Org-goto
  6522. (defvar org-goto-window-configuration nil)
  6523. (defvar org-goto-marker nil)
  6524. (defvar org-goto-map)
  6525. (defun org-goto-map ()
  6526. "Set the keymap `org-goto'."
  6527. (setq org-goto-map
  6528. (let ((map (make-sparse-keymap)))
  6529. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6530. mouse-drag-region universal-argument org-occur))
  6531. cmd)
  6532. (while (setq cmd (pop cmds))
  6533. (substitute-key-definition cmd cmd map global-map)))
  6534. (suppress-keymap map)
  6535. (org-defkey map "\C-m" 'org-goto-ret)
  6536. (org-defkey map [(return)] 'org-goto-ret)
  6537. (org-defkey map [(left)] 'org-goto-left)
  6538. (org-defkey map [(right)] 'org-goto-right)
  6539. (org-defkey map [(control ?g)] 'org-goto-quit)
  6540. (org-defkey map "\C-i" 'org-cycle)
  6541. (org-defkey map [(tab)] 'org-cycle)
  6542. (org-defkey map [(down)] 'outline-next-visible-heading)
  6543. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6544. (if org-goto-auto-isearch
  6545. (if (fboundp 'define-key-after)
  6546. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6547. nil)
  6548. (org-defkey map "q" 'org-goto-quit)
  6549. (org-defkey map "n" 'outline-next-visible-heading)
  6550. (org-defkey map "p" 'outline-previous-visible-heading)
  6551. (org-defkey map "f" 'outline-forward-same-level)
  6552. (org-defkey map "b" 'outline-backward-same-level)
  6553. (org-defkey map "u" 'outline-up-heading))
  6554. (org-defkey map "/" 'org-occur)
  6555. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6556. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6557. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6558. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6559. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6560. map)))
  6561. (defconst org-goto-help
  6562. "Browse buffer copy, to find location or copy text.%s
  6563. RET=jump to location C-g=quit and return to previous location
  6564. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6565. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6566. (defun org-goto (&optional alternative-interface)
  6567. "Look up a different location in the current file, keeping current visibility.
  6568. When you want look-up or go to a different location in a
  6569. document, the fastest way is often to fold the entire buffer and
  6570. then dive into the tree. This method has the disadvantage, that
  6571. the previous location will be folded, which may not be what you
  6572. want.
  6573. This command works around this by showing a copy of the current
  6574. buffer in an indirect buffer, in overview mode. You can dive
  6575. into the tree in that copy, use org-occur and incremental search
  6576. to find a location. When pressing RET or `Q', the command
  6577. returns to the original buffer in which the visibility is still
  6578. unchanged. After RET it will also jump to the location selected
  6579. in the indirect buffer and expose the headline hierarchy above.
  6580. With a prefix argument, use the alternative interface: e.g. if
  6581. `org-goto-interface' is 'outline use 'outline-path-completion."
  6582. (interactive "P")
  6583. (org-goto-map)
  6584. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6585. (org-refile-use-outline-path t)
  6586. (org-refile-target-verify-function nil)
  6587. (interface
  6588. (if (not alternative-interface)
  6589. org-goto-interface
  6590. (if (eq org-goto-interface 'outline)
  6591. 'outline-path-completion
  6592. 'outline)))
  6593. (org-goto-start-pos (point))
  6594. (selected-point
  6595. (if (eq interface 'outline)
  6596. (car (org-get-location (current-buffer) org-goto-help))
  6597. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6598. (org-refile-check-position pa)
  6599. (nth 3 pa)))))
  6600. (if selected-point
  6601. (progn
  6602. (org-mark-ring-push org-goto-start-pos)
  6603. (goto-char selected-point)
  6604. (if (or (outline-invisible-p) (org-invisible-p2))
  6605. (org-show-context 'org-goto)))
  6606. (message "Quit"))))
  6607. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6608. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6609. (defvar org-goto-local-auto-isearch-map) ; defined below
  6610. (defun org-get-location (buf help)
  6611. "Let the user select a location in the Org-mode buffer BUF.
  6612. This function uses a recursive edit. It returns the selected position
  6613. or nil."
  6614. (org-no-popups
  6615. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6616. (isearch-hide-immediately nil)
  6617. (isearch-search-fun-function
  6618. (lambda () 'org-goto-local-search-headings))
  6619. (org-goto-selected-point org-goto-exit-command))
  6620. (save-excursion
  6621. (save-window-excursion
  6622. (delete-other-windows)
  6623. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6624. (org-pop-to-buffer-same-window
  6625. (condition-case nil
  6626. (make-indirect-buffer (current-buffer) "*org-goto*")
  6627. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6628. (with-output-to-temp-buffer "*Org Help*"
  6629. (princ (format help (if org-goto-auto-isearch
  6630. " Just type for auto-isearch."
  6631. " n/p/f/b/u to navigate, q to quit."))))
  6632. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6633. (setq buffer-read-only nil)
  6634. (let ((org-startup-truncated t)
  6635. (org-startup-folded nil)
  6636. (org-startup-align-all-tables nil))
  6637. (org-mode)
  6638. (org-overview))
  6639. (setq buffer-read-only t)
  6640. (if (and (boundp 'org-goto-start-pos)
  6641. (integer-or-marker-p org-goto-start-pos))
  6642. (let ((org-show-hierarchy-above t)
  6643. (org-show-siblings t)
  6644. (org-show-following-heading t))
  6645. (goto-char org-goto-start-pos)
  6646. (and (outline-invisible-p) (org-show-context)))
  6647. (goto-char (point-min)))
  6648. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6649. (message "Select location and press RET")
  6650. (use-local-map org-goto-map)
  6651. (recursive-edit)))
  6652. (kill-buffer "*org-goto*")
  6653. (cons org-goto-selected-point org-goto-exit-command))))
  6654. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6655. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6656. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6657. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6658. (defun org-goto-local-search-headings (string bound noerror)
  6659. "Search and make sure that any matches are in headlines."
  6660. (catch 'return
  6661. (while (if isearch-forward
  6662. (search-forward string bound noerror)
  6663. (search-backward string bound noerror))
  6664. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6665. (and (member :headline context)
  6666. (not (member :tags context))))
  6667. (throw 'return (point))))))
  6668. (defun org-goto-local-auto-isearch ()
  6669. "Start isearch."
  6670. (interactive)
  6671. (goto-char (point-min))
  6672. (let ((keys (this-command-keys)))
  6673. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6674. (isearch-mode t)
  6675. (isearch-process-search-char (string-to-char keys)))))
  6676. (defun org-goto-ret (&optional arg)
  6677. "Finish `org-goto' by going to the new location."
  6678. (interactive "P")
  6679. (setq org-goto-selected-point (point)
  6680. org-goto-exit-command 'return)
  6681. (throw 'exit nil))
  6682. (defun org-goto-left ()
  6683. "Finish `org-goto' by going to the new location."
  6684. (interactive)
  6685. (if (org-at-heading-p)
  6686. (progn
  6687. (beginning-of-line 1)
  6688. (setq org-goto-selected-point (point)
  6689. org-goto-exit-command 'left)
  6690. (throw 'exit nil))
  6691. (user-error "Not on a heading")))
  6692. (defun org-goto-right ()
  6693. "Finish `org-goto' by going to the new location."
  6694. (interactive)
  6695. (if (org-at-heading-p)
  6696. (progn
  6697. (setq org-goto-selected-point (point)
  6698. org-goto-exit-command 'right)
  6699. (throw 'exit nil))
  6700. (user-error "Not on a heading")))
  6701. (defun org-goto-quit ()
  6702. "Finish `org-goto' without cursor motion."
  6703. (interactive)
  6704. (setq org-goto-selected-point nil)
  6705. (setq org-goto-exit-command 'quit)
  6706. (throw 'exit nil))
  6707. ;;; Indirect buffer display of subtrees
  6708. (defvar org-indirect-dedicated-frame nil
  6709. "This is the frame being used for indirect tree display.")
  6710. (defvar org-last-indirect-buffer nil)
  6711. (defun org-tree-to-indirect-buffer (&optional arg)
  6712. "Create indirect buffer and narrow it to current subtree.
  6713. With a numerical prefix ARG, go up to this level and then take that tree.
  6714. If ARG is negative, go up that many levels.
  6715. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6716. indirect buffer previously made with this command, to avoid proliferation of
  6717. indirect buffers. However, when you call the command with a \
  6718. \\[universal-argument] prefix, or
  6719. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6720. is kept so that you can work with several indirect buffers at the same time.
  6721. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6722. \\[universal-argument] prefix also
  6723. requests that a new frame be made for the new buffer, so that the dedicated
  6724. frame is not changed."
  6725. (interactive "P")
  6726. (let ((cbuf (current-buffer))
  6727. (cwin (selected-window))
  6728. (pos (point))
  6729. beg end level heading ibuf)
  6730. (save-excursion
  6731. (org-back-to-heading t)
  6732. (when (numberp arg)
  6733. (setq level (org-outline-level))
  6734. (if (< arg 0) (setq arg (+ level arg)))
  6735. (while (> (setq level (org-outline-level)) arg)
  6736. (org-up-heading-safe)))
  6737. (setq beg (point)
  6738. heading (org-get-heading))
  6739. (org-end-of-subtree t t)
  6740. (if (org-at-heading-p) (backward-char 1))
  6741. (setq end (point)))
  6742. (if (and (buffer-live-p org-last-indirect-buffer)
  6743. (not (eq org-indirect-buffer-display 'new-frame))
  6744. (not arg))
  6745. (kill-buffer org-last-indirect-buffer))
  6746. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6747. org-last-indirect-buffer ibuf)
  6748. (cond
  6749. ((or (eq org-indirect-buffer-display 'new-frame)
  6750. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6751. (select-frame (make-frame))
  6752. (delete-other-windows)
  6753. (org-pop-to-buffer-same-window ibuf)
  6754. (org-set-frame-title heading))
  6755. ((eq org-indirect-buffer-display 'dedicated-frame)
  6756. (raise-frame
  6757. (select-frame (or (and org-indirect-dedicated-frame
  6758. (frame-live-p org-indirect-dedicated-frame)
  6759. org-indirect-dedicated-frame)
  6760. (setq org-indirect-dedicated-frame (make-frame)))))
  6761. (delete-other-windows)
  6762. (org-pop-to-buffer-same-window ibuf)
  6763. (org-set-frame-title (concat "Indirect: " heading)))
  6764. ((eq org-indirect-buffer-display 'current-window)
  6765. (org-pop-to-buffer-same-window ibuf))
  6766. ((eq org-indirect-buffer-display 'other-window)
  6767. (pop-to-buffer ibuf))
  6768. (t (error "Invalid value")))
  6769. (if (featurep 'xemacs)
  6770. (save-excursion (org-mode) (turn-on-font-lock)))
  6771. (narrow-to-region beg end)
  6772. (show-all)
  6773. (goto-char pos)
  6774. (run-hook-with-args 'org-cycle-hook 'all)
  6775. (and (window-live-p cwin) (select-window cwin))))
  6776. (defun org-get-indirect-buffer (&optional buffer heading)
  6777. (setq buffer (or buffer (current-buffer)))
  6778. (let ((n 1) (base (buffer-name buffer)) bname)
  6779. (while (buffer-live-p
  6780. (get-buffer
  6781. (setq bname
  6782. (concat base "-"
  6783. (if heading (concat heading "-" (number-to-string n))
  6784. (number-to-string n))))))
  6785. (setq n (1+ n)))
  6786. (condition-case nil
  6787. (make-indirect-buffer buffer bname 'clone)
  6788. (error (make-indirect-buffer buffer bname)))))
  6789. (defun org-set-frame-title (title)
  6790. "Set the title of the current frame to the string TITLE."
  6791. ;; FIXME: how to name a single frame in XEmacs???
  6792. (unless (featurep 'xemacs)
  6793. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6794. ;;;; Structure editing
  6795. ;;; Inserting headlines
  6796. (defun org-previous-line-empty-p (&optional next)
  6797. "Is the previous line a blank line?
  6798. When NEXT is non-nil, check the next line instead."
  6799. (save-excursion
  6800. (and (not (bobp))
  6801. (or (beginning-of-line (if next 2 0)) t)
  6802. (save-match-data
  6803. (looking-at "[ \t]*$")))))
  6804. (defun org-insert-heading (&optional arg invisible-ok)
  6805. "Insert a new heading or item with same depth at point.
  6806. If point is in a plain list and ARG is nil, create a new list item.
  6807. With one universal prefix argument, insert a heading even in lists.
  6808. With two universal prefix arguments, insert the heading at the end
  6809. of the parent subtree.
  6810. If point is at the beginning of a headline, insert a sibling before
  6811. the current headline. If point is not at the beginning, split the line
  6812. and create a new headline with the text in the current line after point
  6813. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6814. If point is at the beginning of a normal line, turn this line into
  6815. a heading.
  6816. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6817. This is important for non-interactive uses of the command."
  6818. (interactive "P")
  6819. (if (org-called-interactively-p 'any) (org-reveal))
  6820. (let ((itemp (org-in-item-p))
  6821. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6822. (respect-content (or org-insert-heading-respect-content
  6823. (equal arg '(16))))
  6824. (initial-content "")
  6825. (adjust-empty-lines t))
  6826. (cond
  6827. ((or (= (buffer-size) 0)
  6828. (and (not (save-excursion
  6829. (and (ignore-errors (org-back-to-heading invisible-ok))
  6830. (org-at-heading-p))))
  6831. (or arg (not itemp))))
  6832. ;; At beginning of buffer or so high up that only a heading
  6833. ;; makes sense.
  6834. (insert
  6835. (if (or (bobp) (org-previous-line-empty-p)) "" "\n")
  6836. (if (org-in-src-block-p) ",* " "* "))
  6837. (run-hooks 'org-insert-heading-hook))
  6838. ((and itemp (not (equal arg '(4))))
  6839. ;; Insert an item
  6840. (org-insert-item))
  6841. (t
  6842. ;; Insert a heading
  6843. (save-restriction
  6844. (widen)
  6845. (let* ((level nil)
  6846. (on-heading (org-at-heading-p))
  6847. (empty-line-p (if on-heading
  6848. (org-previous-line-empty-p)
  6849. ;; We will decide later
  6850. nil))
  6851. ;; Get a level string to fall back on
  6852. (fix-level
  6853. (save-excursion
  6854. (org-back-to-heading t)
  6855. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6856. (looking-at org-outline-regexp)
  6857. (make-string (1- (length (match-string 0))) ?*)))
  6858. (stars
  6859. (save-excursion
  6860. (condition-case nil
  6861. (progn
  6862. (org-back-to-heading invisible-ok)
  6863. (when (and (not on-heading)
  6864. (featurep 'org-inlinetask)
  6865. (integerp org-inlinetask-min-level)
  6866. (>= (length (match-string 0))
  6867. org-inlinetask-min-level))
  6868. ;; Find a heading level before the inline task
  6869. (while (and (setq level (org-up-heading-safe))
  6870. (>= level org-inlinetask-min-level)))
  6871. (if (org-at-heading-p)
  6872. (org-back-to-heading invisible-ok)
  6873. (error "This should not happen")))
  6874. (unless (and (save-excursion
  6875. (save-match-data
  6876. (org-backward-heading-same-level
  6877. 1 invisible-ok))
  6878. (= (point) (match-beginning 0)))
  6879. (not (org-previous-line-empty-p t)))
  6880. (setq empty-line-p (or empty-line-p
  6881. (org-previous-line-empty-p))))
  6882. (match-string 0))
  6883. (error (or fix-level "* ")))))
  6884. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6885. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6886. pos hide-previous previous-pos)
  6887. ;; If we insert after content, move there and clean up whitespace
  6888. (when respect-content
  6889. (org-end-of-subtree nil t)
  6890. (skip-chars-backward " \r\n")
  6891. (and (looking-at "[ \t]+") (replace-match ""))
  6892. (unless (eobp) (forward-char 1))
  6893. (when (looking-at "^\\*")
  6894. (unless (bobp) (backward-char 1))
  6895. (insert "\n")))
  6896. ;; If we are splitting, grab the text that should be moved to the new headline
  6897. (when may-split
  6898. (if (org-on-heading-p)
  6899. ;; This is a heading, we split intelligently (keeping tags)
  6900. (let ((pos (point)))
  6901. (goto-char (point-at-bol))
  6902. (unless (looking-at org-complex-heading-regexp)
  6903. (error "This should not happen"))
  6904. (when (and (match-beginning 4)
  6905. (> pos (match-beginning 4))
  6906. (< pos (match-end 4)))
  6907. (setq initial-content (buffer-substring pos (match-end 4)))
  6908. (goto-char pos)
  6909. (delete-region (point) (match-end 4))
  6910. (if (looking-at "[ \t]*$")
  6911. (replace-match "")
  6912. (insert (make-string (length initial-content) ?\ )))
  6913. (setq initial-content (org-trim initial-content)))
  6914. (goto-char pos))
  6915. ;; a normal line
  6916. (unless (bolp)
  6917. (setq initial-content (buffer-substring (point) (point-at-eol)))
  6918. (delete-region (point) (point-at-eol))
  6919. (setq initial-content (org-trim initial-content)))))
  6920. ;; If we are at the beginning of the line, insert before it. Else after
  6921. (cond
  6922. ((and (bolp) (looking-at "[ \t]*$")))
  6923. ((and (bolp) (not (looking-at "[ \t]*$")))
  6924. (open-line 1))
  6925. (t
  6926. (goto-char (point-at-eol))
  6927. (insert "\n")))
  6928. ;; Insert the new heading
  6929. (insert stars)
  6930. (just-one-space)
  6931. (insert initial-content)
  6932. (when adjust-empty-lines
  6933. (if (or (not blank)
  6934. (and blank (not (org-previous-line-empty-p))))
  6935. (org-N-empty-lines-before-current (if blank 1 0))))
  6936. (run-hooks 'org-insert-heading-hook)))))))
  6937. (defun org-N-empty-lines-before-current (N)
  6938. "Make the number of empty lines before current exactly N.
  6939. So this will delete or add empty lines."
  6940. (save-excursion
  6941. (goto-char (point-at-bol))
  6942. (if (looking-back "\\s-+" nil 'greedy)
  6943. (replace-match ""))
  6944. (or (bobp) (insert "\n"))
  6945. (while (> N 0)
  6946. (insert "\n")
  6947. (setq N (1- N)))))
  6948. (defun org-get-heading (&optional no-tags no-todo)
  6949. "Return the heading of the current entry, without the stars.
  6950. When NO-TAGS is non-nil, don't include tags.
  6951. When NO-TODO is non-nil, don't include TODO keywords."
  6952. (save-excursion
  6953. (org-back-to-heading t)
  6954. (cond
  6955. ((and no-tags no-todo)
  6956. (looking-at org-complex-heading-regexp)
  6957. (match-string 4))
  6958. (no-tags
  6959. (looking-at (concat org-outline-regexp
  6960. "\\(.*?\\)"
  6961. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6962. (match-string 1))
  6963. (no-todo
  6964. (looking-at org-todo-line-regexp)
  6965. (match-string 3))
  6966. (t (looking-at org-heading-regexp)
  6967. (match-string 2)))))
  6968. (defvar orgstruct-mode) ; defined below
  6969. (defun org-heading-components ()
  6970. "Return the components of the current heading.
  6971. This is a list with the following elements:
  6972. - the level as an integer
  6973. - the reduced level, different if `org-odd-levels-only' is set.
  6974. - the TODO keyword, or nil
  6975. - the priority character, like ?A, or nil if no priority is given
  6976. - the headline text itself, or the tags string if no headline text
  6977. - the tags string, or nil."
  6978. (save-excursion
  6979. (org-back-to-heading t)
  6980. (if (let (case-fold-search)
  6981. (looking-at
  6982. (if orgstruct-mode
  6983. org-heading-regexp
  6984. org-complex-heading-regexp)))
  6985. (if orgstruct-mode
  6986. (list (length (match-string 1))
  6987. (org-reduced-level (length (match-string 1)))
  6988. nil
  6989. nil
  6990. (match-string 2)
  6991. nil)
  6992. (list (length (match-string 1))
  6993. (org-reduced-level (length (match-string 1)))
  6994. (org-match-string-no-properties 2)
  6995. (and (match-end 3) (aref (match-string 3) 2))
  6996. (org-match-string-no-properties 4)
  6997. (org-match-string-no-properties 5))))))
  6998. (defun org-get-entry ()
  6999. "Get the entry text, after heading, entire subtree."
  7000. (save-excursion
  7001. (org-back-to-heading t)
  7002. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7003. (defun org-insert-heading-after-current ()
  7004. "Insert a new heading with same level as current, after current subtree."
  7005. (interactive)
  7006. (org-back-to-heading)
  7007. (org-insert-heading)
  7008. (org-move-subtree-down)
  7009. (end-of-line 1))
  7010. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  7011. "Insert heading with `org-insert-heading-respect-content' set to t."
  7012. (interactive "P")
  7013. (let ((org-insert-heading-respect-content t))
  7014. (org-insert-heading '(4) invisible-ok)))
  7015. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7016. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7017. (interactive "P")
  7018. (let ((org-insert-heading-respect-content t))
  7019. (org-insert-todo-heading force-state '(4))))
  7020. (defun org-insert-todo-heading (arg &optional force-heading)
  7021. "Insert a new heading with the same level and TODO state as current heading.
  7022. If the heading has no TODO state, or if the state is DONE, use the first
  7023. state (TODO by default). Also one prefix arg, force first state. With two
  7024. prefix args, force inserting at the end of the parent subtree."
  7025. (interactive "P")
  7026. (when (or force-heading (not (org-insert-item 'checkbox)))
  7027. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7028. force-heading))
  7029. (save-excursion
  7030. (org-back-to-heading)
  7031. (outline-previous-heading)
  7032. (looking-at org-todo-line-regexp))
  7033. (let*
  7034. ((new-mark-x
  7035. (if (or arg
  7036. (not (match-beginning 2))
  7037. (member (match-string 2) org-done-keywords))
  7038. (car org-todo-keywords-1)
  7039. (match-string 2)))
  7040. (new-mark
  7041. (or
  7042. (run-hook-with-args-until-success
  7043. 'org-todo-get-default-hook new-mark-x nil)
  7044. new-mark-x)))
  7045. (beginning-of-line 1)
  7046. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7047. (if org-treat-insert-todo-heading-as-state-change
  7048. (org-todo new-mark)
  7049. (insert new-mark " "))))
  7050. (when org-provide-todo-statistics
  7051. (org-update-parent-todo-statistics))))
  7052. (defun org-insert-subheading (arg)
  7053. "Insert a new subheading and demote it.
  7054. Works for outline headings and for plain lists alike."
  7055. (interactive "P")
  7056. (org-insert-heading arg)
  7057. (cond
  7058. ((org-at-heading-p) (org-do-demote))
  7059. ((org-at-item-p) (org-indent-item))))
  7060. (defun org-insert-todo-subheading (arg)
  7061. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7062. Works for outline headings and for plain lists alike."
  7063. (interactive "P")
  7064. (org-insert-todo-heading arg)
  7065. (cond
  7066. ((org-at-heading-p) (org-do-demote))
  7067. ((org-at-item-p) (org-indent-item))))
  7068. ;;; Promotion and Demotion
  7069. (defvar org-after-demote-entry-hook nil
  7070. "Hook run after an entry has been demoted.
  7071. The cursor will be at the beginning of the entry.
  7072. When a subtree is being demoted, the hook will be called for each node.")
  7073. (defvar org-after-promote-entry-hook nil
  7074. "Hook run after an entry has been promoted.
  7075. The cursor will be at the beginning of the entry.
  7076. When a subtree is being promoted, the hook will be called for each node.")
  7077. (defun org-promote-subtree ()
  7078. "Promote the entire subtree.
  7079. See also `org-promote'."
  7080. (interactive)
  7081. (save-excursion
  7082. (org-with-limited-levels (org-map-tree 'org-promote)))
  7083. (org-fix-position-after-promote))
  7084. (defun org-demote-subtree ()
  7085. "Demote the entire subtree. See `org-demote'.
  7086. See also `org-promote'."
  7087. (interactive)
  7088. (save-excursion
  7089. (org-with-limited-levels (org-map-tree 'org-demote)))
  7090. (org-fix-position-after-promote))
  7091. (defun org-do-promote ()
  7092. "Promote the current heading higher up the tree.
  7093. If the region is active in `transient-mark-mode', promote all headings
  7094. in the region."
  7095. (interactive)
  7096. (save-excursion
  7097. (if (org-region-active-p)
  7098. (org-map-region 'org-promote (region-beginning) (region-end))
  7099. (org-promote)))
  7100. (org-fix-position-after-promote))
  7101. (defun org-do-demote ()
  7102. "Demote the current heading lower down the tree.
  7103. If the region is active in `transient-mark-mode', demote all headings
  7104. in the region."
  7105. (interactive)
  7106. (save-excursion
  7107. (if (org-region-active-p)
  7108. (org-map-region 'org-demote (region-beginning) (region-end))
  7109. (org-demote)))
  7110. (org-fix-position-after-promote))
  7111. (defun org-fix-position-after-promote ()
  7112. "Make sure that after pro/demotion cursor position is right."
  7113. (let ((pos (point)))
  7114. (when (save-excursion
  7115. (beginning-of-line 1)
  7116. (looking-at org-todo-line-regexp)
  7117. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7118. (cond ((eobp) (insert " "))
  7119. ((eolp) (insert " "))
  7120. ((equal (char-after) ?\ ) (forward-char 1))))))
  7121. (defun org-current-level ()
  7122. "Return the level of the current entry, or nil if before the first headline.
  7123. The level is the number of stars at the beginning of the headline."
  7124. (save-excursion
  7125. (org-with-limited-levels
  7126. (if (ignore-errors (org-back-to-heading t))
  7127. (funcall outline-level)))))
  7128. (defun org-get-previous-line-level ()
  7129. "Return the outline depth of the last headline before the current line.
  7130. Returns 0 for the first headline in the buffer, and nil if before the
  7131. first headline."
  7132. (and (org-current-level)
  7133. (or (and (/= (line-beginning-position) (point-min))
  7134. (save-excursion (beginning-of-line 0) (org-current-level)))
  7135. 0)))
  7136. (defun org-reduced-level (l)
  7137. "Compute the effective level of a heading.
  7138. This takes into account the setting of `org-odd-levels-only'."
  7139. (cond
  7140. ((zerop l) 0)
  7141. (org-odd-levels-only (1+ (floor (/ l 2))))
  7142. (t l)))
  7143. (defun org-level-increment ()
  7144. "Return the number of stars that will be added or removed at a
  7145. time to headlines when structure editing, based on the value of
  7146. `org-odd-levels-only'."
  7147. (if org-odd-levels-only 2 1))
  7148. (defun org-get-valid-level (level &optional change)
  7149. "Rectify a level change under the influence of `org-odd-levels-only'
  7150. LEVEL is a current level, CHANGE is by how much the level should be
  7151. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7152. even level numbers will become the next higher odd number."
  7153. (if org-odd-levels-only
  7154. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7155. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7156. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7157. (max 1 (+ level (or change 0)))))
  7158. (if (boundp 'define-obsolete-function-alias)
  7159. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7160. (define-obsolete-function-alias 'org-get-legal-level
  7161. 'org-get-valid-level)
  7162. (define-obsolete-function-alias 'org-get-legal-level
  7163. 'org-get-valid-level "23.1")))
  7164. (defun org-promote ()
  7165. "Promote the current heading higher up the tree.
  7166. If the region is active in `transient-mark-mode', promote all headings
  7167. in the region."
  7168. (org-back-to-heading t)
  7169. (let* ((level (save-match-data (funcall outline-level)))
  7170. (after-change-functions (remove 'flyspell-after-change-function
  7171. after-change-functions))
  7172. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7173. (diff (abs (- level (length up-head) -1))))
  7174. (cond ((and (= level 1) org-called-with-limited-levels
  7175. org-allow-promoting-top-level-subtree)
  7176. (replace-match "# " nil t))
  7177. ((= level 1)
  7178. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7179. (t (replace-match up-head nil t)))
  7180. ;; Fixup tag positioning
  7181. (unless (= level 1)
  7182. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7183. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7184. (run-hooks 'org-after-promote-entry-hook)))
  7185. (defun org-demote ()
  7186. "Demote the current heading lower down the tree.
  7187. If the region is active in `transient-mark-mode', demote all headings
  7188. in the region."
  7189. (org-back-to-heading t)
  7190. (let* ((level (save-match-data (funcall outline-level)))
  7191. (after-change-functions (remove 'flyspell-after-change-function
  7192. after-change-functions))
  7193. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7194. (diff (abs (- level (length down-head) -1))))
  7195. (replace-match down-head nil t)
  7196. ;; Fixup tag positioning
  7197. (and org-auto-align-tags (org-set-tags nil 'ignore-column))
  7198. (if org-adapt-indentation (org-fixup-indentation diff))
  7199. (run-hooks 'org-after-demote-entry-hook)))
  7200. (defun org-cycle-level ()
  7201. "Cycle the level of an empty headline through possible states.
  7202. This goes first to child, then to parent, level, then up the hierarchy.
  7203. After top level, it switches back to sibling level."
  7204. (interactive)
  7205. (let ((org-adapt-indentation nil))
  7206. (when (org-point-at-end-of-empty-headline)
  7207. (setq this-command 'org-cycle-level) ; Only needed for caching
  7208. (let ((cur-level (org-current-level))
  7209. (prev-level (org-get-previous-line-level)))
  7210. (cond
  7211. ;; If first headline in file, promote to top-level.
  7212. ((= prev-level 0)
  7213. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7214. do (org-do-promote)))
  7215. ;; If same level as prev, demote one.
  7216. ((= prev-level cur-level)
  7217. (org-do-demote))
  7218. ;; If parent is top-level, promote to top level if not already.
  7219. ((= prev-level 1)
  7220. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7221. do (org-do-promote)))
  7222. ;; If top-level, return to prev-level.
  7223. ((= cur-level 1)
  7224. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7225. do (org-do-demote)))
  7226. ;; If less than prev-level, promote one.
  7227. ((< cur-level prev-level)
  7228. (org-do-promote))
  7229. ;; If deeper than prev-level, promote until higher than
  7230. ;; prev-level.
  7231. ((> cur-level prev-level)
  7232. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7233. do (org-do-promote))))
  7234. t))))
  7235. (defun org-map-tree (fun)
  7236. "Call FUN for every heading underneath the current one."
  7237. (org-back-to-heading)
  7238. (let ((level (funcall outline-level)))
  7239. (save-excursion
  7240. (funcall fun)
  7241. (while (and (progn
  7242. (outline-next-heading)
  7243. (> (funcall outline-level) level))
  7244. (not (eobp)))
  7245. (funcall fun)))))
  7246. (defun org-map-region (fun beg end)
  7247. "Call FUN for every heading between BEG and END."
  7248. (let ((org-ignore-region t))
  7249. (save-excursion
  7250. (setq end (copy-marker end))
  7251. (goto-char beg)
  7252. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7253. (< (point) end))
  7254. (funcall fun))
  7255. (while (and (progn
  7256. (outline-next-heading)
  7257. (< (point) end))
  7258. (not (eobp)))
  7259. (funcall fun)))))
  7260. (defvar org-property-end-re) ; silence byte-compiler
  7261. (defun org-fixup-indentation (diff)
  7262. "Change the indentation in the current entry by DIFF.
  7263. However, if any line in the current entry has no indentation, or if it
  7264. would end up with no indentation after the change, nothing at all is done."
  7265. (save-excursion
  7266. (let ((end (save-excursion (outline-next-heading)
  7267. (point-marker)))
  7268. (prohibit (if (> diff 0)
  7269. "^\\S-"
  7270. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7271. col)
  7272. (unless (save-excursion (end-of-line 1)
  7273. (re-search-forward prohibit end t))
  7274. (while (and (< (point) end)
  7275. (re-search-forward "^[ \t]+" end t))
  7276. (goto-char (match-end 0))
  7277. (setq col (current-column))
  7278. (if (< diff 0) (replace-match ""))
  7279. (org-indent-to-column (+ diff col))))
  7280. (move-marker end nil))))
  7281. (defun org-convert-to-odd-levels ()
  7282. "Convert an org-mode file with all levels allowed to one with odd levels.
  7283. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7284. level 5 etc."
  7285. (interactive)
  7286. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7287. (let ((outline-level 'org-outline-level)
  7288. (org-odd-levels-only nil) n)
  7289. (save-excursion
  7290. (goto-char (point-min))
  7291. (while (re-search-forward "^\\*\\*+ " nil t)
  7292. (setq n (- (length (match-string 0)) 2))
  7293. (while (>= (setq n (1- n)) 0)
  7294. (org-demote))
  7295. (end-of-line 1))))))
  7296. (defun org-convert-to-oddeven-levels ()
  7297. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7298. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7299. file contains a section with an even level, conversion would
  7300. destroy the structure of the file. An error is signaled in this
  7301. case."
  7302. (interactive)
  7303. (goto-char (point-min))
  7304. ;; First check if there are no even levels
  7305. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7306. (org-show-context t)
  7307. (error "Not all levels are odd in this file. Conversion not possible"))
  7308. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7309. (let ((outline-regexp org-outline-regexp)
  7310. (outline-level 'org-outline-level)
  7311. (org-odd-levels-only nil) n)
  7312. (save-excursion
  7313. (goto-char (point-min))
  7314. (while (re-search-forward "^\\*\\*+ " nil t)
  7315. (setq n (/ (1- (length (match-string 0))) 2))
  7316. (while (>= (setq n (1- n)) 0)
  7317. (org-promote))
  7318. (end-of-line 1))))))
  7319. (defun org-tr-level (n)
  7320. "Make N odd if required."
  7321. (if org-odd-levels-only (1+ (/ n 2)) n))
  7322. ;;; Vertical tree motion, cutting and pasting of subtrees
  7323. (defun org-move-subtree-up (&optional arg)
  7324. "Move the current subtree up past ARG headlines of the same level."
  7325. (interactive "p")
  7326. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7327. (defun org-move-subtree-down (&optional arg)
  7328. "Move the current subtree down past ARG headlines of the same level."
  7329. (interactive "p")
  7330. (setq arg (prefix-numeric-value arg))
  7331. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7332. 'org-get-last-sibling))
  7333. (ins-point (make-marker))
  7334. (cnt (abs arg))
  7335. (col (current-column))
  7336. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7337. ;; Select the tree
  7338. (org-back-to-heading)
  7339. (setq beg0 (point))
  7340. (save-excursion
  7341. (setq ne-beg (org-back-over-empty-lines))
  7342. (setq beg (point)))
  7343. (save-match-data
  7344. (save-excursion (outline-end-of-heading)
  7345. (setq folded (outline-invisible-p)))
  7346. (outline-end-of-subtree))
  7347. (outline-next-heading)
  7348. (setq ne-end (org-back-over-empty-lines))
  7349. (setq end (point))
  7350. (goto-char beg0)
  7351. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7352. ;; include less whitespace
  7353. (save-excursion
  7354. (goto-char beg)
  7355. (forward-line (- ne-beg ne-end))
  7356. (setq beg (point))))
  7357. ;; Find insertion point, with error handling
  7358. (while (> cnt 0)
  7359. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7360. (progn (goto-char beg0)
  7361. (user-error "Cannot move past superior level or buffer limit")))
  7362. (setq cnt (1- cnt)))
  7363. (if (> arg 0)
  7364. ;; Moving forward - still need to move over subtree
  7365. (progn (org-end-of-subtree t t)
  7366. (save-excursion
  7367. (org-back-over-empty-lines)
  7368. (or (bolp) (newline)))))
  7369. (setq ne-ins (org-back-over-empty-lines))
  7370. (move-marker ins-point (point))
  7371. (setq txt (buffer-substring beg end))
  7372. (org-save-markers-in-region beg end)
  7373. (delete-region beg end)
  7374. (org-remove-empty-overlays-at beg)
  7375. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7376. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7377. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7378. (let ((bbb (point)))
  7379. (insert-before-markers txt)
  7380. (org-reinstall-markers-in-region bbb)
  7381. (move-marker ins-point bbb))
  7382. (or (bolp) (insert "\n"))
  7383. (setq ins-end (point))
  7384. (goto-char ins-point)
  7385. (org-skip-whitespace)
  7386. (when (and (< arg 0)
  7387. (org-first-sibling-p)
  7388. (> ne-ins ne-beg))
  7389. ;; Move whitespace back to beginning
  7390. (save-excursion
  7391. (goto-char ins-end)
  7392. (let ((kill-whole-line t))
  7393. (kill-line (- ne-ins ne-beg)) (point)))
  7394. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7395. (move-marker ins-point nil)
  7396. (if folded
  7397. (hide-subtree)
  7398. (org-show-entry)
  7399. (show-children)
  7400. (org-cycle-hide-drawers 'children))
  7401. (org-clean-visibility-after-subtree-move)
  7402. ;; move back to the initial column we were at
  7403. (move-to-column col)))
  7404. (defvar org-subtree-clip ""
  7405. "Clipboard for cut and paste of subtrees.
  7406. This is actually only a copy of the kill, because we use the normal kill
  7407. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7408. (defvar org-subtree-clip-folded nil
  7409. "Was the last copied subtree folded?
  7410. This is used to fold the tree back after pasting.")
  7411. (defun org-cut-subtree (&optional n)
  7412. "Cut the current subtree into the clipboard.
  7413. With prefix arg N, cut this many sequential subtrees.
  7414. This is a short-hand for marking the subtree and then cutting it."
  7415. (interactive "p")
  7416. (org-copy-subtree n 'cut))
  7417. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7418. "Copy the current subtree it in the clipboard.
  7419. With prefix arg N, copy this many sequential subtrees.
  7420. This is a short-hand for marking the subtree and then copying it.
  7421. If CUT is non-nil, actually cut the subtree.
  7422. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7423. of some markers in the region, even if CUT is non-nil. This is
  7424. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7425. (interactive "p")
  7426. (let (beg end folded (beg0 (point)))
  7427. (if (org-called-interactively-p 'any)
  7428. (org-back-to-heading nil) ; take what looks like a subtree
  7429. (org-back-to-heading t)) ; take what is really there
  7430. (setq beg (point))
  7431. (skip-chars-forward " \t\r\n")
  7432. (save-match-data
  7433. (if nosubtrees
  7434. (outline-next-heading)
  7435. (save-excursion (outline-end-of-heading)
  7436. (setq folded (outline-invisible-p)))
  7437. (condition-case nil
  7438. (org-forward-heading-same-level (1- n) t)
  7439. (error nil))
  7440. (org-end-of-subtree t t)))
  7441. (setq end (point))
  7442. (goto-char beg0)
  7443. (when (> end beg)
  7444. (setq org-subtree-clip-folded folded)
  7445. (when (or cut force-store-markers)
  7446. (org-save-markers-in-region beg end))
  7447. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7448. (setq org-subtree-clip (current-kill 0))
  7449. (message "%s: Subtree(s) with %d characters"
  7450. (if cut "Cut" "Copied")
  7451. (length org-subtree-clip)))))
  7452. (defun org-paste-subtree (&optional level tree for-yank)
  7453. "Paste the clipboard as a subtree, with modification of headline level.
  7454. The entire subtree is promoted or demoted in order to match a new headline
  7455. level.
  7456. If the cursor is at the beginning of a headline, the same level as
  7457. that headline is used to paste the tree.
  7458. If not, the new level is derived from the *visible* headings
  7459. before and after the insertion point, and taken to be the inferior headline
  7460. level of the two. So if the previous visible heading is level 3 and the
  7461. next is level 4 (or vice versa), level 4 will be used for insertion.
  7462. This makes sure that the subtree remains an independent subtree and does
  7463. not swallow low level entries.
  7464. You can also force a different level, either by using a numeric prefix
  7465. argument, or by inserting the heading marker by hand. For example, if the
  7466. cursor is after \"*****\", then the tree will be shifted to level 5.
  7467. If optional TREE is given, use this text instead of the kill ring.
  7468. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7469. move back over whitespace before inserting, and move point to the end of
  7470. the inserted text when done."
  7471. (interactive "P")
  7472. (setq tree (or tree (and kill-ring (current-kill 0))))
  7473. (unless (org-kill-is-subtree-p tree)
  7474. (user-error "%s"
  7475. (substitute-command-keys
  7476. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7477. (org-with-limited-levels
  7478. (let* ((visp (not (outline-invisible-p)))
  7479. (txt tree)
  7480. (^re_ "\\(\\*+\\)[ \t]*")
  7481. (old-level (if (string-match org-outline-regexp-bol txt)
  7482. (- (match-end 0) (match-beginning 0) 1)
  7483. -1))
  7484. (force-level (cond (level (prefix-numeric-value level))
  7485. ((and (looking-at "[ \t]*$")
  7486. (string-match
  7487. "^\\*+$" (buffer-substring
  7488. (point-at-bol) (point))))
  7489. (- (match-end 1) (match-beginning 1)))
  7490. ((and (bolp)
  7491. (looking-at org-outline-regexp))
  7492. (- (match-end 0) (point) 1))))
  7493. (previous-level (save-excursion
  7494. (condition-case nil
  7495. (progn
  7496. (outline-previous-visible-heading 1)
  7497. (if (looking-at ^re_)
  7498. (- (match-end 0) (match-beginning 0) 1)
  7499. 1))
  7500. (error 1))))
  7501. (next-level (save-excursion
  7502. (condition-case nil
  7503. (progn
  7504. (or (looking-at org-outline-regexp)
  7505. (outline-next-visible-heading 1))
  7506. (if (looking-at ^re_)
  7507. (- (match-end 0) (match-beginning 0) 1)
  7508. 1))
  7509. (error 1))))
  7510. (new-level (or force-level (max previous-level next-level)))
  7511. (shift (if (or (= old-level -1)
  7512. (= new-level -1)
  7513. (= old-level new-level))
  7514. 0
  7515. (- new-level old-level)))
  7516. (delta (if (> shift 0) -1 1))
  7517. (func (if (> shift 0) 'org-demote 'org-promote))
  7518. (org-odd-levels-only nil)
  7519. beg end newend)
  7520. ;; Remove the forced level indicator
  7521. (if force-level
  7522. (delete-region (point-at-bol) (point)))
  7523. ;; Paste
  7524. (beginning-of-line (if (bolp) 1 2))
  7525. (setq beg (point))
  7526. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7527. (insert-before-markers txt)
  7528. (unless (string-match "\n\\'" txt) (insert "\n"))
  7529. (setq newend (point))
  7530. (org-reinstall-markers-in-region beg)
  7531. (setq end (point))
  7532. (goto-char beg)
  7533. (skip-chars-forward " \t\n\r")
  7534. (setq beg (point))
  7535. (if (and (outline-invisible-p) visp)
  7536. (save-excursion (outline-show-heading)))
  7537. ;; Shift if necessary
  7538. (unless (= shift 0)
  7539. (save-restriction
  7540. (narrow-to-region beg end)
  7541. (while (not (= shift 0))
  7542. (org-map-region func (point-min) (point-max))
  7543. (setq shift (+ delta shift)))
  7544. (goto-char (point-min))
  7545. (setq newend (point-max))))
  7546. (when (or (org-called-interactively-p 'interactive) for-yank)
  7547. (message "Clipboard pasted as level %d subtree" new-level))
  7548. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7549. kill-ring
  7550. (eq org-subtree-clip (current-kill 0))
  7551. org-subtree-clip-folded)
  7552. ;; The tree was folded before it was killed/copied
  7553. (hide-subtree))
  7554. (and for-yank (goto-char newend)))))
  7555. (defun org-kill-is-subtree-p (&optional txt)
  7556. "Check if the current kill is an outline subtree, or a set of trees.
  7557. Returns nil if kill does not start with a headline, or if the first
  7558. headline level is not the largest headline level in the tree.
  7559. So this will actually accept several entries of equal levels as well,
  7560. which is OK for `org-paste-subtree'.
  7561. If optional TXT is given, check this string instead of the current kill."
  7562. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7563. (re (org-get-limited-outline-regexp))
  7564. (^re (concat "^" re))
  7565. (start-level (and kill
  7566. (string-match
  7567. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7568. kill)
  7569. (- (match-end 2) (match-beginning 2) 1)))
  7570. (start (1+ (or (match-beginning 2) -1))))
  7571. (if (not start-level)
  7572. (progn
  7573. nil) ;; does not even start with a heading
  7574. (catch 'exit
  7575. (while (setq start (string-match ^re kill (1+ start)))
  7576. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7577. (throw 'exit nil)))
  7578. t))))
  7579. (defvar org-markers-to-move nil
  7580. "Markers that should be moved with a cut-and-paste operation.
  7581. Those markers are stored together with their positions relative to
  7582. the start of the region.")
  7583. (defun org-save-markers-in-region (beg end)
  7584. "Check markers in region.
  7585. If these markers are between BEG and END, record their position relative
  7586. to BEG, so that after moving the block of text, we can put the markers back
  7587. into place.
  7588. This function gets called just before an entry or tree gets cut from the
  7589. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7590. called immediately, to move the markers with the entries."
  7591. (setq org-markers-to-move nil)
  7592. (when (featurep 'org-clock)
  7593. (org-clock-save-markers-for-cut-and-paste beg end))
  7594. (when (featurep 'org-agenda)
  7595. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7596. (defun org-check-and-save-marker (marker beg end)
  7597. "Check if MARKER is between BEG and END.
  7598. If yes, remember the marker and the distance to BEG."
  7599. (when (and (marker-buffer marker)
  7600. (equal (marker-buffer marker) (current-buffer)))
  7601. (if (and (>= marker beg) (< marker end))
  7602. (push (cons marker (- marker beg)) org-markers-to-move))))
  7603. (defun org-reinstall-markers-in-region (beg)
  7604. "Move all remembered markers to their position relative to BEG."
  7605. (mapc (lambda (x)
  7606. (move-marker (car x) (+ beg (cdr x))))
  7607. org-markers-to-move)
  7608. (setq org-markers-to-move nil))
  7609. (defun org-narrow-to-subtree ()
  7610. "Narrow buffer to the current subtree."
  7611. (interactive)
  7612. (save-excursion
  7613. (save-match-data
  7614. (org-with-limited-levels
  7615. (narrow-to-region
  7616. (progn (org-back-to-heading t) (point))
  7617. (progn (org-end-of-subtree t t)
  7618. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7619. (point)))))))
  7620. (defun org-narrow-to-block ()
  7621. "Narrow buffer to the current block."
  7622. (interactive)
  7623. (let* ((case-fold-search t)
  7624. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7625. "^[ \t]*#\\+end_.*")))
  7626. (if blockp
  7627. (narrow-to-region (car blockp) (cdr blockp))
  7628. (user-error "Not in a block"))))
  7629. (eval-when-compile
  7630. (defvar org-property-drawer-re))
  7631. (defvar org-property-start-re) ;; defined below
  7632. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7633. "Clone the task (subtree) at point N times.
  7634. The clones will be inserted as siblings.
  7635. In interactive use, the user will be prompted for the number of
  7636. clones to be produced. If the entry has a timestamp, the user
  7637. will also be prompted for a time shift, which may be a repeater
  7638. as used in time stamps, for example `+3d'. To disable this,
  7639. you can call the function with a universal prefix argument.
  7640. When a valid repeater is given and the entry contains any time
  7641. stamps, the clones will become a sequence in time, with time
  7642. stamps in the subtree shifted for each clone produced. If SHIFT
  7643. is nil or the empty string, time stamps will be left alone. The
  7644. ID property of the original subtree is removed.
  7645. If the original subtree did contain time stamps with a repeater,
  7646. the following will happen:
  7647. - the repeater will be removed in each clone
  7648. - an additional clone will be produced, with the current, unshifted
  7649. date(s) in the entry.
  7650. - the original entry will be placed *after* all the clones, with
  7651. repeater intact.
  7652. - the start days in the repeater in the original entry will be shifted
  7653. to past the last clone.
  7654. In this way you can spell out a number of instances of a repeating task,
  7655. and still retain the repeater to cover future instances of the task."
  7656. (interactive "nNumber of clones to produce: ")
  7657. (let ((shift
  7658. (or shift
  7659. (if (and (not (equal current-prefix-arg '(4)))
  7660. (save-excursion
  7661. (re-search-forward org-ts-regexp-both
  7662. (save-excursion
  7663. (org-end-of-subtree t)
  7664. (point)) t)))
  7665. (read-from-minibuffer
  7666. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7667. ""))) ;; No time shift
  7668. (n-no-remove -1)
  7669. (drawer-re org-drawer-regexp)
  7670. beg end template task idprop
  7671. shift-n shift-what doshift nmin nmax)
  7672. (if (not (and (integerp n) (> n 0)))
  7673. (error "Invalid number of replications %s" n))
  7674. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7675. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7676. shift)))
  7677. (error "Invalid shift specification %s" shift))
  7678. (when doshift
  7679. (setq shift-n (string-to-number (match-string 1 shift))
  7680. shift-what (cdr (assoc (match-string 2 shift)
  7681. '(("d" . day) ("w" . week)
  7682. ("m" . month) ("y" . year))))))
  7683. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7684. (setq nmin 1 nmax n)
  7685. (org-back-to-heading t)
  7686. (setq beg (point))
  7687. (setq idprop (org-entry-get nil "ID"))
  7688. (org-end-of-subtree t t)
  7689. (or (bolp) (insert "\n"))
  7690. (setq end (point))
  7691. (setq template (buffer-substring beg end))
  7692. (when (and doshift
  7693. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7694. (delete-region beg end)
  7695. (setq end beg)
  7696. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7697. (goto-char end)
  7698. (loop for n from nmin to nmax do
  7699. ;; prepare clone
  7700. (with-temp-buffer
  7701. (insert template)
  7702. (org-mode)
  7703. (goto-char (point-min))
  7704. (org-show-subtree)
  7705. (and idprop (if org-clone-delete-id
  7706. (org-entry-delete nil "ID")
  7707. (org-id-get-create t)))
  7708. (unless (= n 0)
  7709. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7710. (kill-whole-line))
  7711. (goto-char (point-min))
  7712. (while (re-search-forward drawer-re nil t)
  7713. (org-remove-empty-drawer-at (point))))
  7714. (goto-char (point-min))
  7715. (when doshift
  7716. (while (re-search-forward org-ts-regexp-both nil t)
  7717. (org-timestamp-change (* n shift-n) shift-what))
  7718. (unless (= n n-no-remove)
  7719. (goto-char (point-min))
  7720. (while (re-search-forward org-ts-regexp nil t)
  7721. (save-excursion
  7722. (goto-char (match-beginning 0))
  7723. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7724. (delete-region (match-beginning 1) (match-end 1)))))))
  7725. (setq task (buffer-string)))
  7726. (insert task))
  7727. (goto-char beg)))
  7728. ;;; Outline Sorting
  7729. (defun org-sort (with-case)
  7730. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7731. Optional argument WITH-CASE means sort case-sensitively."
  7732. (interactive "P")
  7733. (cond
  7734. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7735. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7736. (t
  7737. (org-call-with-arg 'org-sort-entries with-case))))
  7738. (defun org-sort-remove-invisible (s)
  7739. "Remove invisible links from string S."
  7740. (remove-text-properties 0 (length s) org-rm-props s)
  7741. (while (string-match org-bracket-link-regexp s)
  7742. (setq s (replace-match (if (match-end 2)
  7743. (match-string 3 s)
  7744. (match-string 1 s)) t t s)))
  7745. (let ((st (format " %s " s)))
  7746. (while (string-match org-emph-re st)
  7747. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7748. (setq s (substring st 1 -1)))
  7749. s)
  7750. (defvar org-priority-regexp) ; defined later in the file
  7751. (defvar org-after-sorting-entries-or-items-hook nil
  7752. "Hook that is run after a bunch of entries or items have been sorted.
  7753. When children are sorted, the cursor is in the parent line when this
  7754. hook gets called. When a region or a plain list is sorted, the cursor
  7755. will be in the first entry of the sorted region/list.")
  7756. (defun org-sort-entries
  7757. (&optional with-case sorting-type getkey-func compare-func property)
  7758. "Sort entries on a certain level of an outline tree.
  7759. If there is an active region, the entries in the region are sorted.
  7760. Else, if the cursor is before the first entry, sort the top-level items.
  7761. Else, the children of the entry at point are sorted.
  7762. Sorting can be alphabetically, numerically, by date/time as given by
  7763. a time stamp, by a property, by priority order, or by a custom function.
  7764. The command prompts for the sorting type unless it has been given to the
  7765. function through the SORTING-TYPE argument, which needs to be a character,
  7766. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7767. precise meaning of each character:
  7768. n Numerically, by converting the beginning of the entry/item to a number.
  7769. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7770. o By order of TODO keywords.
  7771. t By date/time, either the first active time stamp in the entry, or, if
  7772. none exist, by the first inactive one.
  7773. s By the scheduled date/time.
  7774. d By deadline date/time.
  7775. c By creation time, which is assumed to be the first inactive time stamp
  7776. at the beginning of a line.
  7777. p By priority according to the cookie.
  7778. r By the value of a property.
  7779. Capital letters will reverse the sort order.
  7780. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7781. called with point at the beginning of the record. It must return either
  7782. a string or a number that should serve as the sorting key for that record.
  7783. Comparing entries ignores case by default. However, with an optional argument
  7784. WITH-CASE, the sorting considers case as well.
  7785. Sorting is done against the visible part of the headlines, it ignores hidden
  7786. links.
  7787. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7788. (interactive "P")
  7789. (let ((case-func (if with-case 'identity 'downcase))
  7790. (cmstr
  7791. ;; The clock marker is lost when using `sort-subr', let's
  7792. ;; store the clocking string.
  7793. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7794. (save-excursion
  7795. (goto-char org-clock-marker)
  7796. (looking-back "^.*") (match-string-no-properties 0))))
  7797. start beg end stars re re2
  7798. txt what tmp)
  7799. ;; Find beginning and end of region to sort
  7800. (cond
  7801. ((org-region-active-p)
  7802. ;; we will sort the region
  7803. (setq end (region-end)
  7804. what "region")
  7805. (goto-char (region-beginning))
  7806. (if (not (org-at-heading-p)) (outline-next-heading))
  7807. (setq start (point)))
  7808. ((or (org-at-heading-p)
  7809. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7810. ;; we will sort the children of the current headline
  7811. (org-back-to-heading)
  7812. (setq start (point)
  7813. end (progn (org-end-of-subtree t t)
  7814. (or (bolp) (insert "\n"))
  7815. (org-back-over-empty-lines)
  7816. (point))
  7817. what "children")
  7818. (goto-char start)
  7819. (show-subtree)
  7820. (outline-next-heading))
  7821. (t
  7822. ;; we will sort the top-level entries in this file
  7823. (goto-char (point-min))
  7824. (or (org-at-heading-p) (outline-next-heading))
  7825. (setq start (point))
  7826. (goto-char (point-max))
  7827. (beginning-of-line 1)
  7828. (when (looking-at ".*?\\S-")
  7829. ;; File ends in a non-white line
  7830. (end-of-line 1)
  7831. (insert "\n"))
  7832. (setq end (point-max))
  7833. (setq what "top-level")
  7834. (goto-char start)
  7835. (show-all)))
  7836. (setq beg (point))
  7837. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7838. (looking-at "\\(\\*+\\)")
  7839. (setq stars (match-string 1)
  7840. re (concat "^" (regexp-quote stars) " +")
  7841. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7842. txt (buffer-substring beg end))
  7843. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7844. (if (and (not (equal stars "*")) (string-match re2 txt))
  7845. (user-error "Region to sort contains a level above the first entry"))
  7846. (unless sorting-type
  7847. (message
  7848. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7849. [t]ime [s]cheduled [d]eadline [c]reated
  7850. A/N/P/R/O/F/T/S/D/C means reversed:"
  7851. what)
  7852. (setq sorting-type (read-char-exclusive))
  7853. (unless getkey-func
  7854. (and (= (downcase sorting-type) ?f)
  7855. (setq getkey-func
  7856. (org-icompleting-read "Sort using function: "
  7857. obarray 'fboundp t nil nil))
  7858. (setq getkey-func (intern getkey-func))))
  7859. (and (= (downcase sorting-type) ?r)
  7860. (not property)
  7861. (setq property
  7862. (org-icompleting-read "Property: "
  7863. (mapcar 'list (org-buffer-property-keys t))
  7864. nil t))))
  7865. (message "Sorting entries...")
  7866. (save-restriction
  7867. (narrow-to-region start end)
  7868. (let ((dcst (downcase sorting-type))
  7869. (case-fold-search nil)
  7870. (now (current-time)))
  7871. (sort-subr
  7872. (/= dcst sorting-type)
  7873. ;; This function moves to the beginning character of the "record" to
  7874. ;; be sorted.
  7875. (lambda nil
  7876. (if (re-search-forward re nil t)
  7877. (goto-char (match-beginning 0))
  7878. (goto-char (point-max))))
  7879. ;; This function moves to the last character of the "record" being
  7880. ;; sorted.
  7881. (lambda nil
  7882. (save-match-data
  7883. (condition-case nil
  7884. (outline-forward-same-level 1)
  7885. (error
  7886. (goto-char (point-max))))))
  7887. ;; This function returns the value that gets sorted against.
  7888. (lambda nil
  7889. (cond
  7890. ((= dcst ?n)
  7891. (if (looking-at org-complex-heading-regexp)
  7892. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7893. nil))
  7894. ((= dcst ?a)
  7895. (if (looking-at org-complex-heading-regexp)
  7896. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7897. nil))
  7898. ((= dcst ?t)
  7899. (let ((end (save-excursion (outline-next-heading) (point))))
  7900. (if (or (re-search-forward org-ts-regexp end t)
  7901. (re-search-forward org-ts-regexp-both end t))
  7902. (org-time-string-to-seconds (match-string 0))
  7903. (org-float-time now))))
  7904. ((= dcst ?c)
  7905. (let ((end (save-excursion (outline-next-heading) (point))))
  7906. (if (re-search-forward
  7907. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7908. end t)
  7909. (org-time-string-to-seconds (match-string 0))
  7910. (org-float-time now))))
  7911. ((= dcst ?s)
  7912. (let ((end (save-excursion (outline-next-heading) (point))))
  7913. (if (re-search-forward org-scheduled-time-regexp end t)
  7914. (org-time-string-to-seconds (match-string 1))
  7915. (org-float-time now))))
  7916. ((= dcst ?d)
  7917. (let ((end (save-excursion (outline-next-heading) (point))))
  7918. (if (re-search-forward org-deadline-time-regexp end t)
  7919. (org-time-string-to-seconds (match-string 1))
  7920. (org-float-time now))))
  7921. ((= dcst ?p)
  7922. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7923. (string-to-char (match-string 2))
  7924. org-default-priority))
  7925. ((= dcst ?r)
  7926. (or (org-entry-get nil property) ""))
  7927. ((= dcst ?o)
  7928. (if (looking-at org-complex-heading-regexp)
  7929. (- 9999 (length (member (match-string 2)
  7930. org-todo-keywords-1)))))
  7931. ((= dcst ?f)
  7932. (if getkey-func
  7933. (progn
  7934. (setq tmp (funcall getkey-func))
  7935. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7936. tmp)
  7937. (error "Invalid key function `%s'" getkey-func)))
  7938. (t (error "Invalid sorting type `%c'" sorting-type))))
  7939. nil
  7940. (cond
  7941. ((= dcst ?a) 'string<)
  7942. ((= dcst ?f) compare-func)
  7943. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7944. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7945. ;; Reset the clock marker if needed
  7946. (when cmstr
  7947. (save-excursion
  7948. (goto-char start)
  7949. (search-forward cmstr nil t)
  7950. (move-marker org-clock-marker (point))))
  7951. (message "Sorting entries...done")))
  7952. (defun org-do-sort (table what &optional with-case sorting-type)
  7953. "Sort TABLE of WHAT according to SORTING-TYPE.
  7954. The user will be prompted for the SORTING-TYPE if the call to this
  7955. function does not specify it. WHAT is only for the prompt, to indicate
  7956. what is being sorted. The sorting key will be extracted from
  7957. the car of the elements of the table.
  7958. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7959. (unless sorting-type
  7960. (message
  7961. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7962. what)
  7963. (setq sorting-type (read-char-exclusive)))
  7964. (let ((dcst (downcase sorting-type))
  7965. extractfun comparefun)
  7966. ;; Define the appropriate functions
  7967. (cond
  7968. ((= dcst ?n)
  7969. (setq extractfun 'string-to-number
  7970. comparefun (if (= dcst sorting-type) '< '>)))
  7971. ((= dcst ?a)
  7972. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7973. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7974. comparefun (if (= dcst sorting-type)
  7975. 'string<
  7976. (lambda (a b) (and (not (string< a b))
  7977. (not (string= a b)))))))
  7978. ((= dcst ?t)
  7979. (setq extractfun
  7980. (lambda (x)
  7981. (if (or (string-match org-ts-regexp x)
  7982. (string-match org-ts-regexp-both x))
  7983. (org-float-time
  7984. (org-time-string-to-time (match-string 0 x)))
  7985. 0))
  7986. comparefun (if (= dcst sorting-type) '< '>)))
  7987. (t (error "Invalid sorting type `%c'" sorting-type)))
  7988. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7989. table)
  7990. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7991. ;;; The orgstruct minor mode
  7992. ;; Define a minor mode which can be used in other modes in order to
  7993. ;; integrate the org-mode structure editing commands.
  7994. ;; This is really a hack, because the org-mode structure commands use
  7995. ;; keys which normally belong to the major mode. Here is how it
  7996. ;; works: The minor mode defines all the keys necessary to operate the
  7997. ;; structure commands, but wraps the commands into a function which
  7998. ;; tests if the cursor is currently at a headline or a plain list
  7999. ;; item. If that is the case, the structure command is used,
  8000. ;; temporarily setting many Org-mode variables like regular
  8001. ;; expressions for filling etc. However, when any of those keys is
  8002. ;; used at a different location, function uses `key-binding' to look
  8003. ;; up if the key has an associated command in another currently active
  8004. ;; keymap (minor modes, major mode, global), and executes that
  8005. ;; command. There might be problems if any of the keys is otherwise
  8006. ;; used as a prefix key.
  8007. (defcustom orgstruct-heading-prefix-regexp ""
  8008. "Regexp that matches the custom prefix of Org headlines in
  8009. orgstruct(++)-mode."
  8010. :group 'org
  8011. :version "24.4"
  8012. :package-version '(Org . "8.3")
  8013. :type 'regexp)
  8014. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8015. (defcustom orgstruct-setup-hook nil
  8016. "Hook run after orgstruct-mode-map is filled."
  8017. :group 'org
  8018. :version "24.4"
  8019. :package-version '(Org . "8.0")
  8020. :type 'hook)
  8021. (defvar orgstruct-initialized nil)
  8022. (defvar org-local-vars nil
  8023. "List of local variables, for use by `orgstruct-mode'.")
  8024. ;;;###autoload
  8025. (define-minor-mode orgstruct-mode
  8026. "Toggle the minor mode `orgstruct-mode'.
  8027. This mode is for using Org-mode structure commands in other
  8028. modes. The following keys behave as if Org-mode were active, if
  8029. the cursor is on a headline, or on a plain list item (both as
  8030. defined by Org-mode)."
  8031. nil " OrgStruct" (make-sparse-keymap)
  8032. (funcall (if orgstruct-mode
  8033. 'add-to-invisibility-spec
  8034. 'remove-from-invisibility-spec)
  8035. '(outline . t))
  8036. (when orgstruct-mode
  8037. (org-load-modules-maybe)
  8038. (unless orgstruct-initialized
  8039. (orgstruct-setup)
  8040. (setq orgstruct-initialized t))))
  8041. ;;;###autoload
  8042. (defun turn-on-orgstruct ()
  8043. "Unconditionally turn on `orgstruct-mode'."
  8044. (orgstruct-mode 1))
  8045. (defvar org-fb-vars nil)
  8046. (make-variable-buffer-local 'org-fb-vars)
  8047. (defun orgstruct++-mode (&optional arg)
  8048. "Toggle `orgstruct-mode', the enhanced version of it.
  8049. In addition to setting orgstruct-mode, this also exports all
  8050. indentation and autofilling variables from org-mode into the
  8051. buffer. It will also recognize item context in multiline items."
  8052. (interactive "P")
  8053. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8054. (if (< arg 1)
  8055. (progn (orgstruct-mode -1)
  8056. (mapc (lambda(v)
  8057. (org-set-local (car v)
  8058. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8059. org-fb-vars))
  8060. (orgstruct-mode 1)
  8061. (setq org-fb-vars nil)
  8062. (unless org-local-vars
  8063. (setq org-local-vars (org-get-local-variables)))
  8064. (let (var val)
  8065. (mapc
  8066. (lambda (x)
  8067. (when (string-match
  8068. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8069. (symbol-name (car x)))
  8070. (setq var (car x) val (nth 1 x))
  8071. (push (list var `(quote ,(eval var))) org-fb-vars)
  8072. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8073. org-local-vars)
  8074. (org-set-local 'orgstruct-is-++ t))))
  8075. (defvar orgstruct-is-++ nil
  8076. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8077. (make-variable-buffer-local 'orgstruct-is-++)
  8078. ;;;###autoload
  8079. (defun turn-on-orgstruct++ ()
  8080. "Unconditionally turn on `orgstruct++-mode'."
  8081. (orgstruct++-mode 1))
  8082. (defun orgstruct-error ()
  8083. "Error when there is no default binding for a structure key."
  8084. (interactive)
  8085. (funcall (if (fboundp 'user-error)
  8086. 'user-error
  8087. 'error)
  8088. "This key has no function outside structure elements"))
  8089. (defun orgstruct-setup ()
  8090. "Setup orgstruct keymap."
  8091. (dolist (cell '((org-demote . t)
  8092. (org-metaleft . t)
  8093. (org-metaright . t)
  8094. (org-promote . t)
  8095. (org-shiftmetaleft . t)
  8096. (org-shiftmetaright . t)
  8097. org-backward-element
  8098. org-backward-heading-same-level
  8099. org-ctrl-c-ret
  8100. org-ctrl-c-minus
  8101. org-ctrl-c-star
  8102. org-cycle
  8103. org-forward-heading-same-level
  8104. org-insert-heading
  8105. org-insert-heading-respect-content
  8106. org-kill-note-or-show-branches
  8107. org-mark-subtree
  8108. org-meta-return
  8109. org-metadown
  8110. org-metaup
  8111. org-narrow-to-subtree
  8112. org-promote-subtree
  8113. org-reveal
  8114. org-shiftdown
  8115. org-shiftleft
  8116. org-shiftmetadown
  8117. org-shiftmetaup
  8118. org-shiftright
  8119. org-shifttab
  8120. org-shifttab
  8121. org-shiftup
  8122. org-show-subtree
  8123. org-sort
  8124. org-up-element
  8125. outline-demote
  8126. outline-next-visible-heading
  8127. outline-previous-visible-heading
  8128. outline-promote
  8129. outline-up-heading
  8130. show-children))
  8131. (let ((f (or (car-safe cell) cell))
  8132. (disable-when-heading-prefix (cdr-safe cell)))
  8133. (when (fboundp f)
  8134. (let ((new-bindings))
  8135. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8136. (where-is-internal f outline-mode-map)))
  8137. (push binding new-bindings)
  8138. ;; TODO use local-function-key-map
  8139. (dolist (rep '(("<tab>" . "TAB")
  8140. ("<return>" . "RET")
  8141. ("<escape>" . "ESC")
  8142. ("<delete>" . "DEL")))
  8143. (setq binding (read-kbd-macro
  8144. (let ((case-fold-search))
  8145. (replace-regexp-in-string
  8146. (regexp-quote (cdr rep))
  8147. (car rep)
  8148. (key-description binding)))))
  8149. (pushnew binding new-bindings :test 'equal)))
  8150. (dolist (binding new-bindings)
  8151. (let ((key (lookup-key orgstruct-mode-map binding)))
  8152. (when (or (not key) (numberp key))
  8153. (condition-case nil
  8154. (org-defkey orgstruct-mode-map
  8155. binding
  8156. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8157. (error nil)))))))))
  8158. (run-hooks 'orgstruct-setup-hook))
  8159. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8160. "Create a function for binding in the structure minor mode.
  8161. FUN is the command to call inside a table. KEY is the key that
  8162. should be checked in for a command to execute outside of tables.
  8163. Non-nil `disable-when-heading-prefix' means to disable the command
  8164. if `orgstruct-heading-prefix-regexp' is not empty."
  8165. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8166. (let ((nname name)
  8167. (i 0))
  8168. (while (fboundp (intern nname))
  8169. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8170. (setq name (intern nname)))
  8171. (eval
  8172. (let ((bindings '((org-heading-regexp
  8173. (concat "^"
  8174. orgstruct-heading-prefix-regexp
  8175. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8176. (org-outline-regexp
  8177. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8178. (org-outline-regexp-bol
  8179. (concat "^" org-outline-regexp))
  8180. (outline-regexp org-outline-regexp)
  8181. (outline-heading-end-regexp "\n")
  8182. (outline-level 'org-outline-level)
  8183. (outline-heading-alist))))
  8184. `(defun ,name (arg)
  8185. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8186. "Outside of structure, run the binding of `"
  8187. (key-description key) "'."
  8188. (when disable-when-heading-prefix
  8189. (concat
  8190. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8191. "back to the default binding due to limitations of Org's implementation of\n"
  8192. "`" (symbol-name fun) "'.")))
  8193. (interactive "p")
  8194. (let* ((disable
  8195. ,(and disable-when-heading-prefix
  8196. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8197. (fallback
  8198. (or disable
  8199. (not
  8200. (let* ,bindings
  8201. (org-context-p 'headline 'item
  8202. ,(when (memq fun
  8203. '(org-insert-heading
  8204. org-insert-heading-respect-content
  8205. org-meta-return))
  8206. '(when orgstruct-is-++
  8207. 'item-body))))))))
  8208. (if fallback
  8209. (let* ((orgstruct-mode)
  8210. (binding
  8211. (loop with key = ,key
  8212. for rep in
  8213. '(nil
  8214. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8215. ("<\\([^>]*\\)return>" . "\\1RET")
  8216. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8217. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8218. do
  8219. (when rep
  8220. (setq key (read-kbd-macro
  8221. (let ((case-fold-search))
  8222. (replace-regexp-in-string
  8223. (car rep)
  8224. (cdr rep)
  8225. (key-description key))))))
  8226. thereis (key-binding key))))
  8227. (if (keymapp binding)
  8228. (org-set-transient-map binding)
  8229. (let ((func (or binding
  8230. (unless disable
  8231. 'orgstruct-error))))
  8232. (when func
  8233. (call-interactively func)))))
  8234. (org-run-like-in-org-mode
  8235. (lambda ()
  8236. (interactive)
  8237. (let* ,bindings
  8238. (call-interactively ',fun)))))))))
  8239. name))
  8240. (defun org-contextualize-keys (alist contexts)
  8241. "Return valid elements in ALIST depending on CONTEXTS.
  8242. `org-agenda-custom-commands' or `org-capture-templates' are the
  8243. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8244. or `org-capture-templates-contexts' are the associated contexts
  8245. definitions."
  8246. (let ((contexts
  8247. ;; normalize contexts
  8248. (mapcar
  8249. (lambda(c) (cond ((listp (cadr c))
  8250. (list (car c) (car c) (cadr c)))
  8251. ((string= "" (cadr c))
  8252. (list (car c) (car c) (caddr c)))
  8253. (t c))) contexts))
  8254. (a alist) c r s)
  8255. ;; loop over all commands or templates
  8256. (while (setq c (pop a))
  8257. (let (vrules repl)
  8258. (cond
  8259. ((not (assoc (car c) contexts))
  8260. (push c r))
  8261. ((and (assoc (car c) contexts)
  8262. (setq vrules (org-contextualize-validate-key
  8263. (car c) contexts)))
  8264. (mapc (lambda (vr)
  8265. (when (not (equal (car vr) (cadr vr)))
  8266. (setq repl vr))) vrules)
  8267. (if (not repl) (push c r)
  8268. (push (cadr repl) s)
  8269. (push
  8270. (cons (car c)
  8271. (cdr (or (assoc (cadr repl) alist)
  8272. (error "Undefined key `%s' as contextual replacement for `%s'"
  8273. (cadr repl) (car c)))))
  8274. r))))))
  8275. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8276. (delq
  8277. nil
  8278. (delete-dups
  8279. (mapcar (lambda (x)
  8280. (let ((tpl (car x)))
  8281. (when (not (delq
  8282. nil
  8283. (mapcar (lambda(y)
  8284. (equal y tpl)) s))) x)))
  8285. (reverse r))))))
  8286. (defun org-contextualize-validate-key (key contexts)
  8287. "Check CONTEXTS for agenda or capture KEY."
  8288. (let (r rr res)
  8289. (while (setq r (pop contexts))
  8290. (mapc
  8291. (lambda (rr)
  8292. (when
  8293. (and (equal key (car r))
  8294. (if (functionp rr) (funcall rr)
  8295. (or (and (eq (car rr) 'in-file)
  8296. (buffer-file-name)
  8297. (string-match (cdr rr) (buffer-file-name)))
  8298. (and (eq (car rr) 'in-mode)
  8299. (string-match (cdr rr) (symbol-name major-mode)))
  8300. (and (eq (car rr) 'in-buffer)
  8301. (string-match (cdr rr) (buffer-name)))
  8302. (when (and (eq (car rr) 'not-in-file)
  8303. (buffer-file-name))
  8304. (not (string-match (cdr rr) (buffer-file-name))))
  8305. (when (eq (car rr) 'not-in-mode)
  8306. (not (string-match (cdr rr) (symbol-name major-mode))))
  8307. (when (eq (car rr) 'not-in-buffer)
  8308. (not (string-match (cdr rr) (buffer-name)))))))
  8309. (push r res)))
  8310. (car (last r))))
  8311. (delete-dups (delq nil res))))
  8312. (defun org-context-p (&rest contexts)
  8313. "Check if local context is any of CONTEXTS.
  8314. Possible values in the list of contexts are `table', `headline', and `item'."
  8315. (let ((pos (point)))
  8316. (goto-char (point-at-bol))
  8317. (prog1 (or (and (memq 'table contexts)
  8318. (looking-at "[ \t]*|"))
  8319. (and (memq 'headline contexts)
  8320. (looking-at org-outline-regexp))
  8321. (and (memq 'item contexts)
  8322. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8323. (and (memq 'item-body contexts)
  8324. (org-in-item-p)))
  8325. (goto-char pos))))
  8326. (defun org-get-local-variables ()
  8327. "Return a list of all local variables in an Org mode buffer."
  8328. (let (varlist)
  8329. (with-current-buffer (get-buffer-create "*Org tmp*")
  8330. (erase-buffer)
  8331. (org-mode)
  8332. (setq varlist (buffer-local-variables)))
  8333. (kill-buffer "*Org tmp*")
  8334. (delq nil
  8335. (mapcar
  8336. (lambda (x)
  8337. (setq x
  8338. (if (symbolp x)
  8339. (list x)
  8340. (list (car x) (cdr x))))
  8341. (if (and (not (get (car x) 'org-state))
  8342. (string-match
  8343. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8344. (symbol-name (car x))))
  8345. x nil))
  8346. varlist))))
  8347. (defun org-clone-local-variables (from-buffer &optional regexp)
  8348. "Clone local variables from FROM-BUFFER.
  8349. Optional argument REGEXP selects variables to clone."
  8350. (mapc
  8351. (lambda (pair)
  8352. (and (symbolp (car pair))
  8353. (or (null regexp)
  8354. (string-match regexp (symbol-name (car pair))))
  8355. (set (make-local-variable (car pair))
  8356. (cdr pair))))
  8357. (buffer-local-variables from-buffer)))
  8358. ;;;###autoload
  8359. (defun org-run-like-in-org-mode (cmd)
  8360. "Run a command, pretending that the current buffer is in Org-mode.
  8361. This will temporarily bind local variables that are typically bound in
  8362. Org-mode to the values they have in Org-mode, and then interactively
  8363. call CMD."
  8364. (org-load-modules-maybe)
  8365. (unless org-local-vars
  8366. (setq org-local-vars (org-get-local-variables)))
  8367. (let (binds)
  8368. (dolist (var org-local-vars)
  8369. (when (or (not (boundp (car var)))
  8370. (eq (symbol-value (car var))
  8371. (default-value (car var))))
  8372. (push (list (car var) `(quote ,(cadr var))) binds)))
  8373. (eval `(let ,binds
  8374. (call-interactively (quote ,cmd))))))
  8375. ;;;; Archiving
  8376. (defun org-get-category (&optional pos force-refresh)
  8377. "Get the category applying to position POS."
  8378. (save-match-data
  8379. (if force-refresh (org-refresh-category-properties))
  8380. (let ((pos (or pos (point))))
  8381. (or (get-text-property pos 'org-category)
  8382. (progn (org-refresh-category-properties)
  8383. (get-text-property pos 'org-category))))))
  8384. (defun org-refresh-category-properties ()
  8385. "Refresh category text properties in the buffer."
  8386. (let ((case-fold-search t)
  8387. (inhibit-read-only t)
  8388. (def-cat (cond
  8389. ((null org-category)
  8390. (if buffer-file-name
  8391. (file-name-sans-extension
  8392. (file-name-nondirectory buffer-file-name))
  8393. "???"))
  8394. ((symbolp org-category) (symbol-name org-category))
  8395. (t org-category)))
  8396. beg end cat pos optionp)
  8397. (org-with-silent-modifications
  8398. (save-excursion
  8399. (save-restriction
  8400. (widen)
  8401. (goto-char (point-min))
  8402. (put-text-property (point) (point-max) 'org-category def-cat)
  8403. (while (re-search-forward
  8404. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8405. (setq pos (match-end 0)
  8406. optionp (equal (char-after (match-beginning 0)) ?#)
  8407. cat (org-trim (match-string 2)))
  8408. (if optionp
  8409. (setq beg (point-at-bol) end (point-max))
  8410. (org-back-to-heading t)
  8411. (setq beg (point) end (org-end-of-subtree t t)))
  8412. (put-text-property beg end 'org-category cat)
  8413. (put-text-property beg end 'org-category-position beg)
  8414. (goto-char pos)))))))
  8415. (defun org-refresh-properties (dprop tprop)
  8416. "Refresh buffer text properties.
  8417. DPROP is the drawer property and TPROP is the corresponding text
  8418. property to set."
  8419. (let ((case-fold-search t)
  8420. (inhibit-read-only t) p)
  8421. (org-with-silent-modifications
  8422. (save-excursion
  8423. (save-restriction
  8424. (widen)
  8425. (goto-char (point-min))
  8426. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8427. (setq p (org-match-string-no-properties 1))
  8428. (save-excursion
  8429. (org-back-to-heading t)
  8430. (put-text-property
  8431. (point-at-bol) (org-end-of-subtree t t) tprop p))))))))
  8432. ;;;; Link Stuff
  8433. ;;; Link abbreviations
  8434. (defun org-link-expand-abbrev (link)
  8435. "Apply replacements as defined in `org-link-abbrev-alist'."
  8436. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8437. (let* ((key (match-string 1 link))
  8438. (as (or (assoc key org-link-abbrev-alist-local)
  8439. (assoc key org-link-abbrev-alist)))
  8440. (tag (and (match-end 2) (match-string 3 link)))
  8441. rpl)
  8442. (if (not as)
  8443. link
  8444. (setq rpl (cdr as))
  8445. (cond
  8446. ((symbolp rpl) (funcall rpl tag))
  8447. ((string-match "%(\\([^)]+\\))" rpl)
  8448. (replace-match
  8449. (save-match-data
  8450. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8451. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8452. ((string-match "%h" rpl)
  8453. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8454. (t (concat rpl tag)))))
  8455. link))
  8456. ;;; Storing and inserting links
  8457. (defvar org-insert-link-history nil
  8458. "Minibuffer history for links inserted with `org-insert-link'.")
  8459. (defvar org-stored-links nil
  8460. "Contains the links stored with `org-store-link'.")
  8461. (defvar org-store-link-plist nil
  8462. "Plist with info about the most recently link created with `org-store-link'.")
  8463. (defvar org-link-protocols nil
  8464. "Link protocols added to Org-mode using `org-add-link-type'.")
  8465. (defvar org-store-link-functions nil
  8466. "List of functions that are called to create and store a link.
  8467. Each function will be called in turn until one returns a non-nil
  8468. value. Each function should check if it is responsible for creating
  8469. this link (for example by looking at the major mode).
  8470. If not, it must exit and return nil.
  8471. If yes, it should return a non-nil value after a calling
  8472. `org-store-link-props' with a list of properties and values.
  8473. Special properties are:
  8474. :type The link prefix, like \"http\". This must be given.
  8475. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8476. This is obligatory as well.
  8477. :description Optional default description for the second pair
  8478. of brackets in an Org-mode link. The user can still change
  8479. this when inserting this link into an Org-mode buffer.
  8480. In addition to these, any additional properties can be specified
  8481. and then used in capture templates.")
  8482. (defun org-add-link-type (type &optional follow export)
  8483. "Add TYPE to the list of `org-link-types'.
  8484. Re-compute all regular expressions depending on `org-link-types'
  8485. FOLLOW and EXPORT are two functions.
  8486. FOLLOW should take the link path as the single argument and do whatever
  8487. is necessary to follow the link, for example find a file or display
  8488. a mail message.
  8489. EXPORT should format the link path for export to one of the export formats.
  8490. It should be a function accepting three arguments:
  8491. path the path of the link, the text after the prefix (like \"http:\")
  8492. desc the description of the link, if any, or a description added by
  8493. org-export-normalize-links if there is none
  8494. format the export format, a symbol like `html' or `latex' or `ascii'..
  8495. The function may use the FORMAT information to return different values
  8496. depending on the format. The return value will be put literally into
  8497. the exported file. If the return value is nil, this means Org should
  8498. do what it normally does with links which do not have EXPORT defined.
  8499. Org-mode has a built-in default for exporting links. If you are happy with
  8500. this default, there is no need to define an export function for the link
  8501. type. For a simple example of an export function, see `org-bbdb.el'."
  8502. (add-to-list 'org-link-types type t)
  8503. (org-make-link-regexps)
  8504. (if (assoc type org-link-protocols)
  8505. (setcdr (assoc type org-link-protocols) (list follow export))
  8506. (push (list type follow export) org-link-protocols)))
  8507. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8508. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8509. ;;;###autoload
  8510. (defun org-store-link (arg)
  8511. "\\<org-mode-map>Store an org-link to the current location.
  8512. This link is added to `org-stored-links' and can later be inserted
  8513. into an org-buffer with \\[org-insert-link].
  8514. For some link types, a prefix arg is interpreted.
  8515. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8516. For file links, arg negates `org-context-in-file-links'.
  8517. A double prefix arg force skipping storing functions that are not
  8518. part of Org's core.
  8519. A triple prefix arg force storing a link for each line in the
  8520. active region."
  8521. (interactive "P")
  8522. (org-load-modules-maybe)
  8523. (if (and (equal arg '(64)) (org-region-active-p))
  8524. (save-excursion
  8525. (let ((end (region-end)))
  8526. (goto-char (region-beginning))
  8527. (set-mark (point))
  8528. (while (< (point-at-eol) end)
  8529. (move-end-of-line 1) (activate-mark)
  8530. (let (current-prefix-arg)
  8531. (call-interactively 'org-store-link))
  8532. (move-beginning-of-line 2)
  8533. (set-mark (point)))))
  8534. (org-with-limited-levels
  8535. (setq org-store-link-plist nil)
  8536. (let (link cpltxt desc description search
  8537. txt custom-id agenda-link sfuns sfunsn)
  8538. (cond
  8539. ;; Store a link using an external link type
  8540. ((and (not (equal arg '(16)))
  8541. (setq sfuns
  8542. (delq
  8543. nil (mapcar (lambda (f)
  8544. (let (fs) (if (funcall f) (push f fs))))
  8545. org-store-link-functions))
  8546. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8547. (or (and (cdr sfuns)
  8548. (funcall (intern
  8549. (completing-read
  8550. "Which function for creating the link? "
  8551. sfunsn nil t (car sfunsn)))))
  8552. (funcall (caar sfuns)))
  8553. (setq link (plist-get org-store-link-plist :link)
  8554. desc (or (plist-get org-store-link-plist
  8555. :description) link))))
  8556. ;; Store a link from a source code buffer
  8557. ((org-src-edit-buffer-p)
  8558. (let (label gc)
  8559. (while (or (not label)
  8560. (save-excursion
  8561. (save-restriction
  8562. (widen)
  8563. (goto-char (point-min))
  8564. (re-search-forward
  8565. (regexp-quote (format org-coderef-label-format label))
  8566. nil t))))
  8567. (when label (message "Label exists already") (sit-for 2))
  8568. (setq label (read-string "Code line label: " label)))
  8569. (end-of-line 1)
  8570. (setq link (format org-coderef-label-format label))
  8571. (setq gc (- 79 (length link)))
  8572. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8573. (insert link)
  8574. (setq link (concat "(" label ")") desc nil)))
  8575. ;; We are in the agenda, link to referenced location
  8576. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8577. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8578. (get-text-property (point) 'org-marker))))
  8579. (when m
  8580. (org-with-point-at m
  8581. (setq agenda-link
  8582. (if (org-called-interactively-p 'any)
  8583. (call-interactively 'org-store-link)
  8584. (org-store-link nil)))))))
  8585. ((eq major-mode 'calendar-mode)
  8586. (let ((cd (calendar-cursor-to-date)))
  8587. (setq link
  8588. (format-time-string
  8589. (car org-time-stamp-formats)
  8590. (apply 'encode-time
  8591. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8592. nil nil nil))))
  8593. (org-store-link-props :type "calendar" :date cd)))
  8594. ((eq major-mode 'help-mode)
  8595. (setq link (concat "help:" (save-excursion
  8596. (goto-char (point-min))
  8597. (looking-at "^[^ ]+")
  8598. (match-string 0))))
  8599. (org-store-link-props :type "help"))
  8600. ((eq major-mode 'w3-mode)
  8601. (setq cpltxt (if (and (buffer-name)
  8602. (not (string-match "Untitled" (buffer-name))))
  8603. (buffer-name)
  8604. (url-view-url t))
  8605. link (url-view-url t))
  8606. (org-store-link-props :type "w3" :url (url-view-url t)))
  8607. ((eq major-mode 'image-mode)
  8608. (setq cpltxt (concat "file:"
  8609. (abbreviate-file-name buffer-file-name))
  8610. link cpltxt)
  8611. (org-store-link-props :type "image" :file buffer-file-name))
  8612. ;; In dired, store a link to the file of the current line
  8613. ((eq major-mode 'dired-mode)
  8614. (let ((file (dired-get-filename nil t)))
  8615. (setq file (if file
  8616. (abbreviate-file-name
  8617. (expand-file-name (dired-get-filename nil t)))
  8618. ;; otherwise, no file so use current directory.
  8619. default-directory))
  8620. (setq cpltxt (concat "file:" file)
  8621. link cpltxt)))
  8622. ((setq search (run-hook-with-args-until-success
  8623. 'org-create-file-search-functions))
  8624. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8625. "::" search))
  8626. (setq cpltxt (or description link)))
  8627. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8628. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8629. (cond
  8630. ;; Store a link using the target at point
  8631. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8632. (setq cpltxt
  8633. (concat "file:"
  8634. (abbreviate-file-name
  8635. (buffer-file-name (buffer-base-buffer)))
  8636. "::" (match-string 1))
  8637. link cpltxt))
  8638. ((and (featurep 'org-id)
  8639. (or (eq org-id-link-to-org-use-id t)
  8640. (and (org-called-interactively-p 'any)
  8641. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8642. (and (eq org-id-link-to-org-use-id
  8643. 'create-if-interactive-and-no-custom-id)
  8644. (not custom-id))))
  8645. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8646. ;; Store a link using the ID at point
  8647. (setq link (condition-case nil
  8648. (prog1 (org-id-store-link)
  8649. (setq desc (plist-get org-store-link-plist
  8650. :description)))
  8651. (error
  8652. ;; Probably before first headline, link only to file
  8653. (concat "file:"
  8654. (abbreviate-file-name
  8655. (buffer-file-name (buffer-base-buffer))))))))
  8656. (t
  8657. ;; Just link to current headline
  8658. (setq cpltxt (concat "file:"
  8659. (abbreviate-file-name
  8660. (buffer-file-name (buffer-base-buffer)))))
  8661. ;; Add a context search string
  8662. (when (org-xor org-context-in-file-links arg)
  8663. (let* ((ee (org-element-at-point))
  8664. (et (org-element-type ee))
  8665. (ev (plist-get (cadr ee) :value))
  8666. (ek (plist-get (cadr ee) :key))
  8667. (eok (and (stringp ek) (string-match "name" ek))))
  8668. (setq txt (cond
  8669. ((org-at-heading-p) nil)
  8670. ((and (eq et 'keyword) eok) ev)
  8671. ((org-region-active-p)
  8672. (buffer-substring (region-beginning) (region-end)))))
  8673. (when (or (null txt) (string-match "\\S-" txt))
  8674. (setq cpltxt
  8675. (concat cpltxt "::"
  8676. (condition-case nil
  8677. (org-make-org-heading-search-string txt)
  8678. (error "")))
  8679. desc (or (and (eq et 'keyword) eok ev)
  8680. (nth 4 (ignore-errors (org-heading-components)))
  8681. "NONE")))))
  8682. (if (string-match "::\\'" cpltxt)
  8683. (setq cpltxt (substring cpltxt 0 -2)))
  8684. (setq link cpltxt))))
  8685. ((buffer-file-name (buffer-base-buffer))
  8686. ;; Just link to this file here.
  8687. (setq cpltxt (concat "file:"
  8688. (abbreviate-file-name
  8689. (buffer-file-name (buffer-base-buffer)))))
  8690. ;; Add a context string.
  8691. (when (org-xor org-context-in-file-links arg)
  8692. (setq txt (if (org-region-active-p)
  8693. (buffer-substring (region-beginning) (region-end))
  8694. (buffer-substring (point-at-bol) (point-at-eol))))
  8695. ;; Only use search option if there is some text.
  8696. (when (string-match "\\S-" txt)
  8697. (setq cpltxt
  8698. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8699. desc "NONE")))
  8700. (setq link cpltxt))
  8701. ((org-called-interactively-p 'interactive)
  8702. (user-error "No method for storing a link from this buffer"))
  8703. (t (setq link nil)))
  8704. ;; We're done setting link and desc, clean up
  8705. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8706. (setq link (or link cpltxt)
  8707. desc (or desc cpltxt))
  8708. (cond ((equal desc "NONE") (setq desc nil))
  8709. ((string-match org-bracket-link-analytic-regexp desc)
  8710. (let ((d0 (match-string 3 desc))
  8711. (p0 (match-string 5 desc)))
  8712. (setq desc
  8713. (replace-regexp-in-string
  8714. org-bracket-link-regexp
  8715. (concat (or p0 d0)
  8716. (if (equal (length (match-string 0 desc))
  8717. (length desc)) "*" "")) desc)))))
  8718. ;; Return the link
  8719. (if (not (and (or (org-called-interactively-p 'any)
  8720. executing-kbd-macro) link))
  8721. (or agenda-link (and link (org-make-link-string link desc)))
  8722. (push (list link desc) org-stored-links)
  8723. (message "Stored: %s" (or desc link))
  8724. (when custom-id
  8725. (setq link (concat "file:" (abbreviate-file-name
  8726. (buffer-file-name)) "::#" custom-id))
  8727. (push (list link desc) org-stored-links))
  8728. (car org-stored-links))))))
  8729. (defun org-store-link-props (&rest plist)
  8730. "Store link properties, extract names and addresses."
  8731. (let (x adr)
  8732. (when (setq x (plist-get plist :from))
  8733. (setq adr (mail-extract-address-components x))
  8734. (setq plist (plist-put plist :fromname (car adr)))
  8735. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8736. (when (setq x (plist-get plist :to))
  8737. (setq adr (mail-extract-address-components x))
  8738. (setq plist (plist-put plist :toname (car adr)))
  8739. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8740. (let ((from (plist-get plist :from))
  8741. (to (plist-get plist :to)))
  8742. (when (and from to org-from-is-user-regexp)
  8743. (setq plist
  8744. (plist-put plist :fromto
  8745. (if (string-match org-from-is-user-regexp from)
  8746. (concat "to %t")
  8747. (concat "from %f"))))))
  8748. (setq org-store-link-plist plist))
  8749. (defun org-add-link-props (&rest plist)
  8750. "Add these properties to the link property list."
  8751. (let (key value)
  8752. (while plist
  8753. (setq key (pop plist) value (pop plist))
  8754. (setq org-store-link-plist
  8755. (plist-put org-store-link-plist key value)))))
  8756. (defun org-email-link-description (&optional fmt)
  8757. "Return the description part of an email link.
  8758. This takes information from `org-store-link-plist' and formats it
  8759. according to FMT (default from `org-email-link-description-format')."
  8760. (setq fmt (or fmt org-email-link-description-format))
  8761. (let* ((p org-store-link-plist)
  8762. (to (plist-get p :toaddress))
  8763. (from (plist-get p :fromaddress))
  8764. (table
  8765. (list
  8766. (cons "%c" (plist-get p :fromto))
  8767. (cons "%F" (plist-get p :from))
  8768. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8769. (cons "%T" (plist-get p :to))
  8770. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8771. (cons "%s" (plist-get p :subject))
  8772. (cons "%d" (plist-get p :date))
  8773. (cons "%m" (plist-get p :message-id)))))
  8774. (when (string-match "%c" fmt)
  8775. ;; Check if the user wrote this message
  8776. (if (and org-from-is-user-regexp from to
  8777. (save-match-data (string-match org-from-is-user-regexp from)))
  8778. (setq fmt (replace-match "to %t" t t fmt))
  8779. (setq fmt (replace-match "from %f" t t fmt))))
  8780. (org-replace-escapes fmt table)))
  8781. (defun org-make-org-heading-search-string (&optional string)
  8782. "Make search string for the current headline or STRING."
  8783. (let ((s (or string
  8784. (and (derived-mode-p 'org-mode)
  8785. (save-excursion
  8786. (org-back-to-heading t)
  8787. (org-element-property :raw-value (org-element-at-point))))))
  8788. (lines org-context-in-file-links))
  8789. (or string (setq s (concat "*" s))) ; Add * for headlines
  8790. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8791. (when (and string (integerp lines) (> lines 0))
  8792. (let ((slines (org-split-string s "\n")))
  8793. (when (< lines (length slines))
  8794. (setq s (mapconcat
  8795. 'identity
  8796. (reverse (nthcdr (- (length slines) lines)
  8797. (reverse slines))) "\n")))))
  8798. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8799. (defun org-make-link-string (link &optional description)
  8800. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8801. (unless (string-match "\\S-" link)
  8802. (error "Empty link"))
  8803. (when (and description
  8804. (stringp description)
  8805. (not (string-match "\\S-" description)))
  8806. (setq description nil))
  8807. (when (stringp description)
  8808. ;; Remove brackets from the description, they are fatal.
  8809. (while (string-match "\\[" description)
  8810. (setq description (replace-match "{" t t description)))
  8811. (while (string-match "\\]" description)
  8812. (setq description (replace-match "}" t t description))))
  8813. (when (equal link description)
  8814. ;; No description needed, it is identical
  8815. (setq description nil))
  8816. (when (and (not description)
  8817. (not (string-match (org-image-file-name-regexp) link))
  8818. (not (equal link (org-link-escape link))))
  8819. (setq description (org-extract-attributes link)))
  8820. (setq link
  8821. (cond ((string-match (org-image-file-name-regexp) link) link)
  8822. ((string-match org-link-types-re link)
  8823. (concat (match-string 1 link)
  8824. (org-link-escape (substring link (match-end 1)))))
  8825. (t (org-link-escape link))))
  8826. (concat "[[" link "]"
  8827. (if description (concat "[" description "]") "")
  8828. "]"))
  8829. (defconst org-link-escape-chars
  8830. ;;%20 %2B %3B %3D %5B %5D
  8831. '(?\ ?\+ ?\; ?\= ?\[ ?\])
  8832. "List of characters that should be escaped in a link when stored to Org.
  8833. This is the list that is used for internal purposes.")
  8834. (defconst org-link-escape-chars-browser
  8835. ;;%20 %22
  8836. '(?\ ?\")
  8837. "List of characters to be escaped before handing over to the browser.
  8838. If you consider using this constant then you probably want to use
  8839. the function `org-link-escape-browser' instead. See there why
  8840. this constant is a candidate to be removed once Org drops support
  8841. for Emacs 24.1 and 24.2.")
  8842. (defun org-link-escape (text &optional table merge)
  8843. "Return percent escaped representation of TEXT.
  8844. TEXT is a string with the text to escape.
  8845. Optional argument TABLE is a list with characters that should be
  8846. escaped. When nil, `org-link-escape-chars' is used.
  8847. If optional argument MERGE is set, merge TABLE into
  8848. `org-link-escape-chars'."
  8849. (cond
  8850. ((and table merge)
  8851. (mapc (lambda (defchr)
  8852. (unless (member defchr table)
  8853. (setq table (cons defchr table)))) org-link-escape-chars))
  8854. ((null table)
  8855. (setq table org-link-escape-chars)))
  8856. (mapconcat
  8857. (lambda (char)
  8858. (if (or (member char table)
  8859. (and (or (< char 32) (= char ?\%) (> char 126))
  8860. org-url-hexify-p))
  8861. (mapconcat (lambda (sequence-element)
  8862. (format "%%%.2X" sequence-element))
  8863. (or (encode-coding-char char 'utf-8)
  8864. (error "Unable to percent escape character: %s"
  8865. (char-to-string char))) "")
  8866. (char-to-string char))) text ""))
  8867. (defun org-link-escape-browser (text)
  8868. "Escape some characters before handing over to the browser.
  8869. This function is a candidate to be removed together with the
  8870. constant `org-link-escape-chars-browser' once Org drops support
  8871. for Emacs 24.1 and 24.2. All calls to this function will have to
  8872. be replaced with `url-encode-url' which is available since Emacs
  8873. 24.3.1."
  8874. ;; Example with the Org link
  8875. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  8876. ;; to open the browser with +subject:"Release 8.2" filled into the
  8877. ;; query field: In this case the variable TEXT contains the
  8878. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  8879. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  8880. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  8881. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  8882. (if (fboundp 'url-encode-url)
  8883. (url-encode-url text)
  8884. (if (org-string-match-p
  8885. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  8886. text)
  8887. (org-link-escape text org-link-escape-chars-browser)
  8888. text)))
  8889. (defun org-link-unescape (str)
  8890. "Unhex hexified Unicode strings as returned from the JavaScript function
  8891. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8892. (unless (and (null str) (string= "" str))
  8893. (let ((pos 0) (case-fold-search t) unhexed)
  8894. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8895. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8896. (setq str (replace-match unhexed t t str))
  8897. (setq pos (+ pos (length unhexed))))))
  8898. str)
  8899. (defun org-link-unescape-compound (hex)
  8900. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8901. Note: this function also decodes single byte encodings like
  8902. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8903. (save-match-data
  8904. (let* ((bytes (cdr (split-string hex "%")))
  8905. (ret "")
  8906. (eat 0)
  8907. (sum 0))
  8908. (while bytes
  8909. (let* ((val (string-to-number (pop bytes) 16))
  8910. (shift-xor
  8911. (if (= 0 eat)
  8912. (cond
  8913. ((>= val 252) (cons 6 252))
  8914. ((>= val 248) (cons 5 248))
  8915. ((>= val 240) (cons 4 240))
  8916. ((>= val 224) (cons 3 224))
  8917. ((>= val 192) (cons 2 192))
  8918. (t (cons 0 0)))
  8919. (cons 6 128))))
  8920. (if (>= val 192) (setq eat (car shift-xor)))
  8921. (setq val (logxor val (cdr shift-xor)))
  8922. (setq sum (+ (lsh sum (car shift-xor)) val))
  8923. (if (> eat 0) (setq eat (- eat 1)))
  8924. (cond
  8925. ((= 0 eat) ;multi byte
  8926. (setq ret (concat ret (org-char-to-string sum)))
  8927. (setq sum 0))
  8928. ((not bytes) ; single byte(s)
  8929. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8930. )) ;; end (while bytes
  8931. ret )))
  8932. (defun org-link-unescape-single-byte-sequence (hex)
  8933. "Unhexify hex-encoded single byte character sequences."
  8934. (mapconcat (lambda (byte)
  8935. (char-to-string (string-to-number byte 16)))
  8936. (cdr (split-string hex "%")) ""))
  8937. (defun org-xor (a b)
  8938. "Exclusive or."
  8939. (if a (not b) b))
  8940. (defun org-fixup-message-id-for-http (s)
  8941. "Replace special characters in a message id, so it can be used in an http query."
  8942. (when (string-match "%" s)
  8943. (setq s (mapconcat (lambda (c)
  8944. (if (eq c ?%)
  8945. "%25"
  8946. (char-to-string c)))
  8947. s "")))
  8948. (while (string-match "<" s)
  8949. (setq s (replace-match "%3C" t t s)))
  8950. (while (string-match ">" s)
  8951. (setq s (replace-match "%3E" t t s)))
  8952. (while (string-match "@" s)
  8953. (setq s (replace-match "%40" t t s)))
  8954. s)
  8955. (defun org-link-prettify (link)
  8956. "Return a human-readable representation of LINK.
  8957. The car of LINK must be a raw link.
  8958. The cdr of LINK must be either a link description or nil."
  8959. (let ((desc (or (cadr link) "<no description>")))
  8960. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8961. "<" (car link) ">")))
  8962. ;;;###autoload
  8963. (defun org-insert-link-global ()
  8964. "Insert a link like Org-mode does.
  8965. This command can be called in any mode to insert a link in Org-mode syntax."
  8966. (interactive)
  8967. (org-load-modules-maybe)
  8968. (org-run-like-in-org-mode 'org-insert-link))
  8969. (defun org-insert-all-links (arg &optional pre post)
  8970. "Insert all links in `org-stored-links'.
  8971. When a universal prefix, do not delete the links from `org-stored-links'.
  8972. When `ARG' is a number, insert the last N link(s).
  8973. `PRE' and `POST' are optional arguments to define a string to
  8974. prepend or to append."
  8975. (interactive "P")
  8976. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  8977. (links (copy-seq org-stored-links))
  8978. (pr (or pre "- "))
  8979. (po (or post "\n"))
  8980. (cnt 1) l)
  8981. (if (null org-stored-links)
  8982. (message "No link to insert")
  8983. (while (and (or (listp arg) (>= arg cnt))
  8984. (setq l (if (listp arg)
  8985. (pop links)
  8986. (pop org-stored-links))))
  8987. (setq cnt (1+ cnt))
  8988. (insert pr)
  8989. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8990. (insert po)))))
  8991. (defun org-insert-last-stored-link (arg)
  8992. "Insert the last link stored in `org-stored-links'."
  8993. (interactive "p")
  8994. (org-insert-all-links arg "" "\n"))
  8995. (defun org-link-fontify-links-to-this-file ()
  8996. "Fontify links to the current file in `org-stored-links'."
  8997. (let ((f (buffer-file-name)) a b)
  8998. (setq a (mapcar (lambda(l)
  8999. (let ((ll (car l)))
  9000. (when (and (string-match "^file:\\(.+\\)::" ll)
  9001. (equal f (expand-file-name (match-string 1 ll))))
  9002. ll)))
  9003. org-stored-links))
  9004. (when (featurep 'org-id)
  9005. (setq b (mapcar (lambda(l)
  9006. (let ((ll (car l)))
  9007. (when (and (string-match "^id:\\(.+\\)$" ll)
  9008. (equal f (expand-file-name
  9009. (or (org-id-find-id-file
  9010. (match-string 1 ll)) ""))))
  9011. ll)))
  9012. org-stored-links)))
  9013. (mapcar (lambda(l)
  9014. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9015. (delq nil (append a b)))))
  9016. (defvar org-link-links-in-this-file nil)
  9017. (defun org-insert-link (&optional complete-file link-location default-description)
  9018. "Insert a link. At the prompt, enter the link.
  9019. Completion can be used to insert any of the link protocol prefixes like
  9020. http or ftp in use.
  9021. The history can be used to select a link previously stored with
  9022. `org-store-link'. When the empty string is entered (i.e. if you just
  9023. press RET at the prompt), the link defaults to the most recently
  9024. stored link. As SPC triggers completion in the minibuffer, you need to
  9025. use M-SPC or C-q SPC to force the insertion of a space character.
  9026. You will also be prompted for a description, and if one is given, it will
  9027. be displayed in the buffer instead of the link.
  9028. If there is already a link at point, this command will allow you to edit link
  9029. and description parts.
  9030. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9031. be selected using completion. The path to the file will be relative to the
  9032. current directory if the file is in the current directory or a subdirectory.
  9033. Otherwise, the link will be the absolute path as completed in the minibuffer
  9034. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9035. option `org-link-file-path-type'.
  9036. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9037. the current directory or below.
  9038. With three \\[universal-argument] prefixes, negate the meaning of
  9039. `org-keep-stored-link-after-insertion'.
  9040. If `org-make-link-description-function' is non-nil, this function will be
  9041. called with the link target, and the result will be the default
  9042. link description.
  9043. If the LINK-LOCATION parameter is non-nil, this value will be
  9044. used as the link location instead of reading one interactively.
  9045. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9046. be used as the default description."
  9047. (interactive "P")
  9048. (let* ((wcf (current-window-configuration))
  9049. (origbuf (current-buffer))
  9050. (region (if (org-region-active-p)
  9051. (buffer-substring (region-beginning) (region-end))))
  9052. (remove (and region (list (region-beginning) (region-end))))
  9053. (desc region)
  9054. tmphist ; byte-compile incorrectly complains about this
  9055. (link link-location)
  9056. (abbrevs org-link-abbrev-alist-local)
  9057. entry file all-prefixes auto-desc)
  9058. (cond
  9059. (link-location) ; specified by arg, just use it.
  9060. ((org-in-regexp org-bracket-link-regexp 1)
  9061. ;; We do have a link at point, and we are going to edit it.
  9062. (setq remove (list (match-beginning 0) (match-end 0)))
  9063. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9064. (setq link (read-string "Link: "
  9065. (org-link-unescape
  9066. (org-match-string-no-properties 1)))))
  9067. ((or (org-in-regexp org-angle-link-re)
  9068. (org-in-regexp org-plain-link-re))
  9069. ;; Convert to bracket link
  9070. (setq remove (list (match-beginning 0) (match-end 0))
  9071. link (read-string "Link: "
  9072. (org-remove-angle-brackets (match-string 0)))))
  9073. ((member complete-file '((4) (16)))
  9074. ;; Completing read for file names.
  9075. (setq link (org-file-complete-link complete-file)))
  9076. (t
  9077. ;; Read link, with completion for stored links.
  9078. (org-link-fontify-links-to-this-file)
  9079. (org-switch-to-buffer-other-window "*Org Links*")
  9080. (with-current-buffer "*Org Links*"
  9081. (erase-buffer)
  9082. (insert "Insert a link.
  9083. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9084. (when org-stored-links
  9085. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9086. (insert (mapconcat 'org-link-prettify
  9087. (reverse org-stored-links) "\n")))
  9088. (goto-char (point-min)))
  9089. (let ((cw (selected-window)))
  9090. (select-window (get-buffer-window "*Org Links*" 'visible))
  9091. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9092. (unless (pos-visible-in-window-p (point-max))
  9093. (org-fit-window-to-buffer))
  9094. (and (window-live-p cw) (select-window cw)))
  9095. ;; Fake a link history, containing the stored links.
  9096. (setq tmphist (append (mapcar 'car org-stored-links)
  9097. org-insert-link-history))
  9098. (setq all-prefixes (append (mapcar 'car abbrevs)
  9099. (mapcar 'car org-link-abbrev-alist)
  9100. org-link-types))
  9101. (unwind-protect
  9102. (progn
  9103. (setq link
  9104. (org-completing-read
  9105. "Link: "
  9106. (append
  9107. (mapcar (lambda (x) (concat x ":"))
  9108. all-prefixes)
  9109. (mapcar 'car org-stored-links))
  9110. nil nil nil
  9111. 'tmphist
  9112. (caar org-stored-links)))
  9113. (if (not (string-match "\\S-" link))
  9114. (user-error "No link selected"))
  9115. (mapc (lambda(l)
  9116. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9117. org-stored-links)
  9118. (if (or (member link all-prefixes)
  9119. (and (equal ":" (substring link -1))
  9120. (member (substring link 0 -1) all-prefixes)
  9121. (setq link (substring link 0 -1))))
  9122. (setq link (with-current-buffer origbuf
  9123. (org-link-try-special-completion link)))))
  9124. (set-window-configuration wcf)
  9125. (kill-buffer "*Org Links*"))
  9126. (setq entry (assoc link org-stored-links))
  9127. (or entry (push link org-insert-link-history))
  9128. (setq desc (or desc (nth 1 entry)))))
  9129. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9130. (not org-keep-stored-link-after-insertion))
  9131. (setq org-stored-links (delq (assoc link org-stored-links)
  9132. org-stored-links)))
  9133. (if (and (string-match org-plain-link-re link)
  9134. (not (string-match org-ts-regexp link)))
  9135. ;; URL-like link, normalize the use of angular brackets.
  9136. (setq link (org-remove-angle-brackets link)))
  9137. ;; Check if we are linking to the current file with a search
  9138. ;; option If yes, simplify the link by using only the search
  9139. ;; option.
  9140. (when (and buffer-file-name
  9141. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9142. (let* ((path (match-string 1 link))
  9143. (case-fold-search nil)
  9144. (search (match-string 2 link)))
  9145. (save-match-data
  9146. (if (equal (file-truename buffer-file-name) (file-truename path))
  9147. ;; We are linking to this same file, with a search option
  9148. (setq link search)))))
  9149. ;; Check if we can/should use a relative path. If yes, simplify the link
  9150. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9151. (let* ((type (match-string 1 link))
  9152. (path (match-string 2 link))
  9153. (origpath path)
  9154. (case-fold-search nil))
  9155. (cond
  9156. ((or (eq org-link-file-path-type 'absolute)
  9157. (equal complete-file '(16)))
  9158. (setq path (abbreviate-file-name (expand-file-name path))))
  9159. ((eq org-link-file-path-type 'noabbrev)
  9160. (setq path (expand-file-name path)))
  9161. ((eq org-link-file-path-type 'relative)
  9162. (setq path (file-relative-name path)))
  9163. (t
  9164. (save-match-data
  9165. (if (string-match (concat "^" (regexp-quote
  9166. (expand-file-name
  9167. (file-name-as-directory
  9168. default-directory))))
  9169. (expand-file-name path))
  9170. ;; We are linking a file with relative path name.
  9171. (setq path (substring (expand-file-name path)
  9172. (match-end 0)))
  9173. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9174. (setq link (concat type path))
  9175. (if (equal desc origpath)
  9176. (setq desc path))))
  9177. (if org-make-link-description-function
  9178. (setq desc
  9179. (or (condition-case nil
  9180. (funcall org-make-link-description-function link desc)
  9181. (error (progn (message "Can't get link description from `%s'"
  9182. (symbol-name org-make-link-description-function))
  9183. (sit-for 2) nil)))
  9184. (read-string "Description: " default-description)))
  9185. (if default-description (setq desc default-description)
  9186. (setq desc (or (and auto-desc desc)
  9187. (read-string "Description: " desc)))))
  9188. (unless (string-match "\\S-" desc) (setq desc nil))
  9189. (if remove (apply 'delete-region remove))
  9190. (insert (org-make-link-string link desc))))
  9191. (defun org-link-try-special-completion (type)
  9192. "If there is completion support for link type TYPE, offer it."
  9193. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9194. (if (functionp fun)
  9195. (funcall fun)
  9196. (read-string "Link (no completion support): " (concat type ":")))))
  9197. (defun org-file-complete-link (&optional arg)
  9198. "Create a file link using completion."
  9199. (let (file link)
  9200. (setq file (org-iread-file-name "File: "))
  9201. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9202. (pwd1 (file-name-as-directory (abbreviate-file-name
  9203. (expand-file-name ".")))))
  9204. (cond
  9205. ((equal arg '(16))
  9206. (setq link (concat
  9207. "file:"
  9208. (abbreviate-file-name (expand-file-name file)))))
  9209. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9210. (setq link (concat "file:" (match-string 1 file))))
  9211. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9212. (expand-file-name file))
  9213. (setq link (concat
  9214. "file:" (match-string 1 (expand-file-name file)))))
  9215. (t (setq link (concat "file:" file)))))
  9216. link))
  9217. (defun org-iread-file-name (&rest args)
  9218. "Read-file-name using `ido-mode' speedup if available.
  9219. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9220. See `read-file-name' for a description of parameters."
  9221. (org-without-partial-completion
  9222. (if (and org-completion-use-ido
  9223. (fboundp 'ido-read-file-name)
  9224. (boundp 'ido-mode) ido-mode
  9225. (listp (second args)))
  9226. (let ((ido-enter-matching-directory nil))
  9227. (apply 'ido-read-file-name args))
  9228. (apply 'read-file-name args))))
  9229. (defun org-completing-read (&rest args)
  9230. "Completing-read with SPACE being a normal character."
  9231. (let ((enable-recursive-minibuffers t)
  9232. (minibuffer-local-completion-map
  9233. (copy-keymap minibuffer-local-completion-map)))
  9234. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9235. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9236. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9237. (apply 'org-icompleting-read args)))
  9238. (defun org-completing-read-no-i (&rest args)
  9239. (let (org-completion-use-ido org-completion-use-iswitchb)
  9240. (apply 'org-completing-read args)))
  9241. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9242. "Use iswitch as a completing-read replacement to choose from choices.
  9243. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9244. from."
  9245. (let* ((iswitchb-use-virtual-buffers nil)
  9246. (iswitchb-make-buflist-hook
  9247. (lambda ()
  9248. (setq iswitchb-temp-buflist choices))))
  9249. (iswitchb-read-buffer prompt)))
  9250. (defun org-icompleting-read (&rest args)
  9251. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9252. (org-without-partial-completion
  9253. (if (and org-completion-use-ido
  9254. (fboundp 'ido-completing-read)
  9255. (boundp 'ido-mode) ido-mode
  9256. (listp (second args)))
  9257. (let ((ido-enter-matching-directory nil))
  9258. (apply 'ido-completing-read (concat (car args))
  9259. (if (consp (car (nth 1 args)))
  9260. (mapcar 'car (nth 1 args))
  9261. (nth 1 args))
  9262. (cddr args)))
  9263. (if (and org-completion-use-iswitchb
  9264. (boundp 'iswitchb-mode) iswitchb-mode
  9265. (listp (second args)))
  9266. (apply 'org-iswitchb-completing-read (concat (car args))
  9267. (if (consp (car (nth 1 args)))
  9268. (mapcar 'car (nth 1 args))
  9269. (nth 1 args))
  9270. (cddr args))
  9271. (apply 'completing-read args)))))
  9272. (defun org-extract-attributes (s)
  9273. "Extract the attributes cookie from a string and set as text property."
  9274. (let (a attr (start 0) key value)
  9275. (save-match-data
  9276. (when (string-match "{{\\([^}]+\\)}}$" s)
  9277. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9278. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9279. (setq key (match-string 1 a) value (match-string 2 a)
  9280. start (match-end 0)
  9281. attr (plist-put attr (intern key) value))))
  9282. (org-add-props s nil 'org-attr attr))
  9283. s))
  9284. ;;; Opening/following a link
  9285. (defvar org-link-search-failed nil)
  9286. (defvar org-open-link-functions nil
  9287. "Hook for functions finding a plain text link.
  9288. These functions must take a single argument, the link content.
  9289. They will be called for links that look like [[link text][description]]
  9290. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9291. like a filename (e.g. \"./blue.png\").
  9292. These functions will be called *before* Org attempts to resolve the
  9293. link by doing text searches in the current buffer - so if you want a
  9294. link \"[[target]]\" to still find \"<<target>>\", your function should
  9295. handle this as a special case.
  9296. When the function does handle the link, it must return a non-nil value.
  9297. If it decides that it is not responsible for this link, it must return
  9298. nil to indicate that that Org-mode can continue with other options
  9299. like exact and fuzzy text search.")
  9300. (defun org-next-link (&optional search-backward)
  9301. "Move forward to the next link.
  9302. If the link is in hidden text, expose it."
  9303. (interactive "P")
  9304. (when (and org-link-search-failed (eq this-command last-command))
  9305. (goto-char (point-min))
  9306. (message "Link search wrapped back to beginning of buffer"))
  9307. (setq org-link-search-failed nil)
  9308. (let* ((pos (point))
  9309. (ct (org-context))
  9310. (a (assoc :link ct))
  9311. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9312. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9313. ((looking-at org-any-link-re)
  9314. ;; Don't stay stuck at link without an org-link face
  9315. (forward-char (if search-backward -1 1))))
  9316. (if (funcall srch-fun org-any-link-re nil t)
  9317. (progn
  9318. (goto-char (match-beginning 0))
  9319. (if (outline-invisible-p) (org-show-context)))
  9320. (goto-char pos)
  9321. (setq org-link-search-failed t)
  9322. (message "No further link found"))))
  9323. (defun org-previous-link ()
  9324. "Move backward to the previous link.
  9325. If the link is in hidden text, expose it."
  9326. (interactive)
  9327. (funcall 'org-next-link t))
  9328. (defun org-translate-link (s)
  9329. "Translate a link string if a translation function has been defined."
  9330. (if (and org-link-translation-function
  9331. (fboundp org-link-translation-function)
  9332. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9333. (progn
  9334. (setq s (funcall org-link-translation-function
  9335. (match-string 1 s) (match-string 2 s)))
  9336. (concat (car s) ":" (cdr s)))
  9337. s))
  9338. (defun org-translate-link-from-planner (type path)
  9339. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9340. This is still an experimental function, your mileage may vary."
  9341. (cond
  9342. ((member type '("http" "https" "news" "ftp"))
  9343. ;; standard Internet links are the same.
  9344. nil)
  9345. ((and (equal type "irc") (string-match "^//" path))
  9346. ;; Planner has two / at the beginning of an irc link, we have 1.
  9347. ;; We should have zero, actually....
  9348. (setq path (substring path 1)))
  9349. ((and (equal type "lisp") (string-match "^/" path))
  9350. ;; Planner has a slash, we do not.
  9351. (setq type "elisp" path (substring path 1)))
  9352. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9353. ;; A typical message link. Planner has the id after the final slash,
  9354. ;; we separate it with a hash mark
  9355. (setq path (concat (match-string 1 path) "#"
  9356. (org-remove-angle-brackets (match-string 2 path))))))
  9357. (cons type path))
  9358. (defun org-find-file-at-mouse (ev)
  9359. "Open file link or URL at mouse."
  9360. (interactive "e")
  9361. (mouse-set-point ev)
  9362. (org-open-at-point 'in-emacs))
  9363. (defun org-open-at-mouse (ev)
  9364. "Open file link or URL at mouse.
  9365. See the docstring of `org-open-file' for details."
  9366. (interactive "e")
  9367. (mouse-set-point ev)
  9368. (if (eq major-mode 'org-agenda-mode)
  9369. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9370. (org-open-at-point))
  9371. (defvar org-window-config-before-follow-link nil
  9372. "The window configuration before following a link.
  9373. This is saved in case the need arises to restore it.")
  9374. (defvar org-open-link-marker (make-marker)
  9375. "Marker pointing to the location where `org-open-at-point' was called.")
  9376. ;;;###autoload
  9377. (defun org-open-at-point-global ()
  9378. "Follow a link like Org-mode does.
  9379. This command can be called in any mode to follow a link that has
  9380. Org-mode syntax."
  9381. (interactive)
  9382. (org-run-like-in-org-mode 'org-open-at-point))
  9383. ;;;###autoload
  9384. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9385. "Open a link in the string S, as if it was in Org-mode."
  9386. (interactive "sLink: \nP")
  9387. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9388. (with-temp-buffer
  9389. (let ((org-inhibit-startup (not reference-buffer)))
  9390. (org-mode)
  9391. (insert s)
  9392. (goto-char (point-min))
  9393. (when reference-buffer
  9394. (setq org-link-abbrev-alist-local
  9395. (with-current-buffer reference-buffer
  9396. org-link-abbrev-alist-local)))
  9397. (org-open-at-point arg reference-buffer)))))
  9398. (defvar org-open-at-point-functions nil
  9399. "Hook that is run when following a link at point.
  9400. Functions in this hook must return t if they identify and follow
  9401. a link at point. If they don't find anything interesting at point,
  9402. they must return nil.")
  9403. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9404. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9405. (defun org-open-at-point (&optional arg reference-buffer)
  9406. "Open link, timestamp, footnote or tags at point.
  9407. When point is on a link, follow it. Normally, files will be
  9408. opened by an appropriate application. If the optional prefix
  9409. argument ARG is non-nil, Emacs will visit the file. With
  9410. a double prefix argument, try to open outside of Emacs, in the
  9411. application the system uses for this file type.
  9412. When point is on a timestamp, open the agenda at the day
  9413. specified.
  9414. When point is a footnote definition, move to the first reference
  9415. found. If it is on a reference, move to the associated
  9416. definition.
  9417. When point is on a headline, display a list of every link in the
  9418. entry, so it is possible to pick one, or all, of them. If point
  9419. is on a tag, call `org-tags-view' instead.
  9420. When optional argument REFERENCE-BUFFER is non-nil, it should
  9421. specify a buffer from where the link search should happen. This
  9422. is used internally by `org-open-link-from-string'."
  9423. (interactive "P")
  9424. ;; On a code block, open block's results.
  9425. (unless (call-interactively #'org-babel-open-src-block-result)
  9426. (org-load-modules-maybe)
  9427. (move-marker org-open-link-marker (point))
  9428. (setq org-window-config-before-follow-link (current-window-configuration))
  9429. (org-remove-occur-highlights nil nil t)
  9430. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9431. (let* ((context (org-element-context)) type)
  9432. ;; On an unsupported type, check if point is contained within
  9433. ;; a support one.
  9434. (while (and (not (memq (setq type (org-element-type context))
  9435. '(headline inlinetask link footnote-definition
  9436. footnote-reference timestamp)))
  9437. (setq context (org-element-property :parent context))))
  9438. (cond
  9439. ;; Unsupported context: return an error.
  9440. ((not context) (user-error "No link found"))
  9441. ;; On a headline or an inlinetask, but not on a timestamp,
  9442. ;; a link, a footnote reference or on tags.
  9443. ((and (memq type '(headline inlinetask))
  9444. ;; Not on tags.
  9445. (progn (save-excursion (beginning-of-line)
  9446. (looking-at org-complex-heading-regexp))
  9447. (or (not (match-beginning 5))
  9448. (< (point) (match-beginning 5)))))
  9449. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9450. (links (car data))
  9451. (links-end (cdr data)))
  9452. (if links
  9453. (dolist (link (if (stringp links) (list links) links))
  9454. (search-forward link nil links-end)
  9455. (goto-char (match-beginning 0))
  9456. (org-open-at-point))
  9457. (require 'org-attach)
  9458. (org-attach-reveal 'if-exists))))
  9459. ;; Do nothing on white spaces after an object, unless point
  9460. ;; is right after it.
  9461. ((> (point)
  9462. (save-excursion
  9463. (goto-char (org-element-property :end context))
  9464. (skip-chars-backward " \t")
  9465. (point)))
  9466. (user-error "No link found"))
  9467. ((eq type 'timestamp) (org-follow-timestamp-link))
  9468. ;; On tags within a headline or an inlinetask.
  9469. ((and (memq type '(headline inlinetask))
  9470. (progn (save-excursion (beginning-of-line)
  9471. (looking-at org-complex-heading-regexp))
  9472. (and (match-beginning 5)
  9473. (>= (point) (match-beginning 5)))))
  9474. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9475. ((eq type 'link)
  9476. (let ((type (org-element-property :type context))
  9477. (path (org-link-unescape (org-element-property :path context))))
  9478. ;; Switch back to REFERENCE-BUFFER needed when called in
  9479. ;; a temporary buffer through `org-open-link-from-string'.
  9480. (with-current-buffer (or reference-buffer (current-buffer))
  9481. (cond
  9482. ((equal type "file")
  9483. (if (string-match "[*?{]" (file-name-nondirectory path))
  9484. (dired path)
  9485. ;; Look into `org-link-protocols' in order to find
  9486. ;; a DEDICATED-FUNCTION to open file. The function
  9487. ;; will be applied on raw link instead of parsed
  9488. ;; link due to the limitation in `org-add-link-type'
  9489. ;; ("open" function called with a single argument).
  9490. ;; If no such function is found, fallback to
  9491. ;; `org-open-file'.
  9492. ;;
  9493. ;; Note : "file+emacs" and "file+sys" types are
  9494. ;; hard-coded in order to escape the previous
  9495. ;; limitation.
  9496. (let* ((option (org-element-property :search-option context))
  9497. (app (org-element-property :application context))
  9498. (dedicated-function
  9499. (nth 1 (assoc app org-link-protocols))))
  9500. (if dedicated-function
  9501. (funcall dedicated-function
  9502. (concat path
  9503. (and option (concat "::" option))))
  9504. (apply #'org-open-file
  9505. path
  9506. (cond (arg)
  9507. ((equal app "emacs") 'emacs)
  9508. ((equal app "sys") 'system))
  9509. (cond ((not option) nil)
  9510. ((org-string-match-p "\\`[0-9]+\\'" option)
  9511. (list (string-to-number option)))
  9512. (t (list nil option))))))))
  9513. ((assoc type org-link-protocols)
  9514. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9515. ((equal type "help")
  9516. (let ((f-or-v (intern path)))
  9517. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9518. ((boundp f-or-v) (describe-variable f-or-v))
  9519. (t (error "Not a known function or variable")))))
  9520. ((equal type "mailto")
  9521. (let ((cmd (car org-link-mailto-program))
  9522. (args (cdr org-link-mailto-program))
  9523. (spec
  9524. (format-spec-make
  9525. ?a path ; %a is address.
  9526. ?s (let ((option ; %s is subject.
  9527. (org-element-property
  9528. :search-option context)))
  9529. (if (not option) "" (org-link-escape option)))))
  9530. final-args)
  9531. (apply cmd
  9532. (dolist (arg args (nreverse final-args))
  9533. (if (not (stringp arg)) (push arg final-args)
  9534. (push (format-spec arg spec) final-args))))))
  9535. ((member type '("http" "https" "ftp" "news"))
  9536. (browse-url (org-link-escape-browser (concat type ":" path))))
  9537. ((equal type "doi")
  9538. (browse-url
  9539. (org-link-escape-browser (concat org-doi-server-url path))))
  9540. ((equal type "message") (browse-url (concat type ":" path)))
  9541. ((equal type "shell")
  9542. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9543. (cmd path))
  9544. (if (or (and (org-string-nw-p
  9545. org-confirm-shell-link-not-regexp)
  9546. (string-match
  9547. org-confirm-shell-link-not-regexp cmd))
  9548. (not org-confirm-shell-link-function)
  9549. (funcall org-confirm-shell-link-function
  9550. (format "Execute \"%s\" in shell? "
  9551. (org-add-props cmd nil
  9552. 'face 'org-warning))))
  9553. (progn
  9554. (message "Executing %s" cmd)
  9555. (shell-command cmd buf)
  9556. (when (featurep 'midnight)
  9557. (setq clean-buffer-list-kill-buffer-names
  9558. (cons buf
  9559. clean-buffer-list-kill-buffer-names))))
  9560. (error "Abort"))))
  9561. ((equal type "elisp")
  9562. (let ((cmd path))
  9563. (if (or (and (org-string-nw-p
  9564. org-confirm-elisp-link-not-regexp)
  9565. (org-string-match-p
  9566. org-confirm-elisp-link-not-regexp cmd))
  9567. (not org-confirm-elisp-link-function)
  9568. (funcall org-confirm-elisp-link-function
  9569. (format "Execute \"%s\" as elisp? "
  9570. (org-add-props cmd nil
  9571. 'face 'org-warning))))
  9572. (message "%s => %s" cmd
  9573. (if (eq (string-to-char cmd) ?\()
  9574. (eval (read cmd))
  9575. (call-interactively (read cmd))))
  9576. (error "Abort"))))
  9577. ((equal type "id")
  9578. (require 'ord-id)
  9579. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9580. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9581. (unless (run-hook-with-args-until-success
  9582. 'org-open-link-functions path)
  9583. (if (not arg) (org-mark-ring-push)
  9584. (switch-to-buffer-other-window
  9585. (org-get-buffer-for-internal-link (current-buffer))))
  9586. (let ((cmd `(org-link-search
  9587. ,(org-element-property :raw-link context)
  9588. ,(cond ((equal arg '(4)) ''occur)
  9589. ((equal arg '(16)) ''org-occur))
  9590. ,(org-element-property :begin context))))
  9591. (condition-case nil
  9592. (let ((org-link-search-inhibit-query t))
  9593. (eval cmd))
  9594. (error (progn (widen) (eval cmd)))))))
  9595. (t (browse-url-at-point))))))
  9596. ;; On a footnote reference or at a footnote definition's label.
  9597. ((or (eq type 'footnote-reference)
  9598. (and (eq type 'footnote-definition)
  9599. (save-excursion
  9600. ;; Do not validate action when point is on the
  9601. ;; spaces right after the footnote label, in
  9602. ;; order to be on par with behaviour on links.
  9603. (skip-chars-forward " \t")
  9604. (let ((begin
  9605. (org-element-property :contents-begin context)))
  9606. (if begin (< (point) begin)
  9607. (= (org-element-property :post-affiliated context)
  9608. (line-beginning-position)))))))
  9609. (org-footnote-action))
  9610. (t (user-error "No link found")))))
  9611. (move-marker org-open-link-marker nil)
  9612. (run-hook-with-args 'org-follow-link-hook)))
  9613. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9614. "Offer links in the current entry and return the selected link.
  9615. If there is only one link, return it.
  9616. If NTH is an integer, return the NTH link found.
  9617. If ZERO is a string, check also this string for a link, and if
  9618. there is one, return it."
  9619. (with-current-buffer buffer
  9620. (save-excursion
  9621. (save-restriction
  9622. (widen)
  9623. (goto-char marker)
  9624. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9625. "\\(" org-angle-link-re "\\)\\|"
  9626. "\\(" org-plain-link-re "\\)"))
  9627. (cnt ?0)
  9628. (in-emacs (if (integerp nth) nil nth))
  9629. have-zero end links link c)
  9630. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9631. (push (match-string 0 zero) links)
  9632. (setq cnt (1- cnt) have-zero t))
  9633. (save-excursion
  9634. (org-back-to-heading t)
  9635. (setq end (save-excursion (outline-next-heading) (point)))
  9636. (while (re-search-forward re end t)
  9637. (push (match-string 0) links))
  9638. (setq links (org-uniquify (reverse links))))
  9639. (cond
  9640. ((null links)
  9641. (message "No links"))
  9642. ((equal (length links) 1)
  9643. (setq link (car links)))
  9644. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9645. (setq link (nth (if have-zero nth (1- nth)) links)))
  9646. (t ; we have to select a link
  9647. (save-excursion
  9648. (save-window-excursion
  9649. (delete-other-windows)
  9650. (with-output-to-temp-buffer "*Select Link*"
  9651. (mapc (lambda (l)
  9652. (if (not (string-match org-bracket-link-regexp l))
  9653. (princ (format "[%c] %s\n" (incf cnt)
  9654. (org-remove-angle-brackets l)))
  9655. (if (match-end 3)
  9656. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9657. (match-string 3 l) (match-string 1 l)))
  9658. (princ (format "[%c] %s\n" (incf cnt)
  9659. (match-string 1 l))))))
  9660. links))
  9661. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9662. (message "Select link to open, RET to open all:")
  9663. (setq c (read-char-exclusive))
  9664. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9665. (when (equal c ?q) (error "Abort"))
  9666. (if (equal c ?\C-m)
  9667. (setq link links)
  9668. (setq nth (- c ?0))
  9669. (if have-zero (setq nth (1+ nth)))
  9670. (unless (and (integerp nth) (>= (length links) nth))
  9671. (user-error "Invalid link selection"))
  9672. (setq link (nth (1- nth) links)))))
  9673. (cons link end))))))
  9674. ;; TODO: These functions are deprecated since `org-open-at-point'
  9675. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9676. (defun org-open-file-with-system (path)
  9677. "Open file at PATH using the system way of opening it."
  9678. (org-open-file path 'system))
  9679. (defun org-open-file-with-emacs (path)
  9680. "Open file at PATH in Emacs."
  9681. (org-open-file path 'emacs))
  9682. ;;; File search
  9683. (defvar org-create-file-search-functions nil
  9684. "List of functions to construct the right search string for a file link.
  9685. These functions are called in turn with point at the location to
  9686. which the link should point.
  9687. A function in the hook should first test if it would like to
  9688. handle this file type, for example by checking the `major-mode'
  9689. or the file extension. If it decides not to handle this file, it
  9690. should just return nil to give other functions a chance. If it
  9691. does handle the file, it must return the search string to be used
  9692. when following the link. The search string will be part of the
  9693. file link, given after a double colon, and `org-open-at-point'
  9694. will automatically search for it. If special measures must be
  9695. taken to make the search successful, another function should be
  9696. added to the companion hook `org-execute-file-search-functions',
  9697. which see.
  9698. A function in this hook may also use `setq' to set the variable
  9699. `description' to provide a suggestion for the descriptive text to
  9700. be used for this link when it gets inserted into an Org-mode
  9701. buffer with \\[org-insert-link].")
  9702. (defvar org-execute-file-search-functions nil
  9703. "List of functions to execute a file search triggered by a link.
  9704. Functions added to this hook must accept a single argument, the
  9705. search string that was part of the file link, the part after the
  9706. double colon. The function must first check if it would like to
  9707. handle this search, for example by checking the `major-mode' or
  9708. the file extension. If it decides not to handle this search, it
  9709. should just return nil to give other functions a chance. If it
  9710. does handle the search, it must return a non-nil value to keep
  9711. other functions from trying.
  9712. Each function can access the current prefix argument through the
  9713. variable `current-prefix-arg'. Note that a single prefix is used
  9714. to force opening a link in Emacs, so it may be good to only use a
  9715. numeric or double prefix to guide the search function.
  9716. In case this is needed, a function in this hook can also restore
  9717. the window configuration before `org-open-at-point' was called using:
  9718. (set-window-configuration org-window-config-before-follow-link)")
  9719. (defun org-link-search (s &optional type avoid-pos stealth)
  9720. "Search for a link search option.
  9721. If S is surrounded by forward slashes, it is interpreted as a
  9722. regular expression. In org-mode files, this will create an `org-occur'
  9723. sparse tree. In ordinary files, `occur' will be used to list matches.
  9724. If the current buffer is in `dired-mode', grep will be used to search
  9725. in all files. If AVOID-POS is given, ignore matches near that position.
  9726. When optional argument STEALTH is non-nil, do not modify
  9727. visibility around point, thus ignoring
  9728. `org-show-hierarchy-above', `org-show-following-heading' and
  9729. `org-show-siblings' variables."
  9730. (let ((case-fold-search t)
  9731. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9732. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9733. (append '(("") (" ") ("\t") ("\n"))
  9734. org-emphasis-alist)
  9735. "\\|") "\\)"))
  9736. (pos (point))
  9737. (pre nil) (post nil)
  9738. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9739. (cond
  9740. ;; First check if there are any special search functions
  9741. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9742. ;; Now try the builtin stuff
  9743. ((and (equal (string-to-char s0) ?#)
  9744. (> (length s0) 1)
  9745. (save-excursion
  9746. (goto-char (point-min))
  9747. (and
  9748. (re-search-forward
  9749. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9750. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9751. (setq type 'dedicated
  9752. pos (match-beginning 0))))
  9753. ;; There is an exact target for this
  9754. (goto-char pos)
  9755. (org-back-to-heading t)))
  9756. ((save-excursion
  9757. (goto-char (point-min))
  9758. (and
  9759. (re-search-forward
  9760. (concat "<<" (regexp-quote s0) ">>") nil t)
  9761. (setq type 'dedicated
  9762. pos (match-beginning 0))))
  9763. ;; There is an exact target for this
  9764. (goto-char pos))
  9765. ((save-excursion
  9766. (goto-char (point-min))
  9767. (and
  9768. (re-search-forward
  9769. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9770. (setq type 'dedicated pos (match-beginning 0))))
  9771. ;; Found an element with a matching #+name affiliated keyword.
  9772. (goto-char pos))
  9773. ((and (string-match "^(\\(.*\\))$" s0)
  9774. (save-excursion
  9775. (goto-char (point-min))
  9776. (and
  9777. (re-search-forward
  9778. (concat "[^[]" (regexp-quote
  9779. (format org-coderef-label-format
  9780. (match-string 1 s0))))
  9781. nil t)
  9782. (setq type 'dedicated
  9783. pos (1+ (match-beginning 0))))))
  9784. ;; There is a coderef target for this
  9785. (goto-char pos))
  9786. ((string-match "^/\\(.*\\)/$" s)
  9787. ;; A regular expression
  9788. (cond
  9789. ((derived-mode-p 'org-mode)
  9790. (org-occur (match-string 1 s)))
  9791. (t (org-do-occur (match-string 1 s)))))
  9792. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9793. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9794. (goto-char (point-min))
  9795. (cond
  9796. ((let (case-fold-search)
  9797. (re-search-forward (format org-complex-heading-regexp-format
  9798. (regexp-quote s))
  9799. nil t))
  9800. ;; OK, found a match
  9801. (setq type 'dedicated)
  9802. (goto-char (match-beginning 0)))
  9803. ((and (not org-link-search-inhibit-query)
  9804. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9805. (y-or-n-p "No match - create this as a new heading? "))
  9806. (goto-char (point-max))
  9807. (or (bolp) (newline))
  9808. (insert "* " s "\n")
  9809. (beginning-of-line 0))
  9810. (t
  9811. (goto-char pos)
  9812. (error "No match"))))
  9813. (t
  9814. ;; A normal search string
  9815. (when (equal (string-to-char s) ?*)
  9816. ;; Anchor on headlines, post may include tags.
  9817. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9818. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9819. s (substring s 1)))
  9820. (remove-text-properties
  9821. 0 (length s)
  9822. '(face nil mouse-face nil keymap nil fontified nil) s)
  9823. ;; Make a series of regular expressions to find a match
  9824. (setq words (org-split-string s "[ \n\r\t]+")
  9825. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9826. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9827. "\\)" markers)
  9828. re2a_ (concat "\\(" (mapconcat 'downcase words
  9829. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9830. re2a (concat "[ \t\r\n]" re2a_)
  9831. re4_ (concat "\\(" (mapconcat 'downcase words
  9832. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9833. re4 (concat "[^a-zA-Z_]" re4_)
  9834. re1 (concat pre re2 post)
  9835. re3 (concat pre (if pre re4_ re4) post)
  9836. re5 (concat pre ".*" re4)
  9837. re2 (concat pre re2)
  9838. re2a (concat pre (if pre re2a_ re2a))
  9839. re4 (concat pre (if pre re4_ re4))
  9840. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9841. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9842. re5 "\\)"))
  9843. (cond
  9844. ((eq type 'org-occur) (org-occur reall))
  9845. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9846. (t (goto-char (point-min))
  9847. (setq type 'fuzzy)
  9848. (if (or (and (org-search-not-self 1 re0 nil t)
  9849. (setq type 'dedicated))
  9850. (org-search-not-self 1 re1 nil t)
  9851. (org-search-not-self 1 re2 nil t)
  9852. (org-search-not-self 1 re2a nil t)
  9853. (org-search-not-self 1 re3 nil t)
  9854. (org-search-not-self 1 re4 nil t)
  9855. (org-search-not-self 1 re5 nil t))
  9856. (goto-char (match-beginning 1))
  9857. (goto-char pos)
  9858. (error "No match"))))))
  9859. (and (derived-mode-p 'org-mode)
  9860. (not stealth)
  9861. (org-show-context 'link-search))
  9862. type))
  9863. (defun org-search-not-self (group &rest args)
  9864. "Execute `re-search-forward', but only accept matches that do not
  9865. enclose the position of `org-open-link-marker'."
  9866. (let ((m org-open-link-marker))
  9867. (catch 'exit
  9868. (while (apply 're-search-forward args)
  9869. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9870. (goto-char (match-end group))
  9871. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9872. (> (match-beginning 0) (marker-position m))
  9873. (< (match-end 0) (marker-position m)))
  9874. (save-match-data
  9875. (or (not (org-in-regexp
  9876. org-bracket-link-analytic-regexp 1))
  9877. (not (match-end 4)) ; no description
  9878. (and (<= (match-beginning 4) (point))
  9879. (>= (match-end 4) (point))))))
  9880. (throw 'exit (point))))))))
  9881. (defun org-get-buffer-for-internal-link (buffer)
  9882. "Return a buffer to be used for displaying the link target of internal links."
  9883. (cond
  9884. ((not org-display-internal-link-with-indirect-buffer)
  9885. buffer)
  9886. ((string-match "(Clone)$" (buffer-name buffer))
  9887. (message "Buffer is already a clone, not making another one")
  9888. ;; we also do not modify visibility in this case
  9889. buffer)
  9890. (t ; make a new indirect buffer for displaying the link
  9891. (let* ((bn (buffer-name buffer))
  9892. (ibn (concat bn "(Clone)"))
  9893. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9894. (with-current-buffer ib (org-overview))
  9895. ib))))
  9896. (defun org-do-occur (regexp &optional cleanup)
  9897. "Call the Emacs command `occur'.
  9898. If CLEANUP is non-nil, remove the printout of the regular expression
  9899. in the *Occur* buffer. This is useful if the regex is long and not useful
  9900. to read."
  9901. (occur regexp)
  9902. (when cleanup
  9903. (let ((cwin (selected-window)) win beg end)
  9904. (when (setq win (get-buffer-window "*Occur*"))
  9905. (select-window win))
  9906. (goto-char (point-min))
  9907. (when (re-search-forward "match[a-z]+" nil t)
  9908. (setq beg (match-end 0))
  9909. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9910. (setq end (1- (match-beginning 0)))))
  9911. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9912. (goto-char (point-min))
  9913. (select-window cwin))))
  9914. ;;; The mark ring for links jumps
  9915. (defvar org-mark-ring nil
  9916. "Mark ring for positions before jumps in Org-mode.")
  9917. (defvar org-mark-ring-last-goto nil
  9918. "Last position in the mark ring used to go back.")
  9919. ;; Fill and close the ring
  9920. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9921. (loop for i from 1 to org-mark-ring-length do
  9922. (push (make-marker) org-mark-ring))
  9923. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9924. org-mark-ring)
  9925. (defun org-mark-ring-push (&optional pos buffer)
  9926. "Put the current position or POS into the mark ring and rotate it."
  9927. (interactive)
  9928. (setq pos (or pos (point)))
  9929. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9930. (move-marker (car org-mark-ring)
  9931. (or pos (point))
  9932. (or buffer (current-buffer)))
  9933. (message "%s"
  9934. (substitute-command-keys
  9935. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9936. (defun org-mark-ring-goto (&optional n)
  9937. "Jump to the previous position in the mark ring.
  9938. With prefix arg N, jump back that many stored positions. When
  9939. called several times in succession, walk through the entire ring.
  9940. Org-mode commands jumping to a different position in the current file,
  9941. or to another Org-mode file, automatically push the old position
  9942. onto the ring."
  9943. (interactive "p")
  9944. (let (p m)
  9945. (if (eq last-command this-command)
  9946. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9947. (setq p org-mark-ring))
  9948. (setq org-mark-ring-last-goto p)
  9949. (setq m (car p))
  9950. (org-pop-to-buffer-same-window (marker-buffer m))
  9951. (goto-char m)
  9952. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9953. (defun org-remove-angle-brackets (s)
  9954. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9955. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9956. s)
  9957. (defun org-add-angle-brackets (s)
  9958. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9959. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9960. s)
  9961. (defun org-remove-double-quotes (s)
  9962. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9963. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9964. s)
  9965. ;;; Following specific links
  9966. (defun org-follow-timestamp-link ()
  9967. "Open an agenda view for the time-stamp date/range at point."
  9968. (cond
  9969. ((org-at-date-range-p t)
  9970. (let ((org-agenda-start-on-weekday)
  9971. (t1 (match-string 1))
  9972. (t2 (match-string 2)) tt1 tt2)
  9973. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9974. tt2 (time-to-days (org-time-string-to-time t2)))
  9975. (let ((org-agenda-buffer-tmp-name
  9976. (format "*Org Agenda(a:%s)"
  9977. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9978. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9979. ((org-at-timestamp-p t)
  9980. (let ((org-agenda-buffer-tmp-name
  9981. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9982. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9983. (substring (match-string 1) 0 10)))
  9984. 1)))
  9985. (t (error "This should not happen"))))
  9986. ;;; Following file links
  9987. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9988. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9989. (declare-function mailcap-mime-info
  9990. "mailcap" (string &optional request no-decode))
  9991. (defvar org-wait nil)
  9992. (defun org-open-file (path &optional in-emacs line search)
  9993. "Open the file at PATH.
  9994. First, this expands any special file name abbreviations. Then the
  9995. configuration variable `org-file-apps' is checked if it contains an
  9996. entry for this file type, and if yes, the corresponding command is launched.
  9997. If no application is found, Emacs simply visits the file.
  9998. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9999. With a double \\[universal-argument] \\[universal-argument] \
  10000. prefix arg, Org tries to avoid opening in Emacs
  10001. and to use an external application to visit the file.
  10002. Optional LINE specifies a line to go to, optional SEARCH a string
  10003. to search for. If LINE or SEARCH is given, the file will be
  10004. opened in Emacs, unless an entry from org-file-apps that makes
  10005. use of groups in a regexp matches.
  10006. If you want to change the way frames are used when following a
  10007. link, please customize `org-link-frame-setup'.
  10008. If the file does not exist, an error is thrown."
  10009. (let* ((file (if (equal path "")
  10010. buffer-file-name
  10011. (substitute-in-file-name (expand-file-name path))))
  10012. (file-apps (append org-file-apps (org-default-apps)))
  10013. (apps (org-remove-if
  10014. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10015. (apps-dlink (org-remove-if-not
  10016. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10017. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10018. (dirp (if remp nil (file-directory-p file)))
  10019. (file (if (and dirp org-open-directory-means-index-dot-org)
  10020. (concat (file-name-as-directory file) "index.org")
  10021. file))
  10022. (a-m-a-p (assq 'auto-mode apps))
  10023. (dfile (downcase file))
  10024. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10025. (link (cond ((and (eq line nil)
  10026. (eq search nil))
  10027. file)
  10028. (line
  10029. (concat file "::" (number-to-string line)))
  10030. (search
  10031. (concat file "::" search))))
  10032. (dlink (downcase link))
  10033. (old-buffer (current-buffer))
  10034. (old-pos (point))
  10035. (old-mode major-mode)
  10036. ext cmd link-match-data)
  10037. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10038. (setq ext (match-string 1 dfile))
  10039. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10040. (setq ext (match-string 1 dfile))))
  10041. (cond
  10042. ((member in-emacs '((16) system))
  10043. (setq cmd (cdr (assoc 'system apps))))
  10044. (in-emacs (setq cmd 'emacs))
  10045. (t
  10046. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10047. (and dirp (cdr (assoc 'directory apps)))
  10048. ; first, try matching against apps-dlink
  10049. ; if we get a match here, store the match data for later
  10050. (let ((match (assoc-default dlink apps-dlink
  10051. 'string-match)))
  10052. (if match
  10053. (progn (setq link-match-data (match-data))
  10054. match)
  10055. (progn (setq in-emacs (or in-emacs line search))
  10056. nil))) ; if we have no match in apps-dlink,
  10057. ; always open the file in emacs if line or search
  10058. ; is given (for backwards compatibility)
  10059. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10060. 'string-match)
  10061. (cdr (assoc ext apps))
  10062. (cdr (assoc t apps))))))
  10063. (when (eq cmd 'system)
  10064. (setq cmd (cdr (assoc 'system apps))))
  10065. (when (eq cmd 'default)
  10066. (setq cmd (cdr (assoc t apps))))
  10067. (when (eq cmd 'mailcap)
  10068. (require 'mailcap)
  10069. (mailcap-parse-mailcaps)
  10070. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10071. (command (mailcap-mime-info mime-type)))
  10072. (if (stringp command)
  10073. (setq cmd command)
  10074. (setq cmd 'emacs))))
  10075. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10076. (not (file-exists-p file))
  10077. (not org-open-non-existing-files))
  10078. (user-error "No such file: %s" file))
  10079. (cond
  10080. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10081. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10082. (while (string-match "['\"]%s['\"]" cmd)
  10083. (setq cmd (replace-match "%s" t t cmd)))
  10084. (while (string-match "%s" cmd)
  10085. (setq cmd (replace-match
  10086. (save-match-data
  10087. (shell-quote-argument
  10088. (convert-standard-filename file)))
  10089. t t cmd)))
  10090. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10091. (save-match-data
  10092. (let ((match-index 1)
  10093. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10094. (set-match-data link-match-data)
  10095. (while (<= match-index number-of-groups)
  10096. (let ((regex (concat "%" (number-to-string match-index)))
  10097. (replace-with (match-string match-index dlink)))
  10098. (while (string-match regex cmd)
  10099. (setq cmd (replace-match replace-with t t cmd))))
  10100. (setq match-index (+ match-index 1)))))
  10101. (save-window-excursion
  10102. (message "Running %s...done" cmd)
  10103. (start-process-shell-command cmd nil cmd)
  10104. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10105. ((or (stringp cmd)
  10106. (eq cmd 'emacs))
  10107. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10108. (widen)
  10109. (if line (progn (org-goto-line line)
  10110. (if (derived-mode-p 'org-mode)
  10111. (org-reveal)))
  10112. (if search (org-link-search search))))
  10113. ((consp cmd)
  10114. (let ((file (convert-standard-filename file)))
  10115. (save-match-data
  10116. (set-match-data link-match-data)
  10117. (eval cmd))))
  10118. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10119. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10120. (or (not (equal old-buffer (current-buffer)))
  10121. (not (equal old-pos (point))))
  10122. (org-mark-ring-push old-pos old-buffer))))
  10123. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10124. "This function returns non-nil if `entry' uses a regular
  10125. expression which should be matched against the whole link by
  10126. org-open-file.
  10127. It assumes that is the case when the entry uses a regular
  10128. expression which has at least one grouping construct and the
  10129. action is either a lisp form or a command string containing
  10130. '%1', i.e. using at least one subexpression match as a
  10131. parameter."
  10132. (let ((selector (car entry))
  10133. (action (cdr entry)))
  10134. (if (stringp selector)
  10135. (and (> (regexp-opt-depth selector) 0)
  10136. (or (and (stringp action)
  10137. (string-match "%[0-9]" action))
  10138. (consp action)))
  10139. nil)))
  10140. (defun org-default-apps ()
  10141. "Return the default applications for this operating system."
  10142. (cond
  10143. ((eq system-type 'darwin)
  10144. org-file-apps-defaults-macosx)
  10145. ((eq system-type 'windows-nt)
  10146. org-file-apps-defaults-windowsnt)
  10147. (t org-file-apps-defaults-gnu)))
  10148. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10149. "Convert extensions to regular expressions in the cars of LIST.
  10150. Also, weed out any non-string entries, because the return value is used
  10151. only for regexp matching.
  10152. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10153. point to the symbol `emacs', indicating that the file should
  10154. be opened in Emacs."
  10155. (append
  10156. (delq nil
  10157. (mapcar (lambda (x)
  10158. (if (not (stringp (car x)))
  10159. nil
  10160. (if (string-match "\\W" (car x))
  10161. x
  10162. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10163. list))
  10164. (if add-auto-mode
  10165. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10166. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10167. (defun org-file-remote-p (file)
  10168. "Test whether FILE specifies a location on a remote system.
  10169. Return non-nil if the location is indeed remote.
  10170. For example, the filename \"/user@host:/foo\" specifies a location
  10171. on the system \"/user@host:\"."
  10172. (cond ((fboundp 'file-remote-p)
  10173. (file-remote-p file))
  10174. ((fboundp 'tramp-handle-file-remote-p)
  10175. (tramp-handle-file-remote-p file))
  10176. ((and (boundp 'ange-ftp-name-format)
  10177. (string-match (car ange-ftp-name-format) file))
  10178. t)))
  10179. ;;;; Refiling
  10180. (defun org-get-org-file ()
  10181. "Read a filename, with default directory `org-directory'."
  10182. (let ((default (or org-default-notes-file remember-data-file)))
  10183. (read-file-name (format "File name [%s]: " default)
  10184. (file-name-as-directory org-directory)
  10185. default)))
  10186. (defun org-notes-order-reversed-p ()
  10187. "Check if the current file should receive notes in reversed order."
  10188. (cond
  10189. ((not org-reverse-note-order) nil)
  10190. ((eq t org-reverse-note-order) t)
  10191. ((not (listp org-reverse-note-order)) nil)
  10192. (t (catch 'exit
  10193. (let ((all org-reverse-note-order)
  10194. entry)
  10195. (while (setq entry (pop all))
  10196. (if (string-match (car entry) buffer-file-name)
  10197. (throw 'exit (cdr entry))))
  10198. nil)))))
  10199. (defvar org-refile-target-table nil
  10200. "The list of refile targets, created by `org-refile'.")
  10201. (defvar org-agenda-new-buffers nil
  10202. "Buffers created to visit agenda files.")
  10203. (defvar org-refile-cache nil
  10204. "Cache for refile targets.")
  10205. (defvar org-refile-markers nil
  10206. "All the markers used for caching refile locations.")
  10207. (defun org-refile-marker (pos)
  10208. "Get a new refile marker, but only if caching is in use."
  10209. (if (not org-refile-use-cache)
  10210. pos
  10211. (let ((m (make-marker)))
  10212. (move-marker m pos)
  10213. (push m org-refile-markers)
  10214. m)))
  10215. (defun org-refile-cache-clear ()
  10216. "Clear the refile cache and disable all the markers."
  10217. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10218. (setq org-refile-markers nil)
  10219. (setq org-refile-cache nil)
  10220. (message "Refile cache has been cleared"))
  10221. (defun org-refile-cache-check-set (set)
  10222. "Check if all the markers in the cache still have live buffers."
  10223. (let (marker)
  10224. (catch 'exit
  10225. (while (and set (setq marker (nth 3 (pop set))))
  10226. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10227. (when (and marker (null (marker-buffer marker)))
  10228. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10229. (sit-for 3)
  10230. (throw 'exit nil)))
  10231. t)))
  10232. (defun org-refile-cache-put (set &rest identifiers)
  10233. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10234. (let* ((key (sha1 (prin1-to-string identifiers)))
  10235. (entry (assoc key org-refile-cache)))
  10236. (if entry
  10237. (setcdr entry set)
  10238. (push (cons key set) org-refile-cache))))
  10239. (defun org-refile-cache-get (&rest identifiers)
  10240. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10241. (cond
  10242. ((not org-refile-cache) nil)
  10243. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10244. (t
  10245. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10246. org-refile-cache))))
  10247. (and set (org-refile-cache-check-set set) set)))))
  10248. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10249. "Produce a table with refile targets."
  10250. (let ((case-fold-search nil)
  10251. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10252. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10253. targets tgs txt re files f desc descre fast-path-p level pos0)
  10254. (message "Getting targets...")
  10255. (with-current-buffer (or default-buffer (current-buffer))
  10256. (while (setq entry (pop entries))
  10257. (setq files (car entry) desc (cdr entry))
  10258. (setq fast-path-p nil)
  10259. (cond
  10260. ((null files) (setq files (list (current-buffer))))
  10261. ((eq files 'org-agenda-files)
  10262. (setq files (org-agenda-files 'unrestricted)))
  10263. ((and (symbolp files) (fboundp files))
  10264. (setq files (funcall files)))
  10265. ((and (symbolp files) (boundp files))
  10266. (setq files (symbol-value files))))
  10267. (if (stringp files) (setq files (list files)))
  10268. (cond
  10269. ((eq (car desc) :tag)
  10270. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10271. ((eq (car desc) :todo)
  10272. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10273. ((eq (car desc) :regexp)
  10274. (setq descre (cdr desc)))
  10275. ((eq (car desc) :level)
  10276. (setq descre (concat "^\\*\\{" (number-to-string
  10277. (if org-odd-levels-only
  10278. (1- (* 2 (cdr desc)))
  10279. (cdr desc)))
  10280. "\\}[ \t]")))
  10281. ((eq (car desc) :maxlevel)
  10282. (setq fast-path-p t)
  10283. (setq descre (concat "^\\*\\{1," (number-to-string
  10284. (if org-odd-levels-only
  10285. (1- (* 2 (cdr desc)))
  10286. (cdr desc)))
  10287. "\\}[ \t]")))
  10288. (t (error "Bad refiling target description %s" desc)))
  10289. (while (setq f (pop files))
  10290. (with-current-buffer
  10291. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10292. (or
  10293. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10294. (progn
  10295. (if (bufferp f) (setq f (buffer-file-name
  10296. (buffer-base-buffer f))))
  10297. (setq f (and f (expand-file-name f)))
  10298. (if (eq org-refile-use-outline-path 'file)
  10299. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10300. (save-excursion
  10301. (save-restriction
  10302. (widen)
  10303. (goto-char (point-min))
  10304. (while (re-search-forward descre nil t)
  10305. (goto-char (setq pos0 (point-at-bol)))
  10306. (catch 'next
  10307. (when org-refile-target-verify-function
  10308. (save-match-data
  10309. (or (funcall org-refile-target-verify-function)
  10310. (throw 'next t))))
  10311. (when (and (looking-at org-complex-heading-regexp)
  10312. (not (member (match-string 4) excluded-entries))
  10313. (match-string 4))
  10314. (setq level (org-reduced-level
  10315. (- (match-end 1) (match-beginning 1)))
  10316. txt (org-link-display-format (match-string 4))
  10317. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10318. re (format org-complex-heading-regexp-format
  10319. (regexp-quote (match-string 4))))
  10320. (when org-refile-use-outline-path
  10321. (setq txt (mapconcat
  10322. 'org-protect-slash
  10323. (append
  10324. (if (eq org-refile-use-outline-path
  10325. 'file)
  10326. (list (file-name-nondirectory
  10327. (buffer-file-name
  10328. (buffer-base-buffer))))
  10329. (if (eq org-refile-use-outline-path
  10330. 'full-file-path)
  10331. (list (buffer-file-name
  10332. (buffer-base-buffer)))))
  10333. (org-get-outline-path fast-path-p
  10334. level txt)
  10335. (list txt))
  10336. "/")))
  10337. (push (list txt f re (org-refile-marker (point)))
  10338. tgs)))
  10339. (when (= (point) pos0)
  10340. ;; verification function has not moved point
  10341. (goto-char (point-at-eol))))))))
  10342. (when org-refile-use-cache
  10343. (org-refile-cache-put tgs (buffer-file-name) descre))
  10344. (setq targets (append tgs targets))))))
  10345. (message "Getting targets...done")
  10346. (nreverse targets)))
  10347. (defun org-protect-slash (s)
  10348. (while (string-match "/" s)
  10349. (setq s (replace-match "\\" t t s)))
  10350. s)
  10351. (defvar org-olpa (make-vector 20 nil))
  10352. (defun org-get-outline-path (&optional fastp level heading)
  10353. "Return the outline path to the current entry, as a list.
  10354. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10355. routine which makes outline path derivations for an entire file,
  10356. avoiding backtracing. Refile target collection makes use of that."
  10357. (if fastp
  10358. (progn
  10359. (if (> level 19)
  10360. (error "Outline path failure, more than 19 levels"))
  10361. (loop for i from level upto 19 do
  10362. (aset org-olpa i nil))
  10363. (prog1
  10364. (delq nil (append org-olpa nil))
  10365. (aset org-olpa level heading)))
  10366. (let (rtn case-fold-search)
  10367. (save-excursion
  10368. (save-restriction
  10369. (widen)
  10370. (while (org-up-heading-safe)
  10371. (when (looking-at org-complex-heading-regexp)
  10372. (push (org-trim
  10373. (replace-regexp-in-string
  10374. ;; Remove statistical/checkboxes cookies
  10375. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10376. (org-match-string-no-properties 4)))
  10377. rtn)))
  10378. rtn)))))
  10379. (defun org-format-outline-path (path &optional width prefix separator)
  10380. "Format the outline path PATH for display.
  10381. WIDTH is the maximum number of characters that is available.
  10382. PREFIX is a prefix to be included in the returned string,
  10383. such as the file name.
  10384. SEPARATOR is inserted between the different parts of the path,
  10385. the default is \"/\"."
  10386. (setq width (or width 79))
  10387. (if prefix (setq width (- width (length prefix))))
  10388. (if (not path)
  10389. (or prefix "")
  10390. (let* ((nsteps (length path))
  10391. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10392. (maxwidth (if (<= total-width width)
  10393. 10000 ;; everything fits
  10394. ;; we need to shorten the level headings
  10395. (/ (- width nsteps) nsteps)))
  10396. (org-odd-levels-only nil)
  10397. (n 0)
  10398. (total (1+ (length prefix))))
  10399. (setq maxwidth (max maxwidth 10))
  10400. (concat prefix
  10401. (if prefix (or separator "/"))
  10402. (mapconcat
  10403. (lambda (h)
  10404. (setq n (1+ n))
  10405. (if (and (= n nsteps) (< maxwidth 10000))
  10406. (setq maxwidth (- total-width total)))
  10407. (if (< (length h) maxwidth)
  10408. (progn (setq total (+ total (length h) 1)) h)
  10409. (setq h (substring h 0 (- maxwidth 2))
  10410. total (+ total maxwidth 1))
  10411. (if (string-match "[ \t]+\\'" h)
  10412. (setq h (substring h 0 (match-beginning 0))))
  10413. (setq h (concat h "..")))
  10414. (org-add-props h nil 'face
  10415. (nth (% (1- n) org-n-level-faces)
  10416. org-level-faces))
  10417. h)
  10418. path (or separator "/"))))))
  10419. (defun org-display-outline-path (&optional file current separator just-return-string)
  10420. "Display the current outline path in the echo area.
  10421. If FILE is non-nil, prepend the output with the file name.
  10422. If CURRENT is non-nil, append the current heading to the output.
  10423. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10424. the different parts of the path and defaults to \"/\".
  10425. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10426. (interactive "P")
  10427. (let* (case-fold-search
  10428. (bfn (buffer-file-name (buffer-base-buffer)))
  10429. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10430. res)
  10431. (if current (setq path (append path
  10432. (save-excursion
  10433. (org-back-to-heading t)
  10434. (if (looking-at org-complex-heading-regexp)
  10435. (list (match-string 4)))))))
  10436. (setq res
  10437. (org-format-outline-path
  10438. path
  10439. (1- (frame-width))
  10440. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10441. separator))
  10442. (if just-return-string
  10443. (org-no-properties res)
  10444. (org-unlogged-message "%s" res))))
  10445. (defvar org-refile-history nil
  10446. "History for refiling operations.")
  10447. (defvar org-after-refile-insert-hook nil
  10448. "Hook run after `org-refile' has inserted its stuff at the new location.
  10449. Note that this is still *before* the stuff will be removed from
  10450. the *old* location.")
  10451. (defvar org-capture-last-stored-marker)
  10452. (defvar org-refile-keep nil
  10453. "Non-nil means `org-refile' will copy instead of refile.")
  10454. (defun org-copy ()
  10455. "Like `org-refile', but copy."
  10456. (interactive)
  10457. (let ((org-refile-keep t))
  10458. (funcall 'org-refile nil nil nil "Copy")))
  10459. (defun org-refile (&optional goto default-buffer rfloc msg)
  10460. "Move the entry or entries at point to another heading.
  10461. The list of target headings is compiled using the information in
  10462. `org-refile-targets', which see.
  10463. At the target location, the entry is filed as a subitem of the target
  10464. heading. Depending on `org-reverse-note-order', the new subitem will
  10465. either be the first or the last subitem.
  10466. If there is an active region, all entries in that region will be moved.
  10467. However, the region must fulfill the requirement that the first heading
  10468. is the first one sets the top-level of the moved text - at most siblings
  10469. below it are allowed.
  10470. With prefix arg GOTO, the command will only visit the target location
  10471. and not actually move anything.
  10472. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10473. go to the location where the last refiling operation has put the subtree.
  10474. With a numeric prefix argument of `2', refile to the running clock.
  10475. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10476. being set to `t' and copy to the target location, don't move it.
  10477. Beware that keeping refiled entries may result in duplicated ID
  10478. properties.
  10479. RFLOC can be a refile location obtained in a different way.
  10480. MSG is a string to replace \"Refile\" in the default prompt with
  10481. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10482. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10483. If you are using target caching (see `org-refile-use-cache'),
  10484. you have to clear the target cache in order to find new targets.
  10485. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10486. prefix argument (`C-u C-u C-u C-c C-w')."
  10487. (interactive "P")
  10488. (if (member goto '(0 (64)))
  10489. (org-refile-cache-clear)
  10490. (let* ((actionmsg (or msg "Refile"))
  10491. (cbuf (current-buffer))
  10492. (regionp (org-region-active-p))
  10493. (region-start (and regionp (region-beginning)))
  10494. (region-end (and regionp (region-end)))
  10495. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10496. (org-refile-keep (if (equal goto 3) t org-refile-keep))
  10497. pos it nbuf file re level reversed)
  10498. (setq last-command nil)
  10499. (when regionp
  10500. (goto-char region-start)
  10501. (or (bolp) (goto-char (point-at-bol)))
  10502. (setq region-start (point))
  10503. (unless (or (org-kill-is-subtree-p
  10504. (buffer-substring region-start region-end))
  10505. (prog1 org-refile-active-region-within-subtree
  10506. (let ((s (point-at-eol)))
  10507. (org-toggle-heading)
  10508. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10509. (user-error "The region is not a (sequence of) subtree(s)")))
  10510. (if (equal goto '(16))
  10511. (org-refile-goto-last-stored)
  10512. (when (or
  10513. (and (equal goto 2)
  10514. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10515. (prog1
  10516. (setq it (list (or org-clock-heading "running clock")
  10517. (buffer-file-name
  10518. (marker-buffer org-clock-hd-marker))
  10519. ""
  10520. (marker-position org-clock-hd-marker)))
  10521. (setq goto nil)))
  10522. (setq it (or rfloc
  10523. (let (heading-text)
  10524. (save-excursion
  10525. (unless goto
  10526. (org-back-to-heading t)
  10527. (setq heading-text
  10528. (nth 4 (org-heading-components))))
  10529. (org-refile-get-location
  10530. (cond (goto "Goto")
  10531. (regionp (concat actionmsg " region to"))
  10532. (t (concat actionmsg " subtree \""
  10533. heading-text "\" to")))
  10534. default-buffer
  10535. (and (not (equal '(4) goto))
  10536. org-refile-allow-creating-parent-nodes)
  10537. goto))))))
  10538. (setq file (nth 1 it)
  10539. re (nth 2 it)
  10540. pos (nth 3 it))
  10541. (if (and (not goto)
  10542. pos
  10543. (equal (buffer-file-name) file)
  10544. (if regionp
  10545. (and (>= pos region-start)
  10546. (<= pos region-end))
  10547. (and (>= pos (point))
  10548. (< pos (save-excursion
  10549. (org-end-of-subtree t t))))))
  10550. (error "Cannot refile to position inside the tree or region"))
  10551. (setq nbuf (or (find-buffer-visiting file)
  10552. (find-file-noselect file)))
  10553. (if (and goto (not (equal goto 3)))
  10554. (progn
  10555. (org-pop-to-buffer-same-window nbuf)
  10556. (goto-char pos)
  10557. (org-show-context 'org-goto))
  10558. (if regionp
  10559. (progn
  10560. (org-kill-new (buffer-substring region-start region-end))
  10561. (org-save-markers-in-region region-start region-end))
  10562. (org-copy-subtree 1 nil t))
  10563. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10564. (find-file-noselect file)))
  10565. (setq reversed (org-notes-order-reversed-p))
  10566. (save-excursion
  10567. (save-restriction
  10568. (widen)
  10569. (if pos
  10570. (progn
  10571. (goto-char pos)
  10572. (looking-at org-outline-regexp)
  10573. (setq level (org-get-valid-level (funcall outline-level) 1))
  10574. (goto-char
  10575. (if reversed
  10576. (or (outline-next-heading) (point-max))
  10577. (or (save-excursion (org-get-next-sibling))
  10578. (org-end-of-subtree t t)
  10579. (point-max)))))
  10580. (setq level 1)
  10581. (if (not reversed)
  10582. (goto-char (point-max))
  10583. (goto-char (point-min))
  10584. (or (outline-next-heading) (goto-char (point-max)))))
  10585. (if (not (bolp)) (newline))
  10586. (org-paste-subtree level)
  10587. (when org-log-refile
  10588. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10589. (unless (eq org-log-refile 'note)
  10590. (save-excursion (org-add-log-note))))
  10591. (and org-auto-align-tags
  10592. (let ((org-loop-over-headlines-in-active-region nil))
  10593. (org-set-tags nil t)))
  10594. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10595. :last-refile)))
  10596. (when bookmark-name
  10597. (with-demoted-errors
  10598. (bookmark-set bookmark-name))))
  10599. ;; If we are refiling for capture, make sure that the
  10600. ;; last-capture pointers point here
  10601. (when (org-bound-and-true-p org-refile-for-capture)
  10602. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10603. :last-capture-marker)))
  10604. (when bookmark-name
  10605. (with-demoted-errors
  10606. (bookmark-set bookmark-name))))
  10607. (move-marker org-capture-last-stored-marker (point)))
  10608. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10609. (run-hooks 'org-after-refile-insert-hook))))
  10610. (unless org-refile-keep
  10611. (if regionp
  10612. (delete-region (point) (+ (point) (- region-end region-start)))
  10613. (delete-region
  10614. (and (org-back-to-heading t) (point))
  10615. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10616. (when (featurep 'org-inlinetask)
  10617. (org-inlinetask-remove-END-maybe))
  10618. (setq org-markers-to-move nil)
  10619. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10620. (defun org-refile-goto-last-stored ()
  10621. "Go to the location where the last refile was stored."
  10622. (interactive)
  10623. (bookmark-jump "org-refile-last-stored")
  10624. (message "This is the location of the last refile"))
  10625. (defun org-refile--get-location (refloc tbl)
  10626. "When user refile to REFLOC, find the associated target in TBL.
  10627. Also check `org-refile-target-table'."
  10628. (car (delq
  10629. nil
  10630. (mapcar
  10631. (lambda (r) (or (assoc r tbl)
  10632. (assoc r org-refile-target-table)))
  10633. (list (replace-regexp-in-string "/$" "" refloc)
  10634. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10635. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10636. no-exclude)
  10637. "Prompt the user for a refile location, using PROMPT.
  10638. PROMPT should not be suffixed with a colon and a space, because
  10639. this function appends the default value from
  10640. `org-refile-history' automatically, if that is not empty.
  10641. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10642. this is used for the GOTO interface."
  10643. (let ((org-refile-targets org-refile-targets)
  10644. (org-refile-use-outline-path org-refile-use-outline-path)
  10645. excluded-entries)
  10646. (when (and (derived-mode-p 'org-mode)
  10647. (not org-refile-use-cache)
  10648. (not no-exclude))
  10649. (org-map-tree
  10650. (lambda()
  10651. (setq excluded-entries
  10652. (append excluded-entries (list (org-get-heading t t)))))))
  10653. (setq org-refile-target-table
  10654. (org-refile-get-targets default-buffer excluded-entries)))
  10655. (unless org-refile-target-table
  10656. (user-error "No refile targets"))
  10657. (let* ((cbuf (current-buffer))
  10658. (partial-completion-mode nil)
  10659. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10660. (cfunc (if (and org-refile-use-outline-path
  10661. org-outline-path-complete-in-steps)
  10662. 'org-olpath-completing-read
  10663. 'org-icompleting-read))
  10664. (extra (if org-refile-use-outline-path "/" ""))
  10665. (cbnex (concat (buffer-name) extra))
  10666. (filename (and cfn (expand-file-name cfn)))
  10667. (tbl (mapcar
  10668. (lambda (x)
  10669. (if (and (not (member org-refile-use-outline-path
  10670. '(file full-file-path)))
  10671. (not (equal filename (nth 1 x))))
  10672. (cons (concat (car x) extra " ("
  10673. (file-name-nondirectory (nth 1 x)) ")")
  10674. (cdr x))
  10675. (cons (concat (car x) extra) (cdr x))))
  10676. org-refile-target-table))
  10677. (completion-ignore-case t)
  10678. cdef
  10679. (prompt (concat prompt
  10680. (or (and (car org-refile-history)
  10681. (concat " (default " (car org-refile-history) ")"))
  10682. (and (assoc cbnex tbl) (setq cdef cbnex)
  10683. (concat " (default " cbnex ")"))) ": "))
  10684. pa answ parent-target child parent old-hist)
  10685. (setq old-hist org-refile-history)
  10686. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10687. nil 'org-refile-history (or cdef (car org-refile-history))))
  10688. (if (setq pa (org-refile--get-location answ tbl))
  10689. (progn
  10690. (org-refile-check-position pa)
  10691. (when (or (not org-refile-history)
  10692. (not (eq old-hist org-refile-history))
  10693. (not (equal (car pa) (car org-refile-history))))
  10694. (setq org-refile-history
  10695. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10696. org-refile-history
  10697. (cdr org-refile-history))))
  10698. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10699. (pop org-refile-history)))
  10700. pa)
  10701. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10702. (progn
  10703. (setq parent (match-string 1 answ)
  10704. child (match-string 2 answ))
  10705. (setq parent-target (org-refile--get-location parent tbl))
  10706. (when (and parent-target
  10707. (or (eq new-nodes t)
  10708. (and (eq new-nodes 'confirm)
  10709. (y-or-n-p (format "Create new node \"%s\"? "
  10710. child)))))
  10711. (org-refile-new-child parent-target child)))
  10712. (user-error "Invalid target location")))))
  10713. (declare-function org-string-nw-p "org-macs" (s))
  10714. (defun org-refile-check-position (refile-pointer)
  10715. "Check if the refile pointer matches the headline to which it points."
  10716. (let* ((file (nth 1 refile-pointer))
  10717. (re (nth 2 refile-pointer))
  10718. (pos (nth 3 refile-pointer))
  10719. buffer)
  10720. (if (and (not (markerp pos)) (not file))
  10721. (if file
  10722. (user-error "Please save the buffer to a file before refiling")
  10723. (user-error "Please indicate a target file in the refile path"))
  10724. (when (org-string-nw-p re)
  10725. (setq buffer (if (markerp pos)
  10726. (marker-buffer pos)
  10727. (or (find-buffer-visiting file)
  10728. (find-file-noselect file))))
  10729. (with-current-buffer buffer
  10730. (save-excursion
  10731. (save-restriction
  10732. (widen)
  10733. (goto-char pos)
  10734. (beginning-of-line 1)
  10735. (unless (org-looking-at-p re)
  10736. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10737. (defun org-refile-new-child (parent-target child)
  10738. "Use refile target PARENT-TARGET to add new CHILD below it."
  10739. (unless parent-target
  10740. (error "Cannot find parent for new node"))
  10741. (let ((file (nth 1 parent-target))
  10742. (pos (nth 3 parent-target))
  10743. level)
  10744. (with-current-buffer (or (find-buffer-visiting file)
  10745. (find-file-noselect file))
  10746. (save-excursion
  10747. (save-restriction
  10748. (widen)
  10749. (if pos
  10750. (goto-char pos)
  10751. (goto-char (point-max))
  10752. (if (not (bolp)) (newline)))
  10753. (when (looking-at org-outline-regexp)
  10754. (setq level (funcall outline-level))
  10755. (org-end-of-subtree t t))
  10756. (org-back-over-empty-lines)
  10757. (insert "\n" (make-string
  10758. (if pos (org-get-valid-level level 1) 1) ?*)
  10759. " " child "\n")
  10760. (beginning-of-line 0)
  10761. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10762. (defun org-olpath-completing-read (prompt collection &rest args)
  10763. "Read an outline path like a file name."
  10764. (let ((thetable collection)
  10765. (org-completion-use-ido nil) ; does not work with ido.
  10766. (org-completion-use-iswitchb nil)) ; or iswitchb
  10767. (apply
  10768. 'org-icompleting-read prompt
  10769. (lambda (string predicate &optional flag)
  10770. (let (rtn r f (l (length string)))
  10771. (cond
  10772. ((eq flag nil)
  10773. ;; try completion
  10774. (try-completion string thetable))
  10775. ((eq flag t)
  10776. ;; all-completions
  10777. (setq rtn (all-completions string thetable predicate))
  10778. (mapcar
  10779. (lambda (x)
  10780. (setq r (substring x l))
  10781. (if (string-match " ([^)]*)$" x)
  10782. (setq f (match-string 0 x))
  10783. (setq f ""))
  10784. (if (string-match "/" r)
  10785. (concat string (substring r 0 (match-end 0)) f)
  10786. x))
  10787. rtn))
  10788. ((eq flag 'lambda)
  10789. ;; exact match?
  10790. (assoc string thetable)))))
  10791. args)))
  10792. ;;;; Dynamic blocks
  10793. (defun org-find-dblock (name)
  10794. "Find the first dynamic block with name NAME in the buffer.
  10795. If not found, stay at current position and return nil."
  10796. (let ((case-fold-search t) pos)
  10797. (save-excursion
  10798. (goto-char (point-min))
  10799. (setq pos (and (re-search-forward
  10800. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10801. (match-beginning 0))))
  10802. (if pos (goto-char pos))
  10803. pos))
  10804. (defun org-create-dblock (plist)
  10805. "Create a dynamic block section, with parameters taken from PLIST.
  10806. PLIST must contain a :name entry which is used as name of the block."
  10807. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10808. (end-of-line 1)
  10809. (newline))
  10810. (let ((col (current-column))
  10811. (name (plist-get plist :name)))
  10812. (insert "#+BEGIN: " name)
  10813. (while plist
  10814. (if (eq (car plist) :name)
  10815. (setq plist (cddr plist))
  10816. (insert " " (prin1-to-string (pop plist)))))
  10817. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10818. (beginning-of-line -2)))
  10819. (defun org-prepare-dblock ()
  10820. "Prepare dynamic block for refresh.
  10821. This empties the block, puts the cursor at the insert position and returns
  10822. the property list including an extra property :name with the block name."
  10823. (unless (looking-at org-dblock-start-re)
  10824. (user-error "Not at a dynamic block"))
  10825. (let* ((begdel (1+ (match-end 0)))
  10826. (name (org-no-properties (match-string 1)))
  10827. (params (append (list :name name)
  10828. (read (concat "(" (match-string 3) ")")))))
  10829. (save-excursion
  10830. (beginning-of-line 1)
  10831. (skip-chars-forward " \t")
  10832. (setq params (plist-put params :indentation-column (current-column))))
  10833. (unless (re-search-forward org-dblock-end-re nil t)
  10834. (error "Dynamic block not terminated"))
  10835. (setq params
  10836. (append params
  10837. (list :content (buffer-substring
  10838. begdel (match-beginning 0)))))
  10839. (delete-region begdel (match-beginning 0))
  10840. (goto-char begdel)
  10841. (open-line 1)
  10842. params))
  10843. (defun org-map-dblocks (&optional command)
  10844. "Apply COMMAND to all dynamic blocks in the current buffer.
  10845. If COMMAND is not given, use `org-update-dblock'."
  10846. (let ((cmd (or command 'org-update-dblock)))
  10847. (save-excursion
  10848. (goto-char (point-min))
  10849. (while (re-search-forward org-dblock-start-re nil t)
  10850. (goto-char (match-beginning 0))
  10851. (save-excursion
  10852. (condition-case nil
  10853. (funcall cmd)
  10854. (error (message "Error during update of dynamic block"))))
  10855. (unless (re-search-forward org-dblock-end-re nil t)
  10856. (error "Dynamic block not terminated"))))))
  10857. (defun org-dblock-update (&optional arg)
  10858. "User command for updating dynamic blocks.
  10859. Update the dynamic block at point. With prefix ARG, update all dynamic
  10860. blocks in the buffer."
  10861. (interactive "P")
  10862. (if arg
  10863. (org-update-all-dblocks)
  10864. (or (looking-at org-dblock-start-re)
  10865. (org-beginning-of-dblock))
  10866. (org-update-dblock)))
  10867. (defun org-update-dblock ()
  10868. "Update the dynamic block at point.
  10869. This means to empty the block, parse for parameters and then call
  10870. the correct writing function."
  10871. (interactive)
  10872. (save-window-excursion
  10873. (let* ((pos (point))
  10874. (line (org-current-line))
  10875. (params (org-prepare-dblock))
  10876. (name (plist-get params :name))
  10877. (indent (plist-get params :indentation-column))
  10878. (cmd (intern (concat "org-dblock-write:" name))))
  10879. (message "Updating dynamic block `%s' at line %d..." name line)
  10880. (funcall cmd params)
  10881. (message "Updating dynamic block `%s' at line %d...done" name line)
  10882. (goto-char pos)
  10883. (when (and indent (> indent 0))
  10884. (setq indent (make-string indent ?\ ))
  10885. (save-excursion
  10886. (org-beginning-of-dblock)
  10887. (forward-line 1)
  10888. (while (not (looking-at org-dblock-end-re))
  10889. (insert indent)
  10890. (beginning-of-line 2))
  10891. (when (looking-at org-dblock-end-re)
  10892. (and (looking-at "[ \t]+")
  10893. (replace-match ""))
  10894. (insert indent)))))))
  10895. (defun org-beginning-of-dblock ()
  10896. "Find the beginning of the dynamic block at point.
  10897. Error if there is no such block at point."
  10898. (let ((pos (point))
  10899. beg)
  10900. (end-of-line 1)
  10901. (if (and (re-search-backward org-dblock-start-re nil t)
  10902. (setq beg (match-beginning 0))
  10903. (re-search-forward org-dblock-end-re nil t)
  10904. (> (match-end 0) pos))
  10905. (goto-char beg)
  10906. (goto-char pos)
  10907. (error "Not in a dynamic block"))))
  10908. (defun org-update-all-dblocks ()
  10909. "Update all dynamic blocks in the buffer.
  10910. This function can be used in a hook."
  10911. (interactive)
  10912. (when (derived-mode-p 'org-mode)
  10913. (org-map-dblocks 'org-update-dblock)))
  10914. ;;;; Completion
  10915. (declare-function org-export-backend-name "org-export" (cl-x))
  10916. (declare-function org-export-backend-options "org-export" (cl-x))
  10917. (defun org-get-export-keywords ()
  10918. "Return a list of all currently understood export keywords.
  10919. Export keywords include options, block names, attributes and
  10920. keywords relative to each registered export back-end."
  10921. (let (keywords)
  10922. (dolist (backend
  10923. (org-bound-and-true-p org-export--registered-backends)
  10924. (delq nil keywords))
  10925. ;; Back-end name (for keywords, like #+LATEX:)
  10926. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  10927. (dolist (option-entry (org-export-backend-options backend))
  10928. ;; Back-end options.
  10929. (push (nth 1 option-entry) keywords)))))
  10930. (defconst org-options-keywords
  10931. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10932. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10933. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10934. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10935. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10936. (defcustom org-structure-template-alist
  10937. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC" "<src lang=\"?\">\n\n</src>")
  10938. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE" "<example>\n?\n</example>")
  10939. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE" "<quote>\n?\n</quote>")
  10940. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE" "<verse>\n?\n</verse>")
  10941. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM" "<verbatim>\n?\n</verbatim>")
  10942. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER" "<center>\n?\n</center>")
  10943. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10944. "<literal style=\"latex\">\n?\n</literal>")
  10945. ("L" "#+LaTeX: " "<literal style=\"latex\">?</literal>")
  10946. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10947. "<literal style=\"html\">\n?\n</literal>")
  10948. ("H" "#+HTML: " "<literal style=\"html\">?</literal>")
  10949. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII" "")
  10950. ("A" "#+ASCII: " "")
  10951. ("i" "#+INDEX: ?" "#+INDEX: ?")
  10952. ("I" "#+INCLUDE: %file ?"
  10953. "<include file=%file markup=\"?\">"))
  10954. "Structure completion elements.
  10955. This is a list of abbreviation keys and values. The value gets inserted
  10956. if you type `<' followed by the key and then press the completion key,
  10957. usually `M-TAB'. %file will be replaced by a file name after prompting
  10958. for the file using completion. The cursor will be placed at the position
  10959. of the `?` in the template.
  10960. There are two templates for each key, the first uses the original Org syntax,
  10961. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10962. the default when the /org-mtags.el/ module has been loaded. See also the
  10963. variable `org-mtags-prefer-muse-templates'."
  10964. :group 'org-completion
  10965. :type '(repeat
  10966. (list
  10967. (string :tag "Key")
  10968. (string :tag "Template")
  10969. (string :tag "Muse Template"))))
  10970. (defun org-try-structure-completion ()
  10971. "Try to complete a structure template before point.
  10972. This looks for strings like \"<e\" on an otherwise empty line and
  10973. expands them."
  10974. (let ((l (buffer-substring (point-at-bol) (point)))
  10975. a)
  10976. (when (and (looking-at "[ \t]*$")
  10977. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10978. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10979. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10980. (match-beginning 1)) a)
  10981. t)))
  10982. (defun org-complete-expand-structure-template (start cell)
  10983. "Expand a structure template."
  10984. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10985. (rpl (nth (if musep 2 1) cell))
  10986. (ind ""))
  10987. (delete-region start (point))
  10988. (when (string-match "\\`#\\+" rpl)
  10989. (cond
  10990. ((bolp))
  10991. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10992. (setq ind (buffer-substring (point-at-bol) (point))))
  10993. (t (newline))))
  10994. (setq start (point))
  10995. (if (string-match "%file" rpl)
  10996. (setq rpl (replace-match
  10997. (concat
  10998. "\""
  10999. (save-match-data
  11000. (abbreviate-file-name (read-file-name "Include file: ")))
  11001. "\"")
  11002. t t rpl)))
  11003. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11004. (concat "\n" ind)))
  11005. (insert rpl)
  11006. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11007. ;;;; TODO, DEADLINE, Comments
  11008. (defun org-toggle-comment ()
  11009. "Change the COMMENT state of an entry."
  11010. (interactive)
  11011. (save-excursion
  11012. (org-back-to-heading)
  11013. (let (case-fold-search)
  11014. (cond
  11015. ((looking-at (format org-heading-keyword-regexp-format
  11016. org-comment-string))
  11017. (goto-char (match-end 1))
  11018. (looking-at (concat " +" org-comment-string))
  11019. (replace-match "" t t)
  11020. (when (eolp) (insert " ")))
  11021. ((looking-at org-outline-regexp)
  11022. (goto-char (match-end 0))
  11023. (insert org-comment-string " "))))))
  11024. (defvar org-last-todo-state-is-todo nil
  11025. "This is non-nil when the last TODO state change led to a TODO state.
  11026. If the last change removed the TODO tag or switched to DONE, then
  11027. this is nil.")
  11028. (defvar org-setting-tags nil) ; dynamically skipped
  11029. (defvar org-todo-setup-filter-hook nil
  11030. "Hook for functions that pre-filter todo specs.
  11031. Each function takes a todo spec and returns either nil or the spec
  11032. transformed into canonical form." )
  11033. (defvar org-todo-get-default-hook nil
  11034. "Hook for functions that get a default item for todo.
  11035. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11036. nil or a string to be used for the todo mark." )
  11037. (defvar org-agenda-headline-snapshot-before-repeat)
  11038. (defun org-current-effective-time ()
  11039. "Return current time adjusted for `org-extend-today-until' variable."
  11040. (let* ((ct (org-current-time))
  11041. (dct (decode-time ct))
  11042. (ct1
  11043. (cond
  11044. (org-use-last-clock-out-time-as-effective-time
  11045. (or (org-clock-get-last-clock-out-time) ct))
  11046. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11047. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11048. (t ct))))
  11049. ct1))
  11050. (defun org-todo-yesterday (&optional arg)
  11051. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11052. (interactive "P")
  11053. (if (eq major-mode 'org-agenda-mode)
  11054. (apply 'org-agenda-todo-yesterday arg)
  11055. (let* ((hour (third (decode-time
  11056. (org-current-time))))
  11057. (org-extend-today-until (1+ hour)))
  11058. (org-todo arg))))
  11059. (defvar org-block-entry-blocking ""
  11060. "First entry preventing the TODO state change.")
  11061. (defun org-cancel-repeater ()
  11062. "Cancel a repeater by setting its numeric value to zero."
  11063. (interactive)
  11064. (save-excursion
  11065. (org-back-to-heading t)
  11066. (let ((bound1 (point))
  11067. (bound0 (save-excursion (outline-next-heading) (point))))
  11068. (when (re-search-forward
  11069. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11070. org-deadline-time-regexp "\\)\\|\\("
  11071. org-ts-regexp "\\)")
  11072. bound0 t)
  11073. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11074. (replace-match "0" t nil nil 1))))))
  11075. (defun org-todo (&optional arg)
  11076. "Change the TODO state of an item.
  11077. The state of an item is given by a keyword at the start of the heading,
  11078. like
  11079. *** TODO Write paper
  11080. *** DONE Call mom
  11081. The different keywords are specified in the variable `org-todo-keywords'.
  11082. By default the available states are \"TODO\" and \"DONE\".
  11083. So for this example: when the item starts with TODO, it is changed to DONE.
  11084. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11085. DONE are present, add TODO at the beginning of the heading.
  11086. With \\[universal-argument] prefix arg, use completion to determine the new \
  11087. state.
  11088. With numeric prefix arg, switch to that state.
  11089. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11090. keywords (nextset).
  11091. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11092. With a numeric prefix arg of 0, inhibit note taking for the change.
  11093. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11094. When called through ELisp, arg is also interpreted in the following way:
  11095. 'none -> empty state
  11096. \"\"(empty string) -> switch to empty state
  11097. 'done -> switch to DONE
  11098. 'nextset -> switch to the next set of keywords
  11099. 'previousset -> switch to the previous set of keywords
  11100. \"WAITING\" -> switch to the specified keyword, but only if it
  11101. really is a member of `org-todo-keywords'."
  11102. (interactive "P")
  11103. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11104. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11105. 'region-start-level 'region))
  11106. org-loop-over-headlines-in-active-region)
  11107. (org-map-entries
  11108. `(org-todo ,arg)
  11109. org-loop-over-headlines-in-active-region
  11110. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11111. (if (equal arg '(16)) (setq arg 'nextset))
  11112. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11113. (let ((org-blocker-hook org-blocker-hook)
  11114. commentp
  11115. case-fold-search)
  11116. (when (equal arg '(64))
  11117. (setq arg nil org-blocker-hook nil))
  11118. (when (and org-blocker-hook
  11119. (or org-inhibit-blocking
  11120. (org-entry-get nil "NOBLOCKING")))
  11121. (setq org-blocker-hook nil))
  11122. (save-excursion
  11123. (catch 'exit
  11124. (org-back-to-heading t)
  11125. (when (looking-at (concat "^\\*+ " org-comment-string))
  11126. (org-toggle-comment)
  11127. (setq commentp t))
  11128. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11129. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11130. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11131. (let* ((match-data (match-data))
  11132. (startpos (point-at-bol))
  11133. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11134. (org-log-done org-log-done)
  11135. (org-log-repeat org-log-repeat)
  11136. (org-todo-log-states org-todo-log-states)
  11137. (org-inhibit-logging
  11138. (if (equal arg 0)
  11139. (progn (setq arg nil) 'note) org-inhibit-logging))
  11140. (this (match-string 1))
  11141. (hl-pos (match-beginning 0))
  11142. (head (org-get-todo-sequence-head this))
  11143. (ass (assoc head org-todo-kwd-alist))
  11144. (interpret (nth 1 ass))
  11145. (done-word (nth 3 ass))
  11146. (final-done-word (nth 4 ass))
  11147. (org-last-state (or this ""))
  11148. (completion-ignore-case t)
  11149. (member (member this org-todo-keywords-1))
  11150. (tail (cdr member))
  11151. (org-state (cond
  11152. ((and org-todo-key-trigger
  11153. (or (and (equal arg '(4))
  11154. (eq org-use-fast-todo-selection 'prefix))
  11155. (and (not arg) org-use-fast-todo-selection
  11156. (not (eq org-use-fast-todo-selection
  11157. 'prefix)))))
  11158. ;; Use fast selection
  11159. (org-fast-todo-selection))
  11160. ((and (equal arg '(4))
  11161. (or (not org-use-fast-todo-selection)
  11162. (not org-todo-key-trigger)))
  11163. ;; Read a state with completion
  11164. (org-icompleting-read
  11165. "State: " (mapcar 'list org-todo-keywords-1)
  11166. nil t))
  11167. ((eq arg 'right)
  11168. (if this
  11169. (if tail (car tail) nil)
  11170. (car org-todo-keywords-1)))
  11171. ((eq arg 'left)
  11172. (if (equal member org-todo-keywords-1)
  11173. nil
  11174. (if this
  11175. (nth (- (length org-todo-keywords-1)
  11176. (length tail) 2)
  11177. org-todo-keywords-1)
  11178. (org-last org-todo-keywords-1))))
  11179. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11180. (setq arg nil))) ; hack to fall back to cycling
  11181. (arg
  11182. ;; user or caller requests a specific state
  11183. (cond
  11184. ((equal arg "") nil)
  11185. ((eq arg 'none) nil)
  11186. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11187. ((eq arg 'nextset)
  11188. (or (car (cdr (member head org-todo-heads)))
  11189. (car org-todo-heads)))
  11190. ((eq arg 'previousset)
  11191. (let ((org-todo-heads (reverse org-todo-heads)))
  11192. (or (car (cdr (member head org-todo-heads)))
  11193. (car org-todo-heads))))
  11194. ((car (member arg org-todo-keywords-1)))
  11195. ((stringp arg)
  11196. (user-error "State `%s' not valid in this file" arg))
  11197. ((nth (1- (prefix-numeric-value arg))
  11198. org-todo-keywords-1))))
  11199. ((null member) (or head (car org-todo-keywords-1)))
  11200. ((equal this final-done-word) nil) ;; -> make empty
  11201. ((null tail) nil) ;; -> first entry
  11202. ((memq interpret '(type priority))
  11203. (if (eq this-command last-command)
  11204. (car tail)
  11205. (if (> (length tail) 0)
  11206. (or done-word (car org-done-keywords))
  11207. nil)))
  11208. (t
  11209. (car tail))))
  11210. (org-state (or
  11211. (run-hook-with-args-until-success
  11212. 'org-todo-get-default-hook org-state org-last-state)
  11213. org-state))
  11214. (next (if org-state (concat " " org-state " ") " "))
  11215. (change-plist (list :type 'todo-state-change :from this :to org-state
  11216. :position startpos))
  11217. dolog now-done-p)
  11218. (when org-blocker-hook
  11219. (setq org-last-todo-state-is-todo
  11220. (not (member this org-done-keywords)))
  11221. (unless (save-excursion
  11222. (save-match-data
  11223. (org-with-wide-buffer
  11224. (run-hook-with-args-until-failure
  11225. 'org-blocker-hook change-plist))))
  11226. (if (org-called-interactively-p 'interactive)
  11227. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11228. this org-state org-block-entry-blocking)
  11229. ;; fail silently
  11230. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11231. this org-state org-block-entry-blocking)
  11232. (throw 'exit nil))))
  11233. (store-match-data match-data)
  11234. (replace-match next t t)
  11235. (unless (pos-visible-in-window-p hl-pos)
  11236. (message "TODO state changed to %s" (org-trim next)))
  11237. (unless head
  11238. (setq head (org-get-todo-sequence-head org-state)
  11239. ass (assoc head org-todo-kwd-alist)
  11240. interpret (nth 1 ass)
  11241. done-word (nth 3 ass)
  11242. final-done-word (nth 4 ass)))
  11243. (when (memq arg '(nextset previousset))
  11244. (message "Keyword-Set %d/%d: %s"
  11245. (- (length org-todo-sets) -1
  11246. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11247. (length org-todo-sets)
  11248. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11249. (setq org-last-todo-state-is-todo
  11250. (not (member org-state org-done-keywords)))
  11251. (setq now-done-p (and (member org-state org-done-keywords)
  11252. (not (member this org-done-keywords))))
  11253. (and logging (org-local-logging logging))
  11254. (when (and (or org-todo-log-states org-log-done)
  11255. (not (eq org-inhibit-logging t))
  11256. (not (memq arg '(nextset previousset))))
  11257. ;; we need to look at recording a time and note
  11258. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11259. (nth 2 (assoc this org-todo-log-states))))
  11260. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11261. (setq dolog 'time))
  11262. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11263. (and org-state
  11264. (member org-state org-not-done-keywords)
  11265. (not (member this org-not-done-keywords))))
  11266. ;; This is now a todo state and was not one before
  11267. ;; If there was a CLOSED time stamp, get rid of it.
  11268. (org-add-planning-info nil nil 'closed))
  11269. (when (and now-done-p org-log-done)
  11270. ;; It is now done, and it was not done before
  11271. (org-add-planning-info 'closed (org-current-effective-time))
  11272. (if (and (not dolog) (eq 'note org-log-done))
  11273. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11274. (when (and org-state dolog)
  11275. ;; This is a non-nil state, and we need to log it
  11276. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11277. ;; Fixup tag positioning
  11278. (org-todo-trigger-tag-changes org-state)
  11279. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11280. (when org-provide-todo-statistics
  11281. (org-update-parent-todo-statistics))
  11282. (run-hooks 'org-after-todo-state-change-hook)
  11283. (if (and arg (not (member org-state org-done-keywords)))
  11284. (setq head (org-get-todo-sequence-head org-state)))
  11285. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11286. ;; Do we need to trigger a repeat?
  11287. (when now-done-p
  11288. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11289. ;; This is for the agenda, take a snapshot of the headline.
  11290. (save-match-data
  11291. (setq org-agenda-headline-snapshot-before-repeat
  11292. (org-get-heading))))
  11293. (org-auto-repeat-maybe org-state))
  11294. ;; Fixup cursor location if close to the keyword
  11295. (if (and (outline-on-heading-p)
  11296. (not (bolp))
  11297. (save-excursion (beginning-of-line 1)
  11298. (looking-at org-todo-line-regexp))
  11299. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11300. (progn
  11301. (goto-char (or (match-end 2) (match-end 1)))
  11302. (and (looking-at " ") (just-one-space))))
  11303. (when org-trigger-hook
  11304. (save-excursion
  11305. (run-hook-with-args 'org-trigger-hook change-plist)))
  11306. (when commentp (org-toggle-comment))))))))
  11307. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11308. "Block turning an entry into a TODO, using the hierarchy.
  11309. This checks whether the current task should be blocked from state
  11310. changes. Such blocking occurs when:
  11311. 1. The task has children which are not all in a completed state.
  11312. 2. A task has a parent with the property :ORDERED:, and there
  11313. are siblings prior to the current task with incomplete
  11314. status.
  11315. 3. The parent of the task is blocked because it has siblings that should
  11316. be done first, or is child of a block grandparent TODO entry."
  11317. (if (not org-enforce-todo-dependencies)
  11318. t ; if locally turned off don't block
  11319. (catch 'dont-block
  11320. ;; If this is not a todo state change, or if this entry is already DONE,
  11321. ;; do not block
  11322. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11323. (member (plist-get change-plist :from)
  11324. (cons 'done org-done-keywords))
  11325. (member (plist-get change-plist :to)
  11326. (cons 'todo org-not-done-keywords))
  11327. (not (plist-get change-plist :to)))
  11328. (throw 'dont-block t))
  11329. ;; If this task has children, and any are undone, it's blocked
  11330. (save-excursion
  11331. (org-back-to-heading t)
  11332. (let ((this-level (funcall outline-level)))
  11333. (outline-next-heading)
  11334. (let ((child-level (funcall outline-level)))
  11335. (while (and (not (eobp))
  11336. (> child-level this-level))
  11337. ;; this todo has children, check whether they are all
  11338. ;; completed
  11339. (if (and (not (org-entry-is-done-p))
  11340. (org-entry-is-todo-p))
  11341. (progn (setq org-block-entry-blocking (org-get-heading))
  11342. (throw 'dont-block nil)))
  11343. (outline-next-heading)
  11344. (setq child-level (funcall outline-level))))))
  11345. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11346. ;; any previous siblings are undone, it's blocked
  11347. (save-excursion
  11348. (org-back-to-heading t)
  11349. (let* ((pos (point))
  11350. (parent-pos (and (org-up-heading-safe) (point))))
  11351. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11352. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11353. (forward-line 1)
  11354. (re-search-forward org-not-done-heading-regexp pos t))
  11355. (setq org-block-entry-blocking (match-string 0))
  11356. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11357. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11358. (while t
  11359. (goto-char parent-pos)
  11360. (if (not (looking-at org-not-done-heading-regexp))
  11361. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11362. (setq pos (point))
  11363. (setq parent-pos (and (org-up-heading-safe) (point)))
  11364. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11365. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11366. (forward-line 1)
  11367. (re-search-forward org-not-done-heading-regexp pos t)
  11368. (setq org-block-entry-blocking (org-get-heading)))
  11369. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11370. (defcustom org-track-ordered-property-with-tag nil
  11371. "Should the ORDERED property also be shown as a tag?
  11372. The ORDERED property decides if an entry should require subtasks to be
  11373. completed in sequence. Since a property is not very visible, setting
  11374. this option means that toggling the ORDERED property with the command
  11375. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11376. not relevant for the behavior, but it makes things more visible.
  11377. Note that toggling the tag with tags commands will not change the property
  11378. and therefore not influence behavior!
  11379. This can be t, meaning the tag ORDERED should be used, It can also be a
  11380. string to select a different tag for this task."
  11381. :group 'org-todo
  11382. :type '(choice
  11383. (const :tag "No tracking" nil)
  11384. (const :tag "Track with ORDERED tag" t)
  11385. (string :tag "Use other tag")))
  11386. (defun org-toggle-ordered-property ()
  11387. "Toggle the ORDERED property of the current entry.
  11388. For better visibility, you can track the value of this property with a tag.
  11389. See variable `org-track-ordered-property-with-tag'."
  11390. (interactive)
  11391. (let* ((t1 org-track-ordered-property-with-tag)
  11392. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11393. (save-excursion
  11394. (org-back-to-heading)
  11395. (if (org-entry-get nil "ORDERED")
  11396. (progn
  11397. (org-delete-property "ORDERED")
  11398. (and tag (org-toggle-tag tag 'off))
  11399. (message "Subtasks can be completed in arbitrary order"))
  11400. (org-entry-put nil "ORDERED" "t")
  11401. (and tag (org-toggle-tag tag 'on))
  11402. (message "Subtasks must be completed in sequence")))))
  11403. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11404. (defun org-block-todo-from-checkboxes (change-plist)
  11405. "Block turning an entry into a TODO, using checkboxes.
  11406. This checks whether the current task should be blocked from state
  11407. changes because there are unchecked boxes in this entry."
  11408. (if (not org-enforce-todo-checkbox-dependencies)
  11409. t ; if locally turned off don't block
  11410. (catch 'dont-block
  11411. ;; If this is not a todo state change, or if this entry is already DONE,
  11412. ;; do not block
  11413. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11414. (member (plist-get change-plist :from)
  11415. (cons 'done org-done-keywords))
  11416. (member (plist-get change-plist :to)
  11417. (cons 'todo org-not-done-keywords))
  11418. (not (plist-get change-plist :to)))
  11419. (throw 'dont-block t))
  11420. ;; If this task has checkboxes that are not checked, it's blocked
  11421. (save-excursion
  11422. (org-back-to-heading t)
  11423. (let ((beg (point)) end)
  11424. (outline-next-heading)
  11425. (setq end (point))
  11426. (goto-char beg)
  11427. (if (org-list-search-forward
  11428. (concat (org-item-beginning-re)
  11429. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11430. "\\[[- ]\\]")
  11431. end t)
  11432. (progn
  11433. (if (boundp 'org-blocked-by-checkboxes)
  11434. (setq org-blocked-by-checkboxes t))
  11435. (throw 'dont-block nil)))))
  11436. t))) ; do not block
  11437. (defun org-entry-blocked-p ()
  11438. "Is the current entry blocked?"
  11439. (org-with-silent-modifications
  11440. (if (org-entry-get nil "NOBLOCKING")
  11441. nil ;; Never block this entry
  11442. (not (run-hook-with-args-until-failure
  11443. 'org-blocker-hook
  11444. (list :type 'todo-state-change
  11445. :position (point)
  11446. :from 'todo
  11447. :to 'done))))))
  11448. (defun org-update-statistics-cookies (all)
  11449. "Update the statistics cookie, either from TODO or from checkboxes.
  11450. This should be called with the cursor in a line with a statistics cookie."
  11451. (interactive "P")
  11452. (if all
  11453. (progn
  11454. (org-update-checkbox-count 'all)
  11455. (org-map-entries 'org-update-parent-todo-statistics))
  11456. (if (not (org-at-heading-p))
  11457. (org-update-checkbox-count)
  11458. (let ((pos (point-marker))
  11459. end l1 l2)
  11460. (ignore-errors (org-back-to-heading t))
  11461. (if (not (org-at-heading-p))
  11462. (org-update-checkbox-count)
  11463. (setq l1 (org-outline-level))
  11464. (setq end (save-excursion
  11465. (outline-next-heading)
  11466. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11467. (point)))
  11468. (if (and (save-excursion
  11469. (re-search-forward
  11470. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11471. (not (save-excursion (re-search-forward
  11472. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11473. (org-update-checkbox-count)
  11474. (if (and l2 (> l2 l1))
  11475. (progn
  11476. (goto-char end)
  11477. (org-update-parent-todo-statistics))
  11478. (goto-char pos)
  11479. (beginning-of-line 1)
  11480. (while (re-search-forward
  11481. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11482. (point-at-eol) t)
  11483. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11484. (goto-char pos)
  11485. (move-marker pos nil)))))
  11486. (defvar org-entry-property-inherited-from) ;; defined below
  11487. (defun org-update-parent-todo-statistics ()
  11488. "Update any statistics cookie in the parent of the current headline.
  11489. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11490. the entire subtree and this will travel up the hierarchy and update
  11491. statistics everywhere."
  11492. (let* ((prop (save-excursion (org-up-heading-safe)
  11493. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11494. (recursive (or (not org-hierarchical-todo-statistics)
  11495. (and prop (string-match "\\<recursive\\>" prop))))
  11496. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11497. 0))
  11498. (first t)
  11499. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11500. level ltoggle l1 new ndel
  11501. (cnt-all 0) (cnt-done 0) is-percent kwd
  11502. checkbox-beg ov ovs ove cookie-present)
  11503. (catch 'exit
  11504. (save-excursion
  11505. (beginning-of-line 1)
  11506. (setq ltoggle (funcall outline-level))
  11507. ;; Three situations are to consider:
  11508. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11509. ;; to the top-level ancestor on the headline;
  11510. ;; 2. If parent has "recursive" property, repeat up to the
  11511. ;; headline setting that property, taking inheritance into
  11512. ;; account;
  11513. ;; 3. Else, move up to direct parent and proceed only once.
  11514. (while (and (setq level (org-up-heading-safe))
  11515. (or recursive first)
  11516. (>= (point) lim))
  11517. (setq first nil cookie-present nil)
  11518. (unless (and level
  11519. (not (string-match
  11520. "\\<checkbox\\>"
  11521. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11522. "")))))
  11523. (throw 'exit nil))
  11524. (while (re-search-forward box-re (point-at-eol) t)
  11525. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11526. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11527. (save-match-data
  11528. (unless (outline-next-heading) (throw 'exit nil))
  11529. (while (and (looking-at org-complex-heading-regexp)
  11530. (> (setq l1 (length (match-string 1))) level))
  11531. (setq kwd (and (or recursive (= l1 ltoggle))
  11532. (match-string 2)))
  11533. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11534. (and (eq org-provide-todo-statistics t)
  11535. (or (member kwd org-done-keywords)))
  11536. (and (listp org-provide-todo-statistics)
  11537. (stringp (car org-provide-todo-statistics))
  11538. (or (member kwd org-provide-todo-statistics)
  11539. (member kwd org-done-keywords)))
  11540. (and (listp org-provide-todo-statistics)
  11541. (listp (car org-provide-todo-statistics))
  11542. (or (member kwd (car org-provide-todo-statistics))
  11543. (and (member kwd org-done-keywords)
  11544. (member kwd (cadr org-provide-todo-statistics))))))
  11545. (setq cnt-all (1+ cnt-all))
  11546. (if (eq org-provide-todo-statistics t)
  11547. (and kwd (setq cnt-all (1+ cnt-all)))))
  11548. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11549. (member kwd org-done-keywords))
  11550. (and (listp org-provide-todo-statistics)
  11551. (listp (car org-provide-todo-statistics))
  11552. (member kwd org-done-keywords)
  11553. (member kwd (cadr org-provide-todo-statistics)))
  11554. (and (listp org-provide-todo-statistics)
  11555. (stringp (car org-provide-todo-statistics))
  11556. (member kwd org-done-keywords)))
  11557. (setq cnt-done (1+ cnt-done)))
  11558. (outline-next-heading)))
  11559. (setq new
  11560. (if is-percent
  11561. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11562. (format "[%d/%d]" cnt-done cnt-all))
  11563. ndel (- (match-end 0) checkbox-beg))
  11564. ;; handle overlays when updating cookie from column view
  11565. (when (setq ov (car (overlays-at checkbox-beg)))
  11566. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11567. (delete-overlay ov))
  11568. (goto-char checkbox-beg)
  11569. (insert new)
  11570. (delete-region (point) (+ (point) ndel))
  11571. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11572. (when ov (move-overlay ov ovs ove)))
  11573. (when cookie-present
  11574. (run-hook-with-args 'org-after-todo-statistics-hook
  11575. cnt-done (- cnt-all cnt-done))))))
  11576. (run-hooks 'org-todo-statistics-hook)))
  11577. (defvar org-after-todo-statistics-hook nil
  11578. "Hook that is called after a TODO statistics cookie has been updated.
  11579. Each function is called with two arguments: the number of not-done entries
  11580. and the number of done entries.
  11581. For example, the following function, when added to this hook, will switch
  11582. an entry to DONE when all children are done, and back to TODO when new
  11583. entries are set to a TODO status. Note that this hook is only called
  11584. when there is a statistics cookie in the headline!
  11585. (defun org-summary-todo (n-done n-not-done)
  11586. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11587. (let (org-log-done org-log-states) ; turn off logging
  11588. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11589. ")
  11590. (defvar org-todo-statistics-hook nil
  11591. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11592. This hook runs even if there is no statistics cookie present, in which case
  11593. `org-after-todo-statistics-hook' would not run.")
  11594. (defun org-todo-trigger-tag-changes (state)
  11595. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11596. (let ((l org-todo-state-tags-triggers)
  11597. changes)
  11598. (when (or (not state) (equal state ""))
  11599. (setq changes (append changes (cdr (assoc "" l)))))
  11600. (when (and (stringp state) (> (length state) 0))
  11601. (setq changes (append changes (cdr (assoc state l)))))
  11602. (when (member state org-not-done-keywords)
  11603. (setq changes (append changes (cdr (assoc 'todo l)))))
  11604. (when (member state org-done-keywords)
  11605. (setq changes (append changes (cdr (assoc 'done l)))))
  11606. (dolist (c changes)
  11607. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11608. (defun org-local-logging (value)
  11609. "Get logging settings from a property VALUE."
  11610. (let* (words w a)
  11611. ;; directly set the variables, they are already local.
  11612. (setq org-log-done nil
  11613. org-log-repeat nil
  11614. org-todo-log-states nil)
  11615. (setq words (org-split-string value))
  11616. (while (setq w (pop words))
  11617. (cond
  11618. ((setq a (assoc w org-startup-options))
  11619. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11620. (set (nth 1 a) (nth 2 a))))
  11621. ((setq a (org-extract-log-state-settings w))
  11622. (and (member (car a) org-todo-keywords-1)
  11623. (push a org-todo-log-states)))))))
  11624. (defun org-get-todo-sequence-head (kwd)
  11625. "Return the head of the TODO sequence to which KWD belongs.
  11626. If KWD is not set, check if there is a text property remembering the
  11627. right sequence."
  11628. (let (p)
  11629. (cond
  11630. ((not kwd)
  11631. (or (get-text-property (point-at-bol) 'org-todo-head)
  11632. (progn
  11633. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11634. nil (point-at-eol)))
  11635. (get-text-property p 'org-todo-head))))
  11636. ((not (member kwd org-todo-keywords-1))
  11637. (car org-todo-keywords-1))
  11638. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11639. (defun org-fast-todo-selection ()
  11640. "Fast TODO keyword selection with single keys.
  11641. Returns the new TODO keyword, or nil if no state change should occur."
  11642. (let* ((fulltable org-todo-key-alist)
  11643. (done-keywords org-done-keywords) ;; needed for the faces.
  11644. (maxlen (apply 'max (mapcar
  11645. (lambda (x)
  11646. (if (stringp (car x)) (string-width (car x)) 0))
  11647. fulltable)))
  11648. (expert nil)
  11649. (fwidth (+ maxlen 3 1 3))
  11650. (ncol (/ (- (window-width) 4) fwidth))
  11651. tg cnt e c tbl
  11652. groups ingroup)
  11653. (save-excursion
  11654. (save-window-excursion
  11655. (if expert
  11656. (set-buffer (get-buffer-create " *Org todo*"))
  11657. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11658. (erase-buffer)
  11659. (org-set-local 'org-done-keywords done-keywords)
  11660. (setq tbl fulltable cnt 0)
  11661. (while (setq e (pop tbl))
  11662. (cond
  11663. ((equal e '(:startgroup))
  11664. (push '() groups) (setq ingroup t)
  11665. (when (not (= cnt 0))
  11666. (setq cnt 0)
  11667. (insert "\n"))
  11668. (insert "{ "))
  11669. ((equal e '(:endgroup))
  11670. (setq ingroup nil cnt 0)
  11671. (insert "}\n"))
  11672. ((equal e '(:newline))
  11673. (when (not (= cnt 0))
  11674. (setq cnt 0)
  11675. (insert "\n")
  11676. (setq e (car tbl))
  11677. (while (equal (car tbl) '(:newline))
  11678. (insert "\n")
  11679. (setq tbl (cdr tbl)))))
  11680. (t
  11681. (setq tg (car e) c (cdr e))
  11682. (if ingroup (push tg (car groups)))
  11683. (setq tg (org-add-props tg nil 'face
  11684. (org-get-todo-face tg)))
  11685. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11686. (insert "[" c "] " tg (make-string
  11687. (- fwidth 4 (length tg)) ?\ ))
  11688. (when (= (setq cnt (1+ cnt)) ncol)
  11689. (insert "\n")
  11690. (if ingroup (insert " "))
  11691. (setq cnt 0)))))
  11692. (insert "\n")
  11693. (goto-char (point-min))
  11694. (if (not expert) (org-fit-window-to-buffer))
  11695. (message "[a-z..]:Set [SPC]:clear")
  11696. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11697. (cond
  11698. ((or (= c ?\C-g)
  11699. (and (= c ?q) (not (rassoc c fulltable))))
  11700. (setq quit-flag t))
  11701. ((= c ?\ ) nil)
  11702. ((setq e (rassoc c fulltable) tg (car e))
  11703. tg)
  11704. (t (setq quit-flag t)))))))
  11705. (defun org-entry-is-todo-p ()
  11706. (member (org-get-todo-state) org-not-done-keywords))
  11707. (defun org-entry-is-done-p ()
  11708. (member (org-get-todo-state) org-done-keywords))
  11709. (defun org-get-todo-state ()
  11710. "Return the TODO keyword of the current subtree."
  11711. (save-excursion
  11712. (org-back-to-heading t)
  11713. (and (looking-at org-todo-line-regexp)
  11714. (match-end 2)
  11715. (match-string 2))))
  11716. (defun org-at-date-range-p (&optional inactive-ok)
  11717. "Is the cursor inside a date range?"
  11718. (interactive)
  11719. (save-excursion
  11720. (catch 'exit
  11721. (let ((pos (point)))
  11722. (skip-chars-backward "^[<\r\n")
  11723. (skip-chars-backward "<[")
  11724. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11725. (>= (match-end 0) pos)
  11726. (throw 'exit t))
  11727. (skip-chars-backward "^<[\r\n")
  11728. (skip-chars-backward "<[")
  11729. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11730. (>= (match-end 0) pos)
  11731. (throw 'exit t)))
  11732. nil)))
  11733. (defun org-get-repeat (&optional tagline)
  11734. "Check if there is a deadline/schedule with repeater in this entry."
  11735. (save-match-data
  11736. (save-excursion
  11737. (org-back-to-heading t)
  11738. (and (re-search-forward (if tagline
  11739. (concat tagline "\\s-*" org-repeat-re)
  11740. org-repeat-re)
  11741. (org-entry-end-position) t)
  11742. (match-string-no-properties 1)))))
  11743. (defvar org-last-changed-timestamp)
  11744. (defvar org-last-inserted-timestamp)
  11745. (defvar org-log-post-message)
  11746. (defvar org-log-note-purpose)
  11747. (defvar org-log-note-how)
  11748. (defvar org-log-note-extra)
  11749. (defun org-auto-repeat-maybe (done-word)
  11750. "Check if the current headline contains a repeated deadline/schedule.
  11751. If yes, set TODO state back to what it was and change the base date
  11752. of repeating deadline/scheduled time stamps to new date.
  11753. This function is run automatically after each state change to a DONE state."
  11754. ;; last-state is dynamically scoped into this function
  11755. (let* ((repeat (org-get-repeat))
  11756. (aa (assoc org-last-state org-todo-kwd-alist))
  11757. (interpret (nth 1 aa))
  11758. (head (nth 2 aa))
  11759. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11760. (msg "Entry repeats: ")
  11761. (org-log-done nil)
  11762. (org-todo-log-states nil)
  11763. re type n what ts time to-state)
  11764. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11765. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11766. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11767. org-todo-repeat-to-state))
  11768. (unless (and to-state (member to-state org-todo-keywords-1))
  11769. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11770. (org-todo to-state)
  11771. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11772. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11773. (org-time-stamp-format t t))))
  11774. (when org-log-repeat
  11775. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11776. (memq 'org-add-log-note post-command-hook))
  11777. ;; OK, we are already setup for some record
  11778. (if (eq org-log-repeat 'note)
  11779. ;; make sure we take a note, not only a time stamp
  11780. (setq org-log-note-how 'note))
  11781. ;; Set up for taking a record
  11782. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11783. org-last-state
  11784. 'findpos org-log-repeat)))
  11785. (org-back-to-heading t)
  11786. (org-add-planning-info nil nil 'closed)
  11787. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11788. org-deadline-time-regexp "\\)\\|\\("
  11789. org-ts-regexp "\\)"))
  11790. (while (re-search-forward
  11791. re (save-excursion (outline-next-heading) (point)) t)
  11792. (setq type (if (match-end 1) org-scheduled-string
  11793. (if (match-end 3) org-deadline-string "Plain:"))
  11794. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11795. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11796. (setq n (string-to-number (match-string 2 ts))
  11797. what (match-string 3 ts))
  11798. (if (equal what "w") (setq n (* n 7) what "d"))
  11799. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11800. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11801. ;; Preparation, see if we need to modify the start date for the change
  11802. (when (match-end 1)
  11803. (setq time (save-match-data (org-time-string-to-time ts)))
  11804. (cond
  11805. ((equal (match-string 1 ts) ".")
  11806. ;; Shift starting date to today
  11807. (org-timestamp-change
  11808. (- (org-today) (time-to-days time))
  11809. 'day))
  11810. ((equal (match-string 1 ts) "+")
  11811. (let ((nshiftmax 10) (nshift 0))
  11812. (while (or (= nshift 0)
  11813. (<= (time-to-days time)
  11814. (time-to-days (current-time))))
  11815. (when (= (incf nshift) nshiftmax)
  11816. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11817. (error "Abort")))
  11818. (org-timestamp-change n (cdr (assoc what whata)))
  11819. (org-at-timestamp-p t)
  11820. (setq ts (match-string 1))
  11821. (setq time (save-match-data (org-time-string-to-time ts)))))
  11822. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11823. ;; rematch, so that we have everything in place for the real shift
  11824. (org-at-timestamp-p t)
  11825. (setq ts (match-string 1))
  11826. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11827. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11828. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11829. (setq org-log-post-message msg)
  11830. (message "%s" msg))))
  11831. (defun org-show-todo-tree (arg)
  11832. "Make a compact tree which shows all headlines marked with TODO.
  11833. The tree will show the lines where the regexp matches, and all higher
  11834. headlines above the match.
  11835. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11836. With a numeric prefix N, construct a sparse tree for the Nth element
  11837. of `org-todo-keywords-1'."
  11838. (interactive "P")
  11839. (let ((case-fold-search nil)
  11840. (kwd-re
  11841. (cond ((null arg) org-not-done-regexp)
  11842. ((equal arg '(4))
  11843. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11844. (mapcar 'list org-todo-keywords-1))))
  11845. (concat "\\("
  11846. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11847. "\\)\\>")))
  11848. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11849. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11850. org-todo-keywords-1)))
  11851. (t (user-error "Invalid prefix argument: %s" arg)))))
  11852. (message "%d TODO entries found"
  11853. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11854. (defun org-deadline (arg &optional time)
  11855. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11856. With one universal prefix argument, remove any deadline from the item.
  11857. With two universal prefix arguments, prompt for a warning delay.
  11858. With argument TIME, set the deadline at the corresponding date. TIME
  11859. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11860. (interactive "P")
  11861. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11862. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11863. 'region-start-level 'region))
  11864. org-loop-over-headlines-in-active-region)
  11865. (org-map-entries
  11866. `(org-deadline ',arg ,time)
  11867. org-loop-over-headlines-in-active-region
  11868. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11869. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11870. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11871. (repeater (and old-date
  11872. (string-match
  11873. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11874. old-date)
  11875. (match-string 1 old-date))))
  11876. (cond
  11877. ((equal arg '(4))
  11878. (when (and old-date org-log-redeadline)
  11879. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11880. org-log-redeadline))
  11881. (org-remove-timestamp-with-keyword org-deadline-string)
  11882. (message "Item no longer has a deadline."))
  11883. ((equal arg '(16))
  11884. (save-excursion
  11885. (org-back-to-heading t)
  11886. (if (re-search-forward
  11887. org-deadline-time-regexp
  11888. (save-excursion (outline-next-heading) (point)) t)
  11889. (let* ((rpl0 (match-string 1))
  11890. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11891. (replace-match
  11892. (concat org-deadline-string
  11893. " <" rpl
  11894. (format " -%dd"
  11895. (abs
  11896. (- (time-to-days
  11897. (save-match-data
  11898. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11899. (time-to-days old-date-time))))
  11900. ">") t t))
  11901. (user-error "No deadline information to update"))))
  11902. (t
  11903. (org-add-planning-info 'deadline time 'closed)
  11904. (when (and old-date org-log-redeadline
  11905. (not (equal old-date
  11906. (substring org-last-inserted-timestamp 1 -1))))
  11907. (org-add-log-setup 'redeadline nil old-date 'findpos
  11908. org-log-redeadline))
  11909. (when repeater
  11910. (save-excursion
  11911. (org-back-to-heading t)
  11912. (when (re-search-forward (concat org-deadline-string " "
  11913. org-last-inserted-timestamp)
  11914. (save-excursion
  11915. (outline-next-heading) (point)) t)
  11916. (goto-char (1- (match-end 0)))
  11917. (insert " " repeater)
  11918. (setq org-last-inserted-timestamp
  11919. (concat (substring org-last-inserted-timestamp 0 -1)
  11920. " " repeater
  11921. (substring org-last-inserted-timestamp -1))))))
  11922. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11923. (defun org-schedule (arg &optional time)
  11924. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11925. With one universal prefix argument, remove any scheduling date from the item.
  11926. With two universal prefix arguments, prompt for a delay cookie.
  11927. With argument TIME, scheduled at the corresponding date. TIME can
  11928. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11929. (interactive "P")
  11930. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11931. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11932. 'region-start-level 'region))
  11933. org-loop-over-headlines-in-active-region)
  11934. (org-map-entries
  11935. `(org-schedule ',arg ,time)
  11936. org-loop-over-headlines-in-active-region
  11937. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11938. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11939. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11940. (repeater (and old-date
  11941. (string-match
  11942. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11943. old-date)
  11944. (match-string 1 old-date))))
  11945. (cond
  11946. ((equal arg '(4))
  11947. (progn
  11948. (when (and old-date org-log-reschedule)
  11949. (org-add-log-setup 'delschedule nil old-date 'findpos
  11950. org-log-reschedule))
  11951. (org-remove-timestamp-with-keyword org-scheduled-string)
  11952. (message "Item is no longer scheduled.")))
  11953. ((equal arg '(16))
  11954. (save-excursion
  11955. (org-back-to-heading t)
  11956. (if (re-search-forward
  11957. org-scheduled-time-regexp
  11958. (save-excursion (outline-next-heading) (point)) t)
  11959. (let* ((rpl0 (match-string 1))
  11960. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11961. (replace-match
  11962. (concat org-scheduled-string
  11963. " <" rpl
  11964. (format " -%dd"
  11965. (abs
  11966. (- (time-to-days
  11967. (save-match-data
  11968. (org-read-date nil t nil "Delay until" old-date-time)))
  11969. (time-to-days old-date-time))))
  11970. ">") t t))
  11971. (user-error "No scheduled information to update"))))
  11972. (t
  11973. (org-add-planning-info 'scheduled time 'closed)
  11974. (when (and old-date org-log-reschedule
  11975. (not (equal old-date
  11976. (substring org-last-inserted-timestamp 1 -1))))
  11977. (org-add-log-setup 'reschedule nil old-date 'findpos
  11978. org-log-reschedule))
  11979. (when repeater
  11980. (save-excursion
  11981. (org-back-to-heading t)
  11982. (when (re-search-forward (concat org-scheduled-string " "
  11983. org-last-inserted-timestamp)
  11984. (save-excursion
  11985. (outline-next-heading) (point)) t)
  11986. (goto-char (1- (match-end 0)))
  11987. (insert " " repeater)
  11988. (setq org-last-inserted-timestamp
  11989. (concat (substring org-last-inserted-timestamp 0 -1)
  11990. " " repeater
  11991. (substring org-last-inserted-timestamp -1))))))
  11992. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11993. (defun org-get-scheduled-time (pom &optional inherit)
  11994. "Get the scheduled time as a time tuple, of a format suitable
  11995. for calling org-schedule with, or if there is no scheduling,
  11996. returns nil."
  11997. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11998. (when time
  11999. (apply 'encode-time (org-parse-time-string time)))))
  12000. (defun org-get-deadline-time (pom &optional inherit)
  12001. "Get the deadline as a time tuple, of a format suitable for
  12002. calling org-deadline with, or if there is no scheduling, returns
  12003. nil."
  12004. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12005. (when time
  12006. (apply 'encode-time (org-parse-time-string time)))))
  12007. (defun org-remove-timestamp-with-keyword (keyword)
  12008. "Remove all time stamps with KEYWORD in the current entry."
  12009. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12010. beg)
  12011. (save-excursion
  12012. (org-back-to-heading t)
  12013. (setq beg (point))
  12014. (outline-next-heading)
  12015. (while (re-search-backward re beg t)
  12016. (replace-match "")
  12017. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12018. (equal (char-before) ?\ ))
  12019. (backward-delete-char 1)
  12020. (if (string-match "^[ \t]*$" (buffer-substring
  12021. (point-at-bol) (point-at-eol)))
  12022. (delete-region (point-at-bol)
  12023. (min (point-max) (1+ (point-at-eol))))))))))
  12024. (defvar org-time-was-given) ; dynamically scoped parameter
  12025. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12026. (defun org-add-planning-info (what &optional time &rest remove)
  12027. "Insert new timestamp with keyword in the line directly after the headline.
  12028. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  12029. If non is given, the user is prompted for a date.
  12030. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  12031. be removed."
  12032. (interactive)
  12033. (let (org-time-was-given org-end-time-was-given ts
  12034. end default-time default-input)
  12035. (catch 'exit
  12036. (when (and (memq what '(scheduled deadline))
  12037. (or (not time)
  12038. (and (stringp time)
  12039. (string-match "^[-+]+[0-9]" time))))
  12040. ;; Try to get a default date/time from existing timestamp
  12041. (save-excursion
  12042. (org-back-to-heading t)
  12043. (setq end (save-excursion (outline-next-heading) (point)))
  12044. (when (re-search-forward (if (eq what 'scheduled)
  12045. org-scheduled-time-regexp
  12046. org-deadline-time-regexp)
  12047. end t)
  12048. (setq ts (match-string 1)
  12049. default-time
  12050. (apply 'encode-time (org-parse-time-string ts))
  12051. default-input (and ts (org-get-compact-tod ts))))))
  12052. (when what
  12053. (setq time
  12054. (if (stringp time)
  12055. ;; This is a string (relative or absolute), set proper date
  12056. (apply 'encode-time
  12057. (org-read-date-analyze
  12058. time default-time (decode-time default-time)))
  12059. ;; If necessary, get the time from the user
  12060. (or time (org-read-date nil 'to-time nil nil
  12061. default-time default-input)))))
  12062. (when (and org-insert-labeled-timestamps-at-point
  12063. (member what '(scheduled deadline)))
  12064. (insert
  12065. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  12066. (org-insert-time-stamp time org-time-was-given
  12067. nil nil nil (list org-end-time-was-given))
  12068. (setq what nil))
  12069. (save-excursion
  12070. (save-restriction
  12071. (let (col list elt ts buffer-invisibility-spec)
  12072. (org-back-to-heading t)
  12073. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  12074. (goto-char (match-end 1))
  12075. (setq col (current-column))
  12076. (goto-char (match-end 0))
  12077. (if (eobp) (insert "\n") (forward-char 1))
  12078. (when (and (not what)
  12079. (not (looking-at
  12080. (concat "[ \t]*"
  12081. org-keyword-time-not-clock-regexp))))
  12082. ;; Nothing to add, nothing to remove...... :-)
  12083. (throw 'exit nil))
  12084. (if (and (not (looking-at org-outline-regexp))
  12085. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  12086. "[^\r\n]*"))
  12087. (not (equal (match-string 1) org-clock-string)))
  12088. (narrow-to-region (match-beginning 0) (match-end 0))
  12089. (insert-before-markers "\n")
  12090. (backward-char 1)
  12091. (narrow-to-region (point) (point))
  12092. (and org-adapt-indentation (org-indent-to-column col)))
  12093. ;; Check if we have to remove something.
  12094. (setq list (cons what remove))
  12095. (while list
  12096. (setq elt (pop list))
  12097. (when (or (and (eq elt 'scheduled)
  12098. (re-search-forward org-scheduled-time-regexp nil t))
  12099. (and (eq elt 'deadline)
  12100. (re-search-forward org-deadline-time-regexp nil t))
  12101. (and (eq elt 'closed)
  12102. (re-search-forward org-closed-time-regexp nil t)))
  12103. (replace-match "")
  12104. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  12105. (and (looking-at "[ \t]+") (replace-match ""))
  12106. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  12107. (when what
  12108. (insert
  12109. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  12110. (cond ((eq what 'scheduled) org-scheduled-string)
  12111. ((eq what 'deadline) org-deadline-string)
  12112. ((eq what 'closed) org-closed-string))
  12113. " ")
  12114. (setq ts (org-insert-time-stamp
  12115. time
  12116. (or org-time-was-given
  12117. (and (eq what 'closed) org-log-done-with-time))
  12118. (eq what 'closed)
  12119. nil nil (list org-end-time-was-given)))
  12120. (insert
  12121. (if (not (or (bolp) (eq (char-before) ?\ )
  12122. (memq (char-after) '(32 10))
  12123. (eobp))) " " ""))
  12124. (end-of-line 1))
  12125. (goto-char (point-min))
  12126. (widen)
  12127. (if (and (looking-at "[ \t]*\n")
  12128. (equal (char-before) ?\n))
  12129. (delete-region (1- (point)) (point-at-eol)))
  12130. ts))))))
  12131. (defvar org-log-note-marker (make-marker))
  12132. (defvar org-log-note-purpose nil)
  12133. (defvar org-log-note-state nil)
  12134. (defvar org-log-note-previous-state nil)
  12135. (defvar org-log-note-how nil)
  12136. (defvar org-log-note-extra nil)
  12137. (defvar org-log-note-window-configuration nil)
  12138. (defvar org-log-note-return-to (make-marker))
  12139. (defvar org-log-note-effective-time nil
  12140. "Remembered current time so that dynamically scoped
  12141. `org-extend-today-until' affects tha timestamps in state change
  12142. log")
  12143. (defvar org-log-post-message nil
  12144. "Message to be displayed after a log note has been stored.
  12145. The auto-repeater uses this.")
  12146. (defun org-add-note ()
  12147. "Add a note to the current entry.
  12148. This is done in the same way as adding a state change note."
  12149. (interactive)
  12150. (org-add-log-setup 'note nil nil 'findpos nil))
  12151. (defvar org-property-end-re)
  12152. (defun org-add-log-setup (&optional purpose state prev-state
  12153. findpos how extra)
  12154. "Set up the post command hook to take a note.
  12155. If this is about to TODO state change, the new state is expected in STATE.
  12156. When FINDPOS is non-nil, find the correct position for the note in
  12157. the current entry. If not, assume that it can be inserted at point.
  12158. HOW is an indicator what kind of note should be created.
  12159. EXTRA is additional text that will be inserted into the notes buffer."
  12160. (let* ((org-log-into-drawer (org-log-into-drawer))
  12161. (drawer (cond ((stringp org-log-into-drawer)
  12162. org-log-into-drawer)
  12163. (org-log-into-drawer "LOGBOOK"))))
  12164. (save-restriction
  12165. (save-excursion
  12166. (when findpos
  12167. (org-back-to-heading t)
  12168. (narrow-to-region (point) (save-excursion
  12169. (outline-next-heading) (point)))
  12170. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  12171. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  12172. "[^\r\n]*\\)?"))
  12173. (goto-char (match-end 0))
  12174. (cond
  12175. (drawer
  12176. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  12177. nil t)
  12178. (progn
  12179. (goto-char (match-end 0))
  12180. (or org-log-states-order-reversed
  12181. (and (re-search-forward org-property-end-re nil t)
  12182. (goto-char (1- (match-beginning 0))))))
  12183. (insert "\n:" drawer ":\n:END:")
  12184. (beginning-of-line 0)
  12185. (org-indent-line)
  12186. (beginning-of-line 2)
  12187. (org-indent-line)
  12188. (end-of-line 0)))
  12189. ((and org-log-state-notes-insert-after-drawers
  12190. (save-excursion
  12191. (forward-line) (looking-at org-drawer-regexp)))
  12192. (forward-line)
  12193. (while (looking-at org-drawer-regexp)
  12194. (goto-char (match-end 0))
  12195. (re-search-forward org-property-end-re (point-max) t)
  12196. (forward-line))
  12197. (forward-line -1)))
  12198. (unless org-log-states-order-reversed
  12199. (and (= (char-after) ?\n) (forward-char 1))
  12200. (org-skip-over-state-notes)
  12201. (skip-chars-backward " \t\n\r")))
  12202. (move-marker org-log-note-marker (point))
  12203. (setq org-log-note-purpose purpose
  12204. org-log-note-state state
  12205. org-log-note-previous-state prev-state
  12206. org-log-note-how how
  12207. org-log-note-extra extra
  12208. org-log-note-effective-time (org-current-effective-time))
  12209. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12210. (defun org-skip-over-state-notes ()
  12211. "Skip past the list of State notes in an entry."
  12212. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12213. (when (ignore-errors (goto-char (org-in-item-p)))
  12214. (let* ((struct (org-list-struct))
  12215. (prevs (org-list-prevs-alist struct)))
  12216. (while (looking-at "[ \t]*- State")
  12217. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12218. (org-list-get-item-end (point) struct)))))))
  12219. (defun org-add-log-note (&optional purpose)
  12220. "Pop up a window for taking a note, and add this note later at point."
  12221. (remove-hook 'post-command-hook 'org-add-log-note)
  12222. (setq org-log-note-window-configuration (current-window-configuration))
  12223. (delete-other-windows)
  12224. (move-marker org-log-note-return-to (point))
  12225. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12226. (goto-char org-log-note-marker)
  12227. (org-switch-to-buffer-other-window "*Org Note*")
  12228. (erase-buffer)
  12229. (if (memq org-log-note-how '(time state))
  12230. (let (current-prefix-arg) (org-store-log-note))
  12231. (let ((org-inhibit-startup t)) (org-mode))
  12232. (insert (format "# Insert note for %s.
  12233. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12234. (cond
  12235. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12236. ((eq org-log-note-purpose 'done) "closed todo item")
  12237. ((eq org-log-note-purpose 'state)
  12238. (format "state change from \"%s\" to \"%s\""
  12239. (or org-log-note-previous-state "")
  12240. (or org-log-note-state "")))
  12241. ((eq org-log-note-purpose 'reschedule)
  12242. "rescheduling")
  12243. ((eq org-log-note-purpose 'delschedule)
  12244. "no longer scheduled")
  12245. ((eq org-log-note-purpose 'redeadline)
  12246. "changing deadline")
  12247. ((eq org-log-note-purpose 'deldeadline)
  12248. "removing deadline")
  12249. ((eq org-log-note-purpose 'refile)
  12250. "refiling")
  12251. ((eq org-log-note-purpose 'note)
  12252. "this entry")
  12253. (t (error "This should not happen")))))
  12254. (if org-log-note-extra (insert org-log-note-extra))
  12255. (org-set-local 'org-finish-function 'org-store-log-note)
  12256. (run-hooks 'org-log-buffer-setup-hook)))
  12257. (defvar org-note-abort nil) ; dynamically scoped
  12258. (defun org-store-log-note ()
  12259. "Finish taking a log note, and insert it to where it belongs."
  12260. (let ((txt (buffer-string)))
  12261. (kill-buffer (current-buffer))
  12262. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12263. lines ind bul)
  12264. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12265. (setq txt (replace-match "" t t txt)))
  12266. (if (string-match "\\s-+\\'" txt)
  12267. (setq txt (replace-match "" t t txt)))
  12268. (setq lines (org-split-string txt "\n"))
  12269. (when (and note (string-match "\\S-" note))
  12270. (setq note
  12271. (org-replace-escapes
  12272. note
  12273. (list (cons "%u" (user-login-name))
  12274. (cons "%U" user-full-name)
  12275. (cons "%t" (format-time-string
  12276. (org-time-stamp-format 'long 'inactive)
  12277. org-log-note-effective-time))
  12278. (cons "%T" (format-time-string
  12279. (org-time-stamp-format 'long nil)
  12280. org-log-note-effective-time))
  12281. (cons "%d" (format-time-string
  12282. (org-time-stamp-format nil 'inactive)
  12283. org-log-note-effective-time))
  12284. (cons "%D" (format-time-string
  12285. (org-time-stamp-format nil nil)
  12286. org-log-note-effective-time))
  12287. (cons "%s" (if org-log-note-state
  12288. (concat "\"" org-log-note-state "\"")
  12289. ""))
  12290. (cons "%S" (if org-log-note-previous-state
  12291. (concat "\"" org-log-note-previous-state "\"")
  12292. "\"\"")))))
  12293. (if lines (setq note (concat note " \\\\")))
  12294. (push note lines))
  12295. (when (or current-prefix-arg org-note-abort)
  12296. (when org-log-into-drawer
  12297. (org-remove-empty-drawer-at org-log-note-marker))
  12298. (setq lines nil))
  12299. (when lines
  12300. (with-current-buffer (marker-buffer org-log-note-marker)
  12301. (save-excursion
  12302. (goto-char org-log-note-marker)
  12303. (move-marker org-log-note-marker nil)
  12304. (end-of-line 1)
  12305. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12306. (setq ind (save-excursion
  12307. (if (ignore-errors (goto-char (org-in-item-p)))
  12308. (let ((struct (org-list-struct)))
  12309. (org-list-get-ind
  12310. (org-list-get-top-point struct) struct))
  12311. (skip-chars-backward " \r\t\n")
  12312. (cond
  12313. ((and (org-at-heading-p)
  12314. org-adapt-indentation)
  12315. (1+ (org-current-level)))
  12316. ((org-at-heading-p) 0)
  12317. (t (org-get-indentation))))))
  12318. (setq bul (org-list-bullet-string "-"))
  12319. (org-indent-line-to ind)
  12320. (insert bul (pop lines))
  12321. (let ((ind-body (+ (length bul) ind)))
  12322. (while lines
  12323. (insert "\n")
  12324. (org-indent-line-to ind-body)
  12325. (insert (pop lines))))
  12326. (message "Note stored")
  12327. (org-back-to-heading t)
  12328. (org-cycle-hide-drawers 'children))
  12329. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12330. ;; from within `org-add-log-note' because `buffer-undo-list'
  12331. ;; is then modified outside of `org-with-remote-undo'.
  12332. (when (eq this-command 'org-agenda-todo)
  12333. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12334. ;; Don't add undo information when called from `org-agenda-todo'
  12335. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12336. (set-window-configuration org-log-note-window-configuration)
  12337. (with-current-buffer (marker-buffer org-log-note-return-to)
  12338. (goto-char org-log-note-return-to))
  12339. (move-marker org-log-note-return-to nil)
  12340. (and org-log-post-message (message "%s" org-log-post-message))))
  12341. (defun org-remove-empty-drawer-at (pos)
  12342. "Remove an empty drawer at position POS.
  12343. POS may also be a marker."
  12344. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12345. (org-with-wide-buffer
  12346. (goto-char pos)
  12347. (let ((drawer (org-element-at-point)))
  12348. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12349. (not (org-element-property :contents-begin drawer)))
  12350. (delete-region (org-element-property :begin drawer)
  12351. (progn (goto-char (org-element-property :end drawer))
  12352. (skip-chars-backward " \r\t\n")
  12353. (forward-line)
  12354. (point))))))))
  12355. (defvar org-ts-type nil)
  12356. (defun org-sparse-tree (&optional arg type)
  12357. "Create a sparse tree, prompt for the details.
  12358. This command can create sparse trees. You first need to select the type
  12359. of match used to create the tree:
  12360. t Show all TODO entries.
  12361. T Show entries with a specific TODO keyword.
  12362. m Show entries selected by a tags/property match.
  12363. p Enter a property name and its value (both with completion on existing
  12364. names/values) and show entries with that property.
  12365. r Show entries matching a regular expression (`/' can be used as well).
  12366. b Show deadlines and scheduled items before a date.
  12367. a Show deadlines and scheduled items after a date.
  12368. d Show deadlines due within `org-deadline-warning-days'.
  12369. D Show deadlines and scheduled items between a date range."
  12370. (interactive "P")
  12371. (let (ans kwd value ts-type)
  12372. (setq type (or type org-sparse-tree-default-date-type))
  12373. (setq org-ts-type type)
  12374. (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"
  12375. (cond ((eq type 'all) "all timestamps")
  12376. ((eq type 'scheduled) "only scheduled")
  12377. ((eq type 'deadline) "only deadline")
  12378. ((eq type 'active) "only active timestamps")
  12379. ((eq type 'inactive) "only inactive timestamps")
  12380. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12381. ((eq type 'closed) "with a closed time-stamp")
  12382. (t "scheduled/deadline")))
  12383. (setq ans (read-char-exclusive))
  12384. (cond
  12385. ((equal ans ?c)
  12386. (org-sparse-tree
  12387. arg (cadr (member type '(scheduled-or-deadline
  12388. all scheduled deadline active inactive closed)))))
  12389. ((equal ans ?d)
  12390. (call-interactively 'org-check-deadlines))
  12391. ((equal ans ?b)
  12392. (call-interactively 'org-check-before-date))
  12393. ((equal ans ?a)
  12394. (call-interactively 'org-check-after-date))
  12395. ((equal ans ?D)
  12396. (call-interactively 'org-check-dates-range))
  12397. ((equal ans ?t)
  12398. (call-interactively 'org-show-todo-tree))
  12399. ((equal ans ?T)
  12400. (org-show-todo-tree '(4)))
  12401. ((member ans '(?T ?m))
  12402. (call-interactively 'org-match-sparse-tree))
  12403. ((member ans '(?p ?P))
  12404. (setq kwd (org-icompleting-read "Property: "
  12405. (mapcar 'list (org-buffer-property-keys))))
  12406. (setq value (org-icompleting-read "Value: "
  12407. (mapcar 'list (org-property-values kwd))))
  12408. (unless (string-match "\\`{.*}\\'" value)
  12409. (setq value (concat "\"" value "\"")))
  12410. (org-match-sparse-tree arg (concat kwd "=" value)))
  12411. ((member ans '(?r ?R ?/))
  12412. (call-interactively 'org-occur))
  12413. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12414. (defvar org-occur-highlights nil
  12415. "List of overlays used for occur matches.")
  12416. (make-variable-buffer-local 'org-occur-highlights)
  12417. (defvar org-occur-parameters nil
  12418. "Parameters of the active org-occur calls.
  12419. This is a list, each call to org-occur pushes as cons cell,
  12420. containing the regular expression and the callback, onto the list.
  12421. The list can contain several entries if `org-occur' has been called
  12422. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12423. will only contain one set of parameters. When the highlights are
  12424. removed (for example with `C-c C-c', or with the next edit (depending
  12425. on `org-remove-highlights-with-change'), this variable is emptied
  12426. as well.")
  12427. (make-variable-buffer-local 'org-occur-parameters)
  12428. (defun org-occur (regexp &optional keep-previous callback)
  12429. "Make a compact tree which shows all matches of REGEXP.
  12430. The tree will show the lines where the regexp matches, and all higher
  12431. headlines above the match. It will also show the heading after the match,
  12432. to make sure editing the matching entry is easy.
  12433. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12434. call to `org-occur' will be kept, to allow stacking of calls to this
  12435. command.
  12436. If CALLBACK is non-nil, it is a function which is called to confirm
  12437. that the match should indeed be shown."
  12438. (interactive "sRegexp: \nP")
  12439. (when (equal regexp "")
  12440. (user-error "Regexp cannot be empty"))
  12441. (unless keep-previous
  12442. (org-remove-occur-highlights nil nil t))
  12443. (push (cons regexp callback) org-occur-parameters)
  12444. (let ((cnt 0))
  12445. (save-excursion
  12446. (goto-char (point-min))
  12447. (if (or (not keep-previous) ; do not want to keep
  12448. (not org-occur-highlights)) ; no previous matches
  12449. ;; hide everything
  12450. (org-overview))
  12451. (while (re-search-forward regexp nil t)
  12452. (when (or (not callback)
  12453. (save-match-data (funcall callback)))
  12454. (setq cnt (1+ cnt))
  12455. (when org-highlight-sparse-tree-matches
  12456. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12457. (org-show-context 'occur-tree))))
  12458. (when org-remove-highlights-with-change
  12459. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12460. nil 'local))
  12461. (unless org-sparse-tree-open-archived-trees
  12462. (org-hide-archived-subtrees (point-min) (point-max)))
  12463. (run-hooks 'org-occur-hook)
  12464. (if (org-called-interactively-p 'interactive)
  12465. (message "%d match(es) for regexp %s" cnt regexp))
  12466. cnt))
  12467. (defun org-occur-next-match (&optional n reset)
  12468. "Function for `next-error-function' to find sparse tree matches.
  12469. N is the number of matches to move, when negative move backwards.
  12470. RESET is entirely ignored - this function always goes back to the
  12471. starting point when no match is found."
  12472. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12473. (search-func (if (< n 0)
  12474. 'previous-single-char-property-change
  12475. 'next-single-char-property-change))
  12476. (n (abs n))
  12477. (pos (point))
  12478. p1)
  12479. (catch 'exit
  12480. (while (setq p1 (funcall search-func (point) 'org-type))
  12481. (when (equal p1 limit)
  12482. (goto-char pos)
  12483. (error "No more matches"))
  12484. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12485. (setq n (1- n))
  12486. (when (= n 0)
  12487. (goto-char p1)
  12488. (throw 'exit (point))))
  12489. (goto-char p1))
  12490. (goto-char p1)
  12491. (error "No more matches"))))
  12492. (defun org-show-context (&optional key)
  12493. "Make sure point and context are visible.
  12494. How much context is shown depends upon the variables
  12495. `org-show-hierarchy-above', `org-show-following-heading',
  12496. `org-show-entry-below' and `org-show-siblings'."
  12497. (let ((heading-p (org-at-heading-p t))
  12498. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12499. (following-p (org-get-alist-option org-show-following-heading key))
  12500. (entry-p (org-get-alist-option org-show-entry-below key))
  12501. (siblings-p (org-get-alist-option org-show-siblings key)))
  12502. ;; Show heading or entry text
  12503. (if (and heading-p (not entry-p))
  12504. (org-flag-heading nil) ; only show the heading
  12505. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12506. (org-show-hidden-entry))) ; show entire entry
  12507. (when following-p
  12508. ;; Show next sibling, or heading below text
  12509. (save-excursion
  12510. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12511. (org-flag-heading nil))))
  12512. (when siblings-p (org-show-siblings))
  12513. (when hierarchy-p
  12514. ;; show all higher headings, possibly with siblings
  12515. (save-excursion
  12516. (while (and (condition-case nil
  12517. (progn (org-up-heading-all 1) t)
  12518. (error nil))
  12519. (not (bobp)))
  12520. (org-flag-heading nil)
  12521. (when siblings-p (org-show-siblings)))))
  12522. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12523. (defvar org-reveal-start-hook nil
  12524. "Hook run before revealing a location.")
  12525. (defun org-reveal (&optional siblings)
  12526. "Show current entry, hierarchy above it, and the following headline.
  12527. This can be used to show a consistent set of context around locations
  12528. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12529. not t for the search context.
  12530. With optional argument SIBLINGS, on each level of the hierarchy all
  12531. siblings are shown. This repairs the tree structure to what it would
  12532. look like when opened with hierarchical calls to `org-cycle'.
  12533. With double optional argument \\[universal-argument] \\[universal-argument], \
  12534. go to the parent and show the
  12535. entire tree."
  12536. (interactive "P")
  12537. (run-hooks 'org-reveal-start-hook)
  12538. (let ((org-show-hierarchy-above t)
  12539. (org-show-following-heading t)
  12540. (org-show-siblings (if siblings t org-show-siblings)))
  12541. (org-show-context nil))
  12542. (when (equal siblings '(16))
  12543. (save-excursion
  12544. (when (org-up-heading-safe)
  12545. (org-show-subtree)
  12546. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12547. (defun org-highlight-new-match (beg end)
  12548. "Highlight from BEG to END and mark the highlight is an occur headline."
  12549. (let ((ov (make-overlay beg end)))
  12550. (overlay-put ov 'face 'secondary-selection)
  12551. (overlay-put ov 'org-type 'org-occur)
  12552. (push ov org-occur-highlights)))
  12553. (defun org-remove-occur-highlights (&optional beg end noremove)
  12554. "Remove the occur highlights from the buffer.
  12555. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12556. from the `before-change-functions' in the current buffer."
  12557. (interactive)
  12558. (unless org-inhibit-highlight-removal
  12559. (mapc 'delete-overlay org-occur-highlights)
  12560. (setq org-occur-highlights nil)
  12561. (setq org-occur-parameters nil)
  12562. (unless noremove
  12563. (remove-hook 'before-change-functions
  12564. 'org-remove-occur-highlights 'local))))
  12565. ;;;; Priorities
  12566. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12567. "Regular expression matching the priority indicator.")
  12568. (defvar org-remove-priority-next-time nil)
  12569. (defun org-priority-up ()
  12570. "Increase the priority of the current item."
  12571. (interactive)
  12572. (org-priority 'up))
  12573. (defun org-priority-down ()
  12574. "Decrease the priority of the current item."
  12575. (interactive)
  12576. (org-priority 'down))
  12577. (defun org-priority (&optional action show)
  12578. "Change the priority of an item.
  12579. ACTION can be `set', `up', `down', or a character."
  12580. (interactive "P")
  12581. (if (equal action '(4))
  12582. (org-show-priority)
  12583. (unless org-enable-priority-commands
  12584. (user-error "Priority commands are disabled"))
  12585. (setq action (or action 'set))
  12586. (let (current new news have remove)
  12587. (save-excursion
  12588. (org-back-to-heading t)
  12589. (if (looking-at org-priority-regexp)
  12590. (setq current (string-to-char (match-string 2))
  12591. have t))
  12592. (cond
  12593. ((eq action 'remove)
  12594. (setq remove t new ?\ ))
  12595. ((or (eq action 'set)
  12596. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12597. (if (not (eq action 'set))
  12598. (setq new action)
  12599. (message "Priority %c-%c, SPC to remove: "
  12600. org-highest-priority org-lowest-priority)
  12601. (save-match-data
  12602. (setq new (read-char-exclusive))))
  12603. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12604. (= (upcase org-lowest-priority) org-lowest-priority))
  12605. (setq new (upcase new)))
  12606. (cond ((equal new ?\ ) (setq remove t))
  12607. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12608. (user-error "Priority must be between `%c' and `%c'"
  12609. org-highest-priority org-lowest-priority))))
  12610. ((eq action 'up)
  12611. (setq new (if have
  12612. (1- current) ; normal cycling
  12613. ;; last priority was empty
  12614. (if (eq last-command this-command)
  12615. org-lowest-priority ; wrap around empty to lowest
  12616. ;; default
  12617. (if org-priority-start-cycle-with-default
  12618. org-default-priority
  12619. (1- org-default-priority))))))
  12620. ((eq action 'down)
  12621. (setq new (if have
  12622. (1+ current) ; normal cycling
  12623. ;; last priority was empty
  12624. (if (eq last-command this-command)
  12625. org-highest-priority ; wrap around empty to highest
  12626. ;; default
  12627. (if org-priority-start-cycle-with-default
  12628. org-default-priority
  12629. (1+ org-default-priority))))))
  12630. (t (user-error "Invalid action")))
  12631. (if (or (< (upcase new) org-highest-priority)
  12632. (> (upcase new) org-lowest-priority))
  12633. (if (and (memq action '(up down))
  12634. (not have) (not (eq last-command this-command)))
  12635. ;; `new' is from default priority
  12636. (error
  12637. "The default can not be set, see `org-default-priority' why")
  12638. ;; normal cycling: `new' is beyond highest/lowest priority
  12639. ;; and is wrapped around to the empty priority
  12640. (setq remove t)))
  12641. (setq news (format "%c" new))
  12642. (if have
  12643. (if remove
  12644. (replace-match "" t t nil 1)
  12645. (replace-match news t t nil 2))
  12646. (if remove
  12647. (user-error "No priority cookie found in line")
  12648. (let ((case-fold-search nil))
  12649. (looking-at org-todo-line-regexp))
  12650. (if (match-end 2)
  12651. (progn
  12652. (goto-char (match-end 2))
  12653. (insert " [#" news "]"))
  12654. (goto-char (match-beginning 3))
  12655. (insert "[#" news "] "))))
  12656. (org-set-tags nil 'align))
  12657. (if remove
  12658. (message "Priority removed")
  12659. (message "Priority of current item set to %s" news)))))
  12660. (defun org-show-priority ()
  12661. "Show the priority of the current item.
  12662. This priority is composed of the main priority given with the [#A] cookies,
  12663. and by additional input from the age of a schedules or deadline entry."
  12664. (interactive)
  12665. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12666. (org-get-at-bol 'priority)
  12667. (save-excursion
  12668. (save-match-data
  12669. (beginning-of-line)
  12670. (and (looking-at org-heading-regexp)
  12671. (org-get-priority (match-string 0))))))))
  12672. (message "Priority is %d" (if pri pri -1000))))
  12673. (defun org-get-priority (s)
  12674. "Find priority cookie and return priority."
  12675. (save-match-data
  12676. (if (functionp org-get-priority-function)
  12677. (funcall org-get-priority-function)
  12678. (if (not (string-match org-priority-regexp s))
  12679. (* 1000 (- org-lowest-priority org-default-priority))
  12680. (* 1000 (- org-lowest-priority
  12681. (string-to-char (match-string 2 s))))))))
  12682. ;;;; Tags
  12683. (defvar org-agenda-archives-mode)
  12684. (defvar org-map-continue-from nil
  12685. "Position from where mapping should continue.
  12686. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12687. (defvar org-scanner-tags nil
  12688. "The current tag list while the tags scanner is running.")
  12689. (defvar org-trust-scanner-tags nil
  12690. "Should `org-get-tags-at' use the tags for the scanner.
  12691. This is for internal dynamical scoping only.
  12692. When this is non-nil, the function `org-get-tags-at' will return the value
  12693. of `org-scanner-tags' instead of building the list by itself. This
  12694. can lead to large speed-ups when the tags scanner is used in a file with
  12695. many entries, and when the list of tags is retrieved, for example to
  12696. obtain a list of properties. Building the tags list for each entry in such
  12697. a file becomes an N^2 operation - but with this variable set, it scales
  12698. as N.")
  12699. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12700. "Sca headline tags with inheritance and produce output ACTION.
  12701. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12702. or `agenda' to produce an entry list for an agenda view. It can also be
  12703. a Lisp form or a function that should be called at each matched headline, in
  12704. this case the return value is a list of all return values from these calls.
  12705. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12706. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12707. only lines with a not-done TODO keyword are included in the output.
  12708. This should be the same variable that was scoped into
  12709. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12710. START-LEVEL can be a string with asterisks, reducing the scope to
  12711. headlines matching this string."
  12712. (require 'org-agenda)
  12713. (let* ((re (concat "^"
  12714. (if start-level
  12715. ;; Get the correct level to match
  12716. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12717. org-outline-regexp)
  12718. " *\\(\\<\\("
  12719. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12720. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12721. (props (list 'face 'default
  12722. 'done-face 'org-agenda-done
  12723. 'undone-face 'default
  12724. 'mouse-face 'highlight
  12725. 'org-not-done-regexp org-not-done-regexp
  12726. 'org-todo-regexp org-todo-regexp
  12727. 'org-complex-heading-regexp org-complex-heading-regexp
  12728. 'help-echo
  12729. (format "mouse-2 or RET jump to org file %s"
  12730. (abbreviate-file-name
  12731. (or (buffer-file-name (buffer-base-buffer))
  12732. (buffer-name (buffer-base-buffer)))))))
  12733. (org-map-continue-from nil)
  12734. lspos tags tags-list
  12735. (tags-alist (list (cons 0 org-file-tags)))
  12736. (llast 0) rtn rtn1 level category i txt
  12737. todo marker entry priority)
  12738. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12739. (setq action (list 'lambda nil action)))
  12740. (save-excursion
  12741. (goto-char (point-min))
  12742. (when (eq action 'sparse-tree)
  12743. (org-overview)
  12744. (org-remove-occur-highlights))
  12745. (while (let (case-fold-search)
  12746. (re-search-forward re nil t))
  12747. (setq org-map-continue-from nil)
  12748. (catch :skip
  12749. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12750. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12751. (goto-char (setq lspos (match-beginning 0)))
  12752. (setq level (org-reduced-level (org-outline-level))
  12753. category (org-get-category))
  12754. (setq i llast llast level)
  12755. ;; remove tag lists from same and sublevels
  12756. (while (>= i level)
  12757. (when (setq entry (assoc i tags-alist))
  12758. (setq tags-alist (delete entry tags-alist)))
  12759. (setq i (1- i)))
  12760. ;; add the next tags
  12761. (when tags
  12762. (setq tags (org-split-string tags ":")
  12763. tags-alist
  12764. (cons (cons level tags) tags-alist)))
  12765. ;; compile tags for current headline
  12766. (setq tags-list
  12767. (if org-use-tag-inheritance
  12768. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12769. tags)
  12770. org-scanner-tags tags-list)
  12771. (when org-use-tag-inheritance
  12772. (setcdr (car tags-alist)
  12773. (mapcar (lambda (x)
  12774. (setq x (copy-sequence x))
  12775. (org-add-prop-inherited x))
  12776. (cdar tags-alist))))
  12777. (when (and tags org-use-tag-inheritance
  12778. (or (not (eq t org-use-tag-inheritance))
  12779. org-tags-exclude-from-inheritance))
  12780. ;; selective inheritance, remove uninherited ones
  12781. (setcdr (car tags-alist)
  12782. (org-remove-uninherited-tags (cdar tags-alist))))
  12783. (when (and
  12784. ;; eval matcher only when the todo condition is OK
  12785. (and (or (not todo-only) (member todo org-not-done-keywords))
  12786. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12787. (eval matcher)))
  12788. ;; Call the skipper, but return t if it does not skip,
  12789. ;; so that the `and' form continues evaluating
  12790. (progn
  12791. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12792. t)
  12793. ;; Check if timestamps are deselecting this entry
  12794. (or (not todo-only)
  12795. (and (member todo org-not-done-keywords)
  12796. (or (not org-agenda-tags-todo-honor-ignore-options)
  12797. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12798. ;; select this headline
  12799. (cond
  12800. ((eq action 'sparse-tree)
  12801. (and org-highlight-sparse-tree-matches
  12802. (org-get-heading) (match-end 0)
  12803. (org-highlight-new-match
  12804. (match-beginning 1) (match-end 1)))
  12805. (org-show-context 'tags-tree))
  12806. ((eq action 'agenda)
  12807. (setq txt (org-agenda-format-item
  12808. ""
  12809. (concat
  12810. (if (eq org-tags-match-list-sublevels 'indented)
  12811. (make-string (1- level) ?.) "")
  12812. (org-get-heading))
  12813. level category
  12814. tags-list)
  12815. priority (org-get-priority txt))
  12816. (goto-char lspos)
  12817. (setq marker (org-agenda-new-marker))
  12818. (org-add-props txt props
  12819. 'org-marker marker 'org-hd-marker marker 'org-category category
  12820. 'todo-state todo
  12821. 'priority priority 'type "tagsmatch")
  12822. (push txt rtn))
  12823. ((functionp action)
  12824. (setq org-map-continue-from nil)
  12825. (save-excursion
  12826. (setq rtn1 (funcall action))
  12827. (push rtn1 rtn)))
  12828. (t (user-error "Invalid action")))
  12829. ;; if we are to skip sublevels, jump to end of subtree
  12830. (unless org-tags-match-list-sublevels
  12831. (org-end-of-subtree t)
  12832. (backward-char 1))))
  12833. ;; Get the correct position from where to continue
  12834. (if org-map-continue-from
  12835. (goto-char org-map-continue-from)
  12836. (and (= (point) lspos) (end-of-line 1)))))
  12837. (when (and (eq action 'sparse-tree)
  12838. (not org-sparse-tree-open-archived-trees))
  12839. (org-hide-archived-subtrees (point-min) (point-max)))
  12840. (nreverse rtn)))
  12841. (defun org-remove-uninherited-tags (tags)
  12842. "Remove all tags that are not inherited from the list TAGS."
  12843. (cond
  12844. ((eq org-use-tag-inheritance t)
  12845. (if org-tags-exclude-from-inheritance
  12846. (org-delete-all org-tags-exclude-from-inheritance tags)
  12847. tags))
  12848. ((not org-use-tag-inheritance) nil)
  12849. ((stringp org-use-tag-inheritance)
  12850. (delq nil (mapcar
  12851. (lambda (x)
  12852. (if (and (string-match org-use-tag-inheritance x)
  12853. (not (member x org-tags-exclude-from-inheritance)))
  12854. x nil))
  12855. tags)))
  12856. ((listp org-use-tag-inheritance)
  12857. (delq nil (mapcar
  12858. (lambda (x)
  12859. (if (member x org-use-tag-inheritance) x nil))
  12860. tags)))))
  12861. (defun org-match-sparse-tree (&optional todo-only match)
  12862. "Create a sparse tree according to tags string MATCH.
  12863. MATCH can contain positive and negative selection of tags, like
  12864. \"+WORK+URGENT-WITHBOSS\".
  12865. If optional argument TODO-ONLY is non-nil, only select lines that are
  12866. also TODO lines."
  12867. (interactive "P")
  12868. (org-agenda-prepare-buffers (list (current-buffer)))
  12869. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12870. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12871. (defvar org-cached-props nil)
  12872. (defun org-cached-entry-get (pom property)
  12873. (if (or (eq t org-use-property-inheritance)
  12874. (and (stringp org-use-property-inheritance)
  12875. (string-match org-use-property-inheritance property))
  12876. (and (listp org-use-property-inheritance)
  12877. (member property org-use-property-inheritance)))
  12878. ;; Caching is not possible, check it directly
  12879. (org-entry-get pom property 'inherit)
  12880. ;; Get all properties, so that we can do complicated checks easily
  12881. (cdr (assoc property (or org-cached-props
  12882. (setq org-cached-props
  12883. (org-entry-properties pom)))))))
  12884. (defun org-global-tags-completion-table (&optional files)
  12885. "Return the list of all tags in all agenda buffer/files.
  12886. Optional FILES argument is a list of files which can be used
  12887. instead of the agenda files."
  12888. (save-excursion
  12889. (org-uniquify
  12890. (delq nil
  12891. (apply 'append
  12892. (mapcar
  12893. (lambda (file)
  12894. (set-buffer (find-file-noselect file))
  12895. (append (org-get-buffer-tags)
  12896. (mapcar (lambda (x) (if (stringp (car-safe x))
  12897. (list (car-safe x)) nil))
  12898. org-tag-alist)))
  12899. (if (and files (car files))
  12900. files
  12901. (org-agenda-files))))))))
  12902. (defun org-make-tags-matcher (match)
  12903. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12904. The variable `todo-only' is scoped dynamically into this function.
  12905. It will be set to t if the matcher restricts matching to TODO entries,
  12906. otherwise will not be touched.
  12907. Returns a cons of the selection string MATCH and the constructed
  12908. lisp form implementing the matcher. The matcher is to be evaluated
  12909. at an Org entry, with point on the headline, and returns t if the
  12910. entry matches the selection string MATCH. The returned lisp form
  12911. references two variables with information about the entry, which
  12912. must be bound around the form's evaluation: todo, the TODO keyword
  12913. at the entry (or nil of none); and tags-list, the list of all tags
  12914. at the entry including inherited ones. Additionally, the category
  12915. of the entry (if any) must be specified as the text property
  12916. 'org-category on the headline.
  12917. See also `org-scan-tags'.
  12918. "
  12919. (declare (special todo-only))
  12920. (unless (boundp 'todo-only)
  12921. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12922. (unless match
  12923. ;; Get a new match request, with completion against the global
  12924. ;; tags table and the local tags in current buffer
  12925. (let ((org-last-tags-completion-table
  12926. (org-uniquify
  12927. (delq nil (append (org-get-buffer-tags)
  12928. (org-global-tags-completion-table))))))
  12929. (setq match (org-completing-read-no-i
  12930. "Match: " 'org-tags-completion-function nil nil nil
  12931. 'org-tags-history))))
  12932. ;; Parse the string and create a lisp form
  12933. (let ((match0 match)
  12934. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12935. minus tag mm
  12936. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12937. orterms term orlist re-p str-p level-p level-op time-p
  12938. prop-p pn pv po gv rest (start 0) (ss 0))
  12939. ;; Expand group tags
  12940. (setq match (org-tags-expand match))
  12941. ;; Check if there is a TODO part of this match, which would be the
  12942. ;; part after a "/". TO make sure that this slash is not part of
  12943. ;; a property value to be matched against, we also check that there
  12944. ;; is no " after that slash.
  12945. ;; First, find the last slash
  12946. (while (string-match "/+" match ss)
  12947. (setq start (match-beginning 0) ss (match-end 0)))
  12948. (if (and (string-match "/+" match start)
  12949. (not (save-match-data (string-match "\"" match start))))
  12950. ;; match contains also a todo-matching request
  12951. (progn
  12952. (setq tagsmatch (substring match 0 (match-beginning 0))
  12953. todomatch (substring match (match-end 0)))
  12954. (if (string-match "^!" todomatch)
  12955. (setq todo-only t todomatch (substring todomatch 1)))
  12956. (if (string-match "^\\s-*$" todomatch)
  12957. (setq todomatch nil)))
  12958. ;; only matching tags
  12959. (setq tagsmatch match todomatch nil))
  12960. ;; Make the tags matcher
  12961. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12962. (setq tagsmatcher t)
  12963. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12964. (while (setq term (pop orterms))
  12965. (while (and (equal (substring term -1) "\\") orterms)
  12966. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12967. (while (string-match re term)
  12968. (setq rest (substring term (match-end 0))
  12969. minus (and (match-end 1)
  12970. (equal (match-string 1 term) "-"))
  12971. tag (save-match-data (replace-regexp-in-string
  12972. "\\\\-" "-"
  12973. (match-string 2 term)))
  12974. re-p (equal (string-to-char tag) ?{)
  12975. level-p (match-end 4)
  12976. prop-p (match-end 5)
  12977. mm (cond
  12978. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12979. (level-p
  12980. (setq level-op (org-op-to-function (match-string 3 term)))
  12981. `(,level-op level ,(string-to-number
  12982. (match-string 4 term))))
  12983. (prop-p
  12984. (setq pn (match-string 5 term)
  12985. po (match-string 6 term)
  12986. pv (match-string 7 term)
  12987. re-p (equal (string-to-char pv) ?{)
  12988. str-p (equal (string-to-char pv) ?\")
  12989. time-p (save-match-data
  12990. (string-match "^\"[[<].*[]>]\"$" pv))
  12991. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12992. (if time-p (setq pv (org-matcher-time pv)))
  12993. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12994. (cond
  12995. ((equal pn "CATEGORY")
  12996. (setq gv '(get-text-property (point) 'org-category)))
  12997. ((equal pn "TODO")
  12998. (setq gv 'todo))
  12999. (t
  13000. (setq gv `(org-cached-entry-get nil ,pn))))
  13001. (if re-p
  13002. (if (eq po 'org<>)
  13003. `(not (string-match ,pv (or ,gv "")))
  13004. `(string-match ,pv (or ,gv "")))
  13005. (if str-p
  13006. `(,po (or ,gv "") ,pv)
  13007. `(,po (string-to-number (or ,gv ""))
  13008. ,(string-to-number pv) ))))
  13009. (t `(member ,tag tags-list)))
  13010. mm (if minus (list 'not mm) mm)
  13011. term rest)
  13012. (push mm tagsmatcher))
  13013. (push (if (> (length tagsmatcher) 1)
  13014. (cons 'and tagsmatcher)
  13015. (car tagsmatcher))
  13016. orlist)
  13017. (setq tagsmatcher nil))
  13018. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13019. (setq tagsmatcher
  13020. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13021. ;; Make the todo matcher
  13022. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13023. (setq todomatcher t)
  13024. (setq orterms (org-split-string todomatch "|") orlist nil)
  13025. (while (setq term (pop orterms))
  13026. (while (string-match re term)
  13027. (setq minus (and (match-end 1)
  13028. (equal (match-string 1 term) "-"))
  13029. kwd (match-string 2 term)
  13030. re-p (equal (string-to-char kwd) ?{)
  13031. term (substring term (match-end 0))
  13032. mm (if re-p
  13033. `(string-match ,(substring kwd 1 -1) todo)
  13034. (list 'equal 'todo kwd))
  13035. mm (if minus (list 'not mm) mm))
  13036. (push mm todomatcher))
  13037. (push (if (> (length todomatcher) 1)
  13038. (cons 'and todomatcher)
  13039. (car todomatcher))
  13040. orlist)
  13041. (setq todomatcher nil))
  13042. (setq todomatcher (if (> (length orlist) 1)
  13043. (cons 'or orlist) (car orlist))))
  13044. ;; Return the string and lisp forms of the matcher
  13045. (setq matcher (if todomatcher
  13046. (list 'and tagsmatcher todomatcher)
  13047. tagsmatcher))
  13048. (when todo-only
  13049. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13050. matcher)))
  13051. (cons match0 matcher)))
  13052. (defun org-tags-expand (match &optional single-as-list downcased)
  13053. "Expand group tags in MATCH.
  13054. This replaces every group tag in MATCH with a regexp tag search.
  13055. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13056. will be replaced like this:
  13057. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  13058. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  13059. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  13060. Replacing by a regexp preserves the structure of the match.
  13061. E.g., this expansion
  13062. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13063. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13064. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13065. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13066. assumed to be a single group tag, and the function will return
  13067. the list of tags in this group.
  13068. When DOWNCASE is non-nil, expand downcased TAGS."
  13069. (if org-group-tags
  13070. (let* ((case-fold-search t)
  13071. (stable org-mode-syntax-table)
  13072. (tal (or org-tag-groups-alist-for-agenda
  13073. org-tag-groups-alist))
  13074. (tal (if downcased
  13075. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  13076. (tml (mapcar 'car tal))
  13077. (rtnmatch match) rpl)
  13078. ;; @ and _ are allowed as word-components in tags
  13079. (modify-syntax-entry ?@ "w" stable)
  13080. (modify-syntax-entry ?_ "w" stable)
  13081. (while (and tml
  13082. (with-syntax-table stable
  13083. (string-match
  13084. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13085. (regexp-opt tml) "\\>\\)") rtnmatch)))
  13086. (let* ((dir (match-string 1 rtnmatch))
  13087. (tag (match-string 2 rtnmatch))
  13088. (tag (if downcased (downcase tag) tag)))
  13089. (setq tml (delete tag tml))
  13090. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  13091. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  13092. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  13093. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  13094. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  13095. (if single-as-list
  13096. (or (reverse rpl) (list rtnmatch))
  13097. rtnmatch))
  13098. (if single-as-list (list (if downcased (downcase match) match))
  13099. match)))
  13100. (defun org-op-to-function (op &optional stringp)
  13101. "Turn an operator into the appropriate function."
  13102. (setq op
  13103. (cond
  13104. ((equal op "<" ) '(< string< org-time<))
  13105. ((equal op ">" ) '(> org-string> org-time>))
  13106. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13107. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13108. ((member op '("=" "==")) '(= string= org-time=))
  13109. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13110. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13111. (defun org<> (a b) (not (= a b)))
  13112. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13113. (defun org-string>= (a b) (not (string< a b)))
  13114. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13115. (defun org-string<> (a b) (not (string= a b)))
  13116. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13117. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13118. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13119. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13120. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13121. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13122. (defun org-2ft (s)
  13123. "Convert S to a floating point time.
  13124. If S is already a number, just return it. If it is a string, parse
  13125. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13126. (cond
  13127. ((numberp s) s)
  13128. ((stringp s)
  13129. (condition-case nil
  13130. (float-time (apply 'encode-time (org-parse-time-string s)))
  13131. (error 0.)))
  13132. (t 0.)))
  13133. (defun org-time-today ()
  13134. "Time in seconds today at 0:00.
  13135. Returns the float number of seconds since the beginning of the
  13136. epoch to the beginning of today (00:00)."
  13137. (float-time (apply 'encode-time
  13138. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13139. (defun org-matcher-time (s)
  13140. "Interpret a time comparison value."
  13141. (save-match-data
  13142. (cond
  13143. ((string= s "<now>") (float-time))
  13144. ((string= s "<today>") (org-time-today))
  13145. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13146. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13147. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13148. (+ (org-time-today)
  13149. (* (string-to-number (match-string 1 s))
  13150. (cdr (assoc (match-string 2 s)
  13151. '(("d" . 86400.0) ("w" . 604800.0)
  13152. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13153. (t (org-2ft s)))))
  13154. (defun org-match-any-p (re list)
  13155. "Does re match any element of list?"
  13156. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13157. (delq nil list))
  13158. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13159. (defvar org-tags-overlay (make-overlay 1 1))
  13160. (org-detach-overlay org-tags-overlay)
  13161. (defun org-get-local-tags-at (&optional pos)
  13162. "Get a list of tags defined in the current headline."
  13163. (org-get-tags-at pos 'local))
  13164. (defun org-get-local-tags ()
  13165. "Get a list of tags defined in the current headline."
  13166. (org-get-tags-at nil 'local))
  13167. (defun org-get-tags-at (&optional pos local)
  13168. "Get a list of all headline tags applicable at POS.
  13169. POS defaults to point. If tags are inherited, the list contains
  13170. the targets in the same sequence as the headlines appear, i.e.
  13171. the tags of the current headline come last.
  13172. When LOCAL is non-nil, only return tags from the current headline,
  13173. ignore inherited ones."
  13174. (interactive)
  13175. (if (and org-trust-scanner-tags
  13176. (or (not pos) (equal pos (point)))
  13177. (not local))
  13178. org-scanner-tags
  13179. (let (tags ltags lastpos parent)
  13180. (save-excursion
  13181. (save-restriction
  13182. (widen)
  13183. (goto-char (or pos (point)))
  13184. (save-match-data
  13185. (catch 'done
  13186. (condition-case nil
  13187. (progn
  13188. (org-back-to-heading t)
  13189. (while (not (equal lastpos (point)))
  13190. (setq lastpos (point))
  13191. (when (looking-at
  13192. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13193. (setq ltags (org-split-string
  13194. (org-match-string-no-properties 1) ":"))
  13195. (when parent
  13196. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13197. (setq tags (append
  13198. (if parent
  13199. (org-remove-uninherited-tags ltags)
  13200. ltags)
  13201. tags)))
  13202. (or org-use-tag-inheritance (throw 'done t))
  13203. (if local (throw 'done t))
  13204. (or (org-up-heading-safe) (error nil))
  13205. (setq parent t)))
  13206. (error nil)))))
  13207. (if local
  13208. tags
  13209. (reverse (delete-dups
  13210. (reverse (append
  13211. (org-remove-uninherited-tags
  13212. org-file-tags) tags)))))))))
  13213. (defun org-add-prop-inherited (s)
  13214. (add-text-properties 0 (length s) '(inherited t) s)
  13215. s)
  13216. (defun org-toggle-tag (tag &optional onoff)
  13217. "Toggle the tag TAG for the current line.
  13218. If ONOFF is `on' or `off', don't toggle but set to this state."
  13219. (let (res current)
  13220. (save-excursion
  13221. (org-back-to-heading t)
  13222. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13223. (point-at-eol) t)
  13224. (progn
  13225. (setq current (match-string 1))
  13226. (replace-match ""))
  13227. (setq current ""))
  13228. (setq current (nreverse (org-split-string current ":")))
  13229. (cond
  13230. ((eq onoff 'on)
  13231. (setq res t)
  13232. (or (member tag current) (push tag current)))
  13233. ((eq onoff 'off)
  13234. (or (not (member tag current)) (setq current (delete tag current))))
  13235. (t (if (member tag current)
  13236. (setq current (delete tag current))
  13237. (setq res t)
  13238. (push tag current))))
  13239. (end-of-line 1)
  13240. (if current
  13241. (progn
  13242. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13243. (org-set-tags nil t))
  13244. (delete-horizontal-space))
  13245. (run-hooks 'org-after-tags-change-hook))
  13246. res))
  13247. (defun org-align-tags-here (to-col)
  13248. ;; Assumes that this is a headline
  13249. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13250. (beginning-of-line 1)
  13251. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13252. (< pos (match-beginning 2)))
  13253. (progn
  13254. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13255. (goto-char (match-beginning 1))
  13256. (insert " ")
  13257. (delete-region (point) (1+ (match-beginning 2)))
  13258. (setq ncol (max (current-column)
  13259. (1+ col)
  13260. (if (> to-col 0)
  13261. to-col
  13262. (- (abs to-col) tags-l))))
  13263. (setq p (point))
  13264. (insert (make-string (- ncol (current-column)) ?\ ))
  13265. (setq ncol (current-column))
  13266. (when indent-tabs-mode (tabify p (point-at-eol)))
  13267. (org-move-to-column (min ncol col) t nil t))
  13268. (goto-char pos))))
  13269. (defun org-set-tags-command (&optional arg just-align)
  13270. "Call the set-tags command for the current entry."
  13271. (interactive "P")
  13272. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13273. (org-set-tags arg just-align)
  13274. (save-excursion
  13275. (unless (and (org-region-active-p)
  13276. org-loop-over-headlines-in-active-region)
  13277. (org-back-to-heading t))
  13278. (org-set-tags arg just-align))))
  13279. (defun org-set-tags-to (data)
  13280. "Set the tags of the current entry to DATA, replacing the current tags.
  13281. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13282. If DATA is nil or the empty string, any tags will be removed."
  13283. (interactive "sTags: ")
  13284. (setq data
  13285. (cond
  13286. ((eq data nil) "")
  13287. ((equal data "") "")
  13288. ((stringp data)
  13289. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13290. ":"))
  13291. ((listp data)
  13292. (concat ":" (mapconcat 'identity data ":") ":"))))
  13293. (when data
  13294. (save-excursion
  13295. (org-back-to-heading t)
  13296. (when (looking-at org-complex-heading-regexp)
  13297. (if (match-end 5)
  13298. (progn
  13299. (goto-char (match-beginning 5))
  13300. (insert data)
  13301. (delete-region (point) (point-at-eol))
  13302. (org-set-tags nil 'align))
  13303. (goto-char (point-at-eol))
  13304. (insert " " data)
  13305. (org-set-tags nil 'align)))
  13306. (beginning-of-line 1)
  13307. (if (looking-at ".*?\\([ \t]+\\)$")
  13308. (delete-region (match-beginning 1) (match-end 1))))))
  13309. (defun org-align-all-tags ()
  13310. "Align the tags i all headings."
  13311. (interactive)
  13312. (save-excursion
  13313. (or (ignore-errors (org-back-to-heading t))
  13314. (outline-next-heading))
  13315. (if (org-at-heading-p)
  13316. (org-set-tags t)
  13317. (message "No headings"))))
  13318. (defvar org-indent-indentation-per-level)
  13319. (defun org-set-tags (&optional arg just-align)
  13320. "Set the tags for the current headline.
  13321. With prefix ARG, realign all tags in headings in the current buffer.
  13322. When JUST-ALIGN is non-nil, only align tags.
  13323. When JUST-ALIGN is 'ignore-column, align tags without trying to set
  13324. the column by ignoring invisible text."
  13325. (interactive "P")
  13326. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13327. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13328. 'region-start-level 'region))
  13329. org-loop-over-headlines-in-active-region)
  13330. (org-map-entries
  13331. ;; We don't use ARG and JUST-ALIGN here because these args
  13332. ;; are not useful when looping over headlines.
  13333. `(org-set-tags)
  13334. org-loop-over-headlines-in-active-region
  13335. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13336. (let* ((re org-outline-regexp-bol)
  13337. (current (unless arg (org-get-tags-string)))
  13338. (col (current-column))
  13339. (org-setting-tags t)
  13340. table current-tags inherited-tags ; computed below when needed
  13341. tags p0 c0 c1 rpl di tc level)
  13342. (if arg
  13343. (save-excursion
  13344. (goto-char (point-min))
  13345. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13346. (while (re-search-forward re nil t)
  13347. (org-set-tags nil t)
  13348. (end-of-line 1)))
  13349. (message "All tags realigned to column %d" org-tags-column))
  13350. (if just-align
  13351. (setq tags current)
  13352. ;; Get a new set of tags from the user
  13353. (save-excursion
  13354. (setq table (append org-tag-persistent-alist
  13355. (or org-tag-alist (org-get-buffer-tags))
  13356. (and
  13357. org-complete-tags-always-offer-all-agenda-tags
  13358. (org-global-tags-completion-table
  13359. (org-agenda-files))))
  13360. org-last-tags-completion-table table
  13361. current-tags (org-split-string current ":")
  13362. inherited-tags (nreverse
  13363. (nthcdr (length current-tags)
  13364. (nreverse (org-get-tags-at))))
  13365. tags
  13366. (if (or (eq t org-use-fast-tag-selection)
  13367. (and org-use-fast-tag-selection
  13368. (delq nil (mapcar 'cdr table))))
  13369. (org-fast-tag-selection
  13370. current-tags inherited-tags table
  13371. (if org-fast-tag-selection-include-todo
  13372. org-todo-key-alist))
  13373. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13374. (org-trim
  13375. (org-icompleting-read "Tags: "
  13376. 'org-tags-completion-function
  13377. nil nil current 'org-tags-history))))))
  13378. (while (string-match "[-+&]+" tags)
  13379. ;; No boolean logic, just a list
  13380. (setq tags (replace-match ":" t t tags))))
  13381. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13382. (if org-tags-sort-function
  13383. (setq tags (mapconcat 'identity
  13384. (sort (org-split-string
  13385. tags (org-re "[^[:alnum:]_@#%]+"))
  13386. org-tags-sort-function) ":")))
  13387. (if (string-match "\\`[\t ]*\\'" tags)
  13388. (setq tags "")
  13389. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13390. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13391. ;; Insert new tags at the correct column
  13392. (beginning-of-line 1)
  13393. (setq level (or (and (looking-at org-outline-regexp)
  13394. (- (match-end 0) (point) 1))
  13395. 1))
  13396. (cond
  13397. ((and (equal current "") (equal tags "")))
  13398. ((re-search-forward
  13399. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13400. (point-at-eol) t)
  13401. (if (equal tags "")
  13402. (setq rpl "")
  13403. (goto-char (match-beginning 0))
  13404. (setq c0 (current-column)
  13405. ;; compute offset for the case of org-indent-mode active
  13406. di (if (org-bound-and-true-p org-indent-mode)
  13407. (* (1- org-indent-indentation-per-level) (1- level))
  13408. 0)
  13409. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13410. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13411. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13412. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13413. (replace-match rpl t t)
  13414. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13415. tags)
  13416. (t (error "Tags alignment failed")))
  13417. (org-move-to-column col nil nil (not (eq just-align 'ignore-column)))
  13418. (unless just-align
  13419. (run-hooks 'org-after-tags-change-hook))))))
  13420. (defun org-change-tag-in-region (beg end tag off)
  13421. "Add or remove TAG for each entry in the region.
  13422. This works in the agenda, and also in an org-mode buffer."
  13423. (interactive
  13424. (list (region-beginning) (region-end)
  13425. (let ((org-last-tags-completion-table
  13426. (if (derived-mode-p 'org-mode)
  13427. (org-uniquify
  13428. (delq nil (append (org-get-buffer-tags)
  13429. (org-global-tags-completion-table))))
  13430. (org-global-tags-completion-table))))
  13431. (org-icompleting-read
  13432. "Tag: " 'org-tags-completion-function nil nil nil
  13433. 'org-tags-history))
  13434. (progn
  13435. (message "[s]et or [r]emove? ")
  13436. (equal (read-char-exclusive) ?r))))
  13437. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13438. (let ((agendap (equal major-mode 'org-agenda-mode))
  13439. l1 l2 m buf pos newhead (cnt 0))
  13440. (goto-char end)
  13441. (setq l2 (1- (org-current-line)))
  13442. (goto-char beg)
  13443. (setq l1 (org-current-line))
  13444. (loop for l from l1 to l2 do
  13445. (org-goto-line l)
  13446. (setq m (get-text-property (point) 'org-hd-marker))
  13447. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13448. (and agendap m))
  13449. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13450. pos (if agendap m (point)))
  13451. (with-current-buffer buf
  13452. (save-excursion
  13453. (save-restriction
  13454. (goto-char pos)
  13455. (setq cnt (1+ cnt))
  13456. (org-toggle-tag tag (if off 'off 'on))
  13457. (setq newhead (org-get-heading)))))
  13458. (and agendap (org-agenda-change-all-lines newhead m))))
  13459. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13460. (defun org-tags-completion-function (string predicate &optional flag)
  13461. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13462. (confirm (lambda (x) (stringp (car x)))))
  13463. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13464. (setq s1 (match-string 1 string)
  13465. s2 (match-string 2 string))
  13466. (setq s1 "" s2 string))
  13467. (cond
  13468. ((eq flag nil)
  13469. ;; try completion
  13470. (setq rtn (try-completion s2 ctable confirm))
  13471. (if (stringp rtn)
  13472. (setq rtn
  13473. (concat s1 s2 (substring rtn (length s2))
  13474. (if (and org-add-colon-after-tag-completion
  13475. (assoc rtn ctable))
  13476. ":" ""))))
  13477. rtn)
  13478. ((eq flag t)
  13479. ;; all-completions
  13480. (all-completions s2 ctable confirm))
  13481. ((eq flag 'lambda)
  13482. ;; exact match?
  13483. (assoc s2 ctable)))))
  13484. (defun org-fast-tag-insert (kwd tags face &optional end)
  13485. "Insert KDW, and the TAGS, the latter with face FACE.
  13486. Also insert END."
  13487. (insert (format "%-12s" (concat kwd ":"))
  13488. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13489. (or end "")))
  13490. (defun org-fast-tag-show-exit (flag)
  13491. (save-excursion
  13492. (org-goto-line 3)
  13493. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13494. (replace-match ""))
  13495. (when flag
  13496. (end-of-line 1)
  13497. (org-move-to-column (- (window-width) 19) t)
  13498. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13499. (defun org-set-current-tags-overlay (current prefix)
  13500. "Add an overlay to CURRENT tag with PREFIX."
  13501. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13502. (if (featurep 'xemacs)
  13503. (org-overlay-display org-tags-overlay (concat prefix s)
  13504. 'secondary-selection)
  13505. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13506. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13507. (defvar org-last-tag-selection-key nil)
  13508. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13509. "Fast tag selection with single keys.
  13510. CURRENT is the current list of tags in the headline, INHERITED is the
  13511. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13512. possibly with grouping information. TODO-TABLE is a similar table with
  13513. TODO keywords, should these have keys assigned to them.
  13514. If the keys are nil, a-z are automatically assigned.
  13515. Returns the new tags string, or nil to not change the current settings."
  13516. (let* ((fulltable (append table todo-table))
  13517. (maxlen (apply 'max (mapcar
  13518. (lambda (x)
  13519. (if (stringp (car x)) (string-width (car x)) 0))
  13520. fulltable)))
  13521. (buf (current-buffer))
  13522. (expert (eq org-fast-tag-selection-single-key 'expert))
  13523. (buffer-tags nil)
  13524. (fwidth (+ maxlen 3 1 3))
  13525. (ncol (/ (- (window-width) 4) fwidth))
  13526. (i-face 'org-done)
  13527. (c-face 'org-todo)
  13528. tg cnt e c char c1 c2 ntable tbl rtn
  13529. ov-start ov-end ov-prefix
  13530. (exit-after-next org-fast-tag-selection-single-key)
  13531. (done-keywords org-done-keywords)
  13532. groups ingroup)
  13533. (save-excursion
  13534. (beginning-of-line 1)
  13535. (if (looking-at
  13536. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13537. (setq ov-start (match-beginning 1)
  13538. ov-end (match-end 1)
  13539. ov-prefix "")
  13540. (setq ov-start (1- (point-at-eol))
  13541. ov-end (1+ ov-start))
  13542. (skip-chars-forward "^\n\r")
  13543. (setq ov-prefix
  13544. (concat
  13545. (buffer-substring (1- (point)) (point))
  13546. (if (> (current-column) org-tags-column)
  13547. " "
  13548. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13549. (move-overlay org-tags-overlay ov-start ov-end)
  13550. (save-window-excursion
  13551. (if expert
  13552. (set-buffer (get-buffer-create " *Org tags*"))
  13553. (delete-other-windows)
  13554. (split-window-vertically)
  13555. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13556. (erase-buffer)
  13557. (org-set-local 'org-done-keywords done-keywords)
  13558. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13559. (org-fast-tag-insert "Current" current c-face "\n\n")
  13560. (org-fast-tag-show-exit exit-after-next)
  13561. (org-set-current-tags-overlay current ov-prefix)
  13562. (setq tbl fulltable char ?a cnt 0)
  13563. (while (setq e (pop tbl))
  13564. (cond
  13565. ((equal (car e) :startgroup)
  13566. (push '() groups) (setq ingroup t)
  13567. (when (not (= cnt 0))
  13568. (setq cnt 0)
  13569. (insert "\n"))
  13570. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13571. ((equal (car e) :endgroup)
  13572. (setq ingroup nil cnt 0)
  13573. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13574. ((equal e '(:newline))
  13575. (when (not (= cnt 0))
  13576. (setq cnt 0)
  13577. (insert "\n")
  13578. (setq e (car tbl))
  13579. (while (equal (car tbl) '(:newline))
  13580. (insert "\n")
  13581. (setq tbl (cdr tbl)))))
  13582. ((equal e '(:grouptags)) nil)
  13583. (t
  13584. (setq tg (copy-sequence (car e)) c2 nil)
  13585. (if (cdr e)
  13586. (setq c (cdr e))
  13587. ;; automatically assign a character.
  13588. (setq c1 (string-to-char
  13589. (downcase (substring
  13590. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13591. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13592. (while (or (rassoc char ntable) (rassoc char table))
  13593. (setq char (1+ char)))
  13594. (setq c2 c1))
  13595. (setq c (or c2 char)))
  13596. (if ingroup (push tg (car groups)))
  13597. (setq tg (org-add-props tg nil 'face
  13598. (cond
  13599. ((not (assoc tg table))
  13600. (org-get-todo-face tg))
  13601. ((member tg current) c-face)
  13602. ((member tg inherited) i-face))))
  13603. (if (equal (caar tbl) :grouptags)
  13604. (org-add-props tg nil 'face 'org-tag-group))
  13605. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13606. (insert "[" c "] " tg (make-string
  13607. (- fwidth 4 (length tg)) ?\ ))
  13608. (push (cons tg c) ntable)
  13609. (when (= (setq cnt (1+ cnt)) ncol)
  13610. (insert "\n")
  13611. (if ingroup (insert " "))
  13612. (setq cnt 0)))))
  13613. (setq ntable (nreverse ntable))
  13614. (insert "\n")
  13615. (goto-char (point-min))
  13616. (if (not expert) (org-fit-window-to-buffer))
  13617. (setq rtn
  13618. (catch 'exit
  13619. (while t
  13620. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13621. (if (not groups) "no " "")
  13622. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13623. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13624. (setq org-last-tag-selection-key c)
  13625. (cond
  13626. ((= c ?\r) (throw 'exit t))
  13627. ((= c ?!)
  13628. (setq groups (not groups))
  13629. (goto-char (point-min))
  13630. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13631. ((= c ?\C-c)
  13632. (if (not expert)
  13633. (org-fast-tag-show-exit
  13634. (setq exit-after-next (not exit-after-next)))
  13635. (setq expert nil)
  13636. (delete-other-windows)
  13637. (set-window-buffer (split-window-vertically) " *Org tags*")
  13638. (org-switch-to-buffer-other-window " *Org tags*")
  13639. (org-fit-window-to-buffer)))
  13640. ((or (= c ?\C-g)
  13641. (and (= c ?q) (not (rassoc c ntable))))
  13642. (org-detach-overlay org-tags-overlay)
  13643. (setq quit-flag t))
  13644. ((= c ?\ )
  13645. (setq current nil)
  13646. (if exit-after-next (setq exit-after-next 'now)))
  13647. ((= c ?\t)
  13648. (condition-case nil
  13649. (setq tg (org-icompleting-read
  13650. "Tag: "
  13651. (or buffer-tags
  13652. (with-current-buffer buf
  13653. (org-get-buffer-tags)))))
  13654. (quit (setq tg "")))
  13655. (when (string-match "\\S-" tg)
  13656. (add-to-list 'buffer-tags (list tg))
  13657. (if (member tg current)
  13658. (setq current (delete tg current))
  13659. (push tg current)))
  13660. (if exit-after-next (setq exit-after-next 'now)))
  13661. ((setq e (rassoc c todo-table) tg (car e))
  13662. (with-current-buffer buf
  13663. (save-excursion (org-todo tg)))
  13664. (if exit-after-next (setq exit-after-next 'now)))
  13665. ((setq e (rassoc c ntable) tg (car e))
  13666. (if (member tg current)
  13667. (setq current (delete tg current))
  13668. (loop for g in groups do
  13669. (if (member tg g)
  13670. (mapc (lambda (x)
  13671. (setq current (delete x current)))
  13672. g)))
  13673. (push tg current))
  13674. (if exit-after-next (setq exit-after-next 'now))))
  13675. ;; Create a sorted list
  13676. (setq current
  13677. (sort current
  13678. (lambda (a b)
  13679. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13680. (if (eq exit-after-next 'now) (throw 'exit t))
  13681. (goto-char (point-min))
  13682. (beginning-of-line 2)
  13683. (delete-region (point) (point-at-eol))
  13684. (org-fast-tag-insert "Current" current c-face)
  13685. (org-set-current-tags-overlay current ov-prefix)
  13686. (while (re-search-forward
  13687. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13688. (setq tg (match-string 1))
  13689. (add-text-properties
  13690. (match-beginning 1) (match-end 1)
  13691. (list 'face
  13692. (cond
  13693. ((member tg current) c-face)
  13694. ((member tg inherited) i-face)
  13695. (t (get-text-property (match-beginning 1) 'face))))))
  13696. (goto-char (point-min)))))
  13697. (org-detach-overlay org-tags-overlay)
  13698. (if rtn
  13699. (mapconcat 'identity current ":")
  13700. nil))))
  13701. (defun org-get-tags-string ()
  13702. "Get the TAGS string in the current headline."
  13703. (unless (org-at-heading-p t)
  13704. (user-error "Not on a heading"))
  13705. (save-excursion
  13706. (beginning-of-line 1)
  13707. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13708. (org-match-string-no-properties 1)
  13709. "")))
  13710. (defun org-get-tags ()
  13711. "Get the list of tags specified in the current headline."
  13712. (org-split-string (org-get-tags-string) ":"))
  13713. (defun org-get-buffer-tags ()
  13714. "Get a table of all tags used in the buffer, for completion."
  13715. (let (tags)
  13716. (save-excursion
  13717. (goto-char (point-min))
  13718. (while (re-search-forward
  13719. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13720. (when (equal (char-after (point-at-bol 0)) ?*)
  13721. (mapc (lambda (x) (add-to-list 'tags x))
  13722. (org-split-string (org-match-string-no-properties 1) ":")))))
  13723. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13724. (mapcar 'list tags)))
  13725. ;;;; The mapping API
  13726. (defun org-map-entries (func &optional match scope &rest skip)
  13727. "Call FUNC at each headline selected by MATCH in SCOPE.
  13728. FUNC is a function or a lisp form. The function will be called without
  13729. arguments, with the cursor positioned at the beginning of the headline.
  13730. The return values of all calls to the function will be collected and
  13731. returned as a list.
  13732. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13733. does not need to preserve point. After evaluation, the cursor will be
  13734. moved to the end of the line (presumably of the headline of the
  13735. processed entry) and search continues from there. Under some
  13736. circumstances, this may not produce the wanted results. For example,
  13737. if you have removed (e.g. archived) the current (sub)tree it could
  13738. mean that the next entry will be skipped entirely. In such cases, you
  13739. can specify the position from where search should continue by making
  13740. FUNC set the variable `org-map-continue-from' to the desired buffer
  13741. position.
  13742. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13743. Only headlines that are matched by this query will be considered during
  13744. the iteration. When MATCH is nil or t, all headlines will be
  13745. visited by the iteration.
  13746. SCOPE determines the scope of this command. It can be any of:
  13747. nil The current buffer, respecting the restriction if any
  13748. tree The subtree started with the entry at point
  13749. region The entries within the active region, if any
  13750. region-start-level
  13751. The entries within the active region, but only those at
  13752. the same level than the first one.
  13753. file The current buffer, without restriction
  13754. file-with-archives
  13755. The current buffer, and any archives associated with it
  13756. agenda All agenda files
  13757. agenda-with-archives
  13758. All agenda files with any archive files associated with them
  13759. \(file1 file2 ...)
  13760. If this is a list, all files in the list will be scanned
  13761. The remaining args are treated as settings for the skipping facilities of
  13762. the scanner. The following items can be given here:
  13763. archive skip trees with the archive tag
  13764. comment skip trees with the COMMENT keyword
  13765. function or Emacs Lisp form:
  13766. will be used as value for `org-agenda-skip-function', so
  13767. whenever the function returns a position, FUNC will not be
  13768. called for that entry and search will continue from the
  13769. position returned
  13770. If your function needs to retrieve the tags including inherited tags
  13771. at the *current* entry, you can use the value of the variable
  13772. `org-scanner-tags' which will be much faster than getting the value
  13773. with `org-get-tags-at'. If your function gets properties with
  13774. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13775. to t around the call to `org-entry-properties' to get the same speedup.
  13776. Note that if your function moves around to retrieve tags and properties at
  13777. a *different* entry, you cannot use these techniques."
  13778. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13779. (not (org-region-active-p)))
  13780. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13781. (org-agenda-skip-archived-trees (memq 'archive skip))
  13782. (org-agenda-skip-comment-trees (memq 'comment skip))
  13783. (org-agenda-skip-function
  13784. (car (org-delete-all '(comment archive) skip)))
  13785. (org-tags-match-list-sublevels t)
  13786. (start-level (eq scope 'region-start-level))
  13787. matcher file res
  13788. org-todo-keywords-for-agenda
  13789. org-done-keywords-for-agenda
  13790. org-todo-keyword-alist-for-agenda
  13791. org-tag-alist-for-agenda
  13792. todo-only)
  13793. (cond
  13794. ((eq match t) (setq matcher t))
  13795. ((eq match nil) (setq matcher t))
  13796. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13797. (save-excursion
  13798. (save-restriction
  13799. (cond ((eq scope 'tree)
  13800. (org-back-to-heading t)
  13801. (org-narrow-to-subtree)
  13802. (setq scope nil))
  13803. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13804. (org-region-active-p))
  13805. ;; If needed, set start-level to a string like "2"
  13806. (when start-level
  13807. (save-excursion
  13808. (goto-char (region-beginning))
  13809. (unless (org-at-heading-p) (outline-next-heading))
  13810. (setq start-level (org-current-level))))
  13811. (narrow-to-region (region-beginning)
  13812. (save-excursion
  13813. (goto-char (region-end))
  13814. (unless (and (bolp) (org-at-heading-p))
  13815. (outline-next-heading))
  13816. (point)))
  13817. (setq scope nil)))
  13818. (if (not scope)
  13819. (progn
  13820. (org-agenda-prepare-buffers
  13821. (list (buffer-file-name (current-buffer))))
  13822. (setq res (org-scan-tags func matcher todo-only start-level)))
  13823. ;; Get the right scope
  13824. (cond
  13825. ((and scope (listp scope) (symbolp (car scope)))
  13826. (setq scope (eval scope)))
  13827. ((eq scope 'agenda)
  13828. (setq scope (org-agenda-files t)))
  13829. ((eq scope 'agenda-with-archives)
  13830. (setq scope (org-agenda-files t))
  13831. (setq scope (org-add-archive-files scope)))
  13832. ((eq scope 'file)
  13833. (setq scope (list (buffer-file-name))))
  13834. ((eq scope 'file-with-archives)
  13835. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13836. (org-agenda-prepare-buffers scope)
  13837. (while (setq file (pop scope))
  13838. (with-current-buffer (org-find-base-buffer-visiting file)
  13839. (save-excursion
  13840. (save-restriction
  13841. (widen)
  13842. (goto-char (point-min))
  13843. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13844. res)))
  13845. ;;;; Properties
  13846. ;;; Setting and retrieving properties
  13847. (defconst org-special-properties
  13848. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13849. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13850. "The special properties valid in Org-mode.
  13851. These are properties that are not defined in the property drawer,
  13852. but in some other way.")
  13853. (defconst org-default-properties
  13854. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13855. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13856. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13857. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13858. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13859. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13860. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13861. "Some properties that are used by Org-mode for various purposes.
  13862. Being in this list makes sure that they are offered for completion.")
  13863. (defun org-property-action ()
  13864. "Do an action on properties."
  13865. (interactive)
  13866. (let (c)
  13867. (org-at-property-p)
  13868. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13869. (setq c (read-char-exclusive))
  13870. (cond
  13871. ((equal c ?s)
  13872. (call-interactively 'org-set-property))
  13873. ((equal c ?d)
  13874. (call-interactively 'org-delete-property))
  13875. ((equal c ?D)
  13876. (call-interactively 'org-delete-property-globally))
  13877. ((equal c ?c)
  13878. (call-interactively 'org-compute-property-at-point))
  13879. (t (user-error "No such property action %c" c)))))
  13880. (defun org-inc-effort ()
  13881. "Increment the value of the effort property in the current entry."
  13882. (interactive)
  13883. (org-set-effort nil t))
  13884. (defvar org-clock-effort) ;; Defined in org-clock.el
  13885. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13886. (defun org-set-effort (&optional value increment)
  13887. "Set the effort property of the current entry.
  13888. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13889. 10th allowed value.
  13890. When INCREMENT is non-nil, set the property to the next allowed value."
  13891. (interactive "P")
  13892. (if (equal value 0) (setq value 10))
  13893. (let* ((completion-ignore-case t)
  13894. (prop org-effort-property)
  13895. (cur (org-entry-get nil prop))
  13896. (allowed (org-property-get-allowed-values nil prop 'table))
  13897. (existing (mapcar 'list (org-property-values prop)))
  13898. (heading (nth 4 (org-heading-components)))
  13899. rpl
  13900. (val (cond
  13901. ((stringp value) value)
  13902. ((and allowed (integerp value))
  13903. (or (car (nth (1- value) allowed))
  13904. (car (org-last allowed))))
  13905. ((and allowed increment)
  13906. (or (caadr (member (list cur) allowed))
  13907. (user-error "Allowed effort values are not set")))
  13908. (allowed
  13909. (message "Select 1-9,0, [RET%s]: %s"
  13910. (if cur (concat "=" cur) "")
  13911. (mapconcat 'car allowed " "))
  13912. (setq rpl (read-char-exclusive))
  13913. (if (equal rpl ?\r)
  13914. cur
  13915. (setq rpl (- rpl ?0))
  13916. (if (equal rpl 0) (setq rpl 10))
  13917. (if (and (> rpl 0) (<= rpl (length allowed)))
  13918. (car (nth (1- rpl) allowed))
  13919. (org-completing-read "Effort: " allowed nil))))
  13920. (t
  13921. (let (org-completion-use-ido org-completion-use-iswitchb)
  13922. (org-completing-read
  13923. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13924. (concat "[" cur "]") "")
  13925. ": ")
  13926. existing nil nil "" nil cur))))))
  13927. (unless (equal (org-entry-get nil prop) val)
  13928. (org-entry-put nil prop val))
  13929. (save-excursion
  13930. (org-back-to-heading t)
  13931. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13932. (when (string= heading org-clock-current-task)
  13933. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13934. (org-clock-update-mode-line))
  13935. (message "%s is now %s" prop val)))
  13936. (defun org-at-property-p ()
  13937. "Is cursor inside a property drawer?"
  13938. (save-excursion
  13939. (when (equal 'node-property (car (org-element-at-point)))
  13940. (beginning-of-line 1)
  13941. (looking-at org-property-re))))
  13942. (defun org-get-property-block (&optional beg end force)
  13943. "Return the (beg . end) range of the body of the property drawer.
  13944. BEG and END are the beginning and end of the current subtree, or of
  13945. the part before the first headline. If they are not given, they will
  13946. be found. If the drawer does not exist and FORCE is non-nil, create
  13947. the drawer."
  13948. (catch 'exit
  13949. (save-excursion
  13950. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13951. (progn (org-back-to-heading t) (point))))
  13952. (end (or end (and (not (outline-next-heading)) (point-max))
  13953. (point))))
  13954. (goto-char beg)
  13955. (if (re-search-forward org-property-start-re end t)
  13956. (setq beg (1+ (match-end 0)))
  13957. (if force
  13958. (save-excursion
  13959. (org-insert-property-drawer)
  13960. (setq end (progn (outline-next-heading) (point))))
  13961. (throw 'exit nil))
  13962. (goto-char beg)
  13963. (if (re-search-forward org-property-start-re end t)
  13964. (setq beg (1+ (match-end 0)))))
  13965. (if (re-search-forward org-property-end-re end t)
  13966. (setq end (match-beginning 0))
  13967. (or force (throw 'exit nil))
  13968. (goto-char beg)
  13969. (setq end beg)
  13970. (org-indent-line)
  13971. (insert ":END:\n"))
  13972. (cons beg end)))))
  13973. (defun org-entry-properties (&optional pom which specific)
  13974. "Get all properties of the entry at point-or-marker POM.
  13975. This includes the TODO keyword, the tags, time strings for deadline,
  13976. scheduled, and clocking, and any additional properties defined in the
  13977. entry. The return value is an alist, keys may occur multiple times
  13978. if the property key was used several times.
  13979. POM may also be nil, in which case the current entry is used.
  13980. If WHICH is nil or `all', get all properties. If WHICH is
  13981. `special' or `standard', only get that subclass. If WHICH
  13982. is a string only get exactly this property. SPECIFIC can be a string, the
  13983. specific property we are interested in. Specifying it can speed
  13984. things up because then unnecessary parsing is avoided."
  13985. (setq which (or which 'all))
  13986. (org-with-wide-buffer
  13987. (org-with-point-at pom
  13988. (let ((clockstr (substring org-clock-string 0 -1))
  13989. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13990. (case-fold-search nil)
  13991. beg end range props sum-props key key1 value string clocksum clocksumt)
  13992. (when (and (derived-mode-p 'org-mode)
  13993. (ignore-errors (org-back-to-heading t)))
  13994. (setq beg (point))
  13995. (setq sum-props (get-text-property (point) 'org-summaries))
  13996. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13997. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13998. (outline-next-heading)
  13999. (setq end (point))
  14000. (when (memq which '(all special))
  14001. ;; Get the special properties, like TODO and tags
  14002. (goto-char beg)
  14003. (when (and (or (not specific) (string= specific "TODO"))
  14004. (looking-at org-todo-line-regexp) (match-end 2))
  14005. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14006. (when (and (or (not specific) (string= specific "PRIORITY"))
  14007. (looking-at org-priority-regexp))
  14008. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14009. (when (or (not specific) (string= specific "FILE"))
  14010. (push (cons "FILE" buffer-file-name) props))
  14011. (when (and (or (not specific) (string= specific "TAGS"))
  14012. (setq value (org-get-tags-string))
  14013. (string-match "\\S-" value))
  14014. (push (cons "TAGS" value) props))
  14015. (when (and (or (not specific) (string= specific "ALLTAGS"))
  14016. (setq value (org-get-tags-at)))
  14017. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  14018. ":"))
  14019. props))
  14020. (when (or (not specific) (string= specific "BLOCKED"))
  14021. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  14022. (when (or (not specific)
  14023. (member specific
  14024. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  14025. "TIMESTAMP" "TIMESTAMP_IA")))
  14026. (catch 'match
  14027. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  14028. (setq key (if (match-end 1)
  14029. (substring (org-match-string-no-properties 1)
  14030. 0 -1))
  14031. string (if (equal key clockstr)
  14032. (org-trim
  14033. (buffer-substring-no-properties
  14034. (match-beginning 3) (goto-char
  14035. (point-at-eol))))
  14036. (substring (org-match-string-no-properties 3)
  14037. 1 -1)))
  14038. ;; Get the correct property name from the key. This is
  14039. ;; necessary if the user has configured time keywords.
  14040. (setq key1 (concat key ":"))
  14041. (cond
  14042. ((not key)
  14043. (setq key
  14044. (if (= (char-after (match-beginning 3)) ?\[)
  14045. "TIMESTAMP_IA" "TIMESTAMP")))
  14046. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  14047. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  14048. ((equal key1 org-closed-string) (setq key "CLOSED"))
  14049. ((equal key1 org-clock-string) (setq key "CLOCK")))
  14050. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  14051. (progn
  14052. (push (cons key string) props)
  14053. ;; no need to search further if match is found
  14054. (throw 'match t))
  14055. (when (or (equal key "CLOCK") (not (assoc key props)))
  14056. (push (cons key string) props)))))))
  14057. (when (memq which '(all standard))
  14058. ;; Get the standard properties, like :PROP: ...
  14059. (setq range (org-get-property-block beg end))
  14060. (when range
  14061. (goto-char (car range))
  14062. (while (re-search-forward org-property-re
  14063. (cdr range) t)
  14064. (setq key (org-match-string-no-properties 2)
  14065. value (org-trim (or (org-match-string-no-properties 3) "")))
  14066. (unless (member key excluded)
  14067. (push (cons key (or value "")) props)))))
  14068. (if clocksum
  14069. (push (cons "CLOCKSUM"
  14070. (org-columns-number-to-string (/ (float clocksum) 60.)
  14071. 'add_times))
  14072. props))
  14073. (if clocksumt
  14074. (push (cons "CLOCKSUM_T"
  14075. (org-columns-number-to-string (/ (float clocksumt) 60.)
  14076. 'add_times))
  14077. props))
  14078. (unless (assoc "CATEGORY" props)
  14079. (push (cons "CATEGORY" (org-get-category)) props))
  14080. (append sum-props (nreverse props)))))))
  14081. (defun org-entry-get (pom property &optional inherit literal-nil)
  14082. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14083. If INHERIT is non-nil and the entry does not have the property,
  14084. then also check higher levels of the hierarchy.
  14085. If INHERIT is the symbol `selective', use inheritance only if the setting
  14086. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  14087. If the property is present but empty, the return value is the empty string.
  14088. If the property is not present at all, nil is returned.
  14089. Return the value as a string.
  14090. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  14091. do not interpret it as the list atom nil. This is used for inheritance
  14092. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  14093. (org-with-point-at pom
  14094. (if (and inherit (if (eq inherit 'selective)
  14095. (org-property-inherit-p property)
  14096. t))
  14097. (org-entry-get-with-inheritance property literal-nil)
  14098. (if (member property org-special-properties)
  14099. ;; We need a special property. Use `org-entry-properties'
  14100. ;; to retrieve it, but specify the wanted property
  14101. (cdr (assoc property (org-entry-properties nil 'special property)))
  14102. (org-with-wide-buffer
  14103. (let ((range (org-get-property-block)))
  14104. (when (and range (not (eq (car range) (cdr range))))
  14105. (let* ((props
  14106. (list (or (assoc property org-file-properties)
  14107. (assoc property org-global-properties)
  14108. (assoc property org-global-properties-fixed))))
  14109. (ap (lambda (key)
  14110. (when (re-search-forward
  14111. (org-re-property key) (cdr range) t)
  14112. (setq props
  14113. (org-update-property-plist
  14114. key
  14115. (if (match-end 3)
  14116. (org-match-string-no-properties 3) "")
  14117. props)))))
  14118. val)
  14119. (goto-char (car range))
  14120. (funcall ap property)
  14121. (goto-char (car range))
  14122. (while (funcall ap (concat property "+")))
  14123. (setq val (cdr (assoc property props)))
  14124. (when val (if literal-nil val (org-not-nil val)))))))))))
  14125. (defun org-property-or-variable-value (var &optional inherit)
  14126. "Check if there is a property fixing the value of VAR.
  14127. If yes, return this value. If not, return the current value of the variable."
  14128. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14129. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14130. (read prop)
  14131. (symbol-value var))))
  14132. (defun org-entry-delete (pom property)
  14133. "Delete the property PROPERTY from entry at point-or-marker POM."
  14134. (unless (member property org-special-properties)
  14135. (org-with-point-at pom
  14136. (let ((range (org-get-property-block)))
  14137. (if (and range
  14138. (goto-char (car range))
  14139. (re-search-forward
  14140. (org-re-property property)
  14141. (cdr range) t))
  14142. (progn
  14143. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  14144. (org-remove-empty-drawer-at (car range))
  14145. t)
  14146. nil)))))
  14147. ;; Multi-values properties are properties that contain multiple values
  14148. ;; These values are assumed to be single words, separated by whitespace.
  14149. (defun org-entry-add-to-multivalued-property (pom property value)
  14150. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14151. (let* ((old (org-entry-get pom property))
  14152. (values (and old (org-split-string old "[ \t]"))))
  14153. (setq value (org-entry-protect-space value))
  14154. (unless (member value values)
  14155. (setq values (append values (list value)))
  14156. (org-entry-put pom property
  14157. (mapconcat 'identity values " ")))))
  14158. (defun org-entry-remove-from-multivalued-property (pom property value)
  14159. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14160. (let* ((old (org-entry-get pom property))
  14161. (values (and old (org-split-string old "[ \t]"))))
  14162. (setq value (org-entry-protect-space value))
  14163. (when (member value values)
  14164. (setq values (delete value values))
  14165. (org-entry-put pom property
  14166. (mapconcat 'identity values " ")))))
  14167. (defun org-entry-member-in-multivalued-property (pom property value)
  14168. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14169. (let* ((old (org-entry-get pom property))
  14170. (values (and old (org-split-string old "[ \t]"))))
  14171. (setq value (org-entry-protect-space value))
  14172. (member value values)))
  14173. (defun org-entry-get-multivalued-property (pom property)
  14174. "Return a list of values in a multivalued property."
  14175. (let* ((value (org-entry-get pom property))
  14176. (values (and value (org-split-string value "[ \t]"))))
  14177. (mapcar 'org-entry-restore-space values)))
  14178. (defun org-entry-put-multivalued-property (pom property &rest values)
  14179. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14180. VALUES should be a list of strings. Spaces will be protected."
  14181. (org-entry-put pom property
  14182. (mapconcat 'org-entry-protect-space values " "))
  14183. (let* ((value (org-entry-get pom property))
  14184. (values (and value (org-split-string value "[ \t]"))))
  14185. (mapcar 'org-entry-restore-space values)))
  14186. (defun org-entry-protect-space (s)
  14187. "Protect spaces and newline in string S."
  14188. (while (string-match " " s)
  14189. (setq s (replace-match "%20" t t s)))
  14190. (while (string-match "\n" s)
  14191. (setq s (replace-match "%0A" t t s)))
  14192. s)
  14193. (defun org-entry-restore-space (s)
  14194. "Restore spaces and newline in string S."
  14195. (while (string-match "%20" s)
  14196. (setq s (replace-match " " t t s)))
  14197. (while (string-match "%0A" s)
  14198. (setq s (replace-match "\n" t t s)))
  14199. s)
  14200. (defvar org-entry-property-inherited-from (make-marker)
  14201. "Marker pointing to the entry from where a property was inherited.
  14202. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14203. location of the entry where the inheritance search matched. If there was
  14204. no match, the marker will point nowhere.
  14205. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14206. is set.")
  14207. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14208. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14209. The search will stop at the first ancestor which has the property defined.
  14210. If the value found is \"nil\", return nil to show that the property
  14211. should be considered as undefined (this is the meaning of nil here).
  14212. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14213. (move-marker org-entry-property-inherited-from nil)
  14214. (let (tmp)
  14215. (save-excursion
  14216. (save-restriction
  14217. (widen)
  14218. (catch 'ex
  14219. (while t
  14220. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14221. (or (ignore-errors (org-back-to-heading t))
  14222. (goto-char (point-min)))
  14223. (move-marker org-entry-property-inherited-from (point))
  14224. (throw 'ex tmp))
  14225. (or (ignore-errors (org-up-heading-safe))
  14226. (throw 'ex nil))))))
  14227. (setq tmp (or tmp
  14228. (cdr (assoc property org-file-properties))
  14229. (cdr (assoc property org-global-properties))
  14230. (cdr (assoc property org-global-properties-fixed))))
  14231. (if literal-nil tmp (org-not-nil tmp))))
  14232. (defvar org-property-changed-functions nil
  14233. "Hook called when the value of a property has changed.
  14234. Each hook function should accept two arguments, the name of the property
  14235. and the new value.")
  14236. (defun org-entry-put (pom property value)
  14237. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14238. If the value is `nil', it is converted to the empty string.
  14239. If it is not a string, an error is raised."
  14240. (cond ((null value) (setq value ""))
  14241. ((not (stringp value))
  14242. (error "Properties values should be strings.")))
  14243. (org-with-point-at pom
  14244. (org-back-to-heading t)
  14245. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14246. range)
  14247. (cond
  14248. ((equal property "TODO")
  14249. (when (and (string-match "\\S-" value)
  14250. (not (member value org-todo-keywords-1)))
  14251. (user-error "\"%s\" is not a valid TODO state" value))
  14252. (if (or (not value)
  14253. (not (string-match "\\S-" value)))
  14254. (setq value 'none))
  14255. (org-todo value)
  14256. (org-set-tags nil 'align))
  14257. ((equal property "PRIORITY")
  14258. (org-priority (if (and value (string-match "\\S-" value))
  14259. (string-to-char value) ?\ ))
  14260. (org-set-tags nil 'align))
  14261. ((equal property "CLOCKSUM")
  14262. (if (not (re-search-forward
  14263. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14264. (error "Cannot find a clock log")
  14265. (goto-char (- (match-end 1) 2))
  14266. (cond
  14267. ((eq value 'earlier) (org-timestamp-down))
  14268. ((eq value 'later) (org-timestamp-up)))
  14269. (org-clock-sum-current-item)))
  14270. ((equal property "SCHEDULED")
  14271. (if (re-search-forward org-scheduled-time-regexp end t)
  14272. (cond
  14273. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14274. ((eq value 'later) (org-timestamp-change 1 'day))
  14275. (t (call-interactively 'org-schedule)))
  14276. (call-interactively 'org-schedule)))
  14277. ((equal property "DEADLINE")
  14278. (if (re-search-forward org-deadline-time-regexp end t)
  14279. (cond
  14280. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14281. ((eq value 'later) (org-timestamp-change 1 'day))
  14282. (t (call-interactively 'org-deadline)))
  14283. (call-interactively 'org-deadline)))
  14284. ((member property org-special-properties)
  14285. (error "The %s property can not yet be set with `org-entry-put'"
  14286. property))
  14287. (t ; a non-special property
  14288. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14289. (setq range (org-get-property-block beg end 'force))
  14290. (goto-char (car range))
  14291. (if (re-search-forward
  14292. (org-re-property property) (cdr range) t)
  14293. (progn
  14294. (delete-region (match-beginning 0) (match-end 0))
  14295. (goto-char (match-beginning 0)))
  14296. (goto-char (cdr range))
  14297. (insert "\n")
  14298. (backward-char 1)
  14299. (org-indent-line))
  14300. (insert ":" property ":")
  14301. (and value (insert " " value))
  14302. (org-indent-line)))))
  14303. (run-hook-with-args 'org-property-changed-functions property value)))
  14304. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14305. "Get all property keys in the current buffer.
  14306. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14307. like tags and TODO state.
  14308. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14309. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14310. and others.
  14311. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14312. formats in the current buffer."
  14313. (let (rtn range cfmt s p)
  14314. (save-excursion
  14315. (save-restriction
  14316. (widen)
  14317. (goto-char (point-min))
  14318. (while (re-search-forward org-property-start-re nil t)
  14319. (setq range (org-get-property-block))
  14320. (goto-char (car range))
  14321. (while (re-search-forward org-property-re
  14322. (cdr range) t)
  14323. (add-to-list 'rtn (org-match-string-no-properties 2)))
  14324. (outline-next-heading))))
  14325. (when include-specials
  14326. (setq rtn (append org-special-properties rtn)))
  14327. (when include-defaults
  14328. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14329. (add-to-list 'rtn org-effort-property))
  14330. (when include-columns
  14331. (save-excursion
  14332. (save-restriction
  14333. (widen)
  14334. (goto-char (point-min))
  14335. (while (re-search-forward
  14336. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14337. nil t)
  14338. (setq cfmt (match-string 2) s 0)
  14339. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14340. cfmt s)
  14341. (setq s (match-end 0)
  14342. p (match-string 1 cfmt))
  14343. (unless (or (equal p "ITEM")
  14344. (member p org-special-properties))
  14345. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14346. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14347. (defun org-property-values (key)
  14348. "Return a list of all values of property KEY in the current buffer."
  14349. (save-excursion
  14350. (save-restriction
  14351. (widen)
  14352. (goto-char (point-min))
  14353. (let ((re (org-re-property key))
  14354. values)
  14355. (while (re-search-forward re nil t)
  14356. (add-to-list 'values (org-trim (match-string 3))))
  14357. (delete "" values)))))
  14358. (defun org-insert-property-drawer ()
  14359. "Insert a property drawer into the current entry."
  14360. (org-back-to-heading t)
  14361. (looking-at org-outline-regexp)
  14362. (let ((indent (if org-adapt-indentation
  14363. (- (match-end 0) (match-beginning 0))
  14364. 0))
  14365. (beg (point))
  14366. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14367. end hiddenp)
  14368. (outline-next-heading)
  14369. (setq end (point))
  14370. (goto-char beg)
  14371. (while (re-search-forward re end t))
  14372. (setq hiddenp (outline-invisible-p))
  14373. (end-of-line 1)
  14374. (and (equal (char-after) ?\n) (forward-char 1))
  14375. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14376. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14377. ;; just skip this line
  14378. (beginning-of-line 2)
  14379. ;; Drawer start, find the end
  14380. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14381. (beginning-of-line 1)))
  14382. (org-skip-over-state-notes)
  14383. (skip-chars-backward " \t\n\r")
  14384. (if (and (eq (char-before) ?*) (not (eq (char-after) ?\n)))
  14385. (forward-char 1))
  14386. (goto-char (point-at-eol))
  14387. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14388. (beginning-of-line 0)
  14389. (org-indent-to-column indent)
  14390. (beginning-of-line 2)
  14391. (org-indent-to-column indent)
  14392. (beginning-of-line 0)
  14393. (if hiddenp
  14394. (save-excursion
  14395. (org-back-to-heading t)
  14396. (hide-entry))
  14397. (org-flag-drawer t))))
  14398. (defun org-insert-drawer (&optional arg drawer)
  14399. "Insert a drawer at point.
  14400. Optional argument DRAWER, when non-nil, is a string representing
  14401. drawer's name. Otherwise, the user is prompted for a name.
  14402. If a region is active, insert the drawer around that region
  14403. instead.
  14404. Point is left between drawer's boundaries."
  14405. (interactive "P")
  14406. (let* ((drawer (if arg "PROPERTIES"
  14407. (or drawer (read-from-minibuffer "Drawer: ")))))
  14408. (cond
  14409. ;; With C-u, fall back on `org-insert-property-drawer'
  14410. (arg (org-insert-property-drawer))
  14411. ;;
  14412. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14413. (user-error "Invalid drawer name"))
  14414. ;; With an active region, insert a drawer at point.
  14415. ((not (org-region-active-p))
  14416. (progn
  14417. (unless (bolp) (insert "\n"))
  14418. (insert (format ":%s:\n\n:END:\n" drawer))
  14419. (forward-line -2)))
  14420. ;; Otherwise, insert the drawer at point
  14421. (t
  14422. (let ((rbeg (region-beginning))
  14423. (rend (copy-marker (region-end))))
  14424. (unwind-protect
  14425. (progn
  14426. (goto-char rbeg)
  14427. (beginning-of-line)
  14428. (when (save-excursion
  14429. (re-search-forward org-outline-regexp-bol rend t))
  14430. (user-error "Drawers cannot contain headlines"))
  14431. ;; Position point at the beginning of the first
  14432. ;; non-blank line in region. Insert drawer's opening
  14433. ;; there, then indent it.
  14434. (org-skip-whitespace)
  14435. (beginning-of-line)
  14436. (insert ":" drawer ":\n")
  14437. (forward-line -1)
  14438. (indent-for-tab-command)
  14439. ;; Move point to the beginning of the first blank line
  14440. ;; after the last non-blank line in region. Insert
  14441. ;; drawer's closing, then indent it.
  14442. (goto-char rend)
  14443. (skip-chars-backward " \r\t\n")
  14444. (insert "\n:END:")
  14445. (deactivate-mark t)
  14446. (indent-for-tab-command)
  14447. (unless (eolp) (insert "\n")))
  14448. ;; Clear marker, whatever the outcome of insertion is.
  14449. (set-marker rend nil)))))))
  14450. (defvar org-property-set-functions-alist nil
  14451. "Property set function alist.
  14452. Each entry should have the following format:
  14453. (PROPERTY . READ-FUNCTION)
  14454. The read function will be called with the same argument as
  14455. `org-completing-read'.")
  14456. (defun org-set-property-function (property)
  14457. "Get the function that should be used to set PROPERTY.
  14458. This is computed according to `org-property-set-functions-alist'."
  14459. (or (cdr (assoc property org-property-set-functions-alist))
  14460. 'org-completing-read))
  14461. (defun org-read-property-value (property)
  14462. "Read PROPERTY value from user."
  14463. (let* ((completion-ignore-case t)
  14464. (allowed (org-property-get-allowed-values nil property 'table))
  14465. (cur (org-entry-get nil property))
  14466. (prompt (concat property " value"
  14467. (if (and cur (string-match "\\S-" cur))
  14468. (concat " [" cur "]") "") ": "))
  14469. (set-function (org-set-property-function property))
  14470. (val (if allowed
  14471. (funcall set-function prompt allowed nil
  14472. (not (get-text-property 0 'org-unrestricted
  14473. (caar allowed))))
  14474. (let (org-completion-use-ido org-completion-use-iswitchb)
  14475. (funcall set-function prompt
  14476. (mapcar 'list (org-property-values property))
  14477. nil nil "" nil cur)))))
  14478. (if (equal val "")
  14479. cur
  14480. val)))
  14481. (defvar org-last-set-property nil)
  14482. (defvar org-last-set-property-value nil)
  14483. (defun org-read-property-name ()
  14484. "Read a property name."
  14485. (let* ((completion-ignore-case t)
  14486. (keys (org-buffer-property-keys nil t t))
  14487. (default-prop (or (save-excursion
  14488. (save-match-data
  14489. (beginning-of-line)
  14490. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14491. (null (string= (match-string 1) "END"))
  14492. (match-string 1))))
  14493. org-last-set-property))
  14494. (property (org-icompleting-read
  14495. (concat "Property"
  14496. (if default-prop (concat " [" default-prop "]") "")
  14497. ": ")
  14498. (mapcar 'list keys)
  14499. nil nil nil nil
  14500. default-prop)))
  14501. (if (member property keys)
  14502. property
  14503. (or (cdr (assoc (downcase property)
  14504. (mapcar (lambda (x) (cons (downcase x) x))
  14505. keys)))
  14506. property))))
  14507. (defun org-set-property-and-value (use-last)
  14508. "Allow to set [PROPERTY]: [value] direction from prompt.
  14509. When use-default, don't even ask, just use the last
  14510. \"[PROPERTY]: [value]\" string from the history."
  14511. (interactive "P")
  14512. (let* ((completion-ignore-case t)
  14513. (pv (or (and use-last org-last-set-property-value)
  14514. (org-completing-read
  14515. "Enter a \"[Property]: [value]\" pair: "
  14516. nil nil nil nil nil
  14517. org-last-set-property-value)))
  14518. prop val)
  14519. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14520. (setq prop (match-string 1 pv)
  14521. val (match-string 2 pv))
  14522. (org-set-property prop val))))
  14523. (defun org-set-property (property value)
  14524. "In the current entry, set PROPERTY to VALUE.
  14525. When called interactively, this will prompt for a property name, offering
  14526. completion on existing and default properties. And then it will prompt
  14527. for a value, offering completion either on allowed values (via an inherited
  14528. xxx_ALL property) or on existing values in other instances of this property
  14529. in the current file."
  14530. (interactive (list nil nil))
  14531. (let* ((property (or property (org-read-property-name)))
  14532. (value (or value (org-read-property-value property)))
  14533. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14534. (setq org-last-set-property property)
  14535. (setq org-last-set-property-value (concat property ": " value))
  14536. ;; Possibly postprocess the inserted value:
  14537. (when fn (setq value (funcall fn value)))
  14538. (unless (equal (org-entry-get nil property) value)
  14539. (org-entry-put nil property value))))
  14540. (defun org-delete-property (property)
  14541. "In the current entry, delete PROPERTY."
  14542. (interactive
  14543. (let* ((completion-ignore-case t)
  14544. (prop (org-icompleting-read "Property: "
  14545. (org-entry-properties nil 'standard))))
  14546. (list prop)))
  14547. (message "Property %s %s" property
  14548. (if (org-entry-delete nil property)
  14549. "deleted"
  14550. "was not present in the entry")))
  14551. (defun org-delete-property-globally (property)
  14552. "Remove PROPERTY globally, from all entries."
  14553. (interactive
  14554. (let* ((completion-ignore-case t)
  14555. (prop (org-icompleting-read
  14556. "Globally remove property: "
  14557. (mapcar 'list (org-buffer-property-keys)))))
  14558. (list prop)))
  14559. (save-excursion
  14560. (save-restriction
  14561. (widen)
  14562. (goto-char (point-min))
  14563. (let ((cnt 0))
  14564. (while (re-search-forward
  14565. (org-re-property property)
  14566. nil t)
  14567. (setq cnt (1+ cnt))
  14568. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14569. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14570. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14571. (defun org-compute-property-at-point ()
  14572. "Compute the property at point.
  14573. This looks for an enclosing column format, extracts the operator and
  14574. then applies it to the property in the column format's scope."
  14575. (interactive)
  14576. (unless (org-at-property-p)
  14577. (user-error "Not at a property"))
  14578. (let ((prop (org-match-string-no-properties 2)))
  14579. (org-columns-get-format-and-top-level)
  14580. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14581. (user-error "No operator defined for property %s" prop))
  14582. (org-columns-compute prop)))
  14583. (defvar org-property-allowed-value-functions nil
  14584. "Hook for functions supplying allowed values for a specific property.
  14585. The functions must take a single argument, the name of the property, and
  14586. return a flat list of allowed values. If \":ETC\" is one of
  14587. the values, this means that these values are intended as defaults for
  14588. completion, but that other values should be allowed too.
  14589. The functions must return nil if they are not responsible for this
  14590. property.")
  14591. (defun org-property-get-allowed-values (pom property &optional table)
  14592. "Get allowed values for the property PROPERTY.
  14593. When TABLE is non-nil, return an alist that can directly be used for
  14594. completion."
  14595. (let (vals)
  14596. (cond
  14597. ((equal property "TODO")
  14598. (setq vals (org-with-point-at pom
  14599. (append org-todo-keywords-1 '("")))))
  14600. ((equal property "PRIORITY")
  14601. (let ((n org-lowest-priority))
  14602. (while (>= n org-highest-priority)
  14603. (push (char-to-string n) vals)
  14604. (setq n (1- n)))))
  14605. ((member property org-special-properties))
  14606. ((setq vals (run-hook-with-args-until-success
  14607. 'org-property-allowed-value-functions property)))
  14608. (t
  14609. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14610. (when (and vals (string-match "\\S-" vals))
  14611. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14612. (setq vals (mapcar (lambda (x)
  14613. (cond ((stringp x) x)
  14614. ((numberp x) (number-to-string x))
  14615. ((symbolp x) (symbol-name x))
  14616. (t "???")))
  14617. vals)))))
  14618. (when (member ":ETC" vals)
  14619. (setq vals (remove ":ETC" vals))
  14620. (org-add-props (car vals) '(org-unrestricted t)))
  14621. (if table (mapcar 'list vals) vals)))
  14622. (defun org-property-previous-allowed-value (&optional previous)
  14623. "Switch to the next allowed value for this property."
  14624. (interactive)
  14625. (org-property-next-allowed-value t))
  14626. (defun org-property-next-allowed-value (&optional previous)
  14627. "Switch to the next allowed value for this property."
  14628. (interactive)
  14629. (unless (org-at-property-p)
  14630. (user-error "Not at a property"))
  14631. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14632. (key (match-string 2))
  14633. (value (match-string 3))
  14634. (allowed (or (org-property-get-allowed-values (point) key)
  14635. (and (member value '("[ ]" "[-]" "[X]"))
  14636. '("[ ]" "[X]"))))
  14637. (heading (save-match-data (nth 4 (org-heading-components))))
  14638. nval)
  14639. (unless allowed
  14640. (user-error "Allowed values for this property have not been defined"))
  14641. (if previous (setq allowed (reverse allowed)))
  14642. (if (member value allowed)
  14643. (setq nval (car (cdr (member value allowed)))))
  14644. (setq nval (or nval (car allowed)))
  14645. (if (equal nval value)
  14646. (user-error "Only one allowed value for this property"))
  14647. (org-at-property-p)
  14648. (replace-match (concat " :" key ": " nval) t t)
  14649. (org-indent-line)
  14650. (beginning-of-line 1)
  14651. (skip-chars-forward " \t")
  14652. (when (equal prop org-effort-property)
  14653. (save-excursion
  14654. (org-back-to-heading t)
  14655. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14656. (when (string= org-clock-current-task heading)
  14657. (setq org-clock-effort nval)
  14658. (org-clock-update-mode-line)))
  14659. (run-hook-with-args 'org-property-changed-functions key nval)))
  14660. (defun org-find-olp (path &optional this-buffer)
  14661. "Return a marker pointing to the entry at outline path OLP.
  14662. If anything goes wrong, throw an error.
  14663. You can wrap this call to catch the error like this:
  14664. (condition-case msg
  14665. (org-mobile-locate-entry (match-string 4))
  14666. (error (nth 1 msg)))
  14667. The return value will then be either a string with the error message,
  14668. or a marker if everything is OK.
  14669. If THIS-BUFFER is set, the outline path does not contain a file,
  14670. only headings."
  14671. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14672. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14673. (level 1)
  14674. (lmin 1)
  14675. (lmax 1)
  14676. limit re end found pos heading cnt flevel)
  14677. (unless buffer (error "File not found :%s" file))
  14678. (with-current-buffer buffer
  14679. (save-excursion
  14680. (save-restriction
  14681. (widen)
  14682. (setq limit (point-max))
  14683. (goto-char (point-min))
  14684. (while (setq heading (pop path))
  14685. (setq re (format org-complex-heading-regexp-format
  14686. (regexp-quote heading)))
  14687. (setq cnt 0 pos (point))
  14688. (while (re-search-forward re end t)
  14689. (setq level (- (match-end 1) (match-beginning 1)))
  14690. (if (and (>= level lmin) (<= level lmax))
  14691. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14692. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14693. lmax heading))
  14694. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14695. lmax heading))
  14696. (goto-char found)
  14697. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14698. (setq end (save-excursion (org-end-of-subtree t t))))
  14699. (when (org-at-heading-p)
  14700. (point-marker)))))))
  14701. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14702. "Find node HEADING in BUFFER.
  14703. Return a marker to the heading if it was found, or nil if not.
  14704. If POS-ONLY is set, return just the position instead of a marker.
  14705. The heading text must match exact, but it may have a TODO keyword,
  14706. a priority cookie and tags in the standard locations."
  14707. (with-current-buffer (or buffer (current-buffer))
  14708. (save-excursion
  14709. (save-restriction
  14710. (widen)
  14711. (goto-char (point-min))
  14712. (let (case-fold-search)
  14713. (if (re-search-forward
  14714. (format org-complex-heading-regexp-format
  14715. (regexp-quote heading)) nil t)
  14716. (if pos-only
  14717. (match-beginning 0)
  14718. (move-marker (make-marker) (match-beginning 0)))))))))
  14719. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14720. "Find Org node headline HEADING in all .org files in directory DIR.
  14721. When the target headline is found, return a marker to this location."
  14722. (let ((files (directory-files (or dir default-directory)
  14723. nil "\\`[^.#].*\\.org\\'"))
  14724. file visiting m buffer)
  14725. (catch 'found
  14726. (while (setq file (pop files))
  14727. (message "trying %s" file)
  14728. (setq visiting (org-find-base-buffer-visiting file))
  14729. (setq buffer (or visiting (find-file-noselect file)))
  14730. (setq m (org-find-exact-headline-in-buffer
  14731. heading buffer))
  14732. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14733. (and m (throw 'found m))))))
  14734. (defun org-find-entry-with-id (ident)
  14735. "Locate the entry that contains the ID property with exact value IDENT.
  14736. IDENT can be a string, a symbol or a number, this function will search for
  14737. the string representation of it.
  14738. Return the position where this entry starts, or nil if there is no such entry."
  14739. (interactive "sID: ")
  14740. (let ((id (cond
  14741. ((stringp ident) ident)
  14742. ((symbol-name ident) (symbol-name ident))
  14743. ((numberp ident) (number-to-string ident))
  14744. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14745. (case-fold-search nil))
  14746. (save-excursion
  14747. (save-restriction
  14748. (widen)
  14749. (goto-char (point-min))
  14750. (when (re-search-forward
  14751. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14752. nil t)
  14753. (org-back-to-heading t)
  14754. (point))))))
  14755. ;;;; Timestamps
  14756. (defvar org-last-changed-timestamp nil)
  14757. (defvar org-last-inserted-timestamp nil
  14758. "The last time stamp inserted with `org-insert-time-stamp'.")
  14759. (defvar org-ts-what) ; dynamically scoped parameter
  14760. (defun org-time-stamp (arg &optional inactive)
  14761. "Prompt for a date/time and insert a time stamp.
  14762. If the user specifies a time like HH:MM or if this command is
  14763. called with at least one prefix argument, the time stamp contains
  14764. the date and the time. Otherwise, only the date is be included.
  14765. All parts of a date not specified by the user is filled in from
  14766. the current date/time. So if you just press return without
  14767. typing anything, the time stamp will represent the current
  14768. date/time.
  14769. If there is already a timestamp at the cursor, it will be
  14770. modified.
  14771. With two universal prefix arguments, insert an active timestamp
  14772. with the current time without prompting the user.
  14773. When called from lisp, the timestamp is inactive if INACTIVE is
  14774. non-nil."
  14775. (interactive "P")
  14776. (let* ((ts nil)
  14777. (default-time
  14778. ;; Default time is either today, or, when entering a range,
  14779. ;; the range start.
  14780. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14781. (save-excursion
  14782. (re-search-backward
  14783. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14784. (- (point) 20) t)))
  14785. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14786. (current-time)))
  14787. (default-input (and ts (org-get-compact-tod ts)))
  14788. (repeater (save-excursion
  14789. (save-match-data
  14790. (beginning-of-line)
  14791. (when (re-search-forward
  14792. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14793. (save-excursion (progn (end-of-line) (point))) t)
  14794. (match-string 0)))))
  14795. org-time-was-given org-end-time-was-given time)
  14796. (cond
  14797. ((and (org-at-timestamp-p t)
  14798. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14799. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14800. (insert "--")
  14801. (setq time (let ((this-command this-command))
  14802. (org-read-date arg 'totime nil nil
  14803. default-time default-input inactive)))
  14804. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14805. ((org-at-timestamp-p t)
  14806. (setq time (let ((this-command this-command))
  14807. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14808. (when (org-at-timestamp-p t) ; just to get the match data
  14809. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14810. (replace-match "")
  14811. (setq org-last-changed-timestamp
  14812. (org-insert-time-stamp
  14813. time (or org-time-was-given arg)
  14814. inactive nil nil (list org-end-time-was-given)))
  14815. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14816. (setq org-last-changed-timestamp
  14817. (concat (substring org-last-inserted-timestamp 0 -1)
  14818. " " repeater ">"))))
  14819. (message "Timestamp updated"))
  14820. ((equal arg '(16))
  14821. (org-insert-time-stamp (current-time) t inactive))
  14822. (t
  14823. (setq time (let ((this-command this-command))
  14824. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14825. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14826. nil nil (list org-end-time-was-given))))))
  14827. ;; FIXME: can we use this for something else, like computing time differences?
  14828. (defun org-get-compact-tod (s)
  14829. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14830. (let* ((t1 (match-string 1 s))
  14831. (h1 (string-to-number (match-string 2 s)))
  14832. (m1 (string-to-number (match-string 3 s)))
  14833. (t2 (and (match-end 4) (match-string 5 s)))
  14834. (h2 (and t2 (string-to-number (match-string 6 s))))
  14835. (m2 (and t2 (string-to-number (match-string 7 s))))
  14836. dh dm)
  14837. (if (not t2)
  14838. t1
  14839. (setq dh (- h2 h1) dm (- m2 m1))
  14840. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14841. (concat t1 "+" (number-to-string dh)
  14842. (and (/= 0 dm) (format ":%02d" dm)))))))
  14843. (defun org-time-stamp-inactive (&optional arg)
  14844. "Insert an inactive time stamp.
  14845. An inactive time stamp is enclosed in square brackets instead of angle
  14846. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14847. does not link to the calendar and cannot be changed with the S-cursor keys.
  14848. So these are more for recording a certain time/date."
  14849. (interactive "P")
  14850. (org-time-stamp arg 'inactive))
  14851. (defvar org-date-ovl (make-overlay 1 1))
  14852. (overlay-put org-date-ovl 'face 'org-date-selected)
  14853. (org-detach-overlay org-date-ovl)
  14854. (defvar org-ans1) ; dynamically scoped parameter
  14855. (defvar org-ans2) ; dynamically scoped parameter
  14856. (defvar org-plain-time-of-day-regexp) ; defined below
  14857. (defvar org-overriding-default-time nil) ; dynamically scoped
  14858. (defvar org-read-date-overlay nil)
  14859. (defvar org-dcst nil) ; dynamically scoped
  14860. (defvar org-read-date-history nil)
  14861. (defvar org-read-date-final-answer nil)
  14862. (defvar org-read-date-analyze-futurep nil)
  14863. (defvar org-read-date-analyze-forced-year nil)
  14864. (defvar org-read-date-inactive)
  14865. (defvar org-read-date-minibuffer-local-map
  14866. (let* ((org-replace-disputed-keys nil)
  14867. (map (make-sparse-keymap)))
  14868. (set-keymap-parent map minibuffer-local-map)
  14869. (org-defkey map (kbd ".")
  14870. (lambda () (interactive)
  14871. ;; Are we at the beginning of the prompt?
  14872. (if (looking-back "^[^:]+: ")
  14873. (org-eval-in-calendar '(calendar-goto-today))
  14874. (insert "."))))
  14875. (org-defkey map (kbd "C-.")
  14876. (lambda () (interactive)
  14877. (org-eval-in-calendar '(calendar-goto-today))))
  14878. (org-defkey map [(meta shift left)]
  14879. (lambda () (interactive)
  14880. (org-eval-in-calendar '(calendar-backward-month 1))))
  14881. (org-defkey map [(meta shift right)]
  14882. (lambda () (interactive)
  14883. (org-eval-in-calendar '(calendar-forward-month 1))))
  14884. (org-defkey map [(meta shift up)]
  14885. (lambda () (interactive)
  14886. (org-eval-in-calendar '(calendar-backward-year 1))))
  14887. (org-defkey map [(meta shift down)]
  14888. (lambda () (interactive)
  14889. (org-eval-in-calendar '(calendar-forward-year 1))))
  14890. (org-defkey map [?\e (shift left)]
  14891. (lambda () (interactive)
  14892. (org-eval-in-calendar '(calendar-backward-month 1))))
  14893. (org-defkey map [?\e (shift right)]
  14894. (lambda () (interactive)
  14895. (org-eval-in-calendar '(calendar-forward-month 1))))
  14896. (org-defkey map [?\e (shift up)]
  14897. (lambda () (interactive)
  14898. (org-eval-in-calendar '(calendar-backward-year 1))))
  14899. (org-defkey map [?\e (shift down)]
  14900. (lambda () (interactive)
  14901. (org-eval-in-calendar '(calendar-forward-year 1))))
  14902. (org-defkey map [(shift up)]
  14903. (lambda () (interactive)
  14904. (org-eval-in-calendar '(calendar-backward-week 1))))
  14905. (org-defkey map [(shift down)]
  14906. (lambda () (interactive)
  14907. (org-eval-in-calendar '(calendar-forward-week 1))))
  14908. (org-defkey map [(shift left)]
  14909. (lambda () (interactive)
  14910. (org-eval-in-calendar '(calendar-backward-day 1))))
  14911. (org-defkey map [(shift right)]
  14912. (lambda () (interactive)
  14913. (org-eval-in-calendar '(calendar-forward-day 1))))
  14914. (org-defkey map "!"
  14915. (lambda () (interactive)
  14916. (org-eval-in-calendar '(diary-view-entries))
  14917. (message "")))
  14918. (org-defkey map ">"
  14919. (lambda () (interactive)
  14920. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14921. (org-defkey map "<"
  14922. (lambda () (interactive)
  14923. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14924. (org-defkey map "\C-v"
  14925. (lambda () (interactive)
  14926. (org-eval-in-calendar
  14927. '(calendar-scroll-left-three-months 1))))
  14928. (org-defkey map "\M-v"
  14929. (lambda () (interactive)
  14930. (org-eval-in-calendar
  14931. '(calendar-scroll-right-three-months 1))))
  14932. map)
  14933. "Keymap for minibuffer commands when using `org-read-date'.")
  14934. (defvar org-def)
  14935. (defvar org-defdecode)
  14936. (defvar org-with-time)
  14937. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14938. default-time default-input inactive)
  14939. "Read a date, possibly a time, and make things smooth for the user.
  14940. The prompt will suggest to enter an ISO date, but you can also enter anything
  14941. which will at least partially be understood by `parse-time-string'.
  14942. Unrecognized parts of the date will default to the current day, month, year,
  14943. hour and minute. If this command is called to replace a timestamp at point,
  14944. or to enter the second timestamp of a range, the default time is taken
  14945. from the existing stamp. Furthermore, the command prefers the future,
  14946. so if you are giving a date where the year is not given, and the day-month
  14947. combination is already past in the current year, it will assume you
  14948. mean next year. For details, see the manual. A few examples:
  14949. 3-2-5 --> 2003-02-05
  14950. feb 15 --> currentyear-02-15
  14951. 2/15 --> currentyear-02-15
  14952. sep 12 9 --> 2009-09-12
  14953. 12:45 --> today 12:45
  14954. 22 sept 0:34 --> currentyear-09-22 0:34
  14955. 12 --> currentyear-currentmonth-12
  14956. Fri --> nearest Friday after today
  14957. -Tue --> last Tuesday
  14958. etc.
  14959. Furthermore you can specify a relative date by giving, as the *first* thing
  14960. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14961. change in days weeks, months, years.
  14962. With a single plus or minus, the date is relative to today. With a double
  14963. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14964. +4d --> four days from today
  14965. +4 --> same as above
  14966. +2w --> two weeks from today
  14967. ++5 --> five days from default date
  14968. The function understands only English month and weekday abbreviations.
  14969. While prompting, a calendar is popped up - you can also select the
  14970. date with the mouse (button 1). The calendar shows a period of three
  14971. months. To scroll it to other months, use the keys `>' and `<'.
  14972. If you don't like the calendar, turn it off with
  14973. \(setq org-read-date-popup-calendar nil)
  14974. With optional argument TO-TIME, the date will immediately be converted
  14975. to an internal time.
  14976. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14977. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14978. still enter a time, and this function will inform the calling routine
  14979. about this change. The calling routine may then choose to change the
  14980. format used to insert the time stamp into the buffer to include the time.
  14981. With optional argument FROM-STRING, read from this string instead from
  14982. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14983. the time/date that is used for everything that is not specified by the
  14984. user."
  14985. (require 'parse-time)
  14986. (let* ((org-time-stamp-rounding-minutes
  14987. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14988. (org-dcst org-display-custom-times)
  14989. (ct (org-current-time))
  14990. (org-def (or org-overriding-default-time default-time ct))
  14991. (org-defdecode (decode-time org-def))
  14992. (dummy (progn
  14993. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14994. (setcar (nthcdr 2 org-defdecode) -1)
  14995. (setcar (nthcdr 1 org-defdecode) 59)
  14996. (setq org-def (apply 'encode-time org-defdecode)
  14997. org-defdecode (decode-time org-def)))))
  14998. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14999. (calendar-frame-setup nil)
  15000. (calendar-setup nil)
  15001. (calendar-move-hook nil)
  15002. (calendar-view-diary-initially-flag nil)
  15003. (calendar-view-holidays-initially-flag nil)
  15004. (timestr (format-time-string
  15005. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15006. (prompt (concat (if prompt (concat prompt " ") "")
  15007. (format "Date+time [%s]: " timestr)))
  15008. ans (org-ans0 "") org-ans1 org-ans2 final)
  15009. (cond
  15010. (from-string (setq ans from-string))
  15011. (org-read-date-popup-calendar
  15012. (save-excursion
  15013. (save-window-excursion
  15014. (calendar)
  15015. (org-eval-in-calendar '(setq cursor-type nil) t)
  15016. (unwind-protect
  15017. (progn
  15018. (calendar-forward-day (- (time-to-days org-def)
  15019. (calendar-absolute-from-gregorian
  15020. (calendar-current-date))))
  15021. (org-eval-in-calendar nil t)
  15022. (let* ((old-map (current-local-map))
  15023. (map (copy-keymap calendar-mode-map))
  15024. (minibuffer-local-map
  15025. (copy-keymap org-read-date-minibuffer-local-map)))
  15026. (org-defkey map (kbd "RET") 'org-calendar-select)
  15027. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15028. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15029. (unwind-protect
  15030. (progn
  15031. (use-local-map map)
  15032. (setq org-read-date-inactive inactive)
  15033. (add-hook 'post-command-hook 'org-read-date-display)
  15034. (setq org-ans0 (read-string prompt default-input
  15035. 'org-read-date-history nil))
  15036. ;; org-ans0: from prompt
  15037. ;; org-ans1: from mouse click
  15038. ;; org-ans2: from calendar motion
  15039. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15040. (remove-hook 'post-command-hook 'org-read-date-display)
  15041. (use-local-map old-map)
  15042. (when org-read-date-overlay
  15043. (delete-overlay org-read-date-overlay)
  15044. (setq org-read-date-overlay nil)))))
  15045. (bury-buffer "*Calendar*")))))
  15046. (t ; Naked prompt only
  15047. (unwind-protect
  15048. (setq ans (read-string prompt default-input
  15049. 'org-read-date-history timestr))
  15050. (when org-read-date-overlay
  15051. (delete-overlay org-read-date-overlay)
  15052. (setq org-read-date-overlay nil)))))
  15053. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15054. (when org-read-date-analyze-forced-year
  15055. (message "Year was forced into %s"
  15056. (if org-read-date-force-compatible-dates
  15057. "compatible range (1970-2037)"
  15058. "range representable on this machine"))
  15059. (ding))
  15060. ;; One round trip to get rid of 34th of August and stuff like that....
  15061. (setq final (decode-time (apply 'encode-time final)))
  15062. (setq org-read-date-final-answer ans)
  15063. (if to-time
  15064. (apply 'encode-time final)
  15065. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15066. (format "%04d-%02d-%02d %02d:%02d"
  15067. (nth 5 final) (nth 4 final) (nth 3 final)
  15068. (nth 2 final) (nth 1 final))
  15069. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15070. (defun org-read-date-display ()
  15071. "Display the current date prompt interpretation in the minibuffer."
  15072. (when org-read-date-display-live
  15073. (when org-read-date-overlay
  15074. (delete-overlay org-read-date-overlay))
  15075. (when (minibufferp (current-buffer))
  15076. (save-excursion
  15077. (end-of-line 1)
  15078. (while (not (equal (buffer-substring
  15079. (max (point-min) (- (point) 4)) (point))
  15080. " "))
  15081. (insert " ")))
  15082. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15083. " " (or org-ans1 org-ans2)))
  15084. (org-end-time-was-given nil)
  15085. (f (org-read-date-analyze ans org-def org-defdecode))
  15086. (fmts (if org-dcst
  15087. org-time-stamp-custom-formats
  15088. org-time-stamp-formats))
  15089. (fmt (if (or org-with-time
  15090. (and (boundp 'org-time-was-given) org-time-was-given))
  15091. (cdr fmts)
  15092. (car fmts)))
  15093. (txt (format-time-string fmt (apply 'encode-time f)))
  15094. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15095. (txt (concat "=> " txt)))
  15096. (when (and org-end-time-was-given
  15097. (string-match org-plain-time-of-day-regexp txt))
  15098. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15099. org-end-time-was-given
  15100. (substring txt (match-end 0)))))
  15101. (when org-read-date-analyze-futurep
  15102. (setq txt (concat txt " (=>F)")))
  15103. (setq org-read-date-overlay
  15104. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15105. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15106. (defun org-read-date-analyze (ans org-def org-defdecode)
  15107. "Analyze the combined answer of the date prompt."
  15108. ;; FIXME: cleanup and comment
  15109. (let ((nowdecode (decode-time (current-time)))
  15110. delta deltan deltaw deltadef year month day
  15111. hour minute second wday pm h2 m2 tl wday1
  15112. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15113. (setq org-read-date-analyze-futurep nil
  15114. org-read-date-analyze-forced-year nil)
  15115. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15116. (setq ans "+0"))
  15117. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15118. (setq ans (replace-match "" t t ans)
  15119. deltan (car delta)
  15120. deltaw (nth 1 delta)
  15121. deltadef (nth 2 delta)))
  15122. ;; Check if there is an iso week date in there. If yes, store the
  15123. ;; info and postpone interpreting it until the rest of the parsing
  15124. ;; is done.
  15125. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15126. (setq iso-year (if (match-end 1)
  15127. (org-small-year-to-year
  15128. (string-to-number (match-string 1 ans))))
  15129. iso-weekday (if (match-end 3)
  15130. (string-to-number (match-string 3 ans)))
  15131. iso-week (string-to-number (match-string 2 ans)))
  15132. (setq ans (replace-match "" t t ans)))
  15133. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15134. (when (string-match
  15135. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15136. (setq year (if (match-end 2)
  15137. (string-to-number (match-string 2 ans))
  15138. (progn (setq kill-year t)
  15139. (string-to-number (format-time-string "%Y"))))
  15140. month (string-to-number (match-string 3 ans))
  15141. day (string-to-number (match-string 4 ans)))
  15142. (if (< year 100) (setq year (+ 2000 year)))
  15143. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15144. t nil ans)))
  15145. ;; Help matching dotted european dates
  15146. (when (string-match
  15147. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15148. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15149. (setq kill-year t)
  15150. (string-to-number (format-time-string "%Y")))
  15151. day (string-to-number (match-string 1 ans))
  15152. month (string-to-number (match-string 2 ans))
  15153. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15154. t nil ans)))
  15155. ;; Help matching american dates, like 5/30 or 5/30/7
  15156. (when (string-match
  15157. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15158. (setq year (if (match-end 4)
  15159. (string-to-number (match-string 4 ans))
  15160. (progn (setq kill-year t)
  15161. (string-to-number (format-time-string "%Y"))))
  15162. month (string-to-number (match-string 1 ans))
  15163. day (string-to-number (match-string 2 ans)))
  15164. (if (< year 100) (setq year (+ 2000 year)))
  15165. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15166. t nil ans)))
  15167. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15168. ;; If there is a time with am/pm, and *no* time without it, we convert
  15169. ;; so that matching will be successful.
  15170. (loop for i from 1 to 2 do ; twice, for end time as well
  15171. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15172. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15173. (setq hour (string-to-number (match-string 1 ans))
  15174. minute (if (match-end 3)
  15175. (string-to-number (match-string 3 ans))
  15176. 0)
  15177. pm (equal ?p
  15178. (string-to-char (downcase (match-string 4 ans)))))
  15179. (if (and (= hour 12) (not pm))
  15180. (setq hour 0)
  15181. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15182. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15183. t t ans))))
  15184. ;; Check if a time range is given as a duration
  15185. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15186. (setq hour (string-to-number (match-string 1 ans))
  15187. h2 (+ hour (string-to-number (match-string 3 ans)))
  15188. minute (string-to-number (match-string 2 ans))
  15189. m2 (+ minute (if (match-end 5) (string-to-number
  15190. (match-string 5 ans))0)))
  15191. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15192. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15193. t t ans)))
  15194. ;; Check if there is a time range
  15195. (when (boundp 'org-end-time-was-given)
  15196. (setq org-time-was-given nil)
  15197. (when (and (string-match org-plain-time-of-day-regexp ans)
  15198. (match-end 8))
  15199. (setq org-end-time-was-given (match-string 8 ans))
  15200. (setq ans (concat (substring ans 0 (match-beginning 7))
  15201. (substring ans (match-end 7))))))
  15202. (setq tl (parse-time-string ans)
  15203. day (or (nth 3 tl) (nth 3 org-defdecode))
  15204. month (or (nth 4 tl)
  15205. (if (and org-read-date-prefer-future
  15206. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15207. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15208. (nth 4 org-defdecode)))
  15209. year (or (and (not kill-year) (nth 5 tl))
  15210. (if (and org-read-date-prefer-future
  15211. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15212. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15213. (nth 5 org-defdecode)))
  15214. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15215. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15216. second (or (nth 0 tl) 0)
  15217. wday (nth 6 tl))
  15218. (when (and (eq org-read-date-prefer-future 'time)
  15219. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15220. (equal day (nth 3 nowdecode))
  15221. (equal month (nth 4 nowdecode))
  15222. (equal year (nth 5 nowdecode))
  15223. (nth 2 tl)
  15224. (or (< (nth 2 tl) (nth 2 nowdecode))
  15225. (and (= (nth 2 tl) (nth 2 nowdecode))
  15226. (nth 1 tl)
  15227. (< (nth 1 tl) (nth 1 nowdecode)))))
  15228. (setq day (1+ day)
  15229. futurep t))
  15230. ;; Special date definitions below
  15231. (cond
  15232. (iso-week
  15233. ;; There was an iso week
  15234. (require 'cal-iso)
  15235. (setq futurep nil)
  15236. (setq year (or iso-year year)
  15237. day (or iso-weekday wday 1)
  15238. wday nil ; to make sure that the trigger below does not match
  15239. iso-date (calendar-gregorian-from-absolute
  15240. (calendar-absolute-from-iso
  15241. (list iso-week day year))))
  15242. ; FIXME: Should we also push ISO weeks into the future?
  15243. ; (when (and org-read-date-prefer-future
  15244. ; (not iso-year)
  15245. ; (< (calendar-absolute-from-gregorian iso-date)
  15246. ; (time-to-days (current-time))))
  15247. ; (setq year (1+ year)
  15248. ; iso-date (calendar-gregorian-from-absolute
  15249. ; (calendar-absolute-from-iso
  15250. ; (list iso-week day year)))))
  15251. (setq month (car iso-date)
  15252. year (nth 2 iso-date)
  15253. day (nth 1 iso-date)))
  15254. (deltan
  15255. (setq futurep nil)
  15256. (unless deltadef
  15257. (let ((now (decode-time (current-time))))
  15258. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15259. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15260. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15261. ((equal deltaw "m") (setq month (+ month deltan)))
  15262. ((equal deltaw "y") (setq year (+ year deltan)))))
  15263. ((and wday (not (nth 3 tl)))
  15264. ;; Weekday was given, but no day, so pick that day in the week
  15265. ;; on or after the derived date.
  15266. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15267. (unless (equal wday wday1)
  15268. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15269. (if (and (boundp 'org-time-was-given)
  15270. (nth 2 tl))
  15271. (setq org-time-was-given t))
  15272. (if (< year 100) (setq year (+ 2000 year)))
  15273. ;; Check of the date is representable
  15274. (if org-read-date-force-compatible-dates
  15275. (progn
  15276. (if (< year 1970)
  15277. (setq year 1970 org-read-date-analyze-forced-year t))
  15278. (if (> year 2037)
  15279. (setq year 2037 org-read-date-analyze-forced-year t)))
  15280. (condition-case nil
  15281. (ignore (encode-time second minute hour day month year))
  15282. (error
  15283. (setq year (nth 5 org-defdecode))
  15284. (setq org-read-date-analyze-forced-year t))))
  15285. (setq org-read-date-analyze-futurep futurep)
  15286. (list second minute hour day month year)))
  15287. (defvar parse-time-weekdays)
  15288. (defun org-read-date-get-relative (s today default)
  15289. "Check string S for special relative date string.
  15290. TODAY and DEFAULT are internal times, for today and for a default.
  15291. Return shift list (N what def-flag)
  15292. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15293. N is the number of WHATs to shift.
  15294. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15295. the DEFAULT date rather than TODAY."
  15296. (require 'parse-time)
  15297. (when (and
  15298. (string-match
  15299. (concat
  15300. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15301. "\\([0-9]+\\)?"
  15302. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15303. "\\([ \t]\\|$\\)") s)
  15304. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15305. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15306. (string-to-char (substring (match-string 1 s) -1))
  15307. ?+))
  15308. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15309. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15310. (what (if (match-end 3) (match-string 3 s) "d"))
  15311. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15312. (date (if rel default today))
  15313. (wday (nth 6 (decode-time date)))
  15314. delta)
  15315. (if wday1
  15316. (progn
  15317. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15318. (if (= delta 0) (setq delta 7))
  15319. (if (= dir ?-)
  15320. (progn
  15321. (setq delta (- delta 7))
  15322. (if (= delta 0) (setq delta -7))))
  15323. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15324. (list delta "d" rel))
  15325. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15326. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15327. "Turn a user-specified date into the internal representation.
  15328. The internal representation needed by the calendar is (month day year).
  15329. This is a wrapper to handle the brain-dead convention in calendar that
  15330. user function argument order change dependent on argument order."
  15331. (if (boundp 'calendar-date-style)
  15332. (cond
  15333. ((eq calendar-date-style 'american)
  15334. (list arg1 arg2 arg3))
  15335. ((eq calendar-date-style 'european)
  15336. (list arg2 arg1 arg3))
  15337. ((eq calendar-date-style 'iso)
  15338. (list arg2 arg3 arg1)))
  15339. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15340. (if (org-bound-and-true-p european-calendar-style)
  15341. (list arg2 arg1 arg3)
  15342. (list arg1 arg2 arg3)))))
  15343. (defun org-eval-in-calendar (form &optional keepdate)
  15344. "Eval FORM in the calendar window and return to current window.
  15345. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15346. otherwise stick to the current value of `org-ans2'."
  15347. (let ((sf (selected-frame))
  15348. (sw (selected-window)))
  15349. (select-window (get-buffer-window "*Calendar*" t))
  15350. (eval form)
  15351. (when (and (not keepdate) (calendar-cursor-to-date))
  15352. (let* ((date (calendar-cursor-to-date))
  15353. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15354. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15355. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15356. (select-window sw)
  15357. (org-select-frame-set-input-focus sf)))
  15358. (defun org-calendar-select ()
  15359. "Return to `org-read-date' with the date currently selected.
  15360. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15361. (interactive)
  15362. (when (calendar-cursor-to-date)
  15363. (let* ((date (calendar-cursor-to-date))
  15364. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15365. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15366. (if (active-minibuffer-window) (exit-minibuffer))))
  15367. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15368. "Insert a date stamp for the date given by the internal TIME.
  15369. WITH-HM means use the stamp format that includes the time of the day.
  15370. INACTIVE means use square brackets instead of angular ones, so that the
  15371. stamp will not contribute to the agenda.
  15372. PRE and POST are optional strings to be inserted before and after the
  15373. stamp.
  15374. The command returns the inserted time stamp."
  15375. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15376. stamp)
  15377. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15378. (insert-before-markers (or pre ""))
  15379. (when (listp extra)
  15380. (setq extra (car extra))
  15381. (if (and (stringp extra)
  15382. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15383. (setq extra (format "-%02d:%02d"
  15384. (string-to-number (match-string 1 extra))
  15385. (string-to-number (match-string 2 extra))))
  15386. (setq extra nil)))
  15387. (when extra
  15388. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15389. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15390. (insert-before-markers (or post ""))
  15391. (setq org-last-inserted-timestamp stamp)))
  15392. (defun org-toggle-time-stamp-overlays ()
  15393. "Toggle the use of custom time stamp formats."
  15394. (interactive)
  15395. (setq org-display-custom-times (not org-display-custom-times))
  15396. (unless org-display-custom-times
  15397. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15398. (while (setq p (next-single-property-change p 'display))
  15399. (if (and (get-text-property p 'display)
  15400. (eq (get-text-property p 'face) 'org-date))
  15401. (remove-text-properties
  15402. p (setq p (next-single-property-change p 'display))
  15403. '(display t))))
  15404. (set-buffer-modified-p bmp)))
  15405. (if (featurep 'xemacs)
  15406. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15407. (org-restart-font-lock)
  15408. (setq org-table-may-need-update t)
  15409. (if org-display-custom-times
  15410. (message "Time stamps are overlaid with custom format")
  15411. (message "Time stamp overlays removed")))
  15412. (defun org-display-custom-time (beg end)
  15413. "Overlay modified time stamp format over timestamp between BEG and END."
  15414. (let* ((ts (buffer-substring beg end))
  15415. t1 w1 with-hm tf time str w2 (off 0))
  15416. (save-match-data
  15417. (setq t1 (org-parse-time-string ts t))
  15418. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15419. (setq off (- (match-end 0) (match-beginning 0)))))
  15420. (setq end (- end off))
  15421. (setq w1 (- end beg)
  15422. with-hm (and (nth 1 t1) (nth 2 t1))
  15423. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15424. time (org-fix-decoded-time t1)
  15425. str (org-add-props
  15426. (format-time-string
  15427. (substring tf 1 -1) (apply 'encode-time time))
  15428. nil 'mouse-face 'highlight)
  15429. w2 (length str))
  15430. (if (not (= w2 w1))
  15431. (add-text-properties (1+ beg) (+ 2 beg)
  15432. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15433. (if (featurep 'xemacs)
  15434. (progn
  15435. (put-text-property beg end 'invisible t)
  15436. (put-text-property beg end 'end-glyph (make-glyph str)))
  15437. (put-text-property beg end 'display str))))
  15438. (defun org-translate-time (string)
  15439. "Translate all timestamps in STRING to custom format.
  15440. But do this only if the variable `org-display-custom-times' is set."
  15441. (when org-display-custom-times
  15442. (save-match-data
  15443. (let* ((start 0)
  15444. (re org-ts-regexp-both)
  15445. t1 with-hm inactive tf time str beg end)
  15446. (while (setq start (string-match re string start))
  15447. (setq beg (match-beginning 0)
  15448. end (match-end 0)
  15449. t1 (save-match-data
  15450. (org-parse-time-string (substring string beg end) t))
  15451. with-hm (and (nth 1 t1) (nth 2 t1))
  15452. inactive (equal (substring string beg (1+ beg)) "[")
  15453. tf (funcall (if with-hm 'cdr 'car)
  15454. org-time-stamp-custom-formats)
  15455. time (org-fix-decoded-time t1)
  15456. str (format-time-string
  15457. (concat
  15458. (if inactive "[" "<") (substring tf 1 -1)
  15459. (if inactive "]" ">"))
  15460. (apply 'encode-time time))
  15461. string (replace-match str t t string)
  15462. start (+ start (length str)))))))
  15463. string)
  15464. (defun org-fix-decoded-time (time)
  15465. "Set 0 instead of nil for the first 6 elements of time.
  15466. Don't touch the rest."
  15467. (let ((n 0))
  15468. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15469. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15470. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15471. "Difference between TIMESTAMP-STRING and now in days.
  15472. If SECONDS is non-nil, return the difference in seconds."
  15473. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15474. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15475. (funcall fdiff (current-time)))))
  15476. (defun org-deadline-close (timestamp-string &optional ndays)
  15477. "Is the time in TIMESTAMP-STRING close to the current date?"
  15478. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15479. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15480. (not (org-entry-is-done-p))))
  15481. (defun org-get-wdays (ts &optional delay zero-delay)
  15482. "Get the deadline lead time appropriate for timestring TS.
  15483. When DELAY is non-nil, get the delay time for scheduled items
  15484. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15485. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15486. don't try to find the delay cookie in the scheduled timestamp."
  15487. (let ((tv (if delay org-scheduled-delay-days
  15488. org-deadline-warning-days)))
  15489. (cond
  15490. ((or (and delay (< tv 0))
  15491. (and delay zero-delay (<= tv 0))
  15492. (and (not delay) (<= tv 0)))
  15493. ;; Enforce this value no matter what
  15494. (- tv))
  15495. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15496. ;; lead time is specified.
  15497. (floor (* (string-to-number (match-string 1 ts))
  15498. (cdr (assoc (match-string 2 ts)
  15499. '(("d" . 1) ("w" . 7)
  15500. ("m" . 30.4) ("y" . 365.25)
  15501. ("h" . 0.041667)))))))
  15502. ;; go for the default.
  15503. (t tv))))
  15504. (defun org-calendar-select-mouse (ev)
  15505. "Return to `org-read-date' with the date currently selected.
  15506. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15507. (interactive "e")
  15508. (mouse-set-point ev)
  15509. (when (calendar-cursor-to-date)
  15510. (let* ((date (calendar-cursor-to-date))
  15511. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15512. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15513. (if (active-minibuffer-window) (exit-minibuffer))))
  15514. (defun org-check-deadlines (ndays)
  15515. "Check if there are any deadlines due or past due.
  15516. A deadline is considered due if it happens within `org-deadline-warning-days'
  15517. days from today's date. If the deadline appears in an entry marked DONE,
  15518. it is not shown. The prefix arg NDAYS can be used to test that many
  15519. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15520. (interactive "P")
  15521. (let* ((org-warn-days
  15522. (cond
  15523. ((equal ndays '(4)) 100000)
  15524. (ndays (prefix-numeric-value ndays))
  15525. (t (abs org-deadline-warning-days))))
  15526. (case-fold-search nil)
  15527. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15528. (callback
  15529. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15530. (message "%d deadlines past-due or due within %d days"
  15531. (org-occur regexp nil callback)
  15532. org-warn-days)))
  15533. (defsubst org-re-timestamp (type)
  15534. "Return a regexp for timestamp TYPE.
  15535. Allowed values for TYPE are:
  15536. all: all timestamps
  15537. active: only active timestamps (<...>)
  15538. inactive: only inactive timestamps ([...])
  15539. scheduled: only scheduled timestamps
  15540. deadline: only deadline timestamps
  15541. closed: only closed time-stamps
  15542. When TYPE is nil, fall back on returning a regexp that matches
  15543. both scheduled and deadline timestamps."
  15544. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15545. ((eq type 'active) org-ts-regexp)
  15546. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15547. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15548. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15549. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15550. ((eq type 'scheduled-or-deadline)
  15551. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15552. (defun org-check-before-date (date)
  15553. "Check if there are deadlines or scheduled entries before DATE."
  15554. (interactive (list (org-read-date)))
  15555. (let ((case-fold-search nil)
  15556. (regexp (org-re-timestamp org-ts-type))
  15557. (callback
  15558. (lambda () (time-less-p
  15559. (org-time-string-to-time (match-string 1))
  15560. (org-time-string-to-time date)))))
  15561. (message "%d entries before %s"
  15562. (org-occur regexp nil callback) date)))
  15563. (defun org-check-after-date (date)
  15564. "Check if there are deadlines or scheduled entries after DATE."
  15565. (interactive (list (org-read-date)))
  15566. (let ((case-fold-search nil)
  15567. (regexp (org-re-timestamp org-ts-type))
  15568. (callback
  15569. (lambda () (not
  15570. (time-less-p
  15571. (org-time-string-to-time (match-string 1))
  15572. (org-time-string-to-time date))))))
  15573. (message "%d entries after %s"
  15574. (org-occur regexp nil callback) date)))
  15575. (defun org-check-dates-range (start-date end-date)
  15576. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15577. (interactive (list (org-read-date nil nil nil "Range starts")
  15578. (org-read-date nil nil nil "Range end")))
  15579. (let ((case-fold-search nil)
  15580. (regexp (org-re-timestamp org-ts-type))
  15581. (callback
  15582. (lambda ()
  15583. (let ((match (match-string 1)))
  15584. (and
  15585. (not (time-less-p
  15586. (org-time-string-to-time match)
  15587. (org-time-string-to-time start-date)))
  15588. (time-less-p
  15589. (org-time-string-to-time match)
  15590. (org-time-string-to-time end-date)))))))
  15591. (message "%d entries between %s and %s"
  15592. (org-occur regexp nil callback) start-date end-date)))
  15593. (defun org-evaluate-time-range (&optional to-buffer)
  15594. "Evaluate a time range by computing the difference between start and end.
  15595. Normally the result is just printed in the echo area, but with prefix arg
  15596. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15597. If the time range is actually in a table, the result is inserted into the
  15598. next column.
  15599. For time difference computation, a year is assumed to be exactly 365
  15600. days in order to avoid rounding problems."
  15601. (interactive "P")
  15602. (or
  15603. (org-clock-update-time-maybe)
  15604. (save-excursion
  15605. (unless (org-at-date-range-p t)
  15606. (goto-char (point-at-bol))
  15607. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15608. (if (not (org-at-date-range-p t))
  15609. (user-error "Not at a time-stamp range, and none found in current line")))
  15610. (let* ((ts1 (match-string 1))
  15611. (ts2 (match-string 2))
  15612. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15613. (match-end (match-end 0))
  15614. (time1 (org-time-string-to-time ts1))
  15615. (time2 (org-time-string-to-time ts2))
  15616. (t1 (org-float-time time1))
  15617. (t2 (org-float-time time2))
  15618. (diff (abs (- t2 t1)))
  15619. (negative (< (- t2 t1) 0))
  15620. ;; (ys (floor (* 365 24 60 60)))
  15621. (ds (* 24 60 60))
  15622. (hs (* 60 60))
  15623. (fy "%dy %dd %02d:%02d")
  15624. (fy1 "%dy %dd")
  15625. (fd "%dd %02d:%02d")
  15626. (fd1 "%dd")
  15627. (fh "%02d:%02d")
  15628. y d h m align)
  15629. (if havetime
  15630. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15631. y 0
  15632. d (floor (/ diff ds)) diff (mod diff ds)
  15633. h (floor (/ diff hs)) diff (mod diff hs)
  15634. m (floor (/ diff 60)))
  15635. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15636. y 0
  15637. d (floor (+ (/ diff ds) 0.5))
  15638. h 0 m 0))
  15639. (if (not to-buffer)
  15640. (message "%s" (org-make-tdiff-string y d h m))
  15641. (if (org-at-table-p)
  15642. (progn
  15643. (goto-char match-end)
  15644. (setq align t)
  15645. (and (looking-at " *|") (goto-char (match-end 0))))
  15646. (goto-char match-end))
  15647. (if (looking-at
  15648. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15649. (replace-match ""))
  15650. (if negative (insert " -"))
  15651. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15652. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15653. (insert " " (format fh h m))))
  15654. (if align (org-table-align))
  15655. (message "Time difference inserted")))))
  15656. (defun org-make-tdiff-string (y d h m)
  15657. (let ((fmt "")
  15658. (l nil))
  15659. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15660. l (push y l)))
  15661. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15662. l (push d l)))
  15663. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15664. l (push h l)))
  15665. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15666. l (push m l)))
  15667. (apply 'format fmt (nreverse l))))
  15668. (defun org-time-string-to-time (s &optional buffer pos)
  15669. "Convert a timestamp string into internal time."
  15670. (condition-case errdata
  15671. (apply 'encode-time (org-parse-time-string s))
  15672. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15673. s (if (not (and buffer pos))
  15674. ""
  15675. (format " at %d in buffer `%s'" pos buffer))
  15676. (cdr errdata)))))
  15677. (defun org-time-string-to-seconds (s)
  15678. "Convert a timestamp string to a number of seconds."
  15679. (org-float-time (org-time-string-to-time s)))
  15680. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15681. "Convert a time stamp to an absolute day number.
  15682. If there is a specifier for a cyclic time stamp, get the closest
  15683. date to DAYNR.
  15684. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15685. The variable `date' is bound by the calendar when this is called."
  15686. (cond
  15687. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15688. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15689. daynr
  15690. (+ daynr 1000)))
  15691. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15692. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15693. (time-to-days (current-time))) (match-string 0 s)
  15694. prefer show-all))
  15695. (t (time-to-days
  15696. (condition-case errdata
  15697. (apply 'encode-time (org-parse-time-string s))
  15698. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15699. s (if (not (and buffer pos))
  15700. ""
  15701. (format " at %d in buffer `%s'" pos buffer))
  15702. (cdr errdata))))))))
  15703. (defun org-days-to-iso-week (days)
  15704. "Return the iso week number."
  15705. (require 'cal-iso)
  15706. (car (calendar-iso-from-absolute days)))
  15707. (defun org-small-year-to-year (year)
  15708. "Convert 2-digit years into 4-digit years.
  15709. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15710. The year 2000 cannot be abbreviated. Any year larger than 99
  15711. is returned unchanged."
  15712. (if (< year 38)
  15713. (setq year (+ 2000 year))
  15714. (if (< year 100)
  15715. (setq year (+ 1900 year))))
  15716. year)
  15717. (defun org-time-from-absolute (d)
  15718. "Return the time corresponding to date D.
  15719. D may be an absolute day number, or a calendar-type list (month day year)."
  15720. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15721. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15722. (defun org-calendar-holiday ()
  15723. "List of holidays, for Diary display in Org-mode."
  15724. (require 'holidays)
  15725. (let ((hl (funcall
  15726. (if (fboundp 'calendar-check-holidays)
  15727. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15728. (if hl (mapconcat 'identity hl "; "))))
  15729. (defun org-diary-sexp-entry (sexp entry date)
  15730. "Process a SEXP diary ENTRY for DATE."
  15731. (require 'diary-lib)
  15732. (let ((result (if calendar-debug-sexp
  15733. (let ((stack-trace-on-error t))
  15734. (eval (car (read-from-string sexp))))
  15735. (condition-case nil
  15736. (eval (car (read-from-string sexp)))
  15737. (error
  15738. (beep)
  15739. (message "Bad sexp at line %d in %s: %s"
  15740. (org-current-line)
  15741. (buffer-file-name) sexp)
  15742. (sleep-for 2))))))
  15743. (cond ((stringp result) (split-string result "; "))
  15744. ((and (consp result)
  15745. (not (consp (cdr result)))
  15746. (stringp (cdr result))) (cdr result))
  15747. ((and (consp result)
  15748. (stringp (car result))) result)
  15749. (result entry))))
  15750. (defun org-diary-to-ical-string (frombuf)
  15751. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15752. This uses the icalendar.el library."
  15753. (let* ((tmpdir (if (featurep 'xemacs)
  15754. (temp-directory)
  15755. temporary-file-directory))
  15756. (tmpfile (make-temp-name
  15757. (expand-file-name "orgics" tmpdir)))
  15758. buf rtn b e)
  15759. (with-current-buffer frombuf
  15760. (icalendar-export-region (point-min) (point-max) tmpfile)
  15761. (setq buf (find-buffer-visiting tmpfile))
  15762. (set-buffer buf)
  15763. (goto-char (point-min))
  15764. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15765. (setq b (match-beginning 0)))
  15766. (goto-char (point-max))
  15767. (if (re-search-backward "^END:VEVENT" nil t)
  15768. (setq e (match-end 0)))
  15769. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15770. (kill-buffer buf)
  15771. (delete-file tmpfile)
  15772. rtn))
  15773. (defun org-closest-date (start current change prefer show-all)
  15774. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15775. When PREFER is `past', return a date that is either CURRENT or past.
  15776. When PREFER is `future', return a date that is either CURRENT or future.
  15777. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15778. ;; Make the proper lists from the dates
  15779. (catch 'exit
  15780. (let ((a1 '(("h" . hour)
  15781. ("d" . day)
  15782. ("w" . week)
  15783. ("m" . month)
  15784. ("y" . year)))
  15785. (shour (nth 2 (org-parse-time-string start)))
  15786. dn dw sday cday n1 n2 n0
  15787. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15788. (setq start (org-date-to-gregorian start)
  15789. current (org-date-to-gregorian
  15790. (if show-all
  15791. current
  15792. (time-to-days (current-time))))
  15793. sday (calendar-absolute-from-gregorian start)
  15794. cday (calendar-absolute-from-gregorian current))
  15795. (if (<= cday sday) (throw 'exit sday))
  15796. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15797. (setq dn (string-to-number (match-string 1 change))
  15798. dw (cdr (assoc (match-string 2 change) a1)))
  15799. (user-error "Invalid change specifier: %s" change))
  15800. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15801. (cond
  15802. ((eq dw 'hour)
  15803. (let ((missing-hours
  15804. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15805. dn)))
  15806. (setq n1 (if (zerop missing-hours) cday
  15807. (- cday (1+ (floor (/ missing-hours 24)))))
  15808. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15809. ((eq dw 'day)
  15810. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15811. n2 (+ n1 dn)))
  15812. ((eq dw 'year)
  15813. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15814. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15815. (setq date1 (list m d y1)
  15816. n1 (calendar-absolute-from-gregorian date1)
  15817. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15818. n2 (calendar-absolute-from-gregorian date2)))
  15819. ((eq dw 'month)
  15820. ;; approx number of month between the two dates
  15821. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15822. ;; How often does dn fit in there?
  15823. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15824. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15825. m (+ m nm)
  15826. ny (floor (/ m 12))
  15827. y (+ y ny)
  15828. m (- m (* ny 12)))
  15829. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15830. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15831. (setq m2 (+ m dn) y2 y)
  15832. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15833. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15834. (while (<= n2 cday)
  15835. (setq n1 n2 m m2 y y2)
  15836. (setq m2 (+ m dn) y2 y)
  15837. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15838. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15839. ;; Make sure n1 is the earlier date
  15840. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15841. (if show-all
  15842. (cond
  15843. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15844. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15845. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15846. (cond
  15847. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15848. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15849. (t (if (= cday n1) n1 n2)))))))
  15850. (defun org-date-to-gregorian (date)
  15851. "Turn any specification of DATE into a Gregorian date for the calendar."
  15852. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15853. ((and (listp date) (= (length date) 3)) date)
  15854. ((stringp date)
  15855. (setq date (org-parse-time-string date))
  15856. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15857. ((listp date)
  15858. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15859. (defun org-parse-time-string (s &optional nodefault)
  15860. "Parse the standard Org-mode time string.
  15861. This should be a lot faster than the normal `parse-time-string'.
  15862. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15863. hour and minute fields will be nil if not given."
  15864. (cond ((string-match org-ts-regexp0 s)
  15865. (list 0
  15866. (if (or (match-beginning 8) (not nodefault))
  15867. (string-to-number (or (match-string 8 s) "0")))
  15868. (if (or (match-beginning 7) (not nodefault))
  15869. (string-to-number (or (match-string 7 s) "0")))
  15870. (string-to-number (match-string 4 s))
  15871. (string-to-number (match-string 3 s))
  15872. (string-to-number (match-string 2 s))
  15873. nil nil nil))
  15874. ((string-match "^<[^>]+>$" s)
  15875. (decode-time (seconds-to-time (org-matcher-time s))))
  15876. (t (error "Not a standard Org-mode time string: %s" s))))
  15877. (defun org-timestamp-up (&optional arg)
  15878. "Increase the date item at the cursor by one.
  15879. If the cursor is on the year, change the year. If it is on the month,
  15880. the day or the time, change that.
  15881. With prefix ARG, change by that many units."
  15882. (interactive "p")
  15883. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15884. (defun org-timestamp-down (&optional arg)
  15885. "Decrease the date item at the cursor by one.
  15886. If the cursor is on the year, change the year. If it is on the month,
  15887. the day or the time, change that.
  15888. With prefix ARG, change by that many units."
  15889. (interactive "p")
  15890. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15891. (defun org-timestamp-up-day (&optional arg)
  15892. "Increase the date in the time stamp by one day.
  15893. With prefix ARG, change that many days."
  15894. (interactive "p")
  15895. (if (and (not (org-at-timestamp-p t))
  15896. (org-at-heading-p))
  15897. (org-todo 'up)
  15898. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15899. (defun org-timestamp-down-day (&optional arg)
  15900. "Decrease the date in the time stamp by one day.
  15901. With prefix ARG, change that many days."
  15902. (interactive "p")
  15903. (if (and (not (org-at-timestamp-p t))
  15904. (org-at-heading-p))
  15905. (org-todo 'down)
  15906. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15907. (defun org-at-timestamp-p (&optional inactive-ok)
  15908. "Determine if the cursor is in or at a timestamp."
  15909. (interactive)
  15910. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15911. (pos (point))
  15912. (ans (or (looking-at tsr)
  15913. (save-excursion
  15914. (skip-chars-backward "^[<\n\r\t")
  15915. (if (> (point) (point-min)) (backward-char 1))
  15916. (and (looking-at tsr)
  15917. (> (- (match-end 0) pos) -1))))))
  15918. (and ans
  15919. (boundp 'org-ts-what)
  15920. (setq org-ts-what
  15921. (cond
  15922. ((= pos (match-beginning 0)) 'bracket)
  15923. ;; Point is considered to be "on the bracket" whether
  15924. ;; it's really on it or right after it.
  15925. ((= pos (1- (match-end 0))) 'bracket)
  15926. ((= pos (match-end 0)) 'after)
  15927. ((org-pos-in-match-range pos 2) 'year)
  15928. ((org-pos-in-match-range pos 3) 'month)
  15929. ((org-pos-in-match-range pos 7) 'hour)
  15930. ((org-pos-in-match-range pos 8) 'minute)
  15931. ((or (org-pos-in-match-range pos 4)
  15932. (org-pos-in-match-range pos 5)) 'day)
  15933. ((and (> pos (or (match-end 8) (match-end 5)))
  15934. (< pos (match-end 0)))
  15935. (- pos (or (match-end 8) (match-end 5))))
  15936. (t 'day))))
  15937. ans))
  15938. (defun org-toggle-timestamp-type ()
  15939. "Toggle the type (<active> or [inactive]) of a time stamp."
  15940. (interactive)
  15941. (when (org-at-timestamp-p t)
  15942. (let ((beg (match-beginning 0)) (end (match-end 0))
  15943. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15944. (save-excursion
  15945. (goto-char beg)
  15946. (while (re-search-forward "[][<>]" end t)
  15947. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15948. t t)))
  15949. (message "Timestamp is now %sactive"
  15950. (if (equal (char-after beg) ?<) "" "in")))))
  15951. (defun org-at-clock-log-p nil
  15952. "Is the cursor on the clock log line?"
  15953. (save-excursion
  15954. (move-beginning-of-line 1)
  15955. (looking-at "^[ \t]*CLOCK:")))
  15956. (defvar org-clock-history) ; defined in org-clock.el
  15957. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15958. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15959. "Change the date in the time stamp at point.
  15960. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15961. `year', `minute', `second'. If WHAT is not given, the cursor position
  15962. in the timestamp determines what will be changed.
  15963. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15964. (let ((origin (point)) origin-cat
  15965. with-hm inactive
  15966. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15967. org-ts-what
  15968. extra rem
  15969. ts time time0 fixnext clrgx)
  15970. (if (not (org-at-timestamp-p t))
  15971. (user-error "Not at a timestamp"))
  15972. (if (and (not what) (eq org-ts-what 'bracket))
  15973. (org-toggle-timestamp-type)
  15974. ;; Point isn't on brackets. Remember the part of the time-stamp
  15975. ;; the point was in. Indeed, size of time-stamps may change,
  15976. ;; but point must be kept in the same category nonetheless.
  15977. (setq origin-cat org-ts-what)
  15978. (if (and (not what) (not (eq org-ts-what 'day))
  15979. org-display-custom-times
  15980. (get-text-property (point) 'display)
  15981. (not (get-text-property (1- (point)) 'display)))
  15982. (setq org-ts-what 'day))
  15983. (setq org-ts-what (or what org-ts-what)
  15984. inactive (= (char-after (match-beginning 0)) ?\[)
  15985. ts (match-string 0))
  15986. (replace-match "")
  15987. (when (string-match
  15988. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15989. ts)
  15990. (setq extra (match-string 1 ts))
  15991. (if suppress-tmp-delay
  15992. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15993. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15994. (setq with-hm t))
  15995. (setq time0 (org-parse-time-string ts))
  15996. (when (and updown
  15997. (eq org-ts-what 'minute)
  15998. (not current-prefix-arg))
  15999. ;; This looks like s-up and s-down. Change by one rounding step.
  16000. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16001. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16002. (setcar (cdr time0) (+ (nth 1 time0)
  16003. (if (> n 0) (- rem) (- dm rem))))))
  16004. (setq time
  16005. (encode-time (or (car time0) 0)
  16006. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16007. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16008. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16009. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16010. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16011. (nthcdr 6 time0)))
  16012. (when (and (member org-ts-what '(hour minute))
  16013. extra
  16014. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16015. (setq extra (org-modify-ts-extra
  16016. extra
  16017. (if (eq org-ts-what 'hour) 2 5)
  16018. n dm)))
  16019. (when (integerp org-ts-what)
  16020. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16021. (if (eq what 'calendar)
  16022. (let ((cal-date (org-get-date-from-calendar)))
  16023. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16024. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16025. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16026. (setcar time0 (or (car time0) 0))
  16027. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16028. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16029. (setq time (apply 'encode-time time0))))
  16030. ;; Insert the new time-stamp, and ensure point stays in the same
  16031. ;; category as before (i.e. not after the last position in that
  16032. ;; category).
  16033. (let ((pos (point)))
  16034. ;; Stay before inserted string. `save-excursion' is of no use.
  16035. (setq org-last-changed-timestamp
  16036. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16037. (goto-char pos))
  16038. (save-match-data
  16039. (looking-at org-ts-regexp3)
  16040. (goto-char (cond
  16041. ;; `day' category ends before `hour' if any, or at
  16042. ;; the end of the day name.
  16043. ((eq origin-cat 'day)
  16044. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16045. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16046. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16047. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16048. ;; `year' and `month' have both fixed size: point
  16049. ;; couldn't have moved into another part.
  16050. (t origin))))
  16051. ;; Update clock if on a CLOCK line.
  16052. (org-clock-update-time-maybe)
  16053. ;; Maybe adjust the closest clock in `org-clock-history'
  16054. (when org-clock-adjust-closest
  16055. (if (not (and (org-at-clock-log-p)
  16056. (< 1 (length (delq nil (mapcar 'marker-position
  16057. org-clock-history))))))
  16058. (message "No clock to adjust")
  16059. (cond ((save-excursion ; fix previous clock?
  16060. (re-search-backward org-ts-regexp0 nil t)
  16061. (org-looking-back (concat org-clock-string " \\[")))
  16062. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16063. ((save-excursion ; fix next clock?
  16064. (re-search-backward org-ts-regexp0 nil t)
  16065. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16066. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16067. (save-window-excursion
  16068. ;; Find closest clock to point, adjust the previous/next one in history
  16069. (let* ((p (save-excursion (org-back-to-heading t)))
  16070. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16071. (clfixnth
  16072. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  16073. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16074. (if (not clfixpos)
  16075. (message "No clock to adjust")
  16076. (save-excursion
  16077. (org-goto-marker-or-bmk clfixpos)
  16078. (org-show-subtree)
  16079. (when (re-search-forward clrgx nil t)
  16080. (goto-char (match-beginning 1))
  16081. (let (org-clock-adjust-closest)
  16082. (org-timestamp-change n org-ts-what updown))
  16083. (message "Clock adjusted in %s for heading: %s"
  16084. (file-name-nondirectory (buffer-file-name))
  16085. (org-get-heading t t)))))))))
  16086. ;; Try to recenter the calendar window, if any.
  16087. (if (and org-calendar-follow-timestamp-change
  16088. (get-buffer-window "*Calendar*" t)
  16089. (memq org-ts-what '(day month year)))
  16090. (org-recenter-calendar (time-to-days time))))))
  16091. (defun org-modify-ts-extra (s pos n dm)
  16092. "Change the different parts of the lead-time and repeat fields in timestamp."
  16093. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16094. ng h m new rem)
  16095. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16096. (cond
  16097. ((or (org-pos-in-match-range pos 2)
  16098. (org-pos-in-match-range pos 3))
  16099. (setq m (string-to-number (match-string 3 s))
  16100. h (string-to-number (match-string 2 s)))
  16101. (if (org-pos-in-match-range pos 2)
  16102. (setq h (+ h n))
  16103. (setq n (* dm (org-no-warnings (signum n))))
  16104. (when (not (= 0 (setq rem (% m dm))))
  16105. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16106. (setq m (+ m n)))
  16107. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16108. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16109. (setq h (min 24 (max 0 h)))
  16110. (setq ng 1 new (format "-%02d:%02d" h m)))
  16111. ((org-pos-in-match-range pos 6)
  16112. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16113. ((org-pos-in-match-range pos 5)
  16114. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16115. ((org-pos-in-match-range pos 9)
  16116. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16117. ((org-pos-in-match-range pos 8)
  16118. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16119. (when ng
  16120. (setq s (concat
  16121. (substring s 0 (match-beginning ng))
  16122. new
  16123. (substring s (match-end ng))))))
  16124. s))
  16125. (defun org-recenter-calendar (date)
  16126. "If the calendar is visible, recenter it to DATE."
  16127. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16128. (when cwin
  16129. (let ((calendar-move-hook nil))
  16130. (with-selected-window cwin
  16131. (calendar-goto-date (if (listp date) date
  16132. (calendar-gregorian-from-absolute date))))))))
  16133. (defun org-goto-calendar (&optional arg)
  16134. "Go to the Emacs calendar at the current date.
  16135. If there is a time stamp in the current line, go to that date.
  16136. A prefix ARG can be used to force the current date."
  16137. (interactive "P")
  16138. (let ((tsr org-ts-regexp) diff
  16139. (calendar-move-hook nil)
  16140. (calendar-view-holidays-initially-flag nil)
  16141. (calendar-view-diary-initially-flag nil))
  16142. (if (or (org-at-timestamp-p)
  16143. (save-excursion
  16144. (beginning-of-line 1)
  16145. (looking-at (concat ".*" tsr))))
  16146. (let ((d1 (time-to-days (current-time)))
  16147. (d2 (time-to-days
  16148. (org-time-string-to-time (match-string 1)))))
  16149. (setq diff (- d2 d1))))
  16150. (calendar)
  16151. (calendar-goto-today)
  16152. (if (and diff (not arg)) (calendar-forward-day diff))))
  16153. (defun org-get-date-from-calendar ()
  16154. "Return a list (month day year) of date at point in calendar."
  16155. (with-current-buffer "*Calendar*"
  16156. (save-match-data
  16157. (calendar-cursor-to-date))))
  16158. (defun org-date-from-calendar ()
  16159. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16160. If there is already a time stamp at the cursor position, update it."
  16161. (interactive)
  16162. (if (org-at-timestamp-p t)
  16163. (org-timestamp-change 0 'calendar)
  16164. (let ((cal-date (org-get-date-from-calendar)))
  16165. (org-insert-time-stamp
  16166. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16167. (defcustom org-effort-durations
  16168. `(("h" . 60)
  16169. ("d" . ,(* 60 8))
  16170. ("w" . ,(* 60 8 5))
  16171. ("m" . ,(* 60 8 5 4))
  16172. ("y" . ,(* 60 8 5 40)))
  16173. "Conversion factor to minutes for an effort modifier.
  16174. Each entry has the form (MODIFIER . MINUTES).
  16175. In an effort string, a number followed by MODIFIER is multiplied
  16176. by the specified number of MINUTES to obtain an effort in
  16177. minutes.
  16178. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16179. effort string \"2hours\" is equivalent to 120 minutes."
  16180. :group 'org-agenda
  16181. :version "24.1"
  16182. :type '(alist :key-type (string :tag "Modifier")
  16183. :value-type (number :tag "Minutes")))
  16184. (defun org-minutes-to-clocksum-string (m)
  16185. "Format number of minutes as a clocksum string.
  16186. The format is determined by `org-time-clocksum-format',
  16187. `org-time-clocksum-use-fractional' and
  16188. `org-time-clocksum-fractional-format' and
  16189. `org-time-clocksum-use-effort-durations'."
  16190. (let ((clocksum "")
  16191. (m (round m)) ; Don't allow fractions of minutes
  16192. h d w mo y fmt n)
  16193. (setq h (if org-time-clocksum-use-effort-durations
  16194. (cdr (assoc "h" org-effort-durations)) 60)
  16195. d (if org-time-clocksum-use-effort-durations
  16196. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16197. w (if org-time-clocksum-use-effort-durations
  16198. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16199. mo (if org-time-clocksum-use-effort-durations
  16200. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16201. y (if org-time-clocksum-use-effort-durations
  16202. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16203. ;; fractional format
  16204. (if org-time-clocksum-use-fractional
  16205. (cond
  16206. ;; single format string
  16207. ((stringp org-time-clocksum-fractional-format)
  16208. (format org-time-clocksum-fractional-format (/ m (float h))))
  16209. ;; choice of fractional formats for different time units
  16210. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16211. (> (/ (truncate m) (* y d h)) 0))
  16212. (format fmt (/ m (* y d (float h)))))
  16213. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16214. (> (/ (truncate m) (* mo d h)) 0))
  16215. (format fmt (/ m (* mo d (float h)))))
  16216. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16217. (> (/ (truncate m) (* w d h)) 0))
  16218. (format fmt (/ m (* w d (float h)))))
  16219. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16220. (> (/ (truncate m) (* d h)) 0))
  16221. (format fmt (/ m (* d (float h)))))
  16222. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16223. (> (/ (truncate m) h) 0))
  16224. (format fmt (/ m (float h))))
  16225. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16226. (format fmt m))
  16227. ;; fall back to smallest time unit with a format
  16228. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16229. (format fmt (/ m (float h))))
  16230. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16231. (format fmt (/ m (* d (float h)))))
  16232. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16233. (format fmt (/ m (* w d (float h)))))
  16234. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16235. (format fmt (/ m (* mo d (float h)))))
  16236. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16237. (format fmt (/ m (* y d (float h))))))
  16238. ;; standard (non-fractional) format, with single format string
  16239. (if (stringp org-time-clocksum-format)
  16240. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16241. ;; separate formats components
  16242. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16243. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16244. (plist-get org-time-clocksum-format :require-years))
  16245. (setq clocksum (concat clocksum (format fmt n))
  16246. m (- m (* n y d h))))
  16247. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16248. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16249. (plist-get org-time-clocksum-format :require-months))
  16250. (setq clocksum (concat clocksum (format fmt n))
  16251. m (- m (* n mo d h))))
  16252. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16253. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16254. (plist-get org-time-clocksum-format :require-weeks))
  16255. (setq clocksum (concat clocksum (format fmt n))
  16256. m (- m (* n w d h))))
  16257. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16258. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16259. (plist-get org-time-clocksum-format :require-days))
  16260. (setq clocksum (concat clocksum (format fmt n))
  16261. m (- m (* n d h))))
  16262. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16263. (or (> (setq n (/ (truncate m) h)) 0)
  16264. (plist-get org-time-clocksum-format :require-hours))
  16265. (setq clocksum (concat clocksum (format fmt n))
  16266. m (- m (* n h))))
  16267. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16268. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16269. (setq clocksum (concat clocksum (format fmt m))))
  16270. ;; return formatted time duration
  16271. clocksum))))
  16272. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16273. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16274. "Org mode version 8.0")
  16275. (defun org-hours-to-clocksum-string (n)
  16276. (org-minutes-to-clocksum-string (* n 60)))
  16277. (defun org-hh:mm-string-to-minutes (s)
  16278. "Convert a string H:MM to a number of minutes.
  16279. If the string is just a number, interpret it as minutes.
  16280. In fact, the first hh:mm or number in the string will be taken,
  16281. there can be extra stuff in the string.
  16282. If no number is found, the return value is 0."
  16283. (cond
  16284. ((integerp s) s)
  16285. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16286. (+ (* (string-to-number (match-string 1 s)) 60)
  16287. (string-to-number (match-string 2 s))))
  16288. ((string-match "\\([0-9]+\\)" s)
  16289. (string-to-number (match-string 1 s)))
  16290. (t 0)))
  16291. (defcustom org-image-actual-width t
  16292. "Should we use the actual width of images when inlining them?
  16293. When set to `t', always use the image width.
  16294. When set to a number, use imagemagick (when available) to set
  16295. the image's width to this value.
  16296. When set to a number in a list, try to get the width from any
  16297. #+ATTR.* keyword if it matches a width specification like
  16298. #+ATTR_HTML: :width 300px
  16299. and fall back on that number if none is found.
  16300. When set to nil, try to get the width from an #+ATTR.* keyword
  16301. and fall back on the original width if none is found.
  16302. This requires Emacs >= 24.1, build with imagemagick support."
  16303. :group 'org-appearance
  16304. :version "24.4"
  16305. :package-version '(Org . "8.0")
  16306. :type '(choice
  16307. (const :tag "Use the image width" t)
  16308. (integer :tag "Use a number of pixels")
  16309. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16310. (const :tag "Use #+ATTR* or don't resize" nil)))
  16311. (defcustom org-agenda-inhibit-startup nil
  16312. "Inhibit startup when preparing agenda buffers.
  16313. When this variable is `t' (the default), the initialization of
  16314. the Org agenda buffers is inhibited: e.g. the visibility state
  16315. is not set, the tables are not re-aligned, etc."
  16316. :type 'boolean
  16317. :version "24.3"
  16318. :group 'org-agenda)
  16319. (defcustom org-agenda-ignore-drawer-properties nil
  16320. "Avoid updating text properties when building the agenda.
  16321. Properties are used to prepare buffers for effort estimates, appointments,
  16322. and subtree-local categories.
  16323. If you don't use these in the agenda, you can add them to this list and
  16324. agenda building will be a bit faster.
  16325. The value is a list, with zero or more of the symbols `effort', `appt',
  16326. or `category'."
  16327. :type '(set :greedy t
  16328. (const effort)
  16329. (const appt)
  16330. (const category))
  16331. :version "24.3"
  16332. :group 'org-agenda)
  16333. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16334. "Convert a duration string S to minutes.
  16335. A bare number is interpreted as minutes, modifiers can be set by
  16336. customizing `org-effort-durations' (which see).
  16337. Entries containing a colon are interpreted as H:MM by
  16338. `org-hh:mm-string-to-minutes'."
  16339. (let ((result 0)
  16340. (re (concat "\\([0-9.]+\\) *\\("
  16341. (regexp-opt (mapcar 'car org-effort-durations))
  16342. "\\)")))
  16343. (while (string-match re s)
  16344. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16345. (string-to-number (match-string 1 s))))
  16346. (setq s (replace-match "" nil t s)))
  16347. (setq result (floor result))
  16348. (incf result (org-hh:mm-string-to-minutes s))
  16349. (if output-to-string (number-to-string result) result)))
  16350. ;;;; Files
  16351. (defun org-save-all-org-buffers ()
  16352. "Save all Org-mode buffers without user confirmation."
  16353. (interactive)
  16354. (message "Saving all Org-mode buffers...")
  16355. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16356. (when (featurep 'org-id) (org-id-locations-save))
  16357. (message "Saving all Org-mode buffers... done"))
  16358. (defun org-revert-all-org-buffers ()
  16359. "Revert all Org-mode buffers.
  16360. Prompt for confirmation when there are unsaved changes.
  16361. Be sure you know what you are doing before letting this function
  16362. overwrite your changes.
  16363. This function is useful in a setup where one tracks org files
  16364. with a version control system, to revert on one machine after pulling
  16365. changes from another. I believe the procedure must be like this:
  16366. 1. M-x org-save-all-org-buffers
  16367. 2. Pull changes from the other machine, resolve conflicts
  16368. 3. M-x org-revert-all-org-buffers"
  16369. (interactive)
  16370. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16371. (user-error "Abort"))
  16372. (save-excursion
  16373. (save-window-excursion
  16374. (mapc
  16375. (lambda (b)
  16376. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16377. (with-current-buffer b buffer-file-name))
  16378. (org-pop-to-buffer-same-window b)
  16379. (revert-buffer t 'no-confirm)))
  16380. (buffer-list))
  16381. (when (and (featurep 'org-id) org-id-track-globally)
  16382. (org-id-locations-load)))))
  16383. ;;;; Agenda files
  16384. ;;;###autoload
  16385. (defun org-switchb (&optional arg)
  16386. "Switch between Org buffers.
  16387. With one prefix argument, restrict available buffers to files.
  16388. With two prefix arguments, restrict available buffers to agenda files.
  16389. Defaults to `iswitchb' for buffer name completion.
  16390. Set `org-completion-use-ido' to make it use ido instead."
  16391. (interactive "P")
  16392. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16393. ((equal arg '(16)) (org-buffer-list 'agenda))
  16394. (t (org-buffer-list))))
  16395. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16396. (org-completion-use-ido org-completion-use-ido))
  16397. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16398. (setq org-completion-use-iswitchb t))
  16399. (org-pop-to-buffer-same-window
  16400. (org-icompleting-read "Org buffer: "
  16401. (mapcar 'list (mapcar 'buffer-name blist))
  16402. nil t))))
  16403. ;;; Define some older names previously used for this functionality
  16404. ;;;###autoload
  16405. (defalias 'org-ido-switchb 'org-switchb)
  16406. ;;;###autoload
  16407. (defalias 'org-iswitchb 'org-switchb)
  16408. (defun org-buffer-list (&optional predicate exclude-tmp)
  16409. "Return a list of Org buffers.
  16410. PREDICATE can be `export', `files' or `agenda'.
  16411. export restrict the list to Export buffers.
  16412. files restrict the list to buffers visiting Org files.
  16413. agenda restrict the list to buffers visiting agenda files.
  16414. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16415. (let* ((bfn nil)
  16416. (agenda-files (and (eq predicate 'agenda)
  16417. (mapcar 'file-truename (org-agenda-files t))))
  16418. (filter
  16419. (cond
  16420. ((eq predicate 'files)
  16421. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16422. ((eq predicate 'export)
  16423. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16424. ((eq predicate 'agenda)
  16425. (lambda (b)
  16426. (with-current-buffer b
  16427. (and (derived-mode-p 'org-mode)
  16428. (setq bfn (buffer-file-name b))
  16429. (member (file-truename bfn) agenda-files)))))
  16430. (t (lambda (b) (with-current-buffer b
  16431. (or (derived-mode-p 'org-mode)
  16432. (string-match "\*Org .*Export"
  16433. (buffer-name b)))))))))
  16434. (delq nil
  16435. (mapcar
  16436. (lambda(b)
  16437. (if (and (funcall filter b)
  16438. (or (not exclude-tmp)
  16439. (not (string-match "tmp" (buffer-name b)))))
  16440. b
  16441. nil))
  16442. (buffer-list)))))
  16443. (defun org-agenda-files (&optional unrestricted archives)
  16444. "Get the list of agenda files.
  16445. Optional UNRESTRICTED means return the full list even if a restriction
  16446. is currently in place.
  16447. When ARCHIVES is t, include all archive files that are really being
  16448. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16449. `org-agenda-archives-mode' is t."
  16450. (let ((files
  16451. (cond
  16452. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16453. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16454. ((listp org-agenda-files) org-agenda-files)
  16455. (t (error "Invalid value of `org-agenda-files'")))))
  16456. (setq files (apply 'append
  16457. (mapcar (lambda (f)
  16458. (if (file-directory-p f)
  16459. (directory-files
  16460. f t org-agenda-file-regexp)
  16461. (list f)))
  16462. files)))
  16463. (when org-agenda-skip-unavailable-files
  16464. (setq files (delq nil
  16465. (mapcar (function
  16466. (lambda (file)
  16467. (and (file-readable-p file) file)))
  16468. files))))
  16469. (when (or (eq archives t)
  16470. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16471. (setq files (org-add-archive-files files)))
  16472. files))
  16473. (defun org-agenda-file-p (&optional file)
  16474. "Return non-nil, if FILE is an agenda file.
  16475. If FILE is omitted, use the file associated with the current
  16476. buffer."
  16477. (member (or file (buffer-file-name))
  16478. (org-agenda-files t)))
  16479. (defun org-edit-agenda-file-list ()
  16480. "Edit the list of agenda files.
  16481. Depending on setup, this either uses customize to edit the variable
  16482. `org-agenda-files', or it visits the file that is holding the list. In the
  16483. latter case, the buffer is set up in a way that saving it automatically kills
  16484. the buffer and restores the previous window configuration."
  16485. (interactive)
  16486. (if (stringp org-agenda-files)
  16487. (let ((cw (current-window-configuration)))
  16488. (find-file org-agenda-files)
  16489. (org-set-local 'org-window-configuration cw)
  16490. (org-add-hook 'after-save-hook
  16491. (lambda ()
  16492. (set-window-configuration
  16493. (prog1 org-window-configuration
  16494. (kill-buffer (current-buffer))))
  16495. (org-install-agenda-files-menu)
  16496. (message "New agenda file list installed"))
  16497. nil 'local)
  16498. (message "%s" (substitute-command-keys
  16499. "Edit list and finish with \\[save-buffer]")))
  16500. (customize-variable 'org-agenda-files)))
  16501. (defun org-store-new-agenda-file-list (list)
  16502. "Set new value for the agenda file list and save it correctly."
  16503. (if (stringp org-agenda-files)
  16504. (let ((fe (org-read-agenda-file-list t)) b u)
  16505. (while (setq b (find-buffer-visiting org-agenda-files))
  16506. (kill-buffer b))
  16507. (with-temp-file org-agenda-files
  16508. (insert
  16509. (mapconcat
  16510. (lambda (f) ;; Keep un-expanded entries.
  16511. (if (setq u (assoc f fe))
  16512. (cdr u)
  16513. f))
  16514. list "\n")
  16515. "\n")))
  16516. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16517. (org-insert-mode-line-in-empty-file nil))
  16518. (setq org-agenda-files list)
  16519. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16520. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16521. "Read the list of agenda files from a file.
  16522. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16523. filenames, used by `org-store-new-agenda-file-list' to write back
  16524. un-expanded file names."
  16525. (when (file-directory-p org-agenda-files)
  16526. (error "`org-agenda-files' cannot be a single directory"))
  16527. (when (stringp org-agenda-files)
  16528. (with-temp-buffer
  16529. (insert-file-contents org-agenda-files)
  16530. (mapcar
  16531. (lambda (f)
  16532. (let ((e (expand-file-name (substitute-in-file-name f)
  16533. org-directory)))
  16534. (if pair-with-expansion
  16535. (cons e f)
  16536. e)))
  16537. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16538. ;;;###autoload
  16539. (defun org-cycle-agenda-files ()
  16540. "Cycle through the files in `org-agenda-files'.
  16541. If the current buffer visits an agenda file, find the next one in the list.
  16542. If the current buffer does not, find the first agenda file."
  16543. (interactive)
  16544. (let* ((fs (org-agenda-files t))
  16545. (files (append fs (list (car fs))))
  16546. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16547. file)
  16548. (unless files (user-error "No agenda files"))
  16549. (catch 'exit
  16550. (while (setq file (pop files))
  16551. (if (equal (file-truename file) tcf)
  16552. (when (car files)
  16553. (find-file (car files))
  16554. (throw 'exit t))))
  16555. (find-file (car fs)))
  16556. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16557. (defun org-agenda-file-to-front (&optional to-end)
  16558. "Move/add the current file to the top of the agenda file list.
  16559. If the file is not present in the list, it is added to the front. If it is
  16560. present, it is moved there. With optional argument TO-END, add/move to the
  16561. end of the list."
  16562. (interactive "P")
  16563. (let ((org-agenda-skip-unavailable-files nil)
  16564. (file-alist (mapcar (lambda (x)
  16565. (cons (file-truename x) x))
  16566. (org-agenda-files t)))
  16567. (ctf (file-truename
  16568. (or buffer-file-name
  16569. (user-error "Please save the current buffer to a file"))))
  16570. x had)
  16571. (setq x (assoc ctf file-alist) had x)
  16572. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16573. (if to-end
  16574. (setq file-alist (append (delq x file-alist) (list x)))
  16575. (setq file-alist (cons x (delq x file-alist))))
  16576. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16577. (org-install-agenda-files-menu)
  16578. (message "File %s to %s of agenda file list"
  16579. (if had "moved" "added") (if to-end "end" "front"))))
  16580. (defun org-remove-file (&optional file)
  16581. "Remove current file from the list of files in variable `org-agenda-files'.
  16582. These are the files which are being checked for agenda entries.
  16583. Optional argument FILE means use this file instead of the current."
  16584. (interactive)
  16585. (let* ((org-agenda-skip-unavailable-files nil)
  16586. (file (or file buffer-file-name
  16587. (user-error "Current buffer does not visit a file")))
  16588. (true-file (file-truename file))
  16589. (afile (abbreviate-file-name file))
  16590. (files (delq nil (mapcar
  16591. (lambda (x)
  16592. (if (equal true-file
  16593. (file-truename x))
  16594. nil x))
  16595. (org-agenda-files t)))))
  16596. (if (not (= (length files) (length (org-agenda-files t))))
  16597. (progn
  16598. (org-store-new-agenda-file-list files)
  16599. (org-install-agenda-files-menu)
  16600. (message "Removed file: %s" afile))
  16601. (message "File was not in list: %s (not removed)" afile))))
  16602. (defun org-file-menu-entry (file)
  16603. (vector file (list 'find-file file) t))
  16604. (defun org-check-agenda-file (file)
  16605. "Make sure FILE exists. If not, ask user what to do."
  16606. (when (not (file-exists-p file))
  16607. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16608. (abbreviate-file-name file))
  16609. (let ((r (downcase (read-char-exclusive))))
  16610. (cond
  16611. ((equal r ?r)
  16612. (org-remove-file file)
  16613. (throw 'nextfile t))
  16614. (t (error "Abort"))))))
  16615. (defun org-get-agenda-file-buffer (file)
  16616. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16617. it to the list of buffers which might be released later."
  16618. (let ((buf (org-find-base-buffer-visiting file)))
  16619. (if buf
  16620. buf ; just return it
  16621. ;; Make a new buffer and remember it
  16622. (setq buf (find-file-noselect file))
  16623. (if buf (push buf org-agenda-new-buffers))
  16624. buf)))
  16625. (defun org-release-buffers (blist)
  16626. "Release all buffers in list, asking the user for confirmation when needed.
  16627. When a buffer is unmodified, it is just killed. When modified, it is saved
  16628. \(if the user agrees) and then killed."
  16629. (let (buf file)
  16630. (while (setq buf (pop blist))
  16631. (setq file (buffer-file-name buf))
  16632. (when (and (buffer-modified-p buf)
  16633. file
  16634. (y-or-n-p (format "Save file %s? " file)))
  16635. (with-current-buffer buf (save-buffer)))
  16636. (kill-buffer buf))))
  16637. (defun org-agenda-prepare-buffers (files)
  16638. "Create buffers for all agenda files, protect archived trees and comments."
  16639. (interactive)
  16640. (let ((pa '(:org-archived t))
  16641. (pc '(:org-comment t))
  16642. (pall '(:org-archived t :org-comment t))
  16643. (inhibit-read-only t)
  16644. (org-inhibit-startup org-agenda-inhibit-startup)
  16645. (rea (concat ":" org-archive-tag ":"))
  16646. file re pos)
  16647. (setq org-tag-alist-for-agenda nil
  16648. org-tag-groups-alist-for-agenda nil)
  16649. (save-excursion
  16650. (save-restriction
  16651. (while (setq file (pop files))
  16652. (catch 'nextfile
  16653. (if (bufferp file)
  16654. (set-buffer file)
  16655. (org-check-agenda-file file)
  16656. (set-buffer (org-get-agenda-file-buffer file)))
  16657. (widen)
  16658. (org-set-regexps-and-options-for-tags)
  16659. (setq pos (point))
  16660. (goto-char (point-min))
  16661. (let ((case-fold-search t))
  16662. (when (search-forward "#+setupfile" nil t)
  16663. ;; Don't set all regexps and options systematically as
  16664. ;; this is only run for setting agenda tags from setup
  16665. ;; file
  16666. (org-set-regexps-and-options)))
  16667. (or (memq 'category org-agenda-ignore-drawer-properties)
  16668. (org-refresh-category-properties))
  16669. (or (memq 'effort org-agenda-ignore-drawer-properties)
  16670. (org-refresh-properties org-effort-property 'org-effort))
  16671. (or (memq 'appt org-agenda-ignore-drawer-properties)
  16672. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  16673. (setq org-todo-keywords-for-agenda
  16674. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16675. (setq org-done-keywords-for-agenda
  16676. (append org-done-keywords-for-agenda org-done-keywords))
  16677. (setq org-todo-keyword-alist-for-agenda
  16678. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16679. (setq org-tag-alist-for-agenda
  16680. (org-uniquify
  16681. (append org-tag-alist-for-agenda
  16682. org-tag-alist
  16683. org-tag-persistent-alist)))
  16684. (if org-group-tags
  16685. (setq org-tag-groups-alist-for-agenda
  16686. (org-uniquify-alist
  16687. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16688. (org-with-silent-modifications
  16689. (save-excursion
  16690. (remove-text-properties (point-min) (point-max) pall)
  16691. (when org-agenda-skip-archived-trees
  16692. (goto-char (point-min))
  16693. (while (re-search-forward rea nil t)
  16694. (if (org-at-heading-p t)
  16695. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16696. (goto-char (point-min))
  16697. (setq re (format org-heading-keyword-regexp-format
  16698. org-comment-string))
  16699. (while (re-search-forward re nil t)
  16700. (add-text-properties
  16701. (match-beginning 0) (org-end-of-subtree t) pc))))
  16702. (goto-char pos)))))
  16703. (setq org-todo-keywords-for-agenda
  16704. (org-uniquify org-todo-keywords-for-agenda))
  16705. (setq org-todo-keyword-alist-for-agenda
  16706. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16707. ;;;; CDLaTeX minor mode
  16708. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16709. "Keymap for the minor `org-cdlatex-mode'.")
  16710. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16711. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16712. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16713. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16714. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16715. (defvar org-cdlatex-texmathp-advice-is-done nil
  16716. "Flag remembering if we have applied the advice to texmathp already.")
  16717. (define-minor-mode org-cdlatex-mode
  16718. "Toggle the minor `org-cdlatex-mode'.
  16719. This mode supports entering LaTeX environment and math in LaTeX fragments
  16720. in Org-mode.
  16721. \\{org-cdlatex-mode-map}"
  16722. nil " OCDL" nil
  16723. (when org-cdlatex-mode
  16724. (require 'cdlatex)
  16725. (run-hooks 'cdlatex-mode-hook)
  16726. (cdlatex-compute-tables))
  16727. (unless org-cdlatex-texmathp-advice-is-done
  16728. (setq org-cdlatex-texmathp-advice-is-done t)
  16729. (defadvice texmathp (around org-math-always-on activate)
  16730. "Always return t in org-mode buffers.
  16731. This is because we want to insert math symbols without dollars even outside
  16732. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16733. an embedded LaTeX fragment, let texmathp do its job.
  16734. \\[org-cdlatex-mode-map]"
  16735. (interactive)
  16736. (let (p)
  16737. (cond
  16738. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16739. ((eq this-command 'cdlatex-math-symbol)
  16740. (setq ad-return-value t
  16741. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16742. (t
  16743. (let ((p (org-inside-LaTeX-fragment-p)))
  16744. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16745. (setq ad-return-value t
  16746. texmathp-why '("Org-mode embedded math" . 0))
  16747. (if p ad-do-it)))))))))
  16748. (defun turn-on-org-cdlatex ()
  16749. "Unconditionally turn on `org-cdlatex-mode'."
  16750. (org-cdlatex-mode 1))
  16751. (defun org-try-cdlatex-tab ()
  16752. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16753. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16754. - inside a LaTeX fragment, or
  16755. - after the first word in a line, where an abbreviation expansion could
  16756. insert a LaTeX environment."
  16757. (when org-cdlatex-mode
  16758. (cond
  16759. ;; Before any word on the line: No expansion possible.
  16760. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16761. ;; Just after first word on the line: Expand it. Make sure it
  16762. ;; cannot happen on headlines, though.
  16763. ((save-excursion
  16764. (skip-chars-backward "a-zA-Z0-9*")
  16765. (skip-chars-backward " \t")
  16766. (and (bolp) (not (org-at-heading-p))))
  16767. (cdlatex-tab) t)
  16768. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16769. (defun org-cdlatex-underscore-caret (&optional arg)
  16770. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16771. Revert to the normal definition outside of these fragments."
  16772. (interactive "P")
  16773. (if (org-inside-LaTeX-fragment-p)
  16774. (call-interactively 'cdlatex-sub-superscript)
  16775. (let (org-cdlatex-mode)
  16776. (call-interactively (key-binding (vector last-input-event))))))
  16777. (defun org-cdlatex-math-modify (&optional arg)
  16778. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16779. Revert to the normal definition outside of these fragments."
  16780. (interactive "P")
  16781. (if (org-inside-LaTeX-fragment-p)
  16782. (call-interactively 'cdlatex-math-modify)
  16783. (let (org-cdlatex-mode)
  16784. (call-interactively (key-binding (vector last-input-event))))))
  16785. ;;;; LaTeX fragments
  16786. (defun org-inside-LaTeX-fragment-p ()
  16787. "Test if point is inside a LaTeX fragment.
  16788. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16789. sequence appearing also before point.
  16790. Even though the matchers for math are configurable, this function assumes
  16791. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16792. delimiters are skipped when they have been removed by customization.
  16793. The return value is nil, or a cons cell with the delimiter and the
  16794. position of this delimiter.
  16795. This function does a reasonably good job, but can locally be fooled by
  16796. for example currency specifications. For example it will assume being in
  16797. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16798. fragments that are properly closed, but during editing, we have to live
  16799. with the uncertainty caused by missing closing delimiters. This function
  16800. looks only before point, not after."
  16801. (catch 'exit
  16802. (let ((pos (point))
  16803. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16804. (lim (progn
  16805. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16806. (point)))
  16807. dd-on str (start 0) m re)
  16808. (goto-char pos)
  16809. (when dodollar
  16810. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16811. re (nth 1 (assoc "$" org-latex-regexps)))
  16812. (while (string-match re str start)
  16813. (cond
  16814. ((= (match-end 0) (length str))
  16815. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16816. ((= (match-end 0) (- (length str) 5))
  16817. (throw 'exit nil))
  16818. (t (setq start (match-end 0))))))
  16819. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16820. (goto-char pos)
  16821. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16822. (and (match-beginning 2) (throw 'exit nil))
  16823. ;; count $$
  16824. (while (re-search-backward "\\$\\$" lim t)
  16825. (setq dd-on (not dd-on)))
  16826. (goto-char pos)
  16827. (if dd-on (cons "$$" m))))))
  16828. (defun org-inside-latex-macro-p ()
  16829. "Is point inside a LaTeX macro or its arguments?"
  16830. (save-match-data
  16831. (org-in-regexp
  16832. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16833. (defvar org-latex-fragment-image-overlays nil
  16834. "List of overlays carrying the images of latex fragments.")
  16835. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16836. (defun org-remove-latex-fragment-image-overlays ()
  16837. "Remove all overlays with LaTeX fragment images in current buffer."
  16838. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16839. (setq org-latex-fragment-image-overlays nil))
  16840. (defun org-preview-latex-fragment (&optional subtree)
  16841. "Preview the LaTeX fragment at point, or all locally or globally.
  16842. If the cursor is in a LaTeX fragment, create the image and overlay
  16843. it over the source code. If there is no fragment at point, display
  16844. all fragments in the current text, from one headline to the next. With
  16845. prefix SUBTREE, display all fragments in the current subtree. With a
  16846. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16847. the cursor is before the first headline,
  16848. display all fragments in the buffer.
  16849. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16850. (interactive "P")
  16851. (unless buffer-file-name
  16852. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16853. (when (display-graphic-p)
  16854. (org-remove-latex-fragment-image-overlays)
  16855. (save-excursion
  16856. (save-restriction
  16857. (let (beg end at msg)
  16858. (cond
  16859. ((or (equal subtree '(16))
  16860. (not (save-excursion
  16861. (re-search-backward org-outline-regexp-bol nil t))))
  16862. (setq beg (point-min) end (point-max)
  16863. msg "Creating images for buffer...%s"))
  16864. ((equal subtree '(4))
  16865. (org-back-to-heading)
  16866. (setq beg (point) end (org-end-of-subtree t)
  16867. msg "Creating images for subtree...%s"))
  16868. (t
  16869. (if (setq at (org-inside-LaTeX-fragment-p))
  16870. (goto-char (max (point-min) (- (cdr at) 2)))
  16871. (org-back-to-heading))
  16872. (setq beg (point) end (progn (outline-next-heading) (point))
  16873. msg (if at "Creating image...%s"
  16874. "Creating images for entry...%s"))))
  16875. (message msg "")
  16876. (narrow-to-region beg end)
  16877. (goto-char beg)
  16878. (org-format-latex
  16879. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16880. (file-name-nondirectory
  16881. buffer-file-name)))
  16882. default-directory 'overlays msg at 'forbuffer
  16883. org-latex-create-formula-image-program)
  16884. (message msg "done. Use `C-c C-c' to remove images."))))))
  16885. (defun org-format-latex (prefix &optional dir overlays msg at
  16886. forbuffer processing-type)
  16887. "Replace LaTeX fragments with links to an image, and produce images.
  16888. Some of the options can be changed using the variable
  16889. `org-format-latex-options'."
  16890. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16891. (let* ((prefixnodir (file-name-nondirectory prefix))
  16892. (absprefix (expand-file-name prefix dir))
  16893. (todir (file-name-directory absprefix))
  16894. (opt org-format-latex-options)
  16895. (optnew org-format-latex-options)
  16896. (matchers (plist-get opt :matchers))
  16897. (re-list org-latex-regexps)
  16898. (cnt 0) txt hash link beg end re e checkdir
  16899. string
  16900. m n block-type block linkfile movefile ov)
  16901. ;; Check the different regular expressions
  16902. (while (setq e (pop re-list))
  16903. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16904. block (if block-type "\n\n" ""))
  16905. (when (member m matchers)
  16906. (goto-char (point-min))
  16907. (while (re-search-forward re nil t)
  16908. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16909. (or (not overlays)
  16910. (not (eq (get-char-property (match-beginning n)
  16911. 'org-overlay-type)
  16912. 'org-latex-overlay))))
  16913. (cond
  16914. ((eq processing-type 'verbatim))
  16915. ((eq processing-type 'mathjax)
  16916. ;; Prepare for MathJax processing.
  16917. (setq string (match-string n))
  16918. (when (member m '("$" "$1"))
  16919. (save-excursion
  16920. (delete-region (match-beginning n) (match-end n))
  16921. (goto-char (match-beginning n))
  16922. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16923. ((or (eq processing-type 'dvipng)
  16924. (eq processing-type 'imagemagick))
  16925. ;; Process to an image.
  16926. (setq txt (match-string n)
  16927. beg (match-beginning n) end (match-end n)
  16928. cnt (1+ cnt))
  16929. (let ((face (face-at-point))
  16930. (fg (plist-get opt :foreground))
  16931. (bg (plist-get opt :background))
  16932. ;; Ensure full list is printed.
  16933. print-length print-level)
  16934. (when forbuffer
  16935. ;; Get the colors from the face at point.
  16936. (goto-char beg)
  16937. (when (eq fg 'auto)
  16938. (setq fg (face-attribute face :foreground nil 'default)))
  16939. (when (eq bg 'auto)
  16940. (setq bg (face-attribute face :background nil 'default)))
  16941. (setq optnew (copy-sequence opt))
  16942. (plist-put optnew :foreground fg)
  16943. (plist-put optnew :background bg))
  16944. (setq hash (sha1 (prin1-to-string
  16945. (list org-format-latex-header
  16946. org-latex-default-packages-alist
  16947. org-latex-packages-alist
  16948. org-format-latex-options
  16949. forbuffer txt fg bg)))
  16950. linkfile (format "%s_%s.png" prefix hash)
  16951. movefile (format "%s_%s.png" absprefix hash)))
  16952. (setq link (concat block "[[file:" linkfile "]]" block))
  16953. (if msg (message msg cnt))
  16954. (goto-char beg)
  16955. (unless checkdir ; Ensure the directory exists.
  16956. (setq checkdir t)
  16957. (or (file-directory-p todir) (make-directory todir t)))
  16958. (unless (file-exists-p movefile)
  16959. (org-create-formula-image
  16960. txt movefile optnew forbuffer processing-type))
  16961. (if overlays
  16962. (progn
  16963. (mapc (lambda (o)
  16964. (if (eq (overlay-get o 'org-overlay-type)
  16965. 'org-latex-overlay)
  16966. (delete-overlay o)))
  16967. (overlays-in beg end))
  16968. (setq ov (make-overlay beg end))
  16969. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16970. (if (featurep 'xemacs)
  16971. (progn
  16972. (overlay-put ov 'invisible t)
  16973. (overlay-put
  16974. ov 'end-glyph
  16975. (make-glyph (vector 'png :file movefile))))
  16976. (overlay-put
  16977. ov 'display
  16978. (list 'image :type 'png :file movefile :ascent 'center)))
  16979. (push ov org-latex-fragment-image-overlays)
  16980. (goto-char end))
  16981. (delete-region beg end)
  16982. (insert (org-add-props link
  16983. (list 'org-latex-src
  16984. (replace-regexp-in-string
  16985. "\"" "" txt)
  16986. 'org-latex-src-embed-type
  16987. (if block-type 'paragraph 'character))))))
  16988. ((eq processing-type 'mathml)
  16989. ;; Process to MathML
  16990. (unless (save-match-data (org-format-latex-mathml-available-p))
  16991. (user-error "LaTeX to MathML converter not configured"))
  16992. (setq txt (match-string n)
  16993. beg (match-beginning n) end (match-end n)
  16994. cnt (1+ cnt))
  16995. (if msg (message msg cnt))
  16996. (goto-char beg)
  16997. (delete-region beg end)
  16998. (insert (org-format-latex-as-mathml
  16999. txt block-type prefix dir)))
  17000. (t
  17001. (error "Unknown conversion type %s for LaTeX fragments"
  17002. processing-type)))))))))
  17003. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17004. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17005. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17006. sucessful, return the portion between \"<math...> </math>\"
  17007. elements otherwise return nil. When MATHML-FILE is specified,
  17008. write the results in to that file. When invoked as an
  17009. interactive command, prompt for LATEX-FRAG, with initial value
  17010. set to the current active region and echo the results for user
  17011. inspection."
  17012. (interactive (list (let ((frag (when (org-region-active-p)
  17013. (buffer-substring-no-properties
  17014. (region-beginning) (region-end)))))
  17015. (read-string "LaTeX Fragment: " frag nil frag))))
  17016. (unless latex-frag (error "Invalid LaTeX fragment"))
  17017. (let* ((tmp-in-file (file-relative-name
  17018. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17019. (ignore (write-region latex-frag nil tmp-in-file))
  17020. (tmp-out-file (file-relative-name
  17021. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17022. (cmd (format-spec
  17023. org-latex-to-mathml-convert-command
  17024. `((?j . ,(shell-quote-argument
  17025. (expand-file-name org-latex-to-mathml-jar-file)))
  17026. (?I . ,(shell-quote-argument tmp-in-file))
  17027. (?o . ,(shell-quote-argument tmp-out-file)))))
  17028. mathml shell-command-output)
  17029. (when (org-called-interactively-p 'any)
  17030. (unless (org-format-latex-mathml-available-p)
  17031. (user-error "LaTeX to MathML converter not configured")))
  17032. (message "Running %s" cmd)
  17033. (setq shell-command-output (shell-command-to-string cmd))
  17034. (setq mathml
  17035. (when (file-readable-p tmp-out-file)
  17036. (with-current-buffer (find-file-noselect tmp-out-file t)
  17037. (goto-char (point-min))
  17038. (when (re-search-forward
  17039. (concat
  17040. (regexp-quote
  17041. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  17042. "\\(.\\|\n\\)*"
  17043. (regexp-quote "</math>")) nil t)
  17044. (prog1 (match-string 0) (kill-buffer))))))
  17045. (cond
  17046. (mathml
  17047. (setq mathml
  17048. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17049. (when mathml-file
  17050. (write-region mathml nil mathml-file))
  17051. (when (org-called-interactively-p 'any)
  17052. (message mathml)))
  17053. ((message "LaTeX to MathML conversion failed")
  17054. (message shell-command-output)))
  17055. (delete-file tmp-in-file)
  17056. (when (file-exists-p tmp-out-file)
  17057. (delete-file tmp-out-file))
  17058. mathml))
  17059. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17060. prefix &optional dir)
  17061. "Use `org-create-math-formula' but check local cache first."
  17062. (let* ((absprefix (expand-file-name prefix dir))
  17063. (print-length nil) (print-level nil)
  17064. (formula-id (concat
  17065. "formula-"
  17066. (sha1
  17067. (prin1-to-string
  17068. (list latex-frag
  17069. org-latex-to-mathml-convert-command)))))
  17070. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17071. (formula-cache-dir (file-name-directory formula-cache)))
  17072. (unless (file-directory-p formula-cache-dir)
  17073. (make-directory formula-cache-dir t))
  17074. (unless (file-exists-p formula-cache)
  17075. (org-create-math-formula latex-frag formula-cache))
  17076. (if (file-exists-p formula-cache)
  17077. ;; Successful conversion. Return the link to MathML file.
  17078. (org-add-props
  17079. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17080. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17081. 'org-latex-src-embed-type (if latex-frag-type
  17082. 'paragraph 'character)))
  17083. ;; Failed conversion. Return the LaTeX fragment verbatim
  17084. latex-frag)))
  17085. (defun org-create-formula-image (string tofile options buffer &optional type)
  17086. "Create an image from LaTeX source using dvipng or convert.
  17087. This function calls either `org-create-formula-image-with-dvipng'
  17088. or `org-create-formula-image-with-imagemagick' depending on the
  17089. value of `org-latex-create-formula-image-program' or on the value
  17090. of the optional TYPE variable.
  17091. Note: ultimately these two function should be combined as they
  17092. share a good deal of logic."
  17093. (org-check-external-command
  17094. "latex" "needed to convert LaTeX fragments to images")
  17095. (funcall
  17096. (case (or type org-latex-create-formula-image-program)
  17097. ('dvipng
  17098. (org-check-external-command
  17099. "dvipng" "needed to convert LaTeX fragments to images")
  17100. #'org-create-formula-image-with-dvipng)
  17101. ('imagemagick
  17102. (org-check-external-command
  17103. "convert" "you need to install imagemagick")
  17104. #'org-create-formula-image-with-imagemagick)
  17105. (t (error
  17106. "Invalid value of `org-latex-create-formula-image-program'")))
  17107. string tofile options buffer))
  17108. (declare-function org-export-get-backend "ox" (name))
  17109. (declare-function org-export--get-global-options "ox" (&optional backend))
  17110. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17111. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17112. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17113. (defun org-create-formula--latex-header ()
  17114. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17115. (let ((info (org-combine-plists (org-export--get-global-options
  17116. (org-export-get-backend 'latex))
  17117. (org-export--get-inbuffer-options
  17118. (org-export-get-backend 'latex)))))
  17119. (org-latex-guess-babel-language
  17120. (org-latex-guess-inputenc
  17121. (org-splice-latex-header
  17122. org-format-latex-header
  17123. org-latex-default-packages-alist
  17124. org-latex-packages-alist t
  17125. (plist-get info :latex-header)))
  17126. info)))
  17127. ;; This function borrows from Ganesh Swami's latex2png.el
  17128. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17129. "This calls dvipng."
  17130. (require 'ox-latex)
  17131. (let* ((tmpdir (if (featurep 'xemacs)
  17132. (temp-directory)
  17133. temporary-file-directory))
  17134. (texfilebase (make-temp-name
  17135. (expand-file-name "orgtex" tmpdir)))
  17136. (texfile (concat texfilebase ".tex"))
  17137. (dvifile (concat texfilebase ".dvi"))
  17138. (pngfile (concat texfilebase ".png"))
  17139. (fnh (if (featurep 'xemacs)
  17140. (font-height (face-font 'default))
  17141. (face-attribute 'default :height nil)))
  17142. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17143. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17144. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17145. "Black"))
  17146. (bg (or (plist-get options (if buffer :background :html-background))
  17147. "Transparent")))
  17148. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17149. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17150. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17151. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17152. (let ((latex-header (org-create-formula--latex-header)))
  17153. (with-temp-file texfile
  17154. (insert latex-header)
  17155. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17156. (let ((dir default-directory))
  17157. (condition-case nil
  17158. (progn
  17159. (cd tmpdir)
  17160. (call-process "latex" nil nil nil texfile))
  17161. (error nil))
  17162. (cd dir))
  17163. (if (not (file-exists-p dvifile))
  17164. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17165. (condition-case nil
  17166. (if (featurep 'xemacs)
  17167. (call-process "dvipng" nil nil nil
  17168. "-fg" fg "-bg" bg
  17169. "-T" "tight"
  17170. "-o" pngfile
  17171. dvifile)
  17172. (call-process "dvipng" nil nil nil
  17173. "-fg" fg "-bg" bg
  17174. "-D" dpi
  17175. ;;"-x" scale "-y" scale
  17176. "-T" "tight"
  17177. "-o" pngfile
  17178. dvifile))
  17179. (error nil))
  17180. (if (not (file-exists-p pngfile))
  17181. (if org-format-latex-signal-error
  17182. (error "Failed to create png file from %s" texfile)
  17183. (message "Failed to create png file from %s" texfile)
  17184. nil)
  17185. ;; Use the requested file name and clean up
  17186. (copy-file pngfile tofile 'replace)
  17187. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17188. (if (file-exists-p (concat texfilebase e))
  17189. (delete-file (concat texfilebase e))))
  17190. pngfile))))
  17191. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17192. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17193. "This calls convert, which is included into imagemagick."
  17194. (require 'ox-latex)
  17195. (let* ((tmpdir (if (featurep 'xemacs)
  17196. (temp-directory)
  17197. temporary-file-directory))
  17198. (texfilebase (make-temp-name
  17199. (expand-file-name "orgtex" tmpdir)))
  17200. (texfile (concat texfilebase ".tex"))
  17201. (pdffile (concat texfilebase ".pdf"))
  17202. (pngfile (concat texfilebase ".png"))
  17203. (fnh (if (featurep 'xemacs)
  17204. (font-height (face-font 'default))
  17205. (face-attribute 'default :height nil)))
  17206. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17207. (dpi (number-to-string (* scale (floor (if buffer fnh 120.)))))
  17208. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17209. "black"))
  17210. (bg (or (plist-get options (if buffer :background :html-background))
  17211. "white")))
  17212. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17213. (setq fg (org-latex-color-format fg)))
  17214. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17215. (setq bg (org-latex-color-format
  17216. (if (string= bg "Transparent") "white" bg))))
  17217. (let ((latex-header (org-create-formula--latex-header)))
  17218. (with-temp-file texfile
  17219. (insert latex-header)
  17220. (insert "\n\\begin{document}\n"
  17221. "\\definecolor{fg}{rgb}{" fg "}\n"
  17222. "\\definecolor{bg}{rgb}{" bg "}\n"
  17223. "\n\\pagecolor{bg}\n"
  17224. "\n{\\color{fg}\n"
  17225. string
  17226. "\n}\n"
  17227. "\n\\end{document}\n")))
  17228. (org-latex-compile texfile t)
  17229. (if (not (file-exists-p pdffile))
  17230. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17231. (condition-case nil
  17232. (if (featurep 'xemacs)
  17233. (call-process "convert" nil nil nil
  17234. "-density" "96"
  17235. "-trim"
  17236. "-antialias"
  17237. pdffile
  17238. "-quality" "100"
  17239. ;; "-sharpen" "0x1.0"
  17240. pngfile)
  17241. (call-process "convert" nil nil nil
  17242. "-density" dpi
  17243. "-trim"
  17244. "-antialias"
  17245. pdffile
  17246. "-quality" "100"
  17247. ;; "-sharpen" "0x1.0"
  17248. pngfile))
  17249. (error nil))
  17250. (if (not (file-exists-p pngfile))
  17251. (if org-format-latex-signal-error
  17252. (error "Failed to create png file from %s" texfile)
  17253. (message "Failed to create png file from %s" texfile)
  17254. nil)
  17255. ;; Use the requested file name and clean up
  17256. (copy-file pngfile tofile 'replace)
  17257. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17258. (if (file-exists-p (concat texfilebase e))
  17259. (delete-file (concat texfilebase e))))
  17260. pngfile))))
  17261. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17262. "Fill a LaTeX header template TPL.
  17263. In the template, the following place holders will be recognized:
  17264. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17265. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17266. [PACKAGES] \\usepackage statements for PKG
  17267. [NO-PACKAGES] do not include PKG
  17268. [EXTRA] the string EXTRA
  17269. [NO-EXTRA] do not include EXTRA
  17270. For backward compatibility, if both the positive and the negative place
  17271. holder is missing, the positive one (without the \"NO-\") will be
  17272. assumed to be present at the end of the template.
  17273. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17274. EXTRA is a string.
  17275. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17276. (let (rpl (end ""))
  17277. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17278. (setq rpl (if (or (match-end 1) (not def-pkg))
  17279. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17280. tpl (replace-match rpl t t tpl))
  17281. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17282. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17283. (setq rpl (if (or (match-end 1) (not pkg))
  17284. "" (org-latex-packages-to-string pkg snippets-p t))
  17285. tpl (replace-match rpl t t tpl))
  17286. (if pkg (setq end
  17287. (concat end "\n"
  17288. (org-latex-packages-to-string pkg snippets-p)))))
  17289. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17290. (setq rpl (if (or (match-end 1) (not extra))
  17291. "" (concat extra "\n"))
  17292. tpl (replace-match rpl t t tpl))
  17293. (if (and extra (string-match "\\S-" extra))
  17294. (setq end (concat end "\n" extra))))
  17295. (if (string-match "\\S-" end)
  17296. (concat tpl "\n" end)
  17297. tpl)))
  17298. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17299. "Turn an alist of packages into a string with the \\usepackage macros."
  17300. (setq pkg (mapconcat (lambda(p)
  17301. (cond
  17302. ((stringp p) p)
  17303. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17304. (format "%% Package %s omitted" (cadr p)))
  17305. ((equal "" (car p))
  17306. (format "\\usepackage{%s}" (cadr p)))
  17307. (t
  17308. (format "\\usepackage[%s]{%s}"
  17309. (car p) (cadr p)))))
  17310. pkg
  17311. "\n"))
  17312. (if newline (concat pkg "\n") pkg))
  17313. (defun org-dvipng-color (attr)
  17314. "Return a RGB color specification for dvipng."
  17315. (apply 'format "rgb %s %s %s"
  17316. (mapcar 'org-normalize-color
  17317. (if (featurep 'xemacs)
  17318. (color-rgb-components
  17319. (face-property 'default
  17320. (cond ((eq attr :foreground) 'foreground)
  17321. ((eq attr :background) 'background))))
  17322. (color-values (face-attribute 'default attr nil))))))
  17323. (defun org-dvipng-color-format (color-name)
  17324. "Convert COLOR-NAME to a RGB color value for dvipng."
  17325. (apply 'format "rgb %s %s %s"
  17326. (mapcar 'org-normalize-color
  17327. (color-values color-name))))
  17328. (defun org-latex-color (attr)
  17329. "Return a RGB color for the LaTeX color package."
  17330. (apply 'format "%s,%s,%s"
  17331. (mapcar 'org-normalize-color
  17332. (if (featurep 'xemacs)
  17333. (color-rgb-components
  17334. (face-property 'default
  17335. (cond ((eq attr :foreground) 'foreground)
  17336. ((eq attr :background) 'background))))
  17337. (color-values (face-attribute 'default attr nil))))))
  17338. (defun org-latex-color-format (color-name)
  17339. "Convert COLOR-NAME to a RGB color value."
  17340. (apply 'format "%s,%s,%s"
  17341. (mapcar 'org-normalize-color
  17342. (color-values color-name))))
  17343. (defun org-normalize-color (value)
  17344. "Return string to be used as color value for an RGB component."
  17345. (format "%g" (/ value 65535.0)))
  17346. ;; Image display
  17347. (defvar org-inline-image-overlays nil)
  17348. (make-variable-buffer-local 'org-inline-image-overlays)
  17349. (defun org-toggle-inline-images (&optional include-linked)
  17350. "Toggle the display of inline images.
  17351. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17352. (interactive "P")
  17353. (if org-inline-image-overlays
  17354. (progn
  17355. (org-remove-inline-images)
  17356. (message "Inline image display turned off"))
  17357. (org-display-inline-images include-linked)
  17358. (if (and (org-called-interactively-p)
  17359. org-inline-image-overlays)
  17360. (message "%d images displayed inline"
  17361. (length org-inline-image-overlays))
  17362. (message "No images to display inline"))))
  17363. (defun org-redisplay-inline-images ()
  17364. "Refresh the display of inline images."
  17365. (interactive)
  17366. (if (not org-inline-image-overlays)
  17367. (org-toggle-inline-images)
  17368. (org-toggle-inline-images)
  17369. (org-toggle-inline-images)))
  17370. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17371. "Display inline images.
  17372. An inline image is a link which follows either of these
  17373. conventions:
  17374. 1. Its path is a file with an extension matching return value
  17375. from `image-file-name-regexp' and it has no contents.
  17376. 2. Its description consists in a single link of the previous
  17377. type.
  17378. When optional argument INCLUDE-LINKED is non-nil, also links with
  17379. a text description part will be inlined. This can be nice for
  17380. a quick look at those images, but it does not reflect what
  17381. exported files will look like.
  17382. When optional argument REFRESH is non-nil, refresh existing
  17383. images between BEG and END. This will create new image displays
  17384. only if necessary. BEG and END default to the buffer
  17385. boundaries."
  17386. (interactive "P")
  17387. (when (display-graphic-p)
  17388. (unless refresh
  17389. (org-remove-inline-images)
  17390. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17391. (org-with-wide-buffer
  17392. (goto-char (or beg (point-min)))
  17393. (let ((case-fold-search t)
  17394. (file-extension-re (org-image-file-name-regexp)))
  17395. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17396. (let ((link (save-match-data (org-element-context))))
  17397. ;; Check if we're at an inline image.
  17398. (when (and (equal (org-element-property :type link) "file")
  17399. (or include-linked
  17400. (not (org-element-property :contents-begin link)))
  17401. (let ((parent (org-element-property :parent link)))
  17402. (or (not (eq (org-element-type parent) 'link))
  17403. (not (cdr (org-element-contents parent)))))
  17404. (org-string-match-p file-extension-re
  17405. (org-element-property :path link)))
  17406. (let ((file (expand-file-name (org-element-property :path link))))
  17407. (when (file-exists-p file)
  17408. (let ((width
  17409. ;; Apply `org-image-actual-width' specifications.
  17410. (cond
  17411. ((not (image-type-available-p 'imagemagick)) nil)
  17412. ((eq org-image-actual-width t) nil)
  17413. ((listp org-image-actual-width)
  17414. (or
  17415. ;; First try to find a width among
  17416. ;; attributes associated to the paragraph
  17417. ;; containing link.
  17418. (let ((paragraph
  17419. (let ((e link))
  17420. (while (and (setq e (org-element-property
  17421. :parent e))
  17422. (not (eq (org-element-type e)
  17423. 'paragraph))))
  17424. e)))
  17425. (when paragraph
  17426. (save-excursion
  17427. (goto-char (org-element-property :begin paragraph))
  17428. (when (save-match-data
  17429. (re-search-forward
  17430. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17431. (org-element-property
  17432. :post-affiliated paragraph)
  17433. t))
  17434. (string-to-number (match-string 1))))))
  17435. ;; Otherwise, fall-back to provided number.
  17436. (car org-image-actual-width)))
  17437. ((numberp org-image-actual-width)
  17438. org-image-actual-width)))
  17439. (old (get-char-property-and-overlay
  17440. (org-element-property :begin link)
  17441. 'org-image-overlay)))
  17442. (if (and (car-safe old) refresh)
  17443. (image-refresh (overlay-get (cdr old) 'display))
  17444. (let ((image (save-match-data
  17445. (create-image file
  17446. (and width 'imagemagick)
  17447. nil
  17448. :width width))))
  17449. (when image
  17450. (let* ((link
  17451. ;; If inline image is the description
  17452. ;; of another link, be sure to
  17453. ;; consider the latter as the one to
  17454. ;; apply the overlay on.
  17455. (let ((parent
  17456. (org-element-property :parent link)))
  17457. (if (eq (org-element-type parent) 'link)
  17458. parent
  17459. link)))
  17460. (ov (make-overlay
  17461. (org-element-property :begin link)
  17462. (progn
  17463. (goto-char
  17464. (org-element-property :end link))
  17465. (skip-chars-backward " \t")
  17466. (point)))))
  17467. (overlay-put ov 'display image)
  17468. (overlay-put ov 'face 'default)
  17469. (overlay-put ov 'org-image-overlay t)
  17470. (overlay-put
  17471. ov 'modification-hooks
  17472. (list 'org-display-inline-remove-overlay))
  17473. (push ov org-inline-image-overlays)))))))))))))))
  17474. (define-obsolete-function-alias
  17475. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17476. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17477. "Remove inline-display overlay if a corresponding region is modified."
  17478. (let ((inhibit-modification-hooks t))
  17479. (when (and ov after)
  17480. (delete ov org-inline-image-overlays)
  17481. (delete-overlay ov))))
  17482. (defun org-remove-inline-images ()
  17483. "Remove inline display of images."
  17484. (interactive)
  17485. (mapc 'delete-overlay org-inline-image-overlays)
  17486. (setq org-inline-image-overlays nil))
  17487. ;;;; Key bindings
  17488. ;; Outline functions from `outline-mode-prefix-map'
  17489. ;; that can be remapped in Org:
  17490. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17491. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17492. (define-key org-mode-map [remap outline-forward-same-level]
  17493. 'org-forward-heading-same-level)
  17494. (define-key org-mode-map [remap outline-backward-same-level]
  17495. 'org-backward-heading-same-level)
  17496. (define-key org-mode-map [remap show-branches]
  17497. 'org-kill-note-or-show-branches)
  17498. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17499. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17500. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17501. ;; Outline functions from `outline-mode-prefix-map' that can not
  17502. ;; be remapped in Org:
  17503. ;;
  17504. ;; - the column "key binding" shows whether the Outline function is still
  17505. ;; available in Org mode on the same key that it has been bound to in
  17506. ;; Outline mode:
  17507. ;; - "overridden": key used for a different functionality in Org mode
  17508. ;; - else: key still bound to the same Outline function in Org mode
  17509. ;;
  17510. ;; | Outline function | key binding | Org replacement |
  17511. ;; |------------------------------------+-------------+-----------------------|
  17512. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17513. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17514. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17515. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17516. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17517. ;; | `show-entry' | overridden | no replacement |
  17518. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17519. ;; | `show-branches' | `C-c C-k' | still same function |
  17520. ;; | `show-subtree' | overridden | visibility cycling |
  17521. ;; | `show-all' | overridden | no replacement |
  17522. ;; | `hide-subtree' | overridden | visibility cycling |
  17523. ;; | `hide-body' | overridden | no replacement |
  17524. ;; | `hide-entry' | overridden | visibility cycling |
  17525. ;; | `hide-leaves' | overridden | no replacement |
  17526. ;; | `hide-sublevels' | overridden | no replacement |
  17527. ;; | `hide-other' | overridden | no replacement |
  17528. ;; Make `C-c C-x' a prefix key
  17529. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17530. ;; TAB key with modifiers
  17531. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17532. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17533. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17534. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17535. ;; The following line is necessary under Suse GNU/Linux
  17536. (unless (featurep 'xemacs)
  17537. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17538. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17539. (define-key org-mode-map [backtab] 'org-shifttab)
  17540. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17541. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17542. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17543. ;; Cursor keys with modifiers
  17544. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17545. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17546. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17547. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17548. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17549. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17550. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17551. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17552. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17553. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17554. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17555. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17556. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17557. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17558. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17559. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17560. ;; Babel keys
  17561. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17562. (mapc (lambda (pair)
  17563. (define-key org-babel-map (car pair) (cdr pair)))
  17564. org-babel-key-bindings)
  17565. ;;; Extra keys for tty access.
  17566. ;; We only set them when really needed because otherwise the
  17567. ;; menus don't show the simple keys
  17568. (when (or org-use-extra-keys
  17569. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17570. (not window-system))
  17571. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17572. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17573. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17574. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17575. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17576. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17577. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17578. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17579. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17580. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17581. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17582. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17583. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17584. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17585. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17586. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17587. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17588. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17589. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17590. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17591. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17592. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17593. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17594. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17595. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17596. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17597. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17598. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17599. ;; All the other keys
  17600. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17601. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17602. (if (boundp 'narrow-map)
  17603. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17604. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17605. (if (boundp 'narrow-map)
  17606. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17607. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17608. (if (boundp 'narrow-map)
  17609. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17610. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17611. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17612. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17613. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17614. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17615. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17616. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17617. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17618. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17619. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17620. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17621. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17622. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17623. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17624. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17625. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17626. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17627. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17628. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17629. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17630. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17631. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17632. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17633. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17634. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17635. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17636. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17637. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17638. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17639. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17640. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17641. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17642. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17643. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17644. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17645. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17646. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17647. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  17648. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17649. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17650. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17651. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17652. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17653. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17654. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17655. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17656. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17657. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17658. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17659. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17660. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17661. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17662. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17663. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17664. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17665. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17666. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17667. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17668. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17669. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17670. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17671. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17672. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  17673. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  17674. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  17675. (org-defkey org-mode-map "\C-m" 'org-return)
  17676. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17677. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17678. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17679. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17680. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17681. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17682. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17683. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17684. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17685. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17686. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17687. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17688. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17689. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17690. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  17691. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17692. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17693. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17694. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17695. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17696. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17697. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17698. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17699. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17700. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17701. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17702. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17703. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17704. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17705. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17706. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17707. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17708. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17709. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17710. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17711. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17712. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17713. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17714. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17715. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17716. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17717. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17718. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17719. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17720. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17721. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17722. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17723. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17724. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17725. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17726. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17727. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17728. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17729. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17730. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17731. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17732. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17733. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17734. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17735. (when (featurep 'xemacs)
  17736. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17737. (defconst org-speed-commands-default
  17738. '(
  17739. ("Outline Navigation")
  17740. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17741. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17742. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17743. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17744. ("F" . org-next-block)
  17745. ("B" . org-previous-block)
  17746. ("u" . (org-speed-move-safe 'outline-up-heading))
  17747. ("j" . org-goto)
  17748. ("g" . (org-refile t))
  17749. ("Outline Visibility")
  17750. ("c" . org-cycle)
  17751. ("C" . org-shifttab)
  17752. (" " . org-display-outline-path)
  17753. ("s" . org-narrow-to-subtree)
  17754. ("=" . org-columns)
  17755. ("Outline Structure Editing")
  17756. ("U" . org-shiftmetaup)
  17757. ("D" . org-shiftmetadown)
  17758. ("r" . org-metaright)
  17759. ("l" . org-metaleft)
  17760. ("R" . org-shiftmetaright)
  17761. ("L" . org-shiftmetaleft)
  17762. ("i" . (progn (forward-char 1) (call-interactively
  17763. 'org-insert-heading-respect-content)))
  17764. ("^" . org-sort)
  17765. ("w" . org-refile)
  17766. ("a" . org-archive-subtree-default-with-confirmation)
  17767. ("@" . org-mark-subtree)
  17768. ("#" . org-toggle-comment)
  17769. ("Clock Commands")
  17770. ("I" . org-clock-in)
  17771. ("O" . org-clock-out)
  17772. ("Meta Data Editing")
  17773. ("t" . org-todo)
  17774. ("," . (org-priority))
  17775. ("0" . (org-priority ?\ ))
  17776. ("1" . (org-priority ?A))
  17777. ("2" . (org-priority ?B))
  17778. ("3" . (org-priority ?C))
  17779. (":" . org-set-tags-command)
  17780. ("e" . org-set-effort)
  17781. ("E" . org-inc-effort)
  17782. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17783. (org-entry-put (point) "APPT_WARNTIME" m)))
  17784. ("Agenda Views etc")
  17785. ("v" . org-agenda)
  17786. ("/" . org-sparse-tree)
  17787. ("Misc")
  17788. ("o" . org-open-at-point)
  17789. ("?" . org-speed-command-help)
  17790. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17791. (">" . (org-agenda-remove-restriction-lock))
  17792. )
  17793. "The default speed commands.")
  17794. (defun org-print-speed-command (e)
  17795. (if (> (length (car e)) 1)
  17796. (progn
  17797. (princ "\n")
  17798. (princ (car e))
  17799. (princ "\n")
  17800. (princ (make-string (length (car e)) ?-))
  17801. (princ "\n"))
  17802. (princ (car e))
  17803. (princ " ")
  17804. (if (symbolp (cdr e))
  17805. (princ (symbol-name (cdr e)))
  17806. (prin1 (cdr e)))
  17807. (princ "\n")))
  17808. (defun org-speed-command-help ()
  17809. "Show the available speed commands."
  17810. (interactive)
  17811. (if (not org-use-speed-commands)
  17812. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17813. (with-output-to-temp-buffer "*Help*"
  17814. (princ "User-defined Speed commands\n===========================\n")
  17815. (mapc 'org-print-speed-command org-speed-commands-user)
  17816. (princ "\n")
  17817. (princ "Built-in Speed commands\n=======================\n")
  17818. (mapc 'org-print-speed-command org-speed-commands-default))
  17819. (with-current-buffer "*Help*"
  17820. (setq truncate-lines t))))
  17821. (defun org-speed-move-safe (cmd)
  17822. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17823. If not, return to the original position and throw an error."
  17824. (interactive)
  17825. (let ((pos (point)))
  17826. (call-interactively cmd)
  17827. (unless (and (bolp) (org-at-heading-p))
  17828. (goto-char pos)
  17829. (error "Boundary reached while executing %s" cmd))))
  17830. (defvar org-self-insert-command-undo-counter 0)
  17831. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17832. (defvar org-speed-command nil)
  17833. (define-obsolete-function-alias
  17834. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17835. (defun org-speed-command-activate (keys)
  17836. "Hook for activating single-letter speed commands.
  17837. `org-speed-commands-default' specifies a minimal command set.
  17838. Use `org-speed-commands-user' for further customization."
  17839. (when (or (and (bolp) (looking-at org-outline-regexp))
  17840. (and (functionp org-use-speed-commands)
  17841. (funcall org-use-speed-commands)))
  17842. (cdr (assoc keys (append org-speed-commands-user
  17843. org-speed-commands-default)))))
  17844. (define-obsolete-function-alias
  17845. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17846. (defun org-babel-speed-command-activate (keys)
  17847. "Hook for activating single-letter code block commands."
  17848. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17849. (cdr (assoc keys org-babel-key-bindings))))
  17850. (defcustom org-speed-command-hook
  17851. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17852. "Hook for activating speed commands at strategic locations.
  17853. Hook functions are called in sequence until a valid handler is
  17854. found.
  17855. Each hook takes a single argument, a user-pressed command key
  17856. which is also a `self-insert-command' from the global map.
  17857. Within the hook, examine the cursor position and the command key
  17858. and return nil or a valid handler as appropriate. Handler could
  17859. be one of an interactive command, a function, or a form.
  17860. Set `org-use-speed-commands' to non-nil value to enable this
  17861. hook. The default setting is `org-speed-command-activate'."
  17862. :group 'org-structure
  17863. :version "24.1"
  17864. :type 'hook)
  17865. (defun org-self-insert-command (N)
  17866. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17867. If the cursor is in a table looking at whitespace, the whitespace is
  17868. overwritten, and the table is not marked as requiring realignment."
  17869. (interactive "p")
  17870. (org-check-before-invisible-edit 'insert)
  17871. (cond
  17872. ((and org-use-speed-commands
  17873. (setq org-speed-command
  17874. (run-hook-with-args-until-success
  17875. 'org-speed-command-hook (this-command-keys))))
  17876. (cond
  17877. ((commandp org-speed-command)
  17878. (setq this-command org-speed-command)
  17879. (call-interactively org-speed-command))
  17880. ((functionp org-speed-command)
  17881. (funcall org-speed-command))
  17882. ((and org-speed-command (listp org-speed-command))
  17883. (eval org-speed-command))
  17884. (t (let (org-use-speed-commands)
  17885. (call-interactively 'org-self-insert-command)))))
  17886. ((and
  17887. (org-table-p)
  17888. (progn
  17889. ;; check if we blank the field, and if that triggers align
  17890. (and (featurep 'org-table) org-table-auto-blank-field
  17891. (member last-command
  17892. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17893. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17894. ;; got extra space, this field does not determine column width
  17895. (let (org-table-may-need-update) (org-table-blank-field))
  17896. ;; no extra space, this field may determine column width
  17897. (org-table-blank-field)))
  17898. t)
  17899. (eq N 1)
  17900. (looking-at "[^|\n]* |"))
  17901. (let (org-table-may-need-update)
  17902. (goto-char (1- (match-end 0)))
  17903. (backward-delete-char 1)
  17904. (goto-char (match-beginning 0))
  17905. (self-insert-command N)))
  17906. (t
  17907. (setq org-table-may-need-update t)
  17908. (self-insert-command N)
  17909. (org-fix-tags-on-the-fly)
  17910. (if org-self-insert-cluster-for-undo
  17911. (if (not (eq last-command 'org-self-insert-command))
  17912. (setq org-self-insert-command-undo-counter 1)
  17913. (if (>= org-self-insert-command-undo-counter 20)
  17914. (setq org-self-insert-command-undo-counter 1)
  17915. (and (> org-self-insert-command-undo-counter 0)
  17916. buffer-undo-list (listp buffer-undo-list)
  17917. (not (cadr buffer-undo-list)) ; remove nil entry
  17918. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17919. (setq org-self-insert-command-undo-counter
  17920. (1+ org-self-insert-command-undo-counter))))))))
  17921. (defun org-check-before-invisible-edit (kind)
  17922. "Check is editing if kind KIND would be dangerous with invisible text around.
  17923. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17924. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17925. (if (and org-catch-invisible-edits
  17926. (or (not (boundp 'visible-mode)) (not visible-mode))
  17927. (or (get-char-property (point) 'invisible)
  17928. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17929. ;; OK, we need to take a closer look
  17930. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17931. (invisible-before-point (if (bobp) nil (get-char-property
  17932. (1- (point)) 'invisible)))
  17933. (border-and-ok-direction
  17934. (or
  17935. ;; Check if we are acting predictably before invisible text
  17936. (and invisible-at-point (not invisible-before-point)
  17937. (memq kind '(insert delete-backward)))
  17938. ;; Check if we are acting predictably after invisible text
  17939. ;; This works not well, and I have turned it off. It seems
  17940. ;; better to always show and stop after invisible text.
  17941. ;; (and (not invisible-at-point) invisible-before-point
  17942. ;; (memq kind '(insert delete)))
  17943. )))
  17944. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17945. (memq invisible-before-point '(outline org-hide-block t)))
  17946. (if (eq org-catch-invisible-edits 'error)
  17947. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17948. (if (and org-custom-properties-overlays
  17949. (y-or-n-p "Display invisible properties in this buffer? "))
  17950. (org-toggle-custom-properties-visibility)
  17951. ;; Make the area visible
  17952. (save-excursion
  17953. (if invisible-before-point
  17954. (goto-char (previous-single-char-property-change
  17955. (point) 'invisible)))
  17956. (org-cycle))
  17957. (cond
  17958. ((eq org-catch-invisible-edits 'show)
  17959. ;; That's it, we do the edit after showing
  17960. (message
  17961. "Unfolding invisible region around point before editing")
  17962. (sit-for 1))
  17963. ((and (eq org-catch-invisible-edits 'smart)
  17964. border-and-ok-direction)
  17965. (message "Unfolding invisible region around point before editing"))
  17966. (t
  17967. ;; Don't do the edit, make the user repeat it in full visibility
  17968. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17969. (defun org-fix-tags-on-the-fly ()
  17970. (when (and (equal (char-after (point-at-bol)) ?*)
  17971. (org-at-heading-p))
  17972. (org-align-tags-here org-tags-column)))
  17973. (defun org-delete-backward-char (N)
  17974. "Like `delete-backward-char', insert whitespace at field end in tables.
  17975. When deleting backwards, in tables this function will insert whitespace in
  17976. front of the next \"|\" separator, to keep the table aligned. The table will
  17977. still be marked for re-alignment if the field did fill the entire column,
  17978. because, in this case the deletion might narrow the column."
  17979. (interactive "p")
  17980. (save-match-data
  17981. (org-check-before-invisible-edit 'delete-backward)
  17982. (if (and (org-table-p)
  17983. (eq N 1)
  17984. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17985. (looking-at ".*?|"))
  17986. (let ((pos (point))
  17987. (noalign (looking-at "[^|\n\r]* |"))
  17988. (c org-table-may-need-update))
  17989. (backward-delete-char N)
  17990. (if (not overwrite-mode)
  17991. (progn
  17992. (skip-chars-forward "^|")
  17993. (insert " ")
  17994. (goto-char (1- pos))))
  17995. ;; noalign: if there were two spaces at the end, this field
  17996. ;; does not determine the width of the column.
  17997. (if noalign (setq org-table-may-need-update c)))
  17998. (backward-delete-char N)
  17999. (org-fix-tags-on-the-fly))))
  18000. (defun org-delete-char (N)
  18001. "Like `delete-char', but insert whitespace at field end in tables.
  18002. When deleting characters, in tables this function will insert whitespace in
  18003. front of the next \"|\" separator, to keep the table aligned. The table will
  18004. still be marked for re-alignment if the field did fill the entire column,
  18005. because, in this case the deletion might narrow the column."
  18006. (interactive "p")
  18007. (save-match-data
  18008. (org-check-before-invisible-edit 'delete)
  18009. (if (and (org-table-p)
  18010. (not (bolp))
  18011. (not (= (char-after) ?|))
  18012. (eq N 1))
  18013. (if (looking-at ".*?|")
  18014. (let ((pos (point))
  18015. (noalign (looking-at "[^|\n\r]* |"))
  18016. (c org-table-may-need-update))
  18017. (replace-match
  18018. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18019. (goto-char pos)
  18020. ;; noalign: if there were two spaces at the end, this field
  18021. ;; does not determine the width of the column.
  18022. (if noalign (setq org-table-may-need-update c)))
  18023. (delete-char N))
  18024. (delete-char N)
  18025. (org-fix-tags-on-the-fly))))
  18026. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18027. (put 'org-self-insert-command 'delete-selection
  18028. (lambda ()
  18029. (not (run-hook-with-args-until-success
  18030. 'self-insert-uses-region-functions))))
  18031. (put 'orgtbl-self-insert-command 'delete-selection
  18032. (lambda ()
  18033. (not (run-hook-with-args-until-success
  18034. 'self-insert-uses-region-functions))))
  18035. (put 'org-delete-char 'delete-selection 'supersede)
  18036. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18037. (put 'org-yank 'delete-selection 'yank)
  18038. ;; Make `flyspell-mode' delay after some commands
  18039. (put 'org-self-insert-command 'flyspell-delayed t)
  18040. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18041. (put 'org-delete-char 'flyspell-delayed t)
  18042. (put 'org-delete-backward-char 'flyspell-delayed t)
  18043. ;; Make pabbrev-mode expand after org-mode commands
  18044. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18045. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18046. (defun org-remap (map &rest commands)
  18047. "In MAP, remap the functions given in COMMANDS.
  18048. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18049. (let (new old)
  18050. (while commands
  18051. (setq old (pop commands) new (pop commands))
  18052. (if (fboundp 'command-remapping)
  18053. (org-defkey map (vector 'remap old) new)
  18054. (substitute-key-definition old new map global-map)))))
  18055. (defun org-transpose-words ()
  18056. "Transpose words for Org.
  18057. This uses the `org-mode-transpose-word-syntax-table' syntax
  18058. table, which interprets characters in `org-emphasis-alist' as
  18059. word constituents."
  18060. (interactive)
  18061. (with-syntax-table org-mode-transpose-word-syntax-table
  18062. (call-interactively 'transpose-words)))
  18063. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18064. (when (eq org-enable-table-editor 'optimized)
  18065. ;; If the user wants maximum table support, we need to hijack
  18066. ;; some standard editing functions
  18067. (org-remap org-mode-map
  18068. 'self-insert-command 'org-self-insert-command
  18069. 'delete-char 'org-delete-char
  18070. 'delete-backward-char 'org-delete-backward-char)
  18071. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18072. (defvar org-ctrl-c-ctrl-c-hook nil
  18073. "Hook for functions attaching themselves to `C-c C-c'.
  18074. This can be used to add additional functionality to the C-c C-c
  18075. key which executes context-dependent commands. This hook is run
  18076. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18077. run after the last test.
  18078. Each function will be called with no arguments. The function
  18079. must check if the context is appropriate for it to act. If yes,
  18080. it should do its thing and then return a non-nil value. If the
  18081. context is wrong, just do nothing and return nil.")
  18082. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18083. "Hook for functions attaching themselves to `C-c C-c'.
  18084. This can be used to add additional functionality to the C-c C-c
  18085. key which executes context-dependent commands. This hook is run
  18086. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18087. before the first test.
  18088. Each function will be called with no arguments. The function
  18089. must check if the context is appropriate for it to act. If yes,
  18090. it should do its thing and then return a non-nil value. If the
  18091. context is wrong, just do nothing and return nil.")
  18092. (defvar org-tab-first-hook nil
  18093. "Hook for functions to attach themselves to TAB.
  18094. See `org-ctrl-c-ctrl-c-hook' for more information.
  18095. This hook runs as the first action when TAB is pressed, even before
  18096. `org-cycle' messes around with the `outline-regexp' to cater for
  18097. inline tasks and plain list item folding.
  18098. If any function in this hook returns t, any other actions that
  18099. would have been caused by TAB (such as table field motion or visibility
  18100. cycling) will not occur.")
  18101. (defvar org-tab-after-check-for-table-hook nil
  18102. "Hook for functions to attach themselves to TAB.
  18103. See `org-ctrl-c-ctrl-c-hook' for more information.
  18104. This hook runs after it has been established that the cursor is not in a
  18105. table, but before checking if the cursor is in a headline or if global cycling
  18106. should be done.
  18107. If any function in this hook returns t, not other actions like visibility
  18108. cycling will be done.")
  18109. (defvar org-tab-after-check-for-cycling-hook nil
  18110. "Hook for functions to attach themselves to TAB.
  18111. See `org-ctrl-c-ctrl-c-hook' for more information.
  18112. This hook runs after it has been established that not table field motion and
  18113. not visibility should be done because of current context. This is probably
  18114. the place where a package like yasnippets can hook in.")
  18115. (defvar org-tab-before-tab-emulation-hook nil
  18116. "Hook for functions to attach themselves to TAB.
  18117. See `org-ctrl-c-ctrl-c-hook' for more information.
  18118. This hook runs after every other options for TAB have been exhausted, but
  18119. before indentation and \t insertion takes place.")
  18120. (defvar org-metaleft-hook nil
  18121. "Hook for functions attaching themselves to `M-left'.
  18122. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18123. (defvar org-metaright-hook nil
  18124. "Hook for functions attaching themselves to `M-right'.
  18125. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18126. (defvar org-metaup-hook nil
  18127. "Hook for functions attaching themselves to `M-up'.
  18128. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18129. (defvar org-metadown-hook nil
  18130. "Hook for functions attaching themselves to `M-down'.
  18131. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18132. (defvar org-shiftmetaleft-hook nil
  18133. "Hook for functions attaching themselves to `M-S-left'.
  18134. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18135. (defvar org-shiftmetaright-hook nil
  18136. "Hook for functions attaching themselves to `M-S-right'.
  18137. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18138. (defvar org-shiftmetaup-hook nil
  18139. "Hook for functions attaching themselves to `M-S-up'.
  18140. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18141. (defvar org-shiftmetadown-hook nil
  18142. "Hook for functions attaching themselves to `M-S-down'.
  18143. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18144. (defvar org-metareturn-hook nil
  18145. "Hook for functions attaching themselves to `M-RET'.
  18146. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18147. (defvar org-shiftup-hook nil
  18148. "Hook for functions attaching themselves to `S-up'.
  18149. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18150. (defvar org-shiftup-final-hook nil
  18151. "Hook for functions attaching themselves to `S-up'.
  18152. This one runs after all other options except shift-select have been excluded.
  18153. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18154. (defvar org-shiftdown-hook nil
  18155. "Hook for functions attaching themselves to `S-down'.
  18156. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18157. (defvar org-shiftdown-final-hook nil
  18158. "Hook for functions attaching themselves to `S-down'.
  18159. This one runs after all other options except shift-select have been excluded.
  18160. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18161. (defvar org-shiftleft-hook nil
  18162. "Hook for functions attaching themselves to `S-left'.
  18163. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18164. (defvar org-shiftleft-final-hook nil
  18165. "Hook for functions attaching themselves to `S-left'.
  18166. This one runs after all other options except shift-select have been excluded.
  18167. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18168. (defvar org-shiftright-hook nil
  18169. "Hook for functions attaching themselves to `S-right'.
  18170. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18171. (defvar org-shiftright-final-hook nil
  18172. "Hook for functions attaching themselves to `S-right'.
  18173. This one runs after all other options except shift-select have been excluded.
  18174. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18175. (defun org-modifier-cursor-error ()
  18176. "Throw an error, a modified cursor command was applied in wrong context."
  18177. (user-error "This command is active in special context like tables, headlines or items"))
  18178. (defun org-shiftselect-error ()
  18179. "Throw an error because Shift-Cursor command was applied in wrong context."
  18180. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18181. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18182. (user-error "This command works only in special context like headlines or timestamps")))
  18183. (defun org-call-for-shift-select (cmd)
  18184. (let ((this-command-keys-shift-translated t))
  18185. (call-interactively cmd)))
  18186. (defun org-shifttab (&optional arg)
  18187. "Global visibility cycling or move to previous table field.
  18188. Call `org-table-previous-field' within a table.
  18189. When ARG is nil, cycle globally through visibility states.
  18190. When ARG is a numeric prefix, show contents of this level."
  18191. (interactive "P")
  18192. (cond
  18193. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18194. ((integerp arg)
  18195. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18196. (message "Content view to level: %d" arg)
  18197. (org-content (prefix-numeric-value arg2))
  18198. (org-cycle-show-empty-lines t)
  18199. (setq org-cycle-global-status 'overview)))
  18200. (t (call-interactively 'org-global-cycle))))
  18201. (defun org-shiftmetaleft ()
  18202. "Promote subtree or delete table column.
  18203. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18204. `org-table-delete-column', depending on context. See the
  18205. individual commands for more information."
  18206. (interactive)
  18207. (cond
  18208. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18209. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18210. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18211. ((if (not (org-region-active-p)) (org-at-item-p)
  18212. (save-excursion (goto-char (region-beginning))
  18213. (org-at-item-p)))
  18214. (call-interactively 'org-outdent-item-tree))
  18215. (t (org-modifier-cursor-error))))
  18216. (defun org-shiftmetaright ()
  18217. "Demote subtree or insert table column.
  18218. Calls `org-demote-subtree', `org-indent-item-tree', or
  18219. `org-table-insert-column', depending on context. See the
  18220. individual commands for more information."
  18221. (interactive)
  18222. (cond
  18223. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18224. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18225. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18226. ((if (not (org-region-active-p)) (org-at-item-p)
  18227. (save-excursion (goto-char (region-beginning))
  18228. (org-at-item-p)))
  18229. (call-interactively 'org-indent-item-tree))
  18230. (t (org-modifier-cursor-error))))
  18231. (defun org-shiftmetaup (&optional arg)
  18232. "Move subtree up or kill table row.
  18233. Calls `org-move-subtree-up' or `org-table-kill-row' or
  18234. `org-move-item-up' or `org-timestamp-up', depending on context.
  18235. See the individual commands for more information."
  18236. (interactive "P")
  18237. (cond
  18238. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18239. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18240. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18241. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18242. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18243. (call-interactively 'org-timestamp-up)))
  18244. (t (call-interactively 'org-drag-line-backward))))
  18245. (defun org-shiftmetadown (&optional arg)
  18246. "Move subtree down or insert table row.
  18247. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18248. `org-move-item-down' or `org-timestamp-up', depending on context.
  18249. See the individual commands for more information."
  18250. (interactive "P")
  18251. (cond
  18252. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18253. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18254. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18255. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18256. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18257. (call-interactively 'org-timestamp-down)))
  18258. (t (call-interactively 'org-drag-line-forward))))
  18259. (defsubst org-hidden-tree-error ()
  18260. (user-error
  18261. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18262. (defun org-metaleft (&optional arg)
  18263. "Promote heading or move table column to left.
  18264. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18265. With no specific context, calls the Emacs default `backward-word'.
  18266. See the individual commands for more information."
  18267. (interactive "P")
  18268. (cond
  18269. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18270. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18271. ((org-with-limited-levels
  18272. (or (org-at-heading-p)
  18273. (and (org-region-active-p)
  18274. (save-excursion
  18275. (goto-char (region-beginning))
  18276. (org-at-heading-p)))))
  18277. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18278. (call-interactively 'org-do-promote))
  18279. ;; At an inline task.
  18280. ((org-at-heading-p)
  18281. (call-interactively 'org-inlinetask-promote))
  18282. ((or (org-at-item-p)
  18283. (and (org-region-active-p)
  18284. (save-excursion
  18285. (goto-char (region-beginning))
  18286. (org-at-item-p))))
  18287. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18288. (call-interactively 'org-outdent-item))
  18289. (t (call-interactively 'backward-word))))
  18290. (defun org-metaright (&optional arg)
  18291. "Demote a subtree, a list item or move table column to right.
  18292. In front of a drawer or a block keyword, indent it correctly.
  18293. With no specific context, calls the Emacs default `forward-word'.
  18294. See the individual commands for more information."
  18295. (interactive "P")
  18296. (cond
  18297. ((run-hook-with-args-until-success 'org-metaright-hook))
  18298. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18299. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18300. ((org-at-block-p) (call-interactively 'org-indent-block))
  18301. ((org-with-limited-levels
  18302. (or (org-at-heading-p)
  18303. (and (org-region-active-p)
  18304. (save-excursion
  18305. (goto-char (region-beginning))
  18306. (org-at-heading-p)))))
  18307. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18308. (call-interactively 'org-do-demote))
  18309. ;; At an inline task.
  18310. ((org-at-heading-p)
  18311. (call-interactively 'org-inlinetask-demote))
  18312. ((or (org-at-item-p)
  18313. (and (org-region-active-p)
  18314. (save-excursion
  18315. (goto-char (region-beginning))
  18316. (org-at-item-p))))
  18317. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18318. (call-interactively 'org-indent-item))
  18319. (t (call-interactively 'forward-word))))
  18320. (defun org-check-for-hidden (what)
  18321. "Check if there are hidden headlines/items in the current visual line.
  18322. WHAT can be either `headlines' or `items'. If the current line is
  18323. an outline or item heading and it has a folded subtree below it,
  18324. this function returns t, nil otherwise."
  18325. (let ((re (cond
  18326. ((eq what 'headlines) org-outline-regexp-bol)
  18327. ((eq what 'items) (org-item-beginning-re))
  18328. (t (error "This should not happen"))))
  18329. beg end)
  18330. (save-excursion
  18331. (catch 'exit
  18332. (unless (org-region-active-p)
  18333. (setq beg (point-at-bol))
  18334. (beginning-of-line 2)
  18335. (while (and (not (eobp)) ;; this is like `next-line'
  18336. (get-char-property (1- (point)) 'invisible))
  18337. (beginning-of-line 2))
  18338. (setq end (point))
  18339. (goto-char beg)
  18340. (goto-char (point-at-eol))
  18341. (setq end (max end (point)))
  18342. (while (re-search-forward re end t)
  18343. (if (get-char-property (match-beginning 0) 'invisible)
  18344. (throw 'exit t))))
  18345. nil))))
  18346. (defun org-metaup (&optional arg)
  18347. "Move subtree up or move table row up.
  18348. Calls `org-move-subtree-up' or `org-table-move-row' or
  18349. `org-move-item-up', depending on context. See the individual commands
  18350. for more information."
  18351. (interactive "P")
  18352. (cond
  18353. ((run-hook-with-args-until-success 'org-metaup-hook))
  18354. ((org-region-active-p)
  18355. (let* ((a (min (region-beginning) (region-end)))
  18356. (b (1- (max (region-beginning) (region-end))))
  18357. (c (save-excursion (goto-char a)
  18358. (move-beginning-of-line 0)))
  18359. (d (save-excursion (goto-char a)
  18360. (move-end-of-line 0) (point))))
  18361. (transpose-regions a b c d)
  18362. (goto-char c)))
  18363. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18364. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18365. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18366. (t (org-drag-element-backward))))
  18367. (defun org-metadown (&optional arg)
  18368. "Move subtree down or move table row down.
  18369. Calls `org-move-subtree-down' or `org-table-move-row' or
  18370. `org-move-item-down', depending on context. See the individual
  18371. commands for more information."
  18372. (interactive "P")
  18373. (cond
  18374. ((run-hook-with-args-until-success 'org-metadown-hook))
  18375. ((org-region-active-p)
  18376. (let* ((a (min (region-beginning) (region-end)))
  18377. (b (max (region-beginning) (region-end)))
  18378. (c (save-excursion (goto-char b)
  18379. (move-beginning-of-line 1)))
  18380. (d (save-excursion (goto-char b)
  18381. (move-end-of-line 1) (1+ (point)))))
  18382. (transpose-regions a b c d)
  18383. (goto-char d)))
  18384. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18385. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18386. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18387. (t (org-drag-element-forward))))
  18388. (defun org-shiftup (&optional arg)
  18389. "Increase item in timestamp or increase priority of current headline.
  18390. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18391. depending on context. See the individual commands for more information."
  18392. (interactive "P")
  18393. (cond
  18394. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18395. ((and org-support-shift-select (org-region-active-p))
  18396. (org-call-for-shift-select 'previous-line))
  18397. ((org-at-timestamp-p t)
  18398. (call-interactively (if org-edit-timestamp-down-means-later
  18399. 'org-timestamp-down 'org-timestamp-up)))
  18400. ((and (not (eq org-support-shift-select 'always))
  18401. org-enable-priority-commands
  18402. (org-at-heading-p))
  18403. (call-interactively 'org-priority-up))
  18404. ((and (not org-support-shift-select) (org-at-item-p))
  18405. (call-interactively 'org-previous-item))
  18406. ((org-clocktable-try-shift 'up arg))
  18407. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18408. (org-support-shift-select
  18409. (org-call-for-shift-select 'previous-line))
  18410. (t (org-shiftselect-error))))
  18411. (defun org-shiftdown (&optional arg)
  18412. "Decrease item in timestamp or decrease priority of current headline.
  18413. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18414. depending on context. See the individual commands for more information."
  18415. (interactive "P")
  18416. (cond
  18417. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18418. ((and org-support-shift-select (org-region-active-p))
  18419. (org-call-for-shift-select 'next-line))
  18420. ((org-at-timestamp-p t)
  18421. (call-interactively (if org-edit-timestamp-down-means-later
  18422. 'org-timestamp-up 'org-timestamp-down)))
  18423. ((and (not (eq org-support-shift-select 'always))
  18424. org-enable-priority-commands
  18425. (org-at-heading-p))
  18426. (call-interactively 'org-priority-down))
  18427. ((and (not org-support-shift-select) (org-at-item-p))
  18428. (call-interactively 'org-next-item))
  18429. ((org-clocktable-try-shift 'down arg))
  18430. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18431. (org-support-shift-select
  18432. (org-call-for-shift-select 'next-line))
  18433. (t (org-shiftselect-error))))
  18434. (defun org-shiftright (&optional arg)
  18435. "Cycle the thing at point or in the current line, depending on context.
  18436. Depending on context, this does one of the following:
  18437. - switch a timestamp at point one day into the future
  18438. - on a headline, switch to the next TODO keyword.
  18439. - on an item, switch entire list to the next bullet type
  18440. - on a property line, switch to the next allowed value
  18441. - on a clocktable definition line, move time block into the future"
  18442. (interactive "P")
  18443. (cond
  18444. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18445. ((and org-support-shift-select (org-region-active-p))
  18446. (org-call-for-shift-select 'forward-char))
  18447. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18448. ((and (not (eq org-support-shift-select 'always))
  18449. (org-at-heading-p))
  18450. (let ((org-inhibit-logging
  18451. (not org-treat-S-cursor-todo-selection-as-state-change))
  18452. (org-inhibit-blocking
  18453. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18454. (org-call-with-arg 'org-todo 'right)))
  18455. ((or (and org-support-shift-select
  18456. (not (eq org-support-shift-select 'always))
  18457. (org-at-item-bullet-p))
  18458. (and (not org-support-shift-select) (org-at-item-p)))
  18459. (org-call-with-arg 'org-cycle-list-bullet nil))
  18460. ((and (not (eq org-support-shift-select 'always))
  18461. (org-at-property-p))
  18462. (call-interactively 'org-property-next-allowed-value))
  18463. ((org-clocktable-try-shift 'right arg))
  18464. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18465. (org-support-shift-select
  18466. (org-call-for-shift-select 'forward-char))
  18467. (t (org-shiftselect-error))))
  18468. (defun org-shiftleft (&optional arg)
  18469. "Cycle the thing at point or in the current line, depending on context.
  18470. Depending on context, this does one of the following:
  18471. - switch a timestamp at point one day into the past
  18472. - on a headline, switch to the previous TODO keyword.
  18473. - on an item, switch entire list to the previous bullet type
  18474. - on a property line, switch to the previous allowed value
  18475. - on a clocktable definition line, move time block into the past"
  18476. (interactive "P")
  18477. (cond
  18478. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18479. ((and org-support-shift-select (org-region-active-p))
  18480. (org-call-for-shift-select 'backward-char))
  18481. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18482. ((and (not (eq org-support-shift-select 'always))
  18483. (org-at-heading-p))
  18484. (let ((org-inhibit-logging
  18485. (not org-treat-S-cursor-todo-selection-as-state-change))
  18486. (org-inhibit-blocking
  18487. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18488. (org-call-with-arg 'org-todo 'left)))
  18489. ((or (and org-support-shift-select
  18490. (not (eq org-support-shift-select 'always))
  18491. (org-at-item-bullet-p))
  18492. (and (not org-support-shift-select) (org-at-item-p)))
  18493. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18494. ((and (not (eq org-support-shift-select 'always))
  18495. (org-at-property-p))
  18496. (call-interactively 'org-property-previous-allowed-value))
  18497. ((org-clocktable-try-shift 'left arg))
  18498. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18499. (org-support-shift-select
  18500. (org-call-for-shift-select 'backward-char))
  18501. (t (org-shiftselect-error))))
  18502. (defun org-shiftcontrolright ()
  18503. "Switch to next TODO set."
  18504. (interactive)
  18505. (cond
  18506. ((and org-support-shift-select (org-region-active-p))
  18507. (org-call-for-shift-select 'forward-word))
  18508. ((and (not (eq org-support-shift-select 'always))
  18509. (org-at-heading-p))
  18510. (org-call-with-arg 'org-todo 'nextset))
  18511. (org-support-shift-select
  18512. (org-call-for-shift-select 'forward-word))
  18513. (t (org-shiftselect-error))))
  18514. (defun org-shiftcontrolleft ()
  18515. "Switch to previous TODO set."
  18516. (interactive)
  18517. (cond
  18518. ((and org-support-shift-select (org-region-active-p))
  18519. (org-call-for-shift-select 'backward-word))
  18520. ((and (not (eq org-support-shift-select 'always))
  18521. (org-at-heading-p))
  18522. (org-call-with-arg 'org-todo 'previousset))
  18523. (org-support-shift-select
  18524. (org-call-for-shift-select 'backward-word))
  18525. (t (org-shiftselect-error))))
  18526. (defun org-shiftcontrolup (&optional n)
  18527. "Change timestamps synchronously up in CLOCK log lines.
  18528. Optional argument N tells to change by that many units."
  18529. (interactive "P")
  18530. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18531. (let (org-support-shift-select)
  18532. (org-clock-timestamps-up n))
  18533. (user-error "Not at a clock log")))
  18534. (defun org-shiftcontroldown (&optional n)
  18535. "Change timestamps synchronously down in CLOCK log lines.
  18536. Optional argument N tells to change by that many units."
  18537. (interactive "P")
  18538. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  18539. (let (org-support-shift-select)
  18540. (org-clock-timestamps-down n))
  18541. (user-error "Not at a clock log")))
  18542. (defun org-ctrl-c-ret ()
  18543. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18544. (interactive)
  18545. (cond
  18546. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18547. (t (call-interactively 'org-insert-heading))))
  18548. (defun org-find-visible ()
  18549. (let ((s (point)))
  18550. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18551. (get-char-property s 'invisible)))
  18552. s))
  18553. (defun org-find-invisible ()
  18554. (let ((s (point)))
  18555. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18556. (not (get-char-property s 'invisible))))
  18557. s))
  18558. (defun org-copy-visible (beg end)
  18559. "Copy the visible parts of the region."
  18560. (interactive "r")
  18561. (let (snippets s)
  18562. (save-excursion
  18563. (save-restriction
  18564. (narrow-to-region beg end)
  18565. (setq s (goto-char (point-min)))
  18566. (while (not (= (point) (point-max)))
  18567. (goto-char (org-find-invisible))
  18568. (push (buffer-substring s (point)) snippets)
  18569. (setq s (goto-char (org-find-visible))))))
  18570. (kill-new (apply 'concat (nreverse snippets)))))
  18571. (defun org-copy-special ()
  18572. "Copy region in table or copy current subtree.
  18573. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18574. See the individual commands for more information."
  18575. (interactive)
  18576. (call-interactively
  18577. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18578. (defun org-cut-special ()
  18579. "Cut region in table or cut current subtree.
  18580. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18581. See the individual commands for more information."
  18582. (interactive)
  18583. (call-interactively
  18584. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18585. (defun org-paste-special (arg)
  18586. "Paste rectangular region into table, or past subtree relative to level.
  18587. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18588. See the individual commands for more information."
  18589. (interactive "P")
  18590. (if (org-at-table-p)
  18591. (org-table-paste-rectangle)
  18592. (org-paste-subtree arg)))
  18593. (defsubst org-in-fixed-width-region-p ()
  18594. "Is point in a fixed-width region?"
  18595. (save-match-data
  18596. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18597. (defun org-edit-special (&optional arg)
  18598. "Call a special editor for the element at point.
  18599. When at a table, call the formula editor with `org-table-edit-formulas'.
  18600. When in a source code block, call `org-edit-src-code'.
  18601. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18602. When at an #+INCLUDE keyword, visit the included file.
  18603. On a link, call `ffap' to visit the link at point.
  18604. Otherwise, return a user error."
  18605. (interactive "P")
  18606. (let ((element (org-element-at-point)))
  18607. (assert (not buffer-read-only) nil
  18608. "Buffer is read-only: %s" (buffer-name))
  18609. (case (org-element-type element)
  18610. (src-block
  18611. (if (not arg) (org-edit-src-code)
  18612. (let* ((info (org-babel-get-src-block-info))
  18613. (lang (nth 0 info))
  18614. (params (nth 2 info))
  18615. (session (cdr (assq :session params))))
  18616. (if (not session) (org-edit-src-code)
  18617. ;; At a src-block with a session and function called with
  18618. ;; an ARG: switch to the buffer related to the inferior
  18619. ;; process.
  18620. (switch-to-buffer
  18621. (funcall (intern (concat "org-babel-prep-session:" lang))
  18622. session params))))))
  18623. (keyword
  18624. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18625. (find-file-other-window
  18626. (org-remove-double-quotes
  18627. (car (org-split-string (org-element-property :value element)))))
  18628. (user-error "No special environment to edit here")))
  18629. (table
  18630. (if (eq (org-element-property :type element) 'table.el)
  18631. (org-edit-src-code)
  18632. (call-interactively 'org-table-edit-formulas)))
  18633. ;; Only Org tables contain `table-row' type elements.
  18634. (table-row (call-interactively 'org-table-edit-formulas))
  18635. ((example-block export-block) (org-edit-src-code))
  18636. (fixed-width (org-edit-fixed-width-region))
  18637. (otherwise
  18638. ;; No notable element at point. Though, we may be at a link,
  18639. ;; which is an object. Thus, scan deeper.
  18640. (if (eq (org-element-type (org-element-context element)) 'link)
  18641. (call-interactively 'ffap)
  18642. (user-error "No special environment to edit here"))))))
  18643. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18644. (defun org-ctrl-c-ctrl-c (&optional arg)
  18645. "Set tags in headline, or update according to changed information at point.
  18646. This command does many different things, depending on context:
  18647. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18648. this is what we do.
  18649. - If the cursor is on a statistics cookie, update it.
  18650. - If the cursor is in a headline, prompt for tags and insert them
  18651. into the current line, aligned to `org-tags-column'. When called
  18652. with prefix arg, realign all tags in the current buffer.
  18653. - If the cursor is in one of the special #+KEYWORD lines, this
  18654. triggers scanning the buffer for these lines and updating the
  18655. information.
  18656. - If the cursor is inside a table, realign the table. This command
  18657. works even if the automatic table editor has been turned off.
  18658. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18659. the entire table.
  18660. - If the cursor is at a footnote reference or definition, jump to
  18661. the corresponding definition or references, respectively.
  18662. - If the cursor is a the beginning of a dynamic block, update it.
  18663. - If the current buffer is a capture buffer, close note and file it.
  18664. - If the cursor is on a <<<target>>>, update radio targets and
  18665. corresponding links in this buffer.
  18666. - If the cursor is on a numbered item in a plain list, renumber the
  18667. ordered list.
  18668. - If the cursor is on a checkbox, toggle it.
  18669. - If the cursor is on a code block, evaluate it. The variable
  18670. `org-confirm-babel-evaluate' can be used to control prompting
  18671. before code block evaluation, by default every code block
  18672. evaluation requires confirmation. Code block evaluation can be
  18673. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18674. (interactive "P")
  18675. (cond
  18676. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18677. org-occur-highlights
  18678. org-latex-fragment-image-overlays)
  18679. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18680. (org-remove-occur-highlights)
  18681. (org-remove-latex-fragment-image-overlays)
  18682. (message "Temporary highlights/overlays removed from current buffer"))
  18683. ((and (local-variable-p 'org-finish-function (current-buffer))
  18684. (fboundp org-finish-function))
  18685. (funcall org-finish-function))
  18686. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18687. (t
  18688. (let* ((context (org-element-context)) (type (org-element-type context)))
  18689. ;; Test if point is within a blank line.
  18690. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18691. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18692. (user-error "C-c C-c can do nothing useful at this location"))
  18693. (case type
  18694. ;; When at a link, act according to the parent instead.
  18695. (link (setq context (org-element-property :parent context))
  18696. (setq type (org-element-type context)))
  18697. ;; Unsupported object types: check parent element instead.
  18698. ((bold code entity export-snippet inline-babel-call inline-src-block
  18699. italic latex-fragment line-break macro strike-through subscript
  18700. superscript underline verbatim)
  18701. (while (and (setq context (org-element-property :parent context))
  18702. (not (memq (setq type (org-element-type context))
  18703. '(paragraph verse-block table-cell)))))))
  18704. ;; For convenience: at the first line of a paragraph on the
  18705. ;; same line as an item, apply function on that item instead.
  18706. (when (eq type 'paragraph)
  18707. (let ((parent (org-element-property :parent context)))
  18708. (when (and (eq (org-element-type parent) 'item)
  18709. (= (point-at-bol) (org-element-property :begin parent)))
  18710. (setq context parent type 'item))))
  18711. ;; Act according to type of element or object at point.
  18712. (case type
  18713. (clock (org-clock-update-time-maybe))
  18714. (dynamic-block
  18715. (save-excursion
  18716. (goto-char (org-element-property :post-affiliated context))
  18717. (org-update-dblock)))
  18718. (footnote-definition
  18719. (goto-char (org-element-property :post-affiliated context))
  18720. (call-interactively 'org-footnote-action))
  18721. (footnote-reference (call-interactively 'org-footnote-action))
  18722. ((headline inlinetask)
  18723. (save-excursion (goto-char (org-element-property :begin context))
  18724. (call-interactively 'org-set-tags)))
  18725. (item
  18726. ;; At an item: a double C-u set checkbox to "[-]"
  18727. ;; unconditionally, whereas a single one will toggle its
  18728. ;; presence. Without an universal argument, if the item
  18729. ;; has a checkbox, toggle it. Otherwise repair the list.
  18730. (let* ((box (org-element-property :checkbox context))
  18731. (struct (org-element-property :structure context))
  18732. (old-struct (copy-tree struct))
  18733. (parents (org-list-parents-alist struct))
  18734. (prevs (org-list-prevs-alist struct))
  18735. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18736. (org-list-set-checkbox
  18737. (org-element-property :begin context) struct
  18738. (cond ((equal arg '(16)) "[-]")
  18739. ((and (not box) (equal arg '(4))) "[ ]")
  18740. ((or (not box) (equal arg '(4))) nil)
  18741. ((eq box 'on) "[ ]")
  18742. (t "[X]")))
  18743. ;; Mimic `org-list-write-struct' but with grabbing
  18744. ;; a return value from `org-list-struct-fix-box'.
  18745. (org-list-struct-fix-ind struct parents 2)
  18746. (org-list-struct-fix-item-end struct)
  18747. (org-list-struct-fix-bul struct prevs)
  18748. (org-list-struct-fix-ind struct parents)
  18749. (let ((block-item
  18750. (org-list-struct-fix-box struct parents prevs orderedp)))
  18751. (if (and box (equal struct old-struct))
  18752. (if (equal arg '(16))
  18753. (message "Checkboxes already reset")
  18754. (user-error "Cannot toggle this checkbox: %s"
  18755. (if (eq box 'on)
  18756. "all subitems checked"
  18757. "unchecked subitems")))
  18758. (org-list-struct-apply-struct struct old-struct)
  18759. (org-update-checkbox-count-maybe))
  18760. (when block-item
  18761. (message "Checkboxes were removed due to empty box at line %d"
  18762. (org-current-line block-item))))))
  18763. (keyword
  18764. (let ((org-inhibit-startup-visibility-stuff t)
  18765. (org-startup-align-all-tables nil))
  18766. (when (boundp 'org-table-coordinate-overlays)
  18767. (mapc 'delete-overlay org-table-coordinate-overlays)
  18768. (setq org-table-coordinate-overlays nil))
  18769. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18770. (message "Local setup has been refreshed"))
  18771. (plain-list
  18772. ;; At a plain list, with a double C-u argument, set
  18773. ;; checkboxes of each item to "[-]", whereas a single one
  18774. ;; will toggle their presence according to the state of the
  18775. ;; first item in the list. Without an argument, repair the
  18776. ;; list.
  18777. (let* ((begin (org-element-property :contents-begin context))
  18778. (beginm (move-marker (make-marker) begin))
  18779. (struct (org-element-property :structure context))
  18780. (old-struct (copy-tree struct))
  18781. (first-box (save-excursion
  18782. (goto-char begin)
  18783. (looking-at org-list-full-item-re)
  18784. (match-string-no-properties 3)))
  18785. (new-box (cond ((equal arg '(16)) "[-]")
  18786. ((equal arg '(4)) (unless first-box "[ ]"))
  18787. ((equal first-box "[X]") "[ ]")
  18788. (t "[X]"))))
  18789. (cond
  18790. (arg
  18791. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18792. (org-list-get-all-items
  18793. begin struct (org-list-prevs-alist struct))))
  18794. ((and first-box (eq (point) begin))
  18795. ;; For convenience, when point is at bol on the first
  18796. ;; item of the list and no argument is provided, simply
  18797. ;; toggle checkbox of that item, if any.
  18798. (org-list-set-checkbox begin struct new-box)))
  18799. (org-list-write-struct
  18800. struct (org-list-parents-alist struct) old-struct)
  18801. (org-update-checkbox-count-maybe)
  18802. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18803. ((property-drawer node-property)
  18804. (call-interactively 'org-property-action))
  18805. ((radio-target target)
  18806. (call-interactively 'org-update-radio-target-regexp))
  18807. (statistics-cookie
  18808. (call-interactively 'org-update-statistics-cookies))
  18809. ((table table-cell table-row)
  18810. ;; At a table, recalculate every field and align it. Also
  18811. ;; send the table if necessary. If the table has
  18812. ;; a `table.el' type, just give up. At a table row or
  18813. ;; cell, maybe recalculate line but always align table.
  18814. (if (eq (org-element-property :type context) 'table.el)
  18815. (message "Use C-c ' to edit table.el tables")
  18816. (let ((org-enable-table-editor t))
  18817. (if (or (eq type 'table)
  18818. ;; Check if point is at a TBLFM line.
  18819. (and (eq type 'table-row)
  18820. (= (point) (org-element-property :end context))))
  18821. (save-excursion
  18822. (if (org-at-TBLFM-p)
  18823. (progn (require 'org-table)
  18824. (org-table-calc-current-TBLFM))
  18825. (goto-char (org-element-property :contents-begin context))
  18826. (org-call-with-arg 'org-table-recalculate (or arg t))
  18827. (orgtbl-send-table 'maybe)))
  18828. (org-table-maybe-eval-formula)
  18829. (cond (arg (call-interactively 'org-table-recalculate))
  18830. ((org-table-maybe-recalculate-line))
  18831. (t (org-table-align)))))))
  18832. (timestamp (org-timestamp-change 0 'day))
  18833. (otherwise
  18834. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18835. (user-error
  18836. "C-c C-c can do nothing useful at this location")))))))))
  18837. (defun org-mode-restart ()
  18838. (interactive)
  18839. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  18840. (funcall major-mode)
  18841. (hack-local-variables)
  18842. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  18843. (org-indent-mode -1)))
  18844. (message "%s restarted" major-mode))
  18845. (defun org-kill-note-or-show-branches ()
  18846. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18847. (interactive)
  18848. (if (not org-finish-function)
  18849. (progn
  18850. (hide-subtree)
  18851. (call-interactively 'show-branches))
  18852. (let ((org-note-abort t))
  18853. (funcall org-finish-function))))
  18854. (defun org-open-line (n)
  18855. "Insert a new row in tables, call `open-line' elsewhere.
  18856. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  18857. (interactive "*p")
  18858. (cond
  18859. ((not org-special-ctrl-o)
  18860. (open-line n))
  18861. ((org-at-table-p)
  18862. (org-table-insert-row))
  18863. (t
  18864. (open-line n))))
  18865. (defun org-return (&optional indent)
  18866. "Goto next table row or insert a newline.
  18867. Calls `org-table-next-row' or `newline', depending on context.
  18868. See the individual commands for more information."
  18869. (interactive)
  18870. (let (org-ts-what)
  18871. (cond
  18872. ((or (bobp) (org-in-src-block-p))
  18873. (if indent (newline-and-indent) (newline)))
  18874. ((org-at-table-p)
  18875. (org-table-justify-field-maybe)
  18876. (call-interactively 'org-table-next-row))
  18877. ;; when `newline-and-indent' is called within a list, make sure
  18878. ;; text moved stays inside the item.
  18879. ((and (org-in-item-p) indent)
  18880. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18881. (progn
  18882. (save-match-data (newline))
  18883. (org-indent-line-to (length (match-string 0))))
  18884. (let ((ind (org-get-indentation)))
  18885. (newline)
  18886. (if (org-looking-back org-list-end-re)
  18887. (org-indent-line)
  18888. (org-indent-line-to ind)))))
  18889. ((and org-return-follows-link
  18890. (org-at-timestamp-p t)
  18891. (not (eq org-ts-what 'after)))
  18892. (org-follow-timestamp-link))
  18893. ((and org-return-follows-link
  18894. (let ((tprop (get-text-property (point) 'face)))
  18895. (or (eq tprop 'org-link)
  18896. (and (listp tprop) (memq 'org-link tprop)))))
  18897. (call-interactively 'org-open-at-point))
  18898. ((and (org-at-heading-p)
  18899. (looking-at
  18900. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18901. (org-show-entry)
  18902. (end-of-line 1)
  18903. (newline))
  18904. (t (if indent (newline-and-indent) (newline))))))
  18905. (defun org-return-indent ()
  18906. "Goto next table row or insert a newline and indent.
  18907. Calls `org-table-next-row' or `newline-and-indent', depending on
  18908. context. See the individual commands for more information."
  18909. (interactive)
  18910. (org-return t))
  18911. (defun org-ctrl-c-star ()
  18912. "Compute table, or change heading status of lines.
  18913. Calls `org-table-recalculate' or `org-toggle-heading',
  18914. depending on context."
  18915. (interactive)
  18916. (cond
  18917. ((org-at-table-p)
  18918. (call-interactively 'org-table-recalculate))
  18919. (t
  18920. ;; Convert all lines in region to list items
  18921. (call-interactively 'org-toggle-heading))))
  18922. (defun org-ctrl-c-minus ()
  18923. "Insert separator line in table or modify bullet status of line.
  18924. Also turns a plain line or a region of lines into list items.
  18925. Calls `org-table-insert-hline', `org-toggle-item', or
  18926. `org-cycle-list-bullet', depending on context."
  18927. (interactive)
  18928. (cond
  18929. ((org-at-table-p)
  18930. (call-interactively 'org-table-insert-hline))
  18931. ((org-region-active-p)
  18932. (call-interactively 'org-toggle-item))
  18933. ((org-in-item-p)
  18934. (call-interactively 'org-cycle-list-bullet))
  18935. (t
  18936. (call-interactively 'org-toggle-item))))
  18937. (defun org-toggle-item (arg)
  18938. "Convert headings or normal lines to items, items to normal lines.
  18939. If there is no active region, only the current line is considered.
  18940. If the first non blank line in the region is a headline, convert
  18941. all headlines to items, shifting text accordingly.
  18942. If it is an item, convert all items to normal lines.
  18943. If it is normal text, change region into a list of items.
  18944. With a prefix argument ARG, change the region in a single item."
  18945. (interactive "P")
  18946. (let ((shift-text
  18947. (function
  18948. ;; Shift text in current section to IND, from point to END.
  18949. ;; The function leaves point to END line.
  18950. (lambda (ind end)
  18951. (let ((min-i 1000) (end (copy-marker end)))
  18952. ;; First determine the minimum indentation (MIN-I) of
  18953. ;; the text.
  18954. (save-excursion
  18955. (catch 'exit
  18956. (while (< (point) end)
  18957. (let ((i (org-get-indentation)))
  18958. (cond
  18959. ;; Skip blank lines and inline tasks.
  18960. ((looking-at "^[ \t]*$"))
  18961. ((looking-at org-outline-regexp-bol))
  18962. ;; We can't find less than 0 indentation.
  18963. ((zerop i) (throw 'exit (setq min-i 0)))
  18964. ((< i min-i) (setq min-i i))))
  18965. (forward-line))))
  18966. ;; Then indent each line so that a line indented to
  18967. ;; MIN-I becomes indented to IND. Ignore blank lines
  18968. ;; and inline tasks in the process.
  18969. (let ((delta (- ind min-i)))
  18970. (while (< (point) end)
  18971. (unless (or (looking-at "^[ \t]*$")
  18972. (looking-at org-outline-regexp-bol))
  18973. (org-indent-line-to (+ (org-get-indentation) delta)))
  18974. (forward-line)))))))
  18975. (skip-blanks
  18976. (function
  18977. ;; Return beginning of first non-blank line, starting from
  18978. ;; line at POS.
  18979. (lambda (pos)
  18980. (save-excursion
  18981. (goto-char pos)
  18982. (skip-chars-forward " \r\t\n")
  18983. (point-at-bol)))))
  18984. beg end)
  18985. ;; Determine boundaries of changes.
  18986. (if (org-region-active-p)
  18987. (setq beg (funcall skip-blanks (region-beginning))
  18988. end (copy-marker (region-end)))
  18989. (setq beg (funcall skip-blanks (point-at-bol))
  18990. end (copy-marker (point-at-eol))))
  18991. ;; Depending on the starting line, choose an action on the text
  18992. ;; between BEG and END.
  18993. (org-with-limited-levels
  18994. (save-excursion
  18995. (goto-char beg)
  18996. (cond
  18997. ;; Case 1. Start at an item: de-itemize. Note that it only
  18998. ;; happens when a region is active: `org-ctrl-c-minus'
  18999. ;; would call `org-cycle-list-bullet' otherwise.
  19000. ((org-at-item-p)
  19001. (while (< (point) end)
  19002. (when (org-at-item-p)
  19003. (skip-chars-forward " \t")
  19004. (delete-region (point) (match-end 0)))
  19005. (forward-line)))
  19006. ;; Case 2. Start at an heading: convert to items.
  19007. ((org-at-heading-p)
  19008. (let* ((bul (org-list-bullet-string "-"))
  19009. (bul-len (length bul))
  19010. (done (org-entry-is-done-p))
  19011. (todo (org-entry-is-todo-p))
  19012. ;; Indentation of the first heading. It should be
  19013. ;; relative to the indentation of its parent, if any.
  19014. (start-ind (save-excursion
  19015. (cond
  19016. ((not org-adapt-indentation) 0)
  19017. ((not (outline-previous-heading)) 0)
  19018. (t (length (match-string 0))))))
  19019. ;; Level of first heading. Further headings will be
  19020. ;; compared to it to determine hierarchy in the list.
  19021. (ref-level (org-reduced-level (org-outline-level))))
  19022. (when (or done todo) (org-todo ""))
  19023. (while (< (point) end)
  19024. (let* ((level (org-reduced-level (org-outline-level)))
  19025. (delta (max 0 (- level ref-level))))
  19026. ;; If current headline is less indented than the first
  19027. ;; one, set it as reference, in order to preserve
  19028. ;; subtrees.
  19029. (when (< level ref-level) (setq ref-level level))
  19030. (replace-match bul t t)
  19031. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19032. (when (or done todo)
  19033. (let* ((struct (org-list-struct))
  19034. (old (copy-tree struct)))
  19035. (org-list-set-checkbox (line-beginning-position)
  19036. struct
  19037. (if done "[X]" "[ ]"))
  19038. (org-list-write-struct struct
  19039. (org-list-parents-alist struct)
  19040. old)))
  19041. ;; Ensure all text down to END (or SECTION-END) belongs
  19042. ;; to the newly created item.
  19043. (let ((section-end (save-excursion
  19044. (or (outline-next-heading) (point)))))
  19045. (forward-line)
  19046. (funcall shift-text
  19047. (+ start-ind (* (1+ delta) bul-len))
  19048. (min end section-end)))))))
  19049. ;; Case 3. Normal line with ARG: make the first line of region
  19050. ;; an item, and shift indentation of others lines to
  19051. ;; set them as item's body.
  19052. (arg (let* ((bul (org-list-bullet-string "-"))
  19053. (bul-len (length bul))
  19054. (ref-ind (org-get-indentation)))
  19055. (skip-chars-forward " \t")
  19056. (insert bul)
  19057. (forward-line)
  19058. (while (< (point) end)
  19059. ;; Ensure that lines less indented than first one
  19060. ;; still get included in item body.
  19061. (funcall shift-text
  19062. (+ ref-ind bul-len)
  19063. (min end (save-excursion (or (outline-next-heading)
  19064. (point)))))
  19065. (forward-line))))
  19066. ;; Case 4. Normal line without ARG: turn each non-item line
  19067. ;; into an item.
  19068. (t
  19069. (while (< (point) end)
  19070. (unless (or (org-at-heading-p) (org-at-item-p))
  19071. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19072. (replace-match
  19073. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19074. (forward-line))))))))
  19075. (defun org-toggle-heading (&optional nstars)
  19076. "Convert headings to normal text, or items or text to headings.
  19077. If there is no active region, only convert the current line.
  19078. With a \\[universal-argument] prefix, convert the whole list at
  19079. point into heading.
  19080. In a region:
  19081. - If the first non blank line is a headline, remove the stars
  19082. from all headlines in the region.
  19083. - If it is a normal line, turn each and every normal line (i.e.,
  19084. not an heading or an item) in the region into headings. If you
  19085. want to convert only the first line of this region, use one
  19086. universal prefix argument.
  19087. - If it is a plain list item, turn all plain list items into headings.
  19088. When converting a line into a heading, the number of stars is chosen
  19089. such that the lines become children of the current entry. However,
  19090. when a numeric prefix argument is given, its value determines the
  19091. number of stars to add."
  19092. (interactive "P")
  19093. (let ((skip-blanks
  19094. (function
  19095. ;; Return beginning of first non-blank line, starting from
  19096. ;; line at POS.
  19097. (lambda (pos)
  19098. (save-excursion
  19099. (goto-char pos)
  19100. (while (org-at-comment-p) (forward-line))
  19101. (skip-chars-forward " \r\t\n")
  19102. (point-at-bol)))))
  19103. beg end toggled)
  19104. ;; Determine boundaries of changes. If a universal prefix has
  19105. ;; been given, put the list in a region. If region ends at a bol,
  19106. ;; do not consider the last line to be in the region.
  19107. (when (and current-prefix-arg (org-at-item-p))
  19108. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19109. (org-mark-element))
  19110. (if (org-region-active-p)
  19111. (setq beg (funcall skip-blanks (region-beginning))
  19112. end (copy-marker (save-excursion
  19113. (goto-char (region-end))
  19114. (if (bolp) (point) (point-at-eol)))))
  19115. (setq beg (funcall skip-blanks (point-at-bol))
  19116. end (copy-marker (point-at-eol))))
  19117. ;; Ensure inline tasks don't count as headings.
  19118. (org-with-limited-levels
  19119. (save-excursion
  19120. (goto-char beg)
  19121. (cond
  19122. ;; Case 1. Started at an heading: de-star headings.
  19123. ((org-at-heading-p)
  19124. (while (< (point) end)
  19125. (when (org-at-heading-p t)
  19126. (looking-at org-outline-regexp) (replace-match "")
  19127. (setq toggled t))
  19128. (forward-line)))
  19129. ;; Case 2. Started at an item: change items into headlines.
  19130. ;; One star will be added by `org-list-to-subtree'.
  19131. ((org-at-item-p)
  19132. (let* ((stars (make-string
  19133. ;; subtract the star that will be added again by
  19134. ;; `org-list-to-subtree'
  19135. (if (numberp nstars) (1- nstars)
  19136. (or (org-current-level) 0))
  19137. ?*))
  19138. (add-stars
  19139. (cond (nstars "") ; stars from prefix only
  19140. ((equal stars "") "") ; before first heading
  19141. (org-odd-levels-only "*") ; inside heading, odd
  19142. (t "")))) ; inside heading, oddeven
  19143. (while (< (point) end)
  19144. (when (org-at-item-p)
  19145. ;; Pay attention to cases when region ends before list.
  19146. (let* ((struct (org-list-struct))
  19147. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19148. (save-restriction
  19149. (narrow-to-region (point) list-end)
  19150. (insert
  19151. (org-list-to-subtree
  19152. (org-list-parse-list t)
  19153. '(:istart (concat stars add-stars (funcall get-stars depth))
  19154. :icount (concat stars add-stars (funcall get-stars depth)))))))
  19155. (setq toggled t))
  19156. (forward-line))))
  19157. ;; Case 3. Started at normal text: make every line an heading,
  19158. ;; skipping headlines and items.
  19159. (t (let* ((stars
  19160. (make-string
  19161. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19162. (add-stars
  19163. (cond (nstars "") ; stars from prefix only
  19164. ((equal stars "") "*") ; before first heading
  19165. (org-odd-levels-only "**") ; inside heading, odd
  19166. (t "*"))) ; inside heading, oddeven
  19167. (rpl (concat stars add-stars " "))
  19168. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19169. (while (< (point) (if (equal nstars '(4)) lend end))
  19170. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19171. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19172. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19173. (forward-line)))))))
  19174. (unless toggled (message "Cannot toggle heading from here"))))
  19175. (defun org-meta-return (&optional arg)
  19176. "Insert a new heading or wrap a region in a table.
  19177. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19178. on context. See the individual commands for more information."
  19179. (interactive "P")
  19180. (org-check-before-invisible-edit 'insert)
  19181. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19182. (let* ((element (org-element-at-point))
  19183. (type (org-element-type element)))
  19184. (when (eq type 'table-row)
  19185. (setq element (org-element-property :parent element))
  19186. (setq type 'table))
  19187. (if (and (eq type 'table)
  19188. (eq (org-element-property :type element) 'org)
  19189. (>= (point) (org-element-property :contents-begin element))
  19190. (< (point) (org-element-property :contents-end element)))
  19191. (call-interactively 'org-table-wrap-region)
  19192. (call-interactively 'org-insert-heading)))))
  19193. ;;; Menu entries
  19194. (defsubst org-in-subtree-not-table-p ()
  19195. "Are we in a subtree and not in a table?"
  19196. (and (not (org-before-first-heading-p))
  19197. (not (org-at-table-p))))
  19198. ;; Define the Org-mode menus
  19199. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19200. '("Tbl"
  19201. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19202. ["Next Field" org-cycle (org-at-table-p)]
  19203. ["Previous Field" org-shifttab (org-at-table-p)]
  19204. ["Next Row" org-return (org-at-table-p)]
  19205. "--"
  19206. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19207. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19208. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19209. "--"
  19210. ("Column"
  19211. ["Move Column Left" org-metaleft (org-at-table-p)]
  19212. ["Move Column Right" org-metaright (org-at-table-p)]
  19213. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19214. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19215. ("Row"
  19216. ["Move Row Up" org-metaup (org-at-table-p)]
  19217. ["Move Row Down" org-metadown (org-at-table-p)]
  19218. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19219. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19220. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19221. "--"
  19222. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19223. ("Rectangle"
  19224. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19225. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19226. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19227. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19228. "--"
  19229. ("Calculate"
  19230. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19231. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19232. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19233. "--"
  19234. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19235. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19236. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19237. "--"
  19238. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19239. "--"
  19240. ["Sum Column/Rectangle" org-table-sum
  19241. (or (org-at-table-p) (org-region-active-p))]
  19242. ["Which Column?" org-table-current-column (org-at-table-p)])
  19243. ["Debug Formulas"
  19244. org-table-toggle-formula-debugger
  19245. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19246. ["Show Col/Row Numbers"
  19247. org-table-toggle-coordinate-overlays
  19248. :style toggle
  19249. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19250. "--"
  19251. ["Create" org-table-create (and (not (org-at-table-p))
  19252. org-enable-table-editor)]
  19253. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19254. ["Import from File" org-table-import (not (org-at-table-p))]
  19255. ["Export to File" org-table-export (org-at-table-p)]
  19256. "--"
  19257. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  19258. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19259. '("Org"
  19260. ("Show/Hide"
  19261. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19262. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19263. ["Sparse Tree..." org-sparse-tree t]
  19264. ["Reveal Context" org-reveal t]
  19265. ["Show All" show-all t]
  19266. "--"
  19267. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19268. "--"
  19269. ["New Heading" org-insert-heading t]
  19270. ("Navigate Headings"
  19271. ["Up" outline-up-heading t]
  19272. ["Next" outline-next-visible-heading t]
  19273. ["Previous" outline-previous-visible-heading t]
  19274. ["Next Same Level" outline-forward-same-level t]
  19275. ["Previous Same Level" outline-backward-same-level t]
  19276. "--"
  19277. ["Jump" org-goto t])
  19278. ("Edit Structure"
  19279. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19280. "--"
  19281. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19282. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19283. "--"
  19284. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19285. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19286. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19287. "--"
  19288. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19289. "--"
  19290. ["Copy visible text" org-copy-visible t]
  19291. "--"
  19292. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19293. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19294. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19295. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19296. "--"
  19297. ["Sort Region/Children" org-sort t]
  19298. "--"
  19299. ["Convert to odd levels" org-convert-to-odd-levels t]
  19300. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19301. ("Editing"
  19302. ["Emphasis..." org-emphasize t]
  19303. ["Edit Source Example" org-edit-special t]
  19304. "--"
  19305. ["Footnote new/jump" org-footnote-action t]
  19306. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19307. ("Archive"
  19308. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19309. "--"
  19310. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19311. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19312. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19313. )
  19314. "--"
  19315. ("Hyperlinks"
  19316. ["Store Link (Global)" org-store-link t]
  19317. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19318. ["Insert Link" org-insert-link t]
  19319. ["Follow Link" org-open-at-point t]
  19320. "--"
  19321. ["Next link" org-next-link t]
  19322. ["Previous link" org-previous-link t]
  19323. "--"
  19324. ["Descriptive Links"
  19325. org-toggle-link-display
  19326. :style radio
  19327. :selected org-descriptive-links
  19328. ]
  19329. ["Literal Links"
  19330. org-toggle-link-display
  19331. :style radio
  19332. :selected (not org-descriptive-links)])
  19333. "--"
  19334. ("TODO Lists"
  19335. ["TODO/DONE/-" org-todo t]
  19336. ("Select keyword"
  19337. ["Next keyword" org-shiftright (org-at-heading-p)]
  19338. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19339. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19340. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19341. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19342. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19343. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19344. "--"
  19345. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19346. :selected org-enforce-todo-dependencies :style toggle :active t]
  19347. "Settings for tree at point"
  19348. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19349. :selected (org-entry-get nil "ORDERED")
  19350. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19351. ["Do Children parallel" org-toggle-ordered-property :style radio
  19352. :selected (not (org-entry-get nil "ORDERED"))
  19353. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19354. "--"
  19355. ["Set Priority" org-priority t]
  19356. ["Priority Up" org-shiftup t]
  19357. ["Priority Down" org-shiftdown t]
  19358. "--"
  19359. ["Get news from all feeds" org-feed-update-all t]
  19360. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19361. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19362. ("TAGS and Properties"
  19363. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19364. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19365. "--"
  19366. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19367. ["Column view of properties" org-columns t]
  19368. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19369. ("Dates and Scheduling"
  19370. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19371. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19372. ("Change Date"
  19373. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19374. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19375. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19376. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19377. ["Compute Time Range" org-evaluate-time-range t]
  19378. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19379. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19380. "--"
  19381. ["Custom time format" org-toggle-time-stamp-overlays
  19382. :style radio :selected org-display-custom-times]
  19383. "--"
  19384. ["Goto Calendar" org-goto-calendar t]
  19385. ["Date from Calendar" org-date-from-calendar t]
  19386. "--"
  19387. ["Start/Restart Timer" org-timer-start t]
  19388. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19389. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19390. ["Insert Timer String" org-timer t]
  19391. ["Insert Timer Item" org-timer-item t])
  19392. ("Logging work"
  19393. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19394. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19395. ["Clock out" org-clock-out t]
  19396. ["Clock cancel" org-clock-cancel t]
  19397. "--"
  19398. ["Mark as default task" org-clock-mark-default-task t]
  19399. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19400. ["Goto running clock" org-clock-goto t]
  19401. "--"
  19402. ["Display times" org-clock-display t]
  19403. ["Create clock table" org-clock-report t]
  19404. "--"
  19405. ["Record DONE time"
  19406. (progn (setq org-log-done (not org-log-done))
  19407. (message "Switching to %s will %s record a timestamp"
  19408. (car org-done-keywords)
  19409. (if org-log-done "automatically" "not")))
  19410. :style toggle :selected org-log-done])
  19411. "--"
  19412. ["Agenda Command..." org-agenda t]
  19413. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19414. ("File List for Agenda")
  19415. ("Special views current file"
  19416. ["TODO Tree" org-show-todo-tree t]
  19417. ["Check Deadlines" org-check-deadlines t]
  19418. ["Timeline" org-timeline t]
  19419. ["Tags/Property tree" org-match-sparse-tree t])
  19420. "--"
  19421. ["Export/Publish..." org-export-dispatch t]
  19422. ("LaTeX"
  19423. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19424. :selected org-cdlatex-mode]
  19425. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19426. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19427. ["Modify math symbol" org-cdlatex-math-modify
  19428. (org-inside-LaTeX-fragment-p)]
  19429. ["Insert citation" org-reftex-citation t]
  19430. "--"
  19431. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19432. "--"
  19433. ("MobileOrg"
  19434. ["Push Files and Views" org-mobile-push t]
  19435. ["Get Captured and Flagged" org-mobile-pull t]
  19436. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19437. "--"
  19438. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19439. "--"
  19440. ("Documentation"
  19441. ["Show Version" org-version t]
  19442. ["Info Documentation" org-info t])
  19443. ("Customize"
  19444. ["Browse Org Group" org-customize t]
  19445. "--"
  19446. ["Expand This Menu" org-create-customize-menu
  19447. (fboundp 'customize-menu-create)])
  19448. ["Send bug report" org-submit-bug-report t]
  19449. "--"
  19450. ("Refresh/Reload"
  19451. ["Refresh setup current buffer" org-mode-restart t]
  19452. ["Reload Org (after update)" org-reload t]
  19453. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19454. ))
  19455. (defun org-info (&optional node)
  19456. "Read documentation for Org-mode in the info system.
  19457. With optional NODE, go directly to that node."
  19458. (interactive)
  19459. (info (format "(org)%s" (or node ""))))
  19460. ;;;###autoload
  19461. (defun org-submit-bug-report ()
  19462. "Submit a bug report on Org-mode via mail.
  19463. Don't hesitate to report any problems or inaccurate documentation.
  19464. If you don't have setup sending mail from (X)Emacs, please copy the
  19465. output buffer into your mail program, as it gives us important
  19466. information about your Org-mode version and configuration."
  19467. (interactive)
  19468. (require 'reporter)
  19469. (org-load-modules-maybe)
  19470. (org-require-autoloaded-modules)
  19471. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19472. (reporter-submit-bug-report
  19473. "emacs-orgmode@gnu.org"
  19474. (org-version nil 'full)
  19475. (let (list)
  19476. (save-window-excursion
  19477. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19478. (delete-other-windows)
  19479. (erase-buffer)
  19480. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19481. We would like to add your full Org-mode and Outline configuration to the
  19482. bug report. This greatly simplifies the work of the maintainer and
  19483. other experts on the mailing list.
  19484. HOWEVER, some variables you have customized may contain private
  19485. information. The names of customers, colleagues, or friends, might
  19486. appear in the form of file names, tags, todo states, or search strings.
  19487. If you answer yes to the prompt, you might want to check and remove
  19488. such private information before sending the email.")
  19489. (add-text-properties (point-min) (point-max) '(face org-warning))
  19490. (when (yes-or-no-p "Include your Org-mode configuration ")
  19491. (mapatoms
  19492. (lambda (v)
  19493. (and (boundp v)
  19494. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19495. (or (and (symbol-value v)
  19496. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19497. (and
  19498. (get v 'custom-type) (get v 'standard-value)
  19499. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19500. (push v list)))))
  19501. (kill-buffer (get-buffer "*Warn about privacy*"))
  19502. list))
  19503. nil nil
  19504. "Remember to cover the basics, that is, what you expected to happen and
  19505. what in fact did happen. You don't know how to make a good report? See
  19506. http://orgmode.org/manual/Feedback.html#Feedback
  19507. Your bug report will be posted to the Org-mode mailing list.
  19508. ------------------------------------------------------------------------")
  19509. (save-excursion
  19510. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19511. (replace-match "\\1Bug: \\3 [\\2]")))))
  19512. (defun org-install-agenda-files-menu ()
  19513. (let ((bl (buffer-list)))
  19514. (save-excursion
  19515. (while bl
  19516. (set-buffer (pop bl))
  19517. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19518. (when (derived-mode-p 'org-mode)
  19519. (easy-menu-change
  19520. '("Org") "File List for Agenda"
  19521. (append
  19522. (list
  19523. ["Edit File List" (org-edit-agenda-file-list) t]
  19524. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19525. ["Remove Current File from List" org-remove-file t]
  19526. ["Cycle through agenda files" org-cycle-agenda-files t]
  19527. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19528. "--")
  19529. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19530. ;;;; Documentation
  19531. (defun org-require-autoloaded-modules ()
  19532. (interactive)
  19533. (mapc 'require
  19534. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19535. org-table org-timer)))
  19536. ;;;###autoload
  19537. (defun org-reload (&optional uncompiled)
  19538. "Reload all org lisp files.
  19539. With prefix arg UNCOMPILED, load the uncompiled versions."
  19540. (interactive "P")
  19541. (require 'loadhist)
  19542. (let* ((org-dir (org-find-library-dir "org"))
  19543. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19544. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19545. (remove-re (mapconcat 'identity
  19546. (mapcar (lambda (f) (concat "^" f "$"))
  19547. (list (if (featurep 'xemacs)
  19548. "org-colview"
  19549. "org-colview-xemacs")
  19550. "org" "org-loaddefs" "org-version"))
  19551. "\\|"))
  19552. (feats (delete-dups
  19553. (mapcar 'file-name-sans-extension
  19554. (mapcar 'file-name-nondirectory
  19555. (delq nil
  19556. (mapcar 'feature-file
  19557. features))))))
  19558. (lfeat (append
  19559. (sort
  19560. (setq feats
  19561. (delq nil (mapcar
  19562. (lambda (f)
  19563. (if (and (string-match feature-re f)
  19564. (not (string-match remove-re f)))
  19565. f nil))
  19566. feats)))
  19567. 'string-lessp)
  19568. (list "org-version" "org")))
  19569. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19570. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19571. load-uncore load-misses)
  19572. (setq load-misses
  19573. (delq 't
  19574. (mapcar (lambda (f)
  19575. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19576. (and (string= org-dir contrib-dir)
  19577. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19578. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19579. (add-to-list 'load-uncore f 'append)
  19580. 't)
  19581. f))
  19582. lfeat)))
  19583. (if load-uncore
  19584. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19585. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19586. (if load-misses
  19587. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19588. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19589. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19590. ;;;###autoload
  19591. (defun org-customize ()
  19592. "Call the customize function with org as argument."
  19593. (interactive)
  19594. (org-load-modules-maybe)
  19595. (org-require-autoloaded-modules)
  19596. (customize-browse 'org))
  19597. (defun org-create-customize-menu ()
  19598. "Create a full customization menu for Org-mode, insert it into the menu."
  19599. (interactive)
  19600. (org-load-modules-maybe)
  19601. (org-require-autoloaded-modules)
  19602. (if (fboundp 'customize-menu-create)
  19603. (progn
  19604. (easy-menu-change
  19605. '("Org") "Customize"
  19606. `(["Browse Org group" org-customize t]
  19607. "--"
  19608. ,(customize-menu-create 'org)
  19609. ["Set" Custom-set t]
  19610. ["Save" Custom-save t]
  19611. ["Reset to Current" Custom-reset-current t]
  19612. ["Reset to Saved" Custom-reset-saved t]
  19613. ["Reset to Standard Settings" Custom-reset-standard t]))
  19614. (message "\"Org\"-menu now contains full customization menu"))
  19615. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19616. ;;;; Miscellaneous stuff
  19617. ;;; Generally useful functions
  19618. (defun org-get-at-bol (property)
  19619. "Get text property PROPERTY at beginning of line."
  19620. (get-text-property (point-at-bol) property))
  19621. (defun org-find-text-property-in-string (prop s)
  19622. "Return the first non-nil value of property PROP in string S."
  19623. (or (get-text-property 0 prop s)
  19624. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19625. prop s)))
  19626. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19627. "Display the given MESSAGE as a warning."
  19628. (if (fboundp 'display-warning)
  19629. (display-warning 'org message
  19630. (if (featurep 'xemacs) 'warning :warning))
  19631. (let ((buf (get-buffer-create "*Org warnings*")))
  19632. (with-current-buffer buf
  19633. (goto-char (point-max))
  19634. (insert "Warning (Org): " message)
  19635. (unless (bolp)
  19636. (newline)))
  19637. (display-buffer buf)
  19638. (sit-for 0))))
  19639. (defun org-eval (form)
  19640. "Eval FORM and return result."
  19641. (condition-case error
  19642. (eval form)
  19643. (error (format "%%![Error: %s]" error))))
  19644. (defun org-in-clocktable-p ()
  19645. "Check if the cursor is in a clocktable."
  19646. (let ((pos (point)) start)
  19647. (save-excursion
  19648. (end-of-line 1)
  19649. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19650. (setq start (match-beginning 0))
  19651. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19652. (>= (match-end 0) pos)
  19653. start))))
  19654. (defun org-in-commented-line ()
  19655. "Is point in a line starting with `#'?"
  19656. (equal (char-after (point-at-bol)) ?#))
  19657. (defun org-in-indented-comment-line ()
  19658. "Is point in a line starting with `#' after some white space?"
  19659. (save-excursion
  19660. (save-match-data
  19661. (goto-char (point-at-bol))
  19662. (looking-at "[ \t]*#"))))
  19663. (defun org-in-verbatim-emphasis ()
  19664. (save-match-data
  19665. (and (org-in-regexp org-emph-re 2)
  19666. (>= (point) (match-beginning 3))
  19667. (<= (point) (match-end 4))
  19668. (member (match-string 3) '("=" "~")))))
  19669. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19670. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19671. (if (and marker (marker-buffer marker)
  19672. (buffer-live-p (marker-buffer marker)))
  19673. (progn
  19674. (org-pop-to-buffer-same-window (marker-buffer marker))
  19675. (if (or (> marker (point-max)) (< marker (point-min)))
  19676. (widen))
  19677. (goto-char marker)
  19678. (org-show-context 'org-goto))
  19679. (if bookmark
  19680. (bookmark-jump bookmark)
  19681. (error "Cannot find location"))))
  19682. (defun org-quote-csv-field (s)
  19683. "Quote field for inclusion in CSV material."
  19684. (if (string-match "[\",]" s)
  19685. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19686. s))
  19687. (defun org-force-self-insert (N)
  19688. "Needed to enforce self-insert under remapping."
  19689. (interactive "p")
  19690. (self-insert-command N))
  19691. (defun org-string-width (s)
  19692. "Compute width of string, ignoring invisible characters.
  19693. This ignores character with invisibility property `org-link', and also
  19694. characters with property `org-cwidth', because these will become invisible
  19695. upon the next fontification round."
  19696. (let (b l)
  19697. (when (or (eq t buffer-invisibility-spec)
  19698. (assq 'org-link buffer-invisibility-spec))
  19699. (while (setq b (text-property-any 0 (length s)
  19700. 'invisible 'org-link s))
  19701. (setq s (concat (substring s 0 b)
  19702. (substring s (or (next-single-property-change
  19703. b 'invisible s) (length s)))))))
  19704. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19705. (setq s (concat (substring s 0 b)
  19706. (substring s (or (next-single-property-change
  19707. b 'org-cwidth s) (length s))))))
  19708. (setq l (string-width s) b -1)
  19709. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19710. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19711. l))
  19712. (defun org-shorten-string (s maxlength)
  19713. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19714. If the string is shorter or has length MAXLENGTH, just return the
  19715. original string. If it is longer, the functions finds a space in the
  19716. string, breaks this string off at that locations and adds three dots
  19717. as ellipsis. Including the ellipsis, the string will not be longer
  19718. than MAXLENGTH. If finding a good breaking point in the string does
  19719. not work, the string is just chopped off in the middle of a word
  19720. if necessary."
  19721. (if (<= (length s) maxlength)
  19722. s
  19723. (let* ((n (max (- maxlength 4) 1))
  19724. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19725. (if (string-match re s)
  19726. (concat (match-string 1 s) "...")
  19727. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19728. (defun org-get-indentation (&optional line)
  19729. "Get the indentation of the current line, interpreting tabs.
  19730. When LINE is given, assume it represents a line and compute its indentation."
  19731. (if line
  19732. (if (string-match "^ *" (org-remove-tabs line))
  19733. (match-end 0))
  19734. (save-excursion
  19735. (beginning-of-line 1)
  19736. (skip-chars-forward " \t")
  19737. (current-column))))
  19738. (defun org-get-string-indentation (s)
  19739. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19740. (let ((n -1) (i 0) (w tab-width) c)
  19741. (catch 'exit
  19742. (while (< (setq n (1+ n)) (length s))
  19743. (setq c (aref s n))
  19744. (cond ((= c ?\ ) (setq i (1+ i)))
  19745. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19746. (t (throw 'exit t)))))
  19747. i))
  19748. (defun org-remove-tabs (s &optional width)
  19749. "Replace tabulators in S with spaces.
  19750. Assumes that s is a single line, starting in column 0."
  19751. (setq width (or width tab-width))
  19752. (while (string-match "\t" s)
  19753. (setq s (replace-match
  19754. (make-string
  19755. (- (* width (/ (+ (match-beginning 0) width) width))
  19756. (match-beginning 0)) ?\ )
  19757. t t s)))
  19758. s)
  19759. (defun org-fix-indentation (line ind)
  19760. "Fix indentation in LINE.
  19761. IND is a cons cell with target and minimum indentation.
  19762. If the current indentation in LINE is smaller than the minimum,
  19763. leave it alone. If it is larger than ind, set it to the target."
  19764. (let* ((l (org-remove-tabs line))
  19765. (i (org-get-indentation l))
  19766. (i1 (car ind)) (i2 (cdr ind)))
  19767. (if (>= i i2) (setq l (substring line i2)))
  19768. (if (> i1 0)
  19769. (concat (make-string i1 ?\ ) l)
  19770. l)))
  19771. (defun org-remove-indentation (code &optional n)
  19772. "Remove the maximum common indentation from the lines in CODE.
  19773. N may optionally be the number of spaces to remove."
  19774. (with-temp-buffer
  19775. (insert code)
  19776. (org-do-remove-indentation n)
  19777. (buffer-string)))
  19778. (defun org-do-remove-indentation (&optional n)
  19779. "Remove the maximum common indentation from the buffer."
  19780. (untabify (point-min) (point-max))
  19781. (let ((min 10000) re)
  19782. (if n
  19783. (setq min n)
  19784. (goto-char (point-min))
  19785. (while (re-search-forward "^ *[^ \n]" nil t)
  19786. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19787. (unless (or (= min 0) (= min 10000))
  19788. (setq re (format "^ \\{%d\\}" min))
  19789. (goto-char (point-min))
  19790. (while (re-search-forward re nil t)
  19791. (replace-match "")
  19792. (end-of-line 1))
  19793. min)))
  19794. (defun org-fill-template (template alist)
  19795. "Find each %key of ALIST in TEMPLATE and replace it."
  19796. (let ((case-fold-search nil)
  19797. entry key value)
  19798. (setq alist (sort (copy-sequence alist)
  19799. (lambda (a b) (< (length (car a)) (length (car b))))))
  19800. (while (setq entry (pop alist))
  19801. (setq template
  19802. (replace-regexp-in-string
  19803. (concat "%" (regexp-quote (car entry)))
  19804. (or (cdr entry) "") template t t)))
  19805. template))
  19806. (defun org-base-buffer (buffer)
  19807. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19808. (if (not buffer)
  19809. buffer
  19810. (or (buffer-base-buffer buffer)
  19811. buffer)))
  19812. (defun org-trim (s)
  19813. "Remove whitespace at beginning and end of string."
  19814. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19815. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19816. s)
  19817. (defun org-wrap (string &optional width lines)
  19818. "Wrap string to either a number of lines, or a width in characters.
  19819. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19820. that costs. If there is a word longer than WIDTH, the text is actually
  19821. wrapped to the length of that word.
  19822. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19823. many lines, whatever width that takes.
  19824. The return value is a list of lines, without newlines at the end."
  19825. (let* ((words (org-split-string string "[ \t\n]+"))
  19826. (maxword (apply 'max (mapcar 'org-string-width words)))
  19827. w ll)
  19828. (cond (width
  19829. (org-do-wrap words (max maxword width)))
  19830. (lines
  19831. (setq w maxword)
  19832. (setq ll (org-do-wrap words maxword))
  19833. (if (<= (length ll) lines)
  19834. ll
  19835. (setq ll words)
  19836. (while (> (length ll) lines)
  19837. (setq w (1+ w))
  19838. (setq ll (org-do-wrap words w)))
  19839. ll))
  19840. (t (error "Cannot wrap this")))))
  19841. (defun org-do-wrap (words width)
  19842. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19843. (let (lines line)
  19844. (while words
  19845. (setq line (pop words))
  19846. (while (and words (< (+ (length line) (length (car words))) width))
  19847. (setq line (concat line " " (pop words))))
  19848. (setq lines (push line lines)))
  19849. (nreverse lines)))
  19850. (defun org-split-string (string &optional separators)
  19851. "Splits STRING into substrings at SEPARATORS.
  19852. No empty strings are returned if there are matches at the beginning
  19853. and end of string."
  19854. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19855. (start 0)
  19856. notfirst
  19857. (list nil))
  19858. (while (and (string-match rexp string
  19859. (if (and notfirst
  19860. (= start (match-beginning 0))
  19861. (< start (length string)))
  19862. (1+ start) start))
  19863. (< (match-beginning 0) (length string)))
  19864. (setq notfirst t)
  19865. (or (eq (match-beginning 0) 0)
  19866. (and (eq (match-beginning 0) (match-end 0))
  19867. (eq (match-beginning 0) start))
  19868. (setq list
  19869. (cons (substring string start (match-beginning 0))
  19870. list)))
  19871. (setq start (match-end 0)))
  19872. (or (eq start (length string))
  19873. (setq list
  19874. (cons (substring string start)
  19875. list)))
  19876. (nreverse list)))
  19877. (defun org-quote-vert (s)
  19878. "Replace \"|\" with \"\\vert\"."
  19879. (while (string-match "|" s)
  19880. (setq s (replace-match "\\vert" t t s)))
  19881. s)
  19882. (defun org-uuidgen-p (s)
  19883. "Is S an ID created by UUIDGEN?"
  19884. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19885. (defun org-in-src-block-p (&optional inside)
  19886. "Whether point is in a code source block.
  19887. When INSIDE is non-nil, don't consider we are within a src block
  19888. when point is at #+BEGIN_SRC or #+END_SRC."
  19889. (let ((case-fold-search t) ov)
  19890. (or (and (setq ov (overlays-at (point)))
  19891. (memq 'org-block-background
  19892. (overlay-properties (car ov))))
  19893. (and (not inside)
  19894. (save-match-data
  19895. (save-excursion
  19896. (beginning-of-line)
  19897. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19898. (defun org-context ()
  19899. "Return a list of contexts of the current cursor position.
  19900. If several contexts apply, all are returned.
  19901. Each context entry is a list with a symbol naming the context, and
  19902. two positions indicating start and end of the context. Possible
  19903. contexts are:
  19904. :headline anywhere in a headline
  19905. :headline-stars on the leading stars in a headline
  19906. :todo-keyword on a TODO keyword (including DONE) in a headline
  19907. :tags on the TAGS in a headline
  19908. :priority on the priority cookie in a headline
  19909. :item on the first line of a plain list item
  19910. :item-bullet on the bullet/number of a plain list item
  19911. :checkbox on the checkbox in a plain list item
  19912. :table in an org-mode table
  19913. :table-special on a special filed in a table
  19914. :table-table in a table.el table
  19915. :clocktable in a clocktable
  19916. :src-block in a source block
  19917. :link on a hyperlink
  19918. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  19919. :target on a <<target>>
  19920. :radio-target on a <<<radio-target>>>
  19921. :latex-fragment on a LaTeX fragment
  19922. :latex-preview on a LaTeX fragment with overlaid preview image
  19923. This function expects the position to be visible because it uses font-lock
  19924. faces as a help to recognize the following contexts: :table-special, :link,
  19925. and :keyword."
  19926. (let* ((f (get-text-property (point) 'face))
  19927. (faces (if (listp f) f (list f)))
  19928. (case-fold-search t)
  19929. (p (point)) clist o)
  19930. ;; First the large context
  19931. (cond
  19932. ((org-at-heading-p t)
  19933. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19934. (when (progn
  19935. (beginning-of-line 1)
  19936. (looking-at org-todo-line-tags-regexp))
  19937. (push (org-point-in-group p 1 :headline-stars) clist)
  19938. (push (org-point-in-group p 2 :todo-keyword) clist)
  19939. (push (org-point-in-group p 4 :tags) clist))
  19940. (goto-char p)
  19941. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19942. (if (looking-at "\\[#[A-Z0-9]\\]")
  19943. (push (org-point-in-group p 0 :priority) clist)))
  19944. ((org-at-item-p)
  19945. (push (org-point-in-group p 2 :item-bullet) clist)
  19946. (push (list :item (point-at-bol)
  19947. (save-excursion (org-end-of-item) (point)))
  19948. clist)
  19949. (and (org-at-item-checkbox-p)
  19950. (push (org-point-in-group p 0 :checkbox) clist)))
  19951. ((org-at-table-p)
  19952. (push (list :table (org-table-begin) (org-table-end)) clist)
  19953. (if (memq 'org-formula faces)
  19954. (push (list :table-special
  19955. (previous-single-property-change p 'face)
  19956. (next-single-property-change p 'face)) clist)))
  19957. ((org-at-table-p 'any)
  19958. (push (list :table-table) clist)))
  19959. (goto-char p)
  19960. (let ((case-fold-search t))
  19961. ;; New the "medium" contexts: clocktables, source blocks
  19962. (cond ((org-in-clocktable-p)
  19963. (push (list :clocktable
  19964. (and (or (looking-at "#\\+BEGIN: clocktable")
  19965. (search-backward "#+BEGIN: clocktable" nil t))
  19966. (match-beginning 0))
  19967. (and (re-search-forward "#\\+END:?" nil t)
  19968. (match-end 0))) clist))
  19969. ((org-in-src-block-p)
  19970. (push (list :src-block
  19971. (and (or (looking-at "#\\+BEGIN_SRC")
  19972. (search-backward "#+BEGIN_SRC" nil t))
  19973. (match-beginning 0))
  19974. (and (search-forward "#+END_SRC" nil t)
  19975. (match-beginning 0))) clist))))
  19976. (goto-char p)
  19977. ;; Now the small context
  19978. (cond
  19979. ((org-at-timestamp-p)
  19980. (push (org-point-in-group p 0 :timestamp) clist))
  19981. ((memq 'org-link faces)
  19982. (push (list :link
  19983. (previous-single-property-change p 'face)
  19984. (next-single-property-change p 'face)) clist))
  19985. ((memq 'org-special-keyword faces)
  19986. (push (list :keyword
  19987. (previous-single-property-change p 'face)
  19988. (next-single-property-change p 'face)) clist))
  19989. ((org-at-target-p)
  19990. (push (org-point-in-group p 0 :target) clist)
  19991. (goto-char (1- (match-beginning 0)))
  19992. (if (looking-at org-radio-target-regexp)
  19993. (push (org-point-in-group p 0 :radio-target) clist))
  19994. (goto-char p))
  19995. ((setq o (car (delq nil
  19996. (mapcar
  19997. (lambda (x)
  19998. (if (memq x org-latex-fragment-image-overlays) x))
  19999. (overlays-at (point))))))
  20000. (push (list :latex-fragment
  20001. (overlay-start o) (overlay-end o)) clist)
  20002. (push (list :latex-preview
  20003. (overlay-start o) (overlay-end o)) clist))
  20004. ((org-inside-LaTeX-fragment-p)
  20005. ;; FIXME: positions wrong.
  20006. (push (list :latex-fragment (point) (point)) clist)))
  20007. (setq clist (nreverse (delq nil clist)))
  20008. clist))
  20009. ;; FIXME: Compare with at-regexp-p Do we need both?
  20010. (defun org-in-regexp (re &optional nlines visually)
  20011. "Check if point is inside a match of regexp.
  20012. Normally only the current line is checked, but you can include NLINES extra
  20013. lines both before and after point into the search.
  20014. If VISUALLY is set, require that the cursor is not after the match but
  20015. really on, so that the block visually is on the match."
  20016. (catch 'exit
  20017. (let ((pos (point))
  20018. (eol (point-at-eol (+ 1 (or nlines 0))))
  20019. (inc (if visually 1 0)))
  20020. (save-excursion
  20021. (beginning-of-line (- 1 (or nlines 0)))
  20022. (while (re-search-forward re eol t)
  20023. (if (and (<= (match-beginning 0) pos)
  20024. (>= (+ inc (match-end 0)) pos))
  20025. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20026. (defun org-at-regexp-p (regexp)
  20027. "Is point inside a match of REGEXP in the current line?"
  20028. (catch 'exit
  20029. (save-excursion
  20030. (let ((pos (point)) (end (point-at-eol)))
  20031. (beginning-of-line 1)
  20032. (while (re-search-forward regexp end t)
  20033. (if (and (<= (match-beginning 0) pos)
  20034. (>= (match-end 0) pos))
  20035. (throw 'exit t)))
  20036. nil))))
  20037. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20038. "Non-nil when point is between matches of START-RE and END-RE.
  20039. Also return a non-nil value when point is on one of the matches.
  20040. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20041. buffer positions. Default values are the positions of headlines
  20042. surrounding the point.
  20043. The functions returns a cons cell whose car (resp. cdr) is the
  20044. position before START-RE (resp. after END-RE)."
  20045. (save-match-data
  20046. (let ((pos (point))
  20047. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20048. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20049. beg end)
  20050. (save-excursion
  20051. ;; Point is on a block when on START-RE or if START-RE can be
  20052. ;; found before it...
  20053. (and (or (org-at-regexp-p start-re)
  20054. (re-search-backward start-re limit-up t))
  20055. (setq beg (match-beginning 0))
  20056. ;; ... and END-RE after it...
  20057. (goto-char (match-end 0))
  20058. (re-search-forward end-re limit-down t)
  20059. (> (setq end (match-end 0)) pos)
  20060. ;; ... without another START-RE in-between.
  20061. (goto-char (match-beginning 0))
  20062. (not (re-search-backward start-re (1+ beg) t))
  20063. ;; Return value.
  20064. (cons beg end))))))
  20065. (defun org-in-block-p (names)
  20066. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20067. NAMES is a list of strings containing names of blocks.
  20068. Return first block name matched, or nil. Beware that in case of
  20069. nested blocks, the returned name may not belong to the closest
  20070. block from point."
  20071. (save-match-data
  20072. (catch 'exit
  20073. (let ((case-fold-search t)
  20074. (lim-up (save-excursion (outline-previous-heading)))
  20075. (lim-down (save-excursion (outline-next-heading))))
  20076. (mapc (lambda (name)
  20077. (let ((n (regexp-quote name)))
  20078. (when (org-between-regexps-p
  20079. (concat "^[ \t]*#\\+begin_" n)
  20080. (concat "^[ \t]*#\\+end_" n)
  20081. lim-up lim-down)
  20082. (throw 'exit n))))
  20083. names))
  20084. nil)))
  20085. (defun org-in-drawer-p ()
  20086. "Non-nil if point is within a drawer.
  20087. If point is within a drawer, return it, as parsed data."
  20088. (let ((element (save-match-data (org-element-at-point))))
  20089. (while (and element (not (memq (org-element-type element)
  20090. '(drawer property-drawer))))
  20091. (setq element (org-element-property :parent element)))
  20092. element))
  20093. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20094. "Call `multi-occur' with buffers for all agenda files."
  20095. (interactive "sOrg-files matching: \np")
  20096. (let* ((files (org-agenda-files))
  20097. (tnames (mapcar 'file-truename files))
  20098. (extra org-agenda-text-search-extra-files)
  20099. f)
  20100. (when (eq (car extra) 'agenda-archives)
  20101. (setq extra (cdr extra))
  20102. (setq files (org-add-archive-files files)))
  20103. (while (setq f (pop extra))
  20104. (unless (member (file-truename f) tnames)
  20105. (add-to-list 'files f 'append)
  20106. (add-to-list 'tnames (file-truename f) 'append)))
  20107. (multi-occur
  20108. (mapcar (lambda (x)
  20109. (with-current-buffer
  20110. (or (get-file-buffer x) (find-file-noselect x))
  20111. (widen)
  20112. (current-buffer)))
  20113. files)
  20114. regexp)))
  20115. (if (boundp 'occur-mode-find-occurrence-hook)
  20116. ;; Emacs 23
  20117. (add-hook 'occur-mode-find-occurrence-hook
  20118. (lambda ()
  20119. (when (derived-mode-p 'org-mode)
  20120. (org-reveal))))
  20121. ;; Emacs 22
  20122. (defadvice occur-mode-goto-occurrence
  20123. (after org-occur-reveal activate)
  20124. (and (derived-mode-p 'org-mode) (org-reveal)))
  20125. (defadvice occur-mode-goto-occurrence-other-window
  20126. (after org-occur-reveal activate)
  20127. (and (derived-mode-p 'org-mode) (org-reveal)))
  20128. (defadvice occur-mode-display-occurrence
  20129. (after org-occur-reveal activate)
  20130. (when (derived-mode-p 'org-mode)
  20131. (let ((pos (occur-mode-find-occurrence)))
  20132. (with-current-buffer (marker-buffer pos)
  20133. (save-excursion
  20134. (goto-char pos)
  20135. (org-reveal)))))))
  20136. (defun org-occur-link-in-agenda-files ()
  20137. "Create a link and search for it in the agendas.
  20138. The link is not stored in `org-stored-links', it is just created
  20139. for the search purpose."
  20140. (interactive)
  20141. (let ((link (condition-case nil
  20142. (org-store-link nil)
  20143. (error "Unable to create a link to here"))))
  20144. (org-occur-in-agenda-files (regexp-quote link))))
  20145. (defun org-reverse-string (string)
  20146. "Return the reverse of STRING."
  20147. (apply 'string (reverse (string-to-list string))))
  20148. (defun org-uniquify-alist (alist)
  20149. "Merge elements of ALIST with the same key.
  20150. For example, in this alist:
  20151. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20152. => '((a 1 3) (b 2))
  20153. merge (a 1) and (a 3) into (a 1 3).
  20154. The function returns the new ALIST."
  20155. (let (rtn)
  20156. (mapc
  20157. (lambda (e)
  20158. (let (n)
  20159. (if (not (assoc (car e) rtn))
  20160. (push e rtn)
  20161. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20162. (setq rtn (assq-delete-all (car e) rtn))
  20163. (push n rtn))))
  20164. alist)
  20165. rtn))
  20166. (defun org-delete-all (elts list)
  20167. "Remove all elements in ELTS from LIST."
  20168. (while elts
  20169. (setq list (delete (pop elts) list)))
  20170. list)
  20171. (defun org-count (cl-item cl-seq)
  20172. "Count the number of occurrences of ITEM in SEQ.
  20173. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20174. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20175. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20176. (while (< cl-start cl-end)
  20177. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20178. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20179. (setq cl-start (1+ cl-start)))
  20180. cl-count))
  20181. (defun org-remove-if (predicate seq)
  20182. "Remove everything from SEQ that fulfills PREDICATE."
  20183. (let (res e)
  20184. (while seq
  20185. (setq e (pop seq))
  20186. (if (not (funcall predicate e)) (push e res)))
  20187. (nreverse res)))
  20188. (defun org-remove-if-not (predicate seq)
  20189. "Remove everything from SEQ that does not fulfill PREDICATE."
  20190. (let (res e)
  20191. (while seq
  20192. (setq e (pop seq))
  20193. (if (funcall predicate e) (push e res)))
  20194. (nreverse res)))
  20195. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20196. "Reduce two-argument FUNCTION across SEQ.
  20197. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20198. \":initial-value\" removed."
  20199. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20200. (cadr (memq :initial-value cl-keys)))
  20201. (cl-seq (pop cl-seq))
  20202. (t (funcall cl-func)))))
  20203. (while cl-seq
  20204. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20205. cl-accum))
  20206. (defun org-every (pred seq)
  20207. "Return true if PREDICATE is true of every element of SEQ.
  20208. Adapted from `every' in cl.el."
  20209. (catch 'org-every
  20210. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20211. t))
  20212. (defun org-some (pred seq)
  20213. "Return true if PREDICATE is true of any element of SEQ.
  20214. Adapted from `some' in cl.el."
  20215. (catch 'org-some
  20216. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20217. nil))
  20218. (defun org-back-over-empty-lines ()
  20219. "Move backwards over whitespace, to the beginning of the first empty line.
  20220. Returns the number of empty lines passed."
  20221. (let ((pos (point)))
  20222. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20223. (skip-chars-backward " \t\n\r")
  20224. (unless (eobp)
  20225. (forward-line -1)))
  20226. (beginning-of-line 2)
  20227. (goto-char (min (point) pos))
  20228. (count-lines (point) pos)))
  20229. (defun org-skip-whitespace ()
  20230. (skip-chars-forward " \t\n\r"))
  20231. (defun org-point-in-group (point group &optional context)
  20232. "Check if POINT is in match-group GROUP.
  20233. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20234. match. If the match group does not exist or point is not inside it,
  20235. return nil."
  20236. (and (match-beginning group)
  20237. (>= point (match-beginning group))
  20238. (<= point (match-end group))
  20239. (if context
  20240. (list context (match-beginning group) (match-end group))
  20241. t)))
  20242. (defun org-switch-to-buffer-other-window (&rest args)
  20243. "Switch to buffer in a second window on the current frame.
  20244. In particular, do not allow pop-up frames.
  20245. Returns the newly created buffer."
  20246. (org-no-popups
  20247. (apply 'switch-to-buffer-other-window args)))
  20248. (defun org-combine-plists (&rest plists)
  20249. "Create a single property list from all plists in PLISTS.
  20250. The process starts by copying the first list, and then setting properties
  20251. from the other lists. Settings in the last list are the most significant
  20252. ones and overrule settings in the other lists."
  20253. (let ((rtn (copy-sequence (pop plists)))
  20254. p v ls)
  20255. (while plists
  20256. (setq ls (pop plists))
  20257. (while ls
  20258. (setq p (pop ls) v (pop ls))
  20259. (setq rtn (plist-put rtn p v))))
  20260. rtn))
  20261. (defun org-replace-escapes (string table)
  20262. "Replace %-escapes in STRING with values in TABLE.
  20263. TABLE is an association list with keys like \"%a\" and string values.
  20264. The sequences in STRING may contain normal field width and padding information,
  20265. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20266. so values can contain further %-escapes if they are define later in TABLE."
  20267. (let ((tbl (copy-alist table))
  20268. (case-fold-search nil)
  20269. (pchg 0)
  20270. e re rpl)
  20271. (while (setq e (pop tbl))
  20272. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20273. (when (and (cdr e) (string-match re (cdr e)))
  20274. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20275. (safe "SREF"))
  20276. (add-text-properties 0 3 (list 'sref sref) safe)
  20277. (setcdr e (replace-match safe t t (cdr e)))))
  20278. (while (string-match re string)
  20279. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20280. (cdr e)))
  20281. (setq string (replace-match rpl t t string))))
  20282. (while (setq pchg (next-property-change pchg string))
  20283. (let ((sref (get-text-property pchg 'sref string)))
  20284. (when (and sref (string-match "SREF" string pchg))
  20285. (setq string (replace-match sref t t string)))))
  20286. string))
  20287. (defun org-sublist (list start end)
  20288. "Return a section of LIST, from START to END.
  20289. Counting starts at 1."
  20290. (let (rtn (c start))
  20291. (setq list (nthcdr (1- start) list))
  20292. (while (and list (<= c end))
  20293. (push (pop list) rtn)
  20294. (setq c (1+ c)))
  20295. (nreverse rtn)))
  20296. (defun org-find-base-buffer-visiting (file)
  20297. "Like `find-buffer-visiting' but always return the base buffer and
  20298. not an indirect buffer."
  20299. (let ((buf (or (get-file-buffer file)
  20300. (find-buffer-visiting file))))
  20301. (if buf
  20302. (or (buffer-base-buffer buf) buf)
  20303. nil)))
  20304. (defun org-image-file-name-regexp (&optional extensions)
  20305. "Return regexp matching the file names of images.
  20306. If EXTENSIONS is given, only match these."
  20307. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20308. (image-file-name-regexp)
  20309. (let ((image-file-name-extensions
  20310. (or extensions
  20311. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20312. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20313. (concat "\\."
  20314. (regexp-opt (nconc (mapcar 'upcase
  20315. image-file-name-extensions)
  20316. image-file-name-extensions)
  20317. t)
  20318. "\\'"))))
  20319. (defun org-file-image-p (file &optional extensions)
  20320. "Return non-nil if FILE is an image."
  20321. (save-match-data
  20322. (string-match (org-image-file-name-regexp extensions) file)))
  20323. (defun org-get-cursor-date (&optional with-time)
  20324. "Return the date at cursor in as a time.
  20325. This works in the calendar and in the agenda, anywhere else it just
  20326. returns the current time.
  20327. If WITH-TIME is non-nil, returns the time of the event at point (in
  20328. the agenda) or the current time of the day."
  20329. (let (date day defd tp tm hod mod)
  20330. (when with-time
  20331. (setq tp (get-text-property (point) 'time))
  20332. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20333. (setq hod (string-to-number (match-string 1 tp))
  20334. mod (string-to-number (match-string 2 tp))))
  20335. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20336. mod (nth 1 (decode-time (current-time))))))
  20337. (cond
  20338. ((eq major-mode 'calendar-mode)
  20339. (setq date (calendar-cursor-to-date)
  20340. defd (encode-time 0 (or mod 0) (or hod 0)
  20341. (nth 1 date) (nth 0 date) (nth 2 date))))
  20342. ((eq major-mode 'org-agenda-mode)
  20343. (setq day (get-text-property (point) 'day))
  20344. (if day
  20345. (setq date (calendar-gregorian-from-absolute day)
  20346. defd (encode-time 0 (or mod 0) (or hod 0)
  20347. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20348. (or defd (current-time))))
  20349. (defun org-mark-subtree (&optional up)
  20350. "Mark the current subtree.
  20351. This puts point at the start of the current subtree, and mark at
  20352. the end. If a numeric prefix UP is given, move up into the
  20353. hierarchy of headlines by UP levels before marking the subtree."
  20354. (interactive "P")
  20355. (org-with-limited-levels
  20356. (cond ((org-at-heading-p) (beginning-of-line))
  20357. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20358. (t (outline-previous-visible-heading 1))))
  20359. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20360. (if (org-called-interactively-p 'any)
  20361. (call-interactively 'org-mark-element)
  20362. (org-mark-element)))
  20363. ;;; Indentation
  20364. (defun org-indent-line ()
  20365. "Indent line depending on context."
  20366. (interactive)
  20367. (let* ((pos (point))
  20368. (itemp (org-at-item-p))
  20369. (case-fold-search t)
  20370. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20371. (inline-task-p (and (featurep 'org-inlinetask)
  20372. (org-inlinetask-in-task-p)))
  20373. (inline-re (and inline-task-p
  20374. (org-inlinetask-outline-regexp)))
  20375. column)
  20376. (if (and orgstruct-is-++ (eq pos (point)))
  20377. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20378. (indent-according-to-mode))
  20379. (beginning-of-line 1)
  20380. (cond
  20381. ;; Headings
  20382. ((looking-at org-outline-regexp) (setq column 0))
  20383. ;; Footnote definition
  20384. ((looking-at org-footnote-definition-re) (setq column 0))
  20385. ;; Literal examples
  20386. ((looking-at "[ \t]*:\\( \\|$\\)")
  20387. (setq column (org-get-indentation))) ; do nothing
  20388. ;; Lists
  20389. ((ignore-errors (goto-char (org-in-item-p)))
  20390. (setq column (if itemp
  20391. (org-get-indentation)
  20392. (org-list-item-body-column (point))))
  20393. (goto-char pos))
  20394. ;; Drawers
  20395. ((and (looking-at "[ \t]*:END:")
  20396. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20397. (save-excursion
  20398. (goto-char (1- (match-beginning 1)))
  20399. (setq column (current-column))))
  20400. ;; Special blocks
  20401. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20402. (save-excursion
  20403. (re-search-backward
  20404. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20405. (setq column (org-get-indentation (match-string 0))))
  20406. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20407. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20408. (save-excursion
  20409. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20410. (setq column
  20411. (cond ((equal (downcase (match-string 1)) "src")
  20412. ;; src blocks: let `org-edit-src-exit' handle them
  20413. (org-get-indentation))
  20414. ((equal (downcase (match-string 1)) "example")
  20415. (max (org-get-indentation)
  20416. (org-get-indentation (match-string 0))))
  20417. (t
  20418. (org-get-indentation (match-string 0))))))
  20419. ;; This line has nothing special, look at the previous relevant
  20420. ;; line to compute indentation
  20421. (t
  20422. (beginning-of-line 0)
  20423. (while (and (not (bobp))
  20424. (not (looking-at org-table-line-regexp))
  20425. (not (looking-at org-drawer-regexp))
  20426. ;; When point started in an inline task, do not move
  20427. ;; above task starting line.
  20428. (not (and inline-task-p (looking-at inline-re)))
  20429. ;; Skip drawers, blocks, empty lines, verbatim,
  20430. ;; comments, tables, footnotes definitions, lists,
  20431. ;; inline tasks.
  20432. (or (and (looking-at "[ \t]*:END:")
  20433. (re-search-backward org-drawer-regexp nil t))
  20434. (and (looking-at "[ \t]*#\\+end_")
  20435. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20436. (looking-at "[ \t]*[\n:#|]")
  20437. (looking-at org-footnote-definition-re)
  20438. (and (not inline-task-p)
  20439. (featurep 'org-inlinetask)
  20440. (org-inlinetask-in-task-p)
  20441. (or (org-inlinetask-goto-beginning) t))))
  20442. (beginning-of-line 0))
  20443. (cond
  20444. ;; There was a list item above.
  20445. ((ignore-errors (goto-char (org-in-item-p)))
  20446. (goto-char (org-list-get-top-point (org-list-struct)))
  20447. (setq column (org-get-indentation)))
  20448. ;; There was an heading above.
  20449. ((looking-at "\\*+[ \t]+")
  20450. (if (not org-adapt-indentation)
  20451. (setq column 0)
  20452. (goto-char (match-end 0))
  20453. (setq column (current-column))))
  20454. ;; A drawer had started and is unfinished
  20455. ((looking-at org-drawer-regexp)
  20456. (goto-char (1- (match-beginning 1)))
  20457. (setq column (current-column)))
  20458. ;; Else, nothing noticeable found: get indentation and go on.
  20459. (t (setq column (org-get-indentation))))))
  20460. ;; Now apply indentation and move cursor accordingly
  20461. (goto-char pos)
  20462. (if (<= (current-column) (current-indentation))
  20463. (org-indent-line-to column)
  20464. (save-excursion (org-indent-line-to column)))
  20465. ;; Special polishing for properties, see `org-property-format'
  20466. (setq column (current-column))
  20467. (beginning-of-line 1)
  20468. (if (looking-at org-property-re)
  20469. (replace-match (concat (match-string 4)
  20470. (format org-property-format
  20471. (match-string 1) (match-string 3)))
  20472. t t))
  20473. (org-move-to-column column))))
  20474. (defun org-indent-drawer ()
  20475. "Indent the drawer at point."
  20476. (interactive)
  20477. (let ((p (point))
  20478. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20479. (match-end 0)))
  20480. (folded
  20481. (save-excursion
  20482. (end-of-line)
  20483. (when (overlays-at (point))
  20484. (member 'invisible (overlay-properties
  20485. (car (overlays-at (point)))))))))
  20486. (when folded (org-cycle))
  20487. (indent-for-tab-command)
  20488. (while (and (move-beginning-of-line 2) (< (point) e))
  20489. (indent-for-tab-command))
  20490. (goto-char p)
  20491. (when folded (org-cycle)))
  20492. (message "Drawer at point indented"))
  20493. (defun org-indent-block ()
  20494. "Indent the block at point."
  20495. (interactive)
  20496. (let ((p (point))
  20497. (case-fold-search t)
  20498. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20499. (match-end 0)))
  20500. (folded
  20501. (save-excursion
  20502. (end-of-line)
  20503. (when (overlays-at (point))
  20504. (member 'invisible (overlay-properties
  20505. (car (overlays-at (point)))))))))
  20506. (when folded (org-cycle))
  20507. (indent-for-tab-command)
  20508. (while (and (move-beginning-of-line 2) (< (point) e))
  20509. (indent-for-tab-command))
  20510. (goto-char p)
  20511. (when folded (org-cycle)))
  20512. (message "Block at point indented"))
  20513. (defun org-indent-region (start end)
  20514. "Indent region."
  20515. (interactive "r")
  20516. (save-excursion
  20517. (let ((line-end (org-current-line end)))
  20518. (goto-char start)
  20519. (while (< (org-current-line) line-end)
  20520. (cond ((org-in-src-block-p t) (org-src-native-tab-command-maybe))
  20521. (t (call-interactively 'org-indent-line)))
  20522. (move-beginning-of-line 2)))))
  20523. ;;; Filling
  20524. ;; We use our own fill-paragraph and auto-fill functions.
  20525. ;; `org-fill-paragraph' relies on adaptive filling and context
  20526. ;; checking. Appropriate `fill-prefix' is computed with
  20527. ;; `org-adaptive-fill-function'.
  20528. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20529. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20530. ;; `org-adaptive-fill-function' value. Internally,
  20531. ;; `org-comment-line-break-function' breaks the line.
  20532. ;; `org-setup-filling' installs filling and auto-filling related
  20533. ;; variables during `org-mode' initialization.
  20534. (defvar org-element-paragraph-separate) ; org-element.el
  20535. (defun org-setup-filling ()
  20536. (require 'org-element)
  20537. ;; Prevent auto-fill from inserting unwanted new items.
  20538. (when (boundp 'fill-nobreak-predicate)
  20539. (org-set-local
  20540. 'fill-nobreak-predicate
  20541. (org-uniquify
  20542. (append fill-nobreak-predicate
  20543. '(org-fill-line-break-nobreak-p
  20544. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20545. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  20546. (org-set-local 'paragraph-start paragraph-ending)
  20547. (org-set-local 'paragraph-separate paragraph-ending))
  20548. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20549. (org-set-local 'auto-fill-inhibit-regexp nil)
  20550. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20551. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20552. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20553. (defun org-fill-line-break-nobreak-p ()
  20554. "Non-nil when a new line at point would create an Org line break."
  20555. (save-excursion
  20556. (skip-chars-backward "[ \t]")
  20557. (skip-chars-backward "\\\\")
  20558. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20559. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20560. "Non-nil when a new line at point would split a timestamp."
  20561. (and (org-at-timestamp-p t)
  20562. (not (looking-at org-ts-regexp-both))))
  20563. (declare-function message-in-body-p "message" ())
  20564. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20565. (defun org-adaptive-fill-function ()
  20566. "Compute a fill prefix for the current line.
  20567. Return fill prefix, as a string, or nil if current line isn't
  20568. meant to be filled. For convenience, if `adaptive-fill-regexp'
  20569. matches in paragraphs or comments, use it."
  20570. (catch 'exit
  20571. (when (derived-mode-p 'message-mode)
  20572. (save-excursion
  20573. (beginning-of-line)
  20574. (cond ((or (not (message-in-body-p))
  20575. (looking-at orgtbl-line-start-regexp))
  20576. (throw 'exit nil))
  20577. ((looking-at message-cite-prefix-regexp)
  20578. (throw 'exit (match-string-no-properties 0)))
  20579. ((looking-at org-outline-regexp)
  20580. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20581. (org-with-wide-buffer
  20582. (let* ((p (line-beginning-position))
  20583. (element (save-excursion
  20584. (beginning-of-line)
  20585. (or (ignore-errors (org-element-at-point))
  20586. (user-error "An element cannot be parsed line %d"
  20587. (line-number-at-pos (point))))))
  20588. (type (org-element-type element))
  20589. (post-affiliated (org-element-property :post-affiliated element)))
  20590. (unless (and post-affiliated (< p post-affiliated))
  20591. (case type
  20592. (comment
  20593. (save-excursion
  20594. (beginning-of-line)
  20595. (looking-at "[ \t]*")
  20596. (concat (match-string 0) "# ")))
  20597. (footnote-definition "")
  20598. ((item plain-list)
  20599. (make-string (org-list-item-body-column
  20600. (or post-affiliated
  20601. (org-element-property :begin element)))
  20602. ? ))
  20603. (paragraph
  20604. ;; Fill prefix is usually the same as the current line,
  20605. ;; unless the paragraph is at the beginning of an item.
  20606. (let ((parent (org-element-property :parent element)))
  20607. (save-excursion
  20608. (beginning-of-line)
  20609. (cond ((eq (org-element-type parent) 'item)
  20610. (make-string (org-list-item-body-column
  20611. (org-element-property :begin parent))
  20612. ? ))
  20613. ((and adaptive-fill-regexp
  20614. ;; Locally disable
  20615. ;; `adaptive-fill-function' to let
  20616. ;; `fill-context-prefix' handle
  20617. ;; `adaptive-fill-regexp' variable.
  20618. (let (adaptive-fill-function)
  20619. (fill-context-prefix
  20620. post-affiliated
  20621. (org-element-property :end element)))))
  20622. ((looking-at "[ \t]+") (match-string 0))
  20623. (t "")))))
  20624. (comment-block
  20625. ;; Only fill contents if P is within block boundaries.
  20626. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20627. (forward-line)
  20628. (point)))
  20629. (cend (save-excursion
  20630. (goto-char (org-element-property :end element))
  20631. (skip-chars-backward " \r\t\n")
  20632. (line-beginning-position))))
  20633. (when (and (>= p cbeg) (< p cend))
  20634. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20635. (match-string 0)
  20636. ""))))))))))
  20637. (declare-function message-goto-body "message" ())
  20638. (defvar message-cite-prefix-regexp) ; From message.el
  20639. (defun org-fill-paragraph (&optional justify)
  20640. "Fill element at point, when applicable.
  20641. This function only applies to comment blocks, comments, example
  20642. blocks and paragraphs. Also, as a special case, re-align table
  20643. when point is at one.
  20644. If JUSTIFY is non-nil (interactively, with prefix argument),
  20645. justify as well. If `sentence-end-double-space' is non-nil, then
  20646. period followed by one space does not end a sentence, so don't
  20647. break a line there. The variable `fill-column' controls the
  20648. width for filling.
  20649. For convenience, when point is at a plain list, an item or
  20650. a footnote definition, try to fill the first paragraph within."
  20651. (interactive)
  20652. (if (and (derived-mode-p 'message-mode)
  20653. (or (not (message-in-body-p))
  20654. (save-excursion (move-beginning-of-line 1)
  20655. (looking-at message-cite-prefix-regexp))))
  20656. ;; First ensure filling is correct in message-mode.
  20657. (let ((fill-paragraph-function
  20658. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20659. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20660. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20661. (paragraph-separate
  20662. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20663. (fill-paragraph nil))
  20664. (with-syntax-table org-mode-transpose-word-syntax-table
  20665. ;; Move to end of line in order to get the first paragraph
  20666. ;; within a plain list or a footnote definition.
  20667. (let ((element (save-excursion
  20668. (end-of-line)
  20669. (or (ignore-errors (org-element-at-point))
  20670. (user-error "An element cannot be parsed line %d"
  20671. (line-number-at-pos (point)))))))
  20672. ;; First check if point is in a blank line at the beginning of
  20673. ;; the buffer. In that case, ignore filling.
  20674. (case (org-element-type element)
  20675. ;; Use major mode filling function is src blocks.
  20676. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20677. ;; Align Org tables, leave table.el tables as-is.
  20678. (table-row (org-table-align) t)
  20679. (table
  20680. (when (eq (org-element-property :type element) 'org)
  20681. (save-excursion
  20682. (goto-char (org-element-property :post-affiliated element))
  20683. (org-table-align)))
  20684. t)
  20685. (paragraph
  20686. ;; Paragraphs may contain `line-break' type objects.
  20687. (let ((beg (max (point-min)
  20688. (org-element-property :contents-begin element)))
  20689. (end (min (point-max)
  20690. (org-element-property :contents-end element))))
  20691. ;; Do nothing if point is at an affiliated keyword.
  20692. (if (< (line-end-position) beg) t
  20693. (when (derived-mode-p 'message-mode)
  20694. ;; In `message-mode', do not fill following citation
  20695. ;; in current paragraph nor text before message body.
  20696. (let ((body-start (save-excursion (message-goto-body))))
  20697. (when body-start (setq beg (max body-start beg))))
  20698. (when (save-excursion
  20699. (re-search-forward
  20700. (concat "^" message-cite-prefix-regexp) end t))
  20701. (setq end (match-beginning 0))))
  20702. ;; Fill paragraph, taking line breaks into account.
  20703. ;; For that, slice the paragraph using line breaks as
  20704. ;; separators, and fill the parts in reverse order to
  20705. ;; avoid messing with markers.
  20706. (save-excursion
  20707. (goto-char end)
  20708. (mapc
  20709. (lambda (pos)
  20710. (fill-region-as-paragraph pos (point) justify)
  20711. (goto-char pos))
  20712. ;; Find the list of ending positions for line breaks
  20713. ;; in the current paragraph. Add paragraph
  20714. ;; beginning to include first slice.
  20715. (nreverse
  20716. (cons beg
  20717. (org-element-map
  20718. (org-element--parse-objects
  20719. beg end nil (org-element-restriction 'paragraph))
  20720. 'line-break
  20721. (lambda (lb) (org-element-property :end lb)))))))
  20722. t)))
  20723. ;; Contents of `comment-block' type elements should be
  20724. ;; filled as plain text, but only if point is within block
  20725. ;; markers.
  20726. (comment-block
  20727. (let* ((case-fold-search t)
  20728. (beg (save-excursion
  20729. (goto-char (org-element-property :begin element))
  20730. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20731. (forward-line)
  20732. (point)))
  20733. (end (save-excursion
  20734. (goto-char (org-element-property :end element))
  20735. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20736. (line-beginning-position))))
  20737. (if (or (< (point) beg) (> (point) end)) t
  20738. (fill-region-as-paragraph
  20739. (save-excursion (end-of-line)
  20740. (re-search-backward "^[ \t]*$" beg 'move)
  20741. (line-beginning-position))
  20742. (save-excursion (beginning-of-line)
  20743. (re-search-forward "^[ \t]*$" end 'move)
  20744. (line-beginning-position))
  20745. justify))))
  20746. ;; Fill comments.
  20747. (comment
  20748. (let ((begin (org-element-property :post-affiliated element))
  20749. (end (org-element-property :end element)))
  20750. (when (and (>= (point) begin) (<= (point) end))
  20751. (let ((begin (save-excursion
  20752. (end-of-line)
  20753. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  20754. (progn (forward-line) (point))
  20755. begin)))
  20756. (end (save-excursion
  20757. (end-of-line)
  20758. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  20759. (1- (line-beginning-position))
  20760. (skip-chars-backward " \r\t\n")
  20761. (line-end-position)))))
  20762. ;; Do not fill comments when at a blank line.
  20763. (when (> end begin)
  20764. (let ((fill-prefix
  20765. (save-excursion
  20766. (beginning-of-line)
  20767. (looking-at "[ \t]*#")
  20768. (let ((comment-prefix (match-string 0)))
  20769. (goto-char (match-end 0))
  20770. (if (looking-at adaptive-fill-regexp)
  20771. (concat comment-prefix (match-string 0))
  20772. (concat comment-prefix " "))))))
  20773. (save-excursion
  20774. (fill-region-as-paragraph begin end justify))))))
  20775. t))
  20776. ;; Ignore every other element.
  20777. (otherwise t))))))
  20778. (defun org-auto-fill-function ()
  20779. "Auto-fill function."
  20780. ;; Check if auto-filling is meaningful.
  20781. (let ((fc (current-fill-column)))
  20782. (when (and fc (> (current-column) fc))
  20783. (let* ((fill-prefix (org-adaptive-fill-function))
  20784. ;; Enforce empty fill prefix, if required. Otherwise, it
  20785. ;; will be computed again.
  20786. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20787. (when fill-prefix (do-auto-fill))))))
  20788. (defun org-comment-line-break-function (&optional soft)
  20789. "Break line at point and indent, continuing comment if within one.
  20790. The inserted newline is marked hard if variable
  20791. `use-hard-newlines' is true, unless optional argument SOFT is
  20792. non-nil."
  20793. (if soft (insert-and-inherit ?\n) (newline 1))
  20794. (save-excursion (forward-char -1) (delete-horizontal-space))
  20795. (delete-horizontal-space)
  20796. (indent-to-left-margin)
  20797. (insert-before-markers-and-inherit fill-prefix))
  20798. ;;; Fixed Width Areas
  20799. (defun org-toggle-fixed-width ()
  20800. "Toggle fixed-width markup.
  20801. Add or remove fixed-width markup on current line, whenever it
  20802. makes sense. Return an error otherwise.
  20803. If a region is active and if it contains only fixed-width areas
  20804. or blank lines, remove all fixed-width markup in it. If the
  20805. region contains anything else, convert all non-fixed-width lines
  20806. to fixed-width ones.
  20807. Blank lines at the end of the region are ignored unless the
  20808. region only contains such lines."
  20809. (interactive)
  20810. (if (not (org-region-active-p))
  20811. ;; No region:
  20812. ;;
  20813. ;; Remove fixed width marker only in a fixed-with element.
  20814. ;;
  20815. ;; Add fixed width maker in paragraphs, in blank lines after
  20816. ;; elements or at the beginning of a headline or an inlinetask,
  20817. ;; and before any one-line elements (e.g., a clock).
  20818. (progn
  20819. (beginning-of-line)
  20820. (let* ((element (org-element-at-point))
  20821. (type (org-element-type element)))
  20822. (cond
  20823. ((and (eq type 'fixed-width)
  20824. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  20825. (replace-match
  20826. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  20827. ((and (memq type '(babel-call clock comment diary-sexp headline
  20828. horizontal-rule keyword paragraph
  20829. planning))
  20830. (or (not (org-element-property :post-affiliated element))
  20831. (<= (org-element-property :post-affiliated element)
  20832. (point))))
  20833. (skip-chars-forward " \t")
  20834. (insert ": "))
  20835. ((and (org-looking-at-p "[ \t]*$")
  20836. (or (eq type 'inlinetask)
  20837. (save-excursion
  20838. (skip-chars-forward " \r\t\n")
  20839. (<= (org-element-property :end element) (point)))))
  20840. (delete-region (point) (line-end-position))
  20841. (org-indent-line)
  20842. (insert ": "))
  20843. (t (user-error "Cannot insert a fixed-width line here")))))
  20844. ;; Region active.
  20845. (let* ((begin (save-excursion
  20846. (goto-char (region-beginning))
  20847. (line-beginning-position)))
  20848. (end (copy-marker
  20849. (save-excursion
  20850. (goto-char (region-end))
  20851. (unless (eolp) (beginning-of-line))
  20852. (if (save-excursion (re-search-backward "\\S-" begin t))
  20853. (progn (skip-chars-backward " \r\t\n") (point))
  20854. (point)))))
  20855. (all-fixed-width-p
  20856. (catch 'not-all-p
  20857. (save-excursion
  20858. (goto-char begin)
  20859. (skip-chars-forward " \r\t\n")
  20860. (when (eobp) (throw 'not-all-p nil))
  20861. (while (< (point) end)
  20862. (let ((element (org-element-at-point)))
  20863. (if (eq (org-element-type element) 'fixed-width)
  20864. (goto-char (org-element-property :end element))
  20865. (throw 'not-all-p nil))))
  20866. t))))
  20867. (if all-fixed-width-p
  20868. (save-excursion
  20869. (goto-char begin)
  20870. (while (< (point) end)
  20871. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20872. (replace-match
  20873. "" nil nil nil
  20874. (if (= (line-end-position) (match-end 0)) 0 1)))
  20875. (forward-line)))
  20876. (let ((min-ind (point-max)))
  20877. ;; Find minimum indentation across all lines.
  20878. (save-excursion
  20879. (goto-char begin)
  20880. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  20881. (setq min-ind 0)
  20882. (catch 'zerop
  20883. (while (< (point) end)
  20884. (unless (org-looking-at-p "[ \t]*$")
  20885. (let ((ind (org-get-indentation)))
  20886. (setq min-ind (min min-ind ind))
  20887. (when (zerop ind) (throw 'zerop t))))
  20888. (forward-line)))))
  20889. ;; Loop over all lines and add fixed-width markup everywhere
  20890. ;; but in fixed-width lines.
  20891. (save-excursion
  20892. (goto-char begin)
  20893. (while (< (point) end)
  20894. (cond
  20895. ((org-at-heading-p)
  20896. (insert ": ")
  20897. (forward-line)
  20898. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  20899. (insert ":")
  20900. (forward-line)))
  20901. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  20902. (let* ((element (org-element-at-point))
  20903. (element-end (org-element-property :end element)))
  20904. (if (eq (org-element-type element) 'fixed-width)
  20905. (progn (goto-char element-end)
  20906. (skip-chars-backward " \r\t\n")
  20907. (forward-line))
  20908. (let ((limit (min end element-end)))
  20909. (while (< (point) limit)
  20910. (org-move-to-column min-ind t)
  20911. (insert ": ")
  20912. (forward-line))))))
  20913. (t
  20914. (org-move-to-column min-ind t)
  20915. (insert ": ")
  20916. (forward-line)))))))
  20917. (set-marker end nil))))
  20918. ;;; Comments
  20919. ;; Org comments syntax is quite complex. It requires the entire line
  20920. ;; to be just a comment. Also, even with the right syntax at the
  20921. ;; beginning of line, some some elements (i.e. verse-block or
  20922. ;; example-block) don't accept comments. Usual Emacs comment commands
  20923. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20924. ;; Org still relies on `comment-dwim', but cannot trust
  20925. ;; `comment-only-p'. So, `comment-region-function' and
  20926. ;; `uncomment-region-function' both point
  20927. ;; to`org-comment-or-uncomment-region'. Eventually,
  20928. ;; `org-insert-comment' takes care of insertion of comments at the
  20929. ;; beginning of line.
  20930. ;; `org-setup-comments-handling' install comments related variables
  20931. ;; during `org-mode' initialization.
  20932. (defun org-setup-comments-handling ()
  20933. (interactive)
  20934. (org-set-local 'comment-use-syntax nil)
  20935. (org-set-local 'comment-start "# ")
  20936. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20937. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20938. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20939. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20940. (defun org-insert-comment ()
  20941. "Insert an empty comment above current line.
  20942. If the line is empty, insert comment at its beginning. When
  20943. point is within a source block, comment according to the related
  20944. major mode."
  20945. (if (let ((element (org-element-at-point)))
  20946. (and (eq (org-element-type element) 'src-block)
  20947. (< (save-excursion
  20948. (goto-char (org-element-property :post-affiliated element))
  20949. (line-end-position))
  20950. (point))
  20951. (> (save-excursion
  20952. (goto-char (org-element-property :end element))
  20953. (skip-chars-backward " \r\t\n")
  20954. (line-beginning-position))
  20955. (point))))
  20956. (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim))
  20957. (beginning-of-line)
  20958. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  20959. (open-line 1))
  20960. (org-indent-line)
  20961. (insert "# ")))
  20962. (defvar comment-empty-lines) ; From newcomment.el.
  20963. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20964. "Comment or uncomment each non-blank line in the region.
  20965. Uncomment each non-blank line between BEG and END if it only
  20966. contains commented lines. Otherwise, comment them. If region is
  20967. strictly within a source block, use appropriate comment syntax."
  20968. (if (let ((element (org-element-at-point)))
  20969. (and (eq (org-element-type element) 'src-block)
  20970. (< (save-excursion
  20971. (goto-char (org-element-property :post-affiliated element))
  20972. (line-end-position))
  20973. beg)
  20974. (>= (save-excursion
  20975. (goto-char (org-element-property :end element))
  20976. (skip-chars-backward " \r\t\n")
  20977. (line-beginning-position))
  20978. end)))
  20979. (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim))
  20980. (save-restriction
  20981. ;; Restrict region
  20982. (narrow-to-region (save-excursion (goto-char beg)
  20983. (skip-chars-forward " \r\t\n" end)
  20984. (line-beginning-position))
  20985. (save-excursion (goto-char end)
  20986. (skip-chars-backward " \r\t\n" beg)
  20987. (line-end-position)))
  20988. (let ((uncommentp
  20989. ;; UNCOMMENTP is non-nil when every non blank line between
  20990. ;; BEG and END is a comment.
  20991. (save-excursion
  20992. (goto-char (point-min))
  20993. (while (and (not (eobp))
  20994. (let ((element (org-element-at-point)))
  20995. (and (eq (org-element-type element) 'comment)
  20996. (goto-char (min (point-max)
  20997. (org-element-property
  20998. :end element)))))))
  20999. (eobp))))
  21000. (if uncommentp
  21001. ;; Only blank lines and comments in region: uncomment it.
  21002. (save-excursion
  21003. (goto-char (point-min))
  21004. (while (not (eobp))
  21005. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21006. (replace-match "" nil nil nil 1))
  21007. (forward-line)))
  21008. ;; Comment each line in region.
  21009. (let ((min-indent (point-max)))
  21010. ;; First find the minimum indentation across all lines.
  21011. (save-excursion
  21012. (goto-char (point-min))
  21013. (while (and (not (eobp)) (not (zerop min-indent)))
  21014. (unless (looking-at "[ \t]*$")
  21015. (setq min-indent (min min-indent (current-indentation))))
  21016. (forward-line)))
  21017. ;; Then loop over all lines.
  21018. (save-excursion
  21019. (goto-char (point-min))
  21020. (while (not (eobp))
  21021. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21022. ;; Don't get fooled by invisible text (e.g. link path)
  21023. ;; when moving to column MIN-INDENT.
  21024. (let ((buffer-invisibility-spec nil))
  21025. (org-move-to-column min-indent t))
  21026. (insert comment-start))
  21027. (forward-line)))))))))
  21028. (defun org-comment-dwim (arg)
  21029. (interactive "*P")
  21030. "Call `comment-dwim' within a source edit buffer if needed."
  21031. (if (org-in-src-block-p)
  21032. (org-babel-do-in-edit-buffer (call-interactively #'comment-dwim))
  21033. (call-interactively #'comment-dwim)))
  21034. ;;; Planning
  21035. ;; This section contains tools to operate on timestamp objects, as
  21036. ;; returned by, e.g. `org-element-context'.
  21037. (defun org-timestamp-has-time-p (timestamp)
  21038. "Non-nil when TIMESTAMP has a time specified."
  21039. (org-element-property :hour-start timestamp))
  21040. (defun org-timestamp-format (timestamp format &optional end utc)
  21041. "Format a TIMESTAMP element into a string.
  21042. FORMAT is a format specifier to be passed to
  21043. `format-time-string'.
  21044. When optional argument END is non-nil, use end of date-range or
  21045. time-range, if possible.
  21046. When optional argument UTC is non-nil, time will be expressed as
  21047. Universal Time."
  21048. (format-time-string
  21049. format
  21050. (apply 'encode-time
  21051. (cons 0
  21052. (mapcar
  21053. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21054. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21055. '(:minute-start :hour-start :day-start :month-start
  21056. :year-start)))))
  21057. utc))
  21058. (defun org-timestamp-split-range (timestamp &optional end)
  21059. "Extract a timestamp object from a date or time range.
  21060. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  21061. the end of the range. Otherwise, extract its start.
  21062. Return a new timestamp object sharing the same parent as
  21063. TIMESTAMP."
  21064. (let ((type (org-element-property :type timestamp)))
  21065. (if (memq type '(active inactive diary)) timestamp
  21066. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  21067. ;; Set new type.
  21068. (org-element-put-property
  21069. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21070. ;; Copy start properties over end properties if END is
  21071. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21072. (let ((p-alist '((:minute-start . :minute-end)
  21073. (:hour-start . :hour-end)
  21074. (:day-start . :day-end)
  21075. (:month-start . :month-end)
  21076. (:year-start . :year-end))))
  21077. (dolist (p-cell p-alist)
  21078. (org-element-put-property
  21079. split-ts
  21080. (funcall (if end 'car 'cdr) p-cell)
  21081. (org-element-property
  21082. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  21083. ;; Eventually refresh `:raw-value'.
  21084. (org-element-put-property split-ts :raw-value nil)
  21085. (org-element-put-property
  21086. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21087. (defun org-timestamp-translate (timestamp &optional boundary)
  21088. "Apply `org-translate-time' on a TIMESTAMP object.
  21089. When optional argument BOUNDARY is non-nil, it is either the
  21090. symbol `start' or `end'. In this case, only translate the
  21091. starting or ending part of TIMESTAMP if it is a date or time
  21092. range. Otherwise, translate both parts."
  21093. (if (and (not boundary)
  21094. (memq (org-element-property :type timestamp)
  21095. '(active-range inactive-range)))
  21096. (concat
  21097. (org-translate-time
  21098. (org-element-property :raw-value
  21099. (org-timestamp-split-range timestamp)))
  21100. "--"
  21101. (org-translate-time
  21102. (org-element-property :raw-value
  21103. (org-timestamp-split-range timestamp t))))
  21104. (org-translate-time
  21105. (org-element-property
  21106. :raw-value
  21107. (if (not boundary) timestamp
  21108. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  21109. ;;; Other stuff.
  21110. (defun org-reftex-citation ()
  21111. "Use reftex-citation to insert a citation into the buffer.
  21112. This looks for a line like
  21113. #+BIBLIOGRAPHY: foo plain option:-d
  21114. and derives from it that foo.bib is the bibliography file relevant
  21115. for this document. It then installs the necessary environment for RefTeX
  21116. to work in this buffer and calls `reftex-citation' to insert a citation
  21117. into the buffer.
  21118. Export of such citations to both LaTeX and HTML is handled by the contributed
  21119. package ox-bibtex by Taru Karttunen."
  21120. (interactive)
  21121. (let ((reftex-docstruct-symbol 'rds)
  21122. (reftex-cite-format "\\cite{%l}")
  21123. rds bib)
  21124. (save-excursion
  21125. (save-restriction
  21126. (widen)
  21127. (let ((case-fold-search t)
  21128. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  21129. (if (not (save-excursion
  21130. (or (re-search-forward re nil t)
  21131. (re-search-backward re nil t))))
  21132. (error "No bibliography defined in file")
  21133. (setq bib (concat (match-string 1) ".bib")
  21134. rds (list (list 'bib bib)))))))
  21135. (call-interactively 'reftex-citation)))
  21136. ;;;; Functions extending outline functionality
  21137. (defun org-beginning-of-line (&optional arg)
  21138. "Go to the beginning of the current line. If that is invisible, continue
  21139. to a visible line beginning. This makes the function of C-a more intuitive.
  21140. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21141. first attempt, and only move to after the tags when the cursor is already
  21142. beyond the end of the headline."
  21143. (interactive "P")
  21144. (let ((pos (point))
  21145. (special (if (consp org-special-ctrl-a/e)
  21146. (car org-special-ctrl-a/e)
  21147. org-special-ctrl-a/e))
  21148. deactivate-mark refpos)
  21149. (if (org-bound-and-true-p visual-line-mode)
  21150. (beginning-of-visual-line 1)
  21151. (beginning-of-line 1))
  21152. (if (and arg (fboundp 'move-beginning-of-line))
  21153. (call-interactively 'move-beginning-of-line)
  21154. (if (bobp)
  21155. nil
  21156. (backward-char 1)
  21157. (if (org-truely-invisible-p)
  21158. (while (and (not (bobp)) (org-truely-invisible-p))
  21159. (backward-char 1)
  21160. (beginning-of-line 1))
  21161. (forward-char 1))))
  21162. (when special
  21163. (cond
  21164. ((and (looking-at org-complex-heading-regexp)
  21165. (= (char-after (match-end 1)) ?\ ))
  21166. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21167. (point-at-eol)))
  21168. (goto-char
  21169. (if (eq special t)
  21170. (cond ((> pos refpos) refpos)
  21171. ((= pos (point)) refpos)
  21172. (t (point)))
  21173. (cond ((> pos (point)) (point))
  21174. ((not (eq last-command this-command)) (point))
  21175. (t refpos)))))
  21176. ((org-at-item-p)
  21177. ;; Being at an item and not looking at an the item means point
  21178. ;; was previously moved to beginning of a visual line, which
  21179. ;; doesn't contain the item. Therefore, do nothing special,
  21180. ;; just stay here.
  21181. (when (looking-at org-list-full-item-re)
  21182. ;; Set special position at first white space character after
  21183. ;; bullet, and check-box, if any.
  21184. (let ((after-bullet
  21185. (let ((box (match-end 3)))
  21186. (if (not box) (match-end 1)
  21187. (let ((after (char-after box)))
  21188. (if (and after (= after ? )) (1+ box) box))))))
  21189. ;; Special case: Move point to special position when
  21190. ;; currently after it or at beginning of line.
  21191. (if (eq special t)
  21192. (when (or (> pos after-bullet) (= (point) pos))
  21193. (goto-char after-bullet))
  21194. ;; Reversed case: Move point to special position when
  21195. ;; point was already at beginning of line and command is
  21196. ;; repeated.
  21197. (when (and (= (point) pos) (eq last-command this-command))
  21198. (goto-char after-bullet))))))))
  21199. (org-no-warnings
  21200. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21201. (setq disable-point-adjustment
  21202. (or (not (invisible-p (point)))
  21203. (not (invisible-p (max (point-min) (1- (point))))))))
  21204. (defun org-end-of-line (&optional arg)
  21205. "Go to the end of the line.
  21206. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21207. tags on the first attempt, and only move to after the tags when
  21208. the cursor is already beyond the end of the headline."
  21209. (interactive "P")
  21210. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21211. org-special-ctrl-a/e))
  21212. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21213. 'end-of-visual-line)
  21214. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21215. (t 'end-of-line)))
  21216. deactivate-mark)
  21217. (if (or (not special) arg) (call-interactively move-fun)
  21218. (let* ((element (save-excursion (beginning-of-line)
  21219. (org-element-at-point)))
  21220. (type (org-element-type element)))
  21221. (cond
  21222. ((memq type '(headline inlinetask))
  21223. (let ((pos (point)))
  21224. (beginning-of-line 1)
  21225. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21226. (if (eq special t)
  21227. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21228. (goto-char (match-beginning 1))
  21229. (goto-char (match-end 0)))
  21230. (if (or (< pos (match-end 0))
  21231. (not (eq this-command last-command)))
  21232. (goto-char (match-end 0))
  21233. (goto-char (match-beginning 1))))
  21234. (call-interactively move-fun))))
  21235. ((outline-invisible-p (line-end-position))
  21236. ;; If element is hidden, `move-end-of-line' would put point
  21237. ;; after it. Use `end-of-line' to stay on current line.
  21238. (call-interactively 'end-of-line))
  21239. (t (call-interactively move-fun)))))
  21240. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21241. (setq disable-point-adjustment
  21242. (or (not (invisible-p (point)))
  21243. (not (invisible-p (max (point-min) (1- (point))))))))
  21244. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21245. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21246. (defun org-backward-sentence (&optional arg)
  21247. "Go to beginning of sentence, or beginning of table field.
  21248. This will call `backward-sentence' or `org-table-beginning-of-field',
  21249. depending on context."
  21250. (interactive "P")
  21251. (cond
  21252. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  21253. (t (call-interactively 'backward-sentence))))
  21254. (defun org-forward-sentence (&optional arg)
  21255. "Go to end of sentence, or end of table field.
  21256. This will call `forward-sentence' or `org-table-end-of-field',
  21257. depending on context."
  21258. (interactive "P")
  21259. (cond
  21260. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  21261. (t (call-interactively 'forward-sentence))))
  21262. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21263. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21264. (defun org-kill-line (&optional arg)
  21265. "Kill line, to tags or end of line."
  21266. (interactive "P")
  21267. (cond
  21268. ((or (not org-special-ctrl-k)
  21269. (bolp)
  21270. (not (org-at-heading-p)))
  21271. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21272. org-ctrl-k-protect-subtree)
  21273. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21274. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21275. (user-error "C-k aborted as it would kill a hidden subtree")))
  21276. (call-interactively
  21277. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21278. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21279. (kill-region (point) (match-beginning 1))
  21280. (org-set-tags nil t))
  21281. (t (kill-region (point) (point-at-eol)))))
  21282. (define-key org-mode-map "\C-k" 'org-kill-line)
  21283. (defun org-yank (&optional arg)
  21284. "Yank. If the kill is a subtree, treat it specially.
  21285. This command will look at the current kill and check if is a single
  21286. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21287. cursor is at the beginning of a line or after the stars of a currently
  21288. empty headline, then the yank is handled specially. How exactly depends
  21289. on the value of the following variables, both set by default.
  21290. org-yank-folded-subtrees
  21291. When set, the subtree(s) will be folded after insertion, but only
  21292. if doing so would now swallow text after the yanked text.
  21293. org-yank-adjusted-subtrees
  21294. When set, the subtree will be promoted or demoted in order to
  21295. fit into the local outline tree structure, which means that the level
  21296. will be adjusted so that it becomes the smaller one of the two
  21297. *visible* surrounding headings.
  21298. Any prefix to this command will cause `yank' to be called directly with
  21299. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21300. will just
  21301. plainly yank the text as it is.
  21302. \[1] The test checks if the first non-white line is a heading
  21303. and if there are no other headings with fewer stars."
  21304. (interactive "P")
  21305. (org-yank-generic 'yank arg))
  21306. (defun org-yank-generic (command arg)
  21307. "Perform some yank-like command.
  21308. This function implements the behavior described in the `org-yank'
  21309. documentation. However, it has been generalized to work for any
  21310. interactive command with similar behavior."
  21311. ;; pretend to be command COMMAND
  21312. (setq this-command command)
  21313. (if arg
  21314. (call-interactively command)
  21315. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21316. (and (org-kill-is-subtree-p)
  21317. (or (bolp)
  21318. (and (looking-at "[ \t]*$")
  21319. (string-match
  21320. "\\`\\*+\\'"
  21321. (buffer-substring (point-at-bol) (point)))))))
  21322. swallowp)
  21323. (cond
  21324. ((and subtreep org-yank-folded-subtrees)
  21325. (let ((beg (point))
  21326. end)
  21327. (if (and subtreep org-yank-adjusted-subtrees)
  21328. (org-paste-subtree nil nil 'for-yank)
  21329. (call-interactively command))
  21330. (setq end (point))
  21331. (goto-char beg)
  21332. (when (and (bolp) subtreep
  21333. (not (setq swallowp
  21334. (org-yank-folding-would-swallow-text beg end))))
  21335. (org-with-limited-levels
  21336. (or (looking-at org-outline-regexp)
  21337. (re-search-forward org-outline-regexp-bol end t))
  21338. (while (and (< (point) end) (looking-at org-outline-regexp))
  21339. (hide-subtree)
  21340. (org-cycle-show-empty-lines 'folded)
  21341. (condition-case nil
  21342. (outline-forward-same-level 1)
  21343. (error (goto-char end))))))
  21344. (when swallowp
  21345. (message
  21346. "Inserted text not folded because that would swallow text"))
  21347. (goto-char end)
  21348. (skip-chars-forward " \t\n\r")
  21349. (beginning-of-line 1)
  21350. (push-mark beg 'nomsg)))
  21351. ((and subtreep org-yank-adjusted-subtrees)
  21352. (let ((beg (point-at-bol)))
  21353. (org-paste-subtree nil nil 'for-yank)
  21354. (push-mark beg 'nomsg)))
  21355. (t
  21356. (call-interactively command))))))
  21357. (defun org-yank-folding-would-swallow-text (beg end)
  21358. "Would hide-subtree at BEG swallow any text after END?"
  21359. (let (level)
  21360. (org-with-limited-levels
  21361. (save-excursion
  21362. (goto-char beg)
  21363. (when (or (looking-at org-outline-regexp)
  21364. (re-search-forward org-outline-regexp-bol end t))
  21365. (setq level (org-outline-level)))
  21366. (goto-char end)
  21367. (skip-chars-forward " \t\r\n\v\f")
  21368. (if (or (eobp)
  21369. (and (bolp) (looking-at org-outline-regexp)
  21370. (<= (org-outline-level) level)))
  21371. nil ; Nothing would be swallowed
  21372. t))))) ; something would swallow
  21373. (define-key org-mode-map "\C-y" 'org-yank)
  21374. (defun org-truely-invisible-p ()
  21375. "Check if point is at a character currently not visible.
  21376. This version does not only check the character property, but also
  21377. `visible-mode'."
  21378. ;; Early versions of noutline don't have `outline-invisible-p'.
  21379. (if (org-bound-and-true-p visible-mode)
  21380. nil
  21381. (outline-invisible-p)))
  21382. (defun org-invisible-p2 ()
  21383. "Check if point is at a character currently not visible."
  21384. (save-excursion
  21385. (if (and (eolp) (not (bobp))) (backward-char 1))
  21386. ;; Early versions of noutline don't have `outline-invisible-p'.
  21387. (outline-invisible-p)))
  21388. (defun org-back-to-heading (&optional invisible-ok)
  21389. "Call `outline-back-to-heading', but provide a better error message."
  21390. (condition-case nil
  21391. (outline-back-to-heading invisible-ok)
  21392. (error (error "Before first headline at position %d in buffer %s"
  21393. (point) (current-buffer)))))
  21394. (defun org-before-first-heading-p ()
  21395. "Before first heading?"
  21396. (save-excursion
  21397. (end-of-line)
  21398. (null (re-search-backward org-outline-regexp-bol nil t))))
  21399. (defun org-at-heading-p (&optional ignored)
  21400. (outline-on-heading-p t))
  21401. ;; Compatibility alias with Org versions < 7.8.03
  21402. (defalias 'org-on-heading-p 'org-at-heading-p)
  21403. (defun org-at-comment-p nil
  21404. "Is cursor in a line starting with a # character?"
  21405. (save-excursion
  21406. (beginning-of-line)
  21407. (looking-at "^#")))
  21408. (defun org-at-drawer-p nil
  21409. "Is cursor at a drawer keyword?"
  21410. (save-excursion
  21411. (move-beginning-of-line 1)
  21412. (looking-at org-drawer-regexp)))
  21413. (defun org-at-block-p nil
  21414. "Is cursor at a block keyword?"
  21415. (save-excursion
  21416. (move-beginning-of-line 1)
  21417. (looking-at org-block-regexp)))
  21418. (defun org-point-at-end-of-empty-headline ()
  21419. "If point is at the end of an empty headline, return t, else nil.
  21420. If the heading only contains a TODO keyword, it is still still considered
  21421. empty."
  21422. (and (looking-at "[ \t]*$")
  21423. (when org-todo-line-regexp
  21424. (save-excursion
  21425. (beginning-of-line 1)
  21426. (let ((case-fold-search nil))
  21427. (looking-at org-todo-line-regexp)
  21428. (string= (match-string 3) ""))))))
  21429. (defun org-at-heading-or-item-p ()
  21430. (or (org-at-heading-p) (org-at-item-p)))
  21431. (defun org-at-target-p ()
  21432. (or (org-in-regexp org-radio-target-regexp)
  21433. (org-in-regexp org-target-regexp)))
  21434. ;; Compatibility alias with Org versions < 7.8.03
  21435. (defalias 'org-on-target-p 'org-at-target-p)
  21436. (defun org-up-heading-all (arg)
  21437. "Move to the heading line of which the present line is a subheading.
  21438. This function considers both visible and invisible heading lines.
  21439. With argument, move up ARG levels."
  21440. (if (fboundp 'outline-up-heading-all)
  21441. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21442. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21443. (defun org-up-heading-safe ()
  21444. "Move to the heading line of which the present line is a subheading.
  21445. This version will not throw an error. It will return the level of the
  21446. headline found, or nil if no higher level is found.
  21447. Also, this function will be a lot faster than `outline-up-heading',
  21448. because it relies on stars being the outline starters. This can really
  21449. make a significant difference in outlines with very many siblings."
  21450. (let (start-level re)
  21451. (org-back-to-heading t)
  21452. (setq start-level (funcall outline-level))
  21453. (if (equal start-level 1)
  21454. nil
  21455. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21456. (if (re-search-backward re nil t)
  21457. (funcall outline-level)))))
  21458. (defun org-first-sibling-p ()
  21459. "Is this heading the first child of its parents?"
  21460. (interactive)
  21461. (let ((re org-outline-regexp-bol)
  21462. level l)
  21463. (unless (org-at-heading-p t)
  21464. (user-error "Not at a heading"))
  21465. (setq level (funcall outline-level))
  21466. (save-excursion
  21467. (if (not (re-search-backward re nil t))
  21468. t
  21469. (setq l (funcall outline-level))
  21470. (< l level)))))
  21471. (defun org-goto-sibling (&optional previous)
  21472. "Goto the next sibling, even if it is invisible.
  21473. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21474. when a sibling was found. When none is found, return nil and don't
  21475. move point."
  21476. (let ((fun (if previous 're-search-backward 're-search-forward))
  21477. (pos (point))
  21478. (re org-outline-regexp-bol)
  21479. level l)
  21480. (when (condition-case nil (org-back-to-heading t) (error nil))
  21481. (setq level (funcall outline-level))
  21482. (catch 'exit
  21483. (or previous (forward-char 1))
  21484. (while (funcall fun re nil t)
  21485. (setq l (funcall outline-level))
  21486. (when (< l level) (goto-char pos) (throw 'exit nil))
  21487. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21488. (goto-char pos)
  21489. nil))))
  21490. (defun org-show-siblings ()
  21491. "Show all siblings of the current headline."
  21492. (save-excursion
  21493. (while (org-goto-sibling) (org-flag-heading nil)))
  21494. (save-excursion
  21495. (while (org-goto-sibling 'previous)
  21496. (org-flag-heading nil))))
  21497. (defun org-goto-first-child ()
  21498. "Goto the first child, even if it is invisible.
  21499. Return t when a child was found. Otherwise don't move point and
  21500. return nil."
  21501. (let (level (pos (point)) (re org-outline-regexp-bol))
  21502. (when (condition-case nil (org-back-to-heading t) (error nil))
  21503. (setq level (outline-level))
  21504. (forward-char 1)
  21505. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21506. (progn (goto-char (match-beginning 0)) t)
  21507. (goto-char pos) nil))))
  21508. (defun org-show-hidden-entry ()
  21509. "Show an entry where even the heading is hidden."
  21510. (save-excursion
  21511. (org-show-entry)))
  21512. (defun org-flag-heading (flag &optional entry)
  21513. "Flag the current heading. FLAG non-nil means make invisible.
  21514. When ENTRY is non-nil, show the entire entry."
  21515. (save-excursion
  21516. (org-back-to-heading t)
  21517. ;; Check if we should show the entire entry
  21518. (if entry
  21519. (progn
  21520. (org-show-entry)
  21521. (save-excursion
  21522. (and (outline-next-heading)
  21523. (org-flag-heading nil))))
  21524. (outline-flag-region (max (point-min) (1- (point)))
  21525. (save-excursion (outline-end-of-heading) (point))
  21526. flag))))
  21527. (defun org-get-next-sibling ()
  21528. "Move to next heading of the same level, and return point.
  21529. If there is no such heading, return nil.
  21530. This is like outline-next-sibling, but invisible headings are ok."
  21531. (let ((level (funcall outline-level)))
  21532. (outline-next-heading)
  21533. (while (and (not (eobp)) (> (funcall outline-level) level))
  21534. (outline-next-heading))
  21535. (if (or (eobp) (< (funcall outline-level) level))
  21536. nil
  21537. (point))))
  21538. (defun org-get-last-sibling ()
  21539. "Move to previous heading of the same level, and return point.
  21540. If there is no such heading, return nil."
  21541. (let ((opoint (point))
  21542. (level (funcall outline-level)))
  21543. (outline-previous-heading)
  21544. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21545. (while (and (> (funcall outline-level) level)
  21546. (not (bobp)))
  21547. (outline-previous-heading))
  21548. (if (< (funcall outline-level) level)
  21549. nil
  21550. (point)))))
  21551. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21552. "Goto to the end of a subtree."
  21553. ;; This contains an exact copy of the original function, but it uses
  21554. ;; `org-back-to-heading', to make it work also in invisible
  21555. ;; trees. And is uses an invisible-ok argument.
  21556. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21557. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21558. ;; with many children and grandchildren etc, this can be much faster
  21559. ;; than the outline version.
  21560. (org-back-to-heading invisible-ok)
  21561. (let ((first t)
  21562. (level (funcall outline-level)))
  21563. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21564. ;; A true heading (not a plain list item), in Org-mode
  21565. ;; This means we can easily find the end by looking
  21566. ;; only for the right number of stars. Using a regexp to do
  21567. ;; this is so much faster than using a Lisp loop.
  21568. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21569. (forward-char 1)
  21570. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21571. ;; something else, do it the slow way
  21572. (while (and (not (eobp))
  21573. (or first (> (funcall outline-level) level)))
  21574. (setq first nil)
  21575. (outline-next-heading)))
  21576. (unless to-heading
  21577. (if (memq (preceding-char) '(?\n ?\^M))
  21578. (progn
  21579. ;; Go to end of line before heading
  21580. (forward-char -1)
  21581. (if (memq (preceding-char) '(?\n ?\^M))
  21582. ;; leave blank line before heading
  21583. (forward-char -1))))))
  21584. (point))
  21585. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21586. "Use Org version in org-mode, for dramatic speed-up."
  21587. (if (derived-mode-p 'org-mode)
  21588. (progn
  21589. (org-end-of-subtree nil t)
  21590. (unless (eobp) (backward-char 1)))
  21591. ad-do-it))
  21592. (defun org-end-of-meta-data-and-drawers ()
  21593. "Jump to the first text after meta data and drawers in the current entry.
  21594. This will move over empty lines, lines with planning time stamps,
  21595. clocking lines, and drawers."
  21596. (org-back-to-heading t)
  21597. (let ((end (save-excursion (outline-next-heading) (point)))
  21598. (re (concat "\\(" org-drawer-regexp "\\)"
  21599. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21600. (forward-line 1)
  21601. (while (re-search-forward re end t)
  21602. (if (not (match-end 1))
  21603. ;; empty or planning line
  21604. (forward-line 1)
  21605. ;; a drawer, find the end
  21606. (re-search-forward "^[ \t]*:END:" end 'move)
  21607. (forward-line 1)))
  21608. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21609. (point)))
  21610. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21611. "Move forward to the ARG'th subheading at same level as this one.
  21612. Stop at the first and last subheadings of a superior heading.
  21613. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21614. non-nil it will also look at invisible ones."
  21615. (interactive "p")
  21616. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21617. (if (and arg (< arg 0))
  21618. (goto-char (point-min))
  21619. (outline-next-heading))
  21620. (org-at-heading-p)
  21621. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21622. (f (if (and arg (< arg 0))
  21623. 're-search-backward
  21624. 're-search-forward))
  21625. (count (if arg (abs arg) 1))
  21626. (result (point)))
  21627. (while (and (prog1 (> count 0)
  21628. (forward-char (if (and arg (< arg 0)) -1 1)))
  21629. (funcall f org-outline-regexp-bol nil 'move))
  21630. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21631. (cond ((< l level) (setq count 0))
  21632. ((and (= l level)
  21633. (or invisible-ok
  21634. (progn
  21635. (goto-char (line-beginning-position))
  21636. (not (outline-invisible-p)))))
  21637. (setq count (1- count))
  21638. (when (eq l level)
  21639. (setq result (point)))))))
  21640. (goto-char result))
  21641. (beginning-of-line 1)))
  21642. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21643. "Move backward to the ARG'th subheading at same level as this one.
  21644. Stop at the first and last subheadings of a superior heading."
  21645. (interactive "p")
  21646. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21647. (defun org-next-block (arg &optional backward block-regexp)
  21648. "Jump to the next block.
  21649. With a prefix argument ARG, jump forward ARG many source blocks.
  21650. When BACKWARD is non-nil, jump to the previous block.
  21651. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21652. (interactive "p")
  21653. (let ((re (or block-regexp org-block-regexp))
  21654. (re-search-fn (or (and backward 're-search-backward)
  21655. 're-search-forward)))
  21656. (if (looking-at re) (forward-char 1))
  21657. (condition-case nil
  21658. (funcall re-search-fn re nil nil arg)
  21659. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21660. (goto-char (match-beginning 0)) (org-show-context)))
  21661. (defun org-previous-block (arg &optional block-regexp)
  21662. "Jump to the previous block.
  21663. With a prefix argument ARG, jump backward ARG many source blocks.
  21664. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21665. (interactive "p")
  21666. (org-next-block arg t block-regexp))
  21667. (defun org-forward-paragraph ()
  21668. "Move forward to beginning of next paragraph or equivalent.
  21669. The function moves point to the beginning of the next visible
  21670. structural element, which can be a paragraph, a table, a list
  21671. item, etc. It also provides some special moves for convenience:
  21672. - On an affiliated keyword, jump to the beginning of the
  21673. relative element.
  21674. - On an item or a footnote definition, move to the second
  21675. element inside, if any.
  21676. - On a table or a property drawer, jump after it.
  21677. - On a verse or source block, stop after blank lines."
  21678. (interactive)
  21679. (when (eobp) (user-error "Cannot move further down"))
  21680. (let* ((deactivate-mark nil)
  21681. (element (org-element-at-point))
  21682. (type (org-element-type element))
  21683. (post-affiliated (org-element-property :post-affiliated element))
  21684. (contents-begin (org-element-property :contents-begin element))
  21685. (contents-end (org-element-property :contents-end element))
  21686. (end (let ((end (org-element-property :end element)) (parent element))
  21687. (while (and (setq parent (org-element-property :parent parent))
  21688. (= (org-element-property :contents-end parent) end))
  21689. (setq end (org-element-property :end parent)))
  21690. end)))
  21691. (cond ((not element)
  21692. (skip-chars-forward " \r\t\n")
  21693. (or (eobp) (beginning-of-line)))
  21694. ;; On affiliated keywords, move to element's beginning.
  21695. ((and post-affiliated (< (point) post-affiliated))
  21696. (goto-char post-affiliated))
  21697. ;; At a table row, move to the end of the table. Similarly,
  21698. ;; at a node property, move to the end of the property
  21699. ;; drawer.
  21700. ((memq type '(node-property table-row))
  21701. (goto-char (org-element-property
  21702. :end (org-element-property :parent element))))
  21703. ((memq type '(property-drawer table)) (goto-char end))
  21704. ;; Consider blank lines as separators in verse and source
  21705. ;; blocks to ease editing.
  21706. ((memq type '(src-block verse-block))
  21707. (when (eq type 'src-block)
  21708. (setq contents-end
  21709. (save-excursion (goto-char end)
  21710. (skip-chars-backward " \r\t\n")
  21711. (line-beginning-position))))
  21712. (beginning-of-line)
  21713. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  21714. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  21715. (goto-char end)
  21716. (skip-chars-forward " \r\t\n")
  21717. (if (= (point) contents-end) (goto-char end)
  21718. (beginning-of-line))))
  21719. ;; With no contents, just skip element.
  21720. ((not contents-begin) (goto-char end))
  21721. ;; If contents are invisible, skip the element altogether.
  21722. ((outline-invisible-p (line-end-position))
  21723. (case type
  21724. (headline
  21725. (org-with-limited-levels (outline-next-visible-heading 1)))
  21726. ;; At a plain list, make sure we move to the next item
  21727. ;; instead of skipping the whole list.
  21728. (plain-list (forward-char)
  21729. (org-forward-paragraph))
  21730. (otherwise (goto-char end))))
  21731. ((>= (point) contents-end) (goto-char end))
  21732. ((>= (point) contents-begin)
  21733. ;; This can only happen on paragraphs and plain lists.
  21734. (case type
  21735. (paragraph (goto-char end))
  21736. ;; At a plain list, try to move to second element in
  21737. ;; first item, if possible.
  21738. (plain-list (end-of-line)
  21739. (org-forward-paragraph))))
  21740. ;; When contents start on the middle of a line (e.g. in
  21741. ;; items and footnote definitions), try to reach first
  21742. ;; element starting after current line.
  21743. ((> (line-end-position) contents-begin)
  21744. (end-of-line)
  21745. (org-forward-paragraph))
  21746. (t (goto-char contents-begin)))))
  21747. (defun org-backward-paragraph ()
  21748. "Move backward to start of previous paragraph or equivalent.
  21749. The function moves point to the beginning of the current
  21750. structural element, which can be a paragraph, a table, a list
  21751. item, etc., or to the beginning of the previous visible one if
  21752. point is already there. It also provides some special moves for
  21753. convenience:
  21754. - On an affiliated keyword, jump to the first one.
  21755. - On a table or a property drawer, move to its beginning.
  21756. - On a verse or source block, stop before blank lines."
  21757. (interactive)
  21758. (when (bobp) (user-error "Cannot move further up"))
  21759. (let* ((deactivate-mark nil)
  21760. (element (org-element-at-point))
  21761. (type (org-element-type element))
  21762. (contents-begin (org-element-property :contents-begin element))
  21763. (contents-end (org-element-property :contents-end element))
  21764. (post-affiliated (org-element-property :post-affiliated element))
  21765. (begin (org-element-property :begin element)))
  21766. (cond
  21767. ((not element) (goto-char (point-min)))
  21768. ((= (point) begin)
  21769. (backward-char)
  21770. (org-backward-paragraph))
  21771. ((and post-affiliated (<= (point) post-affiliated)) (goto-char begin))
  21772. ((memq type '(node-property table-row))
  21773. (goto-char (org-element-property
  21774. :post-affiliated (org-element-property :parent element))))
  21775. ((memq type '(property-drawer table)) (goto-char begin))
  21776. ((memq type '(src-block verse-block))
  21777. (when (eq type 'src-block)
  21778. (setq contents-begin
  21779. (save-excursion (goto-char begin) (forward-line) (point))))
  21780. (if (= (point) contents-begin) (goto-char post-affiliated)
  21781. ;; Inside a verse block, see blank lines as paragraph
  21782. ;; separators.
  21783. (let ((origin (point)))
  21784. (skip-chars-backward " \r\t\n" contents-begin)
  21785. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  21786. (skip-chars-forward " \r\t\n" origin)
  21787. (if (= (point) origin) (goto-char contents-begin)
  21788. (beginning-of-line))))))
  21789. ((not contents-begin) (goto-char (or post-affiliated begin)))
  21790. ((eq type 'paragraph)
  21791. (goto-char contents-begin)
  21792. ;; When at first paragraph in an item or a footnote definition,
  21793. ;; move directly to beginning of line.
  21794. (let ((parent-contents
  21795. (org-element-property
  21796. :contents-begin (org-element-property :parent element))))
  21797. (when (and parent-contents (= parent-contents contents-begin))
  21798. (beginning-of-line))))
  21799. ;; At the end of a greater element, move to the beginning of the
  21800. ;; last element within.
  21801. ((>= (point) contents-end)
  21802. (goto-char (1- contents-end))
  21803. (org-backward-paragraph))
  21804. (t (goto-char (or post-affiliated begin))))
  21805. ;; Ensure we never leave point invisible.
  21806. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  21807. (defun org-forward-element ()
  21808. "Move forward by one element.
  21809. Move to the next element at the same level, when possible."
  21810. (interactive)
  21811. (cond ((eobp) (user-error "Cannot move further down"))
  21812. ((org-with-limited-levels (org-at-heading-p))
  21813. (let ((origin (point)))
  21814. (goto-char (org-end-of-subtree nil t))
  21815. (unless (org-with-limited-levels (org-at-heading-p))
  21816. (goto-char origin)
  21817. (user-error "Cannot move further down"))))
  21818. (t
  21819. (let* ((elem (org-element-at-point))
  21820. (end (org-element-property :end elem))
  21821. (parent (org-element-property :parent elem)))
  21822. (cond ((and parent (= (org-element-property :contents-end parent) end))
  21823. (goto-char (org-element-property :end parent)))
  21824. ((integer-or-marker-p end) (goto-char end))
  21825. (t (message "No element at point")))))))
  21826. (defun org-backward-element ()
  21827. "Move backward by one element.
  21828. Move to the previous element at the same level, when possible."
  21829. (interactive)
  21830. (cond ((bobp) (user-error "Cannot move further up"))
  21831. ((org-with-limited-levels (org-at-heading-p))
  21832. ;; At a headline, move to the previous one, if any, or stay
  21833. ;; here.
  21834. (let ((origin (point)))
  21835. (org-with-limited-levels (org-backward-heading-same-level 1))
  21836. ;; When current headline has no sibling above, move to its
  21837. ;; parent.
  21838. (when (= (point) origin)
  21839. (or (org-with-limited-levels (org-up-heading-safe))
  21840. (progn (goto-char origin)
  21841. (user-error "Cannot move further up"))))))
  21842. (t
  21843. (let* ((elem (org-element-at-point))
  21844. (beg (org-element-property :begin elem)))
  21845. (cond
  21846. ;; Move to beginning of current element if point isn't
  21847. ;; there already.
  21848. ((null beg) (message "No element at point"))
  21849. ((/= (point) beg) (goto-char beg))
  21850. (t (goto-char beg)
  21851. (skip-chars-backward " \r\t\n")
  21852. (unless (bobp)
  21853. (let ((prev (org-element-at-point)))
  21854. (goto-char (org-element-property :begin prev))
  21855. (while (and (setq prev (org-element-property :parent prev))
  21856. (<= (org-element-property :end prev) beg))
  21857. (goto-char (org-element-property :begin prev)))))))))))
  21858. (defun org-up-element ()
  21859. "Move to upper element."
  21860. (interactive)
  21861. (if (org-with-limited-levels (org-at-heading-p))
  21862. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21863. (let* ((elem (org-element-at-point))
  21864. (parent (org-element-property :parent elem)))
  21865. (if parent (goto-char (org-element-property :begin parent))
  21866. (if (org-with-limited-levels (org-before-first-heading-p))
  21867. (user-error "No surrounding element")
  21868. (org-with-limited-levels (org-back-to-heading)))))))
  21869. (defvar org-element-greater-elements)
  21870. (defun org-down-element ()
  21871. "Move to inner element."
  21872. (interactive)
  21873. (let ((element (org-element-at-point)))
  21874. (cond
  21875. ((memq (org-element-type element) '(plain-list table))
  21876. (goto-char (org-element-property :contents-begin element))
  21877. (forward-char))
  21878. ((memq (org-element-type element) org-element-greater-elements)
  21879. ;; If contents are hidden, first disclose them.
  21880. (when (outline-invisible-p (line-end-position)) (org-cycle))
  21881. (goto-char (or (org-element-property :contents-begin element)
  21882. (user-error "No content for this element"))))
  21883. (t (user-error "No inner element")))))
  21884. (defun org-drag-element-backward ()
  21885. "Move backward element at point."
  21886. (interactive)
  21887. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21888. (let* ((elem (org-element-at-point))
  21889. (prev-elem
  21890. (save-excursion
  21891. (goto-char (org-element-property :begin elem))
  21892. (skip-chars-backward " \r\t\n")
  21893. (unless (bobp)
  21894. (let* ((beg (org-element-property :begin elem))
  21895. (prev (org-element-at-point))
  21896. (up prev))
  21897. (while (and (setq up (org-element-property :parent up))
  21898. (<= (org-element-property :end prev) beg))
  21899. (setq prev up))
  21900. prev)))))
  21901. ;; Error out if no previous element or previous element is
  21902. ;; a parent of the current one.
  21903. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21904. (user-error "Cannot drag element backward")
  21905. (let ((pos (point)))
  21906. (org-element-swap-A-B prev-elem elem)
  21907. (goto-char (+ (org-element-property :begin prev-elem)
  21908. (- pos (org-element-property :begin elem)))))))))
  21909. (defun org-drag-element-forward ()
  21910. "Move forward element at point."
  21911. (interactive)
  21912. (let* ((pos (point))
  21913. (elem (org-element-at-point)))
  21914. (when (= (point-max) (org-element-property :end elem))
  21915. (user-error "Cannot drag element forward"))
  21916. (goto-char (org-element-property :end elem))
  21917. (let ((next-elem (org-element-at-point)))
  21918. (when (or (org-element-nested-p elem next-elem)
  21919. (and (eq (org-element-type next-elem) 'headline)
  21920. (not (eq (org-element-type elem) 'headline))))
  21921. (goto-char pos)
  21922. (user-error "Cannot drag element forward"))
  21923. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21924. ;; body's length (without final blanks) and by the length of
  21925. ;; blanks between ELEM and NEXT-ELEM.
  21926. (let ((size-next (- (save-excursion
  21927. (goto-char (org-element-property :end next-elem))
  21928. (skip-chars-backward " \r\t\n")
  21929. (forward-line)
  21930. ;; Small correction if buffer doesn't end
  21931. ;; with a newline character.
  21932. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21933. (org-element-property :begin next-elem)))
  21934. (size-blank (- (org-element-property :end elem)
  21935. (save-excursion
  21936. (goto-char (org-element-property :end elem))
  21937. (skip-chars-backward " \r\t\n")
  21938. (forward-line)
  21939. (point)))))
  21940. (org-element-swap-A-B elem next-elem)
  21941. (goto-char (+ pos size-next size-blank))))))
  21942. (defun org-drag-line-forward (arg)
  21943. "Drag the line at point ARG lines forward."
  21944. (interactive "p")
  21945. (dotimes (n (abs arg))
  21946. (let ((c (current-column)))
  21947. (if (< 0 arg)
  21948. (progn
  21949. (beginning-of-line 2)
  21950. (transpose-lines 1)
  21951. (beginning-of-line 0))
  21952. (transpose-lines 1)
  21953. (beginning-of-line -1))
  21954. (org-move-to-column c))))
  21955. (defun org-drag-line-backward (arg)
  21956. "Drag the line at point ARG lines backward."
  21957. (interactive "p")
  21958. (org-drag-line-forward (- arg)))
  21959. (defun org-mark-element ()
  21960. "Put point at beginning of this element, mark at end.
  21961. Interactively, if this command is repeated or (in Transient Mark
  21962. mode) if the mark is active, it marks the next element after the
  21963. ones already marked."
  21964. (interactive)
  21965. (let (deactivate-mark)
  21966. (if (and (org-called-interactively-p 'any)
  21967. (or (and (eq last-command this-command) (mark t))
  21968. (and transient-mark-mode mark-active)))
  21969. (set-mark
  21970. (save-excursion
  21971. (goto-char (mark))
  21972. (goto-char (org-element-property :end (org-element-at-point)))))
  21973. (let ((element (org-element-at-point)))
  21974. (end-of-line)
  21975. (push-mark (org-element-property :end element) t t)
  21976. (goto-char (org-element-property :begin element))))))
  21977. (defun org-narrow-to-element ()
  21978. "Narrow buffer to current element."
  21979. (interactive)
  21980. (let ((elem (org-element-at-point)))
  21981. (cond
  21982. ((eq (car elem) 'headline)
  21983. (narrow-to-region
  21984. (org-element-property :begin elem)
  21985. (org-element-property :end elem)))
  21986. ((memq (car elem) org-element-greater-elements)
  21987. (narrow-to-region
  21988. (org-element-property :contents-begin elem)
  21989. (org-element-property :contents-end elem)))
  21990. (t
  21991. (narrow-to-region
  21992. (org-element-property :begin elem)
  21993. (org-element-property :end elem))))))
  21994. (defun org-transpose-element ()
  21995. "Transpose current and previous elements, keeping blank lines between.
  21996. Point is moved after both elements."
  21997. (interactive)
  21998. (org-skip-whitespace)
  21999. (let ((end (org-element-property :end (org-element-at-point))))
  22000. (org-drag-element-backward)
  22001. (goto-char end)))
  22002. (defun org-unindent-buffer ()
  22003. "Un-indent the visible part of the buffer.
  22004. Relative indentation (between items, inside blocks, etc.) isn't
  22005. modified."
  22006. (interactive)
  22007. (unless (eq major-mode 'org-mode)
  22008. (user-error "Cannot un-indent a buffer not in Org mode"))
  22009. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22010. unindent-tree ; For byte-compiler.
  22011. (unindent-tree
  22012. (function
  22013. (lambda (contents)
  22014. (mapc
  22015. (lambda (element)
  22016. (if (memq (org-element-type element) '(headline section))
  22017. (funcall unindent-tree (org-element-contents element))
  22018. (save-excursion
  22019. (save-restriction
  22020. (narrow-to-region
  22021. (org-element-property :begin element)
  22022. (org-element-property :end element))
  22023. (org-do-remove-indentation)))))
  22024. (reverse contents))))))
  22025. (funcall unindent-tree (org-element-contents parse-tree))))
  22026. (defun org-show-subtree ()
  22027. "Show everything after this heading at deeper levels."
  22028. (interactive)
  22029. (outline-flag-region
  22030. (point)
  22031. (save-excursion
  22032. (org-end-of-subtree t t))
  22033. nil))
  22034. (defun org-show-entry ()
  22035. "Show the body directly following this heading.
  22036. Show the heading too, if it is currently invisible."
  22037. (interactive)
  22038. (save-excursion
  22039. (condition-case nil
  22040. (progn
  22041. (org-back-to-heading t)
  22042. (outline-flag-region
  22043. (max (point-min) (1- (point)))
  22044. (save-excursion
  22045. (if (re-search-forward
  22046. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22047. (match-beginning 1)
  22048. (point-max)))
  22049. nil)
  22050. (org-cycle-hide-drawers 'children))
  22051. (error nil))))
  22052. (defun org-make-options-regexp (kwds &optional extra)
  22053. "Make a regular expression for keyword lines."
  22054. (concat
  22055. "^#\\+\\("
  22056. (mapconcat 'regexp-quote kwds "\\|")
  22057. (if extra (concat "\\|" extra))
  22058. "\\):[ \t]*\\(.*\\)"))
  22059. ;; Make isearch reveal the necessary context
  22060. (defun org-isearch-end ()
  22061. "Reveal context after isearch exits."
  22062. (when isearch-success ; only if search was successful
  22063. (if (featurep 'xemacs)
  22064. ;; Under XEmacs, the hook is run in the correct place,
  22065. ;; we directly show the context.
  22066. (org-show-context 'isearch)
  22067. ;; In Emacs the hook runs *before* restoring the overlays.
  22068. ;; So we have to use a one-time post-command-hook to do this.
  22069. ;; (Emacs 22 has a special variable, see function `org-mode')
  22070. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22071. isearch-mode-end-hook-quit)
  22072. ;; Only when the isearch was not quitted.
  22073. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22074. 'append 'local)))
  22075. (org-fix-ellipsis-at-bol)))
  22076. (defun org-isearch-post-command ()
  22077. "Remove self from hook, and show context."
  22078. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22079. (org-show-context 'isearch))
  22080. ;;;; Integration with and fixes for other packages
  22081. ;;; Imenu support
  22082. (defvar org-imenu-markers nil
  22083. "All markers currently used by Imenu.")
  22084. (make-variable-buffer-local 'org-imenu-markers)
  22085. (defun org-imenu-new-marker (&optional pos)
  22086. "Return a new marker for use by Imenu, and remember the marker."
  22087. (let ((m (make-marker)))
  22088. (move-marker m (or pos (point)))
  22089. (push m org-imenu-markers)
  22090. m))
  22091. (defun org-imenu-get-tree ()
  22092. "Produce the index for Imenu."
  22093. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22094. (setq org-imenu-markers nil)
  22095. (let* ((n org-imenu-depth)
  22096. (re (concat "^" (org-get-limited-outline-regexp)))
  22097. (subs (make-vector (1+ n) nil))
  22098. (last-level 0)
  22099. m level head0 head)
  22100. (save-excursion
  22101. (save-restriction
  22102. (widen)
  22103. (goto-char (point-max))
  22104. (while (re-search-backward re nil t)
  22105. (setq level (org-reduced-level (funcall outline-level)))
  22106. (when (and (<= level n)
  22107. (looking-at org-complex-heading-regexp)
  22108. (setq head0 (org-match-string-no-properties 4)))
  22109. (setq head (org-link-display-format head0)
  22110. m (org-imenu-new-marker))
  22111. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22112. (if (>= level last-level)
  22113. (push (cons head m) (aref subs level))
  22114. (push (cons head (aref subs (1+ level))) (aref subs level))
  22115. (loop for i from (1+ level) to n do (aset subs i nil)))
  22116. (setq last-level level)))))
  22117. (aref subs 1)))
  22118. (eval-after-load "imenu"
  22119. '(progn
  22120. (add-hook 'imenu-after-jump-hook
  22121. (lambda ()
  22122. (if (derived-mode-p 'org-mode)
  22123. (org-show-context 'org-goto))))))
  22124. (defun org-link-display-format (link)
  22125. "Replace a link with its the description.
  22126. If there is no description, use the link target."
  22127. (save-match-data
  22128. (if (string-match org-bracket-link-analytic-regexp link)
  22129. (replace-match (if (match-end 5)
  22130. (match-string 5 link)
  22131. (concat (match-string 1 link)
  22132. (match-string 3 link)))
  22133. nil t link)
  22134. link)))
  22135. (defun org-toggle-link-display ()
  22136. "Toggle the literal or descriptive display of links."
  22137. (interactive)
  22138. (if org-descriptive-links
  22139. (progn (org-remove-from-invisibility-spec '(org-link))
  22140. (org-restart-font-lock)
  22141. (setq org-descriptive-links nil))
  22142. (progn (add-to-invisibility-spec '(org-link))
  22143. (org-restart-font-lock)
  22144. (setq org-descriptive-links t))))
  22145. ;; Speedbar support
  22146. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22147. "Overlay marking the agenda restriction line in speedbar.")
  22148. (overlay-put org-speedbar-restriction-lock-overlay
  22149. 'face 'org-agenda-restriction-lock)
  22150. (overlay-put org-speedbar-restriction-lock-overlay
  22151. 'help-echo "Agendas are currently limited to this item.")
  22152. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22153. (defun org-speedbar-set-agenda-restriction ()
  22154. "Restrict future agenda commands to the location at point in speedbar.
  22155. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22156. (interactive)
  22157. (require 'org-agenda)
  22158. (let (p m tp np dir txt)
  22159. (cond
  22160. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22161. 'org-imenu t))
  22162. (setq m (get-text-property p 'org-imenu-marker))
  22163. (with-current-buffer (marker-buffer m)
  22164. (goto-char m)
  22165. (org-agenda-set-restriction-lock 'subtree)))
  22166. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22167. 'speedbar-function 'speedbar-find-file))
  22168. (setq tp (previous-single-property-change
  22169. (1+ p) 'speedbar-function)
  22170. np (next-single-property-change
  22171. tp 'speedbar-function)
  22172. dir (speedbar-line-directory)
  22173. txt (buffer-substring-no-properties (or tp (point-min))
  22174. (or np (point-max))))
  22175. (with-current-buffer (find-file-noselect
  22176. (let ((default-directory dir))
  22177. (expand-file-name txt)))
  22178. (unless (derived-mode-p 'org-mode)
  22179. (user-error "Cannot restrict to non-Org-mode file"))
  22180. (org-agenda-set-restriction-lock 'file)))
  22181. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22182. (move-overlay org-speedbar-restriction-lock-overlay
  22183. (point-at-bol) (point-at-eol))
  22184. (setq current-prefix-arg nil)
  22185. (org-agenda-maybe-redo)))
  22186. (defvar speedbar-file-key-map)
  22187. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22188. (eval-after-load "speedbar"
  22189. '(progn
  22190. (speedbar-add-supported-extension ".org")
  22191. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22192. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22193. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22194. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22195. (add-hook 'speedbar-visiting-tag-hook
  22196. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22197. ;;; Fixes and Hacks for problems with other packages
  22198. (defun org-mode-flyspell-verify ()
  22199. "Function used for `flyspell-generic-check-word-predicate'."
  22200. (if (org-at-heading-p)
  22201. ;; At a headline or an inlinetask, check title only. This is
  22202. ;; faster than relying on `org-element-at-point'.
  22203. (and (save-excursion (beginning-of-line)
  22204. (and (let ((case-fold-search t))
  22205. (not (looking-at "\\*+ END[ \t]*$")))
  22206. (looking-at org-complex-heading-regexp)))
  22207. (match-beginning 4)
  22208. (>= (point) (match-beginning 4))
  22209. (or (not (match-beginning 5))
  22210. (< (point) (match-beginning 5))))
  22211. (let* ((element (org-element-at-point))
  22212. (post-affiliated (org-element-property :post-affiliated element))
  22213. (object-check
  22214. (function
  22215. ;; Non-nil if checks can be done for object at point.
  22216. (lambda ()
  22217. (let ((object (save-excursion
  22218. (when (org-looking-at-p "\\>") (backward-char))
  22219. (org-element-context element))))
  22220. (case (org-element-type object)
  22221. ;; Prevent checks in links due to keybinding conflict
  22222. ;; with Flyspell.
  22223. ((code entity export-snippet inline-babel-call
  22224. inline-src-block line-break latex-fragment link macro
  22225. statistics-cookie target timestamp verbatim)
  22226. nil)
  22227. (footnote-reference
  22228. ;; Only in inline footnotes, within the definition.
  22229. (and (eq (org-element-property :type object) 'inline)
  22230. (< (save-excursion
  22231. (goto-char (org-element-property :begin object))
  22232. (search-forward ":" nil t 2))
  22233. (point))))
  22234. (otherwise t)))))))
  22235. (cond
  22236. ;; Ignore checks in all affiliated keywords but captions.
  22237. ((and post-affiliated (< (point) post-affiliated))
  22238. (and (save-excursion
  22239. (beginning-of-line)
  22240. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22241. (> (point) (match-end 0))
  22242. (funcall object-check)))
  22243. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22244. ((and org-log-into-drawer
  22245. (let ((log (or (org-string-nw-p org-log-into-drawer) "LOGBOOK"))
  22246. (parent element))
  22247. (while (and parent (not (eq (org-element-type parent) 'drawer)))
  22248. (setq parent (org-element-property :parent parent)))
  22249. (and parent
  22250. (eq (compare-strings
  22251. log nil nil
  22252. (org-element-property :drawer-name parent) nil nil t)
  22253. t))))
  22254. nil)
  22255. (t
  22256. (case (org-element-type element)
  22257. ((comment quote-section) t)
  22258. (comment-block
  22259. ;; Allow checks between block markers, not on them.
  22260. (and (> (line-beginning-position)
  22261. (org-element-property :post-affiliated element))
  22262. (save-excursion
  22263. (end-of-line)
  22264. (skip-chars-forward " \r\t\n")
  22265. (< (point) (org-element-property :end element)))))
  22266. ;; Arbitrary list of keywords where checks are meaningful.
  22267. ;; Make sure point is on the value part of the element.
  22268. (keyword
  22269. (and (member (org-element-property :key element)
  22270. '("DESCRIPTION" "TITLE"))
  22271. (< (save-excursion
  22272. (beginning-of-line) (search-forward ":") (point))
  22273. (point))))
  22274. ;; Check is globally allowed in paragraphs verse blocks and
  22275. ;; table rows (after affiliated keywords) but some objects
  22276. ;; must not be affected.
  22277. ((paragraph table-row verse-block)
  22278. (and (>= (point) (org-element-property :contents-begin element))
  22279. (< (point) (org-element-property :contents-end element))
  22280. (funcall object-check)))))))))
  22281. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22282. (defun org-remove-flyspell-overlays-in (beg end)
  22283. "Remove flyspell overlays in region."
  22284. (and (org-bound-and-true-p flyspell-mode)
  22285. (fboundp 'flyspell-delete-region-overlays)
  22286. (flyspell-delete-region-overlays beg end)))
  22287. (defvar flyspell-delayed-commands)
  22288. (eval-after-load "flyspell"
  22289. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22290. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22291. (eval-after-load "bookmark"
  22292. '(if (boundp 'bookmark-after-jump-hook)
  22293. ;; We can use the hook
  22294. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22295. ;; Hook not available, use advice
  22296. (defadvice bookmark-jump (after org-make-visible activate)
  22297. "Make the position visible."
  22298. (org-bookmark-jump-unhide))))
  22299. ;; Make sure saveplace shows the location if it was hidden
  22300. (eval-after-load "saveplace"
  22301. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22302. "Make the position visible."
  22303. (org-bookmark-jump-unhide)))
  22304. ;; Make sure ecb shows the location if it was hidden
  22305. (eval-after-load "ecb"
  22306. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22307. "Make hierarchy visible when jumping into location from ECB tree buffer."
  22308. (if (derived-mode-p 'org-mode)
  22309. (org-show-context))))
  22310. (defun org-bookmark-jump-unhide ()
  22311. "Unhide the current position, to show the bookmark location."
  22312. (and (derived-mode-p 'org-mode)
  22313. (or (outline-invisible-p)
  22314. (save-excursion (goto-char (max (point-min) (1- (point))))
  22315. (outline-invisible-p)))
  22316. (org-show-context 'bookmark-jump)))
  22317. ;; Make session.el ignore our circular variable
  22318. (defvar session-globals-exclude)
  22319. (eval-after-load "session"
  22320. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  22321. ;;;; Finish up
  22322. (provide 'org)
  22323. (run-hooks 'org-load-hook)
  22324. ;;; org.el ends here